Sunday, 2 August 2009

Admin - Delete Category

When the admin decides that a category is no longer needed, he/she can remove it from the database. To delete a category go to the category listing page and click on the 'delete' link on the category you wish to delete

Deleting a category will delete all product in that category and in all it's children. For example if the admin delete the "HOMEWARE" category than all product in "Kitchen ware" and "Electric Goods" will also be deleted. The function used to delete category is called deleteCategory() and it's located in admin/category/processCategory.php

The deletion process is like this :

  1. Update the cat_id for all products in that category to zero
  2. Remove the category image ( if exist )
  3. Delete the category from database
function deleteCategory()
{
if (isset($_GET['catId']) && (int)$_GET['catId'] > 0) {
$catId = (int)$_GET['catId'];
} else {
header('Location: index.php');
}

// find all the children categories
$children = getChildren($catId);

// make an array containing this category and all it's children
$categories = array_merge($children, array($catId));
$numCategory = count($categories);

// remove all product image & thumbnail
// if the product's category is in $categories
$sql = "SELECT pd_id, pd_image, pd_thumbnail
FROM tbl_product
WHERE cat_id IN (" . implode(',', $categories) . ")";
$result = dbQuery($sql);

while ($row = dbFetchAssoc($result)) {
@unlink(SRV_ROOT . PRODUCT_IMAGE_DIR . $row['pd_image']);
@unlink(SRV_ROOT . PRODUCT_IMAGE_DIR . $row['pd_thumbnail']);
}

// delete the products
$sql = "DELETE FROM tbl_product
WHERE cat_id IN (" . implode(',', $categories) . ")";
dbQuery($sql);

// then remove the categories image
_deleteImage($categories);

// finally remove the category from database;
$sql = "DELETE FROM tbl_category
WHERE cat_id IN (" . implode(',', $categories) . ")";
dbQuery($sql);

header('Location: index.php');
}

1 comment:

  1. Can somebody explain me?
    I do not understand a line of code below:

    @unlink(SRV_ROOT . PRODUCT_IMAGE_DIR . $row['pd_image']);

    On this path is deleted image files, but why use @
    When it is used in syntax, and why?

    ReplyDelete