<?php


if ( ! function_exists('url'))
{
    /**
     * Generate a url for the application.
     *
     * @param  string  $path
     * @param  mixed   $parameters
     * @param  bool    $secure
     * @return string
     */
    function url($path = null, $parameters = array(), $secure = null)
    {
		// We first check if the path starts by /portugal or /espana or /brasil
		// with, or without the initial slash, and, if not apply the prefix to the
		// path
		$path = (!preg_match('/^\/?('.( implode('|', AVAILABLE_LANGUAGES) ).')/', $path) && isset($_REQUEST['lang'])) ?
					$_REQUEST['lang'] . '/' . preg_replace('/^\//', '', $path)
				: $path;

        $url = app('url')->to($path, $parameters, $secure);

		return $url;
    }
}


if ( ! function_exists('route'))  {
	/**
	 * Generate a URL to a named route.
	 *
	 * @param  string  $name
	 * @param  array   $parameters
	 * @param  bool  $absolute
	 * @param  \Illuminate\Routing\Route $route
	 * @return string
	 */
	function route($name, $parameters = array(), $absolute = false, $route = null, $lang = null) {

		$url = app('url')->route($name, $parameters, false, $route);

		// apply the prefix of the language to each route
		$url = (isset($_REQUEST['lang']) && $lang === null) ? '/' . $_REQUEST['lang'] . $url : $url;

		// if the param lang is different from null (emails, for example)
		// we set the prefix to the same as the lang param
		$url = (in_array($lang, AVAILABLE_LANGUAGES)) ? '/' . $lang . $url : $url;

		// If the path initially was set to absolute,
		// we have to prepend the base path after adding the lang prefix
		if($absolute) {

			// the base path http://___.local/
			$base = Request::root();

			$url = $base . $url;

		}

		return $url;

	}

}



?>
