Files
AtomicOld/api/Marka.php
2026-02-14 19:34:54 +03:00

137 lines
3.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Simpla CMS
*
* @copyright 2011 Denis Pikusov
* @link http://simplacms.ru
* @author Denis Pikusov
*
*/
require_once('Simpla.php');
class Marka extends Simpla
{
/*
*
* Функция возвращает массив брендов, удовлетворяющих фильтру
* @param $filter
*
*/
public function get_brands($filter = array())
{
$brands = array();
$category_id_filter = '';
//if(!empty($filter['category_id']))
// $category_id_filter = $this->db->placehold('LEFT JOIN __products p ON p.brand_id=b.id LEFT JOIN __products_categories pc ON p.id = pc.product_id WHERE pc.category_id in(?@)', (array)$filter['category_id']);
// Выбираем все бренды
$query = $this->db->placehold("SELECT DISTINCT b.*
FROM __marka b $category_id_filter ORDER BY binary b.name");
$this->db->query($query);
return $this->db->results();
}
/*
*
* Функция возвращает бренд по его id или url
* (в зависимости от типа аргумента, int - id, string - url)
* @param $id id или url поста
*
*/
public function get_brand($id)
{
if(is_int($id))
$filter = $this->db->placehold('id = ?', $id);
else
$filter = $this->db->placehold('url = ?', $id);
$query = "SELECT * FROM __marka WHERE $filter ORDER BY name LIMIT 1";
$this->db->query($query);
return $this->db->result();
}
function getModels($marka){
$id = is_object($marka) ? $marka->id : $marka;
return $this->model->get_models( (int)$id);
}
/*
*
* Добавление бренда
* @param $brand
*
*/
public function add_brand($brand)
{
$brand = (array)$brand;
if(empty($brand['url']))
{
$brand['url'] = preg_replace("/[\s]+/ui", '_', $brand['name']);
$brand['url'] = strtolower(preg_replace("/[^0-9a-zа-я_]+/ui", '', $brand['url']));
}
$this->db->query("INSERT INTO __marka SET ?%", $brand);
return $this->db->insert_id();
}
/*
*
* Обновление бренда(ов)
* @param $brand
*
*/
public function update_brand($id, $brand)
{
$query = $this->db->placehold("UPDATE __marka SET ?% WHERE id=? LIMIT 1", $brand, intval($id));
$this->db->query($query);
return $id;
}
/*
*
* Удаление бренда
* @param $id
*
*/
public function delete_brand($id)
{
if(!empty($id))
{
$this->delete_image($id);
$query = $this->db->placehold("DELETE FROM __marka WHERE id=? LIMIT 1", $id);
$this->db->query($query);
//$query = $this->db->placehold("UPDATE __products SET brand_id=NULL WHERE brand_id=?", $id);
//$this->db->query($query);
}
}
/*
*
* Удаление изображения бренда
* @param $id
*
*/
public function delete_image($brand_id)
{
$query = $this->db->placehold("SELECT image FROM __marka WHERE id=?", intval($brand_id));
$this->db->query($query);
$filename = $this->db->result('image');
if(!empty($filename))
{
$query = $this->db->placehold("UPDATE __marka SET image=NULL WHERE id=?", $brand_id);
$this->db->query($query);
$query = $this->db->placehold("SELECT count(*) as count FROM __marka WHERE image=? LIMIT 1", $filename);
$this->db->query($query);
$count = $this->db->result('count');
if($count == 0)
{
@unlink($this->config->root_dir.$this->config->marka_images_dir.$filename);
}
}
}
}