<?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.controller');

class rseventsController extends JController
{
	var $_db;
	var $_params;
	
	function __construct()
	{
		parent::__construct();
		global $mainframe;
		$this->_db =& JFactory::getDBO();
		$this->_params =& JComponentHelper::getParams('com_rsevents');	
		
		$this->_db->setQuery("SELECT * FROM `#__rsevents_config`");
		$rseventsConfigDb = $this->_db->loadObjectList();
		
		foreach ($rseventsConfigDb as $objConfig)
			$rseventsConfig[$objConfig->ConfigName] = $objConfig->ConfigValue;
			
		$mainframe->setUserState('rseventsConfig',$rseventsConfig);
		
		//archive old events
		if ($rseventsConfig['event.enable.autoarchive'] == 1)
		{
			$time_offset = $rseventsConfig['event.archive.days'] * 86400;
			$this->_db->setQuery("SELECT IdEvent FROM #__rsevents_events WHERE ".time()." > EventEndDate + ".$time_offset." AND EventArchive = '0' ");
		
			$eventIds = $this->_db->loadResultArray();
			$ids = implode(',',$eventIds);
			
			$this->_db->setQuery("UPDATE #__rsevents_events SET published = '-1' WHERE IdEvent IN (".$ids.") ");
			$this->_db->query();

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

		}
		
	}
	
	/* Events */
	
	function listevents()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','default');
		
		parent::display();
	}
	
	function showevent()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','show');
		
		parent::display();
	}
	
	function editevent()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','addeditevent');
		
		parent::display();
	}
	
	function duplicateevent()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','addeditevent');
		parent::display();
	}
	
	function listmyevents()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','listmyevents');
		parent::display();
	}
	
	function printevent()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','printevent');
		parent::display();
	}
	
	function eventsubscribe()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','subscribe');
		parent::display();
	}
	
	function listdaysevents()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','listdaysevent');
		parent::display();
	}
	
	
	/* Extras */
	
	function selectdesign()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','selectdesign');
		parent::display();
	}
	
	function invite()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','invite');
		parent::display();
		exit();
	}
	
	function importyahoo()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','importyahoo');
		parent::display();
	}
	
	function importgmail()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','importgmail');
		parent::display();
	}
	
	function guestssendmessage()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','guestssendmessage');
		parent::display();
		exit();
	}
	
	function edityoutube()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','youtube');
		parent::display();
	}
	
	function editflickr()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','flickr');
		parent::display();
	}
	
	function editflash()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','flash');
		parent::display();
	}
	
	function editmeebo()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','chat');
		parent::display();
	}
	
	function editmp3()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','mp3');
		parent::display();
	}
	
	function addphotos()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','photos');
		parent::display();
		exit();
	}
	
	function suggestguest()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','suggestguest');
		parent::display();
	}
	
	function editfile()
	{
		JRequest::setVar('view','files');
		JRequest::setVar('layout','default');
		parent::display();
	}
	
	function viewalbum()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','album');
		parent::display();
		exit();
	}
	
	/* Locations */
	
	function refreshlocations()
	{
		JRequest::setVar('view','locations');
		parent::display();
		exit();
	}
	
	function listlocations()
	{
		JRequest::setVar('view','locations');
		JRequest::setVar('layout','default');
		parent::display();
	}
	
	function listlocationevents()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','listlocationevents');
		parent::display();
	}
	
	function addeditlocation()
	{
		JRequest::setVar('view','locations');
		JRequest::setVar('layout','addeditlocation');
		parent::display();
	}
	
	function showlocation()
	{
		JRequest::setVar('view','locations');
		JRequest::setVar('layout','show');
		parent::display();
	}
	
	/* Categories */
	
	function listcategories()
	{
		JRequest::setVar('view','categories');
		JRequest::setVar('layout','default');
		parent::display();
	}
	
	function listcategoryevents()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','listcategoryevents');
		parent::display();
	}
	
	/* Tickets */
	
	function listtickets()
	{
		JRequest::setVar('view','tickets');
		JRequest::setVar('layout','default');
		parent::display();
	}
	
	function editticket()
	{
		JRequest::setVar('view','tickets');
		JRequest::setVar('layout','edit');
		parent::display();
	}
	
	/* Subscriptions */
	
	function listsubscriptions()
	{
		JRequest::setVar('view','subscriptions');
		JRequest::setVar('layout','default');
		parent::display();
	}

	function editsubscription()
	{
		JRequest::setVar('view','subscriptions');
		JRequest::setVar('layout','edit');
		parent::display();
	}

	/* Payment */

	function wiredetails()
	{
		JRequest::setVar('view','events');
		JRequest::setVar('layout','wire');
		parent::display();
	}

	function checkvalue()
	{
		JRequest::setVar('view','tickets');
		parent::display();
		exit();
	}
	
	function twoco()
	{
		$get = JRequest::get('get');
		$plugin =& JPluginHelper::getPlugin( 'system', 'rsevents2co' ); 	//Get a reference to this plugin
		$params = new JParameter( $plugin->params );
		
		$url = $params->get('routine') ? 'https://www.2checkout.com/checkout/purchase' : 'https://www.2checkout.com/checkout/spurchase';
		
		$html = '';		
		$html .= '<p>'.JText::_('RSE_REDIRECT_2CO').'</p>';
		$html .= '<form method="post" action="'.$url.'" id="twocoForm">';
		if (!$params->get('mode'))
			$html .= '<input type="hidden" name="demo" value="Y" />';
		$html .= '<input type="hidden" name="fixed" value="Y" />';
		$html .= '<input type="hidden" name="sid" value="'.htmlentities($params->get('id')).'" />';
		$html .= '<input type="hidden" name="cart_order_id" value="'.$get['coi'].'" />';
		$html .= '<input type="hidden" name="total" value="'.$get['price'].'" />';
		$html .= '<input type="hidden" name="x_receipt_link_url" value="'.JRoute::_(JURI::root().'index.php?rse2co=1').'" />';
		$html .= '<input type="hidden" name="custom" value="'.$get['custom'].'" />';
		$html .= '<input type="hidden" name="return_url" value="'.JRoute::_(JURI::root().'index.php?option=com_rsevents&view=events&layout=show&cid='.$get['idevent']).'" />';
		$html .= '</form>';
		
		$html .= '<script type="text/javascript">';
		$html .= 'function twocoFormSubmit() { document.getElementById(\'twocoForm\').submit() }';
		$html .= 'try { window.addEventListener ? window.addEventListener("load",twocoFormSubmit,false) : window.attachEvent("onload",twocoFormSubmit); }';
		$html .= 'catch (err) { twocoFormSubmit(); }';
		$html .= '</script>';
		
		
		echo $html;		
	}
	
	function location_filter()
	{
		$db = &JFactory::getDBO();
		$string = JRequest::getVar('string');
		$return = '';
		
		
		$db->setQuery("SELECT IdLocation,LocationName FROM #__rsevents_locations WHERE LocationName LIKE '%".$db->getEscaped($string)."%' ");
		$locations = $db->loadObjectList();
		$return .= '<select id="IdLocation" class="inputbox" size="1" name="IdLocation">'; 
		$return .= '<option value="0">'.JText::_('RSE_EVENT_SELECT_LOCATION').'</option>';
		foreach($locations as $location)
			$return .= '<option value="'.$location->IdLocation.'">'.$location->LocationName.'</option>'; 
		$return .= '</select>';
		
		echo $return;
		exit();
	}
	
}
?>