2017-08-30 10:59:48 +02:00

142 lines
4.8 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';
$sale['tags'] = $this->extraFields['tags'] ? $this->extraFields['tags'] : [];
if (!class_exists('SaleDelay')) {
require_once _PS_ROOT_DIR_.'/modules/privatesales_delay/saledelay.php';
}
$delay = \SaleDelay::getDayDelay($this->extraFields['delivery_delay'], $cookie->id_lang);
$delay_from = \SaleDelay::getDateWithDelay($delay['value'], new \DateTime());
$delay_to = \SaleDelay::getDateWithDelay($delay['value_max'], new \DateTime());
$sale['delay_from'] = $delay_from->format('d/m');
$sale['delay_to'] = $delay_to->format('d/m');
return $sale;
}
/**
* Returns the associtated tags for $ids_sales
*
* @param array $ids_sales List of sale's id
*
* @return array array(
* id_sale => array(
* id_tag,
* id_tag,
* ...
* )
* )
*/
static public function getTagsBySaleIds(array $ids_sales) {
$sales_tags = array();
if (!empty($ids_sales)) {
$tags = \Db::getInstance()->executeS('
SELECT p.`id_sale`, pcf.`id_parent` as `id_tag`
FROM ps_privatesale p
LEFT JOIN ps_privatesale_category pc ON (pc.`id_sale` = p.`id_sale`)
LEFT JOIN ps_category_family_association pcfa ON (pc.`id_category` = pcfa.`id_category`)
LEFT JOIN ps_category_family pcf ON (pcfa.`id_category_family` = pcf.`id_category_family`)
WHERE p.`id_sale` IN (' . implode(',', $ids_sales) . ')
AND pcf.`id_parent` IS NOT NULL
GROUP BY p.`id_sale`, pcf.`id_parent`
');
foreach ($tags as $tag) {
$sales_tags[(int)$tag['id_sale']][] = (int)$tag['id_tag'];
}
}
return $sales_tags;
}
/**
* Returns the associtated reduction for $ids_sales
*
* @param array $ids_sales List of sale's id
*
* @return array array(
* id_sale => reduction
* )
*/
static public function getReductionBySaleIds(array $ids_sales) {
global $cookie;
$sales_reduction = array();
if (!empty($id_sales)) {
$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 . '
');
$sales_reduction = array();
foreach ($reductions as $reduction) {
$sales_reduction[(int)$reduction['id_sale']] = $reduction['value'];
}
}
return $sales_reduction;
}
/**
* @inheritdoc
*
* Adding the reduction extraField
* Adding the tags extraField
*/
static public function apiGetSales($future = false) {
$sales = parent::apiGetSales($future);
$ids_sales = array();
foreach ($sales as $sale) {
$ids_sales[] = $sale->id;
}
$reductions = static::getReductionBySaleIds($ids_sales);
foreach ($sales as $sale) {
$value = isset($reductions[(int)$sale->id]) ? $reductions[(int)$sale->id] : null;
$sale = $sale->populateRow('reduction', $value, '0');
}
$sales_tags = static::getTagsBySaleIds($ids_sales);
foreach ($sales as $sale) {
$value = isset($sales_tags[(int)$sale->id]) ? $sales_tags[(int)$sale->id] : null;
$sale = $sale->populateRow('tags', $value, array());
}
return $sales;
}
/**
* Populate the current Sale model field ($name) with $value
* If $value is null it will populate the field with the given $defaultValue
*
* @param string $name The field name to populate
* @param any $value The value to populate with
* @param any $defaultValue The defaultValue to apply
*
* @return Sale The current sale model with the populated field.
*/
protected function populateRow($name, $value, $defaultValue = null) {
$value = $value !== null ? $value : $defaultValue;
return $this->populate(array(
$name => $value
), $this->id);
}
}