<?php
/**
 * @package Freestyle Joomla
 * @author Freestyle Joomla
 * @copyright (C) 2013 Freestyle Joomla
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
**/
defined('_JEXEC') or die;

jimport( 'joomla.application.component.view');
require_once (JPATH_SITE.DS.'components'.DS.'com_fss'.DS.'helper'.DS.'pagination.php');

class FssViewAdmin_Support_Users extends FssViewAdmin_Support
{
	function display($tpl = NULL)
	{
		parent::init();	
		
		$qry = "SELECT * FROM #__users ";
		$where = array();
		
		$limitstart = JRequest::getInt('limitstart',0);
		$mainframe = JFactory::getApplication();
		$limit = $mainframe->getUserStateFromRequest('users.limit', 'limit', 10, 'int');
		$search = JRequest::getString('search','');
		
		$this->lists = array();
		$this->lists['order_Dir'] = JRequest::getVar('filter_order_Dir');
		$this->lists['order'] = JRequest::getVar('filter_order');
		
		$db	= JFactory::getDBO();
		
		if ($search != "")
		{
			$where[] = "(username LIKE '%".FSSJ3Helper::getEscaped($db, $search)."%' OR name LIKE '%".FSSJ3Helper::getEscaped($db, $search)."%' OR email LIKE '%".FSSJ3Helper::getEscaped($db, $search)."%')";
		}
				
		if (count($where) > 0)
		{
			$qry .= " WHERE " . implode(" AND ", $where);	
		}

		
		$order = JRequest::getVar('filter_order');
		$dir = JRequest::getVar('filter_order_Dir', 'asc');
		if ($order == "username" || $order == "name" || $order == "email")
		{
			// Sort ordering
			$qry .= " ORDER BY $order $dir ";
		} else {
			$qry .= " ORDER BY name ";
		}
		
		//echo $qry . "<br>";
		// get max items
		
		$db->setQuery( $qry );
		$db->query();
		$maxitems = $db->getNumRows();
			
		
		// select picked items
		$db->setQuery( $qry, $limitstart, $limit );
		$this->users = $db->loadObjectList();

		//print_p(reset($this->users));
		
		// build pagination
		$this->pagination = new JPaginationEx($maxitems, $limitstart, $limit );
		$this->search = $search;
			
		$this->_display();
	}
}