<?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' ); 
jimport( 'joomla.application.component.view');

class rseventsViewlocations extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
		$document = & JFactory::getDocument();
		$db = & JFactory::getDBO();

		$rseventsConfig = $mainframe->getUserState('rseventsConfig');
		
		$Itemid = intval(JRequest::getVar('Itemid','','request'));
		$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		$this->assignRef('itemId',$itemId);
		$this->assignRef('Itemid',$Itemid);
		
		$layout=JRequest::getVar('layout','','request');
		switch($layout)
		{
			default:
			{
				
				$listLocations=$this->get('data');
				$this->assignRef('data',$listLocations);
				
				
				$pathway 	= & $mainframe->getPathWay();
				$pathway->addItem(JText::_('RSE_BC_LISTLOCATIONS'), '');

			} break;
			
			
			case 'addeditlocation':
			{	
				global $mainframe;
				$user = & JFactory::getUser();
				//get permission for creating a new location
				$locationPermission = $this->get('LocationPermission');
				$this->assignRef('rseventsConfig',$rseventsConfig);
				
				if($locationPermission || $user->usertype == "Super Administrator")
				{
				
				//get location details
				$location=$this->get('Location');
				$this->assignRef('data',$location);
				} else 
					$mainframe->redirect('index.php?option=com_rsevents&view=locations'.$itemId,JText::_('RSE_LOCATION_NO_PERMISSION'));
				
				$pathway 	= & $mainframe->getPathWay();
				$pathway->addItem(JText::_('RSE_BC_ADDESITLOCATION'), '');
				
			} break;
			
			case 'show':
			{
				//load the location details
				$locationDetails=$this->get('Location');
				$this->assignRef('data',$locationDetails);	
				
				$pathway 	= & $mainframe->getPathWay();
				$pathway->addItem($locationDetails->LocationName, '');
				
				//load the google map
				if($rseventsConfig['locations.gmaps'])
				$map = rseventsModelLocations::getMap($locationDetails->IdLocation);
				else $map = '';
				
				$this->assignRef('map',$map);
					
			} break;
			
			case 'refreshlocations':
				$db->setQuery("SELECT IdLocation, LocationName FROM #__rsevents_locations WHERE published = 1 ORDER BY ordering");
				$locations = $db->loadObjectList();
				$loc_str = array();
				if(!empty($locations))
					foreach($locations as $locationObj)
						$loc_str[] = $locationObj->IdLocation.'|'.$locationObj->LocationName;
				echo implode("\n",$loc_str);
				exit();
			break;
			
		}		
		parent::display($tpl);
	}
}