2017-08-14 17:56:42 +02:00

50 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use Antadis\API\Front\Models\Sale as BaseSale;
use App\Classes\ImageTrait;
class Sale extends BaseSale {
use ImageTrait;
/**
* {@inheritdoc}
*/
public function toArray() {
global $cookie;
$sale = parent::toArray();
$is_active = strtotime($this->date_start) < strtotime(date('Y-m-d 07:00:00'));
$sale['img'] = $this->getSaleImage($this->id, $is_active);
$sale['reduction'] = $this->extraFields['reduction'] ? $this->extraFields['reduction'] : '0';
return $sale;
}
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;
}
}