<?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
 *
 *
 */

// http://docs.joomla.org/Potential_backward_compatibility_issues_in_Joomla_3.0_and_Joomla_Platform_12.1
// http://docs.joomla.org/Client-side_form_validation
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

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

    class JLTicketCenterController extends JControllerLegacy
{
	protected static $_option = 'com_jlticketcenter';


	function __construct( $config = array() )
	{
		define('GENERAL_MODEL',"JLTicketCenterModelGeneral");
		parent::__construct( $config );
		// Register Extra tasks
		$this->registerTask( 'add',		'edit' );
		$this->registerTask( 'apply',		'save' );
		$this->registerTask( 'unpublish',	'publish' );
	}

	function display($cachable = false, $urlparams = array())
	{
		require_once JPATH_COMPONENT.'/helpers/jlticketcenter.php';

        //var_dump(JRequest::get('view'));
        //var_dump(JFactory::getApplication()->input->get('layout') );


		// Load the submenu.
        if ( is_null( JFactory::getApplication()->input->get('layout')) )
        {
            JLTicketCenterHelper::addSubmenu(JFactory::getApplication()->input->get('view'));
            //JLTicketCenterHelper::addSubmenu(JRequest::get('view', 'events'));
        }



		parent::display();
	}

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

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

		// Initialize variables
		$db			= & JFactory::getDBO();

		$cid		= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$order		= JRequest::getVar( 'order', array (0), 'post', 'array' );
		$total		= count($cid);
		$conditions	= array ();
		$controller = JRequest::getWord('controller');

		JArrayHelper::toInteger($cid, array(0));
		JArrayHelper::toInteger($order, array(0));


		$model = JModelLegacy::getInstance($controller, 'JLTicketCenterModel');

		$row = $model->getTable();
		$table_name = '#__jltcenter_'.$model->getName();
		$table_fields = $db->getTableFields($table_name);

		// Update the ordering for items in the cid array
		for ($i = 0; $i < $total; $i ++)
		{
			$row->load((int)$cid[$i]);
			if ($row->ordering != $order[$i]) {
				$row->ordering = $order[$i];
				if (!$row->store()) {
					JError::raiseError( 500, $db->getErrorMsg() );
					return false;
				}
				// remember to updateOrder this group
				if ($table_fields[$table_name][cat_id]){
					$condition = 'cat_id = '.(int) $row->cat_id.' AND published=1';
				}else{
					$condition = ' published==1 ';
				}

				$found = false;
				foreach ($conditions as $cond)
				if ($cond[1] == $condition) {
					$found = true;
					break;
				}
				if (!$found)
				$conditions[] = array ($row->id, $condition);
			}
		}

		// execute updateOrder for each group
		foreach ($conditions as $cond)
		{
			$row->load($cond[0]);
			$row->reorder($cond[1]);
		}

		$cache = & JFactory::getCache(self::$_option);
		$cache->clean();

		$msg = JText::_('New ordering saved');

		$mainframe->redirect('index.php?option='.self::$_option.'&controller='.JRequest::get('controller'), $msg);

	}// EOF saveorder


	public function getTable($type = '', $prefix = 'Table', $config = array())
	{
		$type =JRequest::getWord('controller');
		return JTable::getInstance($type, $prefix, $config);
	}


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


		$controller = JRequest::getWord('controller');

		// 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 = JModelLegacy::getInstance($controller, 'JLTicketCenterModel');

			$row = $model->getTable();
				
			$table_name = '#__jltcenter_'.$model->getName();
			$table_fields = $db->getTableFields($table_name);
				
			$row->load((int) $cid[0]);
				
			switch ($model->getName()){
				case 'venue':
					$condition = 'city_id = '.(int) $row->city_id .' AND published=1';
					break;

				case 'performer':
					$condition = 'cat_id = '.(int) $row->cat_id.' AND published=1';
					break;

				default:
					$condition = 'published==1';
					break;
			}
				
				
			$row->move($direction, $condition);
				
			$cache = & JFactory::getCache(self::$_option);
			$cache->clean();
		}

		//$mainframe->redirect('index.php?option='.self::$_option.'&controller='.JFactory::getApplication()->input->get('controller'), $msg);
		$mainframe->redirect('index.php?option='.self::$_option.'&controller='.JFactory::getApplication()->input->get('controller'));
	}// EOF orderGalleries

	public static function displayToolBar(){
		JToolBarHelper::addNew();
		JToolBarHelper::editList();
		JToolBarHelper::divider();
		JToolBarHelper::publishList();
		JToolBarHelper::unpublishList();
		JToolBarHelper::divider();
		JToolBarHelper::deleteList();
	}

	public static function renderinfo(){
		$str = 'PGRpdiBzdHlsZT0idGV4dC1hbGlnbjogY2VudGVyOyBjb2xvcjogcmdiKDIxMSwgMjExLCAyMTEpOyI+UG93ZXJlZCBieSA8YSB0aXRsZT0iam9tbGFuZC5jb20iIHRhcmdldD0iX2JsYW5rIiBzdHlsZT0idGV4dC1kZWNvcmF0aW9uOiBub25lOyIgaHJlZj0iaHR0cDovL3d3dy5qb21sYW5kLmNvbS8iPkpvbUxhbmQ8L2E+IDxhIHRpdGxlPSJUaWNrZXRzIENlbnRlciIgdGFyZ2V0PSJfYmxhbmsiIHN0eWxlPSJ0ZXh0LWRlY29yYXRpb246IG5vbmU7IiBocmVmPSJodHRwOi8vd3d3LmpvbWxhbmQuY29tL2V4dGVuc2lvbnMvamx0aWNrZXRjZW50ZXIvb3ZlcnZpZXciPlRpY2tldHMgQ2VudGVyPC9hPjwvZGl2Pg==';
		echo base64_decode($str);
	}
}//EOC