<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );
//require_once(JPATH_BASE.DS.'components'.DS.'com_kiodict'.DS.'helpers'.DS.'dictionary.php');
jimport( 'joomla.application.component.controller' );

define('MODEL_NAME', 'Rules');


class KipControllerRules extends JController
{
	function __construct( $default = array()){
		parent::__construct( $default );

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

	}

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

		$model	=& $this->getModel(MODEL_NAME);
		$model->checkout();

		switch($this->getTask())
		{
			case 'add':
				JRequest::setVar( 'edit', false  );
			break;
			case 'edit':
				JRequest::setVar( 'edit', true  );
			break;

		}
		parent::display();
	}

	function save()
	{
		global $option;
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$model	=& $this->getModel(MODEL_NAME);
		$item =& $model->getItem();

		if ($model->store()) {
			$msg = JText::_( 'Item Saved' );
			$cache = & JFactory::getCache($option);
			$cache->clean();
		} else {
			$msg = $item->getError();
		}


		switch ( $this->_task ) {
			case 'apply':
				$this->setRedirect( 'index.php?option='.$option.'&view='.$model->getName().'&task=edit&cid[]='.$item->id.'' , $msg );
				break;

			case 'save':
			default:
				$this->setRedirect( 'index.php?option='.$option.'&view='.$model->getName(), $msg );
				break;
		}
	}

	function cancel()
	{
		global $option;
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$model	=& $this->getModel(MODEL_NAME);
		$model->checkin();
		$this->setRedirect( 'index.php?option='.$option.'&view='.$model->getName() );
	}
	
	function publish(){
		global $option;
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Get some variables from the request
		$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		$model =& $this->getModel(MODEL_NAME);
		if ($model->setItemState($cid, 1)) {
			$msg = JText::sprintf( 'Item(s) Enabled', count( $cid ) );
			$cache = & JFactory::getCache($option);
			$cache->clean();
		} else {
			$msg = $model->getError();
		}
		$this->setRedirect( 'index.php?option='.$option.'&view='.$model->getName(), $msg );
	}

	function unpublish(){
		global $option;
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Get some variables from the request
		$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		$model =& $this->getModel(MODEL_NAME);
		if ($model->setItemState($cid, 0)) {
			$msg = JText::sprintf( 'Item(s) Disabled', count( $cid ) );
			$cache = & JFactory::getCache($option);
			$cache->clean();
		} else {
			$msg = $model->getError();
		}
		$this->setRedirect( 'index.php?option='.$option.'&view='.$model->getName(), $msg );
	}
	
	function remove(){
		global $option;
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Get some variables from the request
		$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		$msg = '';
		if (!count($cid)) {
			$msg = JText::_('No Items Selected');
		}
		$model =& $this->getModel(MODEL_NAME);
		if(!$msg){
			if ($n = $model->remove($cid)){
				$msg = JText::sprintf('Items removed', $n);
				$cache = & JFactory::getCache($option);
				$cache->clean();
			}else
				$msg = $model->getError();
		}
		$this->setRedirect( 'index.php?option='.$option.'&view='.$model->getName(), $msg );
	}
	
	
}