Re: [Patch] Structures_DataGrid_DataSource_XML
| From: | Olivier Guilyardi | Date: | Wed, 28 Jun 2006 15:33:56 +0000 |
| Subject: | Re: [Patch] Structures_DataGrid_DataSource_XML | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-43164@lists.php.net to get a copy of this message | ||
Markus, please post a feature request.
Thanks
--
og
Markus Ernst wrote:
Hi After you introduced my Suggestion in the XML renderer yesterday I found that the XML datasource driver now did not behave consistent, as the exported data could not be loaded back into the same format. So I modified the datasource driver to accept 3 new options: - 'fieldAttribute' to assign the appropriate Attribute to the column field - 'labelAttribute' to assign the appropriate Attribute to the column label - 'dropAttributes' to change the recordset into the same form as if no attributes would be present in the XML data I am sure my approach is not the most elegant possible one - I tried to understand the XML serializer as much as I could - anyway it works in my application. I tried to act as requested in the PEAR contributing manual, anyway I could not get WinCVS to save a .diff file (I am sorry I just got to know CVS yesterday). So I copied the diff parts from the WinCVS log window into a text file; I hope this is useful for you. Otherwise I hope you don't mind finding the source at http://www.markusernst.ch/stuff_for_the_world/XML.zip or somebody can tell me how to make a proper .diff file with WinCVS... Thank you for your great work on Structures_DataGrid! Markus ------------------------------------------------------------------------ cvs diff -u (in directory C:\Dokumente und Einstellungen\ich\Desktop\Arbeit\z_code\_cvs\pear\Structures_DataGrid\) cvs diff: Diffing . cvs diff: Diffing DataGrid 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 28 Jun 2006 14:30:05 -0000 @@ -44,7 +44,14 @@function Structures_DataGrid_DataSource_XML() { parent::Structures_DataGrid_DataSource_Array(); - $this->_addDefaultOptions(array('xpath' => '')); + $this->_addDefaultOptions( + array( + 'xpath' => '', + 'fieldAttribute' => null, + 'labelAttribute' => null, + 'dropAttributes' => false + ) + ); } /**@@ -80,6 +87,9 @@$unserializer = &new XML_Unserializer(); $unserializer->setOption('parseAttributes', true); $unserializer->setOption('attributesArray', 'attributes'); + if (!is_null($this->_options['fieldAttribute'])) { + $unserializer->setOption('keyAttribute', $this->_options['fieldAttribute']); + } // Unserialize the XML Data $test = $unserializer->unserialize($xml, false);@@ -99,7 +109,30 @@'You may want to set the \'xpath\' option.'); } - $this->_ar[] = $row; + $r = ($this->_options['dropAttributes']) ? array() : $row; + 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[] = $info['attributes'][$this->_options['labelAttribute']]; + } + else { + $labels[] = $item; + } + } + // Drop attributes + if ($this->_options['dropAttributes']) { + if (isset($info['_content'])) $r[$item] = $info['_content']; + else if (is_string($info)) $r[$item] = $info; + else $r[$item] = ''; + } + } + // Set labels if extracted + if (isset($labels)) { + $this->setOptions(array('labels' => $labels)); + }++ $this->_ar[] = $r; } if ($this->_ar and !$this->_options['fields']) {cvs diff: Diffing DataGrid/Renderer ***** CVS exited normally with code 1 ***** cvs diff -u >Structures_DataGrid.diff (in directory C:\Dokumente und Einstellungen\ich\Desktop\Arbeit\z_code\_cvs\pear\Structures_DataGrid\) cvs diff: I know nothing about >Structures_DataGrid.diff ***** CVS exited normally with code 1 *****