<?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 rseventsControllerArchive extends rseventsController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();
	}
	
	function remove()
	{
		$db= JFactory::getDBO();
		$model = $this->getModel('events');
		$cids=JRequest::getVar('cid',array(0),'post','array');
		$row =& JTable::getInstance('Rsevents_Events','Table');
		
		if(!empty($cids))
		{
		foreach($cids as $cid) 
		{
		
		if(!$row->delete($cid)) {
			$msg = JText::_('RSE_EVENT_DELETE_ERROR' );
		} else {
			$db->setQuery("SELECT FileLocation FROM #__rsevents_files WHERE IdEvent=".$cid);
			$arrayLocations=$db->loadObjectList();
			foreach ($arrayLocations as $obj)
			{
				@unlink(JPATH_SITE.DS.'components'.DS.'com_rsevents'.DS.'files'.DS.$obj->FileLocation);
			}
			$db->setQuery("DELETE FROM #__rsevents_files WHERE IdEvent='".$cid."'");
			$db->query();
			$db->setQuery("DELETE FROM #__rsevents_tickets WHERE IdEvent='".$cid."'");
			$db->query();
			$db->setQuery("SELECT IdSubscription FROM #__rsevents_subscriptions WHERE IdEvent='".$cid."'");
			$idSubscriptions = $db->loadObjectList();
			foreach($idSubscriptions as $i => $idSubscription)
			{
				$db->setQuery("DELETE FROM #__rsevents_subscription_tickets WHERE IdSubscription='".$idSubscription->IdSubscription."'");
				$db->query();

			}
			$db->setQuery("DELETE FROM #__rsevents_subscriptions WHERE IdEvent='".$cid."'");
			$db->query();
			$row->reorder();
			$msg = JText::_( 'RSE_EVENT_DELETE' );
		}
		}
		} else
		{
			$msg = JText::_('RSE_EVENT_SELECT' );
		}
		$this->setRedirect( 'index.php?option=com_rsevents&task=archive', $msg );
	}

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

	
	//archive the events
	
	function unarchive()
	{
		global $mainframe;
		$db =& JFactory::getDBO();
		$cid = JRequest::getVar('cid',array(),'request','array');
		
		$cids = implode(',', $cid );
		
		$db->setQuery("UPDATE #__rsevents_events SET published = '0' WHERE IdEvent IN (".$cids.") ");
		$db->query();
		$db->setQuery("UPDATE #__rsevents_events SET EventArchive = '1' WHERE IdEvent IN (".$cids.") ");
		$db->query();
		
		$total = count($cid);
		
		$msg = $total.' '.JText::_('RSE_EVENT_UNARCHIVED');
		
		$mainframe->redirect('index.php?option=com_rsevents&task=archive',$msg );
	}
}