<?php
/**
 * JE GroupBuy package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2011 - 2012 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/


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

// import the Joomla modellist library
jimport('joomla.application.component.modellist');

/**
 * JE GroupBuy  Model
 */
class jegroupbuyModelorders extends JModelList
{
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'id', 'ord.id',
				'p_id', 'ord.p_id',
				'method', 'ord.method',
				'transaction_id', 'ord.transaction_id',
				'pdate', 'ord.pdate',
				'status', 'ord.status',
			);
		}

		parent::__construct($config);
	}
	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return	string	An SQL query
	 */
	protected function getListQuery()
	{
		// Create a new query object.
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);

		// Select some fields
		$query->select( $this->getState(
				'list.select',('ord.*')
			) );
		// From the hello table
		$query->from('#__jegroupbuy_payment AS ord');

		// Join over the asset groups.
			$query->select( 'pro.title AS title, s.shop_name AS shopname' );
			$query->join( 'LEFT', '#__jegroupbuy_product AS pro ON pro.id = ord.p_id' );
			$query->join('LEFT', '#__jegroupbuy_stores AS s ON s.id = pro.store_id' );

		// Filter by published state
			$published		= $this->getState('filter.status');
			if (is_numeric($published)) {
				$query->where('ord.status = ' . (int) $published);
			} else if ($published === '') {
				$query->where('(ord.status = 0 OR ord.status = 1)');
			}

		// Filter by search in title & microblog.
			$search			= $this->getState('filter.search');
			if (!empty($search)) {
				if (stripos($search, 'id:') === 0) {
					$query->where('ord.id = '.(int) substr($search, 3));
				} else {
					$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
					$query->where('(ord.transaction_id LIKE '.$search.') OR (pro.title LIKE '.$search.')');
				}
			}
		
		// Add the list ordering clause.
			$orderCol		= $this->state->get('list.ordering');
			$orderDirn		= $this->state->get('list.direction');

			if ($orderCol == 'ord.ordering' || $orderCol == 'category_title') {
				$orderCol	= 'category_title '. $orderDirn .', ord.ordering';
			}
			
			$query->order($db->getEscaped($orderCol.' '.$orderDirn));

		return $query;
	}
	/**
	 * Method to auto-populate the model state.
	 */
	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
			$app		= JFactory::getApplication();

		// Adjust the context to support modal layouts.
			if ($layout = JRequest::getVar('layout')) {
				$this->context .= '.'.$layout;
			}

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);

		$published		= $this->getUserStateFromRequest($this->context.'.filter.state', 'filter_state', '');
		$this->setState('filter.status', $published);

		// List state information.
			parent::populateState('ord.id', 'asc');
	}

	protected function getStoreId($id = '')
	{
		// Compile the store id.
			$id	.= ':'.$this->getState('filter.search');
			$id	.= ':'.$this->getState('filter.status');

		return parent::getStoreId($id);
	}
}
