PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/proveedores/XML/sql2xml_ext.php

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