Location: PHPKode > scripts > phpMyAccess > phpmyaccess-1.5.3/phpmyaccess/user_menu_items.php
<?php
// XML spec conformance tested OK

function MyHeader ($menutitle) {
echo("<?xml version=\"1.0\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
  <title>$menutitle</title>
</head>
<body bgcolor=\"#ffeb10\">
  <div class=\"main\">
  <h3>$menutitle</h3>
<hr width=\"30%\" align=\"left\"></hr><br/>\n");
}

function displaypage() {
  reset($GLOBALS['qryusers']);
  reset($GLOBALS['qrypages']);
  reset($GLOBALS['qryumenu']);
  reset($GLOBALS['qrymenuitems']);
  // select user
  if (!$_POST['muname']){
     $_POST['muname'] = $GLOBALS['qryusers'][0];
  }
  echo "<form action=\"user_menu_items.php\" method=\"post\">\n";
  echo "<input type=\"hidden\" name=\"vertpos\" value=\"". $GLOBALS['opt']['vertpos'] . "\" />\n";
  echo "<input type=\"hidden\" name=\"horpos\" value=\"". $GLOBALS['opt']['horpos'] . "\" />\n";
  echo "<input type=\"hidden\" name=\"muname\" value=\"". $_POST['muname'] . "\" />\n";
  echo "    <select name=\"muname\" >\n";
  while (list ($key, $val) = each ($GLOBALS['qryusers'])) {
     if ($_POST['muname'] == $val) {
        $w = " selected=\"selected\" ";
     } else {
        $w = '';
     }
     echo "     <option value=\"$val\" $w>$val</option>\n";
  }
  echo '    </select>'."\n";
  echo "<input type=\"submit\" name=\"select\" value=\"select\"/>";
  echo "</form>\n";
  //Column change form
  echo "<form action=\"user_menu_items.php\" method=\"post\">\n";
  echo "<input type=\"hidden\" name=\"vertpos\" value=\"". $GLOBALS['opt']['vertpos'] . "\" />\n";
  echo "<input type=\"hidden\" name=\"horpos\" value=\"". $GLOBALS['opt']['horpos'] . "\" />\n";
  echo "<input type=\"hidden\" name=\"muname\" value=\"". $_POST['muname'] . "\" />\n";
  echo "    <select name=\"column\" >\n";
  $hcount=0;
  while (list ($key, $val) = each ($GLOBALS['qrypages'])) {
     #if (($_POST['column'] == $hcount) || ((!$_POST['column']) && ($GLOBALS['opt']['horpos'] == $hcount))) {
     if ($GLOBALS['opt']['horpos'] == $hcount) {
        $w = " selected=\"selected\" ";
     } else {
        $w = '';
     }	  
     echo "     <option value=\"$hcount\" $w>$val</option>\n";
     $hcount++;
  }
  echo '    </select>'."\n";
  echo "<input type=\"submit\" name=\"Edit\" value=\"edit\"/>";
  echo "</form>\n";
  reset($GLOBALS['qrypages']);
  // select menu items for page
  echo "<form action=\"user_menu_items.php\" method=\"post\">\n";
  echo "<input type=\"hidden\" name=\"muname\" value=\"". $_POST['muname'] . "\" />\n";
  echo "<input type=\"hidden\" name=\"vertpos\" value=\"". $GLOBALS['opt']['vertpos'] . "\" />\n";
  echo "<input type=\"hidden\" name=\"horpos\" value=\"". $GLOBALS['opt']['horpos'] . "\" />\n";
  echo "  <table border=\"2\">\n";
  echo "   <tr>\n";
  echo "   <th>Menu item</th>\n";
  while (list ($key, $name) = each ($GLOBALS['qrypages'])) {
    echo "   <th>" . $name . "</th>\n";
  }
  echo "   </tr>\n";
  reset($GLOBALS['qrypages']);
  $vcount = 0;
  $hcount=0;
  while (list ($key, $val) = each ($GLOBALS['qrymenuitems'])) {
     echo "   <tr>\n     <td>$val</td>\n";
     while (list ($num, $name) = each ($GLOBALS['qrypages'])) {
         $upage=$GLOBALS['qryumenu'][$_POST['muname']][$name];
         $tstvar = $_POST['muname'] . "_". $upage . "_" . $key;
         $tmpvar = $name . "_" . $key;
         echo "    <td>";
         if($upage) {
           if (($vcount >= $GLOBALS['opt']['vertpos']) && ($vcount < $GLOBALS['opt']['vertpos'] + $GLOBALS['opt']['lpp']) && ($hcount == $GLOBALS['opt']['horpos']) ) {
              echo "<select name=\"$tmpvar\">\n";
              for ($l=0; $l <= $GLOBALS['opt']['menusteps']; $l++) {
                echo "     <option value=\"$l\" ";
				if ($GLOBALS['qryumenit'][$tstvar]['val'] == $l && $l <>0 ) {
                   echo " selected=\"selected\" ";
                }
                echo ">$l</option>\n";
              }
              echo "    </select>\n";
           } else {
              echo $GLOBALS['qryumenit'][$tstvar]['val'];
           }
         } else {
            echo "-";
         }
         echo "</td>\n";
         $hcount++;
     }
     echo "   </tr>\n";
     reset($GLOBALS['qrypages']);
     $hcount=0;
     $vcount++;
  }
  echo "  </table>\n";
  echo "  <hr  width=\"30%\" align=\"left\"/>\n";
  echo "  <table>\n";
  echo "  <tr>\n";
  echo "    <td align=\"left\">\n";
  echo "      <input type=\"submit\" name=\"Prev\" value=\"prev\"/>";
  echo "    </td>\n";
  echo "    <td align=\"left\">\n";
  echo "      <input type=\"submit\" name=\"update\" value=\"update\"/>";
  echo "    </td>\n";
  echo "    <td align=\"left\">\n";
  echo "      <input type=\"submit\" name=\"Next\" value=\"next\"/>&nbsp; H-Pos. : " . $GLOBALS['opt']['horpos'] . "  V-Pos. : " . $GLOBALS['opt']['vertpos'];
  echo "    </td>\n";
  echo "   </tr>\n";
  echo "  </table>\n";
  echo "</form>\n";
}

