#558: In case of 404, try to propose the nearest valid address
authorx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Wed, 6 Dec 2006 15:16:06 +0000 (15:16 +0000)
committerx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Wed, 6 Dec 2006 15:16:06 +0000 (15:16 +0000)
git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1248 839d8a87-29fc-0310-9880-83ba4fa771e5

classes/platal.php
classes/xnet.php
templates/404.tpl

index 5cf0d75..4f8a69a 100644 (file)
@@ -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;
         }
index c29ec14..c221c45 100644 (file)
@@ -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();
index 18f68b4..27f1235 100644 (file)
@@ -22,3 +22,8 @@
 
 <h1 class="erreur">Cette page n'existe pas !!!</h1>
 
+{if $near}
+<p>L'adresse suivante semble correspondre à ta demande :<br />
+<a href="{$globals->baseurl}/{$near}">{$globals->baseurl}/{$near}</a>
+</p>
+{/if}