<?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' );

/**
* 	BilletsModelBillets
*/
// ************************************************************************
class BilletsModelBillets extends JModel {

	var $_data;
	var $_total;
	var $_pagination;
	var $_search;
	var $_limit;
	var $_limitstart;
	var $_order;
	var $_order_dir;
	var $_status;
	var $_statuses;
	var $_pagetitle;
	
	function __construct() {
		parent::__construct();
		global $mainframe;

		$config = JFactory::getConfig();

		// 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' );
		$limitstart = intval( JArrayHelper::getValue( $_REQUEST, "limitstart", "0" ) );
		// In case limit has been changed, adjust limitstart accordingly
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

		$this->_limit 		= $limit;
		$this->_limitstart	= $limitstart;		
		$this->_order 		= strval( htmlspecialchars( JArrayHelper::getValue( $_REQUEST, "order", "lastmodified" ) ) );
		$this->_order_dir	= strval( htmlspecialchars( JArrayHelper::getValue( $_REQUEST, "order_dir", "desc" ) ) );	
		// set the status
		$this->_status = strval( htmlspecialchars( $mainframe->getUserStateFromRequest( "status", 'status', 'open' ) ) );
		
		// set the search
		$search				= strval( htmlspecialchars( $mainframe->getUserStateFromRequest( 'search', 'search', ' ' ) ) );
		$this->_search		= JString::strtolower($search);

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);	
		$this->setState('order', $this->_order);
		$this->setState('order_dir', $this->_order_dir);	
		$this->setState('status', $this->_status);
		$this->setState('search', $this->_search);	
			
	}
	
	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildQuery() {
		global $mainframe;
		$database = &JFactory::getDBO();
		$my = &JFactory::getUser();
		
		$where = array();

		$search	= $this->_search;		
		if (isset($search)) {
				$where[] = " LOWER(id) LIKE '%" . $database->getEscaped( trim( strtolower( $search ) ) ) . "%'";
				$where[] = " LOWER(sender_email) LIKE '%" . $database->getEscaped( trim( strtolower( $search ) ) ) . "%'";
				$where[] = " LOWER(subject) LIKE '%" . $database->getEscaped( trim( strtolower( $search ) ) ) . "%'";
				$where[] = " LOWER(description) LIKE '%" . $database->getEscaped( trim( strtolower( $search ) ) ) . "%'";
				$where[] = " LOWER(sender_userid) LIKE '%" . $database->getEscaped( trim( strtolower( $search ) ) ) . "%'";
		}
		
		$order_query = "";
		$order = $this->_order;
		$order_dir = $this->_order_dir;
		switch ($order) {
			case "id":
				$order_query = " ORDER BY t.id ".strtoupper( $order_dir )." ";
			  break;
			case "type":
				$order_query = " ORDER BY t.type ".strtoupper( $order_dir )." ";
			  break;
			case "subject":
				$order_query = " ORDER BY t.subject ".strtoupper( $order_dir )." ";
			  break;
			case "lastmodified":
				$order_query = " ORDER BY t.last_modified_datetime ".strtoupper( $order_dir )." ";
			  break;
			case "created":
				$order_query = " ORDER BY t.created_datetime ".strtoupper( $order_dir )." ";
			  break;
			case "status":
				$order_query = " ORDER BY t.status ".strtoupper( $order_dir )." ";
			  break; 
			case "feedback":
				$order_query = " ORDER BY t.feedback_rating ".strtoupper( $order_dir )." ";
			  break; 
			default:
				$order_query = " ORDER BY t.last_modified_datetime ".strtoupper( $order_dir )." ";
			  break;
		}
	

		$status	= $this->_status;
		// default = open tix
		$status_query = " ";
		switch ($status) {
			case "open":
			  // tix open
  			  $status_query = " AND ( t.status = '0' OR t.status = '3' ) ";
			  $this->_pagetitle = _BILLETS_OPENTICKETS;
			  break;
			case "all":
			  // all tix
			  $status_query = "";
			  $this->_pagetitle = _BILLETS_ALLTICKETS;
			  break;
			default:
			  // open tix
  			  $status_query = " AND ( t.status = '0' OR t.status = '3' ) ";
			  $this->_pagetitle = _BILLETS_OPENTICKETS;
			  break;
		}
			

		$query = "SELECT * FROM #__billets_tickets AS t "
		. " WHERE 1 "
		. $status_query
		. " AND t.sender_userid = '$my->id' "
		. (count( $where ) ? "\n HAVING " . implode( ' OR ', $where ) : "")
		. $order_query 
		;
	
		return $query;
	}
	
	/**
	 * Retrieves the entrys
	 * @return array Array of objects containing the data from the database
	 */
	function getData() {
		// Lets 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;
	}

	/**
	 * Retrieves the pagination
	 * @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 of entrys
	 * @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;
	}

	/**
	 * Retrieves the page title
	 * @return array Array of objects containing the data from the database
	 */
	function getPageTitle() {
		if (empty($this->_pagetitle)) {
			$status	= $this->_status;

			switch ($status) {
				case "open":
				  // tix open
				  $this->_pagetitle = _BILLETS_OPENTICKETS;
				  break;
				case "all":
				  // all tix
				  $this->_pagetitle = _BILLETS_ALLTICKETS;
				  break;
				default:
				  // open tix
				  $this->_pagetitle = _BILLETS_OPENTICKETS;
				  break;
			}
		}

		return $this->_pagetitle;
	}

	/**
	 * Retrieves the statuses
	 * @return array Array of objects containing the data from the database
	 */
	function getStatuses() {
		if (empty($this->_statuses)) {
			$database = &JFactory::getDBO();
			// types
			$types = array();
			$types[] = JHTML::_('select.option', " ", " "._BILLETS_OPENTICKETS." " );
			$types[] = JHTML::_('select.option', "all", " "._BILLETS_ALLTICKETS." " );	
			$javascript 	= 'onchange="document.adminForm.submit();"';	
			$this->_statuses = JHTML::_('select.genericlist', $types, "status", "size='1' $javascript", "value", "text", $this->_status );
		}

		return $this->_statuses;
	}	

	/**
	 * Retrieves the grid helpers
	 * @return array Array of objects containing the data from the database
	 */
	function getGridHelpers() {
		$return = array();

		$return["_search"] = $this->_search;
		$return["_limit"] = $this->_limit;
		$return["_limitstart"] = $this->_limitstart;
		$return["_order"] = $this->_order;
		$return["_order_dir"] = $this->_order_dir;		

		return $return;
	}	

}
