<?php
/**
* @package admin
* @copyright Copyright 2003-2007 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
* @version $Id: categories.php 6991 2007-09-13 01:01:24Z drbyte $
*/
require('includes/application_top.php');
//error_reporting(E_ALL);
require(DIR_WS_MODULES . 'prod_cat_header_code.php');
$action = (isset($_GET['action']) ? $_GET['action'] : '');
// modified by zen-cart.cn
// Ultimate SEO URLs v2.105
// If the action will affect the cache entries
if (preg_match("/(insert|update|setflag)/i", $action)) {
include_once(DIR_WS_INCLUDES . 'reset_seo_cache.php');
}
// end of modification
if (!isset($_SESSION['categories_products_sort_order'])) {
$_SESSION['categories_products_sort_order'] = CATEGORIES_PRODUCTS_SORT_ORDER;
}
if (!isset($_GET['reset_categories_products_sort_order'])) {
$reset_categories_products_sort_order = $_SESSION['categories_products_sort_order'];
}
if (zen_not_null($action)) {
switch ($action) {
case 'set_categories_products_sort_order':
$_SESSION['categories_products_sort_order'] = $_GET['reset_categories_products_sort_order'];
$action='';
zen_redirect(zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $_GET['cPath'] . ((isset($_GET['pID']) and !empty($_GET['pID'])) ? '&pID=' . $_GET['pID'] : '') . ((isset($_GET['page']) and !empty($_GET['page'])) ? '&page=' . $_GET['page'] : '')));
break;
case 'set_editor':
// Reset will be done by init_html_editor.php. Now we simply redirect to refresh page properly.
$action='';
zen_redirect(zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $_GET['cPath'] . ((isset($_GET['pID']) and !empty($_GET['pID'])) ? '&pID=' . $_GET['pID'] : '') . ((isset($_GET['page']) and !empty($_GET['page'])) ? '&page=' . $_GET['page'] : '')));
break;
case 'update_category_status':
// disable category and products including subcategories
if (isset($_POST['categories_id'])) {
$categories_id = zen_db_prepare_input($_POST['categories_id']);
$categories = zen_get_category_tree($categories_id, '', '0', '', true);
for ($i=0, $n=sizeof($categories); $i<$n; $i++) {
$product_ids = $db->Execute("select products_id
from " . TABLE_PRODUCTS_TO_CATEGORIES . "
where categories_id = '" . (int)$categories[$i]['id'] . "'");
while (!$product_ids->EOF) {
$products[$product_ids->fields['products_id']]['categories'][] = $categories[$i]['id'];
$product_ids->MoveNext();
}
}
// change the status of categories and products
zen_set_time_limit(600);
for ($i=0, $n=sizeof($categories); $i<$n; $i++) {
if ($_POST['categories_status'] == '1') {
$categories_status = '0';
$products_status = '0';
} else {
$categories_status = '1';
$products_status = '1';
}
$sql = "update " . TABLE_CATEGORIES . " set categories_status='" . $categories_status . "'
where categories_id='" . $categories[$i]['id'] . "'";
$db->Execute($sql);
// set products_status based on selection
if ($_POST['set_products_status'] == 'set_products_status_nochange') {
// do not change current product status
} else {
if ($_POST['set_products_status'] == 'set_products_status_on') {
$products_status = '1';
} else {
$products_status = '0';
}
$sql = "select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id='" . $categories[$i]['id'] . "'";
$category_products = $db->Execute($sql);
while (!$category_products->EOF) {
$sql = "update " . TABLE_PRODUCTS . " set products_status='" . $products_status . "' where products_id='" . $category_products->fields['products_id'] . "'";
$db->Execute($sql);
$category_products->MoveNext();
}
}
} // for
}
zen_redirect(zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $_GET['cPath'] . '&cID=' . $_GET['cID']));
break;
case 'remove_type':
$sql = "delete from " . TABLE_PRODUCT_TYPES_TO_CATEGORY . "
where category_id = '" . zen_db_prepare_input($_GET['cID']) . "'
and product_type_id = '" . zen_db_prepare_input($_GET['type_id']) . "'";
$db->Execute($sql);
zen_remove_restrict_sub_categories($_GET['cID'], $_GET['type_id']);
$action = "edit";
zen_redirect(zen_href_link(FILENAME_CATEGORIES, 'action=edit_category&cPath=' . $_GET['cPath'] . '&cID=' . zen_db_prepare_input($_GET['cID'])));
break;
case 'setflag':
if ( ($_GET['flag'] == '0') || ($_GET['flag'] == '1') ) {
if (isset($_GET['pID'])) {
zen_set_product_status($_GET['pID'], $_GET['flag']);
}
}
zen_redirect(zen_href_link(FILENAME_CATEGORIES, 'cPath=' . $_GET['cPath'] . '&pID=' . $_GET['pID'] . (isset($_GET['page']) ? '&page=' . $_GET['page'] : '')));
break;
case 'insert_category':
case 'update_category':
if ( isset($_POST['add_type']) or isset($_POST['add_type_all']) ) {
// check if it is already restricted
$sql = "select * from " . TABLE_PRODUCT_TYPES_TO_CATEGORY . "
where category_id = '" . zen_db_prepare_input($_POST['categories_id']) . "'
and product_type_id = '" . zen_db_prepare_input($_POST['restrict_type']) . "'";
$type_to_cat = $db->Execute($sql);
if ($type_to_cat->RecordCount() < 1) {
//@@TODO find all sub-categories and restrict them as well.
$insert_sql_data = array('category_id' => zen_db_prepare_input($_POST['categories_id']),
'product_type_id' => zen_db_prepare_input($_POST['restrict_type']));
zen_db_perform(TABLE_PRODUCT_TYPES_TO_CATEGORY, $insert_sql_data);
/*
// moved below so evaluated separately from current category
if (isset($_POST['add_type_all'])) {
zen_restrict_sub_categories($_POST['categories_id'], $_POST['restrict_type']);
}
*/
}
// add product type restrictions to subcategories if not already set
if (isset($_POST['add_type_all'])) {
zen_restrict_sub_categories($_POST['categories_id'], $_POST['restrict_type']);
}
$action = "edit";
zen_redirect(zen_href_link(FILENAME_CATEGORIES, 'action=edit_category&cPath=' . $cPath . '&cID=' . zen_db_prepare_input($_POST['categories_id'])));
}
if (isset($_POST['categories_id'])) $categories_id = zen_db_prepare_input($_POST['categories_id']);
$sort_order = zen_db_prepare_input($_POST['sort_order']);
$sql_data_array = array('sort_order' => (int)$sort_order);
if ($action == 'insert_category') {
$insert_sql_data = array('parent_id' => $current_category_id,
'date_added' => 'now()');
$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
zen_db_perform(TABLE_CATEGORIES, $sql_data_array);
$categories_id = zen_db_insert_id();
// check if [arent is restricted
$sql = "select parent_id from " . TABLE_CATEGORIES . "
where categories_id = '" . $categories_id . "'";
$parent_cat = $db->Execute($sql);
if ($parent_cat->fields['parent_id'] != '0') {
$sql = "select * from " . TABLE_PRODUCT_TYPES_TO_CATEGORY . "
where category_id = '" . $parent_cat->fields