50 lines
1.5 KiB
PHP
Raw Normal View History

2017-08-11 16:10:27 +02:00
<?php
namespace App\Models;
use Antadis\API\Front\Models\Sale as BaseSale;
2017-08-14 12:39:19 +02:00
use App\Classes\ImageTrait;
2017-08-11 16:10:27 +02:00
class Sale extends BaseSale {
2017-08-14 12:39:19 +02:00
use ImageTrait;
2017-08-11 16:10:27 +02:00
/**
* {@inheritdoc}
*/
public function toArray() {
2017-08-14 17:56:42 +02:00
global $cookie;
2017-08-11 16:10:27 +02:00
$sale = parent::toArray();
2017-08-14 12:39:19 +02:00
$is_active = strtotime($this->date_start) < strtotime(date('Y-m-d 07:00:00'));
$sale['img'] = $this->getSaleImage($this->id, $is_active);
2017-08-14 17:56:42 +02:00
$sale['reduction'] = $this->extraFields['reduction'] ? $this->extraFields['reduction'] : '0';
2017-08-11 16:10:27 +02:00
return $sale;
}
2017-08-14 17:56:42 +02:00
static public function apiGetSales($future = false) {
global $cookie;
$sales = parent::apiGetSales($future);
$ids_sales = array();
foreach ($sales as $sale) {
$ids_sales[] = $sale->id;
}
$reductions = \Db::getInstance()->executeS('
SELECT id_sale, value FROM ' . _DB_PREFIX_ . 'privatesale_extrafield_sale
WHERE `id_field` = 2
AND `id_sale` IN (' . implode(',', $ids_sales) . ')
AND `id_lang` = ' . (int)$cookie->id_lang . '
');
foreach ($reductions as $reduction) {
foreach ($sales as $sale) {
if ((int)$sale->id === (int)$reduction['id_sale']) {
$sale = $sale->populate(array(
'reduction' => $reduction['value'],
), $reduction['id_sale']);
break;
}
}
}
return $sales;
}
2017-08-11 16:10:27 +02:00
}