File "promotion.class.php"
Full Path: /srv/www/www.cadoro.it/src/models/promotion.class.php
File size: 6.43 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace models;
class Promotion extends \ArrayObject {
static public function add($params) {
global $db;
mysqli_query($db, "INSERT INTO promotions (".
"lang,".
"slug,".
"title,".
"date,".
"image,".
"image_small,".
"description,".
"stores,".
"published,".
"published_datetime,".
"published_datetime2,".
"seo_title,".
"seo_image,".
"seo_description,".
"seo_keywords,".
"created_at".
") VALUES (".
_text(_a($params, 'lang')) . ", ".
_text(_a($params, 'slug')) . ", ".
_text(_a($params, 'title')) . ", ".
_date(_a($params, 'date')) . ", ".
_text(_a(_a($params, 'image'), 'asset')) . ", ".
_text(_a(_a($params, 'image_small'), 'asset')) . ", ".
_text(_a($params, 'description')) . ", ".
_from_array(_a($params, 'stores')) . ", ".
_integer(_a($params, 'published')) . ", ".
_datetime(_a($params, 'published_datetime')) . ", ".
_datetime(_a($params, 'published_datetime2')) . ", ".
_text(_a($params, 'seo_title')) . ", ".
_text(_a(_a($params, 'seo_image'), 'asset')) . ", ".
_text(_a($params, 'seo_description')) . ", ".
_text(_a($params, 'seo_keywords')) . ", ".
"NOW()".
");")
or die("query error in Promotion::add: " . mysqli_error($db));
}
static public function get_by_id($id) {
global $db;
$rs = mysqli_query($db, "SELECT * FROM promotions WHERE id = " . _integer($id) . ";") or
die("query error in Promotion::get_by_id: " . mysqli_error($db));
$r = mysqli_fetch_assoc($rs);
if ($r) {
return new Promotion($r);
}
return null;
}
static public function get_by_slug($slug) {
global $db;
$rs = mysqli_query($db, "SELECT * FROM promotions ".
"WHERE slug = " . _text($slug) . " ".
"AND deleted_at IS NULL ".
"ORDER BY id DESC LIMIT 1;") or
die("query error in Promotion::get_by_slug: " . mysqli_error($db));
$r = mysqli_fetch_assoc($rs);
if ($r) {
return new Promotion($r);
}
return null;
}
static public function get($params=array(), $offset=0, $limit=25) {
global $db;
$where = Promotion::get_where($params);
$rs = mysqli_query($db, "SELECT * ".
"FROM promotions ".
"WHERE deleted_at IS NULL $where ".
"ORDER BY title ".
"LIMIT " . _integer($limit) . " OFFSET " . _integer($offset) . ";") or
die("query error in Promotion::get: " . mysqli_error($db));
$results = array();
while ($r = mysqli_fetch_assoc($rs)) {
array_push($results, new Promotion($r));
}
return $results;
}
static public function get_count($params=array()) {
global $db;
$where = Promotion::get_where($params);
$rs = mysqli_query($db, "SELECT COUNT(*) AS count ".
"FROM promotions ".
"WHERE deleted_at IS NULL $where;") or
die("query error in Promotion::get_count: " . mysqli_error($db));
$r = mysqli_fetch_assoc($rs);
return $r['count'];
}
static public function get_where($params=array()) {
$where = "";
if (isset($params['q']) && $params['q']) {
$where .= "AND (lower(title) like " . _text('%' . mb_strtolower($params['q']) . '%') . ") ";
}
return $where;
}
static public function get_vocabulary($params=array()) {
$result = array();
foreach (Promotion::get($params, 0, 100) as $store) {
$result[$store['id']] = $store['title'];
}
return $result;
}
public function delete() {
global $db;
mysqli_query($db, "UPDATE promotions SET ".
"deleted_at = NOW() ".
"WHERE id = " . _integer($this['id']) . ";") or
die("query error in Promotion::delete: " . mysqli_error($db));
}
public function update($params) {
global $db;
mysqli_query($db, "UPDATE promotions SET ".
"lang = " . _text(_a($params, 'lang')) . ", ".
"slug = " . _text(_a($params, 'slug')) . ", ".
"title = " . _text(_a($params, 'title')) . ", ".
"date = " . _date(_a($params, 'date')) . ", ".
"image = " . _text(_a(_a($params, 'image'), 'asset')) . ", ".
"image_small = " . _text(_a(_a($params, 'image_small'), 'asset')) . ", ".
"description = " . _text(_a($params, 'description')) . ", ".
"stores = " . _from_array(_a($params, 'stores')) . ", ".
"published = " . _integer(_a($params, 'published')) . ", ".
"published_datetime = " . _datetime(_a($params, 'published_datetime')) . ", ".
"published_datetime2 = " . _datetime(_a($params, 'published_datetime2')) . ", ".
"seo_title = " . _text(_a($params, 'seo_title')) . ", ".
"seo_image = " . _text(_a(_a($params, 'seo_image'), 'asset')) . ", ".
"seo_description = " . _text(_a($params, 'seo_description')) . ", ".
"seo_keywords = " . _text(_a($params, 'seo_keywords')) . ", ".
"updated_at = NOW() ".
"WHERE id = " . _integer($this['id']) . ";") or
die("query error in Promotion::update: " . mysqli_error($db));
}
function __construct($value) {
parent::__construct($value);
$this['stores'] = $this['stores'] ? _to_array($this['stores']) : null;
}
}
?>