File: //home/temp/yarusvl.ru/api/Stores.php
<?php
require_once('Mgc.php');
class Stores extends Mgc {
/*Выбираем все склады*/
public function get_stores($filter = array()) {
$limit = 100;
$page = 1;
$category_id_filter = '';
$category_join = '';
$visible_filter = '';
$product_id_filter = '';
$product_join = '';
$visible_store_filter = '';
$features_filter = '';
$other_filter = '';
if(isset($filter['limit'])) {
$limit = max(1, intval($filter['limit']));
}
if(isset($filter['page'])) {
$page = max(1, intval($filter['page']));
}
$sql_limit = $this->db->placehold(' LIMIT ?, ? ', ($page-1)*$limit, $limit);
if(isset($filter['visible'])) {
$visible_filter = $this->db->placehold('AND p.visible=?', intval($filter['visible']));
}
if(isset($filter['visible_store'])) {
$visible_store_filter = $this->db->placehold('AND s.visible=?', intval($filter['visible_store']));
}
if(isset($filter['product_id'])) {
$product_id_filter = $this->db->placehold('AND p.id in (?@)', (array)$filter['product_id']);
$product_join = $this->db->placehold("LEFT JOIN __products p ON p.store_id=s.id");
}
if(!empty($filter['category_id'])) {
$category_join = $this->db->placehold("LEFT JOIN __products p ON p.store_id=s.id LEFT JOIN __products_categories pc ON p.id = pc.product_id");
$category_id_filter = $this->db->placehold("AND pc.category_id in(?@) $visible_filter", (array)$filter['category_id']);
}
if(!empty($filter['features'])) {
foreach($filter['features'] as $feature=>$value) {
$features_filter .= $this->db->placehold('AND p.id in (SELECT product_id FROM __options WHERE feature_id=? AND translit in(?@) ) ', $feature, (array)$value);
}
if (empty($category_join)) {
$features_filter .= $visible_filter;
$category_join = $this->db->placehold("LEFT JOIN __products p ON (p.store_id=s.id)");
}
}
if (!empty($filter['other_filter'])) {
$other_filter = "AND (";
if (in_array("featured", $filter['other_filter'])) {
$other_filter .= "p.featured=1 OR ";
}
if (in_array("discounted", $filter['other_filter'])) {
$other_filter .= "(SELECT 1 FROM __variants pv WHERE pv.product_id=p.id AND pv.compare_price>0 LIMIT 1) = 1 OR ";
}
$other_filter = substr($other_filter, 0, -4).")";
if (empty($category_join)) {
$other_filter .= $visible_filter;
$category_join = $this->db->placehold("LEFT JOIN __products p ON (p.store_id=s.id)");
}
}
$lang_sql = $this->languages->get_query(array('object'=>'store'));
// Выбираем все бренды
$query = $this->db->placehold("SELECT
DISTINCT s.id,
s.url,
s.image,
s.last_modify,
s.visible,
s.position,
s.code,
$lang_sql->fields
FROM __stores s
$lang_sql->join
$category_join
$product_join
WHERE
1
$category_id_filter
$features_filter
$visible_store_filter
$product_id_filter
$other_filter
ORDER BY s.position
$sql_limit
");
// echo $query;
$this->db->query($query);
//var_dump($this->db->results());
return $this->db->results();
}
public function count_stores($filter = array()) {
$category_id_filter = '';
$category_join = '';
$visible_filter = '';
$product_id_filter = '';
$product_join = '';
$visible_store_filter = '';
$features_filter = '';
$other_filter = '';
if(isset($filter['visible'])) {
$visible_filter = $this->db->placehold('AND p.visible=?', intval($filter['visible']));
}
if(isset($filter['visible_store'])) {
$visible_store_filter = $this->db->placehold('AND s.visible=?', intval($filter['visible_store']));
}
if(isset($filter['product_id'])) {
$product_id_filter = $this->db->placehold('AND p.id in (?@)', (array)$filter['product_id']);
$product_join = $this->db->placehold("LEFT JOIN __products p ON p.store_id=s.id");
}
if(!empty($filter['category_id'])) {
$category_join = $this->db->placehold("LEFT JOIN __products p ON p.store_id=s.id LEFT JOIN __products_categories pc ON p.id = pc.product_id");
$category_id_filter = $this->db->placehold("AND pc.category_id in(?@) $visible_filter", (array)$filter['category_id']);
}
if(!empty($filter['features'])) {
foreach($filter['features'] as $feature=>$value) {
$features_filter .= $this->db->placehold('AND p.id in (SELECT product_id FROM __options WHERE feature_id=? AND translit in(?@) ) ', $feature, (array)$value);
}
if (empty($category_join)) {
$features_filter .= $visible_filter;
$category_join = $this->db->placehold("LEFT JOIN __products p ON (p.store_id=s.id)");
}
}
if (!empty($filter['other_filter'])) {
$other_filter = "AND (";
if (in_array("featured", $filter['other_filter'])) {
$other_filter .= "p.featured=1 OR ";
}
if (in_array("discounted", $filter['other_filter'])) {
$other_filter .= "(SELECT 1 FROM __variants pv WHERE pv.product_id=p.id AND pv.compare_price>0 LIMIT 1) = 1 OR ";
}
$other_filter = substr($other_filter, 0, -4).")";
if (empty($category_join)) {
$other_filter .= $visible_filter;
$category_join = $this->db->placehold("LEFT JOIN __products p ON (p.store_id=s.id)");
}
}
$lang_sql = $this->languages->get_query(array('object'=>'store'));
// Выбираем все бренды
$query = $this->db->placehold("SELECT
count(distinct s.id) as count
FROM __stores s
$lang_sql->join
$category_join
$product_join
WHERE
1
$category_id_filter
$features_filter
$visible_store_filter
$product_id_filter
$other_filter
");
$this->db->query($query);
return $this->db->result('count');
}
/*Выбираем конкретный бренд*/
public function get_store($id) {
if (empty($id)) {
return false;
}
if(is_int($id)) {
$filter = $this->db->placehold('AND s.id = ?', intval($id));
} else {
$filter = $this->db->placehold('AND s.url = ?', $id);
}
$lang_sql = $this->languages->get_query(array('object'=>'store'));
$query = "SELECT
s.*,
$lang_sql->fields
FROM __stores s
$lang_sql->join
WHERE
1
$filter
LIMIT 1
";
$this->db->query($query);
return $this->db->result();
}
/*Добавление бренда*/
public function add_store($store) {
$store = (object)$store;
$store->url = preg_replace("/[\s]+/ui", '', $store->url);
$store->url = strtolower(preg_replace("/[^0-9a-z]+/ui", '', $store->url));
if(empty($store->url)) {
$store->url = $this->translit_alpha($store->name);
}
while($this->get_store((string)$store->url)) {
if(preg_match('/(.+)([0-9]+)$/', $store->url, $parts)) {
$store->url = $parts[1].''.($parts[2]+1);
} else {
$store->url = $store->url.'2';
}
}
// Проверяем есть ли мультиязычность и забираем описания для перевода
$result = $this->languages->get_description($store, 'store');
$this->db->query("INSERT INTO __stores SET ?%", $store);
$id = $this->db->insert_id();
$this->db->query("UPDATE __stores SET position=id WHERE id=? LIMIT 1", $id);
// Если есть описание для перевода. Указываем язык для обновления
if(!empty($result->description)) {
$this->languages->action_description($id, $result->description, 'store');
}
return $id;
}
/*Обновление бренда*/
public function update_store($id, $store) {
$store = (object)$store;
// Проверяем есть ли мультиязычность и забираем описания для перевода
$result = $this->languages->get_description($store, 'store');
$query = $this->db->placehold("UPDATE __stores SET ?% WHERE id=? LIMIT 1", $store, intval($id));
$this->db->query($query);
// Если есть описание для перевода. Указываем язык для обновления
if(!empty($result->description)) {
$this->languages->action_description($id, $result->description, 'store', $this->languages->lang_id());
}
return $id;
}
/*Удаление бренда*/
public function delete_store($id) {
if(!empty($id)) {
$this->image->delete_image($id, 'image', 'stores', $this->config->original_stores_dir, $this->config->resized_stores_dir);
$query = $this->db->placehold("DELETE FROM __stores WHERE id=? LIMIT 1", $id);
$this->db->query($query);
$query = $this->db->placehold("UPDATE __products SET store_id=NULL WHERE store_id=?", $id);
$this->db->query($query);
$this->db->query("DELETE FROM __lang_stores WHERE store_id=?", $id);
}
}
public function get_stores_variant($variant_id){
$filter = $this->db->placehold('AND s.variant_id = ?', intval($variant_id));
$query = "SELECT
s.*
FROM __stores_variant s
WHERE
1
$filter
";
//echo $query;
$this->db->query($query);
$resulr = $this->db->results();
$out = array();
foreach ($resulr as $res){
$out[$res->store_id] = $res
; }
return $out;
}
public function update_stores_variant($variant_id,$store_id,$stock){
$query = $this->db->placehold("UPDATE __stores_variant SET stock=? WHERE variant_id=? AND store_id=?", $stock,$variant_id,$store_id);
$this->db->query($query);
}
public function update_stores_variant_id($id,$stock){
$query = $this->db->placehold("UPDATE __stores_variant SET stock=? WHERE id=?", $stock,$id);
$this->db->query($query);
}
public function insert_stores_variant($stock){
$query = $this->db->placehold("INSERT INTO __stores_variant SET ?%", $stock);
$this->db->query($query);
}
}