<?php
/**
* @version 1.0.0
* @package RSEvents! 1.0.0
* @copyright (C) 2009 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

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


class rseventsControllerCategoriesElement extends rseventsController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
		$this->registerTask( 'unpublish' , 'publish');
		$this->registerTask( 'orderup' , 'move');
		$this->registerTask( 'orderdown' , 'move');
	}

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_('RSE_CANCEL_OPERATION' );
		$this->setRedirect( 'index.php?option=com_rsevents&task=listcategories', $msg );
	}
	
		function publish()
	{
		global $mainframe;

		$db 	=& JFactory::getDBO();

		$cid		= JRequest::getVar( 'cid', array(), '', 'array' );
		$publish	= ( $this->getTask() == 'publish' ? 1 : 0 );

		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1)
		{
			$action = $publish ? 'publish' : 'unpublish';
			JError::raiseError(500, JText::_('RSE_PUBLISHED_ERROR' .$action, true ) );
		}

		$cids = implode( ',', $cid );
		
		$query = 'UPDATE #__rsevents_categories'
		. ' SET published = ' . (int) $publish
		. ' WHERE IdCategory IN ( '. $cids .' )'
		;
		$db->setQuery( $query );
		if (!$db->query())
		{
			JError::raiseError(500, $db->getErrorMsg() );
		}

		$mainframe->redirect( 'index.php?option=com_rsevents&task=categorieselement&tmpl=component' );
	}
	
	function saveOrder()
	{
		global $mainframe;

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

		// Check for request forgeries
		JRequest::checkToken() 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 ();

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

		// Instantiate an article table object
		$row = & JTable::getInstance('Rsevents_Categories','Table');

		
		for( $i=0; $i < count($cid); $i++ )
		{
			$row->load( (int) $cid[$i] );

			if ($row->ordering != $order[$i])
			{	
				$row->ordering = $order[$i];
				if (!$row->store()) 
				{
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}
		
		$this->setRedirect( 'index.php?option=com_rsevents&task=categorieselement&tmpl=component');

	}
	
	function move() 
	{
		$db			= & JFactory::getDBO();
		$model = $this->getModel('categories');
		$row = & JTable::getInstance('Rsevents_Categories','Table');
		$cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$task = JRequest::getVar('task');
		if($task=='orderup') $direction=-1;
		else $direction=1;
		if(is_array($cid)) $cid=$cid[0];
		
		if (!$row->load($cid)) 
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}	

		$row->move( $direction);
	
		$this->setRedirect( 'index.php?option=com_rsevents&task=categorieselement&tmpl=component');
		

	}
	
}