| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package Joomla.Site 4 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 5 * @license GNU General Public License version 2 or later; see LICENSE.txt 6 */ 7 8 defined('_JEXEC') or die; 9 10 jimport('joomla.application.categories'); 11 12 /** 13 * Build the route for the com_content component 14 * 15 * @param array An array of URL arguments 16 * @return array The URL arguments to use to assemble the subsequent URL. 17 * @since 1.5 18 */ 19 function ContentBuildRoute(&$query) 20 { 21 $segments = array(); 22 23 // get a menu item based on Itemid or currently active 24 $app = JFactory::getApplication(); 25 $menu = $app->getMenu(); 26 $params = JComponentHelper::getParams('com_content'); 27 $advanced = $params->get('sef_advanced_link', 0); 28 29 // we need a menu item. Either the one specified in the query, or the current active one if none specified 30 if (empty($query['Itemid'])) { 31 $menuItem = $menu->getActive(); 32 $menuItemGiven = false; 33 } 34 else { 35 $menuItem = $menu->getItem($query['Itemid']); 36 $menuItemGiven = true; 37 } 38 39 if (isset($query['view'])) { 40 $view = $query['view']; 41 } 42 else { 43 // we need to have a view in the query or it is an invalid URL 44 return $segments; 45 } 46 47 // are we dealing with an article or category that is attached to a menu item? 48 if (($menuItem instanceof stdClass) && $menuItem->query['view'] == $query['view'] && isset($query['id']) && $menuItem->query['id'] == intval($query['id'])) { 49 unset($query['view']); 50 51 if (isset($query['catid'])) { 52 unset($query['catid']); 53 } 54 55 if (isset($query['layout'])) { 56 unset($query['layout']); 57 } 58 59 unset($query['id']); 60 61 return $segments; 62 } 63 64 if ($view == 'category' || $view == 'article') 65 { 66 if (!$menuItemGiven) { 67 $segments[] = $view; 68 } 69 70 unset($query['view']); 71 72 if ($view == 'article') { 73 if (isset($query['id']) && isset($query['catid']) && $query['catid']) { 74 $catid = $query['catid']; 75 // Make sure we have the id and the alias 76 if (strpos($query['id'], ':') === false) { 77 $db = JFactory::getDbo(); 78 $aquery = $db->setQuery($db->getQuery(true) 79 ->select('alias') 80 ->from('#__content') 81 ->where('id='.(int)$query['id']) 82 ); 83 $alias = $db->loadResult(); 84 $query['id'] = $query['id'].':'.$alias; 85 } 86 } else { 87 // we should have these two set for this view. If we don't, it is an error 88 return $segments; 89 } 90 } 91 else { 92 if (isset($query['id'])) { 93 $catid = $query['id']; 94 } else { 95 // we should have id set for this view. If we don't, it is an error 96 return $segments; 97 } 98 } 99 100 if ($menuItemGiven && isset($menuItem->query['id'])) { 101 $mCatid = $menuItem->query['id']; 102 } else { 103 $mCatid = 0; 104 } 105 106 $categories = JCategories::getInstance('Content'); 107 $category = $categories->get($catid); 108 109 if (!$category) { 110 // we couldn't find the category we were given. Bail. 111 return $segments; 112 } 113 114 $path = array_reverse($category->getPath()); 115 116 $array = array(); 117 118 foreach($path as $id) { 119 if ((int)$id == (int)$mCatid) { 120 break; 121 } 122 123 list($tmp, $id) = explode(':', $id, 2); 124 125 $array[] = $id; 126 } 127 128 $array = array_reverse($array); 129 130 if (!$advanced && count($array)) { 131 $array[0] = (int)$catid.':'.$array[0]; 132 } 133 134 $segments = array_merge($segments, $array); 135 136 if ($view == 'article') { 137 if ($advanced) { 138 list($tmp, $id) = explode(':', $query['id'], 2); 139 } 140 else { 141 $id = $query['id']; 142 } 143 $segments[] = $id; 144 } 145 unset($query['id']); 146 unset($query['catid']); 147 } 148 149 if ($view == 'archive') { 150 if (!$menuItemGiven) { 151 $segments[] = $view; 152 unset($query['view']); 153 } 154 155 if (isset($query['year'])) { 156 if ($menuItemGiven) { 157 $segments[] = $query['year']; 158 unset($query['year']); 159 } 160 } 161 162 if (isset($query['year']) && isset($query['month'])) { 163 if ($menuItemGiven) { 164 $segments[] = $query['month']; 165 unset($query['month']); 166 } 167 } 168 } 169 170 // if the layout is specified and it is the same as the layout in the menu item, we 171 // unset it so it doesn't go into the query string. 172 if (isset($query['layout'])) { 173 if ($menuItemGiven && isset($menuItem->query['layout'])) { 174 if ($query['layout'] == $menuItem->query['layout']) { 175 176 unset($query['layout']); 177 } 178 } 179 else { 180 if ($query['layout'] == 'default') { 181 unset($query['layout']); 182 } 183 } 184 } 185 186 return $segments; 187 } 188 189 190 191 /** 192 * Parse the segments of a URL. 193 * 194 * @param array The segments of the URL to parse. 195 * 196 * @return array The URL attributes to be used by the application. 197 * @since 1.5 198 */ 199 function ContentParseRoute($segments) 200 { 201 $vars = array(); 202 203 //Get the active menu item. 204 $app = JFactory::getApplication(); 205 $menu = $app->getMenu(); 206 $item = $menu->getActive(); 207 $params = JComponentHelper::getParams('com_content'); 208 $advanced = $params->get('sef_advanced_link', 0); 209 $db = JFactory::getDBO(); 210 211 // Count route segments 212 $count = count($segments); 213 214 // Standard routing for articles. If we don't pick up an Itemid then we get the view from the segments 215 // the first segment is the view and the last segment is the id of the article or category. 216 if (!isset($item)) { 217 $vars['view'] = $segments[0]; 218 $vars['id'] = $segments[$count - 1]; 219 220 return $vars; 221 } 222 223 // if there is only one segment, then it points to either an article or a category 224 // we test it first to see if it is a category. If the id and alias match a category 225 // then we assume it is a category. If they don't we assume it is an article 226 if ($count == 1) { 227 // we check to see if an alias is given. If not, we assume it is an article 228 if (strpos($segments[0], ':') === false) { 229 $vars['view'] = 'article'; 230 $vars['id'] = (int)$segments[0]; 231 return $vars; 232 } 233 234 list($id, $alias) = explode(':', $segments[0], 2); 235 236 // first we check if it is a category 237 $category = JCategories::getInstance('Content')->get($id); 238 239 if ($category && $category->alias == $alias) { 240 $vars['view'] = 'category'; 241 $vars['id'] = $id; 242 243 return $vars; 244 } else { 245 $query = 'SELECT alias, catid FROM #__content WHERE id = '.(int)$id; 246 $db->setQuery($query); 247 $article = $db->loadObject(); 248 249 if ($article) { 250 if ($article->alias == $alias) { 251 $vars['view'] = 'article'; 252 $vars['catid'] = (int)$article->catid; 253 $vars['id'] = (int)$id; 254 255 return $vars; 256 } 257 } 258 } 259 } 260 261 // if there was more than one segment, then we can determine where the URL points to 262 // because the first segment will have the target category id prepended to it. If the 263 // last segment has a number prepended, it is an article, otherwise, it is a category. 264 if (!$advanced) { 265 $cat_id = (int)$segments[0]; 266 267 $article_id = (int)$segments[$count - 1]; 268 269 if ($article_id > 0) { 270 $vars['view'] = 'article'; 271 $vars['catid'] = $cat_id; 272 $vars['id'] = $article_id; 273 } else { 274 $vars['view'] = 'category'; 275 $vars['id'] = $cat_id; 276 } 277 278 return $vars; 279 } 280 281 // we get the category id from the menu item and search from there 282 $id = $item->query['id']; 283 $category = JCategories::getInstance('Content')->get($id); 284 285 if (!$category) { 286 JError::raiseError(404, JText::_('COM_CONTENT_ERROR_PARENT_CATEGORY_NOT_FOUND')); 287 return $vars; 288 } 289 290 $categories = $category->getChildren(); 291 $vars['catid'] = $id; 292 $vars['id'] = $id; 293 $found = 0; 294 295 foreach($segments as $segment) 296 { 297 $segment = str_replace(':', '-', $segment); 298 299 foreach($categories as $category) 300 { 301 if ($category->alias == $segment) { 302 $vars['id'] = $category->id; 303 $vars['catid'] = $category->id; 304 $vars['view'] = 'category'; 305 $categories = $category->getChildren(); 306 $found = 1; 307 break; 308 } 309 } 310 311 if ($found == 0) { 312 if ($advanced) { 313 $db = JFactory::getDBO(); 314 $query = 'SELECT id FROM #__content WHERE catid = '.$vars['catid'].' AND alias = '.$db->Quote($segment); 315 $db->setQuery($query); 316 $cid = $db->loadResult(); 317 } else { 318 $cid = $segment; 319 } 320 321 $vars['id'] = $cid; 322 323 if ($item->query['view'] == 'archive' && $count != 1){ 324 $vars['year'] = $count >= 2 ? $segments[$count-2] : null; 325 $vars['month'] = $segments[$count-1]; 326 $vars['view'] = 'archive'; 327 } 328 else { 329 $vars['view'] = 'article'; 330 } 331 } 332 333 $found = 0; 334 } 335 336 return $vars; 337 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |