<?php
/**
 * @package     SP Soccer
 * @author      JoomShaper http://www.joomshaper.com
 * @copyright   Copyright (C) 2010 - 2017 JoomShaper. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die('Restricted access');

class SpsoccerModelVanues extends FOFModel {

	public function __construct($config = array()){
		parent::__construct($config);
	}

	// build query
	public function buildQuery($overrideLimits = false){
		$table = $this->getTable();
		$tableName = $table->getTableName();
		$tableKey = $table->getKeyName();
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		// Call the behaviors
		$this->modelDispatcher->trigger('onBeforeBuildQuery', array(&$this, &$query));
		$select = $db->qn($tableName) . '.*';
		$query->select($select)->from($db->qn($tableName));

		//Frontend
		if(FOFPlatform::getInstance()->isFrontend()) {
			// Get Params
			$app = JFactory::getApplication();
			$params   = $app->getMenu()->getActive()->params; // get the active item
			$order_by = $params->get('order_by', 'DESC');

			//Enabled
			$query->where($db->qn('enabled')." = ".$db->quote('1'));
			//Language
			$query->where('language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
			//Access
			$query->where($db->quoteName('access')." IN (" . implode( ',', JFactory::getUser()->getAuthorisedViewLevels() ) . ")");
			$query->order($db->quoteName('ordering') . $order_by);
		}

		if (!$overrideLimits && !FOFPlatform::getInstance()->isFrontend()){
			$order = $this->getState('filter_order', null, 'cmd');

			if (!in_array($order, array_keys($table->getData()))) {
				$order = $tableKey;
			}
			$dir = $this->getState('filter_order_Dir', 'ASC', 'cmd');

			$query->order($order . ' ' . $dir);
		}

		if(!FOFPlatform::getInstance()->isFrontend()) {
			// Call the behaviors
			$this->modelDispatcher->trigger('onAfterBuildQuery', array(&$this, &$query));
		}

		return $query;
	}

	// Get Sessions by tournament ID
	public function getMatchesByVanue($vanue_id = 0, $ordering = 'date_desc', $limit = NULL) {
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);
		$query->select($db->quoteName(array('spsoccer_match_id', 'title', 'slug', 'teamone', 'teamtwo', 'date', 'time', 'spsoccer_vanue_id')));
		$query->from($db->quoteName('#__spsoccer_matches'));
		$query->where($db->quoteName('enabled')." = 1");
		$query->where($db->quoteName('spsoccer_vanue_id')." = ".$db->quote($vanue_id));
		//Language
		$query->where('language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
		//Access
		$query->where($db->quoteName('access')." IN (" . implode( ',', JFactory::getUser()->getAuthorisedViewLevels() ) . ")");
		// ordering
		if ($ordering == 'ASC'  || $ordering == 'DESC') {
			$query->order('ordering '. $ordering);
		} elseif($ordering == 'date_asc'){
			$query->order('date ASC');
			$query->order('time ASC');
		} elseif($ordering == 'date_desc') {
			$query->order('date DESC');
			$query->order('time DESC');
		}
		if ($limit) {
			$query->setLimit($limit);
		}
		$db->setQuery($query);
		$results = $db->loadObjectList();

		return $results;
	}

}
