helper.php 18.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
<?php
// namespace administrator\components\com_jmap\framework\route;
/**
 *
* @package JMAP::FRAMEWORK::administrator::components::com_jmap
* @subpackage framework
* @subpackage route
* @author Joomla! Extensions Store
* @copyright (C) 2015 - Joomla! Extensions Store
* @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
*/
defined ( '_JEXEC' ) or die ();

// Component Helper
jimport ( 'joomla.application.component.helper' );
jimport ( 'joomla.application.categories' );

/**
 * Static generic route helper class
 *
 * @package JMAP::FRAMEWORK::administrator::components::com_jmap
 * @subpackage framework
 * @subpackage route
 * @since 2.3
 */
final class JMapRouteHelper {
	/**
	 * Recursive adjacency list model function to find all cat parents
	 *
	 * @param unknown $option        	
	 * @param string $needles        	
	 * @return NULL
	 */
	protected static function findAllParents($option, $categoryTableName, $categoryParentField, $root, &$allParents, $categoryIdentifier) {
		static $db;
		if (! $db) {
			$db = JFactory::getDbo ();
		}
		
		$query = "SELECT " . $db->quoteName ( $categoryParentField ) . 
				 "\n FROM " . $db->quoteName ( $categoryTableName ) . 
				 "\n WHERE " . $db->quoteName($categoryIdentifier) . " = " . ( int ) $root;
		$hasAParentCatId = $db->setQuery ( $query )->loadResult ();
		if ($hasAParentCatId) {
			$allParents [] = ( int ) $hasAParentCatId;
			self::findAllParents ( $option, $categoryTableName, $categoryParentField, $hasAParentCatId, $allParents, $categoryIdentifier);
		}
		
		return $allParents;
	}
	
	/**
	 * Load route manifests
	 *
	 * @access protected
	 * @return array
	 */
	protected static function loadRouteManifests() {
		$directory = JPATH_ROOT . '/administrator/components/com_jmap/framework/route/manifests/';
		$manifestsArray = array ();
		$iterator = new DirectoryIterator ( $directory );
		foreach ( $iterator as $fileinfo ) {
			if ($fileinfo->isFile () && $fileinfo->getFilename () != 'index.html') {
				// Load the manifest serialized file and assign to local variable
				$manifest = file_get_contents ( $fileinfo->getPathname () );
				$manifestConfiguration = json_decode ( $manifest );
				$componentName = $fileinfo->getBasename ( '.json' );
				$manifestsArray [$componentName] = ( array ) $manifestConfiguration;
				// Cast sublevel object to array
				if(isset($manifestsArray[$componentName]['name_params'])){
					$manifestsArray[$componentName]['name_params'] = (array)$manifestsArray[$componentName]['name_params'];
				}
				if(isset($manifestsArray[$componentName]['additional_custom_fields'])){
					$manifestsArray[$componentName]['additional_custom_fields'] = (array)$manifestsArray[$componentName]['additional_custom_fields'];
				}
			}
		}
		return $manifestsArray;
	}
	
