<?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 rseventsViewTickets extends JView
{
	function display($tpl = null)
	{
		
		global $mainframe;
		
		$db = JFactory::getDBO();
		$layout=JRequest::getVar('layout','','request');
		$task=JRequest::getVar('task','','request');
		
		$document = & JFactory::getDocument();
		$document->addStyleSheet(JURI::root().'components/com_rsevents/style.css');
		$document->addScript("includes/js/joomla.javascript.js");
		
		$Itemid = intval(JRequest::getVar('Itemid','','request'));
		$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		
		$this->assignRef('itemId',$itemId);
		$this->assignRef('Itemid',$Itemid);
		
		$user = &JFactory::getUser();
		$userid = $this->get('UserId');
		
		switch($task)
		{
			case 'checkvalue':
			{
				$cid = intval(JRequest::getVar('cid','','request'));
				$db->setQuery("SELECT TicketPrice , TicketsPerUser , TicketMaxAudience FROM #__rsevents_tickets WHERE IdTicket = ".$cid);
				$tickets = $db->loadObject();
				$db->setQuery("SELECT SUM(st.TicketsSubscribed) FROM #__rsevents_subscription_tickets st LEFT JOIN #__rsevents_subscriptions s ON s.IdSubscription=st.IdSubscription WHERE st.IdTicket='".$cid."' AND s.SubscriptionState IN (0,1) ");
				$ticketsSubscribed = $db->loadResult();
				$avTickets = $tickets->TicketMaxAudience - $ticketsSubscribed;
				
				$numTickets = ($ticketsSubscribed < $tickets->TicketMaxAudience && $avTickets > $tickets->TicketsPerUser) ? $tickets->TicketsPerUser : $avTickets;
				
				$numTickets = ($numTickets == 0 || $numTickets == '') ? 1 : $numTickets;
				
				echo $tickets->TicketPrice."\n".$numTickets;
				exit();
			} break;
		}
		
		switch($layout)
		{
			
			default:
			{
				
				$evName = $this->get('EventName');
				$evId = $this->get('EventId');
				$cid = intval(JRequest::getVar('cid',0,'request'));
			
			if($user->usertype == 'Super Administrator' || $user->id == $userid)
			{	
				
				$sortColumn		=JRequest::getVar('filter_order','#__rsevents_tickets.ordering');
				$sortOrder	=JRequest::getVar('filter_order_Dir','asc');
				$pagination=$this->get('pagination');
				$tickets = $this->get('data');
				$this->assignRef('id',$evId);
				$this->assignRef('evName',$evName);


				$pathway 	= & $mainframe->getPathWay();
				$pathway->addItem(JText::_('RSE_BC_SHOWTICKETS'), '');
				
				$this->assignRef('data',$tickets);
				$this->assignRef('sortColumn',$sortColumn);
				$this->assignRef('sortOrder',$sortOrder);
				$this->assignRef('pagination',$pagination);
				
			} else $mainframe->redirect(html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=show&cid='.$cid.$itemId)),JText::_('RSE_ACCESS_DENIED'));
			
			} break;
			
			case 'edit':
			{
				if($user->usertype == 'Super Administrator' || $user->id == $userid)
				{
			
					$cid = intval(JRequest::getVar('cid',0,'request'));
					$idEvent= intval(JRequest::getVar('IdEvent',0,'request'));
					$ticket = $this->get('Ticket');
					if(empty($ticket->IdEvent))
					$ticket->IdEvent = $idEvent;
					
					$pathway 	= & $mainframe->getPathWay();
					if($cid == 0)
					$pathway->addItem(JText::_('RSE_BC_EDITTICKET'), '');
					else
					$pathway->addItem(JText::_('RSE_BC_NEWTICKET'), '');
					
					$this->assignRef('data',$ticket);
			
				} else $mainframe->redirect(html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=show&cid='.$idEvent.$itemId)),JText::_('RSE_ACCESS_DENIED'));
			}
		}
		
		
		parent::display($tpl);
	}
}