<?php
/**
* @version 1.0.0
* @package RSEvents! 1.0.0
* @copyright (C) 2009 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

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

function rseventsGetAlias($from, $id)
{
	$db =& JFactory::getDBO();
	switch ($from)
	{
		case 'categories':
		$db->setQuery("SELECT CategoryAlias FROM #__rsevents_categories WHERE IdCategory='".(int) $id."' LIMIT 1");
		return $db->loadResult();
		break;
		
		case 'locations':
		$db->setQuery("SELECT LocationAlias FROM #__rsevents_locations WHERE IdLocation='".(int) $id."' LIMIT 1");
		return $db->loadResult();
		break;
		
		case 'events':
		$db->setQuery("SELECT EventAlias FROM #__rsevents_events WHERE IdEvent='".(int) $id."' LIMIT 1");
		return $db->loadResult();
		break;
	}
}

function rseventsBuildRoute(&$query)
{
	$segments = array();
	
	if (isset($query['cid']))
		$query['cid'] = (int) $query['cid'];
	if (isset($query['IdEvent']))
		$query['IdEvent'] = (int) $query['IdEvent'];
	
	// views
	if (isset($query['view']))
		switch ($query['view'])
		{
			/* Categories view */
			case 'categories':
			$segments[] = 'event-categories';
			break;
			
			/* Calendar view*/
			case 'calendar':
			if(!isset($query['layout']))
				$query['layout'] = 'default';
			
				switch($query['layout'])
				{
					default:
					$segments[] = 'calendar';
					if(isset($query['month']))
					$segments[] = $query['month'];
					if(isset($query['year']))
					$segments[] = $query['year'];
					if(isset($query['format']))
						$segments[] = $query['format'];
					unset($query['month']);
					unset($query['year']);
					unset($query['format']);
					break;
					
					case 'day':
					$segments[] = 'calendar-day';
					$segments[] = $query['date'];
					break;
					
					case 'week':
					$segments[] = 'calendar-week';
					$segments[] = $query['date'];
					break;
				}
			break;
			
			/* Events view */
			case 'events':
			if (isset($query['layout']))
				switch ($query['layout'])
				{
					default:
					$segments[] = 'events';
					break;
					
					case 'addeditevent':
					
					if (isset($query['cid']) && $query['cid'] > 0)
					{
						$segments[] = 'edit-event';
						$eventalias = rseventsGetAlias('events',$query['cid']);
						$segments[] = $query['cid'].'-'.$eventalias;
					}
					else
						$segments[] = 'add-event';
					if (isset($query['page']))
						$segments[] = $query['page'];
					break;
					
					case 'listcategoryevents':
					$categoryalias = rseventsGetAlias('categories',$query['cid']);
					$segments[] = 'event-category';
					$segments[] = $query['cid'].'-'.$categoryalias;
					break;
					
					case 'search':
					$segments[] = 'event-search';
					break;
					
					case 'listdaysevent':
					$segments[] = 'events-by-period';
					break;
					
					case 'listlocationevents':
					$locationalias = rseventsGetAlias('locations',$query['cid']);
					$segments[] = 'events-by-location';
					$segments[] = $query['cid'].'-'.$locationalias;
					break;
					
					case 'listmyevents':
					$segments[] = 'my-events';
					break;
					
					case 'show':
					$eventalias = rseventsGetAlias('events',$query['cid']);
					$segments[] = 'event';
					$segments[] = $query['cid'].'-'.$eventalias;
					break;
					
					case 'invite':
					$eventalias = rseventsGetAlias('events',$query['cid']);
					$segments[] = 'event-invite';
					$segments[] = $query['cid'].'-'.$eventalias;
					break;
					
					case 'guestssendmessage':
					$eventalias = rseventsGetAlias('events',$query['cid']);
					$segments[] = 'event-send-messages';
					$segments[] = $query['cid'].'-'.$eventalias;
					break;
					
					case 'subscribe':
					$eventalias = rseventsGetAlias('events',$query['cid']);
					$segments[] = 'event-subscribe';
					$segments[] = $query['cid'].'-'.$eventalias;
					break;
					
					case 'selectdesign':
					$segments[] = 'select-event-design';
					$segments[] = $query['cid'];
					break;
					
					case 'photos':
					$eventalias = rseventsGetAlias('events',$query['cid']);
					$segments[] = 'event-photos';
					$segments[] = $query['cid'].'-'.$eventalias;
					break;
					
					case 'wire':
					$eventalias = rseventsGetAlias('events',$query['cid']);
					$segments[] = 'event-payment-wire';
					$segments[] = $query['cid'].'-'.$eventalias;
					break;
					
					case 'album':
					$eventalias = rseventsGetAlias('events',$query['cid']);
					$segments[] = 'event-album';
					$segments[] = $query['cid'].'-'.$eventalias;
					break;
					
					case 'printevent':
					$eventalias = rseventsGetAlias('events',$query['cid']);
					$segments[] = 'event-print';
					$segments[] = $query['cid'].'-'.$eventalias;
					break;
					
					case 'youtube':
					$segments[] = 'edit-event-youtube';
					$eventalias = rseventsGetAlias('events',$query['IdEvent']);
					$segments[] = $query['IdEvent'].'-'.$eventalias;
					$segments[] = $query['cid'];
					break;
					
					case 'flash':
					$segments[] = 'edit-event-flash';
					$eventalias = rseventsGetAlias('events',$query['IdEvent']);
					$segments[] = $query['IdEvent'].'-'.$eventalias;
					$segments[] = $query['cid'];
					break;
					
					case 'mp3':
					$segments[] = 'edit-event-mp3';
					$eventalias = rseventsGetAlias('events',$query['IdEvent']);
					$segments[] = $query['IdEvent'].'-'.$eventalias;
					$segments[] = $query['cid'];
					break;
					
					case 'chat':
					$segments[] = 'edit-event-meebo';
					$eventalias = rseventsGetAlias('events',$query['IdEvent']);
					$segments[] = $query['IdEvent'].'-'.$eventalias;
					$segments[] = $query['cid'];
					break;
					
					case 'flickr':
					$segments[] = 'edit-event-flickr';
					$eventalias = rseventsGetAlias('events',$query['IdEvent']);
					$segments[] = $query['IdEvent'].'-'.$eventalias;
					$segments[] = $query['cid'];
					break;
				}
				else
					$segments[] = 'events';
			break;
			
			/* Files view */
			case 'files':
			if(!isset($query['layout'])) $query['layout'] = 'default';
				switch ($query['layout'])
				{					
					default:
					$eventalias = rseventsGetAlias('events',$query['IdEvent']);
					$segments[] = 'event-edit-file';
					$segments[] = $query['cid'];
					$segments[] = $query['IdEvent'].'-'.$eventalias;
					break;
				}
			break;
			
			/* Subscriptions view */
			case 'subscriptions':
			
			if (!isset($query['layout'])) $query['layout'] = 'default';
				switch ($query['layout'])
				{
					default:
					$eventalias = rseventsGetAlias('events',$query['IdEvent']);
					$segments[] = 'event-subscriptions';
					$segments[] = $query['IdEvent'].'-'.$eventalias;
					break;
					
					case 'edit':
					$eventalias = rseventsGetAlias('events',$query['IdEvent']);
					$segments[] = 'event-edit-subscription';
					$segments[] = $query['cid'];
					$segments[] = $query['IdEvent'].'-'.$eventalias;
					break;
				}
			break;
			
			/* Tickets view */
			case 'tickets':
			if (!isset($query['layout'])) $query['layout'] = 'default';
				switch ($query['layout'])
				{
					default:
					$eventalias = rseventsGetAlias('events',$query['cid']);
					$segments[] = 'event-tickets';
					$segments[] = $query['cid'].'-'.$eventalias;
					break;
					
					case 'edit':
					$eventalias = rseventsGetAlias('events',$query['IdEvent']);
					$segments[] = 'event-edit-ticket';
					$segments[] = $query['cid'].'-'.$eventalias;
					$segments[] = $query['IdEvent'];
					break;
				}
			break;
			
			/* Locations view */
			case 'locations':
			if (isset($query['layout']))
				switch ($query['layout'])
				{
					default:
					$segments[] = 'locations';
					break;
					
					case 'addeditlocation':
					if (isset($query['cid']) && $query['cid'] > 0)
					{
						$segments[] = 'edit-location';
						$locationalias = rseventsGetAlias('locations',$query['cid']);
						$segments[] = $query['cid'].'-'.$locationalias;
					}
					else
						$segments[] = 'add-location';
					break;
					
					case 'show':
					$segments[] = 'show-location';
					$locationalias = rseventsGetAlias('locations',$query['cid']);
					$segments[] = $query['cid'].'-'.$locationalias;
					$segments[] = @$query['IdEvent'];
					break;
				}
				else
					$segments[] = 'locations';
			break;
		}
	
	// tasks
	if (isset($query['task']))
		switch ($query['task'])
		{
			
			case 'exportguests':
			$segments[] = 'export-event-subscriptions';
			$eventalias = rseventsGetAlias('events',$query['cid']);
			$segments[] = $query['cid'].'-'.$eventalias;
			break;
			
			case 'deleteicon':
			$segments[] = 'event-delete-icon';
			$eventalias = rseventsGetAlias('events',$query['cid']);
			$segments[] = $query['cid'].'-'.$eventalias;
			break;
			
			case 'generate':
			$segments[] = 'event-generate-private-url';
			$eventalias = rseventsGetAlias('events',$query['cid']);
			$segments[] = $query['cid'].'-'.$eventalias;
			break;
			
			case 'duplicateevent':
			$segments[] = 'duplicate-event';
			$eventalias = rseventsGetAlias('events',$query['cid']);
			$segments[] = $query['cid'].'-'.$eventalias;
			break;
			
			case 'postreminder':
			$segments[] = 'post-reminder';
			$eventalias = rseventsGetAlias('events',$query['cid']);
			$segments[] = $query['cid'].'-'.$eventalias;
			break;
			
			case 'cancelevent':
			$segments[] = 'cancel-event';
			$eventalias = rseventsGetAlias('events',$query['cid']);
			$segments[] = $query['cid'].'-'.$eventalias;
			break;
			
			case 'addoutlook':
			$segments[] = 'add-to-outlook-calendar';
			$eventalias = rseventsGetAlias('events',$query['cid']);
			$segments[] = $query['cid'].'-'.$eventalias;
			break;
			
			case 'eventunsubscribe':
			$segments[] = 'unsubscribe-event';
			$eventalias = rseventsGetAlias('events',$query['cid']);
			$segments[] = $query['cid'].'-'.$eventalias;
			break;
			
			case 'deleteticket':
			$segments[] = 'delete-event-ticket';
			$eventalias = rseventsGetAlias('events',$query['IdEvent']);
			$segments[] = $query['IdEvent'].'-'.$eventalias;
			$segments[] = $query['cid'];
			break;
			
			case 'deleteextra':
			$segments[] = 'delete-event-extra';
			$eventalias = rseventsGetAlias('events',$query['IdEvent']);
			$segments[] = $query['IdEvent'].'-'.$eventalias;
			$segments[] = $query['cid'];
			break;
			
			case 'deletefile':
			$segments[] = 'delete-event-file';
			$eventalias = rseventsGetAlias('events',$query['IdEvent']);
			$segments[] = $query['IdEvent'].'-'.$eventalias;
			$segments[] = $query['cid'];
			break;
					
			
			case 'showlocation':
			$segments[] = 'event-location';
			$eventalias = rseventsGetAlias('events',$query['IdEvent']);
			$segments[] = $query['IdEvent'].'-'.$eventalias;
			$locationalias = rseventsGetAlias('locations',$query['cid']);
			$segments[] = $query['cid'].'-'.$locationalias;
			break;
			
			case 'accepted':
			$segments[] = 'accept-event-subscription';
			$eventalias = rseventsGetAlias('events',$query['IdEvent']);
			$segments[] = $query['IdEvent'].'-'.$eventalias;
			$segments[] = $query['cid'];
			break;
			
			case 'pending':
			$segments[] = 'pending-event-subscription';
			$eventalias = rseventsGetAlias('events',$query['IdEvent']);
			$segments[] = $query['IdEvent'].'-'.$eventalias;
			$segments[] = $query['cid'];
			break;
			
			case 'denied':
			$segments[] = 'deny-event-subscription';
			$eventalias = rseventsGetAlias('events',$query['IdEvent']);
			$segments[] = $query['IdEvent'].'-'.$eventalias;
			$segments[] = $query['cid'];
			break;
			
			case 'delete':
			$segments[] = 'delete-event-subscription';
			$eventalias = rseventsGetAlias('events',$query['IdEvent']);
			$segments[] = $query['IdEvent'].'-'.$eventalias;
			$segments[] = $query['cid'];
			break;
			
			case 'listlocationevents':
			$locationalias = rseventsGetAlias('locations',$query['cid']);
			$segments[] = 'list-events-in-location';
			$segments[] = $query['cid'].'-'.$locationalias;
			break;
			
			case 'twoco':
			$segments[] = 'payment-2co';
			break;
			
		}
	
	unset($query['view'], $query['layout'], $query['controller'], $query['task'], $query['cid'], $query['IdEvent'], $query['page'] ,$query['date']);
	
	return $segments;
}

