home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / XML / sql2xml_ext.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  8.4 KB  |  208 lines

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