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'] : []; 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($ids_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 . ' '); 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); } }