X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2FBarrel%2FMenu.php;h=e5279c9d3154bfa78bc2f866a36ff07861b2bd1e;hb=7d7400eafcf18fddfd6df14b7d0a3a1700455ea7;hp=000b036e167c54913b84465a718e0446cb0a6817;hpb=cf637ef87f5fb7d80b62c74b6ecbaae13ecc5da3;p=diogenes.git diff --git a/include/Barrel/Menu.php b/include/Barrel/Menu.php index 000b036..e5279c9 100644 --- a/include/Barrel/Menu.php +++ b/include/Barrel/Menu.php @@ -42,9 +42,10 @@ class Diogenes_Barrel_Menu /** Delete the specified entry. * * @param $MID + * @param $parent * @param $caller */ - function deleteEntry($MID, &$caller) + function deleteEntry($MID, $parent, &$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.")); @@ -55,7 +56,7 @@ class Diogenes_Barrel_Menu $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"); + $res = $this->dbh->query("SELECT MID FROM {$this->table_menu} WHERE MIDpere=$parent ORDER BY ordre"); $i = 0; while (list($MIDtoorder) = mysql_fetch_array($res)) { $i++; @@ -65,17 +66,6 @@ class Diogenes_Barrel_Menu } - /** 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 @@ -101,6 +91,19 @@ class Diogenes_Barrel_Menu } + /** Return the maximum index for children of a given entry. + * + * @param $parent + */ + function maxChildIndex($parent) + { + $res=$this->dbh->query("SELECT MAX(ordre) from {$this->table_menu} where MIDpere=$parent"); + list($maxOrdre)=mysql_fetch_row($res); + mysql_free_result($res); + return $maxOrdre; + } + + /** Return the filiation to get to the root element. * * @param mcache @@ -162,14 +165,16 @@ class Diogenes_Barrel_Menu 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[0]['children'] = array(); + $res = $this->dbh->query("select MID,MIDpere,title,link,PID,ordre from {$this->table_menu} order by ordre"); + while (list($mid, $parent, $title, $link, $pid, $ordre) = mysql_fetch_row($res)) { $menu[$mid]['parent'] = $parent; $menu[$mid]['title'] = $title; $menu[$mid]['link'] = $link; $menu[$mid]['title'] = $title; $menu[$mid]['pid'] = $pid; + $menu[$mid]['ordre'] = $ordre; if (!is_array($menu[$mid]['children'])) $menu[$mid]['children'] = array(); @@ -209,6 +214,42 @@ class Diogenes_Barrel_Menu $this->dbh->query("UPDATE {$this->table_menu} SET ordre=$a WHERE MID=$MIDb"); } + + /** + * Write an entry to database. If $mid is 0, a new entry is created. + * + * @param $mid + * @param $props + */ + function writeEntry($mid, $props) + { + if ($mid == 0) { + $props['ordre'] = $this->maxChildIndex($props['parent']) + 1; + } + + // build SQL string + $nprops = array('parent' => 'MIDpere', 'ordre' => 'ordre', 'title' => 'title', 'link' => 'link', 'pid' => 'pid'); + $sprops = ""; + foreach($nprops as $prop => $dbkey) + { + if (isset($props[$prop])) + { + $val = $props[$prop]; + $sprops .= "$dbkey='$val', "; + } + } + if (!$sprops) + return; + $sprops = substr($sprops, 0, -2); + if ($mid == 0) { + $this->dbh->query("INSERT INTO {$this->table_menu} SET $sprops"); + $mid = mysql_insert_id(); + } else { + $this->dbh->query("UPDATE {$this->table_menu} SET $sprops WHERE MID=$mid"); + } + return $mid; + } + } ?>