<?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
*/

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class rseventsControllerTickets extends rseventsController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
		$this->registerTask( 'apply' , 'save');
		$this->registerTask( 'orderup' , 'move');
		$this->registerTask( 'orderdown' , 'move');
	}

	/**
	 * save a record
	 * @return void
	 */
	function save()
	{
		//get variables
		$model = $this->getModel('tickets');
		$post=JRequest::get('post');
		$idEvent = intval(JRequest::getVar('IdEvent','','request'));

		$Itemid = intval(JRequest::getVar('Itemid','','request'));
		$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		 
		$cid = JRequest::getVar('cid');

		$post['TicketDescription'] = JRequest::getVar('TicketDescription', '', 'post', 'none', JREQUEST_ALLOWRAW);
		$row= & JTable::getInstance('Rsevents_Tickets','Table');
		
		
		if(!$row->bind($post)) {
			return JError::raiseWarning(500, $row->getError());
		}
		if($row->TicketsPerUser == 0 || $row->TicketsPerUser == '') $row->TicketsPerUser = 1;
		if(empty($row->ordering)) $row->ordering = 9999999;
		
		
		if ($row->store()) {
			$msg = JText::_( 'RSE_TICKETS_SAVE' );
			$row->reorder();
		} else {
			$msg = JText::_( 'RSE_TICKETS_SAVE_ERROR' );
		}
		$row->reorder('IdEvent='.$idEvent);

		$link = html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=tickets&cid='.$idEvent.$itemId));
		$this->setRedirect($link, $msg);
	}
	
	//cancel 
	function cancel()
	{
		$idEvent = intval(JRequest::getVar('IdEvent',0,'request'));
		$Itemid = intval(JRequest::getVar('Itemid','','request'));
		$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		$msg = JText::_( 'RSE_CANCELED_OPERATION' );
		$this->setRedirect( html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=tickets&IdEvent='.$idEvent.$itemId)), $msg );
	}

	/**
	 * delete ticket
	 * @return void
	 */
	function deleteTicket()
	{
		global $mainframe;
		$model = $this->getModel('tickets');
		$row =& JTable::getInstance('Rsevents_Tickets','Table');
		$cid= intval(JRequest::getVar('cid'));
		$user = &JFactory::getUser();
		$db = &JFactory::getDBO();
		$idEvent = intval(JRequest::getVar('IdEvent',0,'request'));
		$Itemid = intval(JRequest::getVar('Itemid','','request'));
		$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		
		//get the user id for obtaining the permission to delete
		
		$db->setQuery("SELECT IdUser FROM #__rsevents_events WHERE IdEvent='".$idEvent."'");
		$userid = $db->loadResult();
		
		//see if the ticket has subscribers 
		$db->setQuery("SELECT COUNT(st.IdTicket) FROM #__rsevents_subscription_tickets st LEFT JOIN #__rsevents_subscriptions s ON s.IdSubscription=st.IdSubscription WHERE st.IdTicket ='".$row->IdTicket."'");
		$result = $db->loadResult();
		
		$row->load($cid);
		$idEvent = $row->IdEvent;		
		if(!empty($cid))
		{
			if($user->usertype == 'Super Administrator' || $user->id == $userid)
			{
				if ($result == 0)
				if(!$row->delete($cid))
				{
					$msg = JText::_( 'RSE_TICKETS_DELETE_ERROR' );
				} else 
				{
					$msg = JText::_( 'RSE_TICKETS_DELETE1' );
				} else $msg = JText::_('RSE_TICKET_CONTAINS_SUBSCRIBERS');
			} else $mainframe->redirect(html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=show&cid='.$row->IdEvent.$itemId)),JText::_('RSE_ACCESS_DENIED'));
		} else
		{
			$msg = JText::_( 'RSE_TICKETS_DELETE_ERROR1' );
		}
		
		$this->setRedirect( html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=tickets&cid='.$idEvent.$itemId)), $msg );
	}

	// ordering
	function saveOrder()
	{
		global $mainframe;
		
		$model = $this->getModel('tickets');
		$db			= & JFactory::getDBO();
		$cid		= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$idEvent =  JRequest::getVar('IdEvent',0,'request');
		if(is_array($idEvent)) $idEvent = intval($idEvent[0]);
		
		$order		= JRequest::getVar( 'order', array (0), 'post', 'array' );
		$total		= count($cid);
		$conditions	= array ();

		JArrayHelper::toInteger($cid, array(0));
		JArrayHelper::toInteger($order, array(0));

		// Instantiate an article table object
		$row = & JTable::getInstance('Rsevents_Tickets','Table');

		
		for( $i=0; $i < count($cid); $i++ )
		{
			$row->load( (int) $cid[$i] );

			if ($row->ordering != $order[$i])
			{	
				$row->ordering = $order[$i];
				if (!$row->store()) 
				{
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}
		
		$row->reorder('cid='.$idEvent);

		$msg = JText::_('RSE_TICKETS_ORDER_SAVE');
		
		$this->setRedirect( html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=tickets&cid='.$idEvent)), $msg );

	}
	
	function move() 
	{
		$model = $this->getModel('tickets');
		$row = & JTable::getInstance('Rsevents_Tickets','Table');
		$cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$idEvent =  JRequest::getVar('IdEvent',array(0),'request', 'array');
		if(is_array($idEvent)) $idEvent = intval($idEvent[0]);
		
		$task = JRequest::getVar('task');
		if($task=='orderup') $direction=-1;
		else $direction=1;
		if(is_array($cid)) $cid= intval($cid[0]);
		
		$row->load($cid);
		
		if (!$row->load($cid)) 
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}	

		$row->move($direction,'IdEvent='.$idEvent);
		$row->reorder('IdEvent='.$idEvent);
	
		$this->setRedirect( html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=tickets&IdEvent='.$idEvent)));
		

	}
	//go back to the event
	function back()
	{
	$idEvent= intval(JRequest::getVar('IdEvent',0));
	$Itemid = intval(JRequest::getVar('Itemid','','request'));
	$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
	$this->setRedirect(html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=addeditevenet&cid='.$idEvent.$itemId)));
	}
	
}