<?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' );
jimport('joomla.html.pane');


class JLTicketCenterViewPaymentMethods 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::_( 'Payment Methods - JomLand TicketCenter' ), 'generic.png' );


		/*JToolBarHelper::publishList();
		JToolBarHelper::unpublishList();
		JToolBarHelper::editListX();*/

		$pparams = new JLTicketCenterHelperSetup;

		$context			= 'com_jlticketcenter.paymentmethods.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['state']	= $filter_state;
		$filter['search']	= $search;


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


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

		$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 edit($tpl = null){

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

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

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

		$pparams = new JLTicketCenterHelperSetup;

		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 = JTable::getInstance('PaymentMethods', 'Table');
			$row->load( $cid[0] );
		}

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

		if ($row){
			$payment_config = $this->paymentConfig($row->name);
		}


		// build the html select list for ordering
		$query = 'SELECT ordering AS value, name AS text'
		. ' FROM #__jltcenter_payment_method'
		. ' ORDER BY ordering';

        $lists['ordering'] = JHTML::_('list.ordering',  'ordering', $query, null, $row->ordering );

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

		$this->assignRef('published',$published);
		$this->assignRef('row',$row);
		$this->assignRef('payment_config',$payment_config);
		$this->assignRef('lists',$lists);

		$this->assignRef('params', $pparams);

		parent::display($tpl);

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


	// get payment config
	function paymentConfig($payment_class){

		$path = JPATH_COMPONENT.DS.'models'.DS.'payment'.DS.strtolower($payment_class).'.php';

		if (file_exists($path)) {
			require_once $path;
		} else {
			return;
		}

		$class_name = 'JLTicketCenter'.$payment_class;
		$payment = new $class_name;


			
		$output = $payment->buildPanel($payment_class);
		return $output;
	}//EOF paymentConfig
}//EOC