Created
January 16, 2014 02:36
-
-
Save skylying/8448893 to your computer and use it in GitHub Desktop.
補助通 Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Joomla.Site | |
* @subpackage com_paycare | |
* @author Simon ASika <asika32764@gmail.com> | |
* @copyright Copyright (C) 2013 Asikart. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE.txt | |
*/ | |
// No direct access | |
defined('_JEXEC') or die; | |
/** | |
* PaycareBuildRoute | |
* | |
* @param array &$query A named array | |
* | |
* @return array | |
*/ | |
function PaycareBuildRoute(&$query) | |
{ | |
$segments = array(); | |
$params = array( | |
'view', | |
'id' | |
); | |
$app = JFactory::getApplication(); | |
$menu = $app->getMenu(); | |
$com_menu = $menu->getItems(array('component'), array('com_paycare')); | |
if (isset($com_menu[0])) | |
{ | |
$menuitem = $com_menu[0]; | |
$query['Itemid'] = $menuitem->id; | |
} | |
$view = JArrayHelper::getValue($query, 'view'); | |
switch ($view) | |
{ | |
case 'items': | |
$query['view'] = '補助搜尋'; | |
break; | |
case 'item': | |
$query['view'] = '補助'; | |
break; | |
case 'article': | |
$query['view'] = '文章'; | |
break; | |
} | |
// For pagination | |
if (isset($query['start']) || isset($query['limitstart'])) | |
{ | |
$query['view'] = '補助搜尋'; | |
} | |
// No menu match, use rest uri | |
// ================================================================================================ | |
foreach ($params as $param) | |
{ | |
if (isset($query[$param])) | |
{ | |
$seg = $query[$param]; | |
if ($param == 'id' && isset($query['alias'])) | |
{ | |
$seg .= '-' . $query['alias']; | |
} | |
$segments[] = $seg; | |
unset($query[$param]); | |
} | |
} | |
// Unset needless query | |
// ================================================================================================ | |
unset($query['alias']); | |
return $segments; | |
} | |
/** | |
* PaycareParseRoute | |
* | |
* @param array $segments A named array. | |
* | |
* Formats: | |
* index.php?/item (item in menu) | |
* index.php?/category/xx-item (item in category menu) | |
* index.php?/categories (category menu) | |
* | |
* @return array | |
*/ | |
function PaycareParseRoute($segments) | |
{ | |
$vars = array(); | |
// View is always the first element of the array | |
$count = count($segments); | |
$params = array( | |
'view', | |
'id' | |
); | |
$lastSeg = array_pop($segments); | |
$lastSeg = explode(':', $lastSeg); | |
if (is_numeric($lastSeg[0])) | |
{ | |
$segments[] = $lastSeg[0]; | |
} | |
$lastSeg = implode(':', $lastSeg); | |
$segments[] = $lastSeg; | |
// No menu found, use REST style | |
foreach ($params as $param) | |
{ | |
if ($count) | |
{ | |
$count--; | |
$segment = array_shift($segments); | |
switch ($segment) | |
{ | |
case '補助搜尋': | |
$segment = 'items'; | |
break; | |
case '文章': | |
$segment = 'article'; | |
break; | |
case '補助': | |
$segment = 'item'; | |
break; | |
} | |
if (is_numeric($segment)) | |
{ | |
$vars['id'] = $segment; | |
} | |
elseif ($segment) | |
{ | |
$vars[$param] = $segment; | |
} | |
} | |
} | |
return $vars; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment