File "flyer.class.php"
Full Path: /srv/www/www.cadoro.it/src/models/flyer.class.php
File size: 14.75 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace models;
class Flyer extends \ArrayObject {
static public function add($params) {
global $db;
mysqli_query($db, "INSERT INTO flyers (".
"slug,".
"title,".
"source,".
"code,".
"thumb,".
"type,".
"stores,".
"display,".
"line,".
"date_publishing,".
"date_begin,".
"date_end,".
"pdf,".
"link,".
"flipping_book,".
"created_at) ".
"VALUES (".
_text(_slug($params['title'])) . ", " .
_text($params['title']) . ", " .
_text($params['source']) . ", " .
_text($params['code']) . ", " .
_text(_a($params['thumb'], 'asset')) . ", " .
_text($params['type']) . ", " .
_from_array($params['stores']) . ", " .
_integer($params['display']) . ", " .
_integer($params['line']) . ", " .
_date($params['date_publishing']) . ", " .
_date($params['date_begin']) . ", " .
_date($params['date_end']) . ", " .
_text(_a($params['pdf'], 'asset')) . ", " .
_text($params['link']) . ", " .
_text($params['flipping_book']) . ", " .
"NOW());") or
die("query error in Flyer::add: " . mysqli_error($db));
$flyer_id = mysqli_insert_id($db);
if ($params['flipping_book']) {
$flyer = Flyer::get_by_id($flyer_id);
$flyer->update_flipping_book();
}
return $flyer_id;
}
static public function get_by_id($id) {
global $db;
$rs = mysqli_query($db, "SELECT * ".
"FROM flyers ".
"WHERE deleted_at IS NULL AND id = " . _integer($id) . ";") or
die("query error in Flyer::get_by_id: " . mysqli_error($db));
$r = mysqli_fetch_assoc($rs);
if ($r) {
return new Flyer($r);
}
}
static public function get_by_code($code) {
global $db;
$rs = mysqli_query($db, "SELECT * ".
"FROM flyers ".
"WHERE deleted_at IS NULL AND code = " . _text($code) . ";") or
die("query error in Flyer::get_by_code: " . mysqli_error($db));
$r = mysqli_fetch_assoc($rs);
if ($r) {
return new Flyer($r);
}
}
static public function get($params=array(), $offset=0, $limit=25) {
global $lang, $db;
$where = Flyer::get_where($params);
$rs = mysqli_query($db, "SELECT * ".
"FROM flyers ".
"WHERE deleted_at IS NULL $where ".
"ORDER BY date_publishing DESC, title ".
"LIMIT " . _integer($limit) . " OFFSET " . _integer($offset) . ";") or
die("query error in Flyer::get: " . mysqli_error($db));
$results = array();
while ($r = mysqli_fetch_assoc($rs)) {
array_push($results, new Flyer($r));
}
return $results;
}
static public function get_count($params) {
global $db;
$where = Flyer::get_where($params);
$rs = mysqli_query($db, "SELECT COUNT(*) AS count ".
"FROM flyers ".
"WHERE deleted_at IS NULL $where;") or
die("query error in Flyer::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'])) {
$where .= "AND (title LIKE " . _text('%' . $params['q'] . '%') . ") ";
}
if (isset($params['line'])) {
$where .= "AND (line IN " . _text_in($params['line']) . ") ";
}
if (isset($params['types'])) {
$where .= "AND (type IS NULL OR type = '' OR type IN " . _text_in($params['types']) . ") ";
}
if (isset($params['store']) && $params['store']) {
$where .= "AND (stores LIKE '%;" . _integer($params['store']['id']) . ";%') ";
}
if (isset($params['dates'])) {
$where .= "AND (date_publishing <= current_date AND date_end >= current_date) ";
}
return $where;
}
public function bulk_load($filename) {
# Array
# (
# [cod.] => 240754
# [descr.] => tav.latte milka
# [marca] => milka
# [prec.off.] => 11-19-15
# [n/p] => n
# [um] => kg
# [gr.] => 0,1
# [list.] => 0,99
# [%sc.] => 40
# [off.] => 0,59
# [pr.un.] => 5,9
# [gracq] => saccon
# [fam] => 1150
# [defa] => tavolette di cioccolato
# [ean] => 3045140105502
# [002] => o
# [003] => o
# [004] => o
# [006] => o
# [007] => o
# [008] => o
# [009] => o
# [010] => o
# [011] => o
# [012] => o
# [013] => o
# [014] => o
# [015] => o
# [016] => o
# [018] => o
# [019] => o
# [021] => o
# [022] => o
# [023] => o
# [061] => o
# [062] => o
# [066] => o
# [067] => o
# [bio] =>
# [senza glutine] =>
# [green] =>
# [light] =>
# [catering] =>
# [esclusivo] =>
# [equosolidale] =>
# [kmzero] =>
# [senza lattosio] =>
# [vegano] =>
# [senza olio palma] =>
# [pack sost.] =>
# [tipo] =>
# [zona] =>
# [bindello] =>
# [categorie prodotti] => dolci
# )
$fp = fopen($filename, "r");
$header = null;
$header2 = null;
while ($row = fgetcsv($fp, 0, ';')) {
$row = array_map(function ($i) { return trim(ltrim($i, "'")); }, $row);
if (!$header) {
$row = array_map(function ($i) { return mb_strtolower($i); }, $row);
$header = $row;
continue;
}
if (!$header2) {
$row = array_map(function ($i) { return mb_strtolower($i); }, $row);
$header2 = $row;
foreach ($header2 as $k => $v) {
if ($v) {
$header[$k] = $v;
}
}
continue;
}
$row = array_combine($header, $row);
$matches = glob(DIR_ASSETS . "/../../data/flyer/" . $row['cod ean'] . "*.*"); //Iniziale del nome
//$matches = glob(DIR_ASSETS . "/../../data/flyer/" . $row['cod ean'] . ".*"); //Nome esatto
if ($matches) {
$img = $matches[0];
$md5 = md5_file($img);
$asset = _asset('flyers', $md5, preg_replace('.*\.', '', $img));
$asset_path = _asset_path($asset);
copy($img, $asset_path);
$img = $asset;
$thumb = _resize_c($asset_path, 220, 128, 'flyers');
} else {
$img = null;
$thumb = null;
}
//
$fields = array(
"flyer_id" => $this['id'],
"category" => $row['categoria'],
"subcategory" => null,
"code" => $row['cod.'],
"title" => implode(" ", array($row['descrizione articolo'])),
"description" => implode(" ", array(_a($row, 'gr.'), _a($row, 'um'), _a($row, 'defa'))),
"brand" => $row['marca'],
"offer_price" => preg_replace('/[^0-9,.]/', '', $row['off']),
"offer_price_old" => preg_replace('/[^0-9,.]/', '', $row['list']),
"offer_discount" => $row['%sc%.ar'],
"offer_multi" => null,
"offer_type" => null,
"notes" => null,
"ean" => $row['cod ean'],
"stores" => array(),
"stores_card" => array(),
"label" => array(),
"label2" => array(),
"date_begin" => null,
"date_end" => null,
"below_cost" => null,
"image_thumb" => $thumb,
"image_full" => $img,
"homepage" => 0,
);
foreach (array('biologico', 'senza glutine', 'green', 'light', 'caterig',
'esclusivo', 'equosolidale', 'kmzero', 'senza lattosio',
'vegano', 'senza olio palma', 'pack sost.',
'packaging sostenibile', 'confezione grande risparmio', 'nostra selezione') as $k) {
if (_a($row, $k)) {
if ($k == 'caterig') {
$k = 'catering';
}
$fields['label'][] = $k;
}
}
foreach (array('tipo', 'zona', 'bindello') as $k) {
if ($row[$k]) {
if($k == 'bindello') {
$cartigli = preg_replace('/[^a-z0-9,]+/', '', mb_strtolower($row[$k]));
$cartigli = explode(',', $cartigli);
foreach ($cartigli as $cartiglio) {
if($cartiglio) {
$fields['label2'][] = $cartiglio;
}
}
} else {
$fields['label2'][] = preg_replace('/[^a-z0-9 ]+/', '', mb_strtolower($row[$k]));
}
}
}
foreach ($row as $k => $v) {
if (strlen($k) == 3 && ctype_digit($k)) {
if ($v && !in_array($v, array("FR", "R"))) {
$fields['stores'][] = $k*1;
if ($v == "F") {
$fields['stores_card'][] = $k*1;
}
}
}
}
$fields['label'] = $fields['label'] ? ';' . implode(';', $fields['label']) . ';' : null;
$fields['label2'] = $fields['label2'] ? ';' . implode(';', array_unique($fields['label2'])) . ';' : null;
$fields['stores'] = $fields['stores'] ? ';' . implode(';', $fields['stores']) . ';' : null;
$fields['stores_card'] = $fields['stores_card'] ? ';' . implode(';', $fields['stores_card']) . ';' : null;
FlyerProduct::add($fields);
}
}
public function delete() {
global $db;
mysqli_query($db, "UPDATE flyers SET ".
"deleted_at = NOW() ".
"WHERE id = " . _integer($this['id']) . ";") or
die("query error in Flyer::delete: " . mysqli_error($db));
}
public function get_categories($params=array()) {
global $db;
$where = FlyerProduct::get_where($params);
$rs = mysqli_query($db, "SELECT DISTINCT category ".
"FROM flyers_products ".
"WHERE flyer_id = " . _integer($this['id']) . " AND deleted_at IS NULL AND category IS NOT NULL " . $where . " " .
"ORDER BY category;") or
die("query error in Flyer:get_categories: " . mysqli_error($db));
$result = array();
while ($r = mysqli_fetch_assoc($rs)) {
array_push($result, $r['category']);
}
return $result;
}
public function get_labels($params=array()) {
global $db;
$where = FlyerProduct::get_where($params);
$rs = mysqli_query($db, "SELECT DISTINCT label ".
"FROM flyers_products ".
"WHERE flyer_id = " . _integer($this['id']) . " AND deleted_at IS NULL AND label IS NOT NULL " . $where . " " .
"ORDER BY label;") or
die("query error in Flyer:get_label: " . mysqli_error($db));
$result = array();
while ($r = mysqli_fetch_assoc($rs)) {
foreach (explode(';', $r['label']) as $l) {
if (!in_array($l, $result)) {
$result[] = $l;
}
}
}
return $result;
}
public function get_product_by_id($product_id) {
return FlyerProduct::get_by_flyer_and_id($this['id'], $product_id);
}
public function get_product_by_code($product_code) {
return FlyerProduct::get_by_flyer_and_code($this['id'], $product_code);
}
public function get_products($params, $offset, $limit) {
$params['flyer_id'] = $this['id'];
return FlyerProduct::get($params, $offset, $limit);
}
public function get_products_count($params) {
$params['flyer_id'] = $this['id'];
return FlyerProduct::get_count($params);
}
public function update($params) {
global $db;
mysqli_query($db, "UPDATE flyers SET ".
"title = " . _text($params['title']) . ", " .
"source = " . _text($params['source']) . ", " .
"code = " . _text($params['code']) . ", " .
"thumb = COALESCE(" . _text(_a($params['thumb'], 'asset')) . ", thumb), " .
"type = " . _text($params['type']) . ", " .
"stores = " . _from_array($params["stores"]) . ", ".
"display = " . _integer($params['display']) . ", " .
"line = " . _integer($params['line']) . ", " .
"date_publishing = " . _date($params['date_publishing']) . ", " .
"date_begin = " . _date($params['date_begin']) . ", " .
"date_end = " . _date($params['date_end']) . ", " .
"pdf = COALESCE(" . _text(_a($params['pdf'], 'asset')) . ", pdf), " .
"link = " . _text($params['link']) . ", " .
"flipping_book = " . _text($params['flipping_book']) . ", " .
"updated_at = " . "NOW() ".
"WHERE id = " . _integer($this['id']) . ";") or
die("query error in Flyer::update: " . mysqli_error($db));
if ($params['flipping_book']) {
$this->update_flipping_book();
}
}
public function update_flipping_book() {
if (!$this['flipping_book']) {
return;
}
system('rm -fr "' . DIR_ASSETS . '/flipping-book/' . $this['id'] . '"');
system('mkdir -p "' . DIR_ASSETS . '/flipping-book/' . $this['id'] . '"');
system('cd "' . DIR_ASSETS . '/flipping-book/' . $this['id'] . '" && unzip "' . DIR_ASSETS . '/' . $this['flipping_book'] . '" >/dev/null 2>&1');
}
public function json() {
return array(
"id" => (int)$this['id'],
"title" => $this['title'],
"date_begin" => $this['date_begin'],
"date_end" => $this['date_end'],
"thumb" => $this['thumb'] ? CDN_URL . "/assets/" . $this['thumb'] : null,
"pdf" => $this['pdf'] ? CDN_URL . "/assets/" . $this['pdf'] : null,
"link" => $this['link'],
);
}
function __construct($value) {
parent::__construct($value);
$this['stores'] = _to_array($this['stores']);
}
}
?>