<?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 rsfilesModelUsers extends JModel
{
	var $_query;
	var $_data;
	var $_total=null;
	var $_pagination=null;

	function _buildQuery()
	{
		global $mainframe;
		$db = JFactory::getDBO();
		
		$sortColumn	= $mainframe->getUserStateFromRequest( "com_rsevents.users.filter_order",		'filter_order',		'a.name',	'cmd' );
		$sortOrder	= $mainframe->getUserStateFromRequest( "com_rsevents.users.filter_order_Dir",	'filter_order_Dir',	'',			'word' );
		$group		= $mainframe->getUserStateFromRequest( "com_rsevents.users.filter_type",		'filter_type', 		0,			'string' );
		$search		= $mainframe->getUserStateFromRequest( "com_rsevents.users.search",			'search', 			'',			'string' );
		$search		= JString::strtolower( $search );
		
		$mainframe->setUserState('com_rsevents.users.filter_order',$sortColumn);
		$mainframe->setUserState('com_rsevents.users.filter_order_Dir',$sortOrder);
		$mainframe->setUserState('com_rsevents.users.filter_type',$group);
		$mainframe->setUserState('com_rsevents.users.search',$search);

		$where = array();
		if (isset( $search ) && $search!= '')
		{
			$searchEscaped = $db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			$where[] = 'a.username LIKE '.$searchEscaped.' OR a.email LIKE '.$searchEscaped.' OR a.name LIKE '.$searchEscaped;
		}
		if ( $group )
		{
			if ( $group == 'Public Frontend' )
			{
				$where[] = ' a.usertype = \'Registered\' OR a.usertype = \'Author\' OR a.usertype = \'Editor\' OR a.usertype = \'Publisher\' ';
			}
			else if ( $group == 'Public Backend' )
			{
				$where[] = 'a.usertype = \'Manager\' OR a.usertype = \'Administrator\' OR a.usertype = \'Super Administrator\' ';
			}
			else
			{
				$where[] = 'a.usertype = LOWER( '.$db->Quote($group).' ) ';
			}
		}
		
		$where = ( count( $where ) ? ' WHERE (' . implode( ') AND (', $where ) . ')' : '' );
		$orderby = ' ORDER BY '. $sortColumn .' '. $sortOrder;
		
		$this->_query = 'SELECT a.*, g.name AS groupname'
			. ' FROM #__users AS a'
			. ' INNER JOIN #__core_acl_aro AS aro ON aro.value = a.id'
			. ' INNER JOIN #__core_acl_groups_aro_map AS gm ON gm.aro_id = aro.id'
			. ' INNER JOIN #__core_acl_aro_groups AS g ON g.id = gm.group_id'
			. $where
			. ' GROUP BY a.id'
			. $orderby
		;
		
		//echo $this->_query;
	}
	
	function __construct()
	{	
		parent::__construct();
		$this->_buildQuery();
		global $mainframe;

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

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

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

		//echo $this->_db->_errorMsg;
	}
	
	function getData()
	{
		if (empty($this->_data))
		{
			$this->_data=$this->_getList($this->_query,$this->getState('com_rsfiles.users.limitstart'), $this->getState('com_rsfiles.users.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('com_rsfiles.users.limitstart'), $this->getState('com_rsfiles.users.limit'));
		}
		return $this->_pagination;
	}
}