<?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 rseventsViewSubscriptions extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
		$layout=JRequest::getVar('layout','','request');
		$db = & JFactory::getDBO();
		$user = &JFactory::getUser();
		$userId = $this->get('SubscriptionUser');
		
				
		//adds the css style
		$document = & JFactory::getDocument();
		$document->addStyleSheet(JURI::root().'components/com_rsevents/style.css');
		
		//get the itemId
		$Itemid = intval(JRequest::getVar('Itemid','','request'));
		$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		$this->assignRef('itemId',$itemId);
		$this->assignRef('Itemid',$Itemid);
		
		switch($layout)
		{
			
			default:
			{
				//get variables
				global $mainframe;
				
				$idEvent=JRequest::getVar('IdEvent',0,'request');
				$idEvent = intval($idEvent);
				$ticket_filter = intval(JRequest::getVar('ticket_filter'));
				$state_filter = intval(JRequest::getVar('state_filter'));
				
				$db->setQuery("SELECT EventName FROM #__rsevents_events WHERE IdEvent= '".$idEvent."'");
				$eventName = $db->loadResult();
				
				$pathway 	= & $mainframe->getPathWay();
				$pathway->addItem(JText::_('RSE_BC_SUBSCRIPTION'). ' "'.$eventName.'"', '');
				
				if($user->usertype == 'Super Administrator' || $user->id == $userId)
				{
				$sql = "SELECT IdTicket as val, TicketName  FROM #__rsevents_tickets WHERE IdEvent='".$idEvent."'";
				$db->setQuery($sql);

				$catlist1[]         = JHTML::_('select.option',  '0', JText::_( 'RSE_SUBSCRIBER_SELECT_TICKET' ), 'val', 'TicketName' );
				$catlist1         = array_merge( $catlist1, $db->loadObjectList() );
				$lists1['tickets']      = JHTML::_('select.genericlist', $catlist1, 'ticket_filter', 'class="inputbox" size="1" onchange="submitbutton(\'listsubscriptions\');"','val', 'TicketName', $ticket_filter  );
				
				$this->assignRef('lists1',$lists1);
				
				$state[] = JHTML::_('select.option',  '', JText::_( 'RSE_SUBSCRIBER_SELECT_STATE' ), 'value', 'text' );
				$state[] = JHTML::_('select.option',  '-1', JText::_( 'RSE_SUBSCRIBER_DENIED' ), 'value', 'text' );
				$state[] = JHTML::_('select.option',  '0', JText::_( 'RSE_SUBSCRIBER_PENDING' ), 'value', 'text' );
				$state[] = JHTML::_('select.option',  '1', JText::_( 'RSE_SUBSCRIBER_ACCEPTED' ), 'value', 'text' );
				$lists['state']      = JHTML::_('select.genericlist', $state, 'state_filter', 'class="inputbox" size="1" onchange="submitbutton(\'listsubscriptions\');"','value', 'text' , $state_filter  );
				
				
				
				$filter			=JRequest::getVar('rs_filter');
				$sortColumn		=JRequest::getVar('filter_order','s.SubscriptionDate');
				$sortOrder	=JRequest::getVar('filter_order_Dir','desc');
				$listSubscribers=$this->get('data');
				$pagination=$this->get('pagination');
				
				
				$this->assignRef('eventName',$eventName);
				$this->assignRef('filter',$filter);
				$this->assignRef('idEvent',$idEvent);
				$this->assignRef('lists', $lists);
				$this->assignRef('sortColumn',$sortColumn);
				$this->assignRef('sortOrder',$sortOrder);
				$this->assignRef('data',$listSubscribers);
				$this->assignRef('pagination',$pagination);
				
			} else $mainframe->redirect('index.php?option=com_rsevents&view=events&layout=show&cid='.$idEvent.$itemId,JText::_('RSE_ACCESS_DENIED'));
			
			} break;
			
			case 'edit':
			{
				global $mainframe;
				$db=& JFactory::getDBO();
				$cid=JRequest::getVar('cid',array(0),'request','array');
				if(is_array($cid)) $cid=intval($cid[0]);
				$idEvent=JRequest::getVar('IdEvent',0,'request');
				$idEvent = intval($idEvent);
				
				
				if($user->usertype == 'Super Administrator' || $user->id == $userId)
				{
					//$tickets=$this->get('Tickets');
					$subscription=$this->get('Subscription');
					$this->assignRef('data',$subscription);
					
					
					$pathway 	= & $mainframe->getPathWay();
					$pathway->addItem(JText::_('RSE_BC_EDIT_SUBSCRIPTION'), '');
					
					$sql= 'SELECT id , username FROM #__users';
					$db->setQuery($sql);
					
					$catlist         = $db->loadObjectList(); 
					$lists1['username']      = JHTML::_('select.genericlist', $catlist, 'IdUser', 'class="inputbox" size="5"','id', 'username', $subscription->IdUser );
					
					$this->assignRef('lists1',$lists1);
					
					$catlist1[]        = JHTML::_('select.option',  '1', JText::_( 'RSE_SUBSCRIBER_ACCEPTED' ), 'value', 'text' );
					$catlist1[]        = JHTML::_('select.option',  '0', JText::_( 'RSE_SUBSCRIBER_PENDING' ), 'value', 'text' );
					$catlist1[]        = JHTML::_('select.option',  '-1', JText::_( 'RSE_SUBSCRIBER_DENIED' ), 'value', 'text' );
					$lists2['changeStatus']      = JHTML::_('select.genericlist', $catlist1, 'SubscriptionState', 'class="inputbox" size="1"','value', 'text', intval( $subscription->SubscriptionState )  );
								
					$this->assignRef('lists2',$lists2);
					$this->assignRef('idEvent',$idEvent);
				
				} else $mainframe->redirect('index.php?option=com_rsevents&view=events&layout=show&cid='.$idEvent.$itemId,JText::_('RSE_ACCESS_DENIED'));
				
			} break;
			
			case 'mysubscriptions':
				
				global $mainframe;
				
				$this->assignRef('rseventsConfig',$mainframe->getUserState('rseventsConfig'));
				
				$subscriptionsDetails = $this->get('UserSubscriptions');
				$this->assignRef('subscriptionsDetails',$subscriptionsDetails);
			break;
		}
		
		
		parent::display($tpl);
	}
}