File "notification.class.php"

Full Path: /srv/www/www.cadoro.it/src/models/notification.class.php
File size: 6.14 KB
MIME-type: text/x-php
Charset: utf-8

<?php

  namespace models;

  class Notification extends \ArrayObject {

    static public function add($params) {
      global $db;
      mysqli_query($db, "INSERT INTO notifications (".
                        "title, ".
                        "logo,".
                        "header,".
                        "body,".
                        "target, ".
                        "target_beacon_id, ".
                        "message, ".
                        "data, ".
                        "status, ".
                        "created_at".
                      ") VALUES (".
                        _text(_a($params, 'title')) . ", ".
                        _text(_a(_a($params, 'logo'), 'asset')) . ", ".
                        _text(_a(_a($params, 'header'), 'asset')) . ", ".
                        _text(_a($params, 'body')) . ", ".
                        _text(_a($params, 'target')) . ", ".
                        _integer(_a($params, 'target_beacon_id')) . ", ".
                        _text(_a($params, 'message')) . ", ".
                        _text(_a($params, 'data')) . ", ".
                        _integer(_a($params, 'status')) . ", ".
                        "NOW()".
                      ");") 
                      or die("query error in Notification::add: " . mysqli_error($db));
      return mysqli_insert_id($db);
    }

    static public function get_by_id($id) {
      global $db;
      $rs = mysqli_query($db, "SELECT * FROM notifications WHERE id = " . _integer($id) . ";") or
            die("query error in Notification::get_by_id: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      if ($r) {
        return new Notification($r);
      }
      return null;
    }

    static public function get($params=array(), $offset=0, $limit=25) {
      global $db;
      $where = Notification::get_where($params);
      $rs = mysqli_query($db, "SELECT * ".
                        "FROM notifications ".
                        "WHERE deleted_at IS NULL $where ".
                        "ORDER BY created_at DESC ".
                        "LIMIT " . _integer($limit) . " OFFSET " . _integer($offset) . ";") or
            die("query error in Notification::get: " . mysqli_error($db));
      $results = array();
      while ($r = mysqli_fetch_assoc($rs)) {
        array_push($results, new Notification($r));
      }
      return $results;
    }

    static public function get_count($params) {
      global $db;
      $where = Notification::get_where($params);
      $rs = mysqli_query($db, "SELECT COUNT(*) AS count ".
                        "FROM notifications ".
                        "WHERE deleted_at IS NULL $where;") or
            die("query error in Notification::get_count: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      return $r['count'];
    }

    static public function get_where($params) {
      $where = "";
      if (isset($params['push']) && $params['push']) {
        $where .= "AND (target_status = 1 AND target_datetime <= now()) ";
      }
      if (isset($params['q'])) {
        $where .= "AND (title like " . _text('%' . $params['q'] . '%') . ") ";
      }
      return $where;
    }

    static public function get_vocabulary($params=array()) {
      $result = array();
      foreach (Notification::get($params) as $user) {
        $result[$user['id']] = $user['lastname'] . " " . $user['firstname'];
      }
      return $result;
    }

    public function delete() {
      global $db;
      mysqli_query($db, "UPDATE notifications SET ".
                        "deleted_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in Notification::delete: " . mysqli_error($db));
    }

    public function json() {
      $res = array(
        "id" => (int)$this['id'],
        "title" => $this['title'],
        "logo" => $this['logo'] ? (BASE_URL . "/assets/" . $this['logo']) : null,
        "header" => $this['header'] ? (BASE_URL . "/assets/" . $this['header']) : null,
        "body" => $this['body'],
        "target" => $this['target'],
        "target_beacon_id" => (int)$this['target_beacon_id'],
        "message" => $this['message'],
        "data" => json_decode($this['data'], true),
        "status" => (int)$this['status'],
        "created_at" => $this['created_at'],
        "updated_at" => $this['updated_at'],
      );
      if (!$res['data']) {
        $res['data'] = array();
      }
      $res['data']['notification_id'] = (int)$this['id'];
      return $res;
    }

    public function update($params) {
      global $db;
      mysqli_query($db, "UPDATE notifications SET ".
                        "title = " . _text(_a($params, 'title')) . ", ".
                        "logo = " . _text(_a(_a($params, 'logo'), 'asset')) . ", ".
                        "header = " . _text(_a(_a($params, 'header'), 'asset')) . ", ".
                        "body = " . _text(_a($params, 'body')) . ", ".
                        "target = " . _text(_a($params, 'target')) . ", ".
                        "target_beacon_id = " . _integer(_a($params, 'target_beacon_id')) . ", ".
                        "message = " . _text(_a($params, 'message')) . ", ".
                        "data = " . _text(_a($params, 'data')) . ", ".
                        "status = " . _integer(_a($params, 'status')) . ", ".
                        "updated_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in Notification::update: " . mysqli_error($db));
    }

    public function update_push($params) {
      global $db;
      mysqli_query($db, "UPDATE notifications SET ".
                        "target_filter = " . _text(_a($params, 'target_filter')) . ", ".
                        "target_datetime = " . _datetime(_a($params, 'target_datetime')) . ", ".
                        "target_status = " . _integer(_a($params, 'target_status')) . ", ".
                        "updated_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in Notification::update: " . mysqli_error($db));
    }

    function __construct($value) {
      parent::__construct($value);
    }

  }

?>