<?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');
jimport('joomla.filesystem.file');
jimport('joomla.html.pane');

class rsfilesViewFiles extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
	
		JSubMenuHelper::addEntry(JText::_('RSFL_FILES_MENU'), 'index.php?option=com_rsfiles&task=files',true); 
		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'); 
		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'); 		
	
		$db  = JFactory::getDBO();
		$doc = JFactory::getDocument();
		$rsfilesConfig = rsfilesHelper::getConfig();
		$this->assignRef('rsfilesConfig',$rsfilesConfig);
		$task=JRequest::getVar('task','','request');
		switch($task)
		{
			default:
			case 'files':
			case 'showfiles':
			{				
				JHTML::_('behavior.tooltip');
				
				$folder = JRequest::getVar('folder');
				$template = $this->get('Template');
				
				JToolBarHelper::title(JText::_('RSFL_FILES'),'rsfl-files');
				JToolBarHelper::editListX('editfile');
				JToolBarHelper::deleteList();
				JToolBarHelper::publishList();
				JToolBarHelper::unpublishList();
				JToolBarHelper::custom('purge','purge','purge',JText::_('RSFL_PURGE_FILES'),false);
				JToolBarHelper::custom('enablestatistics','statistics32','statistics32',JText::_('RSFL_ENABLE_STATISTICS'));
				JToolBarHelper::custom('rsfiles','rsfiles-32','rsfiles-32',JText::_('RSFL_PRODUCT'),false);
				
				
				if(!empty($folder))
				if (strpos($folder, $rsfilesConfig['download_folder']) !== 0) $mainframe->redirect('index.php?option=com_rsfiles&task=files',JText::_('RSFL_OUTSIDE_OF_ROOT'),'error');
				
				$files = $this->get('files');
				$this->assignRef('files', $files);
				
				$folders = $this->get('folders');
				$this->assignRef('folders', $folders);
				
				
				$count = count($files) + count ($folders);
				$this->assignRef('elements', $this->get('elements'));
				
				$this->assignRef('current', $this->get('current'));
				$this->assignRef('previous', $this->get('previous'));
				$this->assignRef('count', $count);
				
				$link = 'index.php?option=com_rsfiles&task=files';
				$this->assignRef('link', $link);
				$this->assignRef('template', $template);
				
			} break;
			
			case 'editfile':
			{
			
				$params = array();
				$params['size'] = array('x' => '700','y' => '180');
				JHTML::_('behavior.modal' , 'a.modal', $params);
				
				$cid=JRequest::getVar('cid',array(0),'request','array');
				if(is_array($cid)) $cid = $cid[0];
				JToolBarHelper::title(JText::_('RSFL_EDIT_FILE_TITLE','generic.png'));
				JToolBarHelper::save();
				JToolBarHelper::apply();
				JToolBarHelper::cancel();
				
				$data = $this->get('File');
				$licenses = $this->get('Licenses');
				$mirrors = $this->get('Mirrors');
				$screenshots = $this->get('Screenshots');
				$tabs =& JPane::getInstance('Tabs',array(),true);
				
				
				$lics= array();
				$lics[] = JHTML::_('select.option', 0, JText::_('RSFL_LICENSE_SELECT'));
				foreach ($licenses as $license)
					$lics[] = JHTML::_('select.option', $license->IdLicense, $license->LicenseName);
					
				$lists['licenses'] = JHTML::_('select.genericlist', $lics, 'IdLicense', '', 'value', 'text', $data->IdLicense);
				
				$download_method = array();
				
				$download_method[] = JHTML::_('select.option', 0, JText::_('RSFL_DOWNLOAD_NORMAL'));
				$download_method[] = JHTML::_('select.option', 1, JText::_('RSFL_DOWNLOAD_EMAIL'));
				$lists['downloads'] = JHTML::_('select.genericlist', $download_method, 'DownloadMethod', '', 'value', 'text', $data->DownloadMethod);
				
				$groups = array();
				$groups[] = JHTML::_('select.option', 0, JText::_('RSFL_EVERYBODY'),'IdGroup','GroupName');
				$db->setQuery("SELECT IdGroup,GroupName FROM #__rsfiles_groups ORDER BY IdGroup");
				$groups = array_merge($groups ,$db->loadObjectList());
				
				$selectedCanPerforMaintenance =array();
				$selectedCanDownload = array();
				$selectedCanView = array();
				
				foreach(explode(',',$data->CanPerforMaintenance) as $opt1)
					$selectedCanPerforMaintenance[] = $opt1;
				foreach(explode(',',$data->CanDownload) as $opt2)
					$selectedCanDownload[] = $opt2;
				foreach(explode(',',$data->CanView) as $opt3)
					$selectedCanView[] = $opt3;
				
				if($data->CanPerforMaintenance == '') $selectedCanPerforMaintenance = '';
				if($data->CanDownload == '') $selectedCanDownload = '';
				if($data->CanView == '') $selectedCanView = '';
				
				$lists['canedit'] = JHTML::_('select.genericlist', $groups, 'CanPerforMaintenance[]', 'multiple="multiple" size="7" style="vertical-align: middle;" ', 'IdGroup', 'GroupName', $selectedCanPerforMaintenance);
				$lists['candownload'] = JHTML::_('select.genericlist', $groups, 'CanDownload[]', 'multiple="multiple" size="7" style="vertical-align: middle;"', 'IdGroup', 'GroupName', $selectedCanDownload);
				$lists['canview'] = JHTML::_('select.genericlist', $groups, 'CanView[]', 'multiple="multiple" size="7" style="vertical-align: middle;"', 'IdGroup', 'GroupName', $selectedCanView);
				
				$this->assignRef('data',$data);
				$this->assignRef('tabs',$tabs);
				$this->assignRef('mirrors',$mirrors);
				$this->assignRef('screenshots',$screenshots);
				$this->assignRef('lists',$lists);
				$this->assignRef('editor', JFactory::getEditor());
			
			} break;
			
			
			case 'editmirror':
			{
				$cid = JRequest::getVar('cid');
				$mirror = $this->get('Mirror');
				if(empty($cid)) $mirror->IdFile = JRequest::getVar('IdFile');
				
				$this->assignRef('mirror',$mirror);
			} break;
		}
		
		
		parent::display($tpl);
	}
}