<?php namespace models; class Search extends \ArrayObject { static public function add($params) { global $db; mysqli_query($db, "INSERT INTO search (". "id_type,". "id_ext,". "url,". "title,". "body,". "created_at) ". "VALUES (". _text($params["id_type"]) . ", ". _integer($params["id_ext"]) . ", ". _text($params["url"]) . ", ". _text($params["title"]) . ", ". _text($params["body"]) . ", ". "NOW());") or die("query error in Search::add: " . mysqli_error($db)); return mysqli_insert_id($db); } static public function get($params, $offset=0, $limit=25) { global $db; $fields = ""; $where = Search::get_where($params); if (isset($params['q']) && $params['q']) { $fields .= ", MATCH (s.title,s.body) AGAINST (" . _text($params['q']) . ")"; } $rs = mysqli_query($db, "SELECT s.* $fields". "FROM search s ". "LEFT JOIN blog_articles ba ON (s.id_type = 'NEWS' AND s.id_ext = ba.id) ". "LEFT JOIN pages pa ON (s.id_type = 'PAGE' AND s.id_ext = pa.id) ". "WHERE s.deleted_at IS NULL ". "AND (ba.id IS NULL OR (ba.published = 1 AND (ba.published_datetime IS NULL OR ba.published_datetime < NOW()) AND COALESCE(requires_login, false) = false)) ". "AND (pa.id IS NULL OR (pa.published = 1 AND (pa.published_datetime IS NULL OR pa.published_datetime < NOW()) AND (pa.published_datetime2 IS NULL OR pa.published_datetime2 >= NOW()) AND COALESCE(requires_login, false) = false)) ". "$where". "LIMIT " . _integer($limit) . " OFFSET " . _integer($offset) . ";") or die("query error in Search::get: " . mysqli_error($db)); $result = array(); while ($r = mysqli_fetch_assoc($rs)) { array_push($result, new Search($r)); } return $result; } static public function get_count($params) { global $db; $where = Search::get_where($params); $rs = mysqli_query($db, "SELECT COUNT(*) AS count ". "FROM search s ". "WHERE s.deleted_at IS NULL $where;") or die("query error in Search::get_count: " . mysqli_error($db)); $r = mysqli_fetch_assoc($rs); return $r['count']; } static public function get_where($params) { global $lang; $where = ""; if (isset($params['q']) && $params['q']) { $where .= " AND MATCH (s.title,s.body) AGAINST (" . _text($params['q']) . ")"; } return $where; } static public function get_by_id($id) { global $db; $rs = mysqli_query($db, "SELECT * ". "FROM search ". "WHERE deleted_at IS NULL AND id = " . _integer($id) . ";") or die("query error in Search::get_by_id: " . mysqli_error($db)); $r = mysqli_fetch_assoc($rs); if ($r) { return new Search($r); } } static public function get_by_external_id($id_type, $id_ext) { global $db; $rs = mysqli_query($db, "SELECT * ". "FROM search ". "WHERE deleted_at IS NULL AND id_type = " . _text($id_type) . " AND id_ext = " . _integer($id_ext) . ";") or die("query error in Search::get_by_id: " . mysqli_error($db)); $r = mysqli_fetch_assoc($rs); if ($r) { return new Search($r); } } static public function set($params) { $search = Search::get_by_external_id($params['id_type'], $params['id_ext']); if ($search) { $search->update($params); } else { Search::add($params); } } public function delete() { global $db; mysqli_query($db, "UPDATE search SET ". "deleted_at = NOW() ". "WHERE id = " . _integer($this['id']) . ";") or die("query error in Search::delete: " . mysqli_error($db)); } public function update($params) { global $db; mysqli_query($db, "UPDATE search SET ". "id_type = " . _text($params["id_type"]) . ", ". "id_ext = " . _integer($params["id_ext"]) . ", ". "url = " . _text($params["url"]) . ", ". "title = " . _text($params["title"]) . ", ". "body = " . _text($params["body"]) . ", ". "updated_at = " . "NOW() ". "WHERE id = " . _integer($this['id']) . ";") or die("query error in Search::update: " . mysqli_error($db)); } function __construct($value) { parent::__construct($value); } } ?>