bebeboutik/modules/emarsys_rss/flux.php
2017-11-14 10:28:45 +01:00

461 lines
17 KiB
PHP
Executable File

<?php
require_once('../../config/config.inc.php');
require_once('../privatesales/Sale.php');
require_once('html2text.php');
if (!defined('_PS_BASE_URL_'))
define('_PS_BASE_URL_', Tools::getShopDomain(true));
$catalog = new Catalog();
$catalog->displayContent();
class Catalog
{
private $_xml;
private $_debug;
public $id_lang;
public $currentTime;
public $versions;
const FIELD_EXTRA = 2;
public function __construct() {
$this->_xml ="";
$this->versions = array(
'es' => 3,
'fr' => 2
);
}
public function setContent($version = 'fr')
{
// Flush to load the good sales
CacheRedis::getInstance()->clear('sale*');
$test_for_tomorrow = (int)Tools::getValue('tomorrow',0);
$sales_today = array();
$sales_last_day = array();
$sale_current = array();
$id_lang = (int) $this->versions[$version];
if($test_for_tomorrow) {
$sales_today = Sale::getSales(TRUE, NULL, NULL, 'tomorrow', FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'), '`position` DESC', NULL, NULL, $version);
$sales_last_day = Sale::getSales(TRUE, NULL, NULL, 'ended_tomorrow', FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'), '`position` DESC', NULL, NULL, $version);
$sale_current = self::getSales(TRUE, NULL, NULL, 'forward_tomorrow', FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'), '`position` DESC', NULL, NULL, $version);
} else {
$sales_today = Sale::getSales(TRUE, NULL, NULL, 'today', FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'), '`position` DESC', NULL, NULL, $version);
$sales_last_day = Sale::getSales(TRUE, NULL, NULL, 'last', FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'), '`position` DESC', NULL, NULL, $version);
$sale_current = self::getSales(TRUE, NULL, NULL, 'forward', FALSE, Configuration::get('PRIVATESALES_SHOW_PUBLIC'), '`position` DESC', NULL, NULL, $version);
}
foreach ($sales_today as &$sale) {
$sql = '
SELECT `value`
FROM `'._DB_PREFIX_.'privatesale_extrafield_sale`
WHERE `id_field` = '.(int) self::FIELD_EXTRA .'
AND `id_lang` = '.(int) $id_lang.'
AND id_sale = '.(int) $sale->id
;
if ($value = Db::getInstance()->getValue($sql)) {
$sale->reduction = $value;
} else {
$sale->reduction = 'Non défini';
}
$desc = new html2text($sale->description[(int) $id_lang]);
$sale->description[(int) $id_lang] = $desc->get_text();
}
foreach ($sales_last_day as &$sale) {
$sql = '
SELECT `value`
FROM `'._DB_PREFIX_.'privatesale_extrafield_sale`
WHERE `id_field` = '.(int) self::FIELD_EXTRA .'
AND `id_lang` ='.(int) $id_lang.'
AND `id_sale` ='.(int) $sale->id
;
if ($value = Db::getInstance()->getValue($sql)) {
$sale->reduction = $value;
} else {
$sale->reduction = 'Non défini';
}
$desc = new html2text($sale->description[(int) $id_lang]);
$sale->description[(int) $id_lang] = $desc->get_text();
}
foreach ($sale_current as &$sale) {
$sql = '
SELECT `value`
FROM `'._DB_PREFIX_.'privatesale_extrafield_sale`
WHERE `id_field` = '.(int) self::FIELD_EXTRA .'
AND `id_lang` = '.(int) $id_lang.'
AND `id_sale` = ' . (int) $sale->id
;
if ($value = Db::getInstance()->getValue($sql)) {
$sale->reduction = $value;
} else {
$sale->reduction = 'Non défini';
}
$desc = new html2text($sale->description[(int) $id_lang]);
$sale->description[(int) $id_lang] = $desc->get_text();
}
$this->setXml($sales_today, $sales_last_day, $sale_current, false, $version, $test_for_tomorrow);
}
public function displayContent()
{
foreach ($this->versions as $version => $id_lang) {
$this->setContent($version);
}
echo 'Fichier généré';
}
public function setXml($sales_today, $sales_last_day, $sale_current, $rss=false, $version, $tomorrow=false)
{
$id_lang = (int) $this->versions[$version];
$this->_xml = '<?xml version="1.0" encoding="utf-8"?>';
$this->_xml .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
if ($version == 'es') {
$flux = fopen("flux_".$version.".xml", "w");
// Catalog
$this->_xml .= '<channel>';
//Shop details
$this->_xml .= '<title>Bebeboutik.es</title>';
$this->_xml .= '<link></link>';
$this->_xml .= '<description>Bebeboutik.es</description>';
$this->_xml .= '<logo>http://www.bebeboutik.es/img/logo_3.png</logo>';
$title_sunday = Configuration::get('TITLE_NEWS_DIMANCHE_ES');
$link_sale = 'https://www.bebeboutik.es';
$all_sales = 'Todas las ventas';
} else {
$flux = fopen("flux.xml", "w");
// Catalog
$this->_xml .= '<channel>';
//Shop details
$this->_xml .= '<title>Bebeboutik.com</title>';
$this->_xml .= '<link>https://www.bebeboutik.com</link>';
$this->_xml .= '<description>Bebeboutik.com</description>';
$this->_xml .= '<logo>http://www.bebeboutik.com/img/logo.png</logo>';
$title_sunday = Configuration::get('TITLE_NEWS_DIMANCHE');
$link_sale = 'https://www.bebeboutik.com';
$all_sales = 'Toutes les ventes';
}
$sql_title = '
SELECT `title`, `preheader`
FROM `ps_privatesale_newsletter`
WHERE `date_pub` = '.($tomorrow?'CURDATE() + INTERVAL 1 day':'CURDATE()').'
AND `version` = "'.$version.'"'
;
$info_newsletter = Db::getInstance()->getRow($sql_title);
if (empty($info_newsletter)) {
$info_newsletter['title'] = Configuration::get('PRIVATESALES_TITLE_NEWSLETTER');
$info_newsletter['preheader'] = '';
}
$this->_xml.='<sect_00>';
$this->_xml .= '<titre><![CDATA['.$info_newsletter['title'].']]></titre>';
$this->_xml .= '<description><![CDATA['.$info_newsletter['preheader'].']]></description>';
$this->_xml .= '<titre_sunday><![CDATA['.$title_sunday.']]></titre_sunday>';
$this->_xml.='</sect_00>';
foreach ($sales_today as $item) {
$link = new Link();
$category = new Category($item->id_category);
$sale_link = $link->getCategoryLink($category->id, $category->link_rewrite[(int) $id_lang]);
if ($version == 'es') {
$sale_link = str_replace('bebeboutik.com', 'bebeboutik.es', $sale_link);
}
$sale_link = str_replace('http://','https://', $sale_link);
$sql = '
SELECT `value`
FROM `'._DB_PREFIX_.'privatesale_extrafield_sale`
WHERE `id_field` = 1
AND `id_lang` = '.(int) $id_lang.'
AND `id_sale` = '.(int) $item->id
;
if ($value = Db::getInstance()->getValue($sql)) {
$item->small_title = $value;
} else {
$item->small_title = 'Non défini';
}
$this->_xml.='<sect_01>';
$this->_xml .= '<title><![CDATA['.$item->title[(int) $id_lang].']]>|<![CDATA['.$item->reduction.']]></title>';
$this->_xml .= '<description><![CDATA['.$item->small_title.']]></description>';
//$this->_xml .= '<description></description>';
//$this->_xml .= '<category><![CDATA['.$category->name[(int) $id_lang].']]></category>';
$this->_xml .= '<category></category>';
$this->_xml .= '<link>'.$sale_link.'</link>';
$this->_xml .= '<reduction><![CDATA['.$item->reduction.']]></reduction>';
$this->_xml .= '<image>';
$this->_xml .= '<title><![CDATA['.$item->title[(int) $id_lang].']]> <![CDATA['.$item->reduction.']]></title>';
$this->_xml .= '<url>http://static.bebeboutik.com/modules/privatesales/img/'.$item->id.'/liston_'.(int) $id_lang.'.jpg</url>';
$this->_xml .= '</image>';
$this->_xml.='</sect_01>';
}
if (count($sales_today)%2 == 1){
$this->_xml.='<sect_01>';
$this->_xml .= '<title></title>';
$this->_xml .= '<description><![CDATA['.$all_sales.']]></description>';
$this->_xml .= '<description></description>';
$this->_xml .= '<category></category>';
$this->_xml .= '<link>'.$link_sale.'</link>';
$this->_xml .= '<reduction></reduction>';
$this->_xml .= '<image>';
$this->_xml .= '<title>Bebeboutik.com</title>';
$this->_xml .= '<url>http://www.bebeboutik.com/img/newsletter_'.(int) $id_lang.'.png</url>';
$this->_xml .= '</image>';
$this->_xml.='</sect_01>';
}
foreach ($sales_last_day as $item) {
$link = new Link();
$category = new Category($item->id_category);
$sale_link = $link->getCategoryLink($category->id, $category->link_rewrite[(int) $id_lang]);
if ($version == 'es') {
$sale_link = str_replace('bebeboutik.com', 'bebeboutik.es', $sale_link);
}
$sale_link = str_replace('http://','https://', $sale_link);
$sql = '
SELECT `value`
FROM `'._DB_PREFIX_.'privatesale_extrafield_sale`
WHERE `id_field` = 1
AND `id_lang` = '.(int) $id_lang.'
AND `id_sale` = '.(int) $item->id
;
if ($value = Db::getInstance()->getValue($sql)) {
$item->small_title = $value;
} else {
$item->small_title = 'Non défini';
}
$this->_xml.='<sect_02>';
$this->_xml .= '<title><![CDATA['.$item->title[(int) $id_lang].']]>|<![CDATA['.$item->reduction.']]></title>';
$this->_xml .= '<description><![CDATA['.$item->small_title.']]></description>';
//$this->_xml .= '<description></description>';
//$this->_xml .= '<category><![CDATA['.$category->name[(int) $id_lang].']]></category>';
$this->_xml .= '<category></category>';
$this->_xml .= '<link>'.$sale_link.'</link>';
$this->_xml .= '<reduction><![CDATA['.$item->reduction.']]></reduction>';
$this->_xml .= '<image>';
$this->_xml .= '<title><![CDATA['.$item->title[(int) $id_lang].']]> <![CDATA['.$item->reduction.']]></title>';
$this->_xml .= '<url>http://static.bebeboutik.com/modules/privatesales/img/'.$item->id.'/liston_'.(int) $id_lang.'.jpg</url>';
$this->_xml .= '</image>';
$this->_xml.='</sect_02>';
}
if (count($sales_last_day)%2 == 1){
$this->_xml.='<sect_02>';
$this->_xml .= '<title></title>';
$this->_xml .= '<description><![CDATA['.$all_sales.']]></description>';
$this->_xml .= '<description></description>';
$this->_xml .= '<category></category>';
$this->_xml .= '<link>'.$link_sale.'</link>';
$this->_xml .= '<reduction></reduction>';
$this->_xml .= '<image>';
$this->_xml .= '<title>Bebeboutik.com</title>';
$this->_xml .= '<url>http://www.bebeboutik.com/img/newsletter_'.(int) $id_lang.'.png</url>';
$this->_xml .= '</image>';
$this->_xml.='</sect_02>';
}
foreach ($sale_current as $item) {
$link = new Link();
$category = new Category($item->id_category);
$sale_link = $link->getCategoryLink($category->id, $category->link_rewrite[(int) $id_lang]);
if ($version == 'es') {
$sale_link = str_replace('bebeboutik.com', 'bebeboutik.es', $sale_link);
}
$sale_link = str_replace('http://','https://', $sale_link);
$sql = '
SELECT `value`
FROM `'._DB_PREFIX_.'privatesale_extrafield_sale`
WHERE `id_field` = 1
AND `id_lang` = '.(int) $id_lang.'
AND `id_sale` = '. (int)$item->id
;
if ($value = Db::getInstance()->getValue($sql)) {
$item->small_title = $value;
} else {
$item->small_title = 'Non défini';
}
$this->_xml.='<sect_04>';
$this->_xml .= '<title><![CDATA['.$item->title[(int) $id_lang].']]>|<![CDATA['.$item->reduction.']]></title>';
$this->_xml .= '<description><![CDATA['.$item->small_title.']]></description>';
//$this->_xml .= '<description></description>';
//$this->_xml .= '<category><![CDATA['.$category->name[(int) $id_lang].']]></category>';
$this->_xml .= '<category></category>';
$this->_xml .= '<link>'.$sale_link.'</link>';
$this->_xml .= '<reduction><![CDATA['.$item->reduction.']]></reduction>';
$this->_xml .= '<image>';
$this->_xml .= '<title><![CDATA['.$item->title[(int) $id_lang].']]> <![CDATA['.$item->reduction.']]></title>';
$this->_xml .= '<url>http://static.bebeboutik.com/modules/privatesales/img/'.$item->id.'/liston_'.(int) $id_lang.'.jpg</url>';
$this->_xml .= '</image>';
$this->_xml.='</sect_04>';
}
if (count($sale_current)%2 == 1){
$this->_xml.='<sect_04>';
$this->_xml .= '<title></title>';
$this->_xml .= '<description><![CDATA['.$all_sales.']]></description>';
$this->_xml .= '<description></description>';
$this->_xml .= '<category></category>';
$this->_xml .= '<link>'.$link_sale.'</link>';
$this->_xml .= '<reduction></reduction>';
$this->_xml .= '<image>';
$this->_xml .= '<title>Bebeboutik.com</title>';
$this->_xml .= '<url>http://www.bebeboutik.com/img/newsletter_'.(int) $id_lang.'.png</url>';
$this->_xml .= '</image>';
$this->_xml.='</sect_04>';
}
foreach ($sales_last_day as $item) {
$link = new Link();
$category = new Category($item->id_category);
$sale_link = $link->getCategoryLink($category->id, $category->link_rewrite[(int) $id_lang]);
if ($version == 'es') {
$sale_link = str_replace('bebeboutik.com', 'bebeboutik.es', $sale_link);
}
$sale_link = str_replace('http://','https://', $sale_link);
$sql = '
SELECT `value`
FROM `'._DB_PREFIX_.'privatesale_extrafield_sale`
WHERE `id_field` = 1
AND `id_lang` = '.(int) $id_lang.'
AND `id_sale` = '. (int)$item->id
;
if ($value = Db::getInstance()->getValue($sql)) {
$item->small_title = $value;
} else {
$item->small_title = 'Non défini';
}
$this->_xml.='<sect_03>';
// $this->_xml .= '<id_vente>'.$item->id.'</id_vente>';
$this->_xml .= '<title><![CDATA['.$item->title[(int) $id_lang].']]>|<![CDATA['.$item->reduction.']]></title>';
$this->_xml .= '<description><![CDATA['.$item->small_title.']]></description>';
//$this->_xml .= '<description></description>';
//$this->_xml .= '<category><![CDATA['.$category->name[(int) $id_lang].']]></category>';
$this->_xml .= '<category></category>';
$this->_xml .= '<link>'.$sale_link.'</link>';
$this->_xml .= '<reduction><![CDATA['.$item->reduction.']]></reduction>';
$this->_xml .= '<image>';
$this->_xml .= '<title><![CDATA['.$item->title[(int) $id_lang].']]> <![CDATA['.$item->reduction.']]></title>';
$this->_xml .= '<url>http://static.bebeboutik.com/modules/privatesales/img/'.$item->id.'/liston_'.(int) $id_lang.'.jpg</url>';
$this->_xml .= '</image>';
$this->_xml.='</sect_03>';
}
if (count($sales_last_day)%2 == 1){
$this->_xml.='<sect_03>';
$this->_xml .= '<title></title>';
$this->_xml .= '<description><![CDATA['.$all_sales.']]></description>';
$this->_xml .= '<description></description>';
$this->_xml .= '<category></category>';
$this->_xml .= '<link>'.$link_sale.'</link>';
$this->_xml .= '<reduction></reduction>';
$this->_xml .= '<image>';
$this->_xml .= '<title>Bebeboutik.com</title>';
$this->_xml .= '<url>http://www.bebeboutik.com/img/newsletter_'.(int) $id_lang.'.png</url>';
$this->_xml .= '</image>';
$this->_xml.='</sect_03>';
}
$this->_xml .= '</channel>';
$this->_xml .= '</rss>';
fwrite($flux, $this->_xml);
fclose($flux);
}
public static function getSales($enabled=NULL, $logout=NULL, $featured=NULL, $future=NULL, $lite=FALSE, $pub=FALSE, $order_by='`date_end` ASC', $limit=NULL, $sale_type=NULL, $site_version=FALSE)
{
$result = array();
$where = array();
$query = '
SELECT p.`id_sale`
FROM `'._DB_PREFIX_.'privatesale` p
';
if($sale_type !== NULL){
$query.= ' LEFT JOIN `'._DB_PREFIX_.'privatesale_type` pt ON(pt.id_sale = p.id_sale AND pt.id_sale_type = '. $sale_type.')';
}
if($site_version !== FALSE) {
$query .= '
LEFT JOIN `'._DB_PREFIX_.'privatesale_site_version` sv
ON p.`id_sale` = sv.`id_sale`
';
$where[] = 'sv.`version` = "'.pSQL($site_version).'"';
}
if($enabled !== NULL OR $logout !== NULL OR $featured !== NULL OR $future !== NULL) {
$enabled !== null? $where[] = '`enabled` = '.(int) $enabled: TRUE;
$logout !== null? $where[] = '`logout` = '.(int) $logout: TRUE;
$pub != FALSE? '': $where[] = '`public` = 0';
$featured !== null? $where[] = '`featured` = '.(int) $featured: TRUE;
// $sale_type !== NULL? $where[] = '`sale_type` = '.(int) $sale_type: TRUE;
if($future === 'current') {
$where[] = '`date_start` < NOW() AND `date_end` > NOW()';
} elseif($future === 'not_ended') {
$where[] = '`date_end` > NOW()';
} elseif($future === 'forward') {
$where[] = '`date_start` < NOW() AND `date_end` > NOW() AND forward_news = 1';
} elseif($future === 'forward_tomorrow') {
$where[] = '`date_start` < NOW() AND `date_end` > DATE_ADD(NOW(), INTERVAL 1 DAY) AND forward_news = 1';
} elseif($future === 'all') {
$where[] = ' 1 = 1';
} elseif ($future === 'today') {
$where[] = 'CAST(`date_start` AS DATE) = CAST(NOW() AS DATE)';
} elseif ($future === 'last') {
$where[] = 'CAST(`date_end` AS DATE) = CAST(NOW() AS DATE)';
} elseif(is_int($future)) {
$where[] = '(`date_start` > NOW() AND `date_start` < DATE_ADD(NOW(), INTERVAL '.$future.' DAY))';
} elseif($future === TRUE) {
$where[] = '`date_start` > NOW()';
} elseif($future === FALSE) {
$where[] = '`date_start` < NOW() AND `date_end` < NOW()';
}
$query.= 'WHERE '.implode(' AND ', $where);
}
if($sale_type !== NULL){
$query.= ' AND pt.id_sale_type = '. $sale_type;
}
$query .= ' ORDER BY '.$order_by;
if($limit !== NULL) {
$query .= ' LIMIT '.$limit;
}
if($sales = Db::getInstance()->ExecuteS($query)) {
if($lite) {
foreach($sales AS $sale) {
$result[] = $sale['id_sale'];
}
} else {
foreach($sales AS $sale) {
$result[] = new Sale($sale['id_sale']);
}
}
}
return $result;
}
}