Example #1
0
 /**
  * Wrapper to JRoute to handle itemid
  * We need to try and capture the correct itemid for different view	 
  */
 static function _($url, $xhtml = true, $ssl = null)
 {
     static $itemid = array();
     parse_str(JString::str_ireplace('index.php?', '', $url));
     if (isset($option) && $option != 'com_adsmanager' && $option != 'com_paidsystem') {
         return JRoute::_($url, $xhtml, $ssl);
     }
     if (!isset($option)) {
         return JRoute::_($url, $xhtml, $ssl);
     }
     $urloption = $option;
     if ($urloption == 'com_adsmanager' && empty($view)) {
         $view = 'front';
     }
     if ($urloption == 'com_paidsystem' && empty($view)) {
         $view = 'form';
     }
     if (!empty($task) && ($task = 'write')) {
         $view = 'edit';
     }
     if ($view == "expiration") {
         $view = "myads";
     }
     if ($view == "details") {
         $view = "details" . $catid;
         $originalview = "details";
     } else {
         $originalview = $view;
     }
     if (empty($itemid[$view])) {
         $currentItemid = JRequest::getInt('Itemid', 0);
         $isValid = false;
         $currentView = JRequest::getVar('view', 'front');
         $currentOption = JRequest::getVar('option');
         // If the current Itemid match the expected Itemid based on view
         // we'll just use it
         $db = JFactory::getDBO();
         if ($originalview == "details") {
             $viewId = TRoute::_getDetailsItemid($catid, $urloption);
         } else {
             if ($originalview == "result") {
                 $viewId = TRoute::_getResultViewItemid($view, $urloption);
             } else {
                 $viewId = TRoute::_getViewItemid($view, $urloption);
             }
         }
         // if current itemid
         if ($viewId == 0 && $currentOption == $urloption && $currentView == $view && $currentItemid != 0) {
             $itemid[$view] = $currentItemid;
             $isValid = true;
         } else {
             if ($viewId === $currentItemid && !is_null($currentItemid) && $currentItemid != 0) {
                 $itemid[$view] = $currentItemid;
                 $isValid = true;
             } else {
                 if ($viewId !== 0 && !is_null($viewId)) {
                     $itemid[$view] = $viewId;
                     $isValid = true;
                 }
             }
         }
         if (!$isValid) {
             $id = TRoute::_getDefaultItemid($urloption);
             if ($id !== 0 && !is_null($id)) {
                 $itemid[$view] = $id;
             }
             $isValid = true;
         }
         // Search the mainmenu for the 1st itemid of adsmanager we can find
         if (!$isValid) {
             $db = JFactory::getDBO();
             $query = 'SELECT id FROM #__menu WHERE ' . 'link LIKE ' . $db->Quote('%' . $urloption . '%') . 'AND published =' . $db->Quote(1) . ' ' . 'AND menutype !=' . $db->Quote($config->get('toolbar_menutype')) . ' ' . 'AND type =' . $db->Quote('component');
             $db->setQuery($query);
             $isValid = $db->loadResult();
             if (!empty($isValid)) {
                 $itemid[$view] = $isValid;
             }
         }
         // If not in mainmenu, seach in any menu
         if (!$isValid) {
             $query = 'SELECT id FROM #__menu WHERE ' . ' link LIKE ' . $db->Quote('%' . $urloption . '%') . ' AND published =' . $db->Quote(1) . ' ' . ' AND menutype !=' . $db->Quote($config->get('toolbar_menutype')) . ' ' . ' AND type =' . $db->Quote('component');
             $db->setQuery($query);
             $isValid = $db->loadResult();
             if (!empty($isValid)) {
                 $itemid[$view] = $isValid;
             }
         }
     }
     $pos = strpos($url, '#');
     if ($pos === false) {
         if (isset($itemid[$view])) {
             if (strpos($url, 'Itemid=') === false && strpos($url, $urloption) !== false) {
                 $url .= '&Itemid=' . $itemid[$view];
             }
         }
     } else {
         if (isset($itemid[$view])) {
             $url = str_ireplace('#', '&Itemid=' . $itemid[$view] . '#', $url);
         }
     }
     if (strpos($url, "rootid") === false) {
         $rootid = JRequest::getInt('rootid', 0);
         if ($rootid != 0) {
             $pos = strpos($url, '#');
             if ($pos === false) {
                 $url .= '&rootid=' . $rootid;
             } else {
                 $url = str_ireplace('#', '&rootid=' . $rootid . '#', $url);
             }
         }
     }
     $data = JRoute::_($url, $xhtml, $ssl);
     return $data;
 }