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

	public function display($tpl = null){
		// Get model
		$model = $this->getModel();
		// get item
		$this->item = $model->getItem();

		//Load Match Model
		$match_model = FOFModel::getTmpInstance('Matches', 'SpsoccerModel');

		//Get Players by team id
		$this->players 		 		= $model->getPlayers($this->item->spsoccer_gameteam_id);

		foreach ($this->players as &$this->player) {
			$this->player->url  = JRoute::_('index.php?option=com_spsoccer&view=player&id=' . $this->player->spsoccer_player_id . ':' . $this->player->slug . SpsoccerHelper::getItemid('players'));

			$this->player->image = SpsoccerHelper::hasImg($this->player->image);
		}

		//get image
		$this->item->logo = SpsoccerHelper::hasImg($this->item->logo);

		// Get values
		if (isset($this->item->former_president) && $this->item->former_president) {
			$this->item->former_president 	= $match_model->getPlayerInfo($this->item->former_president)->title;
		}

		if (isset($this->item->former_president) && $this->item->former_president) {
			$this->item->manager 			= $match_model->getPlayerInfo($this->item->manager)->title;
		}

		if (isset($this->item->team_gallery) && $this->item->team_gallery) {
			if (!is_array($this->item->team_gallery) && !count($this->item->team_gallery)) {
				$this->galleries 				= json_decode($this->item->team_gallery)->gallery_images;
			} else {
				$this->galleries = $this->item->team_gallery;
				$imgs = array ();
				foreach ($this->galleries as $key => $gallery) {
					$imgs[] = $gallery['image'];
				}
				$this->galleries = $imgs;
			}
		}


		// Get Honours
		$honour_decode = json_decode($this->item->list_honours);

		if (isset($honour_decode)) {

			$number 		= $honour_decode->number;
			$types 			= $honour_decode->type;
			$titles			= $honour_decode->title;
			$year_lists		= $honour_decode->year_list;

			// Team Honours
			$this->honours = array();
			$hkey 		   = 0;
			foreach ($titles as $id => $title) {
				$this->honours[$hkey] = array(
					'number' 		=> $number[$id],
					'type'			=> $types[$id],
					'title'    		=> $titles[$id],
					'year_list'    	=> $year_lists[$id],
				);
				$hkey ++;
			}
		}


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