Example #1
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;
 }