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

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

class JLTicketCenterControllerPaymentMethods extends JLTicketCenterController
{
	function __construct()
	{
		parent::__construct();

		$this->registerTask('add', 'edit');
		$this->registerTask('orderup', 'orderPaymentMethods');
		$this->registerTask('orderdown', 'orderPaymentMethods');
	}


	function display($cachable = false, $urlparams = Array())
	{
		JRequest::setVar( 'view', 'paymentmethods' );

		parent::display();
	}

	function edit()
	{

		JRequest::setVar( 'view', 'paymentmethods');
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}

	function save()
	{
		JSession::checkToken('request') or die( 'Invalid Token' );

		$model = $this->getModel('paymentmethods');

		$post	= JRequest::get('post');
		$post = $this->savePaymentParams($post);

		if ($model->store($post)) {
			$msg = JText::_( 'Payment Methods Saved!' );
		} else {
			$msg = JText::_( 'Error Saving Payment Methods' );
		}

		$task	= JRequest::getCmd( 'task' );
		$id	= JRequest::getVar( 'id', 0, 'post');
		switch ($task)
		{
			case 'apply':
				$link = 'index.php?option=com_jlticketcenter&controller='.JRequest::getWord('controller').'&task=edit&cid[]='.$id;
				break;

			case 'save':
			default:
				$link = 'index.php?option=com_jlticketcenter&controller='.JRequest::getWord('controller');
				break;
		}


		$this->setRedirect($link, $msg);
	}

	function remove()
	{
		$model = $this->getModel('paymentmethods');
		if(!$model->delete()) {
			$msg = JText::_( 'Error: One or More Cities Could not be Deleted' );
		} else {
			$msg = JText::_( 'Payment Method(s) Deleted' );
		}

		$this->setRedirect( 'index.php?option=com_jlticketcenter&controller='.JRequest::getWord('controller'), $msg );
	}

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$model = $this->getModel('paymentmethods');
		$model->cancel();


		$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=com_jlticketcenter&controller='.JRequest::getWord('controller'), $msg );
	}


	function publish()
	{

		// Check for request forgeries
		JSession::checkToken('request') or jexit( 'Invalid Token' );

		$this->setRedirect( 'index.php?option=com_jlticketcenter&controller='.JRequest::getWord('controller'));

		// Initialize variables
		$db			=& JFactory::getDBO();
		$user		=& JFactory::getUser();
		$cid		= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$task		= JRequest::getCmd( 'task' );
		$publish	= ($task == 'publish');
		$n			= count( $cid );

		if (empty( $cid )) {
			return JError::raiseWarning( 500, JText::_( 'No items selected' ) );
		}

		JArrayHelper::toInteger($cid);
		$cids = implode(',',$cid);

		$query = 'UPDATE #__jltcenter_payment_method'
		. ' SET `published` = ' . (int) $publish
		. ' WHERE id IN ( '. $cids.'  )'
		. ' AND ( checked_out = 0 OR ( checked_out = ' .(int) $user->get('id'). ' ) )'
		;
		$db->setQuery( $query );
		if (!$db->query()) {
			return JError::raiseWarning( 500, $db->getError() );
		}
		$this->setMessage( JText::sprintf( $publish ? 'Items published' : 'Items unpublished', $n ) );
	}//EOF


	function savePaymentParams($data){

		$payment_class = $data['name'];
		$path = JPATH_COMPONENT.DS.'models'.DS.'payment'.DS.strtolower($payment_class).'.php';

		$payment_params = $data['params'];
		unset ($data['params']);


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

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


		$payment->setConfig($payment_class,$payment_params);


		return $data;

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



	function orderPaymentMethods()
	{
		$mainframe = JFactory::getApplication();

		// Check for request forgeries
		JSession::checkToken('request') or jexit( 'Invalid Token' );

		if (JRequest::getVar('task','')=='orderup'){
			$direction = -1;
		}else{
			$direction = 1;
		}

		// Initialize variables
		$db		= & JFactory::getDBO();
		$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );

		if (isset( $cid[0] ))
		{
			$model = $this->getModel('PaymentMethods');
			$row = $model->getTable();
				
			$row->load((int) $cid[0] );
			$row->move($direction);

			$cache = & JFactory::getCache('com_jlticketcenter');
			$cache->clean();
				
		}


		$mainframe->redirect('index.php?option='.self::$_option.'&controller='.JRequest::getWord('controller'));
	}// EOF orderGalleries

}// EOC