Location: PHPKode > projects > Caleb's Code > Cafe_Cart/admin/modules/add_store.php
<?php

if($pnconfig[Version_Sub] == "Rogue")
{
    if (!eregi('admin.php', $PHP_SELF)) die ('Access Denied: You can\'t access this file directly.');
    if (!authorised(0, "$ModName::", '::', ACCESS_ADMIN)) die ('Access Denied: No permissions');
}
else
{
    if (!eregi("admin.php", $PHP_SELF)) die ("Access Denied");
}

include ("header.php");
GraphicAdmin($hlpfile);
cp_menu();
OpenTable();

// output an edit/delete box for current hosts
if (mysql_num_rows($result = mysql_query("SELECT id, acct FROM $pntable[CP_Stores]"))) {
   echo "   <table border=0>\n"
       ."    <tr>\n"
       ."     <td>\n"
       ."      <form action=\"admin.php\" method=\"post\" style=\"margin: 0;\">\n"
       ."       <input type=hidden name=op value=add_store_$ModName>\n"
       ."       <input type=hidden name=cpop value=edit_store><b>Edit Store:&nbsp;&nbsp;</b>\n"
       ."       <select name=editStore>\n";
   while ($row = mysql_fetch_array($result)) echo "        <option value=\"$row[id]\">$row[acct]</option>\n";
   $result = mysql_query("SELECT id, acct FROM $pntable[CP_Stores]");
   echo "       </select>\n"
       ."       <input type=submit value=\"Edit\">\n"
       ."      </form>\n"
       ."     </td>\n"
       ."     <td>&nbsp;&nbsp;</td>\n"
       ."     <td>\n"
       ."      <form action=\"admin.php\" method=\"post\" style=\"margin: 0;\">\n"
       ."       <input type=hidden name=op value=add_store_$ModName>\n"
       ."       <input type=hidden name=cpop value=delete_store>\n"
       ."       <b>Delete Store:&nbsp;&nbsp;</b>\n"
       ."       <select name=deleteStore>\n";
   while ($row = mysql_fetch_array($result)) echo "        <option value=\"$row[id]\">$row[acct]</option>\n";
   echo "       </select>\n"
       ."       <input type=submit value=\"Delete\">\n"
       ."      </form>\n"
       ."     </td>\n"
       ."    </tr>\n"
       ."   </table>\n";
   CloseTable();
   echo "   <br>\n";
   OpenTable();
   }

// shows form to add a store/also is where we redirect unfinnished forms
function showAddForm($name, $acct, $description, $error) {
   global $ModName;
   echo "   <form action=\"admin.php\" method=\"post\">\n"
       ."   <input type=hidden name=op value=\"add_store_$ModName\">\n"
       ."   <input type=hidden name=cpop value=\"add_new\">\n"
       ."   <table border=0 width=\"100%\">\n";
   if ($error) echo "    <tr>\n"
                   ."     <td colspan=2 align=center><b>ERROR: $error</b></td>\n"
                   ."    </tr>\n";
   echo "    <tr>\n"
       ."     <td valign=top width=\"85%\"><h3>Add Store</h3></td>\n"
       ."     <td rowspan=2 valign=top align=center><a href=\"http://www.cafepress.com/cp/info/storeref.aspx?refby=rastafari\" target=\"_blank\"><img src=\"modules/$ModName/images/admin.gif\" alt=\"Don't have a store? Click to signup!\" border=0><br>Click to Get a Store!</a></td>\n"
       ."    </tr>\n"
       ."    <tr>\n"
       ."     <td>\n"
       ."      Store Name/Account:<br>\n"
       ."      <input type=text size=30 maxlength=12 name=acct value=\"$acct\"><br>\n"
       ."      Visible Name:<br>\n"
       ."      <input type=text size=30 maxlength=50 name=name value=\"$name\"><br>\n"
       ."      Description:<br>\n"
       ."      <textarea name=description style=\"width: 75%; height: 100px;\">$description</textarea>\n"
       ."      <br>\n"
       ."      <br>\n"
       ."      <input type=submit value=\"Add Store\">\n"
       ."     </td>\n"
       ."    </tr>\n"
       ."   </table>\n"
       ."   </form>\n";
   }

