Files
AtomicOld/api/Model.php

143 lines
3.5 KiB
PHP
Raw Normal View History

2026-02-14 19:34:54 +03:00
<?php
/**
* Simpla CMS
*
* @copyright 2011 Denis Pikusov
* @link http://simplacms.ru
* @author Denis Pikusov
*
*/
require_once('Simpla.php');
class Model extends Simpla
{
/*
*
* Функция возвращает массив брендов, удовлетворяющих фильтру
* @param $filter
*
*/
public function get_models($marka_id = 0)
{
$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']);
if($marka_id) $filter = $this->db->placehold(' WHERE b.marka_id=?', intval($marka_id));
// Выбираем все бренды
$query = $this->db->placehold("SELECT DISTINCT b.* FROM __model b $filter ORDER BY binary b.name");
$this->db->query($query);
$models = $this->db->results();
foreach($models as $model) $model->marka = $this->getMarka($model);
return $models;
}
/*
*
* Функция возвращает бренд по его id или url
* (в зависимости от типа аргумента, int - id, string - url)
* @param $id id или url поста
*
*/
public function get_model($id)
{
if(is_int($id))
$filter = $this->db->placehold('id = ?', $id);
else
$filter = $this->db->placehold('url = ?', $id);
$query = "SELECT * FROM __model WHERE $filter ORDER BY name LIMIT 1";
$this->db->query($query);
$model = $this->db->result();
if(!$model) return $model;
$model->marka = $this->getMarka($model);
return $model;
}
function getMarka($model){
$id = is_object($model) ? $model->marka_id : $model;
return $this->marka->get_brand( (int)$id);
}
/*
*
* Добавление бренда
* @param $brand
*
*/
public function add_model($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 __model SET ?%", $brand);
return $this->db->insert_id();
}
/*
*
* Обновление бренда(ов)
* @param $brand
*
*/
public function update_model($id, $brand)
{
$query = $this->db->placehold("UPDATE __model SET ?% WHERE id=? LIMIT 1", $brand, intval($id));
$this->db->query($query);
return $id;
}
/*
*
* Удаление бренда
* @param $id
*
*/
public function delete_model($id)
{
if(!empty($id))
{
$this->delete_image($id);
$query = $this->db->placehold("DELETE FROM __model 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 __model WHERE id=?", intval($brand_id));
$this->db->query($query);
$filename = $this->db->result('image');
if(!empty($filename))
{
$query = $this->db->placehold("UPDATE __model SET image=NULL WHERE id=?", $brand_id);
$this->db->query($query);
$query = $this->db->placehold("SELECT count(*) as count FROM __model WHERE image=? LIMIT 1", $filename);
$this->db->query($query);
$count = $this->db->result('count');
if($count == 0)
{
@unlink($this->config->root_dir.$this->config->model_images_dir.$filename);
}
}
}
}