public function execute()
 {
     $model = new shopCategoryProductsModel();
     if (waRequest::post('hash', '')) {
         $model->clearCategory(waRequest::get('id'));
     } else {
         $model->deleteProducts(waRequest::get('id'), waRequest::post('product_id', array(), waRequest::TYPE_ARRAY_INT));
     }
 }
 public function execute()
 {
     $id = $this->get('id', true);
     $this->getProduct($id);
     $category_id = $this->post('category_id', true);
     $category_model = new shopCategoryModel();
     $category = $category_model->getById($category_id);
     if (!$category) {
         throw new waAPIException('invalid_param', 'Category not found', 404);
     }
     if ($category['type'] == shopCategoryModel::TYPE_DYNAMIC) {
         throw new waAPIException('invalid_param', 'Category type must be static');
     }
     $category_products_model = new shopCategoryProductsModel();
     $this->response = $category_products_model->deleteProducts($category_id, $id);
 }
 public function execute()
 {
     $category_ids = waRequest::post('category_id', array(), waRequest::TYPE_ARRAY_INT);
     // create new category
     $new_category_id = null;
     if (waRequest::post('new_category')) {
         $new_category_id = $this->createCategory(waRequest::post('new_category_name'));
         $category_ids[] = $new_category_id;
     }
     if (!$category_ids) {
         return;
     }
     // add products to categories
     $hash = waRequest::post('hash', '');
     if (!$hash) {
         $product_ids = waRequest::post('product_id', array(), waRequest::TYPE_ARRAY_INT);
         if (!$product_ids) {
             return;
         }
         // add just selected products
         $this->category_products_model->add($product_ids, $category_ids);
     } else {
         // add all products of collection with this hash
         $collection = new shopProductsCollection($hash);
         $offset = 0;
         $count = 100;
         $total_count = $collection->count();
         while ($offset < $total_count) {
             $ids = array_keys($collection->getProducts('*', $offset, $count));
             $this->category_products_model->add($ids, $category_ids);
             $offset += count($ids);
         }
     }
     // form a response
     $categories = $this->category_model->getByField('id', $category_ids, 'id');
     if (isset($categories[$new_category_id])) {
         $this->response['new_category'] = $categories[$new_category_id];
         unset($categories[$new_category_id]);
     }
     $this->response['categories'] = $categories;
 }
Example #4
0
 /**
  * @param int $product
  * @param int|null $category_id
  * @return array
  */
 public function getStorefrontMap($product, $category_id = null)
 {
     $storefronts_map = array();
     $product_id = (int) $product;
     $category_product_model = new shopCategoryProductsModel();
     $product_categories = $category_product_model->getByField('product_id', $product_id, 'category_id');
     $product_type = $this->select('type_id')->where('id=' . $product_id)->fetchField();
     if (!$product_categories || !$product_type) {
         return array();
     }
     if ($category_id !== null) {
         if (isset($product_categories[$category_id])) {
             $product_categories = array($category_id => $product_categories[$category_id]);
         } else {
             return array();
         }
     }
     $routing = wa()->getRouting();
     $domain_routes = $routing->getByApp('shop');
     $category_routes_model = new shopCategoryRoutesModel();
     $category_routes = $category_routes_model->getRoutes(array_keys($product_categories));
     foreach ($product_categories as $c_id => &$category) {
         $category['routes'] = isset($category_routes[$c_id]) ? $category_routes[$c_id] : array();
     }
     unset($category);
     foreach ($product_categories as $c_id => $category) {
         $storefronts_map[$c_id] = array();
         foreach ($domain_routes as $domain => $routes) {
             foreach ($routes as $r) {
                 if (!empty($r['private'])) {
                     continue;
                 }
                 if ((empty($r['type_id']) || in_array($product_type, (array) $r['type_id'])) && (!$category['routes'] || in_array($domain . '/' . $r['url'], $category['routes']))) {
                     $routing->setRoute($r, $domain);
                     $storefronts_map[$c_id][] = $routing->getUrl('shop/frontend', array(), true);
                 }
             }
         }
     }
     $all_routes_count = 0;
     foreach ($domain_routes as $domain => $routes) {
         foreach ($routes as $r) {
             if (!empty($r['private'])) {
                 continue;
             }
             $all_routes_count += 1;
         }
     }
     foreach ($storefronts_map as $c_id => &$storefronts_list) {
         if (count($storefronts_list) == $all_routes_count) {
             $storefronts_list = array();
         }
     }
     unset($storefronts_list);
     if ($category_id !== null) {
         return $storefronts_map[$category_id];
     } else {
         return $storefronts_map;
     }
 }
 protected function workupProducts(&$products)
 {
     $currency = $this->getConfig()->getCurrency();
     foreach ($products as &$p) {
         if ($p['min_price'] == $p['max_price']) {
             $p['price_range'] = wa_currency($p['min_price'], $currency);
         } else {
             $p['price_range'] = wa_currency($p['min_price'], $currency) . '...' . wa_currency($p['max_price'], $currency);
         }
         if ($p['badge']) {
             $p['badge'] = shopHelper::getBadgeHtml($p['badge']);
         }
         unset($p['meta_description'], $p['meta_keywords'], $p['meta_title'], $p['description'], $p['summary']);
     }
     unset($p);
     if ($this->sort == 'count') {
         foreach ($products as &$p) {
             $p['icon'] = shopHelper::getStockCountIcon($p['count']);
         }
     } else {
         if ($this->sort == 'create_datetime') {
             foreach ($products as &$p) {
                 $p['create_datetime_str'] = wa_date('humandatetime', $p['create_datetime']);
             }
         } else {
             if ($this->sort == 'rating') {
                 foreach ($products as &$p) {
                     $p['rating_str'] = shopHelper::getRatingHtml($p['rating'], 10, true);
                 }
             } else {
                 if ($this->sort == 'total_sales') {
                     $currency = wa('shop')->getConfig()->getCurrency();
                     foreach ($products as &$p) {
                         $p['total_sales_str'] = wa_currency($p['total_sales'], $currency);
                     }
                 }
             }
         }
     }
     unset($p);
     $info = $this->collection->getInfo();
     if ($info['hash'] == 'category') {
         $product_ids = array_keys($products);
         $category_products_model = new shopCategoryProductsModel();
         $ids = $category_products_model->filterByEnteringInCategories($product_ids, $info['id']);
         $ids = array_flip($ids);
         foreach ($products as $id => &$product) {
             $product['alien'] = $info['type'] == shopCategoryModel::TYPE_STATIC && !isset($ids[$id]);
         }
         unset($product);
     }
 }
Example #6
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;
 }
 public function moveInsideCategory($product_ids, $before_id = null, $list_id)
 {
     $category_products_model = new shopCategoryProductsModel();
     $category_products_model->move($product_ids, $before_id, $list_id);
 }