// this allows us to edit a host
function cpedit($editStore, $error) {
   global $pntable, $ModName;
   if (!$store = mysql_fetch_array(mysql_query("SELECT * FROM $pntable[CP_Stores] WHERE id='$editStore'"))) die ("Could not access database, or store doesn't exist.");
   echo "   <form action=\"admin.php\" method=\"post\">\n"
       ."   <input type=hidden name=op value=\"add_store_$ModName\">\n"
       ."   <input type=hidden name=cpop value=\"update_store\">\n"
       ."   <input type=hidden name=id value=\"$editStore\">\n"
       ."   <input type=hidden name=acct value=\"$store[acct]\">\n"
       ."   <table border=0 width=\"100%\">\n";
   if ($error) echo "    <tr>\n"
                   ."     <td colspan=2 align=center><b>ERROR: $error</b></td>\n"
                   ."    </tr>\n";
   echo "    <tr>\n"
       ."     <td valign=top width=\"85%\"><h3>Edit Store</h3></td>\n"
       ."     <td rowspan=2 valign=top align=center><a href=\"http://www.cafepress.com/cp/info/storeref.aspx?refby=rastafari\" target=\"_blank\"><img src=\"modules/$ModName/images/admin.gif\" alt=\"Don't have a store? Click to signup!\" border=0><br>Click to Get a Store!</a></td>\n"
       ."    </tr>\n"
       ."    <tr>\n"
       ."     <td>\n"
       ."      Visible Name:<br>\n"
       ."      <input type=text size=30 maxlength=50 name=name value=\"$store[name]\"><br>\n"
       ."      Description:<br>\n"
       ."      <textarea name=description style=\"width: 75%; height: 100px;\">$store[description]</textarea>\n"
       ."      <br>\n"
       ."      <br>\n"
       ."      <input type=submit value=\"Update Store\">\n"
       ."     </td>\n"
       ."    </tr>\n"
       ."   </table>\n"
       ."   </form>\n";
   }

// adds the store to database
function addStore($name, $acct, $description) {
   global $pntable;
   if (!$name || !$acct) showAddForm($name, $acct, $description, "Please make sure all feilds are filled in.");
   elseif (mysql_num_rows(mysql_query("SELECT id FROM $pntable[CP_Stores] WHERE name='$name'"))) showAddForm($name, $acct, $description, "That $name is already in the database, please try another.");
   elseif (!mysql_query("INSERT INTO $pntable[CP_Stores] SET name='$name', acct='$acct', description='$description'")) die ("Could not insert into database.");
   else echo "   <center>\n"
            ."    <h2>$name Added!</h2>\n"
            ."   </center>";
   }

// updates a store
function cpupdate($id, $name, $acct, $description) {
   global $pntable;
   if (!$name || !$acct) cpedit($id, "Please make sure all feilds are filled in.");
   elseif (!mysql_query("UPDATE $pntable[CP_Stores] SET name='$name', acct='$acct', description='$description' WHERE id='$id'")) die ("Could not update database.");
   else echo "   <center>\n"
            ."    <h2>$name Edited!</h2>\n"
            ."   </center>";
   }

function cpdelete($deleteStore) {
   global $pntable, $ModName;
   include ("modules/$ModName/classes/products.php");
   $result = mysql_query("SELECT prod, name, ptype FROM $pntable[cp_products] WHERE store='$deleteStore'");
   if (mysql_num_rows($result)) while ($item = mysql_fetch_array($result)) {
      $type = $item[ptype];
      $error .= "Product #$item[prod] - $products[$type]: $item[name] is using this store. Please delete this item before procceding.<br>";
      }
   elseif (!mysql_query("DELETE FROM $pntable[CP_Stores] WHERE id='$deleteHost'")) $error .= "Couldn't delete store from table.<br>";
   else echo "<h2>Store Deleted</h2>";
   echo $error;
   }

switch ($cpop) {
   case "add_new":
      addStore($name, $acct, $description);
      break;
   case "update_store":
      cpupdate($id, $name, $acct, $description);
      break;
   case "edit_store":
      cpedit($editStore, '');
      break;
   case "delete_store":
      cpdelete($deleteStore);
      break;
   default:
      showAddForm('', '', '', '');
      break;
   }

CloseTable();
include ("footer.php");
?>
Return current item: Caleb's Code