<?php
/**
 * @version		$Id: category.php 115 2009-06-17 07:20:36Z eddieajau $
 * @copyright	Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
 * @license		GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
 * @link		http://www.theartofjoomla.com
 */

// no direct access
defined('_JEXEC') or die;

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

/**
 * @package		TAOJ.Quanta
 * @subpackage	com_quanta
 */
class QuantaControllerCategory extends JController
{
	/**
	 * Constructor
	 */
	function __construct()
	{
		parent::__construct();

		$this->registerTask('save2copy',	'save');
		$this->registerTask('save2new',	'save');
		$this->registerTask('apply',		'save');
	}

	/**
	 * Display the view
	 */
	function display()
	{
	}

	/**
	 * Proxy for getModel
	 */
	function &getModel()
	{
		return parent::getModel('Category', '', array('ignore_request' => true));
	}

	/**
	 * Method to edit a object
	 *
	 * Sets object ID in the session from the request, checks the item out, and then redirects to the edit page.
	 *
	 * @access	public
	 * @return	void
	 */
	function edit()
	{
		$cid = JRequest::getVar('cid', array(), '', 'array');
		$id  = JRequest::getInt('id', @$cid[0]);

		$app = &JFactory::getApplication();
		$app->setUserState('com_quanta.edit.category.id', $id);

		// Checkout item
		$model = $this->getModel();

		if ($id) {
			$model->checkout($id);
		}
		$this->setRedirect(JRoute::_('index.php?option=com_quanta&view=category&layout=edit', false));
	}

	/**
	 * Method to cancel an edit
	 *
	 * Checks the item in, sets item ID in the session to null, and then redirects to the list page.
	 *
	 * @access	public
	 * @return	void
	 */
	function cancel()
	{
		// Checkin item if checked out
		$app = &JFactory::getApplication();
		if ($id = (int) $app->getUserState('com_quanta.edit.category.id')) {
			$model = $this->getModel();
			$model->checkin($id);
		}

		// Clear the session of the item
		$app->setUserState('com_quanta.edit.category.id', null);

		$this->setRedirect(JRoute::_('index.php?option=com_quanta&view=categories', false));
	}

	/**
	 * Save the record
	 */
	function save()
	{
		// Check for request forgeries.
		JRequest::checkToken();

		// Get posted form variables.
		$values		= JRequest::getVar('jxform', array(), 'post', 'array');

		// Get the id of the item out of the session.
		$app 		= &JFactory::getApplication();
		$id			= (int) $app->getUserState('com_quanta.edit.category.id');
		$values['id'] = $id;

		$values['section']	= 'com_quanta';

		unset($values['parent_id']);
		unset($values['checked_out']);
		unset($values['checked_out_time']);
		unset($values['editor']);
		unset($values['count']);

		// Get the extensions model and set the post request in its state.
		$model	= &$this->getModel();
		$post = JRequest::get('post');
		$model->setState('request', $post);

		// Get the form model object and validate it.
		$form	= &$model->getForm('model');
		$form->filter($values);
		$result	= $form->validate($values);

		if (JError::isError($result)) {
			JError::raiseError(500, $result->message);
		}

		$result	= $model->save($values);
		$msg	= JError::isError($result) ? $result->message : 'Saved';

		if ($this->_task == 'apply')
		{
			$app->setUserState('com_quanta.edit.category.id', $model->getState('category.id'));
			$this->setRedirect(JRoute::_('index.php?option=com_quanta&view=category&layout=edit', false), JText::_($msg));
		}
		else if ($this->_task == 'save2new')
		{
			$app->setUserState('com_quanta.edit.category.id', null);
			$model->checkin($id);
			$this->setRedirect(JRoute::_('index.php?option=com_quanta&view=category&layout=edit', false), JText::_($msg));
		}
		else
		{
			$app->setUserState('com_quanta.edit.category.id', null);
			$model->checkin($id);
			$this->setRedirect(JRoute::_('index.php?option=com_quanta&view=categories', false), JText::_($msg));
		}
	}

	/**
	 * Removes an item
	 */
	function delete()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit(JText::_('JInvalid_token'));

		// Get items to remove from the request.
		$cid	= JRequest::getVar('cid', array(), '', 'array');

		if (!is_array($cid) || count($cid) < 1) {
			JError::raiseWarning(500, JText::_('Select an item to delete'));
		}
		else {
			// Get the model.
			$model = $this->getModel();

			// Make sure the item ids are integers
			jimport('joomla.utilities.arrayhelper');
			JArrayHelper::toInteger($cid);

			// Remove the items.
			if (!$model->delete($cid)) {
				JError::raiseWarning(500, $model->getError());
			}
		}

		$this->setRedirect('index.php?option=com_quanta&view=categories');
	}

	/**
	 * Method to publish a list of taxa
	 *
	 * @access	public
	 * @return	void
	 * @since	1.0
	 */
	function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit(JText::_('JInvalid_token'));

		// Get items to publish from the request.
		$cid	= JRequest::getVar('cid', array(), '', 'array');
		$values	= array('publish' => 1, 'unpublish' => 0, 'trash' => -2);
		$task	= $this->getTask();
		$value	= JArrayHelper::getValue($values, $task, 0, 'int');

		if (!is_array($cid) || count($cid) < 1) {
			JError::raiseWarning(500, JText::_('Select an item to publish'));
		}
		else {
			// Get the model.
			$model	= $this->getModel();

			// Make sure the item ids are integers
			JArrayHelper::toInteger($cid);

			// Publish the items.
			if (!$model->publish($cid, $value)) {
				JError::raiseWarning(500, $model->getError());
			}
		}

		$this->setRedirect('index.php?option=com_quanta&view=categories');
	}

	/**
	 * Changes the order of an item
	 */
	function ordering()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit(JText::_('JInvalid_token'));

		$cid	= JRequest::getVar('cid', null, 'post', 'array');
		$inc	= $this->getTask() == 'orderup' ? -1 : +1;

		$model = & $this->getModel();
		$model->ordering($cid, $inc);

		$this->setRedirect('index.php?option=com_quanta&view=categories');
	}
}