Location: PHPKode > projects > MyMarket > mymarket-1.72/admin/categories.php
<?
/* categories.php (c) 2000 Ying Zhang (hide@address.com)
 *
 * TERMS OF USAGE:
 * This file was written and developed by Ying Zhang (hide@address.com)
 * for educational and demonstration purposes only.  You are hereby granted the
 * rights to use, modify, and redistribute this file as you like.  The only
 * requirement is that you must retain this notice, without modifications, at
 * the top of your source code.  No warranties or guarantees are expressed or
 * implied. DO NOT use this code in a production environment without
 * understanding the limitations and weaknesses pretaining to or caused by the
 * use of these scripts, directly or indirectly. USE AT YOUR OWN RISK!
 */

/******************************************************************************
 * MAIN
 *****************************************************************************/

include("../application.php");
require_login();
require_priv("admin");

$DOC_TITLE = "MyMarket Category Management";
include("templates/header.php");

switch (nvl($_REQUEST["mode"])) {
	case "add" :
		print_add_category_form(nvl($_REQUEST["id"], 1));
		break;

	case "edit" :
		print_edit_category_form($_REQUEST["id"]);
		break;

	case "del" :
		delete_category($_REQUEST["id"]);
		print_category_list();
		break;

	case "insert" :
		insert_subcategory($_REQUEST["id"], $_POST);
		print_category_list();
		break;

	case "update" :
		update_category($_REQUEST["id"], $_POST);
		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");
}
?>
Return current item: MyMarket