move menu-related operations to Barrel/Menu.php
authorJeremy Laine <jeremy.laine@m4x.org>
Mon, 5 Jun 2006 09:18:30 +0000 (09:18 +0000)
committerJeremy Laine <jeremy.laine@m4x.org>
Mon, 5 Jun 2006 09:18:30 +0000 (09:18 +0000)
include/Barrel/Menu.php [new file with mode: 0644]
include/admin/menus.php
include/diogenes.barrel.inc.php

diff --git a/include/Barrel/Menu.php b/include/Barrel/Menu.php
new file mode 100644 (file)
index 0000000..000b036
--- /dev/null
@@ -0,0 +1,214 @@
+<?php
+/*
+ * Copyright (C) 2003-2005 Polytechnique.org
+ * http://opensource.polytechnique.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+/** This class is used to generate the menu of a Diogenes barrel page.
+ */
+class Diogenes_Barrel_Menu
+{
+  /** Database handle */
+  var $dbh;
+  
+  /** The database table holding the menus */
+  var $table_menu;
+
+  /** Constructs a Smarty-derived object to display contents within a barrel.
+   *
+   * @param $override_pathinfo 
+   */
+  function Diogenes_Barrel_Menu(&$dbh, $table_menu)
+  {
+    $this->dbh =& $dbh;
+    $this->table_menu = $table_menu;
+  }
+
+  /** Delete the specified entry.
+   *
+   * @param $MID
+   * @param $caller
+   */
+  function deleteEntry($MID, &$caller)
+  {
+    if (mysql_num_rows($this->dbh->query("SELECT MID FROM {$this->table_menu} WHERE MIDpere=$MID")) > 0) {
+      $caller->info(__("The selected menu has child items, please remove them first."));
+      return;
+    }
+
+    /* erase the current entry */
+    $this->dbh->query("DELETE FROM {$this->table_menu} WHERE MID=$MID");
+
+    /* renumber the other menu entries so that they are between 1 and the number of entries */
+    $res = $this->dbh->query("SELECT MID FROM {$this->table_menu} WHERE MIDpere=$MIDpere ORDER BY ordre");
+    $i = 0;
+    while (list($MIDtoorder) = mysql_fetch_array($res)) {
+      $i++;
+      $this->dbh->query("UPDATE {$this->table_menu} SET ordre=$i WHERE MID=$MIDtoorder");
+    }
+    mysql_free_result($res);
+  }
+
+
+  /** Get the maximum child index for a given menu entry.
+   */
+  function maxChildIndex($MIDpere)
+  {
+    $res=$this->dbh->query("SELECT MAX(ordre) from {$this->table_menu} where MIDpere=$MIDpere");
+    list($maxOrdre)=mysql_fetch_row($res);
+    mysql_free_result($res);
+    return $maxOrdre;
+  }
+
+
+  /** Build the user-defined part of the menu.
+   *
+   * @param $PID
+   * @param $pid_to_url
+   */
+  function makeMenu($PID, $min_level, $pid_to_url)
+  {
+    // 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);
+    }
+
+    $menu = $this->menuRecurse($mcache, 0, $filiation, 0, $min_level, $pid_to_url);
+
+    return $menu;
+  }
+
+
+  /** 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
+   * @param min_level
+   * @param pid_to_url
+   */
+  function menuRecurse($mcache, $MIDpere, $filiation, $level, $min_level, $pid_to_url) {
+    // 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 = call_user_func($pid_to_url, $mentry['pid']);
+      } else {
+        $link = $mentry['link'];
+      }
+      // decide whether this menu should be expanded
+      $expanded = ($min_level == 0) || 
+                  ($level+1 < $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, $min_level, $pid_to_url));
+    }
+
+    return $out;
+  }
+
+
+  /** Read this barrel's menu entries from database.
+   */
+  function menuRead()
+  {
+    $menu = 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;
+  }
+
+
+  /**
+   * Swap entries $a and $b within $parent.
+   *
+   * @param $parent
+   * @param $a
+   * @param $b
+   */
+  function swapEntries($parent, $a, $b)
+  {
+    $res = $this->dbh->query("SELECT MID from {$this->table_menu} where MIDpere=$parent and (ordre=$a or ordre=$b) ORDER BY ordre");
+    /* make sure that $a <= $b */
+    if ($a > $b)
+    {
+      $c = $a;
+      $a = $b;
+      $b = $c;
+    }
+    /* perform swap */
+    list($MIDa) = mysql_fetch_row($res);
+    list($MIDb) = mysql_fetch_row($res);
+    mysql_free_result($res);
+
+    $this->dbh->query("UPDATE {$this->table_menu} SET ordre=$b WHERE MID=$MIDa");
+    $this->dbh->query("UPDATE {$this->table_menu} SET ordre=$a WHERE MID=$MIDb");
+  }
+
+}
+
+?>
index 269c4c7..5fd313b 100644 (file)
@@ -1,37 +1,15 @@
 <?php
 require_once 'diogenes.common.inc.php';
 require_once 'diogenes.admin.inc.php';
+require_once 'Barrel/Menu.php';
 
 $page = new DiogenesAdmin;
 $bbarrel =& $page->barrel;
