170 lines
4.7 KiB
PHP
Raw Normal View History

2015-08-12 14:10:25 +02:00
<?php
class BlockAds extends ObjectModel
{
public $id;
public $id_block_ads;
public $hook_name;
public $label;
public $width;
public $height;
public $content;
public $date_add;
public $date_upd;
public static $definition = array(
'table' => 'ads',
'primary' => 'id_block_ads',
'multilang' => true,
'multilang_shop' => true,
'fields' => array(
'hook_name' => array('type' => self::TYPE_STRING, 'validate' => 'isHookName', 'copy_post' => false),
'label' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),
'width' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'height' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'content' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isAnything'),
),
);
2015-10-27 13:03:20 +01:00
public function getAdsByPage($page_name, DateTime $date, $limit = false)
2015-08-13 12:41:23 +02:00
{
$sql = 'SELECT ap.id_publication
FROM '._DB_PREFIX_.'ads_publication ap
INNER JOIN '._DB_PREFIX_.'ads_publication_page app
ON ap.id_publication = app.id_publication
AND app.page_name = "'.pSQL($page_name).'"
WHERE ap.id_block_ads = '.(int)$this->id.'
AND ( date_from = "0000-00-00 00:00:00"
OR date_from <= "'.$date->format('Y-m-d H:i:s').'")
AND ( date_to = "0000-00-00 00:00:00"
OR date_to >= "'.$date->format('Y-m-d H:i:s').'")
2015-10-27 13:03:20 +01:00
AND active = 1';
if ($limit) {
$sql.= ' AND ap.`id_publication` NOT IN
(SELECT `id_publication` FROM `'._DB_PREFIX_.'ads_publication_other`)
';
}
$sql.=' GROUP BY ap.id_publication';
2015-10-21 17:57:37 +02:00
return Db::getInstance()->executeS($sql);
}
public function getPublicationsIds($page_name, DateTime $date, $id = null)
{
$publications = $this->getAdsByPage($page_name, $date);
2015-08-13 12:41:23 +02:00
if(empty($publications)
2015-10-21 17:57:37 +02:00
|| count($publications) == 0) {
2015-08-13 12:41:23 +02:00
return array();
}
$publications = array_map(function($line){ return $line['id_publication']; }, $publications);
if(empty($id))
return $publications;
$sql = 'SELECT id_publication, id_free
FROM '._DB_PREFIX_.'ads_publication_other
WHERE page_name = "'.pSQL($page_name).'"
AND id_publication IN ('.implode(',', $publications).')';
$ids_free = Db::getInstance()->executeS($sql);
$publications = array();
foreach($ids_free as $line)
if($line['id_free'] == $id)
{
$publications[$line['id_publication']] = $line['id_publication'];
}
return $publications;
}
public function getPublications($page_name, DateTime $date, $id = null, $id_lang = null)
{
if(empty($id_lang))
$id_lang = Context::getContext()->language->id;
$ids_publications = $this->getPublicationsIds($page_name, $date, $id);
2015-10-21 17:57:37 +02:00
if(count($ids_publications) == 0) {
2015-10-27 13:03:20 +01:00
$publications = $this->getAdsByPage($page_name, $date, true);
2015-10-21 17:57:37 +02:00
if (!empty($publications)) {
$ids_publications = array_map(function($line){ return $line['id_publication']; }, $publications);
} else {
return array();
}
}
2015-08-13 12:41:23 +02:00
$collection = new Collection('Publication', $id_lang);
$collection->where('id_publication', 'in', $ids_publications);
return $collection->getResults();
}
public function getContent($page_name, DateTime $date, $id = null, $id_lang = null)
{
if(empty($id_lang))
$id_lang = Context::getContext()->language->id;
$publications = $this->getPublications($page_name, $date, $id, $id_lang = null);
2015-10-21 16:43:19 +02:00
2015-08-13 12:41:23 +02:00
if(count($publications) > 0)
2015-10-23 14:46:01 +02:00
return $publications[0];
2015-08-13 12:41:23 +02:00
return $this->content;
}
public static function getContentByHook($hook_name, $page_name, DateTime $date = null, $id = null, $id_lang = null)
2015-10-21 17:57:37 +02:00
{
2015-08-13 12:41:23 +02:00
if(empty($id_lang))
$id_lang = Context::getContext()->language->id;
$collection = new Collection('BlockAds', $id_lang);
$collection->where('hook_name', '=', $hook_name);
$ads = $collection->getFirst();
if(empty($ads))
return '';
if(empty($date))
$date = new DateTime();
2015-10-23 14:46:01 +02:00
$publication = $ads->getContent($page_name, $date, $id, $id_lang);
if ($publication instanceOf Publication) {
if(!empty($publication->content)) {
return array(
'content' => $publication->content,
'id' => $publication->id,
'link' => false
);
} else {
if(!empty($publication->link)) {
return array(
2015-11-17 10:12:52 +01:00
'content' => (isset($publication->link) ? $publication->link : ''),
2015-10-23 14:46:01 +02:00
'id' => $publication->id,
'link' => true
);
}
}
}
2015-11-17 10:12:52 +01:00
if(isset($publication->content)) {
return array(
'content' => $publication->content,
'id' => NULL,
'link' => false
);
} else{
return array(
'content' => $publication,
'id' => NULL,
'link' => false
);
}
2015-08-13 12:41:23 +02:00
}
2015-10-21 16:43:19 +02:00
}