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

			//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() ) . ")");

			//get by tournament
			if ($params->get('person_type', '')) {
				$query->where($db->qn('person_type')." = ".$db->quote( $params->get('person_type') ));
			}
			if ($params->get('teamid', '')) {
				$query->where($db->qn('spsoccer_gameteam_id')." = ".$db->quote( $params->get('teamid') ));
			}
			if ($params->get('player_position', '')) {
				$query->where($db->qn('spsoccer_possition_id')." = ".$db->quote( $params->get('player_position') ));
			}
			if ($order_by == 'asc') {
				$query->order($db->quoteName('ordering') . ' ASC');
			} elseif ($order_by == 'team') {
				$query->order($db->quoteName('spsoccer_gameteam_id') . ' DESC');
			} elseif ($order_by == 'position') {
				$query->order($db->quoteName('spsoccer_possition_id') . ' DESC');
			} else {
				$query->order($db->quoteName('ordering') . ' DESC');
			}

		}

		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 Position by Position ID
	public function getPositionByID($position_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_possition_id', 'title', 'slug')));
		$query->from($db->quoteName('#__spsoccer_possitions'));
		$query->where($db->quoteName('enabled')." = 1");
		$query->where($db->quoteName('spsoccer_possition_id')." = ".$db->quote($position_id));
		$query->order('ordering ASC');

		$db->setQuery($query);

		$result = $db->loadObject();

		return $result;
	}

	// Get team info by team ID
	public function getTeamByID($team_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_gameteam_id', 'title', 'slug', 'logo')));
		$query->from($db->quoteName('#__spsoccer_gameteams'));
		$query->where($db->quoteName('enabled')." = 1");
		$query->where($db->quoteName('spsoccer_gameteam_id')." = ".$db->quote($team_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;
	}

}
