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

defined( '_JEXEC' ) or die( 'Restricted access' );

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

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

	function display($cachable = false, $urlparams = Array())
	{
		$model = $this->getModel('state');
		$items	= $model->getData();

		JRequest::setVar('view', 'state');

		parent::display();
	}

	function edit()
	{
		JRequest::setVar('view','state');
		JRequest::setVar('layout','form');
		JRequest::setVar('hidemainmenu',1);

		parent::display();
	}

	function save()
	{
		$model = $this->getModel('state');


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

		$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);
	} // EOF save

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

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


	function cancel()
	{
		$model = $this->getModel('state');
		$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_state'
		. ' 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 ) );
	}

}// EOC