<?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 SpsoccerModelGameteams 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', '');

			//Enabled
			$query->where($db->qn('enabled')." = ".$db->quote('1'));
			//get by tournament
			if ($params->get('team_type')) {
				$query->where($db->qn('team_type')." = ".$db->quote( $params->get('team_type') ));
			}
			if ($order_by == 'asc') {
				$query->order($db->quoteName('ordering') . ' ASC');
			} else {
				$query->order($db->quoteName('ordering') . ' 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 Players by Team ID
	public static function getPlayers($team_id) {
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		$query->select($db->quoteName(array('a.spsoccer_player_id', 'a.title', 'a.slug', 'a.image', 'a.spsoccer_possition_id', 'a.squad_no')));
		$query->select($db->quoteName('b.title', 'player_position'));
		$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->where($db->quoteName('a.enabled')." = 1");
		$query->where($db->quoteName('a.spsoccer_gameteam_id')." = ".$db->quote($team_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);
		$results = $db->loadObjectList();

		return $results;
	}

}
