332 lines
9.3 KiB
PHP
Raw Normal View History

<?php
class CmsPsPost extends ObjectModel {
public $id_post;
public $id_category;
public $date_add;
public $date_upd;
public $active;
2015-07-28 10:33:55 +02:00
public $show_home;
public $trick;
2015-07-31 11:37:25 +02:00
public $note;
2015-08-21 14:40:48 +02:00
public $video;
public $amazon;
public $title;
public $slug;
public $intro;
public $content;
public $meta_title;
public $meta_desc;
2015-07-28 11:51:12 +02:00
public $img_size = array(
array(
'name' => 'small',
'width' => 400,
'height' => 240
),
array(
'name' => 'big',
'width' => 770,
'height' => 500
)
);
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'cmsps_posts',
'primary' => 'id_post',
'multilang' => TRUE,
'multilang_shop' => TRUE,
'fields' => array(
2015-08-12 14:41:06 +02:00
'id_category' => array('type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => TRUE),
'active' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
2015-07-28 10:33:55 +02:00
'show_home' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'trick' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
2015-07-31 11:37:25 +02:00
'note' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
2015-08-21 14:40:48 +02:00
'video' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanPerso'),
'amazon' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanPerso'),
// Lang fields
2015-07-29 16:58:18 +02:00
'title' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'required' => TRUE),
'slug' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName'),
'intro' => array('type' => self::TYPE_HTML, 'lang' => TRUE, 'validate' => 'isCleanHtml'),
'content' => array('type' => self::TYPE_HTML, 'lang' => TRUE, 'validate' => 'isCleanHtml'),
'meta_title' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName'),
'meta_desc' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName'),
'date_add' => array('type' => self::TYPE_DATE),
'date_upd' => array('type' => self::TYPE_DATE)
),
);
public function __construct($id = NULL, $id_lang = NULL, $id_shop = NULL) {
parent::__construct($id, $id_lang, $id_shop);
}
2015-07-29 16:58:18 +02:00
public static function getPostHome($id_lang, $limit = 14) {
$collection_post = new Collection('CmsPsPost', $id_lang);
$collection_post->where('show_home', '=', 1);
$collection_post->where('active', '=', 1);
$collection_post->orderBy('date_add', 'DESC');
$collection_post->setPageSize(14);
$posts = $collection_post->getResults();
return self::injectsData($posts);
}
/**
* Associated data for each obj Post before display on Front
*
* @param array Array of CmsPsPost Object
* @return array array of post object with data associated
*/
2015-08-12 14:41:06 +02:00
public static function injectsData($articles, $full = FALSE) {
$array = TRUE;
if(!is_array($articles)) {
$array = FALSE;
$articles = array($articles);
}
foreach ($articles as $key => $article) {
$article->new = $article->isNew();
2015-08-12 14:41:06 +02:00
if ($full) {
2015-08-20 11:28:09 +02:00
$article->products = $article->loadProductRelation();
2015-08-12 14:41:06 +02:00
}
}
if(!$array) {
return $articles[0];
2015-07-29 16:58:18 +02:00
}
return $articles;
2015-07-29 16:58:18 +02:00
}
2015-08-13 12:28:22 +02:00
/**
* Load Product relation for display
*/
public function loadProductRelation() {
$id_lang = Context::getContext()->language->id;
$relation_product = $this->getRelations('postcms_product');
$products = array();
if(!empty($relation_product)) {
$collection_product = new Collection('Product', $id_lang);
$collection_product->where('id_product', 'IN', $relation_product);
$collection_product->where('active', '=', 1);
$products = $collection_product->getResults();
}
return $products;
}
2015-08-20 11:28:09 +02:00
/**
* Load PostCms relation for display
*/
public function loadPostCmsRelation() {
$id_lang = Context::getContext()->language->id;
$relation_post = $this->getRelations('postcms');
$articles = array();
if(!empty($relation_post)) {
$collection_product = new Collection('CmsPsPost', $id_lang);
$collection_product->where('id_post', 'IN', $relation_post);
$collection_product->where('active', '=', 1);
$articles = $collection_product->getResults();
}
return $articles;
}
2015-07-29 16:58:18 +02:00
public function isNew() {
$date = new DateTime();
2015-07-29 17:24:15 +02:00
$date->modify('-15 days');
2015-07-29 16:58:18 +02:00
$date_post = new DateTime($this->date_add);
if ($date_post > $date) {
return TRUE;
} else {
return FALSE;
}
}
2015-08-21 14:40:48 +02:00
public function deleteRelations($type_relation, $id_group = NULL) {
if(empty($type_relation)) {
return FALSE;
}
2015-08-21 14:40:48 +02:00
if ($type_relation == 'cms_extrafields_'.$id_group) {
if (!Validate::IsInt($id_group))
return false;
return (bool) Db::getInstance()->delete('cmsps_posts_relation_extrafields', 'id_post = '. $this->id . ' and id_group = '.(int)$id_group);
}
elseif ($type_relation == 'postcms') {
return (bool) Db::getInstance()->delete('cmsps_posts_relation', 'id_post = '. $this->id);
} elseif ($type_relation == 'postcms_product') {
return (bool) Db::getInstance()->delete('cmsps_posts_relation_product', 'id_post = '. $this->id);
2015-08-17 17:14:29 +02:00
} elseif ($type_relation == 'postcms_pack') {
return (bool) Db::getInstance()->delete('cmsps_posts_relation_pack', 'id_post = '. $this->id);
}
}
2015-08-21 14:40:48 +02:00
public function addRelation($id_relation, $type_relation, $id_group = NULL) {
if(empty($type_relation)) {
return FALSE;
}
2015-08-21 14:40:48 +02:00
if ($type_relation == 'postcms') {
return Db::getInstance()->execute('
INSERT INTO
`'._DB_PREFIX_.'cmsps_posts_relation`
VALUES (
'.(int)$this->id.',
'.(int)$id_relation.'
)
');
} elseif ($type_relation == 'postcms_product') {
return Db::getInstance()->execute('
INSERT INTO
`'._DB_PREFIX_.'cmsps_posts_relation_product`
VALUES (
'.(int)$this->id.',
'.(int)$id_relation.'
)
');
2015-08-21 14:40:48 +02:00
} elseif ($type_relation == 'postcms_pack') {
2015-08-17 17:14:29 +02:00
return Db::getInstance()->execute('
INSERT INTO
`'._DB_PREFIX_.'cmsps_posts_relation_pack`
VALUES (
'.(int)$this->id.',
'.(int)$id_relation.'
)
');
2015-08-21 14:40:48 +02:00
} elseif ($type_relation == 'cms_extrafields_'.$id_group) {
if (!Validate::IsInt($id_group))
return false;
return Db::getInstance()->execute('
INSERT INTO
`'._DB_PREFIX_.'cmsps_posts_relation_extrafields`
VALUES (
'.(int)$this->id.',
'.(int)$id_group.',
'.(int)$id_relation.'
)
');
}
}
public function getRelations($page_name = null) {
if(!Validate::isLoadedObject($this))
return array();
$array_tmp = array();
if($page_name == 'postcms') {
$sql = 'SELECT `id_relation`
FROM '._DB_PREFIX_.'cmsps_posts_relation
WHERE id_post = '.(int)$this->id.'';
$infos = Db::getInstance()->executeS($sql);
foreach($infos as $info) {
$array_tmp[$info['id_relation']] = $info['id_relation'];
}
2015-08-17 17:14:29 +02:00
} elseif ($page_name == 'postcms_product') {
$sql = 'SELECT `id_product`
FROM '._DB_PREFIX_.'cmsps_posts_relation_product
WHERE id_post = '.(int)$this->id.'';
$infos = Db::getInstance()->executeS($sql);
foreach($infos as $info) {
$array_tmp[$info['id_product']] = $info['id_product'];
}
2015-08-17 17:14:29 +02:00
} elseif ($page_name == 'postcms_pack') {
$sql = 'SELECT `id_pack`
FROM '._DB_PREFIX_.'cmsps_posts_relation_pack
WHERE id_post = '.(int)$this->id.'';
$infos = Db::getInstance()->executeS($sql);
foreach($infos as $info) {
$array_tmp[$info['id_pack']] = $info['id_pack'];
}
}
return $array_tmp;
}
public function getInfosWithValues($page_name) {
$infos = $this->getRelations($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).')';
} else if ($page_name == 'postcms_product') {
$sql = '
SELECT p.id_product as id, pl.name as content
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON p.`id_product` = pl.`id_product`
AND pl.id_lang = '.$id_lang.'
WHERE p.id_product IN ('.implode(',', $infos).')';
2015-08-17 17:14:29 +02:00
} else if ($page_name == 'postcms_pack') {
$sql = '
SELECT p.id_pack as id, p.title as content
FROM `'._DB_PREFIX_.'cms_pack` p
WHERE p.id_pack IN ('.implode(',', $infos).')';
}
$res = Db::getInstance()->executeS($sql);
return $res;
}
2015-08-21 14:40:48 +02:00
// EXTRAFIELDS CMS
public function getRelationsExtra($id_group) {
$array_tmp = array();
$sql = 'SELECT `id_value`
FROM '._DB_PREFIX_.'cmsps_posts_relation_extrafields
WHERE id_post = '.(int)$this->id.'
AND id_group = '.(int)$id_group;
$infos = Db::getInstance()->executeS($sql);
foreach($infos as $info) {
$array_tmp[$info['id_value']] = $info['id_value'];
}
return $array_tmp;
}
public function getInfosWithValuesExtra($page_name) {
$id_group = str_replace('cms_extrafields_', '', $page_name);
if(!Validate::isInt($id_group))
return array();
$infos = $this->getRelationsExtra($id_group);
2015-09-01 18:26:54 +02:00
if ($infos) {
return Db::getInstance()->executeS('
SELECT p.id_cmsps_extrafields_value as id, p.value as content
FROM `'._DB_PREFIX_.'cmsps_extrafields_value_lang` p
WHERE p.id_cmsps_extrafields_value IN ('.implode(',', $infos).')'
);
} else {
return array();
}
2015-08-21 14:40:48 +02:00
}
}
2015-07-29 16:58:18 +02:00