function DoFooter() {
echo " </div>
  <p/>
  <hr  width=\"30%\" align=\"left\"/>
  <table>
   <tr>
     <td><a href=\"index.php\" name=\"link\">Main Page</a></td>
     <td> &nbsp;&nbsp;&nbsp; </td>
     <td><a href=\"index.php?page=menus\" name=\"link\">Menus Maintenance Page</a></td>
   </tr>
  </table>\n";
  echo "<br/>\n<b> User : ".$GLOBALS['auth_user']."</b>\n<br/>\n";
  echo "</body>\n</html>\n";
}

function Dbconnect() {
  $GLOBALS['dbl'] = mysql_connect($GLOBALS['opt']['hn'],$GLOBALS['opt']['un'],$GLOBALS['opt']['pw'])
    or die ('<h1>Could not connect to MySQL</h1>'."\n");
  if (!isset($GLOBALS['opt']['db'])) { die ("<h1>No Database defined</h1>\n</body>\n</html>\n"); }
}

function DoAuth () {
  if ($GLOBALS['auth'] >= 1) {
     $GLOBALS['auth_level'] = 0;
     include 'auth.inc.php';
     if ((!$GLOBALS['auth_level']) || ($GLOBALS['auth_level'] < $GLOBALS['auth'])) {
       // this can be a function to display something nicer, of course
       pma_errpage("Access Denied");
     }
  }
}

function GetUserlist () {
  $query = "Select " . $GLOBALS['opt']['userfldname']. " from " . $GLOBALS['opt']['userdb'] . " ORDER BY " . $GLOBALS['opt']['userfldname'] . " ASC";
  $res = mysql_db_query($GLOBALS['opt']['db'],$query);
  if (!$res) {
     pma_errpage ("User SELECT " . mysql_errno().": ".mysql_error()."Bad Query or DB error");
  }
  while(list($uname) = mysql_fetch_row($res)) {
    $GLOBALS['qryusers'][] = $uname;
    pma_debug ("User : " . $uname);
  }
}

function GetMenuPages () {
  $query = "Select " . $GLOBALS['opt']['pagesfldname'] . ", " . $GLOBALS['opt']['pagesfldId'] . " from " . $GLOBALS['opt']['pagesdb'] . " ORDER BY " . $GLOBALS['opt']['pagesfldname'] . " ASC";
  $res = mysql_db_query($GLOBALS['opt']['db'],$query);
  if (!$res) {
     pma_errpage ("Page SELECT " . mysql_errno().": ".mysql_error()." Bad Query or DB error");
  }
  while(list($name, $id) = mysql_fetch_row($res)) {
    $GLOBALS['qrypages'][$id] = $name;
    pma_debug ($name . " : " . $id );
  }
}

