From cf637ef87f5fb7d80b62c74b6ecbaae13ecc5da3 Mon Sep 17 00:00:00 2001 From: Jeremy Laine Date: Mon, 5 Jun 2006 09:18:30 +0000 Subject: [PATCH] move menu-related operations to Barrel/Menu.php --- include/Barrel/Menu.php | 214 ++++++++++++++++++++++++++++++++++++++++ include/admin/menus.php | 58 ++--------- include/diogenes.barrel.inc.php | 110 +++------------------ 3 files changed, 239 insertions(+), 143 deletions(-) create mode 100644 include/Barrel/Menu.php diff --git a/include/Barrel/Menu.php b/include/Barrel/Menu.php new file mode 100644 index 0000000..000b036 --- /dev/null +++ b/include/Barrel/Menu.php @@ -0,0 +1,214 @@ +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
"; + $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"); + } + +} + +?> diff --git a/include/admin/menus.php b/include/admin/menus.php index 269c4c7..5fd313b 100644 --- a/include/admin/menus.php +++ b/include/admin/menus.php @@ -1,37 +1,15 @@ 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) { diff --git a/include/diogenes.barrel.inc.php b/include/diogenes.barrel.inc.php index bf800e0..f3a27ae 100644 --- a/include/diogenes.barrel.inc.php +++ b/include/diogenes.barrel.inc.php @@ -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
"; - $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); + } + } ?> -- 2.1.4