<?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 (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		$model = $this->getModel('tickets');
		$post=JRequest::get('post');
		$idEvent = JRequest::getVar('IdEvent',array(0),'request','array');
		if(is_array($idEvent)) $idEvent = $idEvent[0];
		 
		$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);
		switch(JRequest::getCmd('task'))
		{
			case 'apply' :
					$link = 'index.php?option=com_rsevents&task=editticket&cid='.$row->IdTicket;

			break;
			
			case 'save' :
					$link = 'index.php?option=com_rsevents&task=listtickets&IdEvent='.$idEvent;

			break;
		}
		
		$this->setRedirect($link, $msg);
	}
	
	function cancel()
	{
		$idEvent = JRequest::getVar('IdEvent',0,'request');
		$msg = JText::_('RSE_CANCEL_OPERATION' );
		$this->setRedirect( 'index.php?option=com_rsevents&task=listtickets&IdEvent='.$idEvent, $msg );
	}

	/**
	 * remove record(s)
	 * @return void
	 */
	 
	function remove()
	{
		//initialize the table 
		$db = & JFactory::getDBO();
		$model = $this->getModel('tickets');
		$row =& JTable::getInstance('Rsevents_Tickets','Table');
		$cids=JRequest::getVar('cid',array(0),'post','array');
		$idEvent = JRequest::getVar('IdEvent',0,'request');
		
		//deletes all the tickets that have been selected
		
		if(!empty($cids))
		{
		foreach($cids as $cid) 
		{
			$db->setQuery("SELECT COUNT(st.IdTicket) FROM #__rsevents_subscription_tickets st LEFT JOIN #__rsevents_subscriptions s ON s.IdSubscription=st.IdSubscription WHERE st.IdTicket ='".$cid."'");
			if ($db->loadResult() == 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
		{
			$msg = JText::_('RSE_TICKETS_DELETE_ERROR1' );
		}
		$this->setRedirect( 'index.php?option=com_rsevents&task=listtickets&IdEvent='.$idEvent, $msg );
	}
	 
	 
	function deleteTicket()
	{
		$model = $this->getModel('tickets');
		$db = &JFactory::getDBO();
		$row =& JTable::getInstance('Rsevents_Tickets','Table');
		
		$cid=JRequest::getVar('cid');
		$idEvent = JRequest::getVar('IdEvent');
		
		$row->load($cid);
		
		$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."'");	
		
		if(!empty($cid))
		{
			if ($db->loadResult() == 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
		{
			$msg = JText::_('RSE_TICKETS_DELETE_ERROR1' );
		}
		
		$this->setRedirect( 'index.php?option=com_rsevents&task=listtickets&IdEvent='.$idEvent, $msg );
	}
	
	function saveOrder()
	{
		global $mainframe;

		
		$model = $this->getModel('tickets');

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Initialize variables
		$db			= & JFactory::getDBO();

		$cid		= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$idEvent =  JRequest::getVar('IdEvent',array(0),'request', 'array');
		if(is_array($idEvent)) $idEvent = $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('IdEvent='.$idEvent);

		$msg = JText::_('RSE_TICKETS_ORDER_SAVE');
		
		$this->setRedirect( 'index.php?option=com_rsevents&task=listtickets&IdEvent='.$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 = $idEvent[0];
		$task = JRequest::getVar('task');
		if($task=='orderup') $direction=-1;
		else $direction=1;
		if(is_array($cid)) $cid=$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( 'index.php?option=com_rsevents&task=listtickets&IdEvent='.$idEvent);
		

	}
	
	function back()
	{
	$idEvent=JRequest::getVar('IdEvent',0);
	$this->setRedirect('index.php?option=com_rsevents&task=editevent&cid='.$idEvent);
	}
	
}