function GetMenuItemList () {
  $query = "Select " . $GLOBALS['opt']['menuitemsId'] . ", " . $GLOBALS['opt']['menuitemsname'] . " from " . $GLOBALS['opt']['menuitemsdb'] . " ORDER BY " . $GLOBALS['opt']['menuitemsname'] . " ASC";
  $res = mysql_db_query($GLOBALS['opt']['db'],$query);
  if (!$res) {
     pma_errpage ("Menu ItemsPage SELECT " . mysql_errno().": ".mysql_error()." Bad Query or DB error");
  }
  while(list($id, $name) = mysql_fetch_row($res)) {
    $GLOBALS['qrymenuitems'][$id]=$name;
    pma_debug ($id . " : " . $name );
  }
}

function GetUserMenuData () {
  $query = "Select " .$GLOBALS['opt']['upagesfldId'] . ", " . $GLOBALS['opt']['upagesfldname'] . ", " .$GLOBALS['opt']['upagesfldpage'] .
               " from " . $GLOBALS['opt']['upagesdb'] . " ORDER BY " . $GLOBALS['opt']['upagesfldname'] . " ASC";
  $res = mysql_db_query($GLOBALS['opt']['db'],$query);
  if (!$res) {
     pma_errpage ("User Page Select " . mysql_errno().": ".mysql_error()." Bad Query or DB error");
  }
  while(list($idno, $logname, $pagename) = mysql_fetch_row($res)) {
    $GLOBALS['qryumenu'][$logname][$pagename]=$idno;
    pma_debug ($logname . " has : $pagename Value : " . $GLOBALS['qryumenu'][$logname][$pagename] );
  }
}

function GetUserMenuItems () {
  $query = "Select " . $GLOBALS['opt']['umenuitemsId'] . ", " . $GLOBALS['opt']['umenuitemmenuid'] . ", " . $GLOBALS['opt']['umenuitemmenuitemid'] . ", " .
            $GLOBALS['opt']['umenuitemorder'] . ", " . $GLOBALS['opt']['umenuitemname'] .
            " from " .$GLOBALS['opt']['umenuitemsdb']  . " ORDER BY " . $GLOBALS['opt']['umenuitemname'] . "," .$GLOBALS['opt']['umenuitemmenuid'] . " ASC";
  $res = mysql_db_query($GLOBALS['opt']['db'],$query);
  if (!$res) {
     pma_errpage ("User Menu Items SELECT " . mysql_errno().": ".mysql_error()." Bad Query or DB error");
  }
  while(list($idno, $menuno, $menuitno, $menuorder, $logname) = mysql_fetch_row($res)) {
    $tmpvar = $logname . "_" . $menuno . "_" . $menuitno;
    $GLOBALS['qryumenit'][$tmpvar]['id'] = $idno;
    $GLOBALS['qryumenit'][$tmpvar]['val'] = $menuorder;
    pma_debug ($logname . "  Menuitem ID : " . $GLOBALS['qrymenuitems'][$GLOBALS['qryumenit'][$tmpvar]['id']] . "  order =" .
                  $GLOBALS['qryumenit'][$tmpvar]['val'] . " Page = " . $GLOBALS['qryumenu'][$menuno] . " - " . $menuno );
  }
}

function DoCreateUmenu ($logname, $menu) {
  $query = "INSERT INTO " . $GLOBALS['opt']['upagesdb'] . " (" . $GLOBALS['opt']['upagesfldname'] . ", " .$GLOBALS['opt']['upagesfldpage']
                 . ") VALUES  ('" . $logname . "', '" . $menu . "')";
  $res = mysql_db_query($GLOBALS['opt']['db'],$query);
  if (!$res) {
     pma_errpage ($query . "User Page INSERT " . mysql_errno().": ".mysql_error()." Bad Query or DB error User Page INSERT");
  }
  $GLOBALS['qryumenu']='';//empty array so it can be cleanly reloaded
  GetUserMenuData();
}

function DoCreateUmenit ($logname, $menu, $num, $menuorder) {
  $query = "INSERT INTO " . $GLOBALS['opt']['umenuitemsdb'] . " (" .$GLOBALS['opt']['umenuitemmenuid'] . ", " . $GLOBALS['opt']['umenuitemmenuitemid'] .
                  ", " . $GLOBALS['opt']['umenuitemorder'] . ", " . $GLOBALS['opt']['umenuitemname'] .
                  ") VALUES  (" . $menu . "," . $num . "," . $menuorder .  ",'" . $logname . "')";
  $res = mysql_db_query($GLOBALS['opt']['db'],$query);
  if (!$res) {
     pma_errpage ($query . "User Menu Item INSERT " . mysql_errno().": ".mysql_error(). " Bad Query or DB error User Menu Item INSERT");
  }
  $GLOBALS['qryumenit']='';//empty array so it can be cleanly reloaded
  GetUserMenuItems();  
}

