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

class rseventsViewEvents extends JView
{
	function display($tpl = null)
	{
		global $mainframe , $option;
		$task=JRequest::getVar('task','','request');
		switch($task)
		{
			
			case 'listevents':
			{
				$db = & JFactory::getDBO();
				
				JToolBarHelper::title(JText::_('RSE_EVENT_LIST'),'generic.png');
				JToolBarHelper::addNewX('editevent');
				JToolBarHelper::editListX('editevent');
				JToolBarHelper::custom( 'copy', 'copy.png', 'copy_f2.png', 'Duplicate' );
				JToolBarHelper::deleteList("Are you sure you want to delete?", 'remove' , "Delete");
				JToolBarHelper::archiveList();
				JToolBarHelper::publishList();
				JToolBarHelper::unpublishList();
				
				JToolBarHelper::custom('main','preview.png','preview_f2.png',JText::_('RSE_RS_PRODUCT'),false);
				
				$filter			=JRequest::getVar('rs_filter');
				$category_filter = JRequest::getVar('rs_cat_filter');
				$location_filter=JRequest::getVar('rs_loc_filter');
				$sortColumn		=JRequest::getVar('filter_order','e.EventStartDate');
				$sortOrder	=JRequest::getVar('filter_order_Dir','desc');
				$listEvents=$this->get('data');
				$pagination=$this->get('pagination');
				
				// loads the settings
				$rseventsConfig = $mainframe->getUserState('rseventsConfig');
				$this->assignRef('rseventsConfig',$rseventsConfig);
				
				//archive old events
				if ($rseventsConfig['event.enable.autoarchive'] == 1)
				{
					$time_offset = $rseventsConfig['event.archive.days'] * 86400;
					$db->setQuery("SELECT IdEvent FROM #__rsevents_events WHERE ".time()." > EventEndDate + ".$time_offset." AND EventArchive = '0' ");
				
					$eventIds = $db->loadResultArray();
					$ids = implode(',',$eventIds);
					
					$db->setQuery("UPDATE #__rsevents_events SET published = '-1' WHERE IdEvent IN (".$ids.") ");
					$db->query();

					$db->setQuery("UPDATE #__rsevents_events SET EventArchive = '0' WHERE IdEvent IN (".$ids.") ");
					$db->query();

				}
				
				$sql = 'SELECT IdCategory, CategoryName  FROM #__rsevents_categories';
				$db->setQuery($sql);

				$catlist[]         = JHTML::_('select.option',  '0', JText::_( 'RSE_EVENT_SELECT_CATEGORY' ), 'IdCategory', 'CategoryName' );
				$catlist         = array_merge( $catlist, $db->loadObjectList() );
				$lists['categories']      = JHTML::_('select.genericlist', $catlist, 'rs_cat_filter', 'class="inputbox" size="1" onchange="submitbutton(\'listevents\');"','IdCategory', 'CategoryName', $category_filter  );
				
				$sql = 'SELECT LocationName as val, LocationName  FROM #__rsevents_locations';
				$db->setQuery($sql);

				$loclist[]         = JHTML::_('select.option',  '0', JText::_( 'RSE_EVENT_SELECT_LOCATION' ), 'val', 'LocationName' );
				$loclist         = array_merge( $loclist, $db->loadObjectList() );
				$lists['locations']      = JHTML::_('select.genericlist', $loclist, 'rs_loc_filter', 'class="inputbox" size="1" onchange="submitbutton(\'listevents\');"','val', 'LocationName', $location_filter  );
			

				$this->assignRef('location_filter',$category_filter);
				$this->assignRef('category_filter',$category_filter);
				$this->assignRef('lists'      	, $lists);
				$this->assignRef('filter',$filter);
				$this->assignRef('sortColumn',$sortColumn);
				$this->assignRef('sortOrder',$sortOrder);
				$this->assignRef('EventList',$EventList);
				$this->assignRef('data',$listEvents);
				$this->assignRef('pagination',$pagination);

			} break;
			
			case 'editevent':
			{
				global $mainframe;
				$db=& JFactory::getDBO();
				$document = &JFactory::getDocument();
				
				//loads the event to edit
				
				$event=$this->get('Event');
				$this->assignRef('data',$event);
				
				// loads the settings
				$rseventsConfig = $mainframe->getUserState('rseventsConfig');
				$this->assignRef('rseventsConfig',$rseventsConfig);
				
				
				$cid=JRequest::getVar('cid',array(0),'request','array');
				if(is_array($cid)) $cid=intval($cid[0]);
				if($cid==0)
					JToolBarHelper::title(JText::_('RSE_EVENT_NEW','generic.png'));
				else 
					JToolBarHelper::title(JText::_('RSE_EVENT_EDIT','generic.png').' : <small>['.$event->EventName.']</small>');
				
				if($cid) JToolBarHelper::custom('previewevent','copy','copy',JText::_('RSE_PREVIEW'),false);
				JToolBarHelper::save();
				JToolBarHelper::apply();
				JToolBarHelper::cancel();
				
				
				//set published to 1 if is a new event
				if($cid == 0) $event->published = 1;
				
				//get all the files
				$files = $this->get('Files');
			
				
				$on = JURI::root().'administrator/components/com_rsevents/images/icons/tick.png';
				$off = JURI::root().'administrator/components/com_rsevents/images/icons/delete.png';
				
				if(!empty($files))
				{
					foreach($files as $i=>$file)
					{
						if(empty($file->FilePermission))
						$file->FilePermission = '111111';
						
						$files[$i]->fp0 = ($file->FilePermission[0]==0 ? '<img src="'.$off.'">' : '<img src="'.$on.'">');		
						$files[$i]->fp1 = ($file->FilePermission[1]==0 ? '<img src="'.$off.'">' : '<img src="'.$on.'">');		
						$files[$i]->fp2 = ($file->FilePermission[2]==0 ? '<img src="'.$off.'">' : '<img src="'.$on.'">');			
						$files[$i]->fp3 = ($file->FilePermission[3]==0 ? '<img src="'.$off.'">' : '<img src="'.$on.'">');			
						$files[$i]->fp4 = ($file->FilePermission[4]==0 ? '<img src="'.$off.'">' : '<img src="'.$on.'">');			
						$files[$i]->fp5 = ($file->FilePermission[5]==0 ? '<img src="'.$off.'">' : '<img src="'.$on.'">');						
					}
				}
				$this->assignRef('files',$files);
				
				$extras = $this->get('Extras');
				$this->assignRef('extras',$extras);
				
				$db->setQuery("SELECT IdCategory FROM #__rsevents_events_cat WHERE IdEvent = ".$event->IdEvent);
				$categ_selected = $db->loadResultArray();
				
				$sql = 'SELECT IdCategory,CategoryName FROM #__rsevents_categories WHERE published = 1 ORDER BY CategoryName ASC';
				$db->setQuery($sql);

				$catlist[]         = JHTML::_('select.option',  '0', JText::_( 'RSE_EVENT_SELECT_CATEGORY' ), 'IdCategory', 'CategoryName' );
				$catlist         = array_merge( $catlist, $db->loadObjectList() );
				$lists['categories']      = JHTML::_('select.genericlist', $catlist, 'IdCategory[]', 'class="inputbox" size="5" multiple="multiple"','IdCategory', 'CategoryName', empty($categ_selected) ? 0 : $categ_selected );
				
				$sql = 'SELECT IdLocation,LocationName FROM #__rsevents_locations WHERE published = 1 ORDER BY LocationName ASC';
				$db->setQuery($sql);
				$locationList = $db->loadObjectList();
				
				$loclist[]         = JHTML::_('select.option',  '0', JText::_( 'RSE_EVENT_SELECT_LOCATION' ), 'IdLocation', 'LocationName' );
				$loclist         = array_merge( $loclist, $locationList );
				$lists1['locations']      = JHTML::_('select.genericlist', $loclist, 'IdLocation', 'class="inputbox" size="1"','IdLocation', 'LocationName', intval( $event->IdLocation ) );
			
				$this->assignRef('lists1',$lists1);
				$this->assignRef('locationList',$locationList);
				
				
				$sql = 'SELECT IdDesign,DesignName FROM #__rsevents_designs WHERE published = 1 ';
				$db->setQuery($sql);
				
				$catlist2[]         = JHTML::_('select.option',  '0', JText::_( 'RSE_EVENT_SELECT_DESIGN' ), 'IdDesign', 'DesignName' );
				$catlist2         = array_merge( $catlist2, $db->loadObjectList() );
				$lists2['designs']      = JHTML::_('select.genericlist', $catlist2, 'IdDesign', 'class="inputbox" size="1"','IdDesign', 'DesignName', intval( $event->IdDesign ) );
			
				$this->assignRef('lists2',$lists2);
				
				$sql= 'SELECT id , username FROM #__users';
				$db->setQuery($sql);
				
				$catlist3         = $db->loadObjectList(); 
				$lists3['username']      = JHTML::_('select.genericlist', $catlist3, 'IdUser', 'class="inputbox" size="5"','id', 'username', $event->IdUser );
				
				$this->assignRef('lists3',$lists3);	
				
				//do we have payment on this event?
				$db->setQuery("SELECT SUM(TicketPrice) as Price, COUNT(*) as Cnt FROM `#__rsevents_tickets` WHERE `IdEvent` = '".$cid."'");
				$ticketProperties = $db->loadObject();
			
				$pieces = array(16=>16,17=>17,18=>18,19=>19,20=>20,26=>26);
				if(!$ticketProperties->Price) 
				{
					unset($pieces[20]);
					unset($pieces[26]);
					if($ticketProperties->Cnt == 1) unset($pieces[19]);
				}
						
				$db->setQuery("SELECT DISTINCT f.FormId ,f.FormName FROM #__rsform_forms f WHERE f.Published = 1 ORDER BY FormId DESC");
				$forms = $db->loadObjectList();
				
				$flist[]         = JHTML::_('select.option',  '0', JText::_( 'RSE_DEFAULT_FORM' ), 'FormId', 'FormName' );
				if(!empty($forms))
					foreach($forms as $i=>$form)
					{
						//Check what items does it have
						$db->setQuery("SELECT COUNT(*) Cnt FROM #__rsform_components WHERE FormId = '".$form->FormId."' AND Published=1 AND ComponentTypeId IN ('".implode("','",$pieces)."')");

						if(count($pieces)!=$db->loadResult()) 
						{
							$forms[$i]->disable = true;
							$forms[$i]->FormName .= ' ('.JText::_('RSE_ADD_RSEVENTS_COMPONENTS').')';
						}
						
					}
				if(!empty($forms))
				$flist = array_merge($flist , $forms);
				
				$lists['forms']      = JHTML::_('select.genericlist', $flist, 'EventRegistrationForm', 'class="inputbox" size="1"','FormId', 'FormName', $event->EventRegistrationForm);
				
				$this->assignRef('lists',$lists);
				
			} break;
			
			case 'edityoutube':
			case 'editflash':
			case 'editmp3':
			case 'editmeebo':
			case 'editflickr':
			{
				$data = $this->get('Extra');
				$this->assignRef('data',$data);
			
			} break;
			
			case 'rsform':
				
				$db = &JFactory::getDBO();
				$formid = JRequest::getVar('formid',0,'request');
				
				echo $formid;
			exit();
			break;
		}
		
		
		parent::display($tpl);
	}
}