Example #1
0
<?php

$model = new shopProductModel();
$model->correctMainCategory();
 /**
  * Delete products from category
  *
  * @param int $category_id
  * @param array|bool $product_ids If true than delete all products from category
  * @return boolean
  */
 public function deleteProducts($category_id, $product_ids = array())
 {
     if (!$category_id) {
         return false;
     }
     $category_model = new shopCategoryModel();
     if ($product_ids === true) {
         $product_ids = array_keys($this->select('product_id')->where('category_id = :c_id', array('c_id' => $category_id))->fetchAll('product_id', true));
         if (!$this->deleteByField('category_id', $category_id)) {
             return false;
         }
         if (!$category_model->updateById($category_id, array('count' => 0))) {
             return false;
         }
     } else {
         if (!$this->deleteByField(array('category_id' => $category_id, 'product_id' => $product_ids))) {
             return false;
         }
         if (!$category_model->recount($category_id)) {
             return false;
         }
     }
     $product_model = new shopProductModel();
     $product_model->correctMainCategory($product_ids);
     return true;
 }
Example #3
0
<?php

$model = new waModel();
try {
    $model->exec("SELECT category_id FROM shop_product WHERE 0");
} catch (waDbException $e) {
    $model->exec("ALTER TABLE shop_product ADD category_id INT (11) NULL DEFAULT NULL");
}
$product_model = new shopProductModel();
$product_model->correctMainCategory();
Example #4
0
 public function delete($id)
 {
     $id = (int) $id;
     $item = $this->getById($id);
     if (!$item) {
         return false;
     }
     $parent_id = (int) $item['parent_id'];
     /**
      * @event category_delete
      */
     wa()->event('category_delete', $item);
     // because all descendants will be thrown one level up
     // it's necessary to ensure uniqueness urls of descendants in new environment (new parent)
     foreach ($this->descendants($item, false)->order("`{$this->depth}`, `{$this->left}`")->query() as $child) {
         $url = $this->suggestUniqueUrl($child['url'], $child['id'], $parent_id);
         if ($url != $child['url']) {
             $this->updateById($child['id'], array('url' => $url, 'full_url' => $this->fullUrl($item['full_url'], $child['url'])));
         }
     }
     // than correct full urls of descendants taking into account full url of new parent
     if (!$parent_id) {
         $this->correctFullUrlOfDescendants($item, '');
     } else {
         $parent = $this->getById($parent_id);
         $this->correctFullUrlOfDescendants($item, $parent['full_url']);
     }
     if (!parent::delete($id)) {
         return false;
     }
     // delete related info
     $category_params_model = new shopCategoryParamsModel();
     $category_params_model->clear($id);
     $category_products_model = new shopCategoryProductsModel();
     $category_products_model->deleteByField('category_id', $id);
     $category_routes_model = new shopCategoryRoutesModel();
     $category_routes_model->deleteByField('category_id', $id);
     $product_model = new shopProductModel();
     $product_model->correctMainCategory(null, $id);
     shopCategories::clear($id);
     $this->clearCache();
     return true;
 }