Menu.php

00001 <?php
00002 /*
00003  * Copyright (C) 2003-2005 Polytechnique.org
00004  * http://opensource.polytechnique.org/
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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     /* erase the current entry */
00056     $this->dbh->query("DELETE FROM {$this->table_menu} WHERE MID=$MID");
00057 
00058     /* renumber the other menu entries so that they are between 1 and the number of entries */
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     // all menu entries from database
00077     $mcache = $this->menuRead();
00078 
00079     // try to figure out the current MID from the current PID
00080     // and build filiation
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     /* add ourself to the path */
00115     array_push($path,$MID);
00116 
00117     if ($MID) {
00118       /* recursion */
00119       return $this->menuToRoot($mcache, $mcache[$MID]['parent'], $path);
00120     } else {
00121       /* termination */
00122       return $path;
00123     }
00124   }
00125 
00126 
00136   function menuRecurse($mcache, $MIDpere, $filiation, $level, $min_level, $pid_to_url) {
00137     // the produced output
00138     $out = array();
00139 
00140     foreach ($mcache[$MIDpere]['children'] as $mid)
00141     {
00142       $mentry = $mcache[$mid];
00143 //      echo "pid : $pid, location : $location<br/>";
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       // decide whether this menu should be expanded
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       // register this entry with its parent
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     /* make sure that $a <= $b */
00202     if ($a > $b)
00203     {
00204       $c = $a;
00205       $a = $b;
00206       $b = $c;
00207     }
00208     /* perform swap */
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     // build SQL string
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 ?>

Generated on Fri Jan 11 01:20:08 2008 for Diogenes by  doxygen 1.5.1