<?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 JLTicketCenterViewCity 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::_( 'Cities - JomLand TicketCenter' ), 'generic.png' );
		JLTicketCenterController::displayToolBar();
			
		$pparams = new JLTicketCenterHelperSetup;

		$context			= 'com_jlticketcenter.city.list.';

		$filter_order		= $mainframe->getUserStateFromRequest($context.'filter_order',		'filter_order',		'cc.title',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($context.'filter_order_Dir',	'filter_order_Dir',	'',			'word' );
		$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['country']	= $filter_country;
		$filter['state']	= $filter_state;
		$filter['search']	= $search;

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

		// Get data from the model
		$items		= $this->get('Data');
		$pagination = $this->get('Pagination');
		if ($pparams->get('display_country',0)>0){
			$countries = $this->buildCounrySelect($filter_country);
			$lists['filter_country'] =  $countries ;
		}else{
			$lists['filter_country'] =  '';
		}

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


		/* 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);


		parent::display($tpl);
		JLTicketCenterController::renderinfo();
	}

	function buildCounrySelect($default='',$avoid_js = false,$name=''){
		$countries =  $this->get('Country');

		if (!$avoid_js){
			$javascript		= 'onchange="document.adminForm.submit();"';
		}

		if (!$name){
			$name = 'filter_country';
		}


		$options[] = JHTML::_('select.option',  '', '- '. JText::_( 'Select a Country' ) .' -' );
		foreach($countries as $country){
			$options[] = JHTML::_('select.option', $country->id, $country->country);
		}

		$dropdown = JHTML::_('select.genericlist',   $options, $name, 'class="inputbox"'. $javascript, 'value', 'text', $default );
			
		return  $dropdown;
	}// EOF buildCounrySelect

	function edit($tpl = null){

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

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

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

		$pparams = new JLTicketCenterHelperSetup;

		$row = JTable::getInstance('city', '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]);
				
			$filter_country = $row->country_id;
			JRequest::setVar('country_id',$row->country_id);
			JRequest::setVar('state_id',$row->state_id);
		}else{
			$context			= 'com_jlticketcenter.city.list.';
			$filter_country		= $mainframe->getUserStateFromRequest( $context.'filter_country',	'filter_country',	'','string');
		}

		if (!empty($cid[0])){
			$row->checkout($user->get('id') );
		} else {
			$row->load(0);
			$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');
		}

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

		$this->assignRef('countries',$countries);
		$this->assignRef('published',$published);
		$this->assignRef('city',$row);
		$this->assignRef('countries',$countries);
		$this->assignRef('states',$states);
		$this->assignRef('params', $pparams);

		parent::display($tpl);

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