<?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
*/

defined( '_JEXEC' ) or die( 'Restricted access' ); 
jimport( 'joomla.application.component.view');

class rseventsViewGroups extends JView
{
	function display($tpl = null)
	{
		$task=JRequest::getVar('task','','request');
		switch($task)
		{
			
			case 'listgroups':
			{
				JToolBarHelper::title(JText::_('RSE_GROUP_LIST'),'generic.png');
				JToolBarHelper::addNewX('editgroups');
				JToolBarHelper::editListX('editgroups');
				JToolBarHelper::deleteList();
				JToolBarHelper::custom('main','preview.png','preview_f2.png',JText::_('RSE_RS_PRODUCT'),false);
				
				
				$filter			=JRequest::getVar('rs_filter');
				$sortColumn		=JRequest::getVar('filter_order','#__rsevents_groups.IdGroup');
				$sortOrder	=JRequest::getVar('filter_order_Dir','asc');
				$listGroup=$this->get('data');
				$pagination=$this->get('pagination');
				$GroupList=$this->get('listGroups');
				
				
				
				$this->assignRef('filter',$filter);
				$this->assignRef('sortColumn',$sortColumn);
				$this->assignRef('sortOrder',$sortOrder);
				$this->assignRef('listGroups',$GroupsList);
				$this->assignRef('data',$listGroup);
				$this->assignRef('pagination',$pagination);
				
			} break;
			
			case 'editgroups':
			{
				$db=& JFactory::getDBO();
				$cid=JRequest::getVar('cid',array(0),'request','array');
				if(is_array($cid)) $cid=intval($cid[0]);
				
				if($cid==0)
					JToolBarHelper::title(JText::_('RSE_GROUP_NEW','generic.png'));
				else 
					JToolBarHelper::title(JText::_('RSE_GROUP_EDIT','generic.png'));
				JToolBarHelper::save();
				JToolBarHelper::apply();
				JToolBarHelper::cancel();
				
				//Get the group
				$Group = $this->get('Group');
				$this->assignRef('Group',$Group);
				
				//Get the group permissions
				$GroupPermissions=$this->get('GroupPermissions');
				
				if(!empty($GroupPermissions))
				foreach($GroupPermissions as $permissionRow)
				{
					$permissions[$permissionRow->PermissionName]=$permissionRow->PermissionValue;
					$this->assignRef('permissions',$permissions);
				}
				
				//Get the group acls
				$GroupAcls=$this->get('GroupAcls');		
				
				//Initialize the access list dropdown
				$AccessList = array();
				
				foreach($GroupAcls as $GroupAcl)
				{	
					$AccessList[] = $GroupAcl->GroupAcl;
				}
				
				//Load all Joomla! user groups
				$JoomlaAcls		= & JFactory::getACL();

		 	  	//Create custom group levels to include into the public group selectList
		 	  	$access   = array();
				$access = array_merge( $access, $JoomlaAcls->get_group_children_tree( null, 'USERS', false ) );
				
				//Get the used acls
				$usedAcls = array();
				$otherGroups = $this->get('UsedGroupAcls');

				foreach ($otherGroups as $otherGroup)
					$usedAcls[] = $otherGroup->GroupAcl;
				
				//Disable the used group acls
				foreach ($access as $key => $accessObject)
					if (array_search($accessObject->value,$usedAcls) !== false)
						$access[$key]->disable = true;
						
				//Create the access control list
				$accessLists['access1']	= JHTML::_('select.genericlist', $access, 'GroupAcl[]', 'class="inputbox" size="8" multiple="multiple"', 'value', 'text', $AccessList );
				
				$this->assignRef('accessLists'	, $accessLists);

					
			} break;
			
		}
		
		
		parent::display($tpl);
	}
}