Skip to content

Instantly share code, notes, and snippets.

@nielsnuebel
Created July 3, 2015 08:43
Show Gist options
  • Save nielsnuebel/1330c11383571bde9d51 to your computer and use it in GitHub Desktop.
Save nielsnuebel/1330c11383571bde9d51 to your computer and use it in GitHub Desktop.
<?php
/**
* NNProducts
* @package NNPRODUCTS
* @author Niels Nübel <[email protected]>
* @copyright Copyright (c) 2015 tec-promotion GmbH / NN-Medienagentur.de
* @license GNU General Public License version 2 or later
*/
defined('_JEXEC') or die;
/**
* Build the route for the com_nnproducts component
* @param array &$query An array of URL arguments
*
* @return array The URL arguments to use to assemble the subsequent URL.
*/
function NNProductsBuildRoute(&$query)
{
$segments = array();
// Get a menu item based on Itemid or currently active
$app = JFactory::getApplication();
$menu = $app->getMenu();
// We need a menu item. Either the one specified in the query, or the current active one if none specified
if (empty($query['Itemid']))
{
$menuItem = $menu->getActive();
$menuItemGiven = false;
}
else
{
$menuItem = $menu->getItem($query['Itemid']);
$menuItemGiven = true;
}
// Check again
if ($menuItemGiven && isset($menuItem) && $menuItem->component != 'com_nnproducts')
{
$menuItemGiven = false;
unset($query['Itemid']);
}
// Are we dealing with an article or category that is attached to a menu item?
if (($menuItem instanceof stdClass) && $menuItem->query['view'] == $query['view'] && ((isset($query['id']) && $menuItem->query['id'] == (int) $query['id']) || (isset($query['catid']) && $menuItem->query['catid'] == (int) $query['catid'])))
{
unset($query['view']);
if (isset($query['catid']))
{
unset($query['catid']);
}
if (isset($query['layout']))
{
unset($query['layout']);
}
unset($query['id']);
return $segments;
}
if (isset($query['view']))
{
//$segments[] = $query['view'];
}
else
{
// We need to have a view in the query or it is an invalid URL
return $segments;
}
if (isset($query['catid']))
{
$segments[] = $query['catid'];
unset($query['catid']);
};
if (isset($query['task']) && $query['task']=='product')
{
$segments[] = $query['task'];
unset($query['task']);
};
if (isset($query['id']))
{
$segments[] = $query['id'];
unset($query['id']);
};
unset($query['view']);
return $segments;
}
/**
* Parse the segments of a URL.
*
* @param array $segments The segments of the URL to parse.
*
* @return array The URL attributes to be used by the application.
*/
function NNProductsParseRoute($segments)
{
$vars = array();
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item = $menu->getActive();
// Count segments
$count = count($segments);
// Handle View and Identifier
switch ($item->query['view'])
{
case 'categories':
if ($count == 1)
{
$vars['view'] = 'category';
}
if ($count == 2)
{
$vars['view'] = 'article';
}
$id = explode(':', $segments[$count-1]);
$vars['id'] = (int) $id[0];
break;
case 'product':
$vars['view'] = 'product';
$id = explode(':', $segments[$count-1]);
$vars['id'] = (int) $id[0];
break;
case 'recipe':
$vars['view'] = 'recipe';
$id = explode(':', $segments[1]);
$vars['id'] = (int) $id[0];
break;
case 'recipes':
if ($count == 1)
{
$vars['view'] = 'recipe';
$id = explode(':', $segments[$count-1]);
$vars['id'] = (int) $id[0];
}
elseif ($count == 2)
{
$vars['view'] = 'recipes';
$vars['task'] = $segments[$count-2];
$id = explode(':', $segments[$count-1]);
$vars['id'] = (int) $id[0];
}
else
$vars['view'] = 'recipes';
break;
case 'category':
$id = $segments[$count-1];
$vars['catid'] = (int) $id[0];
$vars['view'] = 'category';
break;
}
return $vars;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment