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"); } } ?>