<?php
/**
 * @version		3.0.0
 * @package		JomLand TicketCenter
 * @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
 * @link 		www.jomland.com
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

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

class JLTicketCenterModelVenue  extends JModelLegacy
{
	var $_data;
	var $_total = null;
	var $_pagination = null;

	private static $Performer_request 	= 2;
	private static $City_request 		= 4;
	private static $State_request 		= 5;
	private static $Country_request 	= 6;

	function __construct()
	{
		parent::__construct();

		$mainframe = JFactory::getApplication();

		// Get pagination request variables
		$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');

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

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);

		// Order By
		$context			= 'com_jlticketcenter.venue.list.';
		$filter_order     = $mainframe->getUserStateFromRequest($context.'filter_order','filter_order','venue.`venue_name`','cmd');
		$filter_order_Dir = $mainframe->getUserStateFromRequest($context.'filter_order_Dir','filter_order_Dir','asc','word');

		$this->setState('filter_order', $filter_order);
		$this->setState('filter_order_Dir', $filter_order_Dir);
	}


	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildQuery()
	{
		$filter = JRequest::getVar( 'filter', null );// LOWER;
		$search =$filter['search'];
		if ($search){
			$search = strtolower($search);
			$where[] = "(LOWER(venue.`venue_name`) LIKE '%".$search."%' OR LOWER(city.`city_name`) LIKE '%".$search."%' OR LOWER(country.`country`) LIKE '%".$search."%')";
		}
		if ($filter['state']){
			if ($filter['state']=='U'){
				$where[] = "venue.`published`=0";
			}else{
				$where[] = "venue.`published`=1";
			}
		}


		if ($filter['country']){
			$where[] = "venue.`country_id`='".$filter['country']."'";
		}

		if ($filter['states']){
			$where[] = "venue.`state_id`='".$filter['states']."'";
		}
		if ($filter['city']){
			$where[] = "venue.`city_id`='".$filter['city']."'";
		}


		$query = 'SELECT venue.`id`, venue.`venue_name`,venue.`published`, venue.`checked_out`,venue.`checked_out_time`, venue.`ordering`,city.`city_name`, country.`country`,state.`state_name`'
		. ' FROM #__jltcenter_venue venue'
		.' LEFT JOIN  #__jltcenter_city city ON city.`id`=venue.`city_id`'
		.' LEFT JOIN  #__jltcenter_state state ON state.`id`=venue.`state_id`'
		.' LEFT JOIN  #__jltcenter_country country ON country.`id`=venue.`country_id`'
		;

		if (isset($where)){
			$query .=" WHERE ".implode(' AND ',$where);
		}

		$query .= $this->_buildEventsOrderBy();

		//echo $query ;
		return $query;
	}


	function _buildEventsOrderBy()
	{
		$mainframe = JFactory::getApplication();

		$orderby = '';
		$filter_order     = $this->getState('filter_order');
		$filter_order_Dir = $this->getState('filter_order_Dir');

		/* Error handling is never a bad thing*/
		if(!empty($filter_order) && !empty($filter_order_Dir) ){
			$orderby = ' ORDER BY '.$filter_order.' '.$filter_order_Dir;
		}
		return $orderby;
	}


	function getData()
	{
		if (empty($this->_data)) {
			$query = $this->_buildQuery();

			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_data;
	}

	 
	function getTotal()
	{
		if (empty($this->_total)) {
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}
		return $this->_total;
	}
	 

	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;
	}


	function getCountry()
	{
		$db =& JFactory::getDBO();

		$query = 'SELECT *  FROM #__jltcenter_country ORDER BY `country`';
		$db->setQuery( $query );
		$counties = $db->loadObjectList();

		return $counties;
	}

	 
	 
	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store($data)
	{
		$row =& $this->getTable();

		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}


		if (!$row->check($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		if (!$data['id']){
			$data['id'] = $this->_db->insertid();
			JRequest::set($data,'post');
		}

		return true;
	}

	function delete()
	{
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		$row =&$this->getTable();

		if (count( $cids )) {
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getErrorMsg() );
					return false;
				}
			}
		}
		return true;
	}


	function getCountrySelect(){
		if (!class_exists(GENERAL_MODEL)){
			include 'ticketscenter.php';
		}
		$model = JModelLegacy::getInstance('General', 'JLTicketCenterModel');

		return $model->buildCountrySelect();
	}

	function getStateSelect(){
		if (!class_exists(GENERAL_MODEL)){
			include 'ticketscenter.php';
		}
		$model = JModelLegacy::getInstance('General', 'JLTicketCenterModel');

		return $model->buildStateSelect();
	}

	function getCitySelect(){
		if (!class_exists(GENERAL_MODEL)){
			include 'ticketscenter.php';
		}

		$model = JModelLegacy::getInstance('General', 'JLTicketCenterModel');
			
		return $model->buildCitySelect();
	}

	function cancel()
	{
		$row =& $this->getTable();
		$row->bind(JRequest::get('post'));
		$row->checkin();
		return;
	}


} // EOC