Example #1
0
 /**
  * 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);
 }
Example #2
0
 /**
  * Get search data from item
  * @param Item $item
  * @param JSONData $params
  * @return array
  */
 protected function _getSearchData(Item $item, $params)
 {
     // get related categories
     $itemCategories = array();
     $checkCategory = (int) $params->get('check_category', 1);
     if ($checkCategory == 1) {
         $itemCategories[] = $item->getPrimaryCategoryId();
     } else {
         if ($checkCategory == 2) {
             $itemCategories = $item->getRelatedCategoryIds();
         }
     }
     // get manualy conditions
     $conds = array();
     $tmpConds = $params->get('conditions', array());
     foreach ($tmpConds as $cond) {
         if (isset($cond['key']) && !empty($cond['key'])) {
             $key = preg_replace('#[^0-9a-z\\_\\-]#i', '', $cond['key']);
             $value = $cond['value'];
             if (strpos($value, '[') !== false && strpos($value, ']') !== false) {
                 $value = json_decode($value, true);
             }
             if (!empty($value) && !empty($key)) {
                 $conds[$key] = $value;
             }
         }
     }
     // get search data
     $tmpResult = array('_itemfrontpage' => (int) in_array(0, $itemCategories), '_itemcategory' => $itemCategories, '_itemname' => $item->name, '_itemtag' => $item->getTags());
     $elements = $item->getElements();
     foreach ($elements as $element) {
         if ($data = $element->getSearchData()) {
             $tmpResult[$element->identifier] = JString::trim($data);
         }
     }
     $tmpResult = array_merge($tmpResult, $conds);
     // build result
     $checkedFields = $params->get('check_fields', array());
     $checkedFields[] = '_itemtype';
     $result = array();
     foreach ($tmpResult as $id => $values) {
         if (count($checkedFields) > 1 && !in_array($id, $checkedFields, true)) {
             continue;
         }
         $result[$this->_jbtables->getFieldName($id)] = $values;
     }
     return $result;
 }
Example #3
0
 /**
  * Get route to item
  *
  * @param Item $item
  * @param boolean $route If it should be run through JRoute::_()
  *
  * @return string the route
  * @since 2.0
  */
 public function item($item, $route = true)
 {
     $category_id = $this->_category_id;
     $key = $this->_active_menu_item_id . '-item-' . $item->application_id . '_' . $category_id . '_' . $item->id . '_' . $route;
     if ($this->_cache && ($cached = $this->_cache->get($key))) {
         return $cached;
     }
     // Priority 1: direct link to item
     if ($menu_item = $this->_find('item', $item->id)) {
         $link = $menu_item->link . '&Itemid=' . $menu_item->id;
     } else {
         $itemid = null;
         // build item link
         $link = $this->getLinkBase() . '&task=item&item_id=' . $item->id;
         // are we in category view?
         $this->app->table->application->get($item->application_id)->getCategoryTree(true);
         $categories = null;
         if ($category_id) {
             $categories = array_filter($item->getRelatedCategoryIds(true));
             $category_id = in_array($category_id, $categories) ? $category_id : null;
         }
         if (!$category_id) {
             $primary_id = $item->getPrimaryCategoryId();
             // Priority 2: direct link to primary category
             if ($primary_id && ($menu_item = $this->_find('category', $primary_id))) {
                 $itemid = $menu_item->id;
                 // Priority 3: find in primary category path
             } else {
                 if ($primary_id and $primary = $item->getPrimaryCategory() and $menu_item = $this->_findInCategoryPath($primary)) {
                     $itemid = $menu_item->id;
                 } else {
                     $categories = is_null($categories) ? array_filter($item->getRelatedCategoryIds(true)) : $categories;
                     foreach ($categories as $category) {
                         // Priority 4: direct link to any related category
                         if ($menu_item = $this->_find('category', $category)) {
                             $itemid = $menu_item->id;
                             break;
                         }
                     }
                     if (!$itemid) {
                         $categories = $item->getRelatedCategories(true);
                         foreach ($categories as $category) {
                             // Priority 5: find in any related categorys path
                             if ($menu_item = $this->_findInCategoryPath($category)) {
                                 $itemid = $menu_item->id;
                                 break;
                             }
                         }
                     }
                     // Priority 6: link to frontpage
                     if (!$itemid && ($menu_item = $this->_find('frontpage', $item->application_id))) {
                         $itemid = $menu_item->id;
                     }
                 }
             }
         } elseif ($category_id != $item->getPrimaryCategoryId()) {
             $link .= '&category_id=' . $category_id;
         }
         if ($itemid) {
             $link .= '&Itemid=' . $itemid;
             // Priority 7: current item id
         } else {
             if ($menu_item = $this->app->menu->getActive()) {
                 $link .= '&Itemid=' . $menu_item->id;
             }
         }
     }
     if ($route) {
         $link = JRoute::_($link);
     }
     // store link for future lookups
     if ($key && $this->_cache) {
         $this->_cache->set($key, $link)->save();
     }
     return $link;
 }
Example #4
0
 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;
 }