function DoDeleteUmenu ($umenu_ID) {
  $query = "DELETE FROM " . $GLOBALS['opt']['upagesdb'] . " WHERE " . $GLOBALS['opt']['upagesfldId'] . "=" . $umenu_ID;
  $res = mysql_db_query($GLOBALS['opt']['db'],$query);
  if (!$res) {
     pma_errpage ($query . "User Page DELETE " . mysql_errno().": ".mysql_error()." Bad Query or DB error User Page DELETE");
  }
  $GLOBALS['qryumenu']='';//empty array so it can be cleanly reloaded
  GetUserMenuData();    
}

function DoUpdateUmenit ($umenu_item_ID,$menuorder) {
  $query = "UPDATE " . $GLOBALS['opt']['umenuitemsdb'] . " SET " . $GLOBALS['opt']['umenuitemorder'] . " = " .
                   $menuorder . " WHERE " . $GLOBALS['opt']['umenuitemsId'] . "=" . $umenu_item_ID;
  pma_debug ("DoUpdateUmenuItem : " . $query );
  $res = mysql_db_query($GLOBALS['opt']['db'],$query);
  if (!$res) {
     pma_errpage ("User Page UPDATE " . mysql_errno().": ".mysql_error()." Bad Query or DB error User Page UPDATE");
  }
  $GLOBALS['qryumenit']='';//empty array so it can be cleanly reloaded
  GetUserMenuItems();  
}

function DoDeleteUmenit ($umenu_item_ID) {
  $query = "DELETE FROM " . $GLOBALS['opt']['umenuitemsdb'] . " WHERE " . $GLOBALS['opt']['umenuitemsId'] . "=" . $umenu_item_ID;
  pma_debug ("DoDeleteUmenuItem : " . $query );
  $res = mysql_db_query($GLOBALS['opt']['db'],$query);
  if (!$res) {
     pma_errpage ("User Menu Item DELETE " . mysql_errno().": ".mysql_error()." Bad Query or DB error User Menu Item DELETE");
  }
  $GLOBALS['qryumenit']='';//empty array so it can be cleanly reloaded
  GetUserMenuItems();  
}

function displayvars() {
  reset($GLOBALS['qrypages']);
  reset($GLOBALS['qryusers']);
  while (list ($key, $val) = each ($GLOBALS['qrypages'])) {
     while (list ($num, $name) = each ($GLOBALS['qryusers'])) {
         $tmpvar = $name . "_" . $val;
         echo $tmpvar . " set to : " . $_POST[$tmpvar] . "<br/> \n";
     }
     reset($GLOBALS['qryusers']);
  }
}

function getpostvars() {
  reset($GLOBALS['qrypages']);
  reset($GLOBALS['qryusers']);
  reset($GLOBALS['qryumenit']);
  reset($GLOBALS['qrymenuitems']);
  while (list ($num, $name) = each ($GLOBALS['qrymenuitems'])) {
     while (list ($key, $page) = each ($GLOBALS['qrypages'])) {
         $upage=$GLOBALS['qryumenu'][$_POST['muname']][$page];
         $tmpvar = $page . "_" . $num;
         $tstvar = $_POST['muname'] . "_" . $upage . "_" . $num;
         $web_menu_order = $_POST[$tmpvar];
         // echo $tmpvar . " " . $_POST[$tmpvar]. " : ";
         if($upage) {
           if($web_menu_order <> '0' && isset($_POST[$tmpvar])) {
              if ($GLOBALS['qryumenit'][$tstvar]['val'] == $web_menu_order && $web_menu_order <> 0) {
                 // record exists nothing to do
                 pma_debug ("Exists $name on $page [order table-web - " . $GLOBALS['qryumenit'][$tstvar]['val'] ."-". $web_menu_order . "]" );
              } else if ($GLOBALS['qryumenit'][$tstvar]['val'] <> 0) {
                 // record needs to be updated
                 pma_debug ("Exists $name on $page<br>\n" .$GLOBALS['qryumenit'][$tstvar]['val']. "-". $GLOBALS['qryumenit'][$tstvar]["id"] . " - " . $web_menu_order );
                 DoUpdateUmenit ($GLOBALS['qryumenit'][$tstvar]["id"],$web_menu_order);
              } else {
                 // create it, add to create list
                 pma_debug ("Create $tmpvar ( $name [$num] on $page [$upage]) [order=" . $web_menu_order . "]");
                 DoCreateUmenit ($_POST['muname'], $upage, $num, $web_menu_order);
                 // DoCreateUmenit($name, $val);
              }
           } else if($web_menu_order == '0'){
              if ($GLOBALS['qryumenit'][$tstvar]['val'] <> 0 ) {
                 // record exists delete it, add to delete list
                 pma_debug ("Delete tmpvar ( $name on $page ) [id= " . $GLOBALS['qryumenit'][$tstvar]["id"] ."]");
                 DoDeleteUmenit($GLOBALS['qryumenit'][$tstvar]["id"]);
              } else {
                 // No record
                 pma_debug ("Not present $name on $page ");
              }
           }  else {
              // this one wasn't displayed
              // better not touch it, because it would always be set to 0
           }
         }
     }
     reset($GLOBALS['qrypages']);
  }
}

