<?php
/**
 * @package BILLETS
 * @link 	http://www.dioscouri.com
 * @license GNU/GPLv2
 */
 
// no direct access
defined('_JEXEC') or die('Restricted access');

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

/**
 * Billets Model
 *
 * @package BILLETS
 */
class BilletsModelUsers extends JModel {

	var $_data;
	var $_total;
	var $_pagination;
	var $_types;
	var $_type;
	var $_status;	

	/**
	 * constructor
	 * @return array Array of objects containing the data from the database
	 */	
	function __construct() {
		parent::__construct();
		global $mainframe;
		
		// Get the pagination request variables
		$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart	= $mainframe->getUserStateFromRequest( 'billets.limitstart', 'limitstart', 0, 'int' );

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

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);		
		$this->setState( 'order', strval( htmlspecialchars( $mainframe->getUserStateFromRequest( "order", 'order', 'id' ) ) ) );
		$this->setState( 'order_dir', strval( htmlspecialchars( $mainframe->getUserStateFromRequest( "order_dir", 'order_dir', 'asc' ) ) ) );

	}
	
	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildQuery() {
		global $mainframe;
		
		$search				= $mainframe->getUserStateFromRequest( 'search', 'search', '',	'string' );
		$search				= JString::strtolower($search);
	
		$where = array();
			
		if (isset($search)) {
			$where[] = "LOWER(id) LIKE '%" . $this->_db->getEscaped( trim( strtolower( $search ) ) ) . "%'";
			$where[] = "LOWER(name) LIKE '%" . $this->_db->getEscaped( trim( strtolower( $search ) ) ) . "%'";
			$where[] = "LOWER(username) LIKE '%" . $this->_db->getEscaped( trim( strtolower( $search ) ) ) . "%'";
			$where[] = "LOWER(email) LIKE '%" . $this->_db->getEscaped( trim( strtolower( $search ) ) ) . "%'";
		}

		$order_query = "";
		$order = $this->getState( 'order' );
		$order_dir = $this->getState( 'order_dir' );
		switch ($order) {
			case "id":
				$order_query = " ORDER BY u.id ".strtoupper( $order_dir )." ";
			  break;
			case "name":
				$order_query = " ORDER BY u.name ".strtoupper( $order_dir )." ";
			  break;
			case "username":
				$order_query = " ORDER BY u.username ".strtoupper( $order_dir )." ";
			  break;
			case "email":
				$order_query = " ORDER BY u.email ".strtoupper( $order_dir )." ";
			  break;
			default:
				$order_query = " ORDER BY u.id ASC ";
			  break;
		}

	
		// get the total number of records
		$query = "SELECT u.* FROM #__users as u "
		." WHERE 1 "
		. (count( $where ) ? "\n HAVING " . implode( ' OR ', $where ) : "")
		. $order_query 
		;
		
		return $query;
	}
	
	/**
	 * Retrieves the messages
	 * @return array Array of objects containing the data from the database
	 */
	function getData() {
		// load the data if it doesn't already exist
		if (empty( $this->_data )) {
			$query = $this->_buildQuery();
			$this->_data = $this->_getList( $query, $this->getState('limitstart'), $this->getState('limit') );
		}
		return $this->_data;
	}


	/**
	 * Paginates the data
	 * @return array Array of objects containing the data from the database
	 */
	function getPagination() {
		if (empty($this->_pagination)) {
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}
		return $this->_pagination;
	}

	/**
	 * Retrieves the count 
	 * @return array Array of objects containing the data from the database
	 */
	function getTotal() {
		if (empty($this->_total)) {
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}
	 
	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store() {
		global $mainframe;
		$database = &JFactory::getDBO();

		$row = new billetsTicket ( $this->_db );
		$row->load( $this->_id );

    	$data = JRequest::get( 'post' );

    	// Bind the form fields to the hello table
    	if (!$row->bind($data)) {
       	 	$this->setError($this->_db->getErrorMsg());
        	return false;
    	}
		
		// Make sure the hello record is valid
	    if (!$row->check()) {
	        $this->setError($this->_db->getErrorMsg());
	        return false;
	    }
		
   		// Store the entry to the database
    	if (!$row->store()) {
        	$this->setError($this->_db->getErrorMsg());
        	return false;
    	}
		return true;
	}
	
	// Publish or unpublish
	function followup() {
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
	    // $row =& $this->getTable();

		// grab info
		$info = new billetsTicket ($this->_db);
		$info->load( (int)$cids[0] );
	
		if (!$info->id) {
			$this->setError( _BILLETS_TICKET." "._BILLETS_UPDATEFAILED );
			return false;
		}
	
		// change published
		if ($info->followup == 1) { $info->followup = 0; } else { $info->followup = 1; }
		

		if (!$info->store()) {
			$this->setError( $info->getError() );
			return false;
		}
	
	    return true;
	}
	
		
}
// ************************************************************************
