<?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' );

/**
 * Groups Controller
 *
 */
class rseventsControllerGroups extends rseventsController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	 
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
		$this->registerTask( 'editGroups'  , 	'edit' );
		$this->registerTask( 'apply'  , 	'save' );
	}

	function edit()
	{
		JRequest::setVar( 'view', 'groups' );
		JRequest::setVar( 'layout', 'edit'  );
		parent::display();
	}
	
	function remove()
	{
		//initialize the table 
		$db = & JFactory::getDBO();
		$model = $this->getModel('groups');
		$row =& JTable::getInstance('Rsevents_Groups','Table');
		$cids=JRequest::getVar('cid',array(0),'post','array');
		
		//deletes all the groups that have been selected
		
		if(!empty($cids))
		{
		foreach($cids as $cid) 
		{
		if(!$row->delete($cid)) {
			$msg = JText::_('RSE_GROUP_DELETE_ERROR' );
		} else {
			$db->setQuery("DELETE FROM #__rsevents_groups_acl WHERE IdGroup='$cid'");
			$db->query();
			$db->setQuery("DELETE FROM #__rsevents_group_permissions WHERE IdGroup='$cid'");
			$db->query();
			$msg = JText::_('RSE_GROUP_DELETE' );
		}
		}
		} else
		{
			$msg = JText::_('RSE_GROUP_DELETE_ERROR1' );
		}
		$this->setRedirect( 'index.php?option=com_rsevents&task=listgroups', $msg );
	}
	
	/**
	 * save a record 
	 * @return void
	 */
	function save()
	{
		global $mainframe;
		$db = & JFactory::getDBO();
		$model = $this->getModel('groups');
		$post=JRequest::get('post');
		$cid=JRequest::getVar('cid');
		
		$msg='There are no joomla groups to assign';
		if(!isset($_POST['GroupAcl'])) $mainframe->redirect('index.php?option=com_rsevents&task=listgroups', $msg);
		
		$row= & JTable::getInstance('Rsevents_Groups','Table');
		$acls = $post['GroupAcl'];
		
		//bind the table
		if(!$row->bind($post)) {
			return JError::raiseWarning(500, $row->getError());
		}		
		
		// save the group
		
		if ($row->store()) 
		{
			
			//first let's delete the old group acls
			$db->setQuery("DELETE FROM #__rsevents_groups_acl WHERE IdGroup = " . $row->IdGroup);
			$db->query();
			
			//insert the group acls into the #__rsevents_groups_acl table
			
			foreach($acls as $acl)
			{
				$db->setQuery("INSERT INTO #__rsevents_groups_acl SET IdGroup = ".$row->IdGroup." , GroupAcl = ".$acl);
				$db->query();
			}
			
			//see if there are group permissions and insert them into the table
			
			if(isset($post['gp']))
			{
				
				$rowGp = new stdClass();
				$db->setQuery("SELECT IdGroupPermission, PermissionName FROM #__rsevents_group_permissions WHERE IdGroup=$cid");
				$arrayPermissions = $db->loadObjectList();			
				
				foreach($post['gp'] as $key =>$value)
				{
					$row->IdGroupPermission = 0;
					foreach ($arrayPermissions as $permissionRow)
						if($permissionRow->PermissionName == $key)
							$rowGp->IdGroupPermission=$permissionRow->IdGroupPermission;
					$rowGp->IdGroup=$row->IdGroup;
					$rowGp->PermissionName=$key;
					$rowGp->PermissionValue=$value;
					if($rowGp->IdGroupPermission==0)
					$db->setQuery("INSERT INTO #__rsevents_group_permissions (IdGroup,PermissionName,PermissionValue) VALUES ('$rowGp->IdGroup','$rowGp->PermissionName','$rowGp->PermissionValue')");
					else
					$db->setQuery("UPDATE #__rsevents_group_permissions SET PermissionValue='$rowGp->PermissionValue' WHERE PermissionName='$rowGp->PermissionName' AND IdGroup='$rowGp->IdGroup'");
					$db->query();
					
				}
			}
			
			$msg = JText::_('RSE_GROUP_SAVE' );
			} else 
			{
			JError::raiseWarning(500, $row->getError());

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

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

				break;
			}
			$this->setRedirect($link, $msg);
	}

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