File "productcategory.class.php"

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

<?php

  namespace models;

  class ProductCategory extends \ArrayObject {

    static public function add($params) {
      global $db;
      mysqli_query($db, "INSERT INTO products_categories (".
                        "lang,".
                        "slug,".
                        "title,".
                        "image_list,".
                        "image_cover,".
                        "description,".
                        "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(_a($params, 'image_list'), 'asset')) . ", ".
                        _text(_a(_a($params, 'image_cover'), 'asset')) . ", ".
                        _text(_a($params, 'description')) . ", ".
                        _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 ProductCategory::add: " . mysqli_error($db));
    }

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

    static public function get_by_slug($slug) {
      global $db;
      $rs = mysqli_query($db, "SELECT * FROM products_categories ".
                              "WHERE slug = " . _text($slug) . " ".
                              "AND deleted_at IS NULL ".
                              "ORDER BY id DESC LIMIT 1;") or
            die("query error in ProductCategory::get_by_slug: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      if ($r) {
        return new ProductCategory($r);
      }
      return null;
    }

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

    static public function get_count($params=array()) {
      global $db;
      $where = ProductCategory::get_where($params);
      $rs = mysqli_query($db, "SELECT COUNT(*) AS count ".
                        "FROM products_categories ".
                        "WHERE deleted_at IS NULL $where;") or
            die("query error in ProductCategory::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']) {
        $where .= "AND (lower(title) like " . _text('%' . mb_strtolower($params['q']) . '%') . ") ";
      }
      if (isset($params['published']) && $params['published']) {
        $where .= "AND published = 1 ".
                    "AND (published_datetime IS NULL or published_datetime <= NOW()) ".
                    "AND (published_datetime2 IS NULL or published_datetime2 > NOW()) ";
      }
      return $where;
    }

    static public function get_vocabulary($params=array()) {
      global $BLOG_CATEGORIES;
      $result = array();
      foreach (ProductCategory::get($params, 0, 100) as $category) {
        $result[$category['id']] = mb_strtoupper($category['lang']) . ": " . $category['title'];
      }
      return $result;
    }

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

    public function get_product() { // mod
      return Product::get(array("category_id" => $this['id'], "published" => 1 ), 0, 64); // mod
    }

    public function update($params) {
      global $db;
      mysqli_query($db, "UPDATE products_categories SET ".
                            "lang = " . _text(_a($params, 'lang')) . ", ".
                            "slug = " . _text(_a($params, 'slug')) . ", ".
                            "title = " . _text(_a($params, 'title')) . ", ".
                            "image_list = " . _text(_a(_a($params, 'image_list'), 'asset')) . ", ".
                            "image_cover = " . _text(_a(_a($params, 'image_cover'), 'asset')) . ", ".
                            "description = " . _text(_a($params, 'description')) . ", ".
                            "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 ProductCategory::update: " . mysqli_error($db));
    }

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

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

  }

?>