<?php
/**
* @version 1.0.0
* @package RSFiles! 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 rsfilesViewGroups extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
		$db = JFactory::getDBO();
	
		JSubMenuHelper::addEntry(JText::_('RSFL_FILES_MENU'), 'index.php?option=com_rsfiles&task=files'); 
		JSubMenuHelper::addEntry(JText::_('RSFL_LICENSE_MENU'), 'index.php?option=com_rsfiles&task=licenses'); 
		JSubMenuHelper::addEntry(JText::_('RSFL_GROUPS_MENU'), 'index.php?option=com_rsfiles&task=groups',true); 
		JSubMenuHelper::addEntry(JText::_('RSFL_STATISTICS_MENU'), 'index.php?option=com_rsfiles&task=statistics');
		JSubMenuHelper::addEntry(JText::_('RSFL_THEMES_MENU'), 'index.php?option=com_rsfiles&task=themes');
		JSubMenuHelper::addEntry(JText::_('RSFL_SETTINGS_MENU'), 'index.php?option=com_rsfiles&task=settings'); 
		JSubMenuHelper::addEntry(JText::_('RSFL_UPDATES_MENU'), 'index.php?option=com_rsfiles&task=updates'); 		
	
		$rsfilesConfig = rsfilesHelper::getConfig();
		$this->assignRef('rsfilesConfig',$rsfilesConfig);
		$task=JRequest::getVar('task','','request');
		switch($task)
		{
			default:
			case 'groups':
			{
				JToolBarHelper::title(JText::_('RSFL_GROUPS'),'rsfl-groups');
				JToolBarHelper::addNewX('editgroup');
				JToolBarHelper::editListX('editgroup');
				JToolBarHelper::deleteList();
				JToolBarHelper::custom('rsfiles','rsfiles-32','rsfiles-32',JText::_('RSFL_PRODUCT'),false);
				
				$data = $this->get('Data');
				$pagination=$this->get('pagination');
				$jFilter = JRequest::getVar('jFilter');
				
				$this->assignRef('data',$data);
				$this->assignRef('pagination',$pagination);
				$this->assignRef('jFilter',$jFilter);
				
			} break;
			
			case 'editgroup':
			{
				$cid=JRequest::getVar('cid',array(0),'request','array');
				if(is_array($cid)) $cid=intval($cid[0]);
				if($cid==0)
					JToolBarHelper::title(JText::_('RSFL_NEW_GROUP','generic.png'));
				else 
					JToolBarHelper::title(JText::_('RSFL_EDIT_GROUP','generic.png'));
				JToolBarHelper::save();
				JToolBarHelper::apply();
				JToolBarHelper::cancel();
				
				$data = $this->get('Group');				
				
				//Get the group acls
				$GroupAcls=$this->get('GroupAcls');	
				$GroupAcls = explode(',',$GroupAcls);				
				
				//Initialize the access list dropdown
				$AccessList = array();
				
				if(!empty($GroupAcls))
				foreach($GroupAcls as $GroupAcl)
				{	
					$AccessList[] = $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 ) );
				
				//Create the access control list
				$lists['acls']	= JHTML::_('select.genericlist', $access, 'GroupAcl[]', 'class="inputbox" size="8" multiple="multiple"', 'value', 'text', $AccessList );
				
				$db->setQuery("SELECT IdUsers FROM #__rsfiles_group_details WHERE IdGroup =".$data->IdGroup);
				$userids = $db->loadResult();
				
				$groupUsers = array();
				
				if(!empty($userids))
				{
					$userids = explode(',',$userids);
					
					foreach($userids as $userid)
					{
						$newClass = new stdClass();
						$newClass->id = $userid;
						$db->setQuery("SELECT CONCAT_WS(' - ',name,username) as detail FROM #__users WHERE id =".$userid);
						$newClass->detail = $db->loadResult();
						$groupUsers[] = $newClass;
					}
				}
				
				$lists['usersadd']	= JHTML::_('select.genericlist', $groupUsers, 'addusers[]', 'class="inputbox" size="20" multiple="multiple"', 'id', 'detail' );
				
				$this->assignRef('data',$data);
				$this->assignRef('lists'	, $lists);
				
				
			
			} break;
		}
		
		
		parent::display($tpl);
	}
}