00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00024 class Diogenes_Barrel_Menu
00025 {
00027 var $dbh;
00028
00030 var $table_menu;
00031
00036 function Diogenes_Barrel_Menu(&$dbh, $table_menu)
00037 {
00038 $this->dbh =& $dbh;
00039 $this->table_menu = $table_menu;
00040 }
00041
00048 function deleteEntry($MID, $parent, &$caller)
00049 {
00050 if (mysql_num_rows($this->dbh->query("SELECT MID FROM {$this->table_menu} WHERE MIDpere=$MID")) > 0) {
00051 $caller->info(__("The selected menu has child items, please remove them first."));
00052 return;
00053 }
00054
00055
00056 $this->dbh->query("DELETE FROM {$this->table_menu} WHERE MID=$MID");
00057
00058
00059 $res = $this->dbh->query("SELECT MID FROM {$this->table_menu} WHERE MIDpere=$parent ORDER BY ordre");
00060 $i = 0;
00061 while (list($MIDtoorder) = mysql_fetch_row($res)) {
00062 $i++;
00063 $this->dbh->query("UPDATE {$this->table_menu} SET ordre=$i WHERE MID=$MIDtoorder");
00064 }
00065 mysql_free_result($res);
00066 }
00067
00068
00074 function makeMenu($PID, $min_level, $pid_to_url)
00075 {
00076
00077 $mcache = $this->menuRead();
00078
00079
00080
00081 $filiation = array();
00082 foreach ($mcache as $mid => $mentry)
00083 {
00084 if ($mentry['pid'] == $PID)
00085 $filiation = $this->menuToRoot($mcache, $mid, $filiation);
00086 }
00087
00088 $menu = $this->menuRecurse($mcache, 0, $filiation, 0, $min_level, $pid_to_url);
00089
00090 return $menu;
00091 }
00092
00093
00098 function maxChildIndex($parent)
00099 {
00100 $res=$this->dbh->query("SELECT MAX(ordre) from {$this->table_menu} where MIDpere=$parent");
00101 list($maxOrdre)=mysql_fetch_row($res);
00102 mysql_free_result($res);
00103 return $maxOrdre;
00104 }
00105
00106
00113 function menuToRoot($mcache, $MID, $path) {
00114
00115 array_push($path,$MID);
00116
00117 if ($MID) {
00118
00119 return $this->menuToRoot($mcache, $mcache[$MID]['parent'], $path);
00120 } else {
00121
00122 return $path;
00123 }
00124 }
00125
00126
00136 function menuRecurse($mcache, $MIDpere, $filiation, $level, $min_level, $pid_to_url) {
00137
00138 $out = array();
00139
00140 foreach ($mcache[$MIDpere]['children'] as $mid)
00141 {
00142 $mentry = $mcache[$mid];
00143
00144 $entry = htmlentities(stripslashes($mentry['title']), ENT_QUOTES);
00145 if ($mentry['pid'])
00146 {
00147 $link = call_user_func($pid_to_url, $mentry['pid']);
00148 } else {
00149 $link = $mentry['link'];
00150 }
00151
00152 $expanded = ($min_level == 0) ||
00153 ($level+1 < $min_level) ||
00154 in_array($mid, $filiation);
00155 array_push($out, array($level, $entry, $link, $expanded));
00156 $out = array_merge($out, $this->menuRecurse($mcache, $mid, $filiation, $level+1, $min_level, $pid_to_url));
00157 }
00158
00159 return $out;
00160 }
00161
00162
00165 function menuRead()
00166 {
00167 $menu = array();
00168 $menu[0]['children'] = array();
00169 $res = $this->dbh->query("select MID,MIDpere,title,link,PID,ordre from {$this->table_menu} order by ordre");
00170 while (list($mid, $parent, $title, $link, $pid, $ordre) = mysql_fetch_row($res))
00171 {
00172 $menu[$mid]['parent'] = $parent;
00173 $menu[$mid]['title'] = $title;
00174 $menu[$mid]['link'] = $link;
00175 $menu[$mid]['title'] = $title;
00176 $menu[$mid]['pid'] = $pid;
00177 $menu[$mid]['ordre'] = $ordre;
00178 if (!is_array($menu[$mid]['children']))
00179 $menu[$mid]['children'] = array();
00180
00181
00182 if (!is_array($menu[$parent]['children']))
00183 $menu[$parent]['children'] = array();
00184 array_push($menu[$parent]['children'], $mid);
00185 }
00186 mysql_free_result($res);
00187 return $menu;
00188 }
00189
00190
00198 function swapEntries($parent, $a, $b)
00199 {
00200 $res = $this->dbh->query("SELECT MID from {$this->table_menu} where MIDpere=$parent and (ordre=$a or ordre=$b) ORDER BY ordre");
00201
00202 if ($a > $b)
00203 {
00204 $c = $a;
00205 $a = $b;
00206 $b = $c;
00207 }
00208
00209 list($MIDa) = mysql_fetch_row($res);
00210 list($MIDb) = mysql_fetch_row($res);
00211 mysql_free_result($res);
00212
00213 $this->dbh->query("UPDATE {$this->table_menu} SET ordre=$b WHERE MID=$MIDa");
00214 $this->dbh->query("UPDATE {$this->table_menu} SET ordre=$a WHERE MID=$MIDb");
00215 }
00216
00217
00224 function writeEntry($mid, $props)
00225 {
00226 if ($mid == 0) {
00227 $props['ordre'] = $this->maxChildIndex($props['parent']) + 1;
00228 }
00229
00230
00231 $nprops = array('parent' => 'MIDpere', 'ordre' => 'ordre', 'title' => 'title', 'link' => 'link', 'pid' => 'pid');
00232 $sprops = "";
00233 foreach($nprops as $prop => $dbkey)
00234 {
00235 if (isset($props[$prop]))
00236 {
00237 $val = $props[$prop];
00238 $sprops .= "$dbkey='$val', ";
00239 }
00240 }
00241 if (!$sprops)
00242 return;
00243 $sprops = substr($sprops, 0, -2);
00244 if ($mid == 0) {
00245 $this->dbh->query("INSERT INTO {$this->table_menu} SET $sprops");
00246 $mid = mysql_insert_id();
00247 } else {
00248 $this->dbh->query("UPDATE {$this->table_menu} SET $sprops WHERE MID=$mid");
00249 }
00250 return $mid;
00251 }
00252
00253 }
00254
00255 ?>