<?php
/**
 * @package     SP Soccer
 * @author      JoomShaper http://www.joomshaper.com
 * @copyright   Copyright (C) 2010 - 2016 JoomShaper. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die('Restricted access');

function SpsoccerBuildRoute(&$query) {

	$app 		= JFactory::getApplication();
	$menu   	= $app->getMenu();

	$segments = array();

	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_spsoccer')
	{
		$menuItemGiven = false;
		unset($query['Itemid']);
	}

	if (isset($query['view']))
	{
		$view = $query['view'];
	}
	else
	{
		return $segments;
	}

	// ID
	if(isset($query['id']) && $query['id']) {
		$segments[] = $query['id'];
		unset($query['id']);
	}

	if (($menuItem instanceof stdClass) && $menuItem->query['view'] == $query['view']) {

		if (!$menuItemGiven) {
			$segments[] = $view;
		}
	}

	unset($query['view']);

	return $segments;

}


function SpsoccerParseRoute($segments) {

	$app 		= JFactory::getApplication();
	$menu   	= $app->getMenu();
	$item 		= $menu->getActive();
	$total 		= count($segments);
	$vars 		= array();

	//print_r($segments);
	switch ($item->query['view']) {
		case 'tournaments':
			if($total==1) {
				$vars['view'] = 'tournament';
				$vars['id'] = (int) $segments[0];
			}
			break;

		case 'matches':
			if($total==1) {
				$vars['view'] = 'match';
				$vars['id'] = (int) $segments[0];
			}
			break;

		case 'players':
			if($total==1) {
				$vars['view'] = 'player';
				$vars['id'] = (int) $segments[0];
			}
			break;

		case 'gameteams':
			if($total==1) {
				$vars['view'] = 'gameteam';
				$vars['id'] = (int) $segments[0];
			}
			break;

		case 'vanues':
			if($total==1) {
				$vars['view'] = 'vanue';
				$vars['id'] = (int) $segments[0];
			}
			break;
	}

	return $vars;
}
