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

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

class SpsoccerModelMatches extends FOFModel {

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

	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', '');
			// Now time
			$nowDate = date('Y-m-d');

			if (!$params->get('previous_match', 0)) {
				//Time sorting
				$query->where($db->qn('date')." > ".$db->quote($nowDate));
			}

			if ($params->get('tournament')) {
				$query->where($db->qn('spsoccer_tournament_id')." = ".$db->quote( $params->get('tournament') ));
			}
			//Enabled
			$query->where($db->qn('enabled')." = ".$db->quote('1'));

			// ordering
			if ($order_by == 'asc') {
				$query->order($db->quoteName('ordering') . ' ASC');
			} elseif ($order_by == 'desc') {
				$query->order($db->quoteName('ordering') . ' DESC');
			} elseif ($order_by == 'dateasc') {
				$query->order($db->quoteName('date') . ' ASC');
			} else {
				$query->order($db->quoteName('date') . ' DESC');
			}

			//Language
			$query->where('language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
			//Access
			$query->where($db->quoteName('access')." IN (" . implode( ',', JFactory::getUser()->getAuthorisedViewLevels() ) . ")");
		}

		if (!$overrideLimits && !FOFPlatform::getInstance()->isFrontend()) {
			$order = $this->getState('filter_order', null, 'cmd');
			if (!in_array($order, array_keys($table->getData()))){
				$order = $tableKey;
			}
			$order = $db->qn($order);
			$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 groups by tournament ID
	public function getTournamentInfo($tournament_id) {
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		// Select all records from the user profile table where key begins with "custom.".
		$query->select($db->quoteName(array('spsoccer_tournament_id', 'title', 'slug', 'description')));
		$query->from($db->quoteName('#__spsoccer_tournaments'));
		$query->where($db->quoteName('enabled')." = 1");
		$query->where($db->quoteName('spsoccer_tournament_id')." = ".$db->quote($tournament_id));
		//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('ordering ASC');

		$db->setQuery($query);

		$result = $db->loadObject();

		return $result;
	}

	// Get Player by player ID
	public function getPlayerInfo($player_id) {
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		$query->select($db->quoteName(array('a.spsoccer_player_id', 'a.title', 'a.slug', 'a.description', 'a.squad_no', 'a.description', 'a.image', 'a.birth_place', 'a.dob', 'a.height', 'a.weight', 'a.banner_image')));
		$query->select($db->quoteName('b.title', 'position'));
		$query->select($db->quoteName('c.title', 'team'));
		$query->from($db->quoteName('#__spsoccer_players', 'a'));
		$query->join('LEFT', $db->quoteName('#__spsoccer_possitions', 'b') . ' ON (' . $db->quoteName('a.spsoccer_possition_id') . ' = ' . $db->quoteName('b.spsoccer_possition_id') . ')');
		$query->join('LEFT', $db->quoteName('#__spsoccer_gameteams', 'c') . ' ON (' . $db->quoteName('a.spsoccer_gameteam_id') . ' = ' . $db->quoteName('c.spsoccer_gameteam_id') . ')');
		$query->where($db->quoteName('a.enabled')." = 1");
		$query->where($db->quoteName('a.spsoccer_player_id')." = ".$db->quote($player_id));
		//Language
		$query->where('a.language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
		//Access
		$query->where($db->quoteName('a.access')." IN (" . implode( ',', JFactory::getUser()->getAuthorisedViewLevels() ) . ")");

		$db->setQuery($query);
		$result = $db->loadObject();

		return $result;
	}


}
