<?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 SpsoccerViewVanue extends FOFViewHtml{

	public function display($tpl = null){
		// Get model
		$model = $this->getModel();
		$tournament_model = FOFModel::getTmpInstance('Tournaments', 'SpsoccerModel');
		$this->item = $model->getItem();

		JHtml::_('jquery.framework');
		// get component params
		jimport('joomla.application.component.helper');
		$app 								= JFactory::getApplication();
		$menuParams 				= $app->getMenu()->getActive();
		$this->cParams      = JComponentHelper::getParams('com_spsoccer');
		$gmap_api           = $this->cParams->get('gmap_api');
		$order_by           = $menuParams->params->get('order_by', 'DESC');
		$matches_order      = $menuParams->params->get('matches_order', 'date_desc');
		$matches_limit      = $menuParams->params->get('matches_limit', '20');

		// Add Script
		$doc = JFactory::getDocument();
		if ($gmap_api) {
				$doc->addScript('//maps.google.com/maps/api/js?libraries=places&key='. $gmap_api .'');
		} else{
				$doc->addScript('//maps.google.com/maps/api/js?libraries=places');
		}

		$doc->addScript( JURI::base(true) . '/components/com_spsoccer/assets/js/gmap.js');

		$this->vanue_matches = $model->getMatchesByVanue($this->item->spsoccer_vanue_id, $matches_order, $matches_limit);
		$this->item->map = explode(',', $this->item->map);
		$this->item->socials 		= json_decode($this->item->socials);

		//$this->item->gallery 		= json_decode($this->item->gallery);

		foreach ($this->vanue_matches as &$this->vanue_match) {
			//Get team one info
			$this->vanue_match->team_one_info = $tournament_model->getTeamInfoById($this->vanue_match->teamone);

			$this->vanue_match->team_one_url = JRoute::_('index.php?option=com_spsoccer&view=gameteam&id=' . $this->vanue_match->team_one_info->spsoccer_gameteam_id . ':' . $this->vanue_match->team_one_info->slug . SpsoccerHelper::getItemid('gameteams'));

			//Get team two info
			$this->vanue_match->team_two_info = $tournament_model->getTeamInfoById($this->vanue_match->teamtwo);

			$this->vanue_match->team_two_url = JRoute::_('index.php?option=com_spsoccer&view=gameteam&id=' . $this->vanue_match->team_two_info->spsoccer_gameteam_id . ':' . $this->vanue_match->team_two_info->slug . SpsoccerHelper::getItemid('gameteams'));

			//Match Date
			$this->vanue_match->match_date = JHtml::date($this->vanue_match->date, 'd M, Y');
			//Match Month
			$this->vanue_match->match_time = date ('h:i A',strtotime($this->vanue_match->time));
		}

		return parent::display($tpl = null);
	}
}
