<?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();
 
jimport( 'joomla.application.component.model' );

class rsfilesModelFiles extends JModel
{
	var $_query;
	var $_data;
	var $_total=null;
	var $_pagination=null;
	var $_folder=null;
	
	function _buildQuery()
	{
		global $mainframe;
		$db =& JFactory::getDBO();
		
		$sortColumn=$mainframe->getUserStateFromRequest('com_rsfiles.files.sortColumn','filter_order','ordering');
		$sortOrder=$mainframe->getUserStateFromRequest('com_rsfiles.files.sortOrder','filter_order_Dir','asc');
		
		$mainframe->setUserState('com_rsfiles.files.sortColumn',$sortColumn);
		$mainframe->setUserState('com_rsfiles.files.sortOrder',$sortOrder);

		
		$this->_query="SELECT IdFile , FileName , RealFileName , Type , ordering, published FROM #__rsfiles_files ORDER BY $sortColumn $sortOrder";
		//echo $this->_query;
	}
	
	function __construct()
	{
		global $mainframe;
		$rsfilesConfig = rsfilesHelper::getConfig();
		parent::__construct();		
		if (is_dir(JRequest::getVar('folder')))
		{
			$this->_folder = JRequest::getVar('folder');
			if (substr($this->_folder, -1) == DS)
				$this->_folder = substr($this->_folder, 0, -1);
		}
		else
			$this->_folder = $rsfilesConfig['download_folder'];
	}
	
	function getFolders()
	{
		$db = JFactory::getDBO();
		$folders = array();
		
		$all_folders = JFolder::folders($this->_folder);
		foreach ($all_folders as $folder)
		{
			if (JFile::stripExt($folder) == '') continue;
			
			$db->setQuery("SELECT published FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped($this->_folder.DS.$folder)."' ");
			$publish = $db->loadResult();
			$publish = isset($publish) ? $publish : 1;
			
			$element = new stdClass();
			$element->name = $folder;
			$element->fullpath = urlencode($this->_folder.DS.$folder);
			$element->published = $publish;
			$folders[] = $element;
		}
		
		return $folders;
	}
	
	function getFiles()
	{
		$db =& JFactory::getDBO();
		$files = array();
		
		$all_files = JFolder::files($this->_folder);
		foreach ($all_files as $file)
		{
			if (JFile::stripExt($file) == '') continue;
			
			$reports = 0;
			$db->setQuery("SELECT IdFile,published FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped($this->_folder.DS.$file)."'");
			$details = $db->loadObject();
			if (!empty($details) && $details->IdFile > 0)
			{
				$db->setQuery("SELECT COUNT(IdReport) FROM #__rsfiles_reports WHERE IdFile = '".$details->IdFile."' ");
				$reports = $db->loadResult();
			}

			if ($reports <= 0) $reports = JText::_('RSFL_NO_REPORT');
			if ($reports == 1) $reports = JText::_('RSFL_ONE_REPORT');
			if ($reports > 1) $reports = JText::sprintf('RSFL_MANY_REPORT',$reports);
			
			$publish = isset($details->published) ? $details->published : 1;
			
			$element = new stdClass();
			$element->name = $file;
			$element->fullpath = urlencode($this->_folder.DS.$file);
			$element->reports = $reports;
			$element->published = $publish;
			$files[] = $element;
		}
		
		return $files;
	}
	
	function getElements()
	{		
		$elements = explode(DS, $this->_folder);
		$navigation_path = '';
		
		if(!empty($elements))
			foreach($elements as $i=>$element)
			{
				$navigation_path .= $element;
				$newelement = new stdClass();
				$newelement->name = $element;
				$newelement->fullpath = $navigation_path;
				$elements[$i] = $newelement;
				$navigation_path .= DS;
			}
		
		return $elements;
	}
	
	function getCurrent()
	{
		return $this->_folder;
	}
	
	function getPrevious()
	{
		$elements = explode(DS, $this->_folder);
		if (count($elements) > 1)
			array_pop($elements);
		return urlencode(implode(DS, $elements));
	}
	
	function getFile()
	{
		$db = JFactory::getDBO();
		$cid=JRequest::getVar('cid',array(0),'request','array');
		if(is_array($cid)) $cid = urldecode($cid[0]);
		
		
		$db->setQuery("SELECT * FROM #__rsfiles_files WHERE FilePath= '".$db->getEscaped($cid)."' ");
		$details = $db->loadObject();
		
		if(!empty($details)) return $details;
		else 
		{
			$row =& JTable::getInstance('Rsfiles_Files','Table');
			$row->load(0);
			$row->FilePath = $cid;
			$row->DateAdded = time();
			return $row;
		}
	}
	
	function getLicenses()
	{
		return $this->_getList("SELECT IdLicense , LicenseName FROM #__rsfiles_licenses ORDER BY LicenseName DESC");
	}
	
	function getMirrors()
	{
		$db = JFactory::getDBO();
		$cid = JRequest::getVar('cid');
		if(is_array($cid)) $cid = $cid[0];
		
		$db->setQuery("SELECT IdFile FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped($cid)."'");
		$IdFile = $db->loadResult();
		
		if($IdFile)
		{
			$db->setQuery("SELECT * FROM #__rsfiles_mirrors WHERE IdFile = ".$IdFile);
			return $db->loadObjectList();
		}
		else return array();
		
		
	}
	
	function getMirror()
	{
		$cid = JRequest::getVar('cid');
		$row =& JTable::getInstance('Rsfiles_Mirrors','Table');
		$row->load($cid);
		
		return $row;
	}
	
	function getScreenshots()
	{
		$db = JFactory::getDBO();
		$cid=JRequest::getVar('cid',array(0),'request','array');
		if(is_array($cid)) $cid = $cid[0];
		
		$db->setQuery("SELECT IdFile FROM #__rsfiles_files WHERE FilePath= '".$db->getEscaped($cid)."' ");
		$IdFile = $db->loadResult();
		
		$db->setQuery("SELECT * FROM #__rsfiles_screenshots WHERE IdFile = ".$IdFile);
		
		return $db->loadObjectList();
	}
	
	function getTemplate()
	{
		$db = JFactory::getDBO();
		$db->setQuery("SELECT ThemeName FROM #__rsfiles_themes WHERE `default` = 1");
		
		return $db->loadResult();
	}
	
	
	
}