	/**
	 * Run the real finding of itemid
	 *
	 *@access protected
	 * @param string $option
	 * @param array $needles
	 * @param object $manifestObject
	 * @param string $thisView
	 * @return int
	 */
	protected static function findItem($option, $needles, $manifestObject, $thisView) {
		static $lookup;
		static $menuInstance;
		
		// Load menu structure only the first time for the first link
		if (! $menuInstance) {
			$menuInstance = new JMapRouteMenu ();
		}
		
		// Build the lookup array for this component if not exists
		if (! isset ( $lookup [$option] )) {
			$lookup [$option] = array ();
			
			$component = JComponentHelper::getComponent ( $option );
			$items = $menuInstance->getItems ( 'component_id', $component->id );
			foreach ( $items as $item ) {
				// If some extensions does not support view on menu links, fallback on controller
				if(isset ( $item ['query'] ) && !isset ( $item ['query'] ['view'])) {
					$item ['query'] ['view'] = isset($item ['query'] ['controller']) ? $item ['query'] ['controller'] : $thisView;
				}
				
				if (isset ( $item ['query'] ) && isset ( $item ['query'] ['view'] )) {
					$view = $item ['query'] ['view'];
					if (! isset ( $lookup [$option] [$view] )) {
						$lookup [$option] [$view] = array ();
					}
					if (isset ( $item ['query'] ['id'] )) { // ID identifier for single item
						// No override already more specific itemid for a certain language
						if (isset ( $lookup [$option] [$view] [$item ['query'] ['id']] )) {
							$alreadyAssigned = $lookup [$option] [$view] [$item ['query'] ['id']];
							$alreadyAssignedLanguage = $items [$alreadyAssigned] ['language'];
							if ($alreadyAssignedLanguage != '*' && $item ['language'] == '*') {
								continue;
							}
						}
						// Standard assignment using id query identifier
						$lookup [$option] [$view] [$item ['query'] ['id']] = $item ['id'];
					} elseif (isset ( $item ['query'] ['catid'] )) { // CATID identifier for categories
						if(!is_array( $item ['query'] ['catid'] )) {
							// No override already more specific itemid for a certain language
							if (isset ( $lookup [$option] [$view] [$item ['query'] ['catid']] )) {
								$alreadyAssigned = $lookup [$option] [$view] [$item ['query'] ['catid']];
								$alreadyAssignedLanguage = $items [$alreadyAssigned] ['language'];
								if ($alreadyAssignedLanguage != '*' && $item ['language'] == '*') {
									continue;
								}
							}
							// Standard assignment using catid query identifier
							$lookup [$option] [$view] [$item ['query'] ['catid']] = $item ['id'];
						} elseif(is_array( $item ['query'] ['catid'] )) { // CATID identifier as array
							foreach ( $item ['query'] ['catid'] as $cycledCatid) {
								// No override already more specific itemid for a certain language
								if (isset ( $lookup [$option] [$view] [$cycledCatid] )) {
									$alreadyAssigned = $lookup [$option] [$view] [$cycledCatid];
									$alreadyAssignedLanguage = $items [$alreadyAssigned] ['language'];
									if ($alreadyAssignedLanguage != '*' && $item ['language'] == '*') {
										continue;
									}
								}
								
								// Standard assignment using catid query identifier
								$lookup [$option] [$view] [$cycledCatid] = $item ['id'];
							}
						}
					}  elseif (isset ( $item ['query'] ['cid'] )) { // CID identifier for categories
						// No override already more specific itemid for a certain language
						if (isset ( $lookup [$option] [$view] [$item ['query'] ['cid']] )) {
							$alreadyAssigned = $lookup [$option] [$view] [$item ['query'] ['cid']];
							$alreadyAssignedLanguage = $items [$alreadyAssigned] ['language'];
							if ($alreadyAssignedLanguage != '*' && $item ['language'] == '*') {
								continue;
							}
						}
						// Standard assignment using catid query identifier
						$lookup [$option] [$view] [$item ['query'] ['cid']] = $item ['id'];
					}  elseif (isset ( $item ['query'] ['cat'] )) { // CAT identifier for categories
						// No override already more specific itemid for a certain language
						if (isset ( $lookup [$option] [$view] [$item ['query'] ['cat']] )) {
							$alreadyAssigned = $lookup [$option] [$view] [$item ['query'] ['cat']];
							$alreadyAssignedLanguage = $items [$alreadyAssigned] ['language'];
							if ($alreadyAssignedLanguage != '*' && $item ['language'] == '*') {
								continue;
							}
						}
						// Standard assignment using catid query identifier
						$lookup [$option] [$view] [$item ['query'] ['cat']] = $item ['id'];
					} elseif (isset($manifestObject['holdon_params']) && isset($manifestObject['name_params'][$view])) {
						// Try to guess an id param called = view name in the params json serialized for the menu
						$menuParams = json_decode ( $item ['params'] );
						if(isset($menuParams->{$manifestObject['name_params'][$view]})) {  // CUSTOM identifier for categories
							// Add lookups for both single or multiple view params ids
							if(is_array($menuParams->{$manifestObject['name_params'][$view]})) {
								foreach ($menuParams->{$manifestObject['name_params'][$view]} as $id) {
									// No override already more specific itemid for a certain language
									if (isset ( $lookup [$option] [$view] [$id] )) {
										$alreadyAssigned = $lookup [$option] [$view] [$id];
										$alreadyAssignedLanguage = $items [$alreadyAssigned] ['language'];
										if ($alreadyAssignedLanguage != '*' && $item ['language'] == '*') {
											continue;
										}
									}
									// Standard assignment using custom query identifier
									$lookup [$option] [$view] [$id] = $item ['id'];
								}
							} else {
								// No override already more specific itemid for a certain language
								if (isset ( $lookup [$option] [$view] [$menuParams->{$manifestObject['name_params'][$view]}] )) {
									$alreadyAssigned = $lookup [$option] [$view] [$menuParams->{$manifestObject['name_params'][$view]}];
									$alreadyAssignedLanguage = $items [$alreadyAssigned] ['language'];
									if ($alreadyAssignedLanguage != '*' && $item ['language'] == '*') {
										continue;
									}
								}
								// Standard assignment using custom query identifier
								$lookup [$option] [$view] [$menuParams->{$manifestObject['name_params'][$view]}] = $item ['id'];
							}
						} else {
							// Assign directly to view array name, as a view of fallback categories/frontpage/main entrypoint
							$lookup [$option] [$view] [] = $item ['id'];  // NO identifier found for the view, fallback to view generic scope
						}
					} elseif (isset($manifestObject['name_params'][$view])) {
						$customField = $manifestObject['name_params'][$view];
						if(isset($item ['query'] [$customField])) {  // CUSTOM identifier for categories
							// No override already more specific itemid for a certain language
							if (isset($item ['query'] [$customField]) && isset ( $lookup [$option] [$view] [$item ['query'] [$customField]] )) {
								$alreadyAssigned = $lookup [$option] [$view] [$item ['query'] [$customField]];
								$alreadyAssignedLanguage = $items [$alreadyAssigned] ['language'];
								if ($alreadyAssignedLanguage != '*' && $item ['language'] == '*') {
									continue;
								}
							}
							// Standard assignment using catid query identifier
							$lookup [$option] [$view] [$item ['query'] [$customField]] = $item ['id'];
						} else {
							// Assign directly to view array name, as a view of fallback categories/frontpage/main entrypoint
							$lookup [$option] [$view] [] = $item ['id'];  // NO identifier found for the view, fallback to view generic scope
						}
					}  elseif (isset($manifestObject['additional_custom_fields'][$view])) {
						$customField = $manifestObject['additional_custom_fields'][$view];
						if(isset($item ['query'] [$customField])) {  // CUSTOM identifier for generic elements in the query string not in params used when in params is also active
							// No override already more specific itemid for a certain language
							if (isset($item ['query'] [$customField]) && isset ( $lookup [$option] [$view] [$item ['query'] [$customField]] )) {
								$alreadyAssigned = $lookup [$option] [$view] [$item ['query'] [$customField]];
								$alreadyAssignedLanguage = $items [$alreadyAssigned] ['language'];
								if ($alreadyAssignedLanguage != '*' && $item ['language'] == '*') {
									continue;
								}
							}
							// Standard assignment using catid query identifier
							$lookup [$option] [$view] [$item ['query'] [$customField]] = $item ['id'];
						} else {
							// Assign directly to view array name, as a view of fallback categories/frontpage/main entrypoint
							$lookup [$option] [$view] [] = $item ['id'];  // NO identifier found for the view, fallback to view generic scope
						}
					} else {
						// Assign directly to view array name, as a view of fallback categories/frontpage/main entrypoint
						$lookup [$option] [$view] [] = $item ['id'];  // NO identifier found for the view, fallback to view generic scope
					}
				}
			}
		}
		
		// Search the specific link needles against the lookup array previously built to find an Itemid match
		if ($needles) {
			foreach ( $needles as $view => $ids ) {
				if (isset ( $lookup [$option] [$view] )) {
					foreach ( $ids as $id ) {
						if (isset ( $lookup [$option] [$view] [$id] )) {
							return $lookup [$option] [$view] [$id];
						}
						if (isset ( $lookup [$option] [$view] [0] ) && $id === - 1) {
							return $lookup [$option] [$view] [0];
						}
					}
				}
			}
		} else {
			// No Itemid match found, fallback on current menu Itemid of JSitemap generation, won't work and will be unuseful, use the manual dropdown system :(
			$active = $menus->getActive ();
			if ($active) {
				return $active ['id'];
			}
		}
		
		return null;
	}
	
