File "usershoppinglist.class.php"

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

<?php

  namespace models;

  class UserShoppingList extends \ArrayObject {

    static public function add($params) {
      global $db;
      mysqli_query($db, "INSERT INTO users_shopping_list (".
                        "user_id, ".
                        "title, ".
                        "created_at".
                      ") VALUES (".
                        _integer(_a($params, 'user_id')) . ", ".
                        _text(_a($params, 'title')) . ", ".
                        "NOW()".
                      ");")
                      or die("query error in User::add: " . mysqli_error($db));
      return mysqli_insert_id($db);
    }

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

    public function delete() {
      global $db;
      mysqli_query($db, "DELETE FROM users_shopping_list ".
                        "WHERE id = " . _integer($this['id']) . ";") or
                        die("query error in UserShoppingList::delete: " . mysqli_error($db));
    }

    public function json() {
      return array(
        "id" => (int)$this['id'],
        "title" => $this['title'],
      );
    }

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

      $this['title'] = strip_tags($this['title']);
    }

  }