203 lines
4.9 KiB
PHP
Raw Normal View History

2015-08-12 14:10:25 +02:00
<?php
class Publication extends ObjectModel
{
public $id;
public $id_publication;
public $id_block_ads;
public $id_shop;
public $date_from;
public $date_to;
public $date_add;
public $date_upd;
2015-08-12 16:12:56 +02:00
public $pages = null;
public $pages_infos = null;
2015-08-12 14:10:25 +02:00
public static $definition = array(
'table' => 'ads_publication',
'primary' => 'id_publication',
'multilang' => true,
'fields' => array(
'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'content' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isAnything'),
2015-08-12 16:12:56 +02:00
'date_from' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
'date_to' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
2015-08-12 14:10:25 +02:00
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
),
);
2015-08-12 16:12:56 +02:00
public function getInfos($page_name = null)
{
if(!Validate::isLoadedObject($this))
$this->pages_infos = array();
if($this->pages_infos === null)
{
$array_tmp = array();
$sql = 'SELECT *
FROM '._DB_PREFIX_.'ads_publication_other
WHERE id_publication = '.(int)$this->id.'';
$infos = Db::getInstance()->executeS($sql);
foreach($infos as $info)
{
2015-08-13 09:59:25 +02:00
$array_tmp[$info['page_name']][] = $info['id_free'];
2015-08-12 16:12:56 +02:00
}
$this->pages_infos = $array_tmp;
}
if(empty($page_name))
return $this->pages_infos;
if(isset($this->pages_infos[$page_name]))
return $this->pages_infos[$page_name];
return array();
}
2015-08-13 09:59:25 +02:00
public function getInfosWithValues($page_name)
{
$infos = $this->getInfos($page_name);
if(count($infos) == 0)
return array();
$id_lang = Context::getContext()->language->id;
if($page_name == 'postcms')
{
$sql = '
SELECT c.id_post as id, lc.title as content
FROM `'._DB_PREFIX_.'cmsps_posts` c
LEFT JOIN `'._DB_PREFIX_.'cmsps_posts_lang` lc
ON c.`id_post` = lc.`id_post`
AND lc.id_lang = '.$id_lang.'
WHERE c.id_post IN ('.implode(',', $infos).')';
}
elseif($page_name == 'postedito')
{
$sql = '
SELECT c.id_edito as id, lc.title as content
FROM `'._DB_PREFIX_.'cmsps_editos` c
LEFT JOIN `'._DB_PREFIX_.'cmsps_editos_lang` lc
ON c.`id_edito` = lc.`id_edito`
AND lc.id_lang = '.$id_lang.'
WHERE c.id_edito IN ('.implode(',', $infos).')
GROUP BY c.id_edito';
}
elseif($page_name == 'categorycms')
{
$sql = '
SELECT c.id_category as id, lc.title as content
FROM `'._DB_PREFIX_.'cmsps_categories` c
LEFT JOIN `'._DB_PREFIX_.'cmsps_categories_lang` lc
ON c.`id_category` = lc.`id_category`
AND lc.id_lang = '.$id_lang.'
LEFT JOIN `'._DB_PREFIX_.'cmsps_categories_shop` ls
ON c.`id_category` = ls.`id_category`
WHERE c.`id_category IN ('.implode(',', $infos).')
GROUP BY c.id_category';
}
$res = Db::getInstance()->executeS($sql);
return $res;
}
2015-08-12 16:12:56 +02:00
public function getSelectedPages()
{
if(!Validate::isLoadedObject($this))
$this->pages = array();
if($this->pages === null)
{
$sql = 'SELECT page_name
FROM '._DB_PREFIX_.'ads_publication_page
WHERE id_publication = '.(int)$this->id;
$pages = Db::getInstance()->executeS($sql);
$pages = array_map(function($line){ return $line['page_name']; }, $pages);
$this->pages = $pages;
}
return $this->pages;
}
public function setPages($pages = array())
{
2015-08-12 16:59:20 +02:00
$this->pages = $pages;
if(Validate::isLoadedObject($this))
{
Db::getInstance()->execute('
DELETE FROM '._DB_PREFIX_.'ads_publication_page
WHERE id_publication = '.$this->id);
$req = array();
foreach($pages as $page_name)
$req[] = array(
'id_publication' => $this->id,
'page_name' => $page_name
);
Db::getInstance()->insert('ads_publication_page', $req);
}
}
public function setPageInfo($page_name, $infos)
{
$this->pages_infos[$page_name] = $infos;
if(Validate::isLoadedObject($this))
{
Db::getInstance()->execute('
DELETE FROM '._DB_PREFIX_.'ads_publication_other
WHERE id_publication = '.$this->id.'
2015-08-13 09:59:25 +02:00
AND page_name = "'.pSQL($page_name).'"');
2015-08-12 16:59:20 +02:00
$req = array();
foreach($infos as $info)
$req[] = array(
'id_publication' => $this->id,
'page_name' => $page_name,
'id_free' => $info
);
Db::getInstance()->insert('ads_publication_other', $req);
}
2015-08-12 16:12:56 +02:00
}
2015-08-12 14:10:25 +02:00
public static function getAllPages()
{
return array(
'index',
'postcms', //Article
'categorycms', //Category d'article
'postedito', //Article mise en forme différent
);
}
public static function getPageNameTranslated($page_name)
{
$array = array(
'index' => 'Page d\'accueil',
'postcms' => 'Article', //Article
'categorycms' => 'Catégorie d\'article', //Category d'article
'postedito' => 'Edito', //Article mise en forme différent
);
return isset($array[$page_name]) ? $array[$page_name] : $page_name;
}
2015-08-12 16:12:56 +02:00
public static function getPageNeedInfos()
{
return array(
'postcms',
'categorycms',
'postedito'
);
}
2015-08-12 14:10:25 +02:00
}