3 * Copyright (C) 2003-2005 Polytechnique.org
4 * http://opensource.polytechnique.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /** This class is used to generate the menu of a Diogenes barrel page.
24 class Diogenes_Barrel_Menu
26 /** Database handle */
29 /** The database table holding the menus */
32 /** Constructs a Smarty-derived object to display contents within a barrel.
34 * @param $override_pathinfo
36 function Diogenes_Barrel_Menu(&$dbh, $table_menu)
39 $this->table_menu
= $table_menu;
42 /** Delete the specified entry.
48 function deleteEntry($MID, $parent, &$caller)
50 if (mysql_num_rows($this->dbh
->query("SELECT MID FROM {$this->table_menu} WHERE MIDpere=$MID")) > 0) {
51 $caller->info(__("The selected menu has child items, please remove them first."));
55 /* erase the current entry */
56 $this->dbh
->query("DELETE FROM {$this->table_menu} WHERE MID=$MID");
58 /* renumber the other menu entries so that they are between 1 and the number of entries */
59 $res = $this->dbh
->query("SELECT MID FROM {$this->table_menu} WHERE MIDpere=$parent ORDER BY ordre");
61 while (list($MIDtoorder) = mysql_fetch_array($res)) {
63 $this->dbh
->query("UPDATE {$this->table_menu} SET ordre=$i WHERE MID=$MIDtoorder");
65 mysql_free_result($res);
69 /** Build the user-defined part of the menu.
74 function makeMenu($PID, $min_level, $pid_to_url)
76 // all menu entries from database
77 $mcache = $this->menuRead();
79 // try to figure out the current MID from the current PID
80 // and build filiation
82 foreach ($mcache as $mid => $mentry)
84 if ($mentry['pid'] == $PID)
85 $filiation = $this->menuToRoot($mcache, $mid, $filiation);
88 $menu = $this->menuRecurse($mcache, 0, $filiation, 0, $min_level, $pid_to_url);
94 /** Return the maximum index for children of a given entry.
98 function maxChildIndex($parent)
100 $res=$this->dbh
->query("SELECT MAX(ordre) from {$this->table_menu} where MIDpere=$parent");
101 list($maxOrdre)=mysql_fetch_row($res);
102 mysql_free_result($res);
107 /** Return the filiation to get to the root element.
113 function menuToRoot($mcache, $MID, $path) {
114 /* add ourself to the path */
115 array_push($path,$MID);
119 return $this->menuToRoot($mcache, $mcache[$MID]['parent'], $path);
127 /** Recursively add menu entries
136 function menuRecurse($mcache, $MIDpere, $filiation, $level, $min_level, $pid_to_url) {
137 // the produced output
140 foreach ($mcache[$MIDpere]['children'] as $mid)
142 $mentry = $mcache[$mid];
143 // echo "pid : $pid, location : $location<br/>";
144 $entry = htmlentities(stripslashes($mentry['title']), ENT_QUOTES
);
147 $link = call_user_func($pid_to_url, $mentry['pid']);
149 $link = $mentry['link'];
151 // decide whether this menu should be expanded
152 $expanded = ($min_level == 0) ||
153 ($level+
1 < $min_level) ||
154 in_array($mid, $filiation);
155 array_push($out, array($level, $entry, $link, $expanded));
156 $out = array_merge($out, $this->menuRecurse($mcache, $mid, $filiation, $level+
1, $min_level, $pid_to_url));
163 /** Read this barrel's menu entries from database.
168 $menu[0]['children'] = array();
169 $res = $this->dbh
->query("select MID,MIDpere,title,link,PID,ordre from {$this->table_menu} order by ordre");
170 while (list($mid, $parent, $title, $link, $pid, $ordre) = mysql_fetch_row($res))
172 $menu[$mid]['parent'] = $parent;
173 $menu[$mid]['title'] = $title;
174 $menu[$mid]['link'] = $link;
175 $menu[$mid]['title'] = $title;
176 $menu[$mid]['pid'] = $pid;
177 $menu[$mid]['ordre'] = $ordre;
178 if (!is_array($menu[$mid]['children']))
179 $menu[$mid]['children'] = array();
181 // register this entry with its parent
182 if (!is_array($menu[$parent]['children']))
183 $menu[$parent]['children'] = array();
184 array_push($menu[$parent]['children'], $mid);
186 mysql_free_result($res);
192 * Swap entries $a and $b within $parent.
198 function swapEntries($parent, $a, $b)
200 $res = $this->dbh
->query("SELECT MID from {$this->table_menu} where MIDpere=$parent and (ordre=$a or ordre=$b) ORDER BY ordre");
201 /* make sure that $a <= $b */
209 list($MIDa) = mysql_fetch_row($res);
210 list($MIDb) = mysql_fetch_row($res);
211 mysql_free_result($res);
213 $this->dbh
->query("UPDATE {$this->table_menu} SET ordre=$b WHERE MID=$MIDa");
214 $this->dbh
->query("UPDATE {$this->table_menu} SET ordre=$a WHERE MID=$MIDb");
219 * Write an entry to database. If $mid is 0, a new entry is created.
224 function writeEntry($mid, $props)
227 $props['ordre'] = $this->maxChildIndex($props['parent']) +
1;
231 $nprops = array('parent' => 'MIDpere', 'ordre' => 'ordre', 'title' => 'title', 'link' => 'link', 'pid' => 'pid');
233 foreach($nprops as $prop => $dbkey)
235 if (isset($props[$prop]))
237 $val = $props[$prop];
238 $sprops .= "$dbkey='$val', ";
243 $sprops = substr($sprops, 0, -2);
245 $this->dbh
->query("INSERT INTO {$this->table_menu} SET $sprops");
246 $mid = mysql_insert_id();
248 $this->dbh
->query("UPDATE {$this->table_menu} SET $sprops WHERE MID=$mid");