<?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 rseventsControllerDesigns 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');
	}
	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		$model = $this->getModel('designs');
		$db = & JFactory::getDBO();
		$row =& JTable::getInstance('Rsevents_Designs','Table');
		
		$cids=JRequest::getVar('cid',array(0),'post','array');
		
		
		if(!empty($cids))
		{
		foreach($cids as $cid) 
		{
		$db->setQuery("SELECT DesignName FROM #__rsevents_designs WHERE IdDesign='".$cid."'");
		$designName = $db->loadResult();
		if(!$row->delete($cid)) {
			$msg = JText::_('RSE_DESIGN_DELETE_ERROR' );
		} else {
			$dirname = JPATH_ROOT.DS.'components'.DS.'com_rsevents'.DS.'designs'.DS.$designName;

			if ($dirHandle = @opendir($dirname)) {
				while (false !== ($file = @readdir($dirHandle))) {
					if ($file == '.' || $file == '..') continue;
					else
					{
						$fullpath = $dirname . '/' . $file;
						@unlink($fullpath);
					}
				}
			}
			@rmdir(JPATH_ROOT.DS.'components'.DS.'com_rsevents'.DS.'designs'.DS.$designName);
			$msg = JText::_('RSE_DESIGN_DELETE' );
		}
		}
		} else
		{
			$msg = JText::_('RSE_DESIGN_SELECT' );
		}
		$row->reorder();
		$this->setRedirect( 'index.php?option=com_rsevents&task=listdesigns', $msg );
	}

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

		$db 	=& JFactory::getDBO();
		$msg='';
		$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 ) );
		}
		
		$db->setQuery("SELECT IdDesign FROM #__rsevents_designs WHERE DesignDefault='1'");
		$defaultIdDesign = $db->loadResult();
		foreach($cid as $i => $idDesign)
		if($defaultIdDesign==$idDesign) 
		{
			unset($cid[$i]);
			$msg=JText::_('RSE_DESIGN_UNPUBLISH_ERROR');
		}
		if(count($cid)>0) 
		{
			$cids = implode( ',', $cid );
			
			$query = 'UPDATE #__rsevents_designs'
			. ' SET published = ' . (int) $publish
			. ' WHERE IdDesign IN ( '. $cids .' )'
			;
			$db->setQuery( $query );
			if (!$db->query())
			{
				JError::raiseError(500, $db->getErrorMsg() );
			}
		}
		$mainframe->redirect( 'index.php?option=com_rsevents&task=listdesigns',$msg );
	}
	
	
	function makeDefault()
	{
		global $mainframe;

		$db 	=& JFactory::getDBO();

		$cid		= JRequest::getVar( 'cid', array(), '', 'array' );
		
		if(is_array($cid)) $cid=$cid[0];
		$db->setQuery("SELECT published FROM #__rsevents_designs WHERE IdDesign=".$cid);
		if(!$db->loadResult())
			$mainframe->redirect('index.php?option=com_rsevents&task=listdesigns',JText::_('RSE_DESIGN_DEFAULT_ERROR'));
		$query = 'UPDATE #__rsevents_designs'
		. ' SET DesignDefault = 0'
		;
		$db->setQuery( $query );
		if (!$db->query())
		{
			JError::raiseError(500, $db->getErrorMsg() );
		}
		$query = 'UPDATE #__rsevents_designs'
		. ' SET DesignDefault = 1'
		. ' WHERE IdDesign= '.$cid
		;
		$db->setQuery( $query );
		if (!$db->query())
		{
			JError::raiseError(500, $db->getErrorMsg() );
		}

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

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

		// 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_Designs','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_DESIGN_ORDER_SAVE');
		
		$this->setRedirect( 'index.php?option=com_rsevents&task=listdesigns', $msg );

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

	}
	
	
}