<?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');

if(file_exists(JPATH_SITE.DS.'components'.DS.'com_rsform'.DS.'rsform.php'))
{
require_once(JPATH_SITE.DS.'components'.DS.'com_rsform'.DS.'submissions.class.php');
require_once(JPATH_SITE.DS.'components'.DS.'com_rsform'.DS.'controller'.DS.'functions.php');
require_once(JPATH_SITE.DS.'components'.DS.'com_rsform'.DS.'controller'.DS.'adapter.php');
global $RSadapter;
$RSadapter = new RSadapter();
}

class rseventsViewSubscriptions extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
		$task=JRequest::getVar('task','','request');
		$db = & JFactory::getDBO();
		switch($task)
		{
			
			case 'listsubscriptions':
			{
				JToolBarHelper::title(JText::_('RSE_LIST_SUBSCRIPTION'),'generic.png');
				JToolBarHelper::addNewX('editsubscription');
				JToolBarHelper::editListX('editsubscription');
				JToolBarHelper::deleteList();
				JToolBarHelper::custom('accepted','apply.png','apply_f2.png',JText::_('RSE_SUBSCRIBER_ACCEPTED'));
				JToolBarHelper::custom('pending','default.png','default_f2.png',JText::_('RSE_SUBSCRIBER_PENDING'));
				JToolBarHelper::custom('denied','cancel.png','cancel_f2.png',JText::_('RSE_SUBSCRIBER_DENIED'));
				JToolBarHelper::custom('main','preview.png','preview_f2.png',JText::_('RSE_RS_PRODUCT'),false);
				
				
				$event_filter = $mainframe->getUserState('com_rsevents.subscriptions.rs_evn_filter');
				$ticket_filter = JRequest::getVar('ticket_filter');
				$state_filter = JRequest::getVar('state_filter');
				$sql = 'SELECT IdEvent as val, EventName  FROM #__rsevents_events';
				$db->setQuery($sql);

				$catlist[]         = JHTML::_('select.option',  '0', JText::_( 'RSE_SUBSCRIBER_SELECT_EVENT' ), 'val', 'EventName' );
				$catlist         = array_merge( $catlist, $db->loadObjectList() );
				$lists['events']      = JHTML::_('select.genericlist', $catlist, 'rs_evn_filter', 'class="inputbox" size="1" onchange="submitbutton(\'listsubscriptions\');"','val', 'EventName', $event_filter  );
				
				$sql = "SELECT IdTicket as val, TicketName  FROM #__rsevents_tickets WHERE IdEvent='".$event_filter."'";
				$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  );
				
				//$this->assignRef('lists2',$lists2);
				
				$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');
				
				//print_r($listSubscribers);die();
				
				$this->assignRef('filter',$filter);
				$this->assignRef('event_filter',$event_filter);
				$this->assignRef('lists', $lists);
				$this->assignRef('sortColumn',$sortColumn);
				$this->assignRef('sortOrder',$sortOrder);
				$this->assignRef('data',$listSubscribers);
				$this->assignRef('pagination',$pagination);
			} break;
			
			case 'editsubscription':
			{
				
				$db=& JFactory::getDBO();
				$cid=JRequest::getVar('cid',array(0),'request','array');
				if(is_array($cid)) $cid=intval($cid[0]);
				if($cid==0)
					JToolBarHelper::title(JText::_('RSE_SUBSCRIBER_NEW','generic.png'));
				else 
					JToolBarHelper::title(JText::_('RSE_SUBSCRIBER_EDIT','generic.png'));
				JToolBarHelper::save();
				JToolBarHelper::apply();
				JToolBarHelper::cancel();
				
				//$tickets=$this->get('Tickets');
				$subscription=$this->get('Subscription');
				$this->assignRef('data',$subscription);
				
				//get config data
				$rseventsConfig = $mainframe->getUserState('rseventsConfig');
				
				$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);

			
				$sql = "SELECT IdEvent as value, EventName as text FROM #__rsevents_events WHERE EventEnableRegistration='1'";
				$db->setQuery($sql);
				$rows = $db->loadObjectList();
				$catlist2[]         = JHTML::_('select.option',  '0', JText::_( 'RSE_SUBSCRIBER_SELECT_TICKET' ), 'value', 'text' );
				foreach($rows as $objEvent)
				{	
					$catlist2[]         = JHTML::_('select.optgroup', JText::_( $objEvent->text ), 'value', 'text' );
						$db->setQuery("SELECT IdTicket as value ,TicketName ,TicketPrice FROM #__rsevents_tickets WHERE IdEvent=".$objEvent->value);
						$rows1 = $db->loadObjectList();
						foreach($rows1 as $objTicket)
						{
							$catlist2[]      = JHTML::_('select.option' , $objTicket->value,$objTicket->TicketName . ' (' . number_format($objTicket->TicketPrice,$rseventsConfig['payment.nrdecimals'], $rseventsConfig['payment.decimal'], $rseventsConfig['payment.thousands']).' '.$rseventsConfig['payment.currency'].')', 'value','text' );
						}					
				}
				$lists3['events']      = JHTML::_('select.genericlist', $catlist2, 'IdTicket', 'class="inputbox" size="1"','value', 'text', empty($subscription->IdTicket) ? '' : intval($subscription->IdTicket)  );

				$this->assignRef('lists3',$lists3);
				
			} break;
			
			case 'showtransaction':
				$db=& JFactory::getDBO();
				$cid=JRequest::getVar('cid',0,'request');			
				
				$db->setQuery("SELECT * FROM #__rsevents_subscriptions WHERE IdSubscription = '".$cid."'");
				$Subscription = $db->loadObject();
				
				
				if(defined('RSEVENTS_PAYPAL') && $Subscription->TransactionGateway == 'paypal')
				{
					$params = new JParameter( $Subscription->TransactionParams );
					$TD = plgSystemRSEventspaypal::parseParams($params);		
					
				}
				
				//get extra fields when using rsform forms
				$db->setQuery("SELECT EventRegistrationForm FROM #__rsevents_events WHERE IdEvent=".$Subscription->IdEvent);
				$formid = $db->loadResult();
				
				
				if($formid != 0)
				{
					//here we will get the extra fields
					if($Subscription->SubscriptionFormId != 0 && $formid != 0)
					{
						$where = " WHERE sv.FormId = '".$formid."' AND sv.SubmissionId = '".$Subscription->SubscriptionFormId."'";
						$db->setQuery("SELECT DISTINCT sv.FieldName , sv.FieldValue FROM #__rsform_submission_values sv LEFT JOIN #__rsform_submissions s ON s.FormId=sv.FormId ".$where);
						$res = $db->loadObjectList();
					}
					
					$fields = array();
					if(!empty($res))
					foreach ($res as $row)
						$fields[$row->FieldName] = $row->FieldValue;
						
					unset($fields['RSEventsFirstName'],$fields['RSEventsLastName'],$fields['RSEventsEmail'],$fields['RSEventsTickets'],$fields['RSEventsPayment'] ,$fields['formId']);
					
					$fields = str_replace(JPATH_SITE,substr(JURI::root(),0,-1),$fields);
					
					$captions = array();
					foreach ($fields as $fieldname => $fieldvalue)
						$captions[$fieldname] = RSfrontComponentCaption(RSresolveComponentName($fieldname,$formid));
					
				}
				
				$this->assignRef('TD',$TD);	
				$this->assignRef('fields',$fields);	
				$this->assignRef('captions',$captions);	
			break;
			
		}
		
		
		parent::display($tpl);
	}
}