/** * Do the real exporting * * @return string The JSON dump of the items and categories * * @throws AppExporterException * * @since 2.0 */ public function export() { if (!($this->_application = $this->app->zoo->getApplication())) { throw new AppExporterException('No application selected.'); } // export frontpage $frontpage = $this->app->object->create('Category'); $frontpage->name = 'Root'; $frontpage->alias = '_root'; $frontpage->description = $this->_application->description; // export categories $this->_categories = $this->_application->getCategories(); $this->_categories[0] = $frontpage; foreach ($this->_categories as $category) { $this->_addZooCategory($category); } // export items $this->_comment_table = $this->app->table->comment; $item_table = $this->app->table->item; foreach ($this->_application->getTypes() as $type) { foreach ($item_table->getByType($type->id, $this->_application->id) as $key => $item) { $this->_addZooItem($item, $type); $item_table->unsetObject($key); } } return parent::export(); }
/** * Returns type select list html string. * * @param Application $application The Application object * @param array $options The options * @param string $name The html name * @param array|string $attribs The html attributes * @param string $key * @param string $text * @param string $selected The selected value * @param string $idtag * @param boolean $translate * @param array $filter The type filter * * @return string the type select list html output * @since 2.0 */ public function typeList($application, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false, $filter = array()) { // set options settype($options, 'array'); reset($options); foreach ($application->getTypes() as $type) { if (empty($filter) || in_array($type->id, $filter)) { $options[] = $this->_('select.option', $type->id, JText::_($type->name)); } } return $this->_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate); }
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; }
/** * Imports the items * * @param Application $application The Application object * @param array $items the items to import * @param array $element_assignment The element assignment array * @param array $types The selected types array * @param array $categories The category objects * * @since 2.0 */ private function _importItems(Application $application, $items = array(), $element_assignment = array(), $types = array(), $categories = array()) { // init vars $db = $this->app->database; $table = $this->app->table->item; $comment_table = $this->app->table->comment; $item_vars = array_keys(get_class_vars('Item')); $comment_vars = array_keys(get_class_vars('Comment')); $user_id = $this->app->user->get()->get('id'); $app_types = $application->getTypes(); $authors = $this->app->data->create($db->queryObjectList('SELECT id, username, name FROM #__users', 'id')); // disconnect from comment save event $this->app->event->dispatcher->disconnect('comment:saved', array('CommentEvent', 'saved')); $item_objects = array(); foreach ($items as $alias => $item) { if (isset($item['group'], $types[$item['group']]) && !empty($types[$item['group']]) && ($type = $app_types[$types[$item['group']]])) { $item_obj = $this->app->object->create('Item'); $item_obj->alias = $this->app->string->sluggify($alias); $item_obj->type = $type->id; // set a valid category alias $item_obj->alias = $this->app->alias->item->getUniqueAlias(0, $item_obj->alias); $db->query('INSERT INTO ' . $table->name . '(alias) VALUES (' . $db->quote($item_obj->alias) . ')'); $item_obj->id = $db->insertid(); // set item values foreach ($item as $property => $value) { if (in_array($property, $item_vars)) { $item_obj->{$property} = $value; } } // fix access if j16 if ($this->app->joomla->version->isCompatible('1.6') && $item_obj->access == 0) { $item_obj->access = $this->app->joomla->getDefaultAccess(); } // store application id $item_obj->application_id = $application->id; // store tags if (isset($item['tags'])) { $item_obj->setTags($item['tags']); } // store author $item_obj->created_by_alias = ""; if (isset($item['author'])) { if ($key = $authors->searchRecursive($item['author'])) { $item_obj->created_by = (int) $authors[$key]->id; } else { $item_obj->created_by_alias = $item['author']; } } // if author is unknown set current user as author if (!$item_obj->created_by) { $item_obj->created_by = $user_id; } // store modified_by $item_obj->modified_by = $user_id; // store element_data $item_obj->elements = $this->app->data->create(); if (isset($item['elements'])) { foreach ($item['elements'] as $old_element_alias => $element) { if (isset($element['data']) && isset($element_assignment[$item['group']][$old_element_alias][$type->id]) && ($element_alias = $element_assignment[$item['group']][$old_element_alias][$type->id]) && ($element_obj = $item_obj->getElement($element_alias))) { $element_obj->bindData($element['data']); } } } // set metadata, content, config params $item_obj->getParams()->set('metadata.', @$item['metadata']); $item_obj->getParams()->set('content.', @$item['content']); $item_obj->getParams()->set('config.', @$item['config']); $item_objects[$alias] = $item_obj; // save item -> category relationship if (isset($item['categories'])) { if (isset($item['config']['primary_category'], $categories[$item['config']['primary_category']])) { $item_obj->getParams()->set('config.primary_category', $categories[$item['config']['primary_category']]->id); } else { if (isset($item['config']['primary_category']) && ($id = $this->app->alias->category->translateAliasToID($item['config']['primary_category']))) { $item_obj->getParams()->set('config.primary_category', $id); } } $item_categories = array(); foreach ($item['categories'] as $category_alias) { if (isset($categories[$category_alias]) || $category_alias == '_root') { $item_categories[] = $category_alias == '_root' ? 0 : (int) $categories[$category_alias]->id; } else { if ($id = $this->app->alias->category->translateAliasToID($category_alias)) { $item_categories[] = $id; } } } if (!empty($item_categories)) { $this->app->category->saveCategoryItemRelations($item_obj, $item_categories); } } // save comments if (isset($item['comments']) && is_array($item['comments'])) { $comments = array(); foreach ($item['comments'] as $key => $comment) { $comment_obj = $this->app->object->create('comment'); $comment_obj->item_id = $item_obj->id; // set item values foreach ($comment as $property => $value) { if (in_array($property, $comment_vars)) { $comment_obj->{$property} = $value; } } if (isset($comment_obj->user_type) && $comment_obj->user_type == 'joomla') { if (isset($comment['username']) && ($key = $authors->searchRecursive($comment['username']))) { $comment_obj->user_id = (int) $authors[$key]->id; $comment_obj->author = (string) $authors[$key]->name; } else { $comment_obj->user_id = $comment_obj->user_type = ''; } } $comment_table->save($comment_obj); $comments[$key] = $comment_obj; } // sanatize parent ids foreach ($comments as $key => $comment) { if ($comment->parent_id && isset($comments[$comment->parent_id])) { $comment->parent_id = $comments[$comment->parent_id]->id; $comment_table->save($comment); } } } } } foreach ($item_objects as $item) { foreach ($item->getElements() as $element) { // sanatize relateditems elements if ($element->getElementType() == 'relateditems') { $relateditems = $element->get('item', array()); $new_related_items = array(); foreach ($relateditems as $relateditem) { if (isset($items[$relateditem])) { $new_related_items[] = $item_objects[$relateditem]->id; } } $element->set('item', $new_related_items); // sanitize relatedcategories elements aliases } else { if ($element->getElementType() == 'relatedcategories') { $relatedcategories = $element->get('category', array()); $new_related_categories = array(); foreach ($relatedcategories as $relatedcategory) { if (isset($categories[$relatedcategory])) { $new_related_categories[] = $categories[$relatedcategory]->id; } else { if ($id = $this->app->alias->category->translateAliasToID($relatedcategory)) { $new_related_categories[] = $id; } } } $element->set('category', $new_related_categories); } } } try { $table->save($item); } catch (AppException $e) { $this->app->error->raiseNotice(0, JText::_('Error Importing Item') . ' (' . $e . ')'); } } return $item_objects; }