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

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		//get variables
		$model = $this->getModel('files');
		$post=JRequest::get('post');
		$db = & JFactory::getDBO();
		$cid = JRequest::getVar('cid',0);
		$cid = intval($cid);
		
		//get the itemId
		$Itemid = intval(JRequest::getVar('Itemid','','request'));
		$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		
		//set permissions
		$filePermission = (isset($post['FilePermission'][0])? 1 : 0);
		$filePermission .= (isset($post['FilePermission'][1])? 1 : 0);
		$filePermission .= (isset($post['FilePermission'][2])? 1 : 0);
		$filePermission .= (isset($post['FilePermission'][3])? 1 : 0);
		$filePermission .= (isset($post['FilePermission'][4])? 1 : 0);
		$filePermission .= (isset($post['FilePermission'][5])? 1 : 0);
		
		
		$post['FilePermission'] = $filePermission;
		
		$row= & JTable::getInstance('Rsevents_Files','Table');

		$db->setQuery("SELECT EventName FROM #__rsevents_events WHERE IdEvent='".$cid."'");
		$res = $db->loadResult();
		
		
		if(!$row->bind($post)) {		
		return JError::raiseWarning(500, $row->getError());
		}

		if ($row->store()) {
			$msg = JText::_( 'RSE_FILE_SAVE' );
		} else {
			$msg = JText::_( 'RSE_FILE_SAVE_ERROR' );
		}
		

		$link = html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=addeditevent&cid='.$cid.':'.JFilterOutput:: stringURLSafe($res).'&page=2'.$itemId));
		$this->setRedirect($link, $msg);
	}

	/**
	 * delete record(s)
	 * @return void
	 */
	function deleteFile()
	{
		$model = $this->getModel('files');
		$db = & JFactory::getDBO();
		$user = &JFactory::getUser();
		$row =& JTable::getInstance('Rsevents_Files','Table');
		$row1 =& JTable::getInstance('Rsevents_Events','Table');
		
		//get the itemId
		$Itemid = intval(JRequest::getVar('Itemid','','request'));
		$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		
		$cid= intval(JRequest::getVar('cid'));
		$idEvent = intval(JRequest::getVar('IdEvent'));
		$db->setQuery("SELECT EventName FROM #__rsevents_events WHERE IdEvent='".$idEvent."'");
		$res = $db->loadResult();
		$row->load($cid);
		$row1->load($idEvent);
		
		if($user->usertype == 'Super Administrator' || $user->id == $row1->IdUser )
		{
			unlink(JPATH_SITE.DS.'components'.DS.'com_rsevents'.DS.'files'.DS.$row->FileLocation);
			
			if(!empty($cid))
			{
			if(!$row->delete($cid)) {
				$msg = JText::_( 'RSE_FILE_DELETE_ERROR' );
			} else {
				$msg = JText::_( 'RSE_FILE_DELETE' );
			}
			
			} else
			{
				$msg = JText::_( 'RSE_FILE_DELETE_ERROR1' );
			}
			$this->setRedirect(html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=addeditevent&cid='.$idEvent.':'.JFilterOutput:: stringURLSafe($res).'&page=2'.$itemId)), $msg );
		} else $this->setRedirect(html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=show&cid='.$idEvent.':'.JFilterOutput:: stringURLSafe($res).$itemId)), JText::_('RSE_ACCESS_DENIED'));
	}
	
}