<?php
/**
 * @version		3.0.0
 * @package		JomLand TicketCenter
 * @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view' );

class JLTicketCenterViewEvents extends JViewLegacy
{
	function display($tpl = null)
	{
		$mainframe = JFactory::getApplication();

		$task =  JRequest::getVar('task');

		if ($task=='add' ||$task=='edit' ){
			$this->edit($tpl);
			return;
		}

		JToolBarHelper::title(JText::_('Events - JomLand TicketCenter' ), 'generic.png' );

		//JLTicketCenterController::displayToolBar();

		JToolBarHelper::addNew();
		JToolBarHelper::editList();
		JToolBarHelper::divider();
		JToolBarHelper::publishList();
		JToolBarHelper::unpublishList();
		JToolBarHelper::divider();

		//JToolBarHelper::divider();
		JToolBarHelper::deleteList();

		//$pparams = new JLTicketCenterHelperSetup;
		$pparams = new JLTicketCenterHelperSetup;
		if (JLTicketCenterControllerEvents::$_fb_sync_events==1){
			$fb = new JLTicketCenterFaceBook;
			$fb_link = $fb->fb_connect_link();
			$this->assignRef('fb_link',$fb_link);
		}

		$context			= 'com_jlticketcenter.events.list.';
		$filter_order		= $mainframe->getUserStateFromRequest($context.'filter_order','filter_order','e.event_date','cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($context.'filter_order_Dir','filter_order_Dir','','word' );
		$filter_catid		= $mainframe->getUserStateFromRequest($context.'filter_categories','filter_catid','','string');
		$filter_city		= $mainframe->getUserStateFromRequest($context.'filter_city','filter_city','','string');
		$filter_venue		= $mainframe->getUserStateFromRequest($context.'filter_venue','filter_venue','','string');
		$filter_performer	= $mainframe->getUserStateFromRequest($context.'filter_performer','filter_performer','','string');
		$filter_states		= $mainframe->getUserStateFromRequest($context.'filter_states','filter_states','','string');
		$filter_country		= $mainframe->getUserStateFromRequest($context.'filter_country','filter_country','','string');
		$filter_state		= $mainframe->getUserStateFromRequest($context.'filter_state','filter_state','','word');
		$search				= $mainframe->getUserStateFromRequest($context.'search','search','','string');
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);

		$filter['catid']	= $filter_catid;
		$filter['country']	= $filter_country;
		$filter['city']		= $filter_city;
		$filter['venue']	= $filter_venue;
		$filter['performer']= $filter_performer;
		$filter['states']	= $filter_states;
		$filter['state']	= $filter_state;
		$filter['search']	= $search;

		JRequest::setVar( 'filter', $filter);

		// Get data from the model
		$items		=  $this->get('Data');
		$pagination = $this->get('Pagination');

		JRequest::setVar('list',true);

		if ($pparams->get('display_country',0)>0){
			$countries = $this->get('CountrySelect');
			$lists['filter_country'] = $countries ;
		}else{
			$lists['filter_country'] = '';
		}

		if ($pparams->get('display_state',0)>0){
			$states =  $this->get('StateSelect');
			$lists['filter_states']	=$states;
		}else{
			$lists['filter_states']	='';
		}

		if ($pparams->get('display_city',0)>0){
			$cities =  $this->get('CitySelect');
			$lists['filter_city']	=$cities;
		}else{
			$lists['filter_city']	='';
		}

		if ($pparams->get('display_venue',0)>0){
			$venues = $this->get('VenueSelect');
			$lists['filter_venue']	=$venues;
		}else{
			$lists['filter_venue']='';
		}

		$lists['filter_performer']= $this->get('PerformerSelect');

		// build list of categories
		$javascript		= 'onchange="document.adminForm.submit();"';
		//$lists['cat_id'] = JHTML::_('list.category',  'filter_catid', 'com_jlticketcenter', (int)$filter_catid, $javascript );

		$lists['state']	= JHTML::_('grid.state',  $filter_state );
		$lists['search']	= $search;


		$lists['search']	= $search;
		//$lists['filter_categories']	=  $categories ;

		/* Call the state object */
		$state =  $this->get('state');
		$lists['order_Dir'] = $state->get('filter_order_Dir');
		$lists['order'] 	= $state->get('filter_order' );

		// push data into the template
		$this->assignRef('items',$items);
		$this->assignRef('pagination', $pagination);
		$this->assignRef('lists', $lists);
		$this->assignRef('params', $pparams);

		$csv_available = false;
		if(file_exists(JPATH_ADMINISTRATOR.'/components/com_jlticketcenter/helpers/jlticketcenter_pro_helper.php')){
			JLoader::import('components.com_jlticketcenter.helpers.jlticketcenter_pro_helper',JPATH_ADMINISTRATOR);
			$csv_available = true;
		}

		$this->assignRef('csv_available',$csv_available);
			

		parent::display($tpl);

		JLTicketCenterController::renderinfo();
	}


	function edit($tpl = null){

		$mainframe = JFactory::getApplication();
		$user	=  JFactory::getUser();

		JHTML::_('behavior.framework');
		JHTML::_('behavior.framework',true);
		JHtml::_('behavior.modal');

		$task = JRequest::getVar( 'task', '', 'method', 'string');

		JToolBarHelper::title( $task == 'add' ? JText::_('Event').': <small><small>[ '. JText::_( 'New' ) .' ]</small></small>' : JText::_( 'Event' ) . ': <small><small>[ '. JText::_( 'Edit' ) .' ]</small></small>', 'generic.png' );
		JToolBarHelper::apply('apply');
		JToolBarHelper::save('save');


		$doc = JFactory::getDocument();
		$jInsertFieldValue =
			'function jInsertFieldValue(value, id) {
				var old_id = document.id(id).value;
				if (old_id != id) {
					var elem = document.id(id)
					elem.value = value;
					elem.fireEvent("change");
				}
			}';
		$doc->addScriptDeclaration($jInsertFieldValue);

		$pparams = new JLTicketCenterHelperSetup;

		$context			= 'com_jlticketcenter.events.list.';
		$row = JTable::getInstance('events', 'Table');
			
		if ($task == 'edit') {
			$cid	= JRequest::getVar( 'cid', array(), 'get', 'array' );
				
			if (!$cid){
				$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );
			}
				
			JArrayHelper::toInteger($cid);
			$cids = implode(',',$cid);
				
			$row->load($cid[0]);
				
			JRequest::setVar('country_id',$row->country_id);
			JRequest::setVar('state_id',$row->state_id);
			JRequest::setVar('city_id',$row->city_id);
			JRequest::setVar('venue_id',$row->venue_id);
			JRequest::setVar('performer_id',$row->performer_id);
				
			// get Tickets Type
			JLoader::import('eventsticketstype', JPATH_ADMINISTRATOR.DS.'components'.DS.'com_jlticketcenter'.DS.'models');
			$tickets_model = JModelLegacy::getInstance('EventsTicketsType','JLTicketCenterModel');
			$tickets = $tickets_model->getLines($cid[0]);
				
			$this->assignRef('tickets',$tickets);

			if(file_exists(JPATH_ADMINISTRATOR.'/components/com_jlticketcenter/helpers/jlticketcenter_pro_helper.php')){
				JLoader::import('components.com_jlticketcenter.helpers.jlticketcenter_pro_helper',JPATH_ADMINISTRATOR);
				JLTicketCenterPro::eventDuplicate();
			}

		}else{
			// New events
			$filter_country	= $mainframe->getUserStateFromRequest( $context.'filter_country', 'filter_country','','string');
			$cid = 0;
				
			$row->load(0);
			$date = new JDate('now');
			$row->event_date = $date->toSql();
				
		}//end new


		JToolBarHelper::cancel('cancel');

		if ($cid[0]) {
			$row->checkout($user->get('id'));
		} else {
			$row->published = 1;
		}


		if ($pparams->get('display_country',1)>0){
			$countries = $this->get('CountrySelect');
		}
		if ($pparams->get('display_state',1)>0){
			$states = $this->get('StateSelect');
		}

		if ($pparams->get('display_city',1)>0){
			$cities= $this->get('CitySelect');
		}

		if ($pparams->get('display_venue',1)>0){
			$venues= $this->get('VenueSelect');
		}

		$performers = $this->get('PerformerSelect');

		$published 	= JHTML::_('select.genericlist', array(0=>JText::_('JNO'),1=>JText::_('JYES')), 'published', null, $optKey = 'value', $optText = 'text',$row->published);
		$event_type = JHTML::_('select.genericlist', array(0=>JText::_('PAID'),1=>JText::_('FREE'),2=>JText::_('ON_DOOR')), 'event_type', null, $optKey = 'value', $optText = 'text',$row->event_type);
		$feature	= JHTML::_('select.genericlist', array(0=>JText::_('JNO'),1=>JText::_('JYES')), 'feature', null, $optKey = 'value', $optText = 'text',$row->feature);

		if(file_exists(JPATH_ADMINISTRATOR.'/components/com_jlticketcenter/helpers/jlticketcenter_pro_helper.php')){
			JLoader::import('components.com_jlticketcenter.helpers.jlticketcenter_pro_helper',JPATH_ADMINISTRATOR);
			$ticket_template = JLTicketCenterPro::getTicketTemplateDropDown($row->ticket_template);
			$this->assignRef('ticket_template',$ticket_template);
		}
			

		$this->assignRef('countries',$countries);
		$this->assignRef('states',$states);
		$this->assignRef('cities',$cities);
		$this->assignRef('venues',$venues);
		$this->assignRef('performers',$performers);
		$this->assignRef('published',$published);
		$this->assignRef('feature',$feature);
		$this->assignRef('event',$row);
		$this->assignRef('params', $pparams);
		$this->assignRef('event_type', $event_type );

		parent::display($tpl);

		JLTicketCenterController::renderinfo();
	} //EOF edit
}//EOC