	/**
	 * Get the item route based on complex fallbacks chain for categories and views
	 *
	 * @access public
	 * @param string $option        	
	 * @param string $thisView
	 * @param int $id
	 * @param object $elm
	 * @param string $mainTable        	
	 * @return mixed Returns an integer if an Itemid is found, false otherwise
	 */
	public static function getItemRoute($option, $thisView, $id, $elm, $mainTable) {
		static $manifests;
		
		if (! $manifests) {
			$manifests = self::loadRouteManifests ();
		}
		if (! array_key_exists ( $option, $manifests )) {
			return false;
		}
		
		// Cover cases when view name is not set and used by component links BUT is always set in the menu links and so for Lookup array
		if(!$thisView) {
			// Orphan view detected, help rescue view to find a match!
			$rescueViewsMapping = $manifests[$option]['views_rescue'];
			foreach ($rescueViewsMapping as $checkValue=>$view) {
				if(property_exists($elm, $checkValue)) {
					$thisView = $view;
					break;
				}
			}
		}
		
		// By default the needle is the exact view for this item, valid for items and categories itself
		if ($id) {
			$needles = array (
					$thisView => array (
							( int ) $id 
					) 
			);
			// Add if any additional custom components entity views
			if(isset($manifests [$option] ['additional_id_view'])) {
				$needles [$manifests [$option] ['additional_id_view']] = array (
						( int ) $id
				);
			}
			
			// Are we dealing with a view for items that are categories itself?
			if (preg_match ( '/categor|cats|catg/i', $mainTable )) {
				// Find all parents category if supported
				if(isset($manifests [$option] ['categories_parent_id'])) {
					$categoryIdentifier = isset($manifests[$option]['categories_table_id_field']) ? $manifests[$option]['categories_table_id_field'] : 'id';
					$parentCats = array ();
					$parentCats = self::findAllParents ( $option, $manifests [$option] ['categories_table'], $manifests [$option] ['categories_parent_id'], $id, $parentCats, $categoryIdentifier);
					if (! empty ( $parentCats )) {
						$needles [$thisView] = array_merge ( $needles [$thisView], $parentCats );
					}
				}
			} else {
				// Find all parents category if supported
				if(isset($manifests [$option] ['override_priority_view'])) {
					foreach ($manifests [$option] ['override_priority_view'] as $fallBackView) {
						$needles [$fallBackView] = array (
								- 1
						);
					}
				}
			}
		}
		
		// If there is also a categorization and catid add more needles as fallback
		$catid = isset ( $elm->jsitemap_category_id ) ? $elm->jsitemap_category_id : null;
		if ($catid) {
			$needles ['category'] = array (
					( int ) $catid 
			);
			
			// Not casted to int, compatibility with slug as for Docman
			$needles ['list'] = array (
					$catid 
			);
			
			// Add if any additional custom components categories views
			if(isset($manifests [$option] ['additional_categories_needles'])) {
				$needles [$manifests [$option] ['additional_categories_needles']] = array (
						( int ) $catid
				);
			}
			
			// Add if any additional custom components categories views
			if(isset($manifests [$option] ['additional_categories_items_needles'])) {
				$needles [$manifests [$option] ['additional_categories_items_needles']] = array (
						( int ) $catid
				);
			}
			
			// Find all parents category if supported
			if(isset($manifests [$option] ['categories_parent_id'])) {
				$categoryIdentifier = isset($manifests[$option]['categories_table_id_field']) ? $manifests[$option]['categories_table_id_field'] : 'id';
				$parentCats = array ();
				$parentCats = self::findAllParents ( $option, $manifests [$option] ['categories_table'], $manifests [$option] ['categories_parent_id'], $catid, $parentCats, $categoryIdentifier );
				if (! empty ( $parentCats )) {
					$needles ['category'] = array_merge ( $needles ['category'], $parentCats );
					$needles ['list'] = $needles ['category'];
					// Check if custom categories views are set and so update them also
					if(isset($manifests [$option] ['additional_categories_needles'])) {
						$needles [$manifests [$option] ['additional_categories_needles']] = $needles ['category'];
					}
					if(isset($manifests [$option] ['additional_categories_items_needles'])) {
						$needles [$manifests [$option] ['additional_categories_items_needles']] = $needles ['category'];
					}
				}
			}
		}
		
		// Check if the categories fallback is not already set before define -1 default
		if(!isset($needles['categories'])) {
			$needles ['categories'] = array (
					- 1 
			);
		}
		$needles ['frontpage'] = array (
				- 1 
		);
		$needles ['home'] = array (
				- 1
		);
		$needles ['front'] = array (
				- 1
		);
		
		// If the component works using category_id = 0 as the fallback view treated as the home, add a fallback needles accordingly 
		if(isset($manifests [$option] ['category_zero_as_home'])) {
			// Check if the array has been initialized by items entity case, otherwise it could be a direct category entity case
			if(!isset($needles ['category'])) {
				$needles ['category'] = array ();
			}
			
			// Push standard category view as zero leading root
			array_push($needles ['category'], 0);
			array_push($needles ['list'], 0);
			
			// Add if any additional custom components categories views as zero leading root
			if(isset($manifests [$option] ['additional_categories_needles'])) {
				array_push($needles [$manifests [$option] ['additional_categories_needles']], 0);
			}
			
			if(isset($manifests [$option] ['additional_categories_items_needles'])) {
				array_push($needles [$manifests [$option] ['additional_categories_items_needles']], 0);
			}
		}
		
		// Find all parents category if supported
		if(isset($manifests [$option] ['fallback_views'])) {
			foreach ($manifests [$option] ['fallback_views'] as $fallBackView) {
				$needles [$fallBackView] = array (
						- 1
				);
			}
		}
		
		if ($foundItemid = self::findItem ( $option, $needles, $manifests [$option], $thisView )) {
			return $foundItemid;
		}
		
		return false;
	}
}