Esempio n. 1
0
 /**
  * @param $data
  * @param $parent
  * @param $name
  * @return unknown_type
  */
 private function createNode($data, &$parent, $name = null)
 {
     if ($name) {
         $e = $this->_xml->createElement($this->elName($name));
     } else {
         $e = $parent;
     }
     /* not complex data - without attributes aso */
     if (is_array($data) && !isset($data['_complex'])) {
         foreach ($data as $label => $values) {
             /* half-complex data - just to define custom child tag */
             if (is_array($values) && isset($values['_value'])) {
                 if (isset($values['_class']) || isset($values['_id'])) {
                     $label = isset($values['_tag']) ? $values['_tag'] : $label;
                     $attr = array();
                     if (isset($values['_class'])) {
                         $attr['class'] = $values['_class'];
                     }
                     if (isset($values['_id'])) {
                         $attr['id'] = $values['_id'];
                     }
                     $values = array('_complex' => 1, '_data' => $values['_value'], '_attributes' => $attr);
                 } else {
                     $label = isset($values['_tag']) ? $values['_tag'] : $label;
                     $values = $values['_value'];
                 }
             }
             if (!$label || is_integer($label)) {
                 $label = SPLang::singular($name);
             }
             $this->createNode($values, $e, $label);
         }
     } elseif (is_array($data) && isset($data['_complex'])) {
         if (isset($data['_xml']) && $data['_xml']) {
             $_t = new DOMDocument();
             $_t->formatOutput = true;
             /* html entities to compatible XML within textareas */
             $data['_data'] = preg_replace_callback('/(<textarea.*>)(.*)(<\\/textarea>)/s', 'SPTemplateXSLT::entities', $data['_data']);
             try {
                 /* im trying to catch this damn error already :( */
                 /* assuming the XML-Structure was ok */
                 if (@$_t->loadXML('<span>' . $data['_data'] . '</span>')) {
                     $nodes = $_t->firstChild->childNodes;
                 } elseif (class_exists('tidy') && @$_t->loadXML('<span>' . $this->repairHtml($data['_data']) . '</span>')) {
                     $nodes = $_t->firstChild->childNodes;
                 } else {
                     /* pass as escaped html text to the template */
                     $data['_attributes']['escaped'] = 'true';
                     if (!@$_t->loadXML('<span>' . htmlentities($data['_data']) . '</span>')) {
                         /* in case even this failed - pass as CDATA section */
                         $e->appendChild($this->_xml->createCDATASection($data['_data']));
                         $nodes = null;
                     } else {
                         $nodes = $_t->firstChild->childNodes;
                     }
                 }
             } catch (DOMException $x) {
                 Sobi::Error('template', $x->getMessage(), SPC::WARNING, 0, __LINE__, __FILE__);
             }
             if (count($nodes)) {
                 foreach ($nodes as $node) {
                     if ($node instanceof DOMNode) {
                         $node = $this->_xml->importNode($node, true);
                         $e->appendChild($node);
                     } else {
                         Sobi::Error('template', 'Cannot parse data', SPC::WARNING, 0, __LINE__, __FILE__);
                     }
                 }
             }
         } elseif (isset($data['_cdata']) && $data['_cdata']) {
             $e->appendChild($this->_xml->createCDATASection($data['_data']));
         } elseif (isset($data['_data']) && is_array($data['_data'])) {
             $this->createNode($data['_data'], $e);
         } elseif (isset($data['_data'])) {
             //$data[ '_attributes' ][ 'escaped' ] = 'true';
             $e->appendChild($this->_xml->createTextNode($data['_data']));
         }
         if (isset($data['_attributes']) && is_array($data['_attributes']) && count($data['_attributes'])) {
             foreach ($data['_attributes'] as $an => $av) {
                 $an = SPLang::varName($an);
                 // legacy for 1.0
                 // data- is allowed html5 attribute but data_ will not be valid
                 if (strstr($an, '-') && !strstr($an, 'data-')) {
                     $a = $this->_xml->createAttribute(str_replace('-', '_', $an));
                     $a->appendChild($this->_xml->createTextNode($av));
                     $e->appendChild($a);
                 }
                 $a = $this->_xml->createAttribute($an);
                 $a->appendChild($this->_xml->createTextNode($av));
                 $e->appendChild($a);
             }
         }
     } elseif ($name) {
         $e = $this->_xml->createElement($this->elName($name), SPLang::entities($data, true));
     }
     if ($name) {
         $parent->appendChild($e);
     }
 }
Esempio n. 2
0
 protected function importData($node, $data, $name)
 {
     $root = $this->_xml->createElement($name);
     if (isset($data['_data']) && is_array($data['_data'])) {
         foreach ($data['_data'] as $index => $value) {
             if (is_array($value)) {
                 $this->importData($root, $value, $index);
             } else {
                 $child = $this->_xml->createElement($index, $value);
                 $root->appendChild($child);
             }
         }
     } elseif (!isset($data['_data']) && is_array($data) && count($data)) {
         foreach ($data as $i => $v) {
             if (is_numeric($i)) {
                 $i = SPLang::singular($name);
             }
             if (is_array($v)) {
                 $this->importData($root, $v, $i);
             } else {
                 $root->appendChild($this->_xml->createElement($i, $v));
             }
         }
     } elseif (isset($data['_data'])) {
         $root->nodeValue = $data['_data'];
     }
     if (isset($data['_attributes']) && $data['_attributes']) {
         foreach ($data['_attributes'] as $i => $v) {
             $a = $this->_xml->createAttribute(SPLang::varName($i));
             $a->appendChild($this->_xml->createTextNode($v));
             $root->appendChild($a);
         }
     }
     $node->appendChild($root);
 }