/** * Add an item to the exporting list * * @param Item $item The item to export * @param Type $type The type of the item * * @return AppExporterZoo2 $this for chaining support * * @since 2.0 */ protected function _addZooItem(Item $item, Type $type) { $data = array(); foreach ($this->item_attributes as $attribute) { if (isset($item->{$attribute})) { $data[$attribute] = $item->{$attribute}; } } if ($user = $this->app->user->get($item->created_by)) { $data['author'] = $user->username; } $data['tags'] = $item->getTags(); // store item content, metadata, config params $data['content'] = $item->getParams()->get('content.'); $data['metadata'] = $item->getParams()->get('metadata.'); $data['config'] = $item->getParams()->get('config.'); // add categories 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)) { $data['categories'][] = $alias; } if ($item->getPrimaryCategoryId() == $category_id) { $data['config']['primary_category'] = $alias; } } foreach ($item->elements as $identifier => $element_data) { if (!($element = $type->getElement($identifier))) { continue; } $element_type = $element->getElementType(); switch ($element_type) { case 'relateditems': $items = array(); if (isset($element_data['item'])) { foreach ($element_data['item'] as $item_id) { $items[] = $this->app->alias->item->translateIDToAlias($item_id); } } $element_data['item'] = $items; break; case 'relatedcategories': $categories = array(); if (isset($element_data['category'])) { foreach ($element_data['category'] as $category_id) { $categories[] = isset($this->_categories[$category_id]) ? $this->_categories[$category_id]->alias : $this->app->alias->category->translateIDToAlias($category_id); } } $element_data['category'] = $categories; break; } $data['elements'][$identifier]['type'] = $element_type; $data['elements'][$identifier]['name'] = $element->config->get('name'); $data['elements'][$identifier]['data'] = $element_data; foreach ($this->_comment_table->getCommentsForItem($item->id) as $comment) { foreach ($this->comment_attributes as $attribute) { if (isset($comment->{$attribute})) { $data['comments'][$comment->id][$attribute] = $comment->{$attribute}; } } if ($comment->user_type == 'joomla' && ($user = $this->app->user->get($comment->user_id))) { $data['comments'][$comment->id]['username'] = $user->username; } } } parent::_addItem($item->name, $item->alias, $type->name, $data); }
public function save() { // check for request forgeries YRequest::checkToken() or jexit('Invalid Token'); // init vars $db = JFactory::getDBO(); $config = JFactory::getConfig(); $now = JFactory::getDate(); $post = YRequest::get('post'); $frontpage = YRequest::getBool('frontpage', false); $categories = YRequest::getArray('categories', null); $details = YRequest::getArray('details', null); $metadata = YRequest::getArray('meta', null); $cid = YRequest::getArray('cid.0', '', 'int'); $tzoffset = $config->getValue('config.offset'); $post = array_merge($post, $details); try { // get item table $table = YTable::getInstance('item'); // get item if ($cid) { $item = $table->get($cid); } else { $item = new Item(); $item->application_id = $this->application->id; $item->type = YRequest::getVar('type'); } // bind item data $item->bind($post, array('elements', 'params', 'created_by')); $created_by = isset($post['created_by']) ? $post['created_by'] : ''; $item->created_by = empty($created_by) ? JFactory::getUser()->id : $created_by == 'NO_CHANGE' ? $item->created_by : $created_by; $tags = isset($post['tags']) ? $post['tags'] : array(); $item->setTags($tags); // bind element data foreach ($item->getElements() as $id => $element) { if (isset($post['elements'][$id])) { $element->bindData($post['elements'][$id]); } else { $element->bindData(); } } // set alias $item->alias = ItemHelper::getUniqueAlias($item->id, YString::sluggify($item->alias)); // set modified $item->modified = $now->toMySQL(); $item->modified_by = $this->user->get('id'); // set created date if ($item->created && strlen(trim($item->created)) <= 10) { $item->created .= ' 00:00:00'; } $date = JFactory::getDate($item->created, $tzoffset); $item->created = $date->toMySQL(); // set publish up date if (strlen(trim($item->publish_up)) <= 10) { $item->publish_up .= ' 00:00:00'; } $date = JFactory::getDate($item->publish_up, $tzoffset); $item->publish_up = $date->toMySQL(); // set publish down date if (trim($item->publish_down) == JText::_('Never') || trim($item->publish_down) == '') { $item->publish_down = $db->getNullDate(); } else { if (strlen(trim($item->publish_down)) <= 10) { $item->publish_down .= ' 00:00:00'; } $date = JFactory::getDate($item->publish_down, $tzoffset); $item->publish_down = $date->toMySQL(); } // get primary category $primary_category = @$post['params']['primary_category']; if (empty($primary_category) && count($categories)) { $primary_category = $categories[0]; } // set params $item->params = $item->getParams()->remove('metadata.')->remove('template.')->set('metadata.', @$post['params']['metadata'])->set('template.', @$post['params']['template'])->set('config.enable_comments', @$post['params']['enable_comments'])->set('config.primary_category', $primary_category)->toString(); // save item $table->save($item); // make sure categories contain primary category if (!empty($primary_category) && !in_array($primary_category, $categories)) { $categories[] = $primary_category; } // save category relations if ($frontpage) { $categories[] = 0; } CategoryHelper::saveCategoryItemRelations($item->id, $categories); // set redirect message $msg = JText::_('Item Saved'); } catch (YException $e) { // raise notice on exception JError::raiseNotice(0, JText::_('Error Saving Item') . ' (' . $e . ')'); $this->_task = 'apply'; $msg = null; } $link = $this->baseurl; switch ($this->getTask()) { case 'apply': $link .= '&task=edit&type=' . $item->type . '&cid[]=' . $item->id; break; case 'saveandnew': $link .= '&task=add'; break; } $this->setRedirect($link, $msg); }
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; }
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; }