File "beacon.class.php"

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

<?php

  namespace models;

  class Beacon extends \ArrayObject {

    static public function add($params) {
      global $db;
      mysqli_query($db, "INSERT INTO beacons (".
                        "name, ".
                        "uuid, ".
                        "identifier, ".
                        "major, ".
                        "minor, ".
                        "event_enter_region, ".
                        "event_exit_region, ".
                        "event_did_range, ".
                        "created_at".
                      ") VALUES (".
                        _text(_a($params, 'name')) . ", ".
                        _text(_a($params, 'uuid')) . ", ".
                        _text(_a($params, 'identifier')) . ", ".
                        _integer(_a($params, 'major')) . ", ".
                        _integer(_a($params, 'minor')) . ", ".
                        _text(_a($params, 'event_enter_region')) . ", ".
                        _text(_a($params, 'event_exit_region')) . ", ".
                        _text(_a($params, 'event_did_range')) . ", ".
                        "NOW()".
                      ");") 
                      or die("query error in Beacon::add: " . mysqli_error($db));
      return mysqli_insert_id($db);
    }

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

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

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

    static public function get_where($params) {
      $where = "";
      if (isset($params['q'])) {
        $where .= "AND (uuid like " . _text('%' . $params['q'] . '%') . ") ";
      }
      return $where;
    }

    static public function get_vocabulary($params=array()) {
      $result = array();
      foreach (Beacon::get($params) as $beacon) {
        $result[$beacon['id']] = $beacon['name'];
      }
      return $result;
    }

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

    public function json() {
      return array(
        "id" => $this['id'],
        "uuid" => $this['uuid'],
        "identifier" => $this['identifier'],
        "major" => (int)$this['major'],
        "minor" => (int)$this['minor'],
        "event_enter_region" => $this['event_enter_region'],
        "event_exit_region" => $this['event_exit_region'],
        "event_did_range" => $this['event_did_range'],
        "created_at" => $this['created_at'],
        "updated_at" => $this['updated_at'],
      );
    }

    public function update($params) {
      global $db;
      mysqli_query($db, "UPDATE beacons SET ".
                        "name = " . _text(_a($params, 'name')) . ", ".
                        "uuid = " . _text(_a($params, 'uuid')) . ", ".
                        "identifier = " . _text(_a($params, 'identifier')) . ", ".
                        "major = " . _integer(_a($params, 'major')) . ", ".
                        "minor = " . _integer(_a($params, 'minor')) . ", ".
                        "event_enter_region = " . _text(_a($params, 'event_enter_region')) . ", ".
                        "event_exit_region = " . _text(_a($params, 'event_exit_region')) . ", ".
                        "event_did_range = " . _text(_a($params, 'event_did_range')) . ", ".
                        "updated_at = NOW() ".
                      "WHERE id = " . _integer($this['id']) . ";") or
                      die("query error in Beacon::update: " . mysqli_error($db));
    }

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

  }

?>