From 409de7a72aeb2cef7abfa7dbb4c3a8289e2312f9 Mon Sep 17 00:00:00 2001 From: x2003bruneau Date: Wed, 6 Dec 2006 15:16:06 +0000 Subject: [PATCH] #558: In case of 404, try to propose the nearest valid address git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1248 839d8a87-29fc-0310-9880-83ba4fa771e5 --- classes/platal.php | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- classes/xnet.php | 20 ++++++++++++++++++ templates/404.tpl | 5 +++++ 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/classes/platal.php b/classes/platal.php index 5cf0d75..4f8a69a 100644 --- a/classes/platal.php +++ b/classes/platal.php @@ -87,11 +87,67 @@ class Platal return $hook; } + function find_nearest_key($key, &$array) + { + $keys = array_keys($array); + if (in_array($key, $keys)) { + return $key; + } + foreach ($keys as $k) { + if (strpos($key, $k) !== false || strpos($k, $key) !== false) { + return $k; + } + } + if (in_array("#final#", $keys)) { + return "#final#"; + } + return null; + } + + function list_hooks() + { + $hooks = array(); + foreach ($this->__hooks as $hook=>$handler) { + $parts = split('/', $hook); + $place =& $hooks; + foreach ($parts as $part) { + if (!isset($place[$part])) { + $place[$part] = array(); + } + $place =& $place[$part]; + } + $place["#final#"] = array(); + } + + $p = split('/', $this->path); + $place =& $hooks; + $link = ''; + $ended = false; + foreach ($p as $k) { + if (!$ended) { + $key = $this->find_nearest_key($k, $place); + } + if ($ended || $key == "#final#") { + $key = $k; + $ended = true; + } + if (!is_null($key)) { + if (!empty($link)) { + $link .= '/'; + } + $link .= $key; + $place =& $place[$key]; + } else { + return null; + } + } + return $link; + } + function call_hook(&$page) { $hook = $this->find_hook(); - - if (is_null($hook)) { + if (empty($hook)) { return PL_NOT_FOUND; } @@ -146,6 +202,7 @@ class Platal break; case PL_NOT_FOUND: + $page->assign('near', $this->list_hooks()); $this->__mods['core']->handler_404($page); break; } diff --git a/classes/xnet.php b/classes/xnet.php index c29ec14..c221c45 100644 --- a/classes/xnet.php +++ b/classes/xnet.php @@ -38,6 +38,26 @@ class Xnet extends Platal } } + function find_nearest_key($key, &$array) + { + global $globals; + $k = parent::find_nearest_key($key, $array); + if (is_null($k) && in_array('%grp', array_keys($array)) && $globals->asso()) { + return '%grp'; + } + return $k; + } + + function list_hooks() + { + $link = parent::list_hooks(); + if (strpos($link, '%grp') !== false) { + global $globals; + return str_replace('%grp', $globals->asso('diminutif'), $link); + } + return $link; + } + function find_hook() { $ans = parent::find_hook(); diff --git a/templates/404.tpl b/templates/404.tpl index 18f68b4..27f1235 100644 --- a/templates/404.tpl +++ b/templates/404.tpl @@ -22,3 +22,8 @@

Cette page n'existe pas !!!

+{if $near} +

L'adresse suivante semble correspondre à ta demande :
+{$globals->baseurl}/{$near} +

+{/if} -- 2.1.4