XML_sql2xml
[ class tree: XML_sql2xml ] [ index: XML_sql2xml ] [ all elements ]

Source for file sql2xml_ext.php

Documentation is available at sql2xml_ext.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Christian Stocker <chregu@phant.ch>                         |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: sql2xml_ext.php,v 1.10 2008/03/23 19:08:34 dufuz Exp $
  19.  
  20. require_once 'XML/sql2xml.php';
  21.  
  22. /**
  23.  *  This class shows with an example, how the base sql2xml-class
  24.  *   could be extended.
  25.  *
  26.  * Usage example
  27.  *
  28.  * include_once("XML/sql2xml_ext.php");
  29.  * $options= array( user_options => array (xml_seperator =>"_",
  30.  *                                       element_id => "id"),
  31.  * );
  32.  * $sql2xml = new xml_sql2xml_ext("mysql://root@localhost/xmltest");
  33.  * $sql2xml->SetOptions($options);
  34.  * $xmlstring = $sql2xml->getxml("select * from bands");
  35.  
  36.  * more examples and outputs on
  37.  *   http://php.chregu.tv/sql2xml/
  38.  *   for the time being
  39.  *
  40.  * @author   Christian Stocker <chregu@nomad.ch>
  41.  * @version  $Id: sql2xml_ext.php,v 1.10 2008/03/23 19:08:34 dufuz Exp $
  42.  */
  43. class XML_sql2xml_ext extends XML_sql2xml
  44. {
  45.     /**
  46.     * Constructor
  47.     * The Constructor can take a Pear::DB "data source name" (eg.
  48.     *  "mysql://user:passwd@localhost/dbname") and will then connect
  49.     *  to the DB, or a PEAR::DB object link, if you already connected
  50.     *  the db before.
  51.     "  If you provide nothing as $dsn, you only can later add stuff with
  52.     *   a pear::db-resultset or as an array. providing sql-strings will
  53.     *   not work.
  54.     * the $root param is used, if you want to provide another name for your
  55.     *  root-tag than "root". if you give an empty string (""), there will be no
  56.     *  root element created here, but only when you add a resultset/array/sql-string.
  57.     *  And the first tag of this result is used as the root tag.
  58.     *
  59.     * @param  string with PEAR::DB "data source name" or object DB object
  60.     * @param  string of the name of the xml-doc root element.
  61.     * @access public
  62.     * @see  XML_sql2xml::XML_sql2xml()
  63.     */
  64.     function XML_sql2xml_ext($dsn = null$root 'root')
  65.     {
  66.         $this->XML_sql2xml($dsn,$root);
  67.         // DefaultValues for user_options
  68.  
  69.         $user_options = array (
  70.                'xml_seperator'       => '_',
  71.                'element_id'          => 'ID',
  72.                'print_empty_ids'     => true,
  73.                'selected_id'         => array(),
  74.                'field_translate'     => array(),
  75.                'attributes'          => array(),
  76.                'TableNameForRowTags' => true
  77.         );
  78.  
  79.        $this->setOptions(array('user_options' => $user_options));
  80.     }
  81.  
  82.    /*
  83.     * @param  $dsn string with PEAR::DB "data source name" or object DB object
  84.     * @param  $root string of the name of the xml-doc root element.
  85.     * @access public
  86.     * @see  XML_sql2xml::XML_sql2xml()
  87.     */
  88.     function insertNewRow($parent_row$res$key&$tableInfo)
  89.     {
  90.         if (!$tableInfo[$key]['table'{
  91.             $tableInfo[$key]['table'$this->tagNameResult;
  92.         }
  93.  
  94.         if ($this->user_options['element_id'&& !$res[$tableInfo['id'][$tableInfo[$key]['table']]] && !$this->user_options['print_empty_ids']{
  95.             return null;
  96.         }
  97.  
  98.         if (!$this->user_options['TableNameForRowTags']{
  99.             $new_row $parent_row->new_child($this->tagNameRownull);
  100.         else {
  101.             $new_row $parent_row->new_child($tableInfo[$key]['table']null);
  102.         }
  103.         /* make an unique ID attribute in the row element with tablename.id if there's an id
  104.                otherwise just make an unique id with the php-function, just that there's a unique id for this row.
  105.                 CAUTION: This ID changes every time ;) (if no id from db-table)
  106.                */
  107.         $this->SetAttribute($new_row'type''row');
  108.  
  109.         if ($res[$tableInfo['id'][$tableInfo[$key]['table']]]{
  110.         /* make attribute selected if ID = selected_id OR tableName.ID = selected_id. for the second case
  111.             you can give an array for multiple selected entries */
  112.  
  113.         if ($res[$tableInfo['id'][$tableInfo[$key]['table']]] == $this->user_options['selected_id']
  114.             || $tableInfo[$key]['table'].$res[$tableInfo['id'][$tableInfo[$key]['table']]] == $this->user_options['selected_id']
  115.             || (is_array($this->user_options['selected_id']&& in_array($tableInfo[$key]['table'].$res[$tableInfo['id'][$tableInfo[$key]['table']]]$this->user_options['selected_id']))
  116.             ||    $this->user_options['selected_id'== 'all'
  117.             ||  ($this->user_options['selected_id']  == 'first'&& !isset($this->table_selected[$tableInfo[$key]['table']])
  118.         {
  119.                 $this->SetAttribute($new_row'selected''selected');
  120.                 $this->table_selected[$tableInfo[$key]['table']] = True;
  121.             }
  122.             $this->SetAttribute($new_row'ID'utf8_encode($tableInfo[$key]['table'$res[$tableInfo['id'][$tableInfo[$key]['table']]]));
  123.         else {
  124.             $this->IDcounter[$tableInfo[$key]['table']]++;
  125.             $this->SetAttribute($new_row'ID'$tableInfo[$key]['table'].$this->IDcounter[$tableInfo[$key]['table']]);
  126.  
  127.         }
  128.  
  129.         return $new_row;
  130.     }
  131.  
  132.     function insertNewResult(&$tableInfo)
  133.     {
  134.         if (isset($this->user_options['result_root'])) {
  135.             $result_root $this->user_options['result_root'];
  136.         elseif (isset($tableInfo[0]['table'])) {
  137.             $result_root $tableInfo[0]['table'];
  138.         else {
  139.             $result_root 'resultset';
  140.         }
  141.  
  142.         if ($this->xmlroot{
  143.             $xmlroot $this->xmlroot->new_child($result_rootnull);
  144.         else {
  145.             $xmlroot $this->xmldoc->add_root($result_root);
  146.         }
  147.         $this->SetAttribute($xmlroot'type''resultset');
  148.         return $xmlroot;
  149.     }
  150.  
  151.  
  152.     function insertNewElement($parent$res$key&$tableInfo&$subrow)
  153.     {
  154.         if (is_array($this->user_options['attributes']&& in_array($tableInfo[$key]['name']$this->user_options['attributes'])) {
  155.             $subrow=$this->SetAttribute($parent,$tableInfo[$key]['name']$this->xml_encode($res[$key]));
  156.         elseif ($this->user_options['xml_seperator'])  {
  157.            // initialize some variables to get rid of warning messages
  158.             $beforetags '';
  159.             $before[-1= null;
  160.             //the preg should be only done once...
  161.             $i = 0;
  162.             preg_match_all("/([^" $this->user_options['xml_seperator'"]+)" $this->user_options['xml_seperator'"*/"$tableInfo[$key]["name"]$regs);
  163.  
  164.             if (isset($regs[1][-1])) {
  165.                 $subrow[$regs[1][-1]] $parent;
  166.             else {
  167.                 $subrow[null$parent;
  168.             }
  169.             // here we separate db fields to subtags.
  170.  
  171.             for ($i = 0; $i (count($regs[1]- 1)$i++{
  172.                 $beforetags .=$regs[1][$i'_';
  173.                 $before[$i$beforetags;
  174.                 if (!isset($subrow[$before[$i]])) {
  175.                     $subrow[$before[$i]] $subrow[$before[$i - 1]]->new_child($regs[1][$i]null);
  176.                 }
  177.             }
  178.             $subrows $subrow[$before[$i - 1]]->new_child($regs[1][$i]$this->xml_encode($res[$key]));
  179.         else {
  180.             $subrow $parent->new_child($tableInfo[$key]['name']$this->xml_encode($res[$key]));
  181.         }
  182.     }
  183.  
  184.     function addTableinfo($key$value&$tableInfo)
  185.     {
  186.         if (!isset($tableInfo['id'][$value['table']]&& $value['name'== $this->user_options['element_id']{
  187.             $tableInfo['id'][$value['table']]$key;
  188.         }
  189.  
  190.         if (isset($this->user_options['field_translate'][$value['name']])) {
  191.             $tableInfo[$key]['name'$this->user_options['field_translate'][$value['name']];
  192.         }
  193.     }
  194.  
  195.     // A wrapper for set setattr/set_attribute, since the function changed in php 4.0.6...
  196.     function SetAttribute($node$name$value)
  197.     {
  198.         if (method_exists($node'Set_attribute')) {
  199.             return $node->Set_Attribute($name$value);
  200.         else {
  201.             return $node->setattr($name$value);
  202.         }
  203.     }
  204.  
  205.     function SetResultRootTag ($resultroot)
  206.     {
  207.         if (isset($resultroot)) {
  208.             $options = array('user_options' => array('result_root' => $resultroot));
  209.             $this->setoptions($options);
  210.         }
  211.     }
  212. }

Documentation generated on Sun, 23 Mar 2008 21:00:05 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.