2014-07-23 15:52:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* La classe Url permet de gérer les URL à travers MINZ
|
|
|
|
*/
|
|
|
|
class Minz_Url {
|
|
|
|
/**
|
|
|
|
* Affiche une Url formatée
|
|
|
|
* @param $url l'url à formater définie comme un tableau :
|
|
|
|
* $url['c'] = controller
|
|
|
|
* $url['a'] = action
|
|
|
|
* $url['params'] = tableau des paramètres supplémentaires
|
|
|
|
* ou comme une chaîne de caractère
|
|
|
|
* @param $encodage pour indiquer comment encoder les & (& ou & pour html)
|
|
|
|
* @return l'url formatée
|
|
|
|
*/
|
|
|
|
public static function display ($url = array (), $encodage = 'html', $absolute = false) {
|
|
|
|
$isArray = is_array($url);
|
|
|
|
|
|
|
|
if ($isArray) {
|
2015-11-06 16:54:20 +01:00
|
|
|
$url = self::checkUrl($url);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$url_string = '';
|
|
|
|
|
|
|
|
if ($absolute) {
|
2016-07-24 11:59:17 +02:00
|
|
|
$url_string = Minz_Request::getBaseUrl();
|
|
|
|
if ($url_string == '') {
|
2015-11-06 16:54:20 +01:00
|
|
|
$url_string = Minz_Request::guessBaseUrl();
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
2016-07-24 11:59:17 +02:00
|
|
|
if ($isArray) {
|
|
|
|
$url_string .= PUBLIC_TO_INDEX_PATH;
|
|
|
|
}
|
|
|
|
if ($absolute === 'root') {
|
|
|
|
$url_string = parse_url($url_string, PHP_URL_PATH);
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
} else {
|
|
|
|
$url_string = $isArray ? '.' : PUBLIC_RELATIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($isArray) {
|
2015-11-06 16:54:20 +01:00
|
|
|
$url_string .= self::printUri($url, $encodage);
|
|
|
|
} elseif ($encodage === 'html') {
|
|
|
|
$url_string = Minz_Helper::htmlspecialchars_utf8($url_string . $url);
|
2014-07-23 15:52:50 +02:00
|
|
|
} else {
|
|
|
|
$url_string .= $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url_string;
|
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
/**
|
|
|
|
* Construit l'URI d'une URL
|
|
|
|
* @param l'url sous forme de tableau
|
|
|
|
* @param $encodage pour indiquer comment encoder les & (& ou & pour html)
|
|
|
|
* @return l'uri sous la forme ?key=value&key2=value2
|
|
|
|
*/
|
2015-02-08 18:55:48 +01:00
|
|
|
private static function printUri($url, $encodage) {
|
2014-07-23 15:52:50 +02:00
|
|
|
$uri = '';
|
|
|
|
$separator = '?';
|
2015-02-08 18:55:48 +01:00
|
|
|
|
|
|
|
if ($encodage === 'html') {
|
2014-07-23 15:52:50 +02:00
|
|
|
$and = '&';
|
|
|
|
} else {
|
|
|
|
$and = '&';
|
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
|
|
|
if (isset($url['c'])
|
|
|
|
&& $url['c'] != Minz_Request::defaultControllerName()) {
|
2014-07-23 15:52:50 +02:00
|
|
|
$uri .= $separator . 'c=' . $url['c'];
|
|
|
|
$separator = $and;
|
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
|
|
|
if (isset($url['a'])
|
|
|
|
&& $url['a'] != Minz_Request::defaultActionName()) {
|
2014-07-23 15:52:50 +02:00
|
|
|
$uri .= $separator . 'a=' . $url['a'];
|
|
|
|
$separator = $and;
|
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
|
|
|
if (isset($url['params'])) {
|
2014-07-23 15:52:50 +02:00
|
|
|
foreach ($url['params'] as $key => $param) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$uri .= $separator . urlencode($key) . '=' . urlencode($param);
|
2014-07-23 15:52:50 +02:00
|
|
|
$separator = $and;
|
|
|
|
}
|
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
return $uri;
|
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
/**
|
|
|
|
* Vérifie que les éléments du tableau représentant une url soit ok
|
|
|
|
* @param l'url sous forme de tableau (sinon renverra directement $url)
|
|
|
|
* @return l'url vérifié
|
|
|
|
*/
|
|
|
|
public static function checkUrl ($url) {
|
|
|
|
$url_checked = $url;
|
2015-02-08 18:55:48 +01:00
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
if (is_array ($url)) {
|
|
|
|
if (!isset ($url['c'])) {
|
|
|
|
$url_checked['c'] = Minz_Request::defaultControllerName ();
|
|
|
|
}
|
|
|
|
if (!isset ($url['a'])) {
|
|
|
|
$url_checked['a'] = Minz_Request::defaultActionName ();
|
|
|
|
}
|
|
|
|
if (!isset ($url['params'])) {
|
|
|
|
$url_checked['params'] = array ();
|
|
|
|
}
|
|
|
|
}
|
2015-02-08 18:55:48 +01:00
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
return $url_checked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _url ($controller, $action) {
|
|
|
|
$nb_args = func_num_args ();
|
|
|
|
|
|
|
|
if($nb_args < 2 || $nb_args % 2 != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$args = func_get_args ();
|
|
|
|
$params = array ();
|
|
|
|
for($i = 2; $i < $nb_args; $i = $i + 2) {
|
|
|
|
$params[$args[$i]] = $args[$i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return Minz_Url::display (array ('c' => $controller, 'a' => $action, 'params' => $params));
|
|
|
|
}
|