/** * Deletes one or more ShopCategories * * Only succeeds if there are no subcategories, and if all contained * Products can be deleted as well. Products that are present in any * order won't be deleted. * @param integer $category_id The optional ShopCategory ID. * If this is no valid ID, it is taken * from the request parameters * $_GET['delete_category_id'], or * $_POST['selectedCatId'], in this * order. * @return boolean True on success, null on noop, * false otherwise. */ function delete_categories($category_id = 0) { global $objDatabase, $_ARRAYLANG; $arrCategoryId = array(); $deleted = false; if (empty($category_id)) { if (!empty($_GET['delete_category_id'])) { array_push($arrCategoryId, $_GET['delete_category_id']); } elseif (!empty($_POST['selected_category_id']) && is_array($_POST['selected_category_id'])) { $arrCategoryId = $_POST['selected_category_id']; } } else { array_push($arrCategoryId, $category_id); } if (empty($arrCategoryId)) { return null; } // When multiple IDs are posted, the list must be reversed, // so subcategories are removed first $arrCategoryId = array_reverse($arrCategoryId); //DBG::log("delete_categories($category_id): Got ".var_export($arrCategoryId, true)); foreach ($arrCategoryId as $category_id) { // Check whether this category has subcategories $arrChildId = ShopCategories::getChildCategoryIdArray($category_id, false); //DBG::log("delete_categories($category_id): Children of $category_id: ".var_export($arrChildId, true)); if (count($arrChildId)) { \Message::warning($_ARRAYLANG['TXT_CATEGORY_NOT_DELETED_BECAUSE_IN_USE'] . " (" . $_ARRAYLANG['TXT_CATEGORY'] . " " . $category_id . ")"); continue; } // Get Products in this category $count = 1000000000.0; $arrProducts = Products::getByShopParams($count, 0, null, $category_id, null, null, false, false, '', null, true); //DBG::log("delete_categories($category_id): Products in $category_id: ".var_export($arrProducts, true)); // Delete the products in the category foreach ($arrProducts as $objProduct) { // Check whether there are orders with this Product ID $product_id = $objProduct->id(); $query = "\n SELECT 1\n FROM " . DBPREFIX . "module_shop" . MODULE_INDEX . "_order_items\n WHERE product_id={$product_id}"; $objResult = $objDatabase->Execute($query); if (!$objResult || $objResult->RecordCount()) { \Message::error($_ARRAYLANG['TXT_COULD_NOT_DELETE_ALL_PRODUCTS'] . " (" . sprintf($_ARRAYLANG['TXT_SHOP_CATEGORY_ID_FORMAT'], $category_id) . ")"); continue 2; } } if (Products::deleteByShopCategory($category_id) === false) { \Message::error($_ARRAYLANG['TXT_ERROR_DELETING_PRODUCT'] . " (" . $_ARRAYLANG['TXT_CATEGORY'] . " " . $category_id . ")"); continue; } // Delete the Category now $result = ShopCategories::deleteById($category_id); if ($result === null) { continue; } if ($result === false) { return self::error_database(); } $deleted = true; } if ($deleted) { $objDatabase->Execute("OPTIMIZE TABLE " . DBPREFIX . "module_shop" . MODULE_INDEX . "_categories"); $objDatabase->Execute("OPTIMIZE TABLE " . DBPREFIX . "module_shop" . MODULE_INDEX . "_products"); return \Message::ok($_ARRAYLANG['TXT_DELETED_CATEGORY_AND_PRODUCTS']); } return null; }