first commit
This commit is contained in:
452
modules/ph_simpleblog/models/SimpleBlogCategory.php
Normal file
452
modules/ph_simpleblog/models/SimpleBlogCategory.php
Normal file
@@ -0,0 +1,452 @@
|
||||
<?php
|
||||
/**
|
||||
* Blog for PrestaShop module by Krystian Podemski from PrestaHome.
|
||||
*
|
||||
* @author Krystian Podemski <krystian@prestahome.com>
|
||||
* @copyright Copyright (c) 2014-2017 Krystian Podemski - www.PrestaHome.com / www.Podemski.info
|
||||
* @license You only can use module, nothing more!
|
||||
*/
|
||||
class SimpleBlogCategory extends ObjectModel
|
||||
{
|
||||
public $id;
|
||||
public $id_shop;
|
||||
public $id_simpleblog_category;
|
||||
public $name;
|
||||
public $description;
|
||||
public $link_rewrite;
|
||||
public $meta_title;
|
||||
public $meta_keywords;
|
||||
public $meta_description;
|
||||
//public $canonical;
|
||||
public $date_add;
|
||||
public $date_upd;
|
||||
public $cover;
|
||||
public $position;
|
||||
public $image;
|
||||
public $id_parent;
|
||||
public $active = 1;
|
||||
|
||||
protected static $_links = array();
|
||||
|
||||
public static $definition = array(
|
||||
'table' => 'simpleblog_category',
|
||||
'primary' => 'id_simpleblog_category',
|
||||
'multilang' => true,
|
||||
'fields' => array(
|
||||
'cover' => array(
|
||||
'type' => self::TYPE_STRING
|
||||
),
|
||||
'position' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt'
|
||||
),
|
||||
'id_parent' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt'
|
||||
),
|
||||
'active' => array(
|
||||
'type' => self::TYPE_BOOL,
|
||||
'validate' => 'isBool',
|
||||
'required' => true
|
||||
),
|
||||
'date_add' => array(
|
||||
'type' => self::TYPE_DATE,
|
||||
'validate' => 'isDate'
|
||||
),
|
||||
'date_upd' => array(
|
||||
'type' => self::TYPE_DATE,
|
||||
'validate' => 'isDate'
|
||||
),
|
||||
|
||||
// Lang fields
|
||||
'name' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'lang' => true,
|
||||
'validate' => 'isCatalogName',
|
||||
'required' => true,
|
||||
'size' => 64
|
||||
),
|
||||
'link_rewrite' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'lang' => true,
|
||||
'validate' => 'isLinkRewrite',
|
||||
'required' => true,
|
||||
'size' => 64
|
||||
),
|
||||
'description' => array(
|
||||
'type' => self::TYPE_HTML,
|
||||
'lang' => true,
|
||||
'validate' => 'isCleanHtml'
|
||||
),
|
||||
'meta_title' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'lang' => true,
|
||||
'validate' => 'isGenericName',
|
||||
'size' => 128
|
||||
),
|
||||
'meta_description' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'lang' => true,
|
||||
'validate' => 'isGenericName',
|
||||
'size' => 255
|
||||
),
|
||||
'meta_keywords' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'lang' => true,
|
||||
'validate' => 'isGenericName',
|
||||
'size' => 255
|
||||
),
|
||||
// 'canonical' => array(
|
||||
// 'type' => self::TYPE_STRING,
|
||||
// 'lang' => true,
|
||||
// 'validate' => 'isUrlOrEmpty',
|
||||
// ),
|
||||
),
|
||||
);
|
||||
|
||||
public function __construct($id_simpleblog_category = null, $id_lang = null, $id_shop = null)
|
||||
{
|
||||
parent::__construct($id_simpleblog_category, $id_lang, $id_shop);
|
||||
|
||||
if (file_exists(_PS_MODULE_DIR_.'ph_simpleblog/covers_cat/'.$this->id_simpleblog_category.'.'.$this->cover)) {
|
||||
$this->image = _MODULE_DIR_.'ph_simpleblog/covers_cat/'.$this->id_simpleblog_category.'.'.$this->cover;
|
||||
}
|
||||
|
||||
if ($this->id) {
|
||||
$tags = SimpleBlogTag::getPostTags((int) $this->id);
|
||||
$this->tags = $tags;
|
||||
|
||||
if (isset($tags) && isset($tags[$id_lang])) {
|
||||
$this->tags_list = $tags[$id_lang];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function add($autodate = true, $null_values = false)
|
||||
{
|
||||
$this->position = self::getNewLastPosition($this->id_parent);
|
||||
|
||||
foreach ($this->name as $k => $value) {
|
||||
if (preg_match('/^[1-9]\./', $value)) {
|
||||
$this->name[$k] = '0'.$value;
|
||||
}
|
||||
}
|
||||
$ret = parent::add($autodate, $null_values);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
if ((int) $this->id === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::deleteCover($this) && parent::delete() && $this->cleanPositions($this->id_parent);
|
||||
}
|
||||
|
||||
public function update($null_values = false)
|
||||
{
|
||||
foreach ($this->name as $k => $value) {
|
||||
if (preg_match('/^[1-9]\./', $value)) {
|
||||
$this->name[$k] = '0'.$value;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::update($null_values);
|
||||
}
|
||||
|
||||
public function removeChildrens($id_parent)
|
||||
{
|
||||
$result = Db::getInstance()->executeS('
|
||||
SELECT `id_simpleblog_category`
|
||||
FROM `'._DB_PREFIX_.'simpleblog_category`
|
||||
WHERE `id_parent` = '.(int) $id_parent.'
|
||||
');
|
||||
$sizeof = count($result);
|
||||
for ($i = 0; $i < $sizeof; ++$i) {
|
||||
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'simpleblog_category` WHERE id_simpleblog_category = '.(int) $result[$i]['id_simpleblog_category']);
|
||||
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'simpleblog_category_lang` WHERE id_simpleblog_category = '.(int) $result[$i]['id_simpleblog_category']);
|
||||
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'simpleblog_category_shop` WHERE id_simpleblog_category = '.(int) $result[$i]['id_simpleblog_category']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function getNbCats($id_parent = null)
|
||||
{
|
||||
return (int) Db::getInstance()->getValue('
|
||||
SELECT COUNT(*)
|
||||
FROM `'._DB_PREFIX_.'simpleblog_category` sbc
|
||||
'.(!is_null($id_parent) ? 'WHERE sbc.`id_parent` = '.(int) $id_parent : ''));
|
||||
}
|
||||
|
||||
public static function getChildrens($id_parent)
|
||||
{
|
||||
$context = Context::getContext();
|
||||
$id_lang = $context->language->id;
|
||||
$link = $context->link;
|
||||
|
||||
$child_categories = DB::getInstance()->executeS('
|
||||
SELECT *
|
||||
FROM `'._DB_PREFIX_.'simpleblog_category` sbc
|
||||
LEFT JOIN `'._DB_PREFIX_.'simpleblog_category_lang` sbcl
|
||||
ON (sbc.`id_simpleblog_category` = sbcl.`id_simpleblog_category` AND sbcl.`id_lang` = '.(int) $id_lang.')
|
||||
WHERE sbc.`id_parent` = '.(int) $id_parent.' AND sbc.active = 1
|
||||
ORDER BY sbc.`position` ASC
|
||||
');
|
||||
|
||||
if ($child_categories) {
|
||||
foreach ($child_categories as &$category) {
|
||||
$category['url'] = $link->getModuleLink('ph_simpleblog', 'category', array('sb_category' => $category['link_rewrite']));
|
||||
}
|
||||
}
|
||||
|
||||
if (!$child_categories) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return $child_categories;
|
||||
}
|
||||
|
||||
public static function getNewLastPosition($id_parent)
|
||||
{
|
||||
return Db::getInstance()->getValue('
|
||||
SELECT IFNULL(MAX(position),0)+1
|
||||
FROM `'._DB_PREFIX_.'simpleblog_category`
|
||||
WHERE `id_parent` = '.(int) $id_parent);
|
||||
}
|
||||
|
||||
public function move($direction)
|
||||
{
|
||||
$nb_cats = self::getNbCats($this->id_parent);
|
||||
if ($direction != 'l' && $direction != 'r') {
|
||||
return false;
|
||||
}
|
||||
if ($nb_cats <= 1) {
|
||||
return false;
|
||||
}
|
||||
if ($direction == 'l' && $this->position <= 1) {
|
||||
return false;
|
||||
}
|
||||
if ($direction == 'r' && $this->position >= $nb_cats) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$new_position = ($direction == 'l') ? $this->position - 1 : $this->position + 1;
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'simpleblog_category` sbc
|
||||
SET position = '.(int) $this->position.'
|
||||
WHERE id_parent = '.(int) $this->id_parent.'
|
||||
AND position = '.(int) $new_position);
|
||||
$this->position = $new_position;
|
||||
|
||||
return $this->update();
|
||||
}
|
||||
|
||||
public function cleanPositions($id_parent)
|
||||
{
|
||||
$result = Db::getInstance()->executeS('
|
||||
SELECT `id_simpleblog_category`
|
||||
FROM `'._DB_PREFIX_.'simpleblog_category`
|
||||
WHERE `id_parent` = '.(int) $id_parent.'
|
||||
ORDER BY `position`
|
||||
');
|
||||
$sizeof = count($result);
|
||||
for ($i = 0; $i < $sizeof; ++$i) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'simpleblog_category`
|
||||
SET `position` = '.($i + 1).'
|
||||
WHERE `id_simpleblog_category` = '.(int) $result[$i]['id_simpleblog_category']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updatePosition($way, $position)
|
||||
{
|
||||
if (!$res = Db::getInstance()->executeS('
|
||||
SELECT sbc.`id_simpleblog_category`, sbc.`position`, sbc.`id_parent`
|
||||
FROM `'._DB_PREFIX_.'simpleblog_category` sbc
|
||||
WHERE sbc.`id_parent` = '.(int) $this->id_parent.'
|
||||
ORDER BY sbc.`position` ASC')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($res as $cat) {
|
||||
if ((int) $cat['id_simpleblog_category'] == (int) $this->id) {
|
||||
$moved_cat = $cat;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($moved_cat) || !isset($position)) {
|
||||
return false;
|
||||
}
|
||||
// < and > statements rather than BETWEEN operator
|
||||
// since BETWEEN is treated differently according to databases
|
||||
$result = (Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'simpleblog_category`
|
||||
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
|
||||
WHERE `position`
|
||||
'.($way
|
||||
? '> '.(int) $moved_cat['position'].' AND `position` <= '.(int) $position
|
||||
: '< '.(int) $moved_cat['position'].' AND `position` >= '.(int) $position).'
|
||||
AND `id_parent`='.(int) $moved_cat['id_parent'])
|
||||
&& Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'simpleblog_category`
|
||||
SET `position` = '.(int) $position.'
|
||||
WHERE `id_parent` = '.(int) $moved_cat['id_parent'].'
|
||||
AND `id_simpleblog_category`='.(int) $moved_cat['id_simpleblog_category']));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return available categories.
|
||||
*
|
||||
* @param int $id_lang Language ID
|
||||
* @param bool $active return only active categories
|
||||
*
|
||||
* @return array Categories
|
||||
*/
|
||||
public static function getCategories($id_lang, $active = true, $without_parent = true)
|
||||
{
|
||||
if (!Validate::isBool($active)) {
|
||||
die(Tools::displayError());
|
||||
}
|
||||
|
||||
$context = Context::getContext();
|
||||
|
||||
switch (Configuration::get('PH_CATEGORY_SORTBY')) {
|
||||
case 'c.position':
|
||||
$orderby = 'position';
|
||||
break;
|
||||
|
||||
case 'name':
|
||||
$orderby = 'cl.name';
|
||||
break;
|
||||
|
||||
case 'id':
|
||||
$orderby = 'c.id_simpleblog_category';
|
||||
break;
|
||||
|
||||
default:
|
||||
$orderby = 'c.position';
|
||||
break;
|
||||
}
|
||||
|
||||
$sql = '
|
||||
SELECT *
|
||||
FROM `'._DB_PREFIX_.'simpleblog_category` c
|
||||
'.Shop::addSqlAssociation('simpleblog_category', 'c').'
|
||||
LEFT JOIN `'._DB_PREFIX_.'simpleblog_category_lang` cl ON c.`id_simpleblog_category` = cl.`id_simpleblog_category`
|
||||
WHERE `id_lang` = '.(int) $id_lang.'
|
||||
'.($active ? 'AND `active` = 1' : '').'
|
||||
'.($without_parent ? 'AND `id_parent` = 0' : '').'
|
||||
ORDER BY '.$orderby.' ASC';
|
||||
|
||||
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
|
||||
|
||||
$categories = array();
|
||||
|
||||
foreach ($result as $row) {
|
||||
$categories[$row['id_simpleblog_category']]['name'] = $row['name'];
|
||||
$categories[$row['id_simpleblog_category']]['url'] = self::getLink($row['link_rewrite'], $id_lang);
|
||||
$categories[$row['id_simpleblog_category']]['id'] = $row['id_simpleblog_category'];
|
||||
$categories[$row['id_simpleblog_category']]['is_child'] = $row['id_parent'] > 0 ? true : false;
|
||||
if (sizeof(self::getChildrens($row['id_simpleblog_category']))) {
|
||||
$categories[$row['id_simpleblog_category']]['childrens'] = self::getChildrens($row['id_simpleblog_category']);
|
||||
}
|
||||
}
|
||||
|
||||
return $categories;
|
||||
}
|
||||
|
||||
public static function getSimpleCategories($id_lang)
|
||||
{
|
||||
switch (Configuration::get('PH_CATEGORY_SORTBY')) {
|
||||
case 'c.position':
|
||||
$orderby = 'position';
|
||||
break;
|
||||
|
||||
case 'name':
|
||||
$orderby = 'cl.name';
|
||||
break;
|
||||
|
||||
case 'id':
|
||||
$orderby = 'c.id_simpleblog_category';
|
||||
break;
|
||||
|
||||
default:
|
||||
$orderby = 'c.position';
|
||||
break;
|
||||
}
|
||||
|
||||
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
|
||||
SELECT c.`id_simpleblog_category`, cl.`name`
|
||||
FROM `'._DB_PREFIX_.'simpleblog_category` c
|
||||
LEFT JOIN `'._DB_PREFIX_.'simpleblog_category_lang` cl ON (c.`id_simpleblog_category` = cl.`id_simpleblog_category`)
|
||||
WHERE cl.`id_lang` = '.(int) $id_lang.'
|
||||
ORDER BY '.$orderby.' ASC');
|
||||
}
|
||||
|
||||
public static function getByRewrite($rewrite = null, $id_lang = false)
|
||||
{
|
||||
if (!$rewrite) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sql = new DbQuery();
|
||||
$sql->select('l.id_simpleblog_category');
|
||||
$sql->from('simpleblog_category_lang', 'l');
|
||||
|
||||
if ($id_lang) {
|
||||
$sql->where('l.link_rewrite = \''.$rewrite.'\' AND l.id_lang = '.(int) $id_lang);
|
||||
} else {
|
||||
$sql->where('l.link_rewrite = \''.$rewrite.'\'');
|
||||
}
|
||||
|
||||
$category = new self(Db::getInstance()->getValue($sql), (int) $id_lang);
|
||||
|
||||
return $category;
|
||||
}
|
||||
|
||||
public static function getRewriteByCategory($id_simpleblog_category, $id_lang)
|
||||
{
|
||||
$sql = new DbQuery();
|
||||
$sql->select('l.link_rewrite');
|
||||
$sql->from('simpleblog_category_lang', 'l');
|
||||
$sql->where('l.id_simpleblog_category = '.$id_simpleblog_category.' AND l.id_lang = '.(int) $id_lang);
|
||||
|
||||
return Db::getInstance()->getValue($sql);
|
||||
}
|
||||
|
||||
public static function getLink($rewrite, $id_lang = null, $id_shop = null)
|
||||
{
|
||||
return Context::getContext()->link->getModuleLink('ph_simpleblog', 'category', array('sb_category' => $rewrite));
|
||||
}
|
||||
|
||||
public function getObjectLink()
|
||||
{
|
||||
return self::getLink($this->link_rewrite);
|
||||
}
|
||||
|
||||
public static function deleteCover($object)
|
||||
{
|
||||
$tmp_location = _PS_TMP_IMG_DIR_.'ph_simpleblog_cat_'.$object->id.'.'.$object->cover;
|
||||
if (file_exists($tmp_location)) {
|
||||
@unlink($tmp_location);
|
||||
}
|
||||
|
||||
$orig_location = _PS_MODULE_DIR_.'ph_simpleblog/covers_cat/'.$object->id.'.'.$object->cover;
|
||||
|
||||
if (file_exists($orig_location)) {
|
||||
@unlink($orig_location);
|
||||
}
|
||||
|
||||
$object->cover = null;
|
||||
$object->update();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
143
modules/ph_simpleblog/models/SimpleBlogComment.php
Normal file
143
modules/ph_simpleblog/models/SimpleBlogComment.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/**
|
||||
* Blog for PrestaShop module by Krystian Podemski from PrestaHome.
|
||||
*
|
||||
* @author Krystian Podemski <krystian@prestahome.com>
|
||||
* @copyright Copyright (c) 2014-2017 Krystian Podemski - www.PrestaHome.com / www.Podemski.info
|
||||
* @license You only can use module, nothing more!
|
||||
*/
|
||||
require_once _PS_MODULE_DIR_.'ph_simpleblog/ph_simpleblog.php';
|
||||
|
||||
class SimpleBlogComment extends ObjectModel
|
||||
{
|
||||
private static $commentHierarchy = array();
|
||||
|
||||
public $id_simpleblog_comment;
|
||||
public $id_simpleblog_post;
|
||||
public $id_parent = 0;
|
||||
public $id_customer;
|
||||
public $id_guest;
|
||||
public $name;
|
||||
public $email;
|
||||
public $comment;
|
||||
public $active = 0;
|
||||
public $ip;
|
||||
public $date_add;
|
||||
public $date_upd;
|
||||
|
||||
/**
|
||||
* @see ObjectModel::$definition
|
||||
*/
|
||||
public static $definition = array(
|
||||
'table' => 'simpleblog_comment',
|
||||
'primary' => 'id_simpleblog_comment',
|
||||
'multilang' => false,
|
||||
'fields' => array(
|
||||
'id_simpleblog_comment' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt'
|
||||
),
|
||||
'id_simpleblog_post' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt'
|
||||
),
|
||||
'id_parent' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt'
|
||||
),
|
||||
'id_customer' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt'
|
||||
),
|
||||
'id_guest' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt'
|
||||
),
|
||||
'name' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isCleanHtml',
|
||||
'size' => 255
|
||||
),
|
||||
'email' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isCleanHtml',
|
||||
'size' => 140
|
||||
),
|
||||
'comment' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isCleanHtml'
|
||||
),
|
||||
'active' => array(
|
||||
'type' => self::TYPE_BOOL,
|
||||
'validate' => 'isBool'
|
||||
),
|
||||
'ip' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isCleanHtml',
|
||||
'size' => 255
|
||||
),
|
||||
'date_add' => array(
|
||||
'type' => self::TYPE_DATE,
|
||||
'validate' => 'isDate'
|
||||
),
|
||||
'date_upd' => array(
|
||||
'type' => self::TYPE_DATE,
|
||||
'validate' => 'isDate'
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
public function __construct($id_simpleblog_comment = null, $id_lang = null, $id_shop = null)
|
||||
{
|
||||
parent::__construct($id_simpleblog_comment, $id_lang, $id_shop);
|
||||
}
|
||||
|
||||
public static function getComments($id_simpleblog_post, $withHierarchy = true)
|
||||
{
|
||||
$response = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(
|
||||
'SELECT id_simpleblog_comment, id_parent
|
||||
FROM '._DB_PREFIX_.'simpleblog_comment
|
||||
WHERE id_simpleblog_post = '.(int) $id_simpleblog_post.'
|
||||
AND active = 1'
|
||||
);
|
||||
|
||||
if ($withHierarchy) {
|
||||
return self::renderComments($response);
|
||||
} else {
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
public static function renderComments(&$comments, $parent = 0, $depth = 0)
|
||||
{
|
||||
foreach ($comments as $key => $comment) {
|
||||
if ($comment['id_parent'] == $parent) {
|
||||
$SimpleBlogComment = new self($comment['id_simpleblog_comment']);
|
||||
|
||||
self::$commentHierarchy[$comment['id_simpleblog_comment']]['depth'] = $depth;
|
||||
self::$commentHierarchy[$comment['id_simpleblog_comment']]['id'] = (int) $SimpleBlogComment->id_simpleblog_comment;
|
||||
self::$commentHierarchy[$comment['id_simpleblog_comment']]['name'] = $SimpleBlogComment->name;
|
||||
self::$commentHierarchy[$comment['id_simpleblog_comment']]['email'] = $SimpleBlogComment->email;
|
||||
self::$commentHierarchy[$comment['id_simpleblog_comment']]['comment'] = $SimpleBlogComment->comment;
|
||||
self::$commentHierarchy[$comment['id_simpleblog_comment']]['date_add'] = $SimpleBlogComment->date_add;
|
||||
|
||||
unset($comments[$key]);
|
||||
self::renderComments($comments, $comment['id_simpleblog_comment'], $depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
reset($comments);
|
||||
|
||||
return self::$commentHierarchy;
|
||||
}
|
||||
|
||||
public static function getCommentsCount($id_simpleblog_post)
|
||||
{
|
||||
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
|
||||
'SELECT COUNT(id_simpleblog_comment)
|
||||
FROM '._DB_PREFIX_.'simpleblog_comment
|
||||
WHERE id_simpleblog_post = '.(int) $id_simpleblog_post.'
|
||||
AND active = 1'
|
||||
);
|
||||
}
|
||||
}
|
||||
935
modules/ph_simpleblog/models/SimpleBlogPost.php
Normal file
935
modules/ph_simpleblog/models/SimpleBlogPost.php
Normal file
@@ -0,0 +1,935 @@
|
||||
<?php
|
||||
/**
|
||||
* Blog for PrestaShop module by Krystian Podemski from PrestaHome.
|
||||
*
|
||||
* @author Krystian Podemski <krystian@prestahome.com>
|
||||
* @copyright Copyright (c) 2008-2019 Krystian Podemski - www.PrestaHome.com / www.Podemski.info
|
||||
* @license You only can use module, nothing more!
|
||||
*/
|
||||
require_once _PS_MODULE_DIR_.'ph_simpleblog/ph_simpleblog.php';
|
||||
|
||||
class SimpleBlogPost extends ObjectModel
|
||||
{
|
||||
public $id;
|
||||
public $id_simpleblog_post;
|
||||
public $id_simpleblog_category;
|
||||
public $id_simpleblog_post_type;
|
||||
public $id_simpleblog_author;
|
||||
public $author;
|
||||
public $likes;
|
||||
public $views;
|
||||
public $allow_comments = 3;
|
||||
public $is_featured = 0;
|
||||
public $access;
|
||||
public $cover;
|
||||
public $featured;
|
||||
public $id_product;
|
||||
public $active = 1;
|
||||
|
||||
public $title;
|
||||
public $meta_title;
|
||||
public $meta_description;
|
||||
public $meta_keywords;
|
||||
// public $canonical;
|
||||
public $short_content;
|
||||
public $content;
|
||||
public $link_rewrite;
|
||||
public $video_code;
|
||||
public $external_url;
|
||||
|
||||
public $date_add;
|
||||
public $date_upd;
|
||||
|
||||
public $featured_image;
|
||||
public $banner;
|
||||
public $tags;
|
||||
public $post_type;
|
||||
public $category_url;
|
||||
public $parent_category = false;
|
||||
public $category;
|
||||
|
||||
public static $definition = array(
|
||||
'table' => 'simpleblog_post',
|
||||
'primary' => 'id_simpleblog_post',
|
||||
'multilang' => true,
|
||||
'fields' => array(
|
||||
'id_simpleblog_category' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt',
|
||||
),
|
||||
'id_simpleblog_post_type' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt',
|
||||
),
|
||||
'id_simpleblog_author' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt',
|
||||
),
|
||||
'active' => array(
|
||||
'type' => self::TYPE_BOOL,
|
||||
),
|
||||
'is_featured' => array(
|
||||
'type' => self::TYPE_BOOL,
|
||||
),
|
||||
'access' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
),
|
||||
'author' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
),
|
||||
'likes' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt',
|
||||
),
|
||||
'views' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt',
|
||||
),
|
||||
'allow_comments' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedInt',
|
||||
),
|
||||
'cover' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
),
|
||||
'featured' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
),
|
||||
'id_product' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'size' => '3999999999999',
|
||||
),
|
||||
'date_add' => array(
|
||||
'type' => self::TYPE_DATE,
|
||||
'validate' => 'isDate',
|
||||
),
|
||||
'date_upd' => array(
|
||||
'type' => self::TYPE_DATE,
|
||||
'validate' => 'isDate',
|
||||
),
|
||||
|
||||
// Lang fields
|
||||
'title' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'lang' => true,
|
||||
'validate' => 'isGenericName',
|
||||
'required' => true,
|
||||
'size' => 255,
|
||||
),
|
||||
'meta_title' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'lang' => true,
|
||||
'validate' => 'isGenericName',
|
||||
'size' => 255,
|
||||
),
|
||||
'meta_description' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'lang' => true,
|
||||
'validate' => 'isGenericName',
|
||||
'size' => 255,
|
||||
),
|
||||
'meta_keywords' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'lang' => true,
|
||||
'validate' => 'isGenericName',
|
||||
'size' => 255,
|
||||
),
|
||||
// 'canonical' => array(
|
||||
// 'type' => self::TYPE_STRING,
|
||||
// 'lang' => true,
|
||||
// 'validate' => 'isUrlOrEmpty',
|
||||
// ),
|
||||
'link_rewrite' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'lang' => true,
|
||||
'validate' => 'isLinkRewrite',
|
||||
'required' => true,
|
||||
'size' => 128,
|
||||
),
|
||||
'short_content' => array(
|
||||
'type' => self::TYPE_HTML,
|
||||
'lang' => true,
|
||||
'validate' => 'isCleanHtml',
|
||||
'size' => 3999999999999,
|
||||
),
|
||||
'content' => array(
|
||||
'type' => self::TYPE_HTML,
|
||||
'lang' => true,
|
||||
'validate' => 'isCleanHtml',
|
||||
'size' => 3999999999999,
|
||||
),
|
||||
'video_code' => array(
|
||||
'type' => self::TYPE_HTML,
|
||||
'lang' => true,
|
||||
'validate' => 'isCleanHtml',
|
||||
'size' => 3999999999999,
|
||||
),
|
||||
'external_url' => array(
|
||||
'type' => self::TYPE_HTML,
|
||||
'lang' => true,
|
||||
'validate' => 'isCleanHtml',
|
||||
'size' => 3999999999999,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
public function __construct($id_simpleblog_post = null, $id_lang = null, $id_shop = null)
|
||||
{
|
||||
parent::__construct($id_simpleblog_post, $id_lang, $id_shop);
|
||||
|
||||
$context = Context::getContext();
|
||||
|
||||
if ($id_lang && $this->id) {
|
||||
$category = new SimpleBlogCategory($this->id_simpleblog_category, $id_lang);
|
||||
$this->category = $category->name;
|
||||
$this->category_rewrite = $category->link_rewrite;
|
||||
|
||||
$this->url = $context->link->getModuleLink(
|
||||
'ph_simpleblog',
|
||||
'single',
|
||||
array(
|
||||
'rewrite' => $this->link_rewrite,
|
||||
'sb_category' => $this->category_rewrite,
|
||||
)
|
||||
);
|
||||
|
||||
$this->category_url = $context->link->getModuleLink(
|
||||
'ph_simpleblog',
|
||||
'category',
|
||||
array(
|
||||
'sb_category' => $this->category_rewrite,
|
||||
)
|
||||
);
|
||||
|
||||
if ($category->id_parent > 0) {
|
||||
$this->parent_category = new SimpleBlogCategory($category->id_parent, $id_lang);
|
||||
}
|
||||
}
|
||||
|
||||
if (file_exists(_PS_MODULE_DIR_.'ph_simpleblog/covers/'.$this->id_simpleblog_post.'.'.$this->cover)) {
|
||||
$this->banner = _MODULE_DIR_.'ph_simpleblog/covers/'.$this->id_simpleblog_post.'.'.$this->cover;
|
||||
$this->banner_thumb = _MODULE_DIR_.'ph_simpleblog/covers/'.$this->id_simpleblog_post.'-thumb.'.$this->cover;
|
||||
$this->banner_wide = _MODULE_DIR_.'ph_simpleblog/covers/'.$this->id_simpleblog_post.'-wide.'.$this->cover;
|
||||
}
|
||||
|
||||
if (file_exists(_PS_MODULE_DIR_.'ph_simpleblog/featured/'.$this->id_simpleblog_post.'.'.$this->featured)) {
|
||||
$this->featured_image = _MODULE_DIR_.'ph_simpleblog/featured/'.$this->id_simpleblog_post.'.'.$this->featured;
|
||||
}
|
||||
|
||||
if ($this->id) {
|
||||
$tags = SimpleBlogTag::getPostTags((int) $this->id);
|
||||
$this->tags = $tags;
|
||||
|
||||
if (isset($tags) && isset($tags[$id_lang])) {
|
||||
$this->tags_list = $tags[$id_lang];
|
||||
}
|
||||
|
||||
$this->comments = SimpleBlogComment::getCommentsCount((int) $this->id);
|
||||
$this->post_type = SimpleBlogPostType::getSlugById((int) $this->id_simpleblog_post_type);
|
||||
|
||||
if ($this->post_type == 'gallery') {
|
||||
$this->gallery = SimpleBlogPostImage::getAllById((int) $this->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function add($autodate = false, $null_values = false)
|
||||
{
|
||||
$ret = parent::add($autodate, $null_values);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
if ((int) $this->id === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::deleteCover($this->id)
|
||||
&& self::deleteFeatured($this->id)
|
||||
&& parent::delete();
|
||||
}
|
||||
|
||||
public static function getUrlRewriteInformations($id_simpleblog)
|
||||
{
|
||||
$sql = 'SELECT l.`id_lang`, c.`link_rewrite`
|
||||
FROM `'._DB_PREFIX_.'simpleblog_lang` AS c
|
||||
LEFT JOIN `'._DB_PREFIX_.'lang` AS l ON c.`id_lang` = l.`id_lang`
|
||||
WHERE c.`id_simpleblog` = '.(int) $id_simpleblog.'
|
||||
AND l.`active` = 1';
|
||||
|
||||
return Db::getInstance()->executeS($sql);
|
||||
}
|
||||
|
||||
public static function getSimplePosts($id_lang, $id_shop = null, Context $context = null, $filter = null, $selected = array(), $archive = false)
|
||||
{
|
||||
if (!isset($context)) {
|
||||
$context = Context::getContext();
|
||||
}
|
||||
|
||||
if (!$id_shop) {
|
||||
$id_shop = $context->shop->id;
|
||||
}
|
||||
|
||||
$sql = new DbQuery();
|
||||
$sql->select('sbp.id_simpleblog_post, l.title');
|
||||
$sql->from('simpleblog_post', 'sbp');
|
||||
$sql->innerJoin('simpleblog_post_lang', 'l', 'sbp.id_simpleblog_post = l.id_simpleblog_post AND l.id_lang = '.(int) $id_lang);
|
||||
$sql->innerJoin('simpleblog_post_shop', 'sbps', 'sbp.id_simpleblog_post = sbps.id_simpleblog_post AND sbps.id_shop = '.(int) $id_shop);
|
||||
|
||||
if ($filter) {
|
||||
$sql->where('sbp.id_simpleblog_post '.$filter.' ('.implode(',', $selected).')');
|
||||
}
|
||||
|
||||
$sql->where('sbp.date_add <= \''.SimpleBlogHelper::now(Configuration::get('PH_BLOG_TIMEZONE')).'\'');
|
||||
$sql->where('sbp.active = 1');
|
||||
|
||||
$posts = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
|
||||
|
||||
if (sizeof($posts)) {
|
||||
$posts = self::checkAccess($posts);
|
||||
}
|
||||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
public static function checkAccess($posts)
|
||||
{
|
||||
if (Context::getContext()->customer) {
|
||||
foreach ($posts as $key => $post) {
|
||||
if ($userGroups = Context::getContext()->customer->getGroups()) {
|
||||
$tmpLinkGroups = unserialize($post['access']);
|
||||
$linkGroups = array();
|
||||
|
||||
foreach ($tmpLinkGroups as $groupID => $status) {
|
||||
if ($status) {
|
||||
$linkGroups[] = $groupID;
|
||||
}
|
||||
}
|
||||
|
||||
$intersect = array_intersect($userGroups, $linkGroups);
|
||||
if (!count($intersect)) {
|
||||
unset($posts[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $posts;
|
||||
} else {
|
||||
return $posts;
|
||||
}
|
||||
}
|
||||
|
||||
public static function findPosts($type = 'author', $keyword = false, $id_lang = null, $limit = 10, $page = null)
|
||||
{
|
||||
if ($type == 'author') {
|
||||
return self::getPosts($id_lang, $limit, null, $page, $page, null, null, null, null, false, $keyword);
|
||||
} elseif ($type == 'tag') {
|
||||
return self::getPosts($id_lang, $limit, null, $page, $page, null, null, null, null, false, false, $keyword);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getAllAvailablePosts($id_lang)
|
||||
{
|
||||
return self::getPosts($id_lang, 99999, null, null, false, false, false, null, false, false, null, false, false, false);
|
||||
}
|
||||
|
||||
public static function getPosts(
|
||||
$id_lang,
|
||||
$limit = 10,
|
||||
$id_simpleblog_category = null,
|
||||
$page = null,
|
||||
$active = true,
|
||||
$orderby = false,
|
||||
$orderway = false,
|
||||
$exclude = null,
|
||||
$featured = false,
|
||||
$author = false,
|
||||
$id_shop = null,
|
||||
$filter = false,
|
||||
$selected = array(),
|
||||
$check_access = true
|
||||
) {
|
||||
$context = Context::getContext();
|
||||
|
||||
$start = $limit * ($page == 0 ? 0 : $page - 1);
|
||||
|
||||
$sql = new DbQuery();
|
||||
$sql->select('*');
|
||||
$sql->from('simpleblog_post', 'sbp');
|
||||
|
||||
if ($id_lang) {
|
||||
$sql->innerJoin('simpleblog_post_lang', 'l', 'sbp.id_simpleblog_post = l.id_simpleblog_post AND l.id_lang = '.(int) $id_lang);
|
||||
}
|
||||
|
||||
if (!$id_shop) {
|
||||
$id_shop = $context->shop->id;
|
||||
}
|
||||
|
||||
$sql->innerJoin('simpleblog_post_shop', 'sbps', 'sbp.id_simpleblog_post = sbps.id_simpleblog_post AND sbps.id_shop = '.(int) $id_shop);
|
||||
|
||||
if ($active) {
|
||||
$sql->where('sbp.active = 1');
|
||||
}
|
||||
|
||||
if (isset($id_simpleblog_category) && (int) $id_simpleblog_category > 0) {
|
||||
$childrens = SimpleBlogCategory::getChildrens((int) $id_simpleblog_category);
|
||||
if ($childrens && sizeof($childrens)) {
|
||||
$child_categories = array((int) $id_simpleblog_category);
|
||||
foreach ($childrens as $child) {
|
||||
$child_categories[] = $child['id_simpleblog_category'];
|
||||
}
|
||||
$sql->where('sbp.id_simpleblog_category IN ('.implode(',', $child_categories).')');
|
||||
} else {
|
||||
$sql->where('sbp.id_simpleblog_category = '.(int) $id_simpleblog_category);
|
||||
}
|
||||
}
|
||||
|
||||
if ($exclude) {
|
||||
$sql->where('sbp.id_simpleblog_post != '.(int) $exclude);
|
||||
}
|
||||
|
||||
if ($featured) {
|
||||
$sql->where('sbp.is_featured = 1');
|
||||
}
|
||||
|
||||
if ($author && Configuration::get('PH_BLOG_POST_BY_AUTHOR')) {
|
||||
$sql->where('sbp.author = \''.pSQL($author).'\'');
|
||||
}
|
||||
|
||||
if ($filter) {
|
||||
$sql->where('sbp.id_simpleblog_post '.$filter.' ('.implode(',', $selected).')');
|
||||
}
|
||||
|
||||
$sql->where('sbp.date_add <= \''.SimpleBlogHelper::now(Configuration::get('PH_BLOG_TIMEZONE')).'\'');
|
||||
|
||||
if (!$orderby) {
|
||||
$orderby = 'sbp.date_add';
|
||||
}
|
||||
|
||||
if (!$orderway) {
|
||||
$orderway = 'DESC';
|
||||
}
|
||||
|
||||
$sql->orderBy($orderby.' '.$orderway);
|
||||
|
||||
$sql->limit($limit, $start);
|
||||
|
||||
$result = Db::getInstance()->executeS($sql);
|
||||
|
||||
if (sizeof($result) && $check_access == true) {
|
||||
$result = self::checkAccess($result);
|
||||
}
|
||||
|
||||
if (sizeof($result)) {
|
||||
foreach ($result as &$row) {
|
||||
$category_rewrite = SimpleBlogCategory::getRewriteByCategory($row['id_simpleblog_category'], $id_lang);
|
||||
|
||||
$category_obj = new SimpleBlogCategory($row['id_simpleblog_category'], $id_lang);
|
||||
|
||||
$category_url = SimpleBlogCategory::getLink($category_obj->link_rewrite, $id_lang);
|
||||
|
||||
if (file_exists(_PS_MODULE_DIR_.'ph_simpleblog/covers/'.$row['id_simpleblog_post'].'.'.$row['cover'])) {
|
||||
$row['banner'] = _MODULE_DIR_.'ph_simpleblog/covers/'.$row['id_simpleblog_post'].'.'.$row['cover'];
|
||||
$row['banner_thumb'] = _MODULE_DIR_.'ph_simpleblog/covers/'.$row['id_simpleblog_post'].'-thumb.'.$row['cover'];
|
||||
$row['banner_wide'] = _MODULE_DIR_.'ph_simpleblog/covers/'.$row['id_simpleblog_post'].'-wide.'.$row['cover'];
|
||||
}
|
||||
|
||||
if (file_exists(_PS_MODULE_DIR_.'ph_simpleblog/featured/'.$row['id_simpleblog_post'].'.'.$row['featured'])) {
|
||||
$row['featured'] = _MODULE_DIR_.'ph_simpleblog/featured/'.$row['id_simpleblog_post'].'.'.$row['featured'];
|
||||
}
|
||||
|
||||
$row['url'] = self::getLink($row['link_rewrite'], $category_obj->link_rewrite, $id_lang);
|
||||
$row['category'] = $category_obj->name;
|
||||
$row['category_rewrite'] = $category_obj->link_rewrite;
|
||||
$row['category_url'] = $category_url;
|
||||
|
||||
$row['allow_comments'] = self::checkIfAllowComments($row['allow_comments']);
|
||||
$row['comments'] = SimpleBlogComment::getCommentsCount((int) $row['id_simpleblog_post']);
|
||||
|
||||
$tags = SimpleBlogTag::getPostTags((int) $row['id_simpleblog_post']);
|
||||
|
||||
$row['tags'] = isset($tags[$id_lang]) && sizeof($tags[$id_lang] > 0) ? $tags[$id_lang] : false;
|
||||
$row['post_type'] = SimpleBlogPostType::getSlugById((int) $row['id_simpleblog_post_type']);
|
||||
|
||||
if ($row['post_type'] == 'gallery') {
|
||||
$row['gallery'] = SimpleBlogPostImage::getAllById((int) $row['id_simpleblog_post']);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function getLink($rewrite, $category)
|
||||
{
|
||||
return Context::getContext()->link->getModuleLink('ph_simpleblog', 'single', array('rewrite' => $rewrite, 'sb_category' => $category));
|
||||
}
|
||||
|
||||
public static function getSearchLink($type = 'author', $keyword = false)
|
||||
{
|
||||
return Context::getContext()->link->getModuleLink('ph_simpleblog', 'search', array('type' => $type, 'keyword' => $keyword));
|
||||
}
|
||||
|
||||
public static function getByRewrite($rewrite = null, $id_lang = null)
|
||||
{
|
||||
if (!$rewrite) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sql = new DbQuery();
|
||||
$sql->select('l.id_simpleblog_post');
|
||||
$sql->from('simpleblog_post_lang', 'l');
|
||||
|
||||
if ($id_lang) {
|
||||
$sql->where('l.link_rewrite = \''.$rewrite.'\' AND l.id_lang = '.(int) $id_lang);
|
||||
} else {
|
||||
$sql->where('l.link_rewrite = \''.$rewrite.'\'');
|
||||
}
|
||||
|
||||
$result = Db::getInstance()->getValue($sql);
|
||||
|
||||
if (!$result) {
|
||||
$sql = new DbQuery();
|
||||
$sql->select('l.id_simpleblog_post');
|
||||
$sql->from('simpleblog_post_lang', 'l');
|
||||
$sql->where('l.link_rewrite = \''.$rewrite.'\'');
|
||||
$searched_post = Db::getInstance()->getValue($sql);
|
||||
|
||||
if ($searched_post) {
|
||||
$sql = new DbQuery();
|
||||
$sql->select('l.link_rewrite');
|
||||
$sql->from('simpleblog_post_lang', 'l');
|
||||
$sql->where('l.id_lang = '.(int) $id_lang.' AND l.id_simpleblog_post = '.(int) $searched_post);
|
||||
$rewrite = Db::getInstance()->getValue($sql);
|
||||
}
|
||||
|
||||
if ($rewrite) {
|
||||
$sql = new DbQuery();
|
||||
$sql->select('l.id_simpleblog_post');
|
||||
$sql->from('simpleblog_post_lang', 'l');
|
||||
|
||||
if ($id_lang) {
|
||||
$sql->where('l.link_rewrite = \''.$rewrite.'\' AND l.id_lang = '.(int) $id_lang);
|
||||
} else {
|
||||
$sql->where('l.link_rewrite = \''.$rewrite.'\'');
|
||||
}
|
||||
|
||||
$post = new self(Db::getInstance()->getValue($sql), $id_lang);
|
||||
|
||||
return $post;
|
||||
} else {
|
||||
return '404';
|
||||
}
|
||||
} else {
|
||||
$post = new self(Db::getInstance()->getValue($sql), $id_lang);
|
||||
|
||||
return $post;
|
||||
}
|
||||
}
|
||||
|
||||
public function getTags($id_lang)
|
||||
{
|
||||
if (is_null($this->tags)) {
|
||||
$this->tags = SimpleBlogTag::getPostTags($this->id);
|
||||
}
|
||||
|
||||
if (!($this->tags && key_exists($id_lang, $this->tags))) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$result = '';
|
||||
foreach ($this->tags[$id_lang] as $tag_name) {
|
||||
$result .= $tag_name.', ';
|
||||
}
|
||||
|
||||
return rtrim($result, ', ');
|
||||
}
|
||||
|
||||
public static function getPageLink($page_nb, $type = false, $rewrite = false)
|
||||
{
|
||||
$url = ph_simpleblog::myRealUrl();
|
||||
$id_lang = Context::getContext()->language->id;
|
||||
$context = Context::getContext();
|
||||
|
||||
$dispatcher = Dispatcher::getInstance();
|
||||
$params = array();
|
||||
$additionalParams = array();
|
||||
$params['p'] = $page_nb;
|
||||
|
||||
|
||||
if ($type == 'category') {
|
||||
if ($page_nb == 1 && isset($rewrite)) {
|
||||
return SimpleBlogCategory::getLink($rewrite, $id_lang);
|
||||
}
|
||||
|
||||
if (isset($rewrite)) {
|
||||
$params['sb_category'] = $rewrite;
|
||||
|
||||
return $url.$dispatcher->createUrl('module-ph_simpleblog-categorypage', $id_lang, $params);
|
||||
}
|
||||
}
|
||||
|
||||
if (Tools::getValue('y', 0)) {
|
||||
$additionalParams['y'] = (int) Tools::getValue('y');
|
||||
}
|
||||
|
||||
if ($page_nb > 1) {
|
||||
return $context->link->getModuleLink('ph_simpleblog', 'page', array_merge($params, $additionalParams));
|
||||
} else {
|
||||
return $context->link->getModuleLink('ph_simpleblog', 'list', $additionalParams);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getPaginationLink($nb = false, $sort = false, $pagination = true, $array = false)
|
||||
{
|
||||
$vars = array();
|
||||
$vars_nb = array('n', 'search_query');
|
||||
$vars_sort = array('orderby', 'orderway');
|
||||
$vars_pagination = array('p');
|
||||
$url = ph_simpleblog::myRealUrl();
|
||||
|
||||
foreach ($_GET as $k => $value) {
|
||||
if (Configuration::get('PS_REWRITING_SETTINGS') && ($k == 'isolang' || $k == 'id_lang')) {
|
||||
continue;
|
||||
}
|
||||
$if_nb = (!$nb || ($nb && !in_array($k, $vars_nb)));
|
||||
$if_sort = (!$sort || ($sort && !in_array($k, $vars_sort)));
|
||||
$if_pagination = (!$pagination || ($pagination && !in_array($k, $vars_pagination)));
|
||||
if ($if_nb && $if_sort && $if_pagination) {
|
||||
if (!is_array($value)) {
|
||||
$vars[urlencode($k)] = $value;
|
||||
} else {
|
||||
foreach (explode('&', http_build_query(array($k => $value), '', '&')) as $key => $val) {
|
||||
$data = explode('=', $val);
|
||||
$vars[urldecode($data[0])] = $data[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$array) {
|
||||
if (count($vars)) {
|
||||
return $url.((Configuration::get('PS_REWRITING_SETTINGS') == 1) ? '?' : '&').http_build_query($vars, '', '&');
|
||||
} else {
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
|
||||
$vars['requestUrl'] = $url;
|
||||
|
||||
return $vars;
|
||||
}
|
||||
|
||||
public static function deleteCover($id_simpleblog_post)
|
||||
{
|
||||
$post = new SimpleBlogPost((int) $id_simpleblog_post);
|
||||
|
||||
if (!Validate::isLoadedObject($post)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tmp_location = _PS_TMP_IMG_DIR_.'ph_simpleblog_'.$post->id.'.'.$post->cover;
|
||||
if (file_exists($tmp_location)) {
|
||||
@unlink($tmp_location);
|
||||
}
|
||||
|
||||
$orig_location = _PS_MODULE_DIR_.'ph_simpleblog/covers/'.$post->id.'.'.$post->cover;
|
||||
$thumb = _PS_MODULE_DIR_.'ph_simpleblog/covers/'.$post->id.'-thumb.'.$post->cover;
|
||||
$thumbWide = _PS_MODULE_DIR_.'ph_simpleblog/covers/'.$post->id.'-wide.'.$post->cover;
|
||||
|
||||
if (file_exists($orig_location)) {
|
||||
@unlink($orig_location);
|
||||
}
|
||||
|
||||
if (file_exists($thumb)) {
|
||||
@unlink($thumb);
|
||||
}
|
||||
|
||||
if (file_exists($thumbWide)) {
|
||||
@unlink($thumbWide);
|
||||
}
|
||||
|
||||
Db::getInstance()->update('simpleblog_post', array('cover' => ''), 'id_simpleblog_post = '.$post->id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function deleteFeatured($id_simpleblog_post)
|
||||
{
|
||||
$post = new SimpleBlogPost((int) $id_simpleblog_post);
|
||||
|
||||
if (!Validate::isLoadedObject($post)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tmp_location = _PS_TMP_IMG_DIR_.'ph_simpleblog_'.$post->id.'.'.$post->featured;
|
||||
if (file_exists($tmp_location)) {
|
||||
@unlink($tmp_location);
|
||||
}
|
||||
|
||||
$orig_location = _PS_MODULE_DIR_.'ph_simpleblog/featured/'.$post->id.'.'.$post->featured;
|
||||
|
||||
if (file_exists($orig_location)) {
|
||||
@unlink($orig_location);
|
||||
}
|
||||
|
||||
Db::getInstance()->update('simpleblog_post', array('featured' => ''), 'id_simpleblog_post = '.$post->id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function regenerateThumbnails()
|
||||
{
|
||||
$posts = self::getAllAvailablePosts(Context::getContext()->language->id);
|
||||
|
||||
if (!$posts) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($posts as $post) {
|
||||
if (isset($post['cover']) && !empty($post['cover'])) {
|
||||
$tmp_location = _PS_TMP_IMG_DIR_.'ph_simpleblog_'.$post['id_simpleblog_post'].'.'.$post['cover'];
|
||||
if (file_exists($tmp_location)) {
|
||||
@unlink($tmp_location);
|
||||
}
|
||||
|
||||
$orig_location = _PS_MODULE_DIR_.'ph_simpleblog/covers/'.$post['id_simpleblog_post'].'.'.$post['cover'];
|
||||
$thumbLoc = _PS_MODULE_DIR_.'ph_simpleblog/covers/'.$post['id_simpleblog_post'].'-thumb.'.$post['cover'];
|
||||
$thumbWideLoc = _PS_MODULE_DIR_.'ph_simpleblog/covers/'.$post['id_simpleblog_post'].'-wide.'.$post['cover'];
|
||||
|
||||
if (file_exists($thumbLoc)) {
|
||||
@unlink($thumbLoc);
|
||||
}
|
||||
|
||||
if (file_exists($thumbWideLoc)) {
|
||||
@unlink($thumbWideLoc);
|
||||
}
|
||||
|
||||
$thumbX = Configuration::get('PH_BLOG_THUMB_X');
|
||||
$thumbY = Configuration::get('PH_BLOG_THUMB_Y');
|
||||
|
||||
$thumb_wide_X = Configuration::get('PH_BLOG_THUMB_X_WIDE');
|
||||
$thumb_wide_Y = Configuration::get('PH_BLOG_THUMB_Y_WIDE');
|
||||
|
||||
$thumbMethod = Configuration::get('PH_BLOG_THUMB_METHOD');
|
||||
|
||||
try {
|
||||
$thumb = PhpThumbFactory::create($orig_location);
|
||||
$thumbWide = PhpThumbFactory::create($orig_location);
|
||||
} catch (Exception $e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($thumbMethod == '1') {
|
||||
$thumb->adaptiveResize($thumbX, $thumbY);
|
||||
$thumbWide->adaptiveResize($thumb_wide_X, $thumb_wide_Y);
|
||||
} elseif ($thumbMethod == '2') {
|
||||
$thumb->cropFromCenter($thumbX, $thumbY);
|
||||
$thumbWide->cropFromCenter($thumb_wide_X, $thumb_wide_Y);
|
||||
}
|
||||
|
||||
$thumb->save($thumbLoc);
|
||||
$thumbWide->save($thumbWideLoc);
|
||||
|
||||
unset($thumb);
|
||||
unset($thumbWide);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function changeRating($way = 'up', $id_simpleblog_post = false)
|
||||
{
|
||||
if ($way == 'up') {
|
||||
$sql = 'UPDATE `'._DB_PREFIX_.'simpleblog_post` SET `likes` = `likes` + 1 WHERE id_simpleblog_post = '.$id_simpleblog_post;
|
||||
} elseif ($way == 'down') {
|
||||
$sql = 'UPDATE `'._DB_PREFIX_.'simpleblog_post` SET `likes` = `likes` - 1 WHERE id_simpleblog_post = '.$id_simpleblog_post;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
$result = Db::getInstance()->Execute($sql);
|
||||
|
||||
$sql = 'SELECT `likes` FROM `'._DB_PREFIX_.'simpleblog_post` WHERE id_simpleblog_post = '.$id_simpleblog_post;
|
||||
|
||||
$res = Db::getInstance()->ExecuteS($sql);
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function increaseViewsNb()
|
||||
{
|
||||
$sql = 'UPDATE `'._DB_PREFIX_.'simpleblog_post` SET `views` = `views` + 1 WHERE id_simpleblog_post = '.$this->id_simpleblog_post;
|
||||
|
||||
$result = Db::getInstance()->Execute($sql);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function isAccessGranted()
|
||||
{
|
||||
if ($userGroups = Context::getContext()->customer->getGroups()) {
|
||||
if (!isset($this->id_simpleblog_post)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$tmpLinkGroups = unserialize($this->access);
|
||||
$linkGroups = array();
|
||||
|
||||
foreach ($tmpLinkGroups as $groupID => $status) {
|
||||
if ($status) {
|
||||
$linkGroups[] = $groupID;
|
||||
}
|
||||
}
|
||||
|
||||
$intersect = array_intersect($userGroups, $linkGroups);
|
||||
if (count($intersect)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getRelatedProducts($ids)
|
||||
{
|
||||
if (!$ids) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$context = Context::getContext();
|
||||
$id_lang = $context->language->id;
|
||||
|
||||
$front = true;
|
||||
if (!in_array($context->controller->controller_type, array('front', 'modulefront'))) {
|
||||
$front = false;
|
||||
}
|
||||
|
||||
$groups = FrontController::getCurrentCustomerGroups();
|
||||
$sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
|
||||
|
||||
$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, pl.`description`, pl.`description_short`, MAX(product_attribute_shop.id_product_attribute) id_product_attribute,
|
||||
pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`,
|
||||
pl.`name`, MAX(image_shop.`id_image`) id_image, il.`legend`, m.`name` AS manufacturer_name,
|
||||
DATEDIFF(
|
||||
p.`date_add`,
|
||||
DATE_SUB(
|
||||
NOW(),
|
||||
INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY
|
||||
)
|
||||
) > 0 AS new
|
||||
FROM `'._DB_PREFIX_.'product` p
|
||||
'.Shop::addSqlAssociation('product', 'p').'
|
||||
LEFT JOIN '._DB_PREFIX_.'product_attribute pa ON (pa.id_product = p.id_product)
|
||||
'.Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.default_on=1').'
|
||||
'.Product::sqlStock('p', 0, false, $context->shop).'
|
||||
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (
|
||||
p.`id_product` = pl.`id_product`
|
||||
AND pl.`id_lang` = '.(int) $id_lang.Shop::addSqlRestrictionOnLang('pl').'
|
||||
)
|
||||
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product`)'.
|
||||
Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
|
||||
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int) $id_lang.')
|
||||
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
|
||||
WHERE product_shop.`active` = 1
|
||||
'.($front ? ' AND p.`visibility` IN ("both", "catalog")' : '').'
|
||||
AND p.`id_product` IN ('.$ids.')
|
||||
AND p.`id_product` IN (
|
||||
SELECT cp.`id_product`
|
||||
FROM `'._DB_PREFIX_.'category_group` cg
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
|
||||
WHERE cg.`id_group` '.$sql_groups.'
|
||||
)
|
||||
GROUP BY product_shop.id_product
|
||||
ORDER BY `pl`.name ASC';
|
||||
|
||||
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
|
||||
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Product::getProductsProperties($id_lang, $result);
|
||||
}
|
||||
|
||||
public static function checkIfAllowComments($flag)
|
||||
{
|
||||
$allow_comments = false;
|
||||
|
||||
switch ($flag) {
|
||||
case 1:
|
||||
$allow_comments = true;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
$allow_comments = false;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
$allow_comments = Configuration::get('PH_BLOG_COMMENT_ALLOW');
|
||||
break;
|
||||
|
||||
default:
|
||||
$allow_comments = false;
|
||||
}
|
||||
|
||||
return $allow_comments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets previous post if available
|
||||
* @return array previous blog post
|
||||
*/
|
||||
public function getPreviousPost()
|
||||
{
|
||||
$finder = new BlogPostsFinder;
|
||||
$finder->setLimit(1);
|
||||
$finder->setCustomWhere($finder->getTablePrefix().'date_add < "'.$this->date_add.'"');
|
||||
$post = $finder->findPosts();
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets next post if available
|
||||
* @return array next blog post
|
||||
*/
|
||||
public function getNextPost()
|
||||
{
|
||||
$finder = new BlogPostsFinder;
|
||||
$finder->setLimit(1);
|
||||
$finder->setOrderWay('ASC');
|
||||
$finder->setCustomWhere($finder->getTablePrefix().'date_add > "'.$this->date_add.'"');
|
||||
$post = $finder->findPosts();
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if post allows to send comments.
|
||||
*
|
||||
* @return bool allowing comments status
|
||||
*/
|
||||
public function allowComments()
|
||||
{
|
||||
return self::checkIfAllowComments($this->allow_comments);
|
||||
}
|
||||
|
||||
public function getPostCategory()
|
||||
{
|
||||
return new SimpleBlogCategory((int) $this->id_simpleblog_category, (int) Context::getContext()->language->id);
|
||||
}
|
||||
|
||||
public function getPostType()
|
||||
{
|
||||
return new SimpleBlogPostType((int) $this->id_simpleblog_post_type, (int) Context::getContext()->language->id);
|
||||
}
|
||||
}
|
||||
124
modules/ph_simpleblog/models/SimpleBlogPostAuthor.php
Normal file
124
modules/ph_simpleblog/models/SimpleBlogPostAuthor.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/**
|
||||
* Blog for PrestaShop module by Krystian Podemski from PrestaHome.
|
||||
*
|
||||
* @author Krystian Podemski <krystian@prestahome.com>
|
||||
* @copyright Copyright (c) 2008-2019 Krystian Podemski - www.PrestaHome.com / www.Podemski.info
|
||||
* @license You only can use module, nothing more!
|
||||
*/
|
||||
require_once _PS_MODULE_DIR_.'ph_simpleblog/ph_simpleblog.php';
|
||||
|
||||
class SimpleBlogPostAuthor extends ObjectModel
|
||||
{
|
||||
public $id_simpleblog_author;
|
||||
public $firstname;
|
||||
public $lastname;
|
||||
public $photo;
|
||||
public $email;
|
||||
public $facebook;
|
||||
public $google;
|
||||
public $linkedin;
|
||||
public $twitter;
|
||||
public $instagram;
|
||||
public $phone;
|
||||
public $www;
|
||||
public $active = 1;
|
||||
|
||||
public $bio;
|
||||
public $additional_info;
|
||||
|
||||
public $posts;
|
||||
|
||||
public static $definition = array(
|
||||
'table' => 'simpleblog_author',
|
||||
'primary' => 'id_simpleblog_author',
|
||||
'multilang' => true,
|
||||
'fields' => array(
|
||||
'firstname' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isGenericName',
|
||||
'size' => 255
|
||||
),
|
||||
'lastname' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isGenericName',
|
||||
'size' => 255
|
||||
),
|
||||
'photo' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isAnything',
|
||||
'size' => 9999
|
||||
),
|
||||
'email' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isEmail',
|
||||
'size' => 140
|
||||
),
|
||||
'facebook' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isAnything'
|
||||
),
|
||||
'google' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isAnything'
|
||||
),
|
||||
'linkedin' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isAnything'
|
||||
),
|
||||
'twitter' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isAnything'
|
||||
),
|
||||
'instagram' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isAnything'
|
||||
),
|
||||
'phone' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isPhoneNumber'
|
||||
),
|
||||
'www' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isUrl'
|
||||
),
|
||||
'active' => array(
|
||||
'type' => self::TYPE_BOOL,
|
||||
'validate' => 'isBool'
|
||||
),
|
||||
'bio' => array(
|
||||
'type' => self::TYPE_HTML,
|
||||
'validate' => 'isCleanHtml',
|
||||
'lang' => true,
|
||||
),
|
||||
'additional_info' => array(
|
||||
'type' => self::TYPE_HTML,
|
||||
'validate' => 'isCleanHtml',
|
||||
'lang' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
public function __construct($id_simpleblog_author = null, $id_lang = null, $id_shop = null)
|
||||
{
|
||||
parent::__construct($id_simpleblog_author, $id_lang, $id_shop);
|
||||
|
||||
if ($this->id_simpleblog_author && !isset(Context::getContext()->employee)) {
|
||||
$finder = new BlogPostsFinder;
|
||||
$finder->setAuthor($this->id_simpleblog_author);
|
||||
$this->posts = $finder->findPosts();
|
||||
}
|
||||
}
|
||||
|
||||
public static function getAll()
|
||||
{
|
||||
$sql = new DbQuery();
|
||||
$sql->select('CONCAT (sba.`firstname`, " ", sba.`lastname`) as name, sba.*, sbal.bio, sbal. additional_info');
|
||||
$sql->from('simpleblog_author', 'sba');
|
||||
$sql->leftJoin('simpleblog_author_lang', 'sbal', 'sba.`id_simpleblog_author` = sbal.`id_simpleblog_author` AND sbal.`id_lang` = '.Context::getContext()->language->id);
|
||||
$sql->where('sba.active = 1');
|
||||
$sql->orderBy('id_simpleblog_author ASC');
|
||||
|
||||
return Db::getInstance()->executeS($sql);
|
||||
}
|
||||
}
|
||||
126
modules/ph_simpleblog/models/SimpleBlogPostImage.php
Normal file
126
modules/ph_simpleblog/models/SimpleBlogPostImage.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* Blog for PrestaShop module by Krystian Podemski from PrestaHome.
|
||||
*
|
||||
* @author Krystian Podemski <krystian@prestahome.com>
|
||||
* @copyright Copyright (c) 2014-2017 Krystian Podemski - www.PrestaHome.com / www.Podemski.info
|
||||
* @license You only can use module, nothing more!
|
||||
*/
|
||||
require_once _PS_MODULE_DIR_.'ph_simpleblog/ph_simpleblog.php';
|
||||
|
||||
class SimpleBlogPostImage extends ObjectModel
|
||||
{
|
||||
public $id;
|
||||
public $id_simpleblog_post_image;
|
||||
public $id_simpleblog_post;
|
||||
public $position;
|
||||
public $image;
|
||||
|
||||
public static $definition = array(
|
||||
'table' => 'simpleblog_post_image',
|
||||
'primary' => 'id_simpleblog_post_image',
|
||||
'multilang' => false,
|
||||
'fields' => array(
|
||||
'id_simpleblog_post' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'required' => true,
|
||||
'validate' => 'isUnsignedInt'
|
||||
),
|
||||
'position' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'required' => true,
|
||||
'validate' => 'isUnsignedInt'
|
||||
),
|
||||
'image' => array(
|
||||
'type' => self::TYPE_STRING
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
public function __construct($id_simpleblog_post_image = null, $id_lang = null, $id_shop = null)
|
||||
{
|
||||
parent::__construct($id_simpleblog_post_image, $id_lang, $id_shop);
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
if (!parent::delete()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->deletePostImage()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->cleanPositions($this->id_simpleblog_post)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function getAll()
|
||||
{
|
||||
$sql = new DbQuery();
|
||||
$sql->select('*');
|
||||
$sql->from('simpleblog_post_image', 'sbpi');
|
||||
$sql->orderBy('position ASC');
|
||||
|
||||
return Db::getInstance()->executeS($sql);
|
||||
}
|
||||
|
||||
public static function getAllById($id_simpleblog_post)
|
||||
{
|
||||
if (!Validate::isUnsignedInt($id_simpleblog_post)) {
|
||||
die('getAllById - invalid ID');
|
||||
}
|
||||
|
||||
$sql = new DbQuery();
|
||||
$sql->select('*');
|
||||
$sql->from('simpleblog_post_image', 'sbpi');
|
||||
$sql->where('id_simpleblog_post = '.(int) $id_simpleblog_post);
|
||||
$sql->orderBy('position ASC');
|
||||
|
||||
return Db::getInstance()->executeS($sql);
|
||||
}
|
||||
|
||||
public static function getNewLastPosition($id_simpleblog_post)
|
||||
{
|
||||
return Db::getInstance()->getValue('
|
||||
SELECT IFNULL(MAX(position),0)+1
|
||||
FROM `'._DB_PREFIX_.'simpleblog_post_image`
|
||||
WHERE `id_simpleblog_post` = '.(int) $id_simpleblog_post);
|
||||
}
|
||||
|
||||
public function cleanPositions($id_simpleblog_post)
|
||||
{
|
||||
$result = Db::getInstance()->executeS('
|
||||
SELECT `id_simpleblog_post_image`
|
||||
FROM `'._DB_PREFIX_.'simpleblog_post_image`
|
||||
WHERE `id_simpleblog_post` = '.(int) $id_simpleblog_post.'
|
||||
ORDER BY `position`
|
||||
');
|
||||
$sizeof = count($result);
|
||||
for ($i = 0; $i < $sizeof; ++$i) {
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'simpleblog_post_image`
|
||||
SET `position` = '.($i + 1).'
|
||||
WHERE `id_simpleblog_post_image` = '.(int) $result[$i]['id_simpleblog_post_image']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function deletePostImage()
|
||||
{
|
||||
$response = true;
|
||||
|
||||
$images = glob(_SIMPLEBLOG_GALLERY_DIR_.(int) $this->id.'-'.(int) $this->id_simpleblog_post.'-*');
|
||||
|
||||
foreach ($images as $image) {
|
||||
$response &= @unlink($image);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
72
modules/ph_simpleblog/models/SimpleBlogPostType.php
Normal file
72
modules/ph_simpleblog/models/SimpleBlogPostType.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* Blog for PrestaShop module by Krystian Podemski from PrestaHome.
|
||||
*
|
||||
* @author Krystian Podemski <krystian@prestahome.com>
|
||||
* @copyright Copyright (c) 2014-2017 Krystian Podemski - www.PrestaHome.com / www.Podemski.info
|
||||
* @license You only can use module, nothing more!
|
||||
*/
|
||||
require_once _PS_MODULE_DIR_ . 'ph_simpleblog/ph_simpleblog.php';
|
||||
|
||||
class SimpleBlogPostType extends ObjectModel
|
||||
{
|
||||
public $id;
|
||||
public $id_simpleblog_post_type;
|
||||
public $name;
|
||||
public $slug;
|
||||
public $description;
|
||||
|
||||
public static $definition = array(
|
||||
'table' => 'simpleblog_post_type',
|
||||
'primary' => 'id_simpleblog_post_type',
|
||||
'multilang' => false,
|
||||
'fields' => array(
|
||||
'name' => array('type' => self::TYPE_STRING),
|
||||
'slug' => array('type' => self::TYPE_STRING),
|
||||
'description' => array('type' => self::TYPE_STRING),
|
||||
),
|
||||
);
|
||||
|
||||
public function __construct($id_simpleblog_post_type = null, $id_lang = null, $id_shop = null)
|
||||
{
|
||||
parent::__construct($id_simpleblog_post_type, $id_lang, $id_shop);
|
||||
}
|
||||
|
||||
public static function getAll()
|
||||
{
|
||||
$sql = new DbQuery();
|
||||
$sql->select('id_simpleblog_post_type, name');
|
||||
$sql->from('simpleblog_post_type', 'sbpt');
|
||||
$sql->orderBy('id_simpleblog_post_type ASC');
|
||||
|
||||
return Db::getInstance()->executeS($sql);
|
||||
}
|
||||
|
||||
public static function getSlugById($id_simpleblog_post_type)
|
||||
{
|
||||
if (!Validate::isUnsignedInt($id_simpleblog_post_type)) {
|
||||
die('getSlugByID - invalid ID');
|
||||
}
|
||||
|
||||
$sql = new DbQuery();
|
||||
$sql->select('slug');
|
||||
$sql->from('simpleblog_post_type', 'sbpt');
|
||||
$sql->where('id_simpleblog_post_type = '.(int) $id_simpleblog_post_type);
|
||||
|
||||
return Db::getInstance()->getValue($sql);
|
||||
}
|
||||
|
||||
public static function getIdBySlug($slug)
|
||||
{
|
||||
if (!Validate::isLinkRewrite($slug)) {
|
||||
die('getIdBySlug - invalid slug');
|
||||
}
|
||||
|
||||
$sql = new DbQuery();
|
||||
$sql->select('id_simpleblog_post_type');
|
||||
$sql->from('simpleblog_post_type', 'sbpt');
|
||||
$sql->where('slug = \''.$slug.'\'');
|
||||
|
||||
return Db::getInstance()->getValue($sql);
|
||||
}
|
||||
}
|
||||
216
modules/ph_simpleblog/models/SimpleBlogTag.php
Normal file
216
modules/ph_simpleblog/models/SimpleBlogTag.php
Normal file
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
/**
|
||||
* Blog for PrestaShop module by Krystian Podemski from PrestaHome.
|
||||
*
|
||||
* @author Krystian Podemski <krystian@prestahome.com>
|
||||
* @copyright Copyright (c) 2014-2017 Krystian Podemski - www.PrestaHome.com / www.Podemski.info
|
||||
* @license You only can use module, nothing more!
|
||||
*/
|
||||
class SimpleBlogTag extends ObjectModel
|
||||
{
|
||||
/** @var int Language id */
|
||||
public $id_lang;
|
||||
|
||||
/** @var string Name */
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @see ObjectModel::$definition
|
||||
*/
|
||||
public static $definition = array(
|
||||
'table' => 'simpleblog_tag',
|
||||
'primary' => 'id_simpleblog_tag',
|
||||
'fields' => array(
|
||||
'id_lang' => array(
|
||||
'type' => self::TYPE_INT,
|
||||
'validate' => 'isUnsignedId',
|
||||
'required' => true
|
||||
),
|
||||
'name' => array(
|
||||
'type' => self::TYPE_STRING,
|
||||
'validate' => 'isGenericName',
|
||||
'required' => true,
|
||||
'size' => 32
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
public function __construct($id = null, $name = null, $id_lang = null)
|
||||
{
|
||||
$this->def = Tag::getDefinition($this);
|
||||
$this->setDefinitionRetrocompatibility();
|
||||
|
||||
if ($id) {
|
||||
parent::__construct($id);
|
||||
} elseif ($name && Validate::isGenericName($name) && $id_lang && Validate::isUnsignedId($id_lang)) {
|
||||
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
|
||||
SELECT *
|
||||
FROM `'._DB_PREFIX_.'simpleblog_tag` t
|
||||
WHERE `name` LIKE \''.pSQL($name).'\' AND `id_lang` = '.(int) $id_lang);
|
||||
|
||||
if ($row) {
|
||||
$this->id = (int) $row['id_simpleblog_tag'];
|
||||
$this->id_lang = (int) $row['id_lang'];
|
||||
$this->name = $row['name'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function add($autodate = true, $null_values = false)
|
||||
{
|
||||
if (!parent::add($autodate, $null_values)) {
|
||||
return false;
|
||||
} elseif (isset($_POST['posts'])) {
|
||||
return $this->setPosts(Tools::getValue('posts'));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add several tags in database and link it to a product.
|
||||
*
|
||||
* @param int $id_lang Language id
|
||||
* @param int $id_simpleblog_post Post id to link tags with
|
||||
* @param string|array $tag_list List of tags, as array or as a string with comas
|
||||
*
|
||||
* @return bool Operation success
|
||||
*/
|
||||
public static function addTags($id_lang, $id_simpleblog_post, $tag_list, $separator = ',')
|
||||
{
|
||||
if (!Validate::isUnsignedId($id_lang)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_array($tag_list)) {
|
||||
$tag_list = array_filter(array_unique(array_map('trim', preg_split('#\\'.$separator.'#', $tag_list, null, PREG_SPLIT_NO_EMPTY))));
|
||||
}
|
||||
|
||||
$list = array();
|
||||
if (is_array($tag_list)) {
|
||||
foreach ($tag_list as $tag) {
|
||||
if (!Validate::isGenericName($tag)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$tag_obj = new self(null, $tag, (int) $id_lang);
|
||||
|
||||
/* Tag does not exist in database */
|
||||
if (!Validate::isLoadedObject($tag_obj)) {
|
||||
$tag_obj->name = $tag;
|
||||
$tag_obj->id_lang = (int) $id_lang;
|
||||
$tag_obj->add();
|
||||
}
|
||||
if (!in_array($tag_obj->id, $list)) {
|
||||
$list[] = $tag_obj->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
$data = '';
|
||||
foreach ($list as $tag) {
|
||||
$data .= '('.(int) $tag.','.(int) $id_simpleblog_post.'),';
|
||||
}
|
||||
$data = rtrim($data, ',');
|
||||
|
||||
$sql = 'INSERT INTO `'._DB_PREFIX_.'simpleblog_post_tag` (`id_simpleblog_tag`, `id_simpleblog_post`) VALUES '.$data;
|
||||
|
||||
return Db::getInstance()->execute($sql);
|
||||
}
|
||||
|
||||
public static function getMainTags($id_lang, $nb = 10)
|
||||
{
|
||||
$groups = FrontController::getCurrentCustomerGroups();
|
||||
$sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
|
||||
|
||||
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
|
||||
SELECT t.name, COUNT(pt.id_tag) AS times
|
||||
FROM `'._DB_PREFIX_.'simpleblog_post_tag` pt
|
||||
LEFT JOIN `'._DB_PREFIX_.'tag` t ON (t.id_tag = pt.id_tag)
|
||||
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.id_simpleblog_post = pt.id_simpleblog_post)
|
||||
'.Shop::addSqlAssociation('product', 'p').'
|
||||
WHERE t.`id_lang` = '.(int) $id_lang.'
|
||||
AND product_shop.`active` = 1
|
||||
AND product_shop.`id_simpleblog_post` IN (
|
||||
SELECT cp.`id_simpleblog_post`
|
||||
FROM `'._DB_PREFIX_.'category_group` cg
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
|
||||
WHERE cg.`id_group` '.$sql_groups.'
|
||||
)
|
||||
GROUP BY t.id_tag
|
||||
ORDER BY times DESC
|
||||
LIMIT 0, '.(int) $nb);
|
||||
}
|
||||
|
||||
public static function getPostTags($id_simpleblog_post)
|
||||
{
|
||||
if (!$tmp = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
|
||||
SELECT t.`id_lang`, t.`name`
|
||||
FROM '._DB_PREFIX_.'simpleblog_tag t
|
||||
LEFT JOIN '._DB_PREFIX_.'simpleblog_post_tag pt ON (pt.id_simpleblog_tag = t.id_simpleblog_tag)
|
||||
WHERE pt.`id_simpleblog_post`='.(int) $id_simpleblog_post)) {
|
||||
return array();
|
||||
}
|
||||
$result = array();
|
||||
foreach ($tmp as $tag) {
|
||||
$result[$tag['id_lang']][] = $tag['name'];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getPosts($associated = true, Context $context = null)
|
||||
{
|
||||
if (!$context) {
|
||||
$context = Context::getContext();
|
||||
}
|
||||
$id_lang = $this->id_lang ? $this->id_lang : $context->language->id;
|
||||
|
||||
if (!$this->id && $associated) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$in = $associated ? 'IN' : 'NOT IN';
|
||||
|
||||
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
|
||||
SELECT pl.title, pl.id_simpleblog_post
|
||||
FROM `'._DB_PREFIX_.'simpleblog_post` p
|
||||
LEFT JOIN `'._DB_PREFIX_.'simpleblog_post_lang` pl ON p.id_simpleblog_post = pl.id_simpleblog_post
|
||||
WHERE pl.id_lang = '.(int) $id_lang.'
|
||||
'.($this->id ? ('AND p.id_simpleblog_post '.$in.' (SELECT pt.id_simpleblog_post FROM `'._DB_PREFIX_.'simpleblog_post_tag` pt WHERE pt.id_simpleblog_tag = '.(int) $this->id.')') : '').'
|
||||
ORDER BY pl.title');
|
||||
}
|
||||
|
||||
public function setPosts($array)
|
||||
{
|
||||
$result = Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'simpleblog_post_tag WHERE id_simpleblog_tag = '.(int) $this->id);
|
||||
if (is_array($array)) {
|
||||
$array = array_map('intval', $array);
|
||||
$ids = array();
|
||||
foreach ($array as $id_simpleblog_post) {
|
||||
$ids[] = '('.(int) $id_simpleblog_post.','.(int) $this->id.')';
|
||||
}
|
||||
|
||||
if ($result) {
|
||||
$result &= Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'simpleblog_post_tag (id_simpleblog_post, id_simpleblog_tag) VALUES '.implode(',', $ids));
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function deleteTagsForPost($id_simpleblog_post)
|
||||
{
|
||||
return Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'simpleblog_post_tag` WHERE `id_simpleblog_post` = '.(int) $id_simpleblog_post);
|
||||
}
|
||||
|
||||
public static function getLink($tag, $id_lang = null, $id_shop = null)
|
||||
{
|
||||
$url = ph_simpleblog::myRealUrl();
|
||||
|
||||
$dispatcher = Dispatcher::getInstance();
|
||||
$params = array();
|
||||
$params['tag'] = $tag;
|
||||
|
||||
return $url.$dispatcher->createUrl('ph_simpleblog_tag', $id_lang, $params);
|
||||
}
|
||||
}
|
||||
35
modules/ph_simpleblog/models/index.php
Normal file
35
modules/ph_simpleblog/models/index.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2014 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License (AFL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/afl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2014 PrestaShop SA
|
||||
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
Reference in New Issue
Block a user