function DisplayPostvars() {
  while (list ($num, $name) = each ($_POST)) {
     echo $num . " set to : " . $name . "<br/> \n";
  }
}

//-------------MAIN
$opt['pagesdb']='menu_pages';
$opt['pagesfldname']='menu_page_name';
$opt['pagesfldId']='menu_page_ID';

$opt['userdb']='useraccounts';
$opt['userfldname']='user_logname';
$opt['userfldId']='user_ID';

$opt['upagesdb']='umenu_pages';
$opt['upagesfldId']='umenu_ID';
$opt['upagesfldname']='user_logname';
$opt['upagesfldpage']='menu_page_name';

$opt['menuitemsdb']='menu_items';
$opt['menuitemsId']='menu_item_ID';
$opt['menuitemsname']='menu_name';

$opt['umenuitemsdb']='umenu_items';
$opt['umenuitemsId']='umenu_item_ID';
$opt['umenuitemmenuid']='umenu_ID';
$opt['umenuitemmenuitemid']='menu_item_ID';
$opt['umenuitemorder']='umenu_item_order';
$opt['umenuitemname']='user_logname';

// global arrays and uses
//  $qryusers[1]='john'  Login Users of app
//  $qrypages[1] = 'main' list of page names to select from
//  $qrymenuitems[1]='update.php' list of menu items to select from
//  $qryumenu['john']['main'] = 12 user page unique id
//  $qryumenit["user_1_1"]['val'] == menuorder
//  $qryumenit["user_1_1"]['id'] == user menu item unique id
//  user_1_15 codes login user name(=user), user menu page number(=1), menu item(=15)

include 'dbaccess.php';
include 'pma-debuginc.php';
$debug=0;
$auth = '999';

Dbconnect();
DoAuth();

MyHeader("User Menu Items Setup");

GetMenuPages();
GetUserlist();
GetUserMenuData();
GetMenuItemList();
GetUserMenuItems();

$opt['menusteps'] = 10;
$opt['lpp'] = 100;
$opt['cpp'] = 1;
// web vars to $opt[]
if (!$_POST['vertpos']) {$_POST['vertpos']=0; $opt['vertpos']=0;} else { $opt['vertpos'] = $_POST['vertpos'];}
if (!$_POST['horpos']) {$_POST['horpos']=0; $opt['horpos']=0;} else { $opt['horpos'] = $_POST['horpos']; }

$opt['vertcount'] = count($GLOBALS['qrymenuitems']);
$opt['horcount'] = count($GLOBALS['qrypages']);

if ($_POST['select']=="select") {
   //DisplayPage();
   //DoFooter();
} else if ($_POST['Next']=="next") {
   $opt['horpos'] = $_POST['horpos'] + $opt['cpp'];
   if ($opt['horpos'] - $opt['horcount'] >= 0 ) {
       $opt['horpos'] = $opt['horcount'] - $opt['cpp'];
   }
} else if ($_POST['Prev']=="prev") {
   $opt['horpos'] = $_POST['horpos'] - $opt['cpp'];
   if ($opt['horpos'] < 0 ) {
       $opt['horpos'] = 0;
   }
} else if ($_POST['Edit']=="edit") {
   $opt['horpos'] = $_POST['column'];
   if ($opt['horpos'] - $opt['horcount'] >= 0 ) {
       $opt['horpos'] = $opt['horcount'] - $opt['cpp'];
   }
   if ($opt['horpos'] < 0 ) {
       $opt['horpos'] = 0;
   }
} else if ($_POST['update']=="update") {
   getpostvars();
   //GetUserMenuItems();
} //else 
DisplayPage();
DoFooter();

?>
Return current item: phpMyAccess