<?php
/**
* @version		3.0.0
* @package		JomLand TicketCenter
* @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
* @link 		www.jomland.com
* @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

function JLTicketCenterBuildRoute(&$query)
{
	
	$segments = array();
	
	$app	= JFactory::getApplication();
	$menu	= $app->getMenu();
	$params	= JComponentHelper::getParams('com_contact');
	$advanced = $params->get('sef_advanced_link', 0);

	if (empty($query['Itemid'])) {
		$menuItem = $menu->getActive();
	} else {
		$menuItem = $menu->getItem($query['Itemid']);
	}

	if (isset($query['view'])){
		$mView	= $query['view'];
	}else{
		$mView		= (empty($menuItem->query['view'])) ? null : $menuItem->query['view'];
		$mCat_Id	= (empty($menuItem->query['cat_id'])) ? null : $menuItem->query['cat_id'];
		$mCity_Id	= (empty($menuItem->query['city_id'])) ? null : $menuItem->query['city_id'];
		$mCountry_Id= (empty($menuItem->query['country'])) ? null : $menuItem->query['country'];
		$mPerformer = (empty($menuItem->query['performer_id'])) ? null : $menuItem->query['performer_id'];
		$mState		= (empty($menuItem->query['state'])) ? null : $menuItem->query['state'];
		$mVenue		= (empty($menuItem->query['venue_id'])) ? null : $menuItem->query['venue_id'];
		
		$mCatid	= (empty($menuItem->query['cat_id'])) ? null : $menuItem->query['cat_id'];
		if (!empty($menuItem->query['view'])){
			if ($menuItem->query['view']=='venues'){
				$mVenueCity	= (empty($menuItem->query['city'])) ? null : $menuItem->query['city'];
				$mVenueState	= (empty($menuItem->query['state'])) ? null : $menuItem->query['state'];
				$mVenueCountry	= (empty($menuItem->query['country'])) ? null : $menuItem->query['country'];	
			}	
		}
		
		
	}
	$segments[] = $mView;
	
	if (isset($mView)){
		
		switch ($mView){
			case 'event':
				if(isset($mCat_Id)){
					$segments[] = 'category';
					$segments[] = $mCat_Id;
				}
				
				if(isset($mCity_Id)){
					$segments[] = 'city';
					$segments[] = $mCity_Id;
				}
				
				if(isset($mCountry_Id)){
					$segments[] = 'country';
					$segments[] = $mCountry_Id;
				}

				if(isset($mPerformer)){
					$segments[] = 'performer';
					$segments[] = $mPerformer;
				}
				
				
				if(isset($mState)){
					$segments[] = 'state';
					$segments[] = $mState;
				}
				
				
				if(isset($mVenue)){
					$segments[] = 'venue';
					$segments[] = $mVenue;
				}
					
				if(isset($query['performer'])){
			    	$segments[] = 'performer';
			    	$segments[] = $query['performer'];
			       	unset($query['performer']);
			     }
				
			     if(isset($query['venue'])){
			    	$segments[] = 'venue';
			    	$segments[] = $query['venue'];
			       	unset( $query['venue'] );
			     }
			     
				if(isset($query['city'])){
			    	$segments[] = 'city';
			    	$segments[] = $query['city'];
			       	unset($query['city']);
			     }
				if(isset($query['state'])){
			    	$segments[] = 'state';
			    	$segments[] = $query['state'];
			       	unset($query['state']);
			     }
				if(isset($query['country'])){
			    	$segments[] = 'country';
			    	$segments[] = $query['country'];
			       	unset($query['country']);
			     }
			break;
			
			case 'tickets':
				if ($menuItem != null && $menuItem->query['view'] == 'tickets'){
					$segments[] = $menuItem->query['id'];
					 
				}else{
					$segments[] = $query['id'];
				}
	       		
				unset($query['id']);
			break;
			
			case 'venues':
				
				if(isset($mVenueCity)){
					$segments[] = 'city';
			    	$segments[] = $mVenueCity;
				}
				
				if(isset($mVenueState)){
					$segments[] = 'state';
			    	$segments[] = $mVenueState;
				}
				
				if(isset($mVenueCountry)){
					$segments[] = 'country';
			    	$segments[] = $mVenueCountry;
				}
				
				if(isset($query['city'])){
			    	$segments[] = 'city';
			    	$segments[] = $query['city'];
			       	unset($query['city']);
			     }
			     
				if(isset($query['state'])){
			    	$segments[] = 'state';
			    	$segments[] = $query['state'];
			       	unset($query['state']);
			     }
		
			     if(isset($query['country'])){
			    	$segments[] = 'country';
			    	$segments[] = $query['country'];
			       	unset($query['country']);
			     }
			break;
			
			case 'category':
				
				if (empty($mCatid) && isset($query['id'])){
					$mCatid = $query['id'];
					unset($query['id']);	
				}
				
				if (!empty($mCatid)){
					$segments[] = $mCatid;					
				}
			break;
			
		}
	}
	
	//var_dump($segments);
	unset($query['view']);
	
	return $segments;
}

				
function JLTicketCenterParseRoute($segments)
{
	$vars = array();
	$vars['view'] = $segments[0];
	
	switch ($vars['view']){
		case 'event':
			if (!empty($segments[1])){
				$vars['layout'] =$segments[1];
				switch ($segments[1]){
					case 'performer':
						$vars['performer'] =$segments[2];
					break;
					case 'venue':
						$vars['venue'] =$segments[2]; 
					break;
					case 'city':
						$vars['city'] =$segments[2];
					break;
					
					case 'country':
						$vars['country'] =$segments[2];
					break;
					
					case 'category':
						$vars['cat_id'] =$segments[2];
					break;
	
					case 'state':
						$vars['state'] =$segments[2];
					break;
					
				}
			}
		break;
		
		case 'tickets':
			$app	= JFactory::getApplication();
			$menu	= $app->getMenu();
	
			$this_menuItem = $menu->getActive();
			if($this_menuItem != null && $this_menuItem->query['view']=='tickets'){
				$vars['id'] = $this_menuItem->query['id'];
			}
			else{
				$vars['id'] = $segments[1];
			}
			
		break;
		
		case 'venues':
			switch ($segments[1]){
				case 'city':
					$vars['city'] =$segments[2];
				break;
				case 'state':
					$vars['state'] =$segments[2]; 
				break;
				case 'country':
					$vars['country'] =$segments[2];
				break;
			}
		break;
		
		case 'category':
			if (isset($segments[1])){
				$vars['id'] = $segments[1];	
			}
			
		break;
		
		
	}   

    return $vars;
}


