diff --git a/api/Brands.php b/api/Brands.php index 80c04940..40d94939 100644 --- a/api/Brands.php +++ b/api/Brands.php @@ -22,6 +22,8 @@ public function get_brands($filter = array(), $count = false) { $lang_sql->fields"; $visible = ''; + $joins .= $this->db->placehold(" LEFT JOIN __products p ON p.brand_id=b.id"); + if ($count === true) { $select = "COUNT(DISTINCT b.id) as count"; } @@ -46,7 +48,6 @@ public function get_brands($filter = array(), $count = false) { if(isset($filter['product_id'])) { $where .= $this->db->placehold(' AND p.id in (?@)', (array)$filter['product_id']); - $joins = $this->db->placehold(" LEFT JOIN __products p ON p.brand_id=b.id"); } $first_currency = $this->money->get_currencies(array('enabled'=>1)); @@ -65,13 +66,11 @@ public function get_brands($filter = array(), $count = false) { if(isset($filter['price']['max'])) { $where .= $this->db->placehold(" AND floor(IF(pv.currency_id=0 OR c.id is null,pv.price, pv.price*c.rate_to/c.rate_from)*$coef)<= ? ", $this->db->escape(trim($filter['price']['max']))); } - $joins .= $this->db->placehold(" LEFT JOIN __products p ON p.brand_id=b.id"); - $joins .= ' LEFT JOIN __variants pv ON pv.product_id = p.id'; - $joins .= ' LEFT JOIN __currencies c ON c.id=pv.currency_id'; + $joins .= $this->db->placehold(" LEFT JOIN __variants pv ON pv.product_id = p.id"); + $joins .= $this->db->placehold(" LEFT JOIN __currencies c ON c.id=pv.currency_id"); } if(!empty($filter['category_id'])) { - $joins .= $this->db->placehold(" LEFT JOIN __products p ON p.brand_id=b.id"); $joins .= $this->db->placehold(" LEFT JOIN __products_categories pc ON p.id = pc.product_id"); $where .= $this->db->placehold(" AND pc.category_id in(?@) $visible", (array)$filter['category_id']); } @@ -125,8 +124,6 @@ public function get_brands($filter = array(), $count = false) { $other_filter .= "(SELECT 1 FROM __variants pv WHERE pv.product_id=p.id AND pv.compare_price>0 LIMIT 1) = 1 OR "; } $where .= substr($other_filter, 0, -4).")"; - - $joins .= $this->db->placehold(" LEFT JOIN __products p ON (p.brand_id=b.id)"); } if(!empty($filter['selected_brands'])) { diff --git a/api/Categories.php b/api/Categories.php index d0ee1300..cf9069d4 100644 --- a/api/Categories.php +++ b/api/Categories.php @@ -217,6 +217,11 @@ public function update_category($id, $category) { $this->update_level_depth($id, $category->level_depth); } + $category = (array)$category; + unset($category['path']); + unset($category['level']); + unset($category['subcategories']); + unset($category['children']); $category = (object)$category; $result = $this->languages->get_description($category, 'category'); @@ -283,9 +288,21 @@ public function update_main_product_category($product_ids) { } /*Удаление категории из товара*/ - public function delete_product_category($product_id, $category_id) { - $this->db->query("update __categories set last_modify=now() where id=?", intval($category_id)); - $query = $this->db->placehold("DELETE FROM __products_categories WHERE product_id=? AND category_id=? LIMIT 1", intval($product_id), intval($category_id)); + public function delete_product_category($products_ids, $categories_ids = array()) { + + $products_ids = (array)$products_ids; + $categories_ids = (array)$categories_ids; + $category_id_filter = ''; + if (empty($category_id)) { + foreach ($this->get_categories(array('product_id'=>$products_ids)) as $c) { + $categories_ids[] = $c->id; + } + } + if (!empty($categories_ids)) { + $this->db->query("UPDATE __categories SET last_modify=NOW() WHERE id IN (?@)", $categories_ids); + $category_id_filter = $this->db->placehold("AND category_id IN (?@)", $categories_ids); + } + $query = $this->db->placehold("DELETE FROM __products_categories WHERE product_id IN (?@) $category_id_filter", $products_ids); $this->db->query($query); } diff --git a/api/Config.php b/api/Config.php index 45ee3d13..0a3221fc 100644 --- a/api/Config.php +++ b/api/Config.php @@ -10,7 +10,7 @@ class Config { /*Версия системы*/ - public $version = '2.3.1'; + public $version = '2.3.2'; /*Тип системы*/ public $version_type = 'pro'; diff --git a/api/Products.php b/api/Products.php index 375aa623..79531e78 100644 --- a/api/Products.php +++ b/api/Products.php @@ -376,61 +376,59 @@ public function add_product($product) { } /*Удаление товара*/ - public function delete_product($id) { - if(!empty($id)) { + public function delete_product($ids) { + if (!empty($ids)) { + $ids = (array)$ids; // Удаляем варианты - $variants = $this->variants->get_variants(array('product_id'=>$id)); - foreach($variants as $v) { - $this->variants->delete_variant($v->id); + foreach ($this->variants->get_variants(array('product_id'=>$ids)) as $v) { + $variants_ids[] = $v->id; + } + if (!empty($variants_ids)) { + $this->variants->delete_variant($variants_ids); } // Удаляем изображения - $images = $this->get_images(array('product_id'=>$id)); + $images = $this->get_images(array('product_id'=>$ids)); foreach($images as $i) { $this->delete_image($i->id); } // Удаляем категории - $categories = $this->categories->get_categories(array('product_id'=>$id)); - foreach($categories as $c) { - $this->categories->delete_product_category($id, $c->id); - } + $this->categories->delete_product_category($ids); // Удаляем свойства - $this->features_values->delete_product_value($id); - + $this->features_values->delete_product_value($ids); + // Удаляем связанные товары - $related = $this->get_related_products($id); - foreach($related as $r) { - $this->delete_related_product($id, $r->related_id); - } + $query = $this->db->placehold("DELETE FROM __related_products WHERE product_id IN (?@)", $ids); + $this->db->query($query); // Удаляем товар из связанных с другими - $query = $this->db->placehold("DELETE FROM __related_products WHERE related_id=?", intval($id)); + $query = $this->db->placehold("DELETE FROM __related_products WHERE related_id IN (?@)", $ids); $this->db->query($query); // Удаляем отзывы - $comments = $this->comments->get_comments(array('object_id'=>$id, 'type'=>'product')); + $comments = $this->comments->get_comments(array('object_id'=>$ids, 'type'=>'product')); foreach($comments as $c) { $this->comments->delete_comment($c->id); } // Удаляем из покупок - $this->db->query('UPDATE __purchases SET product_id=NULL WHERE product_id=?', intval($id)); + $this->db->query('UPDATE __purchases SET product_id=NULL WHERE product_id IN (?@)', $ids); //lastModify - $this->db->query('select brand_id from __products where id=?', intval($id)); - $bid = (int)$this->db->result('brand_id'); - if ($bid) { - $this->db->query('update __brands set last_modify=now() where id=?', $bid); + $this->db->query('SELECT brand_id FROM __products WHERE id IN (?@)', $ids); + $bid = $this->db->results('brand_id'); + if (!empty($bid)) { + $this->db->query('UPDATE __brands SET last_modify=NOW() WHERE id IN (?@)', $bid); } // Удаляем языки - $query = $this->db->placehold("DELETE FROM __lang_products WHERE product_id=?", intval($id)); + $query = $this->db->placehold("DELETE FROM __lang_products WHERE product_id IN (?@)", $ids); $this->db->query($query); // Удаляем товар - $query = $this->db->placehold("DELETE FROM __products WHERE id=? LIMIT 1", intval($id)); + $query = $this->db->placehold("DELETE FROM __products WHERE id IN (?@)", $ids); if($this->db->query($query)) { return true; } diff --git a/api/Subscribes.php b/api/Subscribes.php index 32c38f85..30a1e520 100644 --- a/api/Subscribes.php +++ b/api/Subscribes.php @@ -46,7 +46,7 @@ public function get_subscribes($filter = array(), $count = false) { } $query = $this->db->placehold("SELECT $select - FROM __subescribe_mailing s + FROM __subscribe_mailing s $joins WHERE $where diff --git a/api/Variants.php b/api/Variants.php index 70a4c88e..9e14d804 100644 --- a/api/Variants.php +++ b/api/Variants.php @@ -179,27 +179,32 @@ public function add_variant($variant) { return $variant_id; } - public function delete_variant($id) { - if(!empty($id)) { - $this->delete_attachment($id); - $query = $this->db->placehold("DELETE FROM __variants WHERE id = ? LIMIT 1", intval($id)); + public function delete_variant($ids) { + $ids = (array)$ids; + if (!empty($ids)) { + $this->delete_attachment($ids); + $query = $this->db->placehold("DELETE FROM __variants WHERE id IN (?@)", $ids); $this->db->query($query); - $this->db->query('UPDATE __purchases SET variant_id=NULL WHERE variant_id=?', intval($id)); - $this->db->query("DELETE FROM __lang_variants WHERE variant_id = ?", intval($id)); + $this->db->query('UPDATE __purchases SET variant_id=NULL WHERE variant_id IN (?@)', $ids); + $this->db->query("DELETE FROM __lang_variants WHERE variant_id IN (?@)", $ids); } } - public function delete_attachment($id) { - $query = $this->db->placehold("SELECT attachment FROM __variants WHERE id=?", $id); + public function delete_attachment($ids) { + $ids = (array)$ids; + $query = $this->db->placehold("SELECT id, attachment FROM __variants WHERE id IN (?@) AND attachment !=''", $ids); $this->db->query($query); - $filename = $this->db->result('attachment'); - $query = $this->db->placehold("SELECT 1 FROM __variants WHERE attachment=? AND id!=?", $filename, $id); - $this->db->query($query); - $exists = $this->db->num_rows(); - if(!empty($filename) && $exists == 0) { - @unlink($this->config->root_dir.'/'.$this->config->downloads_dir.$filename); + $results = (array)$this->db->results(); + + foreach ($results as $result) { + $query = $this->db->placehold("SELECT 1 FROM __variants WHERE attachment=? AND id!=?", $result->filename, $result->id); + $this->db->query($query); + $exists = $this->db->num_rows(); + if (!empty($result->filename) && $exists == 0) { + @unlink($this->config->root_dir . '/' . $this->config->downloads_dir . $result->filename); + } + $this->update_variant($result->id, array('attachment' => null)); } - $this->update_variant($id, array('attachment'=>null)); } } diff --git a/backend/ajax/import.php b/backend/ajax/import.php index e3612f6d..e5349c7a 100644 --- a/backend/ajax/import.php +++ b/backend/ajax/import.php @@ -203,7 +203,20 @@ private function import_item($item) { // Если задан артикул варианта, найдем этот вариант и соответствующий товар if (!empty($variant['sku'])) { - $this->db->query('SELECT v.id as variant_id, v.product_id, p.url FROM __variants v, __products p WHERE v.sku=? AND v.product_id = p.id LIMIT 1', $variant['sku']); + + // Если у варианта есть еще и название, тогда ищем по артикулу + название, это как бы первичный ключ будет, + // чтобы можно было импортировать варианты с одинаковым артикулом + $variant_filter = ''; + if (!empty($variant['name'])) { + $variant_filter = $this->db->placehold(" AND v.name=?", $variant['name']); + } + + $this->db->query("SELECT v.id as variant_id, v.product_id + FROM __variants v + WHERE + v.sku=? + $variant_filter + LIMIT 1", $variant['sku']); $result = $this->db->result(); if ($result) { $product_id = $result->product_id; @@ -211,7 +224,7 @@ private function import_item($item) { $imported_item->status = 'updated'; } elseif (!empty($product['name'])) { // если по артикулу не нашли попробуем по названию(если он задано) - $this->db->query('SELECT p.id as product_id, p.url FROM __products p WHERE p.name=? LIMIT 1', $product['name']); + $this->db->query('SELECT p.id as product_id FROM __products p WHERE p.name=? LIMIT 1', $product['name']); $result = $this->db->result(); if ($result) { $product_id = $result->product_id; @@ -222,7 +235,7 @@ private function import_item($item) { } } else { // если нет артикула попробуем по названию товара - $this->db->query('SELECT v.id as variant_id, p.id as product_id, p.url + $this->db->query('SELECT v.id as variant_id, p.id as product_id FROM __products p LEFT JOIN __variants v ON v.product_id=p.id AND v.name=? WHERE p.name=? @@ -248,6 +261,20 @@ private function import_item($item) { $product['url'] = $this->translit($product['name']); } if (empty($product_id)) { + + // Если для нового товара не заданы метаданные, запишем туда название товара + if (!isset($product['meta_title']) || empty($product['meta_title'])) { + $product['meta_title'] = $product['name']; + } + + if (!isset($product['meta_keywords']) || empty($product['meta_keywords'])) { + $product['meta_keywords'] = $product['name']; + } + + if (!isset($product['meta_description']) || empty($product['meta_description'])) { + $product['meta_description'] = $product['name']; + } + $product_id = $this->products->add_product($product); } else { $this->products->update_product($product_id, $product); diff --git a/backend/core/ProductsAdmin.php b/backend/core/ProductsAdmin.php index 972a0455..192beb54 100644 --- a/backend/core/ProductsAdmin.php +++ b/backend/core/ProductsAdmin.php @@ -144,9 +144,7 @@ public function fetch() { } case 'delete': { /*Удалить товар*/ - foreach($ids as $id) { - $this->products->delete_product($id); - } + $this->products->delete_product($ids); break; } case 'duplicate': { diff --git a/backend/design/html/brands.tpl b/backend/design/html/brands.tpl index 6b1c0e0a..a5aa1fd5 100644 --- a/backend/design/html/brands.tpl +++ b/backend/design/html/brands.tpl @@ -140,7 +140,7 @@ {/if} -