<?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 SpsoccerHelper {

	// Common
	public static function getItemid($view = 'tournaments') {
		$db = JFactory::getDbo();

		$query = $db->getQuery(true);
		$query->select($db->quoteName(array('id')));
		$query->from($db->quoteName('#__menu'));
		$query->where($db->quoteName('link') . ' LIKE '. $db->quote('%option=com_spsoccer&view='. $view .'%'));
		$query->where($db->quoteName('published') . ' = '. $db->quote('1'));
		$db->setQuery($query);
		$result = $db->loadResult();

		if(count($result)) {
			return '&Itemid=' . $result;
		}

		return false;
	}

	// Get teams info by team id
	public function getTeamsInfoById($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));
		$query->order('ordering DESC');
		$db->setQuery($query);
		$results = $db->loadObjectList();

		// has data
		if(count($results)) {
			return $results;
		}else{
			return false;
		}

	}
	// if item hasn't image then set no image
	public static function hasImg($image = ''){

		$params = JComponentHelper::getParams('com_spsoccer');
		$noImg  = $params->get('no_image', '');

		if (isset($image) && $image) {
			return $image;
		} else {
			// if image selected from admin
			if (isset($noImg) && $noImg) {
				$image = $noImg;
			} else {
				// else set soccer default no image
				$image = JURI::root(true) . '/components/com_spsoccer/assets/images/soccer-no-image.png';
			}

			return $image;
		}

	} // hasImage

}
