From a3ec5f4af2e46256ac7efebb3a11e7084783080b Mon Sep 17 00:00:00 2001
From: matthiasgrimm <matthiasgrimm@users.sourceforge.net>
Date: Thu, 19 May 2005 19:40:25 +0200
Subject: [PATCH] add a back button to parent page

This patch extends the template functions with back
button linking to the current pages' parent if
available. Both tpl_button() and tpl_actionlink()
are supported.

For this to work the first page in the namespace must
have the same name as the namespace itself. The 'back'
button of every page in this namespace links to
namespace:namespace. The 'back' button of the page
namespace:namespace links to the first page of the
containing namespace and so forth until the start page
has been reached.

Because of the precondition decribed above, the default
template hasn't got the new 'back' button. It is reserved
for custom made templates and installations which take
care of the precondition.

darcs-hash:20050519174025-7ef76-1e9c78c941f53871905fc1ba08b28a826553daa3.gz
---
 inc/template.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 lang/de/lang.php |  1 +
 lang/en/lang.php |  1 +
 3 files changed, 52 insertions(+)

diff --git a/inc/template.php b/inc/template.php
index ea8b99b6c..7d3765ee0 100644
--- a/inc/template.php
+++ b/inc/template.php
@@ -193,6 +193,42 @@ function tpl_link($url,$name,$more=''){
   print ">$name</a>";
 }
 
+/**
+ * get the parent page
+ *
+ * Tries to find out which page is parent.
+ * returns false if none is available
+ *
+ * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
+ */
+function tpl_getparent($ID){
+  global $conf;
+    
+  if ($ID != $conf['start']) {
+    $idparts = explode(':', $ID);
+    $pn = array_pop($idparts);    // get the page name
+
+    for ($n=0; $n < 2; $n++) {
+      if (count($idparts) == 0) {
+        $ID = $conf['start'];     // go to topmost page
+        break;
+      }else{
+        $ns = array_pop($idparts);     // get the last part of namespace
+        if ($pn != $ns) {                 // are we already home?
+          array_push($idparts, $ns, $ns); // no, then add a page with same name
+          $ID = implode (':', $idparts); // as the namespace and recombine $ID
+          break;
+        }
+      }
+    }
+    
+    if (@file_exists(wikiFN($ID))) {
+      return $ID;
+    }
+  }
+  return false;
+}
+
 /**
  * Print one of the buttons
  *
@@ -205,8 +241,10 @@ function tpl_link($url,$name,$more=''){
  *  index   - The index
  *  admin   - admin page - if enough rights
  *  top     - a back to top button
+ *  back    - a back to parent button - if available
  *
  * @author Andreas Gohr <andi@splitbrain.org>
+ * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
  */
 function tpl_button($type){
   global $ID;
@@ -226,6 +264,11 @@ function tpl_button($type){
     case 'index':
       print html_btn(index,$ID,'x',array('do' => 'index'));
       break;
+    case 'back':
+      if ($ID = tpl_getparent($ID)) {
+        print html_btn(back,$ID,'b',array('do' => 'show'));
+      }
+      break;
     case 'top':
       print html_topbtn();
       break;
@@ -259,8 +302,10 @@ function tpl_button($type){
  *  index   - The index
  *  admin   - admin page - if enough rights
  *  top     - a back to top button
+ *  back    - a back to parent button - if available
  *
  * @author Andreas Gohr <andi@splitbrain.org>
+ * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
  * @see    tpl_button
  */
 function tpl_actionlink($type,$pre='',$suf=''){
@@ -308,6 +353,11 @@ function tpl_actionlink($type,$pre='',$suf=''){
     case 'top':
       print '<a href="#top" class="action" accesskey="x">'.$pre.$lang['btn_top'].$suf.'</a>';
       break;
+    case 'back':
+      if ($ID = tpl_getparent($ID)) {
+        tpl_link(wl($ID,'do=show'),$pre.$lang['btn_back'].$suf,'class="action" accesskey="b"');
+      }
+      break;
     case 'login':
       if($conf['useacl']){
         if($_SERVER['REMOTE_USER']){
diff --git a/lang/de/lang.php b/lang/de/lang.php
index 145be6bcc..c4f65d814 100644
--- a/lang/de/lang.php
+++ b/lang/de/lang.php
@@ -31,6 +31,7 @@ $lang['btn_logout'] = 'Abmelden';
 $lang['btn_admin']  = 'Admin';
 $lang['btn_update'] = 'Updaten';
 $lang['btn_delete'] = 'Löschen';
+$lang['btn_back']   = 'Zurück';
 
 $lang['loggedinas'] = 'Angemeldet als';
 $lang['user']       = 'Benutzername';
diff --git a/lang/en/lang.php b/lang/en/lang.php
index d639276d9..49773b594 100644
--- a/lang/en/lang.php
+++ b/lang/en/lang.php
@@ -30,6 +30,7 @@ $lang['btn_logout'] = 'Logout';
 $lang['btn_admin']  = 'Admin';
 $lang['btn_update'] = 'Update';
 $lang['btn_delete'] = 'Delete';
+$lang['btn_back']   = 'Back';
 
 $lang['loggedinas'] = 'Logged in as';
 $lang['user']       = 'Username';
-- 
GitLab