<?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 BilletsModelTickets extends JModel {

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

	/**
	 * constructor
	 * @return array Array of objects containing the data from the database
	 */	
	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' );

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

		// set the type
		$this->_type = strval( htmlspecialchars( $mainframe->getUserStateFromRequest( "type", 'type', ' ' ) ) );
		switch ($this->_type) {
			case "lang": $this->_type = ''; break;
			case "mod": $this->_type = ''; break;
			case "report": $this->_type = ''; break;
			default: break;
		}

		// set the status
		$this->_status = strval( htmlspecialchars( $mainframe->getUserStateFromRequest( "status", 'status', 'open' ) ) );

	}
	
	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildQuery() {
		global $mainframe;
		$database = &JFactory::getDBO();
		// ************************************** //
		//	RECORDS
		// ************************************** //
		$where = array();
		$lists = array();

		$search				= $mainframe->getUserStateFromRequest( 'search', 'search', '',	'string' );
		$search				= JString::strtolower($search);
			
		$type = $this->_type;
		if ($type) {
			$type_query = " AND LOWER(t.type) LIKE '%" . $database->getEscaped( trim( strtolower( $type ) ) ) . "%'";
		}
				
		$order_query = "";
		$order = $this->getState( 'order' );
		$order_dir = $this->getState( '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 "from":
				$order_query = " ORDER BY t.sender_userid ".strtoupper( $order_dir )." ";
			  break;
			case "followup":
				$order_query = " ORDER BY t.followup ".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.id DESC ";
			  break;
		}
	

		$status	= $this->_status;
		// default = open tix
		$status_query = " AND ( t.status = '0' ) ";
		switch ($status) {
			case "followup":
			  // tix to followup on
			  $status_query = " AND ( t.followup = '1' ) ";
			  $this->_pagetitle = _BILLETS_TICKETSFORFOLLOWUP;
			  break;
			case "open":
			  // tix open
			  $status_query = " AND ( t.status = '0' ) ";
			  $this->_pagetitle = _BILLETS_OPENTICKETS;
			  break;
			case "all":
			  // all tix
			  $status_query = "";
			  $this->_pagetitle = _BILLETS_ALLTICKETS;
			  break;
			case "feedback":
			  // tix requiring feedback
			  $status_query = " AND ( t.status = '3' ) ";
			  $this->_pagetitle = _BILLETS_TICKETSREQUIRINGFEEDBACK;
			  break;
			case "closed":
			  // tix closed
			  $status_query = " AND ( t.status = '2' ) ";
			  $this->_pagetitle = _BILLETS_CLOSEDTICKETS;
			  break;
			default:
			  // open tix
			  $status_query = " AND ( t.status = '0' ) ";
			  $this->_pagetitle = _BILLETS_OPENTICKETS;
			  break;
		}
		
		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 ) ) ) . "%'";
		}
	
	
		// all records		
		$query = "SELECT t.* FROM #__billets_tickets as t "
		. " WHERE 1 "
		. $status_query
		. $type_query
		. (count( $where ) ? "\n HAVING " . implode( ' OR ', $where ) : "")
		. $order_query 
		;
		
		return $query;
	}
	
	/**
	 * Retrieves the data
	 * @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;
	}
	
	/**
	 * Retrieves the types
	 * @return array Array of objects containing the data from the database
	 */
	function getTypes() {
		if (empty($this->_types)) {
			$database = &JFactory::getDBO();
			// types
			$types = array();
			$allrows = billetsSupportTicketTypeOptions ( );
			$types[] = JHTML::_('select.option', ' ', "- "._BILLETS_SELECTTYPE." -" );
			if ($allrows) { foreach ($allrows as $a) {
				$types[] = JHTML::_('select.option', $a, constant($a) );
			} }	
			$javascript 	= 'onchange="document.adminForm.submit();"';	
			$this->_types = JHTML::_('select.genericlist', $types, "type", "size='1' $javascript", "value", "text", $this->_type );
		}

		return $this->_types;
	}

	/**
	 * Retrieves the type
	 * @return array Array of objects containing the data from the database
	 */
	function getType() {
		if (empty($this->_type)) {
			global $mainframe;
			// set the type
			$this->_type = strval( htmlspecialchars( $mainframe->getUserStateFromRequest( "type", 'type', ' ' ) ) );

		}

		return $this->_type;
	}

	/**
	 * 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 "followup":
				  // tix to followup on
				  $this->_pagetitle = _BILLETS_TICKETSFORFOLLOWUP;
				  break;
				case "open":
				  // tix open
				  $this->_pagetitle = _BILLETS_OPENTICKETS;
				  break;
				case "all":
				  // all tix
				  $this->_pagetitle = _BILLETS_ALLTICKETS;
				  break;
				case "feedback":
				  // tix requiring feedback
				  $this->_pagetitle = _BILLETS_TICKETSREQUIRINGFEEDBACK;
				  break;
				case "closed":
				  // tix closed
				  $this->_pagetitle = _BILLETS_CLOSEDTICKETS;
				  break;
				default:
				  // open tix
				  $this->_pagetitle = _BILLETS_OPENTICKETS;
				  break;
			}
		}

		return $this->_pagetitle;
	}
	
	
		
}
// ************************************************************************
