<?php
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.helper');
jimport( 'joomla.application.component.model');

class KipModelUsers extends JModel
{
	var $_list = null;

	var $_page = null;

	function getList()
	{
		global $mainframe;

		if (!empty($this->_list)) {
			return $this->_list;
		}

		// Initialize variables
		$db		=& $this->getDBO();
		$filter	= null;

		// Get some variables from the request
		//$sectionid			= JRequest::getVar( 'sectionid', -1, '', 'int' );
		//$redirect			= $sectionid;
		$option				= JRequest::getCmd( 'option' );
		$filter_order		= $mainframe->getUserStateFromRequest('kipelement.filter_order',		'filter_order',		'',	'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest('kipelement.filter_order_Dir',	'filter_order_Dir',	'',	'word');
		$filter_type		= $mainframe->getUserStateFromRequest('kipelement.filter_type',				'filter_type',			0,	'string');
		$filter_logged		= $mainframe->getUserStateFromRequest('kipelement.filter_logged',		'filter_logged',	0,	'int');
		//$filter_sectionid	= $mainframe->getUserStateFromRequest('articleelement.filter_sectionid',	'filter_sectionid',	-1,	'int');
		$limit				= $mainframe->getUserStateFromRequest('global.list.limit',					'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart			= $mainframe->getUserStateFromRequest('kipelement.limitstart',			'limitstart',		0,	'int');
		$search				= $mainframe->getUserStateFromRequest('kipelement.search',				'search',			'',	'string');
		$search				= JString::strtolower($search);

		//$where[] = "c.state >= 0";
		//$where[] = "c.state != -2";

		if (!$filter_order) {
			$filter_order = 'name';
		}
		$order = ' ORDER BY '. $filter_order .' '. $filter_order_Dir;
		$all = 1;

		if ( $filter_type )
		{
			if ( $filter_type == 'Public Frontend' )
			{
				$where[] = ' c.usertype = \'Registered\' OR c.usertype = \'Author\' OR c.usertype = \'Editor\' OR c.usertype = \'Publisher\' ';
			}
			else if ( $filter_type == 'Public Backend' )
			{
				$where[] = 'c.usertype = \'Manager\' OR c.usertype = \'Administrator\' OR c.usertype = \'Super Administrator\' ';
			}
			else
			{
				$where[] = 'c.usertype = LOWER( '.$db->Quote($filter_type).' ) ';
			}
		}
		if ( $filter_logged == 1 )
		{
			$where[] = 's.userid = c.id';
		}
		else if ($filter_logged == 2)
		{
			$where[] = 's.userid IS NULL';
		}

		$filter = '';
		if ($filter_logged == 1 || $filter_logged == 2)
		{
			$filter = ' INNER JOIN #__session AS s ON s.userid = c.id';
		}


		// Only published articles
		//$where[] = 'c.state = 1';

		// Keyword filter
		if ($search) {
			$where[] = '(LOWER( c.username ) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false )
			.' OR LOWER( c.name ) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false) .')';
		}

		// Build the where clause of the content record query
		$where = (count($where) ? ' WHERE '.implode(' AND ', $where) : '');

		// Get the total number of records
		$query = 'SELECT COUNT(*)' .
				' FROM #__users AS c' .
				$filter.
				$where;
		$db->setQuery($query);
		//echo $db->getQuery();exit;
		$total = $db->loadResult();

		// Create the pagination object
		jimport('joomla.html.pagination');
		$this->_page = new JPagination($total, $limitstart, $limit);

		// Get the articles
		$query = 'SELECT c.*, g.name AS groupname'
				. ' FROM #__users AS c'
				. ' INNER JOIN #__core_acl_aro AS aro ON aro.value = c.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'
				. $filter
				. $where
				. ' GROUP BY c.id'
				. $order;
		$db->setQuery($query, $this->_page->limitstart, $this->_page->limit);
		$this->_list = $db->loadObjectList();

		$template = 'SELECT COUNT(s.userid)'
			. ' FROM #__session AS s'
			. ' WHERE s.userid = %d'
		;
		for ($i = 0; $i < $total; $i++)
		{
			$row = &$this->_list[$i];
			$query = sprintf( $template, intval( $row->id ) );
			$db->setQuery( $query );
			$row->loggedin = $db->loadResult();
		}


		// If there is a db query error, throw a HTTP 500 and exit
		if ($db->getErrorNum()) {
			JError::raiseError( 500, $db->stderr() );
			return false;
		}

		return $this->_list;
	}

	function getPagination()
	{
		if (is_null($this->_list) || is_null($this->_page)) {
			$this->getList();
		}
		return $this->_page;
	}
	
}
?>