function rseventsParseRoute($segments)
{
	$query = array();
	
	$segments[0] = str_replace(':','-',$segments[0]);
	
	switch ($segments[0])
	{
		case 'event-categories':
		$query['view'] = 'categories';
		break;
		
		case 'events':
		$query['view'] = 'events';
		$query['layout'] = 'default';
		break;
		
		case 'calendar':
		$query['view'] = 'calendar';
		$query['month'] = @$segments[1];
		$query['year'] = @$segments[2];
		if(isset($query['format']))
			$query['format'] = 'raw';
		if(isset($query['tmpl']))
			$query['tmpl'] = 'component';
		
		break;
		
		case 'calendar-day':
		$query['view'] = 'calendar';
		$query['layout'] = 'day';
		$query['date'] = $segments[1].'/'.$segments[2].'/'.$segments[3];
		break;
		
		case 'calendar-week':
		$query['view'] = 'calendar';
		$query['layout'] = 'week';
		$query['date'] = $segments[1].'/'.$segments[2].'/'.$segments[3];
		break;
		
		case 'add-event':
		$query['view'] = 'events';
		$query['layout'] = 'addeditevent';
		break;
		
		case 'edit-event':
		$query['view'] = 'events';
		$query['layout'] = 'addeditevent';
		$query['cid'] = @$segments[1];
		if (isset($segments[2]))
			$query['page'] = $segments[2];
		break;
		
		case 'event-category':
		$query['view'] = 'events';
		$query['layout'] = 'listcategoryevents';
		$query['cid'] = $segments[1];
		break;
		
		case 'event-search':
		$query['view'] = 'events';
		$query['layout'] = 'search';
		break;
		
		case 'events-by-period':
		$query['view'] = 'events';
		$query['layout'] = 'listdaysevent';
		break;
		
		case 'events-by-location':
		$query['view'] = 'events';
		$query['layout'] = 'listlocationevents';
		$query['cid'] = $segments[1];
		break;
		
		case 'show-location':
		$query['view'] = 'locations';
		$query['layout'] = 'show';
		$query['cid'] = $segments[1];
		$query['IdEvent'] = @$segments[2];
		break;
		
		case 'my-events':
		$query['view'] = 'events';
		$query['layout'] = 'listmyevents';
		break;
		
		case 'event-invite':
		$query['view'] = 'events';
		$query['layout'] = 'invite';
		$query['cid'] = @$segments[1];
		break;
		
		case 'event-subscribe':
		$query['view'] = 'events';
		$query['layout'] = 'subscribe';
		$query['cid'] = @$segments[1];
		break;
		
		case 'event-payment-wire':
		$query['view'] = 'events';
		$query['layout'] = 'wire';
		$query['cid'] = @$segments[1];
		break;
		
		case 'event-photos':
		$query['view'] = 'events';
		$query['layout'] = 'photos';
		$query['cid'] = @$segments[1];
		break;
		
		case 'event-album':
		$query['view'] = 'events';
		$query['layout'] = 'album';
		$query['cid'] = @$segments[1];
		break;
		
		case 'event-tickets':
		$query['view'] = 'tickets';
		$query['layout'] = 'default';
		$query['cid'] = @$segments[1];
		break;
		
		case 'event-edit-ticket':
		$query['view'] = 'tickets';
		$query['layout'] = 'edit';
		$query['cid'] = @$segments[1];
		$query['IdEvent'] = @$segments[2];
		break;
		
		case 'event-print':
		$query['view'] = 'events';
		$query['layout'] = 'printevent';
		$query['cid'] = @$segments[1];
		$query['tmpl'] = 'component';
		break;
		
		case 'event-send-messages':
		$query['view'] = 'events';
		$query['layout'] = 'guestssendmessage';
		$query['cid'] = @$segments[1];
		break;
		
		case 'event-subscriptions':
		$query['view'] = 'subscriptions';
		$query['layout'] = 'default';
		$query['IdEvent'] = @$segments[1];
		break;
		
		case 'event-edit-subscription':
		$query['view'] = 'subscriptions';
		$query['layout'] = 'edit';
		$query['cid'] = $segments[1];
		$query['IdEvent'] = @$segments[2];
		break;
		
		case 'locations':
		$query['view'] = 'locations';
		if (isset($segments[1]))
			switch ($segments[1])
			{	
				case 'edit-location':
				$query['layout'] = 'addeditlocation';
				$query['cid'] = $segments[2];
				break;
				
				case 'add-location':
				$query['layout'] = 'addeditlocation';
				break;
			}
		break;
		
		case 'event':
		$query['task'] = 'showevent';
		$query['cid'] = @$segments[1];
		break;
		
		case 'edit-event':
		$query['task'] = 'editevent';
		$query['cid'] = $segments[1];
		if (isset($segments[2]))
			$query['page'] = $segments[2];
		break;
		
		case 'export-event-subscriptions':
		$query['task'] = 'exportguests';
		$query['controller'] = 'events';
		$query['cid'] = $segments[1];
		break;
		
		case 'event-delete-icon':
		$query['task'] = 'deleteicon';
		$query['controller'] = 'events';
		$query['cid'] = $segments[1];
		break;
		
		case 'event-generate-private-url':
		$query['task'] = 'generate';
		$query['controller'] = 'events';
		$query['cid'] = $segments[1];
		break;
		
		case 'list-event-subscriptions':
		$query['task'] = 'listsubscriptions';
		$query['IdEvent'] = @$segments[1];
		break;
		
		case 'edit-event-subscriptions':
		$query['task'] = 'editsubscription';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = @$segments[2];
		break;
		
		case 'duplicate-event':
		$query['task'] = 'duplicateevent';
		$query['cid'] = $segments[1];
		break;
		
		case 'post-reminder':
		$query['task'] = 'postreminder';
		$query['controller'] = 'events';
		$query['cid'] = $segments[1];
		break;
		
		case 'cancel-event':
		$query['task'] = 'cancelevent';
		$query['controller'] = 'events';
		$query['cid'] = $segments[1];
		break;
		
		case 'add-to-outlook-calendar':
		$query['task'] = 'addoutlook';
		$query['controller'] = 'events';
		$query['cid'] = $segments[1];
		break;
		
		case 'print-event':
		$query['task'] = 'printevent';
		$query['cid'] = $segments[1];
		$query['tmpl'] = 'component';
		break;
		
		case 'unsubscribe-event':
		$query['task'] = 'eventunsubscribe';
		$query['controller'] = 'events';
		$query['cid'] = $segments[1];
		break;
		
		case 'subscribe-event':
		$query['task'] = 'eventsubscribe';
		$query['cid'] = @$segments[1];
		break;
		
		case 'add-location':
		$query['task'] = 'addeditlocation';
		break;
		
		case 'list-event-tickets':
		$query['task'] = 'listtickets';
		$query['cid'] = $segments[1];
		break;
		
		case 'add-event-ticket':
		$query['task'] = 'editticket';
		$query['IdEvent'] = @$segments[1];
		$query['cid'] = 0;
		break;
		
		case 'edit-event-ticket':
		$query['task'] = 'editticket';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = @$segments[2];
		break;
		
		case 'delete-event-ticket':
		$query['task'] = 'deleteticket';
		$query['controller'] = 'tickets';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		break;
		
		case 'select-event-design':
		$query['task'] = 'selectdesign';
		$query['cid'] = $segments[1];
		$query['tmpl'] = 'component';
		break;
		
		case 'edit-event-youtube':
		$query['task'] = 'edityoutube';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		$query['tmpl'] = 'component';
		break;
		
		case 'edit-event-flash':
		$query['task'] = 'editflash';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		$query['tmpl'] = 'component';
		break;
		
		case 'edit-event-mp3':
		$query['task'] = 'editmp3';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		$query['tmpl'] = 'component';
		break;
		
		case 'edit-event-meebo':
		$query['task'] = 'editmeebo';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		$query['tmpl'] = 'component';
		break;
		
		case 'edit-event-flickr':
		$query['task'] = 'editflickr';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		$query['tmpl'] = 'component';
		break;
		
		case 'delete-event-extra':
		$query['task'] = 'deleteextra';
		$query['controller'] = 'events';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		break;
		
		case 'event-edit-file':
		$query['view'] = 'files';
		$query['layout'] = 'default';
		$query['cid'] = $segments[1];
		$query['IdEvent'] = $segments[2];
		$query['tmpl'] = 'component';
		break;
		
		case 'delete-event-file':
		$query['task'] = 'deletefile';
		$query['controller'] = 'files';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		break;
		
		case 'event-location':
		$query['task'] = 'showlocation';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		break;
		
		case 'accept-event-subscription':
		$query['task'] = 'accepted';
		$query['controller'] = 'subscriptions';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		break;
		
		case 'pending-event-subscription':
		$query['task'] = 'pending';
		$query['controller'] = 'subscriptions';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		break;
		
		case 'deny-event-subscription':
		$query['task'] = 'denied';
		$query['controller'] = 'subscriptions';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		break;
		
		case 'delete-event-subscription':
		$query['task'] = 'delete';
		$query['controller'] = 'subscriptions';
		$query['IdEvent'] = $segments[1];
		$query['cid'] = $segments[2];
		break;
		
		case 'list-events-in-location':
		$query['task'] = 'listlocationevents';
		$query['cid'] = $segments[1];
	//	if(isset($query[]))
		break;
		
		case 'payment-2co':
		$query['task'] = 'twoco';
		break;
	}
	
	return $query;
}
?>