public function encodeData() { $xml = YXMLElement::create($this->_element->getElementType())->addAttribute('identifier', $this->_element->identifier); $value = $this->_params->get('value', ''); if (!empty($value)) { $tzoffset = JFactory::getConfig()->getValue('config.offset'); $date = JFactory::getDate($value, $tzoffset); $value = $date->toMySQL(); } $xml->addChild('value', $value, null, true); return $xml; }
public function encodeData() { $xml = YXMLElement::create($this->_element->getElementType()); $xml->addAttribute('identifier', $this->_element->identifier); foreach ($this->_params->get('country', array()) as $country) { $xml->addChild('country', $country, null, true); } return $xml; }
protected function _buildElement($name, $alias, $element_name, array $values = array()) { $elem_xml = YXMLElement::create($name)->addAttribute('identifier', $alias)->addAttribute('name', $element_name); foreach ($values as $key => $value) { if (is_array($value)) { foreach ($value as $single_value) { $elem_xml->addChild($key, $single_value, null, true); } } else { $elem_xml->addChild($key, $value, null, true); } } return $elem_xml; }
public function encodeData() { $xml = YXMLElement::create($this->_element->getElementType())->addAttribute('identifier', $this->_element->identifier); foreach ($this->_params->toArray() as $key => $value) { $xml->addChild($key, $value, null, true); } return $xml; }
private static function _importItems(Application $application, $items_xml_array = array(), $element_assignment = array(), $types = array()) { // init vars $db = YDatabase::getInstance(); $table = YTable::getInstance('item'); $item_vars = array_keys(get_class_vars('Item')); $user_id = JFactory::getUser()->get('id'); $app_types = $application->getTypes(); $authors = new YArray(YDatabase::getInstance()->queryObjectList('SELECT id, username FROM #__users')); $items = array(); foreach ($items_xml_array as $key => $items_xml) { $index = (string) $items_xml->attributes()->name; if (isset($types[$index]) && !empty($types[$index]) && ($type = $app_types[$types[$index]])) { $elements = $type->getElements(); $traverse = true; while ($traverse) { $traverse = false; foreach ($items_xml->item as $item_xml) { $traverse = true; $item = new Item(); $item->old_alias = (string) $item_xml->attributes()->id; $item->alias = YString::sluggify($item->old_alias); $item->type = $type->id; // set a valid category alias while (ItemHelper::checkAliasExists($item->alias)) { $item->alias .= '-2'; } $db->query('INSERT INTO ' . ZOO_TABLE_ITEM . '(alias) VALUES (' . $db->quote($item->alias) . ')'); $item->id = $db->insertid(); // set item values foreach ($item_xml->children() as $child) { $name = $child->getName(); if (in_array($name, $item_vars)) { $item->{$name} = (string) $child; } } // store application id $item->application_id = $application->id; // store categories $item->categories = array(); foreach ($item_xml->getElementsByPath('categories/category') as $category_xml) { $item->categories[] = (string) $category_xml; } // store tags $tags = array(); foreach ($item_xml->getElementsByPath('tags/tag') as $tag_xml) { $tags[] = (string) $tag_xml; } $item->setTags($tags); // store author $item->created_by_alias = ""; if ($item_xml->author) { $author = (string) $item_xml->author; $key = $authors->searchRecursive($author); if ($key !== false) { $item->created_by = (int) $authors[$key]->id; } else { $item->created_by_alias = $author; } } // if author is unknown set current user as author if (!$item->created_by) { $item->created_by = $user_id; } // store modified_by $item->modified_by = $user_id; // store element_data if ($data = $item_xml->data) { $elements_xml = YXMLElement::create('elements'); $nodes_to_delete = array(); foreach ($data->children() as $key => $element_xml) { $old_element_alias = (string) $element_xml->attributes()->identifier; if (isset($element_assignment[$index][$old_element_alias][$type->id]) && ($element_alias = $element_assignment[$index][$old_element_alias][$type->id])) { $element_xml->addAttribute('identifier', $element_alias); $elements_xml->appendChild($element_xml); } else { $nodes_to_delete[] = $element_xml; } } foreach ($nodes_to_delete as $node) { $data->removeChild($node); } $item->elements = $elements_xml->asXML(true, true); } // store metadata $params = $item->getParams(); if ($metadata = $item_xml->metadata) { foreach ($metadata->children() as $metadata_xml) { $params->set('metadata.' . $metadata_xml->getName(), (string) $metadata_xml); } } $item->params = $params->toString(); $items[$item->old_alias] = $item; $items_xml->removeChild($item_xml); } } } } // sanatize relateditems elements foreach ($items as $key => $item) { foreach ($item->getElements() as $element) { if ($element->getElementType() == 'relateditems') { $relateditems = $element->getElementData()->get('item', array()); $new_related_items = array(); foreach ($relateditems as $key => $relateditem) { if (isset($items[$relateditem])) { $new_related_items[] = $items[$relateditem]->id; } } $element->getElementData()->set('item', $new_related_items); } } try { $table->save($item); $item->unsetElementData(); } catch (YException $e) { JError::raiseNotice(0, JText::_('Error Importing Item') . ' (' . $e . ')'); } } return $items; }
public static function toXML($elements) { $type = YXMLElement::create('type')->addAttribute('version', '1.0.0'); $params = YXMLElement::create('params'); foreach ($elements as $element) { $params->appendChild($element->getConfigXML()); } return $type->appendChild($params)->asXML(true, true); }
public function save() { $old_identifier = $this->id; $rename = false; if (empty($this->id)) { // check identifier if (file_exists($this->getXMLFile($this->identifier))) { throw new TypeException('Identifier already exists'); } // set xml $this->setXML(YXMLElement::create('type')->addAttribute('version', '1.0.0')->asXML(true, true)); } else { if ($old_identifier != $this->identifier) { // check identifier if (file_exists($this->getXMLFile($this->identifier))) { throw new TypeException('Identifier already exists'); } // rename xml file if (!JFile::move($this->getXMLFile(), $this->getXMLFile($this->identifier))) { throw new TypeException('Renaming xml file failed'); } $rename = true; } } // update id $this->id = $this->identifier; // set data $this->setXML(YXML::loadString($this->getXML())->addAttribute('name', $this->name)->asXML(true, true)); // save xml file if ($file = $this->getXMLFile()) { if (!JFile::write($file, $this->getXML())) { throw new TypeException('Writing type xml file failed'); } } // rename related items if ($rename) { $table = YTable::getInstance('item'); // get database $db = $table->getDBO(); // update childrens parent category $query = "UPDATE " . $table->getTableName() . " SET type=" . $db->quote($this->identifier) . " WHERE type=" . $db->quote($old_identifier); $db->query($query); } return $this; }
public function encodeData() { $xml = YXMLElement::create($this->_element->getElementType())->addAttribute('identifier', $this->_element->identifier); foreach ($this->_params->get('category', array()) as $category) { $xml->addChild('category')->setData($category); } return $xml; }
protected function _itemToXML(Item $item) { $attributes = array(); foreach (self::$item_attributes as $attribute) { if (isset($item->{$attribute})) { $attributes[$attribute] = $item->{$attribute}; } } $attributes['author'] = JFactory::getUser($item->created_by)->username; $item_xml = $this->_buildItem($item->alias, $item->name, $attributes); foreach ($item->getRelatedCategoryIds() as $category_id) { $alias = ''; if (empty($category_id)) { $alias = '_root'; } else { if (isset($this->categories[$category_id])) { $alias = $this->categories[$category_id]->alias; } } if (!empty($alias)) { $this->_addItemCategory($item_xml, $alias); } } foreach ($item->getTags() as $tag) { $this->_addItemTag($item_xml, $tag); } foreach ($item->getElements() as $element) { $xml = YXML::loadString('<wrapper>' . $element->toXML() . '</wrapper>'); foreach ($xml->children() as $element_xml) { $element_xml->addAttribute('name', $element->getConfig()->get('name')); $this->_addItemData($item_xml, $element_xml); } } $metadata = array(); foreach ($item->getParams()->get('metadata.', array()) as $key => $value) { $metadata[preg_replace('/^metadata\\./', '', $key)] = $value; } if (!empty($metadata)) { $this->_addItemMetadata($item_xml, $metadata); } // sanitize relateditems elements $related_item_xmls = $item_xml->xpath('data/relateditems/item'); if ($related_item_xmls) { foreach ($related_item_xmls as $related_item_xml) { $item_xml->replaceChild(YXMLElement::create('item', ItemHelper::translateIDToAlias((string) $related_item_xml), true), $related_item_xml); } } return $item_xml; }