<?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 JLTicketCenterModelPerformer  extends JModelLegacy
{
	var $_data;

	var $_total = null;
	var $_pagination = null;


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

		$mainframe = JFactory::getApplication();

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

		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

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

		// Ordering
		$context			= 'com_jlticketcenter.performer.list.';
		$filter_order     = $mainframe->getUserStateFromRequest($context.'filter_order','filter_order','p.performer_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);
	}


	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 _buildQuery()
	{
		$filter = JRequest::getVar( 'filter', null );// LOWER;
		$search =$filter['search'];
		if ($search){
			$search = strtolower($search);
			$where[] = " LOWER(`performer_name`) LIKE '%".$search."%'";
		}

		if ($filter['category']>0){
			$where[] = " p.`cat_id`=".$filter['category'];
		}

		$query = " SELECT p.*, cat.`title` as `cat_title` "
		. " FROM #__jltcenter_performer p "
		. " LEFT JOIN #__categories cat ON cat.`id`=p.`cat_id` AND cat.`extension` = 'com_jlticketcenter'"
		;

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

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

		return $query;
	}


	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;
	}
	 
	/**
	 * 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 cancel()
	{
		$row =& $this->getTable();
		$row->bind(JRequest::get('post'));
		$row->checkin();
		return;
	}

}//EOC