<?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 rsfilesModelStatistics extends JModel
{
	var $_query;
	var $_squery;
	var $_data;
	var $_sdata;
	var $_total=null;
	var $_stotal=null;
	var $_pagination=null;
	var $_spagination=null;
	var $_componentList=null;
	function _buildQuery()
	{
	
		global $mainframe;
		$db =& JFactory::getDBO();
		
		$jFilter = JRequest::getVar('jFilter');
		$cid= JRequest::getVar('cid');
		if(is_array($cid)) $cid=$cid[0];
		
		$this->_squery="SELECT s.* , u.username FROM #__rsfiles_statistics s LEFT JOIN #__users u ON u.id=s.UserId WHERE IdFile = ".$cid." ORDER BY Date DESC";
		$this->_query="SELECT IdFile ,FilePath , hits FROM #__rsfiles_files WHERE FileStatistics = 1 AND FilePath LIKE '%".$db->getEscaped($jFilter)."%' ORDER BY FilePath DESC";
		
		//echo $this->_query;
	}
	function __construct()
	{	
		parent::__construct();
		$this->_buildQuery();
		global $mainframe, $option;

		// Get pagination request variables
		$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int');

		// In case limit has been changed, adjust it
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
		

		//echo $this->_db->_errorMsg;
	}
	function getData()
	{
		if (empty($this->_data))
		{
			$this->_data=$this->_getList($this->_query,$this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_data;
	}
	function getTotal()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$this->_total = $this->_getListCount($this->_query);	
		}
		return $this->_total;
	}
	
	function getPagination()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_pagination;
	}
	
	function getStatistic()
	{
		if (empty($this->_sdata))
		{
			$this->_sdata=$this->_getList($this->_squery,$this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_sdata;
	}
	
	function getsTotal()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_stotal))
		{
			$this->_stotal = $this->_getListCount($this->_squery);	
		}
		return $this->_stotal;
	}
 
	function getsPagination()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_spagination))
		{
			jimport('joomla.html.pagination');
			$this->_spagination = new JPagination($this->getsTotal(), $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_spagination;
	}
}