+$bmenu = new Diogenes_Barrel_Menu($globals->db, $bbarrel->table_menu);
 
 // the id of the parent menu
 $MIDpere = isset($_REQUEST['MIDpere']) ? $_REQUEST['MIDpere'] : 0;
 
-/**
- * This function swaps entries $a and $b within $parent.
- */
-function swapentries($parent,$a,$b,$table_menu)
-{
-  global $globals;
-  $res = $globals->db->query("SELECT MID from $table_menu where MIDpere=$parent and (ordre=$a or ordre=$b) ORDER BY ordre");
-  /* make sure that $a <= $b */
-  if ($a > $b)
-  {
-    $c = $a;
-    $a = $b;
-    $b = $c;
-  }
-  /* perform swap */
-  list($MIDa) = mysql_fetch_row($res);
-  list($MIDb) = mysql_fetch_row($res);
-  mysql_free_result($res);
-
-  $globals->db->query("UPDATE $table_menu SET ordre=$b WHERE MID=$MIDa");
-  $globals->db->query("UPDATE $table_menu SET ordre=$a WHERE MID=$MIDb");
-}
-
-
 //// start constructing the page
 
 $page->assign('greeting',__("The site's menus"));
@@ -41,34 +19,19 @@ switch ($action) {
 /* we want to erase the current entry */
 case "supprimer":
   $MID = $_REQUEST['MID'];
-  if (mysql_num_rows($globals->db->query("SELECT MID FROM {$bbarrel->table_menu} WHERE MIDpere=$MID")) > 0) {
-    $page->info(__("The selected menu has child items, please remove them first."));
-    break;
-  }
-
-  /* erase the current entry */
-  $globals->db->query("DELETE FROM {$bbarrel->table_menu} WHERE MID=$MID");
-
-  /* renumber the other menu entries so that they are between 1 and the number of entries */
-  $res = $globals->db->query("SELECT MID FROM {$bbarrel->table_menu} WHERE MIDpere=$MIDpere ORDER BY ordre");
-  $i = 0;
-  while (list($MIDtoorder) = mysql_fetch_array($res)) {
-    $i++;
-    $globals->db->query("UPDATE {$bbarrel->table_menu} SET ordre=$i WHERE MID=$MIDtoorder");
-  }
-  mysql_free_result($res);
+  $bmenu->deleteEntry($MID, $page);
   break;
 
 /* bring an entry up in the menu */
 case "remonter":
   $ordre = $_REQUEST['ordre'];
-  swapentries($MIDpere,$ordre-1,$ordre,$bbarrel->table_menu);
+  $bmenu->swapEntries($MIDpere, $ordre-1, $ordre);
   break;
 
 /* push an entry down in the menu */
 case "descendre":
   $ordre = $_REQUEST['ordre'];
-  swapentries($MIDpere,$ordre,$ordre+1,$bbarrel->table_menu);
+  $bmenu->swapEntries($MIDpere, $ordre, $ordre+1);
   break;
 
 /* create or update a menu entry */
@@ -90,8 +53,7 @@ case "modifier":
     $MID = $_REQUEST['MID'];
     $title = $_REQUEST['title'];
     if ($MID == 0) {
-      $res=$globals->db->query("SELECT MAX(ordre) from {$bbarrel->table_menu} where MIDpere=$MIDpere");
-      list($ordre) = mysql_fetch_row($res);
+      $ordre = $bmenu->maxChildIndex($MIDpere);
       $ordre++;
       $globals->db->query("INSERT INTO {$bbarrel->table_menu} SET MIDpere='$MIDpere',ordre='$ordre',title='$title',link='$link',pid='$pid'");
       $MID = mysql_insert_id();
@@ -168,9 +130,7 @@ case "editer":
 }
 
 // get the maximum order
-$res=$globals->db->query("SELECT MAX(ordre) from {$bbarrel->table_menu} where MIDpere=$MIDpere");
-list($maxOrdre)=mysql_fetch_row($res);
-mysql_free_result($res);
+$maxOrdre = $bmenu->maxChildIndex($MIDpere);
 
 // retrieve the entries
 $res = $globals->db->query("SELECT m.MID,m.ordre,m.title,m.link,m.PID,p.title ".
@@ -208,8 +168,8 @@ while (list($MID,$ordre,$title,$link,$PID,$ptitle) = mysql_fetch_row($res)) {
 mysql_free_result($res);
 
 // all menu entries from database
-$mcache = $page->menuRead();
-$filiation = $page->menuToRoot($mcache,$MIDpere,array());
+$mcache = $bmenu->menuRead();
+$filiation = $bmenu->menuToRoot($mcache,$MIDpere,array());
 $menubar = array();
 foreach($filiation as $mykey=>$myval) {
   if ($myval == 0) {
index bf800e0..f3a27ae 100644 (file)
@@ -21,6 +21,7 @@
 
 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
@@ -354,99 +355,8 @@ class DiogenesBarrel extends DiogenesPage
       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
-    $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();
-    $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;
+    $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')));
   }
 
 
@@ -496,7 +406,7 @@ class DiogenesBarrel extends DiogenesPage
     }
     return $path;
   }
+
 
   /** Returns the URL to one of the barrel's pages relative to
    *  the current location.
@@ -511,6 +421,18 @@ 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);
+  }
+
 }
 
 ?>