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

class rseventsModelSubscriptions extends JModel
{
	var $_query;
	var $_data;
	var $_total=null;
	var $_pagination=null;
	var $_componentList=null;
	function _buildQuery()
	{
	
		 global $mainframe;
		$ticket_filter=intval(JRequest::getVar('ticket_filter'));
		$event_filter=JRequest::getVar('rs_evn_filter',$mainframe->getUserState('com_rsevents.subscriptions.rs_evn_filter'));
		$state_filter= JRequest::getInt('state_filter');
		$sortOrder=JRequest::getVar('filter_order_Dir','desc');
		$sortColumn=JRequest::getVar('filter_order','s.SubscriptionDate');
		
		$idEvent = intval(JRequest::getVar('IdEvent',0,'request'));
		
		$mainframe->setUserState('com_rsevents.subscriptions.rs_evn_filter',$event_filter);
		$mainframe->setUserState('com_rsevents.subscriptions.sortColumn',$sortColumn);
		$mainframe->setUserState('com_rsevents.subscriptions.sortOrder',$sortOrder);
		
		if($state_filter !== '')
		$state_filter="AND s.SubscriptionState='$state_filter'";
		else
		$state_filter="";
		
		if($ticket_filter)
		$ticket_filter="AND st.IdTicket='$ticket_filter'";
		else
		$ticket_filter="";
		
		$filter=JRequest::getVar('rs_filter');
		
		$this->_query="SELECT s.IdSubscription as RecordId, s.IdEvent , s.FirstName , s.IdUser, s.LastName , s.Email , s.SubscriptionState , s.SubscriptionDate , e.IdEvent , e.EventName , u.username , t.TicketName FROM #__rsevents_subscriptions s "
		."LEFT JOIN #__rsevents_events e ON e.IdEvent = s.IdEvent "
		."LEFT JOIN #__users u ON s.IdUser = u.id "
		."LEFT JOIN #__rsevents_subscription_tickets st ON st.IdSubscription = s.IdSubscription "
		."LEFT JOIN #__rsevents_tickets t ON st.IdTicket=t.IdTicket "
		."WHERE 1=1 AND e.IdEvent='".$idEvent."' $ticket_filter $state_filter"
		."ORDER BY $sortColumn $sortOrder ";
		//echo $this->_query;
	}
	function __construct()
	{	
		parent::__construct();
		$this->_buildQuery();
		global $mainframe, $option;

		// Get pagination request variables
		$limit		= JRequest::getVar('limit', $mainframe->getCfg('list_limit'), '', 'int');
		$limitstart	= JRequest::getVar('limitstart', 0, '', 'int');
		
		// In case limit has been changed, adjust it
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

		$this->setState($option.'.fsubscriptions.limit', $limit);
		$this->setState($option.'.fsubscriptions.limitstart', $limitstart);
		
		//echo $this->_db->_errorMsg;
	}
	
	function getData()
	{
		global $option;
		if (empty($this->_data))
		{
			$this->_data=$this->_getList($this->_query,$this->getState($option.'.fsubscriptions.limitstart'), $this->getState($option.'.fsubscriptions.limit'));
		}
		return $this->_data;
	}
	
	function getTotal()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$this->_total = $this->_getListCount($this->_query);	
		}
		return $this->_total;
	}
	
	function getPagination()
	{
		global $option;
		// Load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotal(), $this->getState($option.'.fsubscriptions.limitstart'), $this->getState($option.'.fsubscriptions.limit'));
		}
		return $this->_pagination;
	}
	
	function getSubscription()
	{
		$db = & JFactory::getDBO();
		$row = & JTable::getInstance('Rsevents_Subscriptions','Table');
		$cid = intval(JRequest::getVar('cid',0,'request'));
		$row->load($cid);
		$db->setQuery("SELECT st.IdTicket ,st.TicketsSubscribed , t.TicketName FROM #__rsevents_subscription_tickets st LEFT JOIN #__rsevents_tickets t ON t.IdTicket=st.IdTicket WHERE IdSubscription=".$cid);
		$details = $db->loadObject();
		$row->IdTicket = $details->IdTicket;
		$row->TicketsSubscribed = $details->TicketsSubscribed;
		$row->TicketName = $details->TicketName;
		return $row;
	}
	
	function getSubscriptionUser()
	{
		$db = &JFactory::getDBO();
		$idEvent = intval(JRequest::getVar('IdEvent',0,'request'));

		$db->setQuery("SELECT IdUser FROM #__rsevents_events WHERE IdEvent='".$idEvent."'");
		$row = $db->loadResult();
		return $row;
	}
	
	function getUserSubscriptions()
	{
		$db		=& JFactory::getDBO();
		$user	=& JFactory::getUser();
		
		if($user->id != 0)
		{
			$db->setQuery("SELECT e.IdEvent , e.EventName , s.SubscriptionState , s.SubscriptionDate FROM #__rsevents_subscriptions s LEFT JOIN #__rsevents_events e ON e.IdEvent=s.IdEvent WHERE s.IdUser='".$user->id."'");
			$row = $db->loadObjectList();
		} else $row = '';
		
		return $row;
	}
}