cvs diff: Diffing . Index: DataGrid.php =================================================================== RCS file: /repository/pear/Structures_DataGrid/DataGrid.php,v retrieving revision 1.87 diff -u -r1.87 DataGrid.php --- DataGrid.php 29 Jun 2006 12:55:39 -0000 1.87 +++ DataGrid.php 29 Jun 2006 18:22:50 -0000 @@ -365,6 +365,10 @@ return $error; } + if (is_array($type)) { + list($type, $options_detected) = $type; + $options = array_merge($options, $options_detected); + } $className = "Structures_DataGrid_DataSource_$type"; if (PEAR::isError($driver =& $this->loadDriver($className))) { @@ -1036,6 +1040,18 @@ } /** + * Get DataSource + * + * Retrieves the dataSource driver object as a reference + * + * @access public + */ + function &getDataSource() + { + return isset($this->_dataSource) ? $this->_dataSource : false; + } + + /** * Request the datasource to sort its data * * @return void @@ -1256,9 +1272,32 @@ break; // CSV - //case is_string($source): - // return DATAGRID_SOURCE_CSV; - // break; + case is_string($source): + // Get first 3 rows, remove quoted strings + $lines = explode("\n", $source, 4); + unset($lines[3]); + for ($i=0; $i<3; $i++) { + $lines[$i] = str_replace('""', '', $lines[$i]); + $lines[$i] = preg_replace('/"[^"]*"/', '', $lines[$i]); + } + // Search delimiter appearances in lines + $delimiters = array(',', ';', "\t"); + $appearances = array(); + foreach ($delimiters as $delimiter) { + $count = array(); + for ($i=0; $i<3; $i++) { + $count[$i] = substr_count($lines[$i], $delimiter); + } + if ($count[0] == $count[1] && $count[1] == $count[2]) { + $appearances[$delimiter] = $count[0]; + } + } + if (!empty($appearances)) { + arsort($appearances); + $delimiters_detected = array_keys($appearances); + return array(DATAGRID_SOURCE_CSV, array('delimiter' => $delimiters_detected[0])); + } + break; default: return null; cvs diff: Diffing DataGrid Index: DataGrid/DataSource.php =================================================================== RCS file: /repository/pear/Structures_DataGrid/DataGrid/DataSource.php,v retrieving revision 1.20 diff -u -r1.20 DataSource.php --- DataGrid/DataSource.php 28 May 2006 02:18:40 -0000 1.20 +++ DataGrid/DataSource.php 29 Jun 2006 18:25:57 -0000 @@ -201,6 +201,22 @@ return $columns; } + + /** + * Get column labels + * + * @access public + */ + function getLabels() + { + $labels = $this->_options['labels']; + if (!$labels && $this->_options['fields']) { + foreach ($this->_options['fields'] as $field) { + $labels[$field] = $field; + } + } + return $labels; + } // Begin driver method prototypes DocBook template @@ -297,7 +313,32 @@ /**#@-*/ // End DocBook template - + + /** + * Retrieve driver type + * + * @return string Driver type + * @access public + */ + function getType() + { + return strtoupper( + str_replace("structures_datagrid_datasource_", "", + strtolower(get_class($this)))); + } + + /** + * Retrieve driver option + * + * @param string $name Option name + * @return mixed Option value if option is set; false if not + * @access public + */ + function getOption($name) + { + return isset($this->_options[$name]) ? $this->_options[$name] : false; + } + /** * List special driver features * cvs diff: Diffing DataGrid/DataSource Index: DataGrid/DataSource/XML.php =================================================================== RCS file: /repository/pear/Structures_DataGrid/DataGrid/DataSource/XML.php,v retrieving revision 1.8 diff -u -r1.8 XML.php --- DataGrid/DataSource/XML.php 23 Apr 2006 18:45:10 -0000 1.8 +++ DataGrid/DataSource/XML.php 29 Jun 2006 18:29:07 -0000 @@ -28,6 +28,8 @@ * This driver accepts the following options: * * "xpath": XPath to a subset of the XML data. + * "fieldAttribute": Attribute in XML source to be used as column field name. + * "labelAttribute": Attribute in XML source to be used as column label. * * @package Structures_DataGrid * @author Olivier Guilyardi @@ -44,7 +46,13 @@ function Structures_DataGrid_DataSource_XML() { parent::Structures_DataGrid_DataSource_Array(); - $this->_addDefaultOptions(array('xpath' => '')); + $this->_addDefaultOptions( + array( + 'xpath' => '', + 'fieldAttribute' => null, + 'labelAttribute' => null + ) + ); } /** @@ -80,6 +88,10 @@ $unserializer = &new XML_Unserializer(); $unserializer->setOption('parseAttributes', true); $unserializer->setOption('attributesArray', 'attributes'); + $unserializer->setOption('contentName', 'content'); + if (!is_null($this->_options['fieldAttribute'])) { + $unserializer->setOption('keyAttribute', $this->_options['fieldAttribute']); + } // Unserialize the XML Data $test = $unserializer->unserialize($xml, false); @@ -99,11 +111,32 @@ 'You may want to set the \'xpath\' option.'); } - $this->_ar[] = $row; + $row_processed = array(); + foreach ($row as $item => $info) { + // Extract labels if not already set + if (!$this->_options['labels'] && !is_null($this->_options['labelAttribute'])) { + if (isset($info['attributes'][$this->_options['labelAttribute']])) { + $labels[$item] = $info['attributes'][$this->_options['labelAttribute']]; + } + else { + $labels[$item] = $item; + } + } + // Create required array structure + if (isset($info['content'])) $row_processed[$item] = $info['content']; + else if (is_string($info)) $row_processed[$item] = $info; + else $row_processed[$item] = ''; + } + // Set labels if extracted + if (isset($labels)) { + $this->setOption('labels', array_merge($labels, $this->_options['labels'])); + } + + $this->_ar[] = $row_processed; } if ($this->_ar and !$this->_options['fields']) { - $this->setOptions(array('fields' => array_keys($this->_ar[0]))); + $this->setOption('fields', array_keys($this->_ar[0])); } return true; cvs diff: Diffing DataGrid/Renderer cvs diff: Diffing docs cvs diff: Diffing docs/examples cvs diff: Diffing tools cvs diff: Diffing tools/manual-gen