Example #1
0
 public static function delete($id)
 {
     global $lC_Database;
     $lC_CategoryTree = new lC_CategoryTree_Admin();
     if (is_numeric($id)) {
         $lC_CategoryTree->setBreadcrumbUsage(false);
         $categories = array_merge(array(array('id' => $id, 'text' => '')), $lC_CategoryTree->getArray($id));
         $products = array();
         $products_delete = array();
         foreach ($categories as $category) {
             $Qproducts = $lC_Database->query('select products_id from :table_products_to_categories where categories_id = :categories_id');
             $Qproducts->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
             $Qproducts->bindInt(':categories_id', $category['id']);
             $Qproducts->execute();
             while ($Qproducts->next()) {
                 $products[$Qproducts->valueInt('products_id')]['categories'][] = $category['id'];
             }
         }
         foreach ($products as $key => $value) {
             $Qcheck = $lC_Database->query('select categories_id from :table_products_to_categories where products_id = :products_id and categories_id not in :categories_id limit 1');
             $Qcheck->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
             $Qcheck->bindInt(':products_id', $key);
             $Qcheck->bindRaw(':categories_id', '("' . implode('", "', $value['categories']) . '")');
             $Qcheck->execute();
             if ($Qcheck->numberOfRows() === 0) {
                 $products_delete[$key] = $key;
             }
         }
         lc_set_time_limit(0);
         foreach ($categories as $category) {
             $lC_Database->startTransaction();
             $Qimage = $lC_Database->query('select categories_image from :table_categories where categories_id = :categories_id');
             $Qimage->bindTable(':table_categories', TABLE_CATEGORIES);
             $Qimage->bindInt(':categories_id', $category['id']);
             $Qimage->execute();
             $Qc = $lC_Database->query('delete from :table_categories where categories_id = :categories_id');
             $Qc->bindTable(':table_categories', TABLE_CATEGORIES);
             $Qc->bindInt(':categories_id', $category['id']);
             $Qc->setLogging($_SESSION['module'], $id);
             $Qc->execute();
             if (!$lC_Database->isError()) {
                 $Qcd = $lC_Database->query('delete from :table_categories_description where categories_id = :categories_id');
                 $Qcd->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
                 $Qcd->bindInt(':categories_id', $category['id']);
                 $Qcd->setLogging($_SESSION['module'], $id);
                 $Qcd->execute();
                 if (!$lC_Database->isError()) {
                     $Qp2c = $lC_Database->query('delete from :table_products_to_categories where categories_id = :categories_id');
                     $Qp2c->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
                     $Qp2c->bindInt(':categories_id', $category['id']);
                     $Qp2c->setLogging($_SESSION['module'], $id);
                     $Qp2c->execute();
                     if (!$lC_Database->isError()) {
                         // permalink
                         $Qpb = $lC_Database->query('delete from :table_permalinks where item_id = :item_id');
                         $Qpb->bindTable(':table_permalinks', TABLE_PERMALINKS);
                         $Qpb->bindInt(':item_id', $category['id']);
                         $Qpb->execute();
                         if (!$lC_Database->isError()) {
                             $lC_Database->commitTransaction();
                             lC_Cache::clear('categories');
                             lC_Cache::clear('category_tree');
                             lC_Cache::clear('also_purchased');
                             if (!lc_empty($Qimage->value('categories_image'))) {
                                 $Qcheck = $lC_Database->query('select count(*) as total from :table_categories where categories_image = :categories_image');
                                 $Qcheck->bindTable(':table_categories', TABLE_CATEGORIES);
                                 $Qcheck->bindValue(':categories_image', $Qimage->value('categories_image'));
                                 $Qcheck->execute();
                                 if ($Qcheck->numberOfRows() === 0) {
                                     if (file_exists(realpath('../' . DIR_WS_IMAGES . 'categories/' . $Qimage->value('categories_image')))) {
                                         @unlink(realpath('../' . DIR_WS_IMAGES . 'categories/' . $Qimage->value('categories_image')));
                                     }
                                 }
                             }
                         } else {
                             $lC_Database->rollbackTransaction();
                         }
                     } else {
                         $lC_Database->rollbackTransaction();
                     }
                 } else {
                     $lC_Database->rollbackTransaction();
                 }
             } else {
                 $lC_Database->rollbackTransaction();
             }
         }
         foreach ($products_delete as $id) {
             lC_Products_Admin::remove($id);
         }
         lC_Cache::clear('categories');
         lC_Cache::clear('category_tree');
         lC_Cache::clear('also_purchased');
         return true;
     }
     return false;
 }