recreate 'trunk' from 'branches/diogenes-0.9.19'
[diogenes.git] / include / diogenes.barrel.inc.php
index b348c7e..3893284 100644 (file)
@@ -21,7 +21,6 @@
 
 require_once 'diogenes.page.inc.php';
 require_once 'Barrel.php';
-require_once 'Barrel/Menu.php';
 
 /** This class is used to display a page of a Diogenes barrel,
  *  that is an RCS-managed website with a virtual directory
@@ -214,7 +213,6 @@ class DiogenesBarrel extends DiogenesPage
         $render_plugin = $plugobj;
       }
     }
-
     // source page or pass it to a rendering plugin
     if (is_object($render_plugin)) {    
       $content = $render_plugin->render($path);
@@ -314,9 +312,6 @@ class DiogenesBarrel extends DiogenesPage
     // favicon
     if ($bbarrel->options->favicon)
       array_push($this->head, '<link rel="icon" href="'.$this->urlSite("", $bbarrel->options->favicon).'" type="image/png" />');
-
-    // RSS feed
-    array_push($this->head, '<link rel="alternate" type="text/xml" title="'.stripslashes($bbarrel->options->title).'" href="'.$this->urlSite("admin", "rss").'" />');
   }
 
 
@@ -357,9 +352,101 @@ class DiogenesBarrel extends DiogenesPage
     if (!isset($this->table_menu))
       return;
    
+    // all menu entries from database
+    $mcache = $this->menuRead();
+
+    // try to figure out the current MID from the current PID
+    // and build filiation
+    $filiation = array();
+    foreach ($mcache as $mid => $mentry)
+    {
+      if ($mentry['pid'] == $PID)
+        $filiation = $this->menuToRoot($mcache, $mid, $filiation);
+    }
+
     // add the user-defined part of the menu
-    $bmenu = new Diogenes_Barrel_Menu($this->dbh, $this->table_menu);
-    $this->menu = array_merge($this->menu, $bmenu->makeMenu($PID, $this->barrel->options->menu_min_level, array($this, 'urlSiteByPid')));
+    $this->menu = array_merge($this->menu,$this->menuRecurse($mcache,0,$filiation,0));
+  }
+
+
+  /** Return the filiation to get to the root element.
+   *
+   * @param mcache
+   * @param MID
+   * @param path
+   */
+  function menuToRoot($mcache, $MID, $path) {
+    /* add ourself to the path */
+    array_push($path,$MID);
+
+    if ($MID) {
+      /* recursion */
+      return $this->menuToRoot($mcache, $mcache[$MID]['parent'], $path);
+    } else {
+      /* termination */
+      return $path;
+    }
+  }
+
+
+  /** Recursively add menu entries
+   *
+   * @param mcache
+   * @param MIDpere
+   * @param filiation
+   * @param level
+   */
+  function menuRecurse($mcache, $MIDpere, $filiation, $level) {
+    // the produced output
+    $out = array();
+
+    foreach ($mcache[$MIDpere]['children'] as $mid)
+    {
+      $mentry = $mcache[$mid];
+//      echo "pid : $pid, location : $location<br/>";
+      $entry = htmlentities(stripslashes($mentry['title']), ENT_QUOTES);
+      if ($mentry['pid'])
+      {
+        $link = $this->urlSite($this->barrel->getLocation($mentry['pid']));
+      } else {
+        $link = $mentry['link'];
+      }
+      // decide whether this menu should be expanded
+      $expanded = ($this->barrel->options->menu_min_level == 0) || 
+                  ($level+1 < $this->barrel->options->menu_min_level) || 
+                   in_array($mid, $filiation);
+      array_push($out, array($level, $entry, $link, $expanded));
+      $out = array_merge($out, $this->menuRecurse($mcache, $mid, $filiation, $level+1));
+    }
+
+    return $out;
+  }
+
+
+  /** Read this barrel's menu entries from database.
+   */
+  function menuRead()
+  {
+    $menu = array();
+    $menu[0]['children'] = array();
+    $res = $this->dbh->query("select MID,MIDpere,title,link,PID from {$this->table_menu} order by ordre");
+    while (list($mid, $parent, $title, $link, $pid) = mysql_fetch_row($res))
+    {
+      $menu[$mid]['parent'] = $parent;
+      $menu[$mid]['title'] = $title;
+      $menu[$mid]['link'] = $link;
+      $menu[$mid]['title'] = $title;
+      $menu[$mid]['pid'] = $pid;
+      if (!is_array($menu[$mid]['children']))
+        $menu[$mid]['children'] = array();
+
+      // register this entry with its parent
+      if (!is_array($menu[$parent]['children']))
+        $menu[$parent]['children'] = array();
+      array_push($menu[$parent]['children'], $mid);
+    }
+    mysql_free_result($res);
+    return $menu;
   }
 
 
@@ -409,7 +496,7 @@ class DiogenesBarrel extends DiogenesPage
     }
     return $path;
   }
-
 
   /** Returns the URL to one of the barrel's pages relative to
    *  the current location.
@@ -424,18 +511,6 @@ class DiogenesBarrel extends DiogenesPage
     return strlen($url) ? $url : "./";
   }
 
-
-  /** Returns the URL to one of the barrel's pages relative to
-   *  the current location.
-   *
-   * @param dir
-   * @param file
-   */
-  function urlSiteByPid($PID, $file = '')
-  {
-    return $this->urlSite($this->barrel->getLocation($PID), $file);
-  }
-
 }
 
 ?>