<?
/*
GiftOrder, Open Source Gift Ordering Solutions
http://www.giftorder.org
Copyright (c) 2002 GiftOrder
Released under the GNU General Public License
*/
/******************************************************************************
* MAIN
*****************************************************************************/
include("../config.php");
require_login();
require_priv("admin");
$DOC_TITLE = "GiftOrder Category Management";
include("templates/header.php");
switch (nvl($mode)) {
case "add" :
print_add_category_form(nvl($id, 1));
break;
case "edit" :
print_edit_category_form($id);
break;
case "del" :
delete_category($id);
print_category_list();
break;
case "insert" :
insert_subcategory($id, $HTTP_POST_VARS);
print_category_list();
break;
case "update" :
update_category($id, $HTTP_POST_VARS);
print_category_list();
break;
default :
print_category_list();
break;
}
include("templates/footer.php");
/******************************************************************************
* FUNCTIONS
*****************************************************************************/
function print_add_category_form($id) {
/* print a blank category form so we can add a new category */
global $CFG, $ME;
/* set default values for the reset of the fields */
$frm["parent"] = array($id);
$frm["newmode"] = "insert";
$frm["name"] = "";
$frm["description"] = "";
$frm["submit_caption"] = "Add Subcategory";
/* build the categories listbox options, preselect the selected item */
build_category_tree($category_options, $frm["parent"]);
include("templates/category_form.php");
}
function print_edit_category_form($id) {
/* print a category form so we can add a edit the selected category */
global $CFG, $ME;
/* load up the information for the category */
$qid = db_query("
SELECT name, description, parent_id
FROM categories
WHERE id = $id
");
$frm = db_fetch_array($qid);
/* set values for the form */
$frm["parent"] = array($frm["parent_id"]);
$frm["newmode"] = "update";
$frm["submit_caption"] = "Save Changes";
/* build the categories listbox options, preselect the selected item */
build_category_tree($category_options, $frm["parent"]);
include("templates/category_form.php");
}
function delete_category($id) {
/* delete the category specified by $id, and move all the products under
* that category to the immediate parent. this should be wrapped inside a
* transaction, unfortunately MySQL currently does not support transactions. */
global $CFG, $ME;
/* find the parent of this category */
$qid = db_query("
SELECT cat.name, cat.parent_id, parent.name AS parent
FROM categories cat, categories parent
WHERE parent.id = cat.parent_id
AND cat.id = $id
");
$cat = db_fetch_object($qid);
/* delete this category */
$qid = db_query("
DELETE FROM categories
WHERE id = $id
");
/* re-assign all the products in this category to the parent category */
$qid = db_query("
UPDATE products_categories
SET category_id = $cat->parent_id
WHERE category_id = $id
");
/* re-assign all sub categories of this category to the parent category */
$qid = db_query("
UPDATE categories
SET parent_id = $cat->parent_id
WHERE parent_id = $id
");
include("templates/category_deleted.php");
}
function insert_subcategory($id, $frm) {
/* add a new subcategory under the parent $id. all the fields that we want are
* going to in the variable $frm */
global $CFG, $ME;
$qid = db_query("
INSERT INTO categories (parent_id, name, description)
VALUES ($frm[parent], '$frm[name]', '$frm[description]')
");
}
function update_category($id, $frm) {
/* update the category $id with new values. all the fields that we want are
* going to in the variable $frm */
global $CFG, $ME;
$qid = db_query("
UPDATE categories SET
parent_id = '$frm[parent]'
,name = '$frm[name]'
,description = '$frm[description]'
WHERE id = $id
");
}
function print_category_list() {
/* read all the categories from the database and print them into a table. we
* will use a template to display the listings to keep this main script clean */
global $CFG, $ME;
$qid = db_query("
SELECT cat.id, cat.name, cat.description, parent.name AS parent
FROM categories cat, categories parent
WHERE parent.id = cat.parent_id
AND cat.id > 0
");
include("templates/category_list.php");
}
?>