<?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()
	{
		$model = $this->getModel('files');
		$post=JRequest::get('post');
		 
		$cid = JRequest::getVar('cid');
		
		$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;
		
		//print_r($post);die();
		
		$row= & JTable::getInstance('Rsevents_Files','Table');
		
		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 = 'index.php?option=com_rsevents&task=editevent&cid='.$cid;
		$this->setRedirect($link, $msg);
	}

	/**
	 * remove record(s)
	 * @return void
	 */
	function deleteFile()
	{
		$model = $this->getModel('files');
		
		$row =& JTable::getInstance('Rsevents_Files','Table');
		
		$cid=JRequest::getVar('cid');
		$idEvent = JRequest::getVar('IdEvent');
		
		$row->load($cid);
		
		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( 'index.php?option=com_rsevents&task=editevent&cid='.$idEvent, $msg );
	}
	
}