File "page.class.php"
Full Path: /srv/www/www.cadoro.it/src/models/page.class.php
File size: 9.32 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace models;
class Page extends \ArrayObject {
static public function add($params) {
global $db;
mysqli_query($db, "INSERT INTO pages (".
"lang,".
"slug,".
"title,".
"subtitle,".
"link,".
"hero,".
"hero_mobile,".
"hero_video,".
"extra_props,".
"header,".
"content,".
"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')) . ", ".
_text(_a($params, 'subtitle')) . ", ".
_text(_a($params, 'link')) . ", ".
_text(_a(_a($params, 'hero'), 'asset')) . ", ".
_text(_a(_a($params, 'hero_mobile'), 'asset')) . ", ".
_text(_a(_a($params, 'hero_video'), 'asset')) . ", ".
_text(_a(_a($params, 'extra_props'), 'asset')) . ", ".
_text(_a(_a($params, 'header'), 'asset')) . ", ".
_text(_a($params, 'content')) . ", ".
_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 Page::add: " . mysqli_error($db));
}
static public function get_by_id($id) {
global $db;
$rs = mysqli_query($db, "SELECT * FROM pages WHERE id = " . _integer($id) . ";") or
die("query error in Page::get_by_id: " . mysqli_error($db));
$r = mysqli_fetch_assoc($rs);
if ($r) {
return new Page($r);
}
return null;
}
static public function get_by_slug($slug) {
global $db;
$rs = mysqli_query($db, "SELECT * FROM pages ".
"WHERE slug = " . _text($slug) . " ".
"AND deleted_at IS NULL ".
"AND published = 1 ".
"AND (published_datetime IS NULL or published_datetime <= NOW()) ".
"AND (published_datetime2 IS NULL or published_datetime2 > NOW()) ".
"ORDER BY id DESC LIMIT 1;") or
die("query error in Page::get_by_slug: " . mysqli_error($db));
$r = mysqli_fetch_assoc($rs);
if ($r) {
return new Page($r);
}
return null;
}
static public function get($params=array(), $offset=0, $limit=25) {
global $db;
$where = Page::get_where($params);
$rs = mysqli_query($db, "SELECT * ".
"FROM pages ".
"WHERE deleted_at IS NULL $where ".
"ORDER BY id ".
"LIMIT " . _integer($limit) . " OFFSET " . _integer($offset) . ";") or
die("query error in Page::get: " . mysqli_error($db));
$results = array();
while ($r = mysqli_fetch_assoc($rs)) {
array_push($results, new Page($r));
}
return $results;
}
static public function get_count($params=array()) {
global $db;
$where = Page::get_where($params);
$rs = mysqli_query($db, "SELECT COUNT(*) AS count ".
"FROM pages ".
"WHERE deleted_at IS NULL $where;") or
die("query error in Page::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']) {
$or = array();
foreach (preg_split("/[\s\n]+/", $params['q']) as $q) {
$or[] = "(lower(title) like " . _text('%' . mb_strtolower($q) . '%') . " OR " .
"lower(content) like " . _text('%' . mb_strtolower($q) . '%') . ")";
}
if ($or) {
$where .= "AND (" . implode(" OR ", $or) . ") ";
}
}
if (isset($params['published']) && $params['published']) {
$where .= "AND (published = 1) ";
$where .= "AND (published_datetime IS NULL OR published_datetime < NOW()) ";
$where .= "AND (published_datetime2 IS NULL OR published_datetime2 > NOW()) ";
}
return $where;
}
static public function get_vocabulary($params=array()) {
$result = array();
foreach (Page::get($params, 0, 100) as $page) {
$result[$page['id']] = $page['slug'];
}
return $result;
}
public function delete() {
global $db;
mysqli_query($db, "UPDATE pages SET ".
"deleted_at = NOW() ".
"WHERE id = " . _integer($this['id']) . ";") or
die("query error in Page::delete: " . mysqli_error($db));
}
public function content() {
$result = array();
foreach ($this['content'] as $c) {
$result[] = $c;
}
return $result;
}
static public function content_page($page) {
return array(
'name' => _a($page, 'name'),
'rel' => _a($page, 'rel'),
'link' => _a($page, 'link'),
'class' => _a($page, 'class'),
'children' => array_map('models\Page::content_page', _a($page, 'children', array())),
);
}
public function add_content($params) {
$this['content'][] = $params;
$this->update_content($this);
}
public function set_content($params) {
foreach ($this['content'] as $i => $c) {
if ($c['id'] == $params['id']) {
$this['content'][$i] = $params;
break;
}
}
$this->update_content($this);
}
public function unset_content($content_id) {
foreach ($this['content'] as $i => $c) {
if ($c['id'] == $content_id) {
unset($this['content'][$i]);
break;
}
}
$this->update_content($this);
}
public function update($params) {
global $db;
mysqli_query($db, "UPDATE pages SET ".
"lang = " . _text(_a($params, 'lang')) . ", ".
"slug = " . _text(_a($params, 'slug')) . ", ".
"title = " . _text(_a($params, 'title')) . ", ".
"subtitle = " . _text(_a($params, 'subtitle')) . ", ".
"link = " . _text(_a($params, 'link')) . ", ".
"hero = " . _text(_a(_a($params, 'hero'), 'asset')) . ", ".
"hero_mobile = " . _text(_a(_a($params, 'hero_mobile'), 'asset')) . ", ".
"hero_video = " . _text(_a(_a($params, 'hero_video'), 'asset')) . ", ".
"extra_props = " . _text(_a($params, 'extra_props')) . ", ".
"header = " . _text(_a(_a($params, 'header'), 'asset')) . ", ".
"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 Page::update: " . mysqli_error($db));
}
public function update_content($params) {
global $db;
$content = _a($params, 'content', array());
usort($content, function ($a, $b) {
if ($a['sorting'] == $b['sorting']) {
return 0;
}
return ($a['sorting'] < $b['sorting']) ? -1 : 1;
});
mysqli_query($db, "UPDATE pages SET ".
"content = " . _text_json($content) . ", ".
"updated_at = NOW() ".
"WHERE id = " . _integer($this['id']) . ";") or
die("query error in Page::update: " . mysqli_error($db));
}
function __construct($value) {
parent::__construct($value);
$this['content'] = $this['content'] ? json_decode($this['content'], true) : array();
}
}
?>