File "flyerproduct.class.php"

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

<?php

  namespace models;

  class FlyerProduct extends \ArrayObject {

    static public function add($params) {
      global $db;
      mysqli_query($db, "INSERT INTO flyers_products (".
                    "flyer_id,".
                    "category,".
                    "subcategory,".
                    "code,".
                    "title,".
                    "description,".
                    "brand,".
                    "offer_price,".
                    "offer_price_old,".
                    "offer_discount,".
                    "offer_multi,".
                    "offer_type,".
                    "notes,".
                    "ean,".
                    "stores,".
                    "stores_card,".
                    "label,".
                    "label2,".
                    "date_begin,".
                    "date_end,".
                    "image_thumb,".
                    "image_full,".
                    "below_cost,".
                    "homepage,".
                    "created_at) ".
                  "VALUES (".
                    _integer($params['flyer_id']) . ", ".
                    _text($params['category']) . ", ".
                    _text($params['subcategory']) . ", ".
                    _text($params['code']) . ", ".
                    _text($params['title']) . ", ".
                    _text($params['description']) . ", ".
                    _text($params['brand']) . ", ".
                    _numeric($params['offer_price']) . ", ".
                    _numeric($params['offer_price_old']) . ", ".
                    _integer($params['offer_discount']) . ", ".
                    _text($params['offer_multi']) . ", ".
                    _text($params['offer_type']) . ", ".
                    _text($params['notes']) . ", ".
                    _text($params['ean']) . ", ".
                    _text($params['stores']) . ", ".
                    _text($params['stores_card']) . ", ".
                    _text($params['label']) . ", ".
                    _text($params['label2']) . ", ".
                    _date($params['date_begin']) . ", ".
                    _date($params['date_end']) . ", ".
                    _text($params['image_thumb']) . ", ".
                    _text($params['image_full']) . ", ".
                    _boolean($params['below_cost']) . ", ".
                    _integer($params['homepage']) . ", ".
                    "NOW());") or
                  die("query error in Flyer::add: " . mysqli_error($db));
      return mysqli_insert_id($db);
    }

    static public function get_by_flyer_and_id($flyer_id, $id) {
      global $db;
      $rs = mysqli_query($db, "SELECT * ".
                        "FROM flyers_products ".
                        "WHERE deleted_at IS NULL AND flyer_id = " . _integer($flyer_id) . " AND id = " . _integer($id) . " ".
                        "ORDER BY created_at DESC LIMIT 1;") or
            die("query error in FlyerProduct::get_by_flyer_and_id: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      if ($r) {
        return new FlyerProduct($r);
      }
    }

    static public function get_by_flyer_and_code($flyer_id, $code) {
      global $db;
      $rs = mysqli_query($db, "SELECT * ".
                        "FROM flyers_products ".
                        "WHERE code = " . _text($code) . " AND flyer_id = " . _integer($flyer_id) . " ".
                        "ORDER BY created_at DESC LIMIT 1;") or
            die("query error in FlyerProduct::get_by_flyer_and_code: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      if ($r) {
        return new FlyerProduct($r);
      }
    }

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

    static public function get_by_code($code) {
      global $db;
      $rs = mysqli_query($db, "SELECT * ".
                        "FROM flyers_products ".
                        "WHERE deleted_at IS NULL AND code = " . _text($code) . " ".
                        "ORDER BY created_at DESC LIMIT 1;") or
            die("query error in FlyerProduct::get_by_code: " . mysqli_error($db));
      $r = mysqli_fetch_assoc($rs);
      if ($r) {
        return new FlyerProduct($r);
      }
    }

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

    static public function get_count($params) {
      global $db;
      $where = FlyerProduct::get_where($params);
      $rs = mysqli_query($db, "SELECT COUNT(*) AS count ".
                        "FROM flyers_products ".
                        "WHERE deleted_at IS NULL $where;") or
            die("query error in FlyerProduct::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 (lower(title) like " . _text('%' . $params['q'] . '%') . ") ";
      }
      if (isset($params['dates']) && $params['dates']) {
        $where .= "AND (current_date BETWEEN date_begin AND date_end) ";
      }
      if (isset($params['flyer_id']) && $params['flyer_id']) {
        $where .= "AND (flyer_id = " . _integer($params['flyer_id']) . ") ";
      }
      if (isset($params['category']) && $params['category']) {
        $where .= "AND (category = " . _text($params['category']) . ") ";
      }
      if (isset($params['label']) && $params['label']) {
        $where .= "AND (label LIKE " . _text('%' . $params['label'] . '%') . ") ";
      }
      if (isset($params['store']) && $params['store']) {
        $where .= "AND (stores LIKE " . _text('%;' . $params['store'] . ';%') . ") ";
      }
      if (isset($params['homepage']) && $params['homepage']) {
        $where .= "AND (homepage = 1) ";
      }
      if (isset($params['filter']) && $params['filter'] == 'missing-image') {
        $where .= "AND (image_thumb IS NULL) ";
      }
      if (isset($params['filter']) && $params['filter'] == 'homepage') {
        $where .= "AND (homepage = 1) ";
      }
      return $where;
    }

    static public function search($params) {
      global $lang, $db;
      $where = Flyer::get_where($params);
      $rs = mysqli_query($db, "SELECT DISTINCT code, title, image_thumb, image_full ".
                        "FROM flyers_products ".
                        "WHERE deleted_at IS NULL $where ".
                        "ORDER BY title;") or
            die("query error in Flyer::get: " . mysqli_error($db));
      $results = array();
      while ($r = mysqli_fetch_assoc($rs)) {
        array_push($results, new FlyerProduct($r));
      }
      return $results;
    }


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

    public function update($params) {
      global $db;
      mysqli_query($db, "UPDATE flyers_products SET ".
                    "category = " . _text($params['category']) . ", ".
                    "subcategory = " . _text($params['subcategory']) . ", ".
                    "code = " . _text(sprintf("%07d", $params['code'])) . ", ".
                    "title = " . _text($params['title']) . ", ".
                    "description = " . _text($params['description']) . ", ".
                    "brand = " . _text($params['brand']) . ", ".
                    "offer_price = " . _numeric($params['offer_price']) . ", ".
                    "offer_price_old = " . _numeric(_a($params, 'offer_price_old')) . ", ".
                    "offer_discount = " . _integer($params['offer_discount']) . ", ".
                    "offer_multi = " . _text($params['offer_multi']) . ", ".
                    "offer_type = " . _text($params['offer_type']) . ", ".
                    "notes = " . _text($params['notes']) . ", ".
                    "ean = " . _text($params['ean']) . ", ".
                    "stores = " . _text($params['stores']) . ", ".
                    "stores_card = " . _text($params['stores_card']) . ", ".
                    "label = " . _text($params['label']) . ", ".
                    "label2 = " . _text($params['label2']) . ", ".
                    "date_begin = " . _date($params['date_begin']) . ", " .
                    "date_end = " . _date($params['date_end']) . ", " .
                    "image_full = COALESCE(" . _text($params['image_full']) . ", image_full), ".
                    "image_thumb = COALESCE(" . _text($params['image_thumb']) . ", image_thumb), ".
                    "below_cost = " . _integer($params['below_cost']) . ", ".
                    "homepage = " . _integer($params['homepage']) . ", ".
                    "updated_at = " . "NOW() ".
                  "WHERE id = " . _integer($this['id']) . ";") or
                  die("query error in FlyerProduct::update: " . mysqli_error($db));
    }

    public function update_description($params) {
      global $db;
      mysqli_query($db, "UPDATE flyers_products SET ".
                    "title = " . _text($params['title']) . ", ".
                    "description = " . _text($params['description']) . ", ".
                    "updated_at = NOW(), ".
                    "deleted_at = NULL ".
                  "WHERE id = " . _integer($this['id']) . ";") or
                  die("query error in FlyerProduct::update: " . mysqli_error($db));
    }

    public function update_homepage($homepage) {
      global $db;
      mysqli_query($db, "UPDATE flyers_products SET ".
                    "homepage = " . _integer($homepage) . ", ".
                    "updated_at = NOW() ".
                  "WHERE id = " . _integer($this['id']) . ";") or
                  die("query error in FlyerProduct::update_homepage: " . mysqli_error($db));
    }

    function __construct($value) {
      parent::__construct($value);
      if (!$this['image_thumb']) {
        $this['image_thumb'] = '../images/prodotto-default.jpg';
      }
      if (!$this['image_full']) {
        $this['image_full'] = '../images/prodotto-default.jpg';
      }
      if ($this['offer_multi'] == '2x1') {
        $this['offer_multi'] = '1+1 gratis';
      }
    }

  }

?>