<?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 rseventsControllerLocations extends rseventsController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();

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

	/**
	 * display the edit form
	 * @return void
	 */
	function edit()
	{
		JRequest::setVar( 'view', 'locations' );
		JRequest::setVar( 'layout', 'edit'  );
		JRequest::setVar('hidemainmenu', 1);
		parent::display();
	}

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		$model = $this->getModel('locations');
		$post=JRequest::get('post',JREQUEST_ALLOWHTML);
		
		$row= & JTable::getInstance('Rsevents_Locations','Table');
		
		if(!$row->bind($post)) {
			return JError::raiseWarning(500, $row->getError());
		}
		
		//creating the alias
		$alias = JFilterOutput:: stringURLSafe($row->LocationName);
		if(empty($row->LocationAlias)) $row->LocationAlias = $alias;
		
		if ($row->store()) {
			$msg = JText::_('RSE_LOCATION_SAVE' );
		} else {
			JError::raiseWarning(500, $row->getError());

			$msg = JText::_('RSE_LOCATION_SAVE_ERROR' );
		}
		$row->reorder();
		switch(JRequest::getCmd('task'))
		{
			case 'apply' :
					$link = 'index.php?option=com_rsevents&task=editlocation&cid='.$row->IdLocation;

			break;
			
			case 'save' :
					$link = 'index.php?option=com_rsevents&task=listlocations';

			break;
		}
		// Check the table in so it can be edited.... we are done with it anyway
		$this->setRedirect($link, $msg);
	}

	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		$model = $this->getModel('locations');
		$row =& JTable::getInstance('Rsevents_Locations','Table');
		$cids=JRequest::getVar('cid',array(0),'post','array');
		$db = & JFactory::getDBO();
		
		$db->setQuery("SELECT COUNT(*) AS cnt FROM #__rsevents_events WHERE IdLocation IN ('".implode("','",$cids)."')");
		$eventsFound = $db->loadResult();
		if(empty($eventsFound)) 
		{
			$db->setQuery("DELETE FROM #__rsevents_locations WHERE IdLocation IN ('".implode("','",$cids)."')");
			$db->query();
			$msg= JText::sprintf('RSE_LOCATION_DELETE',count($cids));
			$this->setRedirect( 'index.php?option=com_rsevents&task=listlocations', $msg );
			$row->reorder();
		}
		else 
		$msg = JText::_('RSE_LOCATION_DELETE_ERROR' );
		$this->setRedirect( 'index.php?option=com_rsevents&task=listlocations', $msg , 'error');
	}

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_('RSE_CANCEL_OPERATION' );
		$this->setRedirect( 'index.php?option=com_rsevents&task=listlocations', $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_locations'
		. ' SET published = ' . (int) $publish
		. ' WHERE IdLocation IN ( '. $cids .' )'
		;
		$db->setQuery( $query );
		if (!$db->query())
		{
			JError::raiseError(500, $db->getErrorMsg() );
		}

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

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

		// 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_Locations','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;
				}
			}
		}


		$msg = JText::_('RSE_LOCATION_SAVE_ORDER');
		
		$this->setRedirect( 'index.php?option=com_rsevents&task=listlocations', $msg );

	}
	
	function move() 
	{
		$db			= & JFactory::getDBO();
		$model = $this->getModel('locations');
		$row = & JTable::getInstance('Rsevents_Locations','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=listlocations');
		

	}
	
}