Add Project-Pro Blog module with initial SQL setup, CSS styles, and template files
- Created SQL installation scripts for categories and posts tables. - Added uninstall scripts to drop the created tables. - Introduced CSS styles for blog layout, including responsive design for posts and categories. - Implemented PHP redirection in index files to prevent direct access. - Developed Smarty templates for blog category tree, post list, and individual post details. - Ensured proper caching headers in PHP files to enhance performance.
This commit is contained in:
8
modules/projectproblog/controllers/front/index.php
Normal file
8
modules/projectproblog/controllers/front/index.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
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;
|
||||
252
modules/projectproblog/controllers/front/list.php
Normal file
252
modules/projectproblog/controllers/front/list.php
Normal file
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
/**
|
||||
* Project-Pro Blog — Front controller: lista wpisów
|
||||
*
|
||||
* @author Project-Pro <https://www.project-pro.pl>
|
||||
* @copyright 2024 Project-Pro
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
require_once _PS_MODULE_DIR_ . 'projectproblog/classes/BlogCategory.php';
|
||||
require_once _PS_MODULE_DIR_ . 'projectproblog/classes/BlogPost.php';
|
||||
|
||||
class ProjectproblogListModuleFrontController extends ModuleFrontController
|
||||
{
|
||||
public $php_self = 'list';
|
||||
|
||||
/** @var BlogCategory|null */
|
||||
protected $currentCategory = null;
|
||||
|
||||
/** @var string */
|
||||
protected $slug = '';
|
||||
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
|
||||
$idLang = (int) $this->context->language->id;
|
||||
$this->slug = Tools::getValue('slug', '');
|
||||
|
||||
// Jeśli podano slug kategorii — rozwiąż go
|
||||
if ($this->slug !== '') {
|
||||
$this->currentCategory = BlogCategory::getByLinkRewrite($this->slug, $idLang);
|
||||
if (!$this->currentCategory) {
|
||||
Tools::redirect('index.php?controller=404');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function initContent()
|
||||
{
|
||||
parent::initContent();
|
||||
|
||||
$this->registerStylesheet(
|
||||
'module-projectproblog-blog',
|
||||
'modules/projectproblog/views/css/blog.css',
|
||||
['media' => 'all', 'priority' => 200]
|
||||
);
|
||||
|
||||
$idLang = (int) $this->context->language->id;
|
||||
$page = max(1, (int) Tools::getValue('page', 1));
|
||||
$perPage = Projectproblog::POSTS_PER_PAGE;
|
||||
$idCategory = $this->currentCategory ? (int) $this->currentCategory->id : null;
|
||||
|
||||
// Wpisy
|
||||
$rawPosts = BlogPost::getList($idLang, $page, $perPage, $idCategory);
|
||||
$total = BlogPost::getCount($idLang, $idCategory);
|
||||
$pages = max(1, (int) ceil($total / $perPage));
|
||||
|
||||
// Wzbogac wpisy o URL-e
|
||||
$posts = $this->enrichPosts($rawPosts, $idLang);
|
||||
|
||||
// Paginacja
|
||||
$pagination = $this->buildPagination($page, $pages, $this->slug, $idLang);
|
||||
|
||||
// Drzewo kategorii z URL-ami
|
||||
$categoryTree = $this->enrichCategoryTree(
|
||||
BlogCategory::getCategoryTree($idLang),
|
||||
$idLang
|
||||
);
|
||||
|
||||
// Meta strony
|
||||
$this->setPageMeta($idLang);
|
||||
|
||||
$this->context->smarty->assign([
|
||||
'blog_posts' => $posts,
|
||||
'blog_category_tree' => $categoryTree,
|
||||
'blog_current_cat' => $this->currentCategory,
|
||||
'blog_pagination' => $pagination,
|
||||
'blog_page' => $page,
|
||||
'blog_pages_count' => $pages,
|
||||
'blog_total' => $total,
|
||||
'blog_url' => Projectproblog::getBlogUrl($idLang),
|
||||
]);
|
||||
|
||||
$this->setTemplate('module:projectproblog/views/templates/front/list.tpl');
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Breadcrumb */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
public function getBreadcrumbLinks()
|
||||
{
|
||||
$breadcrumb = parent::getBreadcrumbLinks();
|
||||
|
||||
$breadcrumb['links'][] = [
|
||||
'title' => $this->l('Blog'),
|
||||
'url' => Projectproblog::getBlogUrl(),
|
||||
];
|
||||
|
||||
if ($this->currentCategory) {
|
||||
$breadcrumb['links'][] = [
|
||||
'title' => $this->currentCategory->name,
|
||||
'url' => Projectproblog::getCategoryUrl($this->currentCategory->link_rewrite),
|
||||
];
|
||||
}
|
||||
|
||||
return $breadcrumb;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Canonical URL */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* Wyłącza canonical redirection — PS generuje błędny fallback URL
|
||||
* (bez fc=module) gdy hookModuleRoutes nie załadował tras w Dispatcher.
|
||||
*/
|
||||
protected function canonicalRedirection($canonical_url = '')
|
||||
{
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Helpery prywatne */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* Dodaje thumbnail_url i url do każdego wpisu.
|
||||
*/
|
||||
protected function enrichPosts(array $posts, $idLang)
|
||||
{
|
||||
$base = $this->context->link->getBaseLink();
|
||||
|
||||
foreach ($posts as &$post) {
|
||||
$post['thumbnail_url'] = $post['thumbnail']
|
||||
? $base . 'modules/projectproblog/views/img/posts/' . $post['thumbnail']
|
||||
: null;
|
||||
|
||||
$post['url'] = Projectproblog::getPostUrl($post['link_rewrite'], $idLang);
|
||||
}
|
||||
unset($post);
|
||||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rekurencyjnie dodaje url do węzłów drzewa kategorii.
|
||||
*/
|
||||
protected function enrichCategoryTree(array $tree, $idLang)
|
||||
{
|
||||
foreach ($tree as &$node) {
|
||||
$node['category']['url'] = Projectproblog::getCategoryUrl(
|
||||
$node['category']['link_rewrite'],
|
||||
$idLang
|
||||
);
|
||||
$node['children'] = $this->enrichCategoryTree($node['children'], $idLang);
|
||||
}
|
||||
unset($node);
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* Buduje tablicę linków paginacji.
|
||||
* Każdy element: ['page' => int, 'url' => string, 'current' => bool]
|
||||
*/
|
||||
protected function buildPagination($currentPage, $pagesCount, $slug, $idLang)
|
||||
{
|
||||
if ($pagesCount <= 1) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$links = [];
|
||||
|
||||
for ($i = 1; $i <= $pagesCount; $i++) {
|
||||
$params = [];
|
||||
if ($slug !== '') {
|
||||
$params['slug'] = $slug;
|
||||
}
|
||||
if ($i > 1) {
|
||||
$params['page'] = $i;
|
||||
}
|
||||
|
||||
$links[] = [
|
||||
'page' => $i,
|
||||
'url' => $this->context->link->getModuleLink(
|
||||
'projectproblog',
|
||||
'list',
|
||||
$params,
|
||||
true,
|
||||
$idLang
|
||||
),
|
||||
'current' => ($i === $currentPage),
|
||||
];
|
||||
}
|
||||
|
||||
// Linki poprzednia/następna
|
||||
$prev = null;
|
||||
$next = null;
|
||||
|
||||
if ($currentPage > 1) {
|
||||
$params = $slug !== '' ? ['slug' => $slug] : [];
|
||||
if ($currentPage - 1 > 1) {
|
||||
$params['page'] = $currentPage - 1;
|
||||
}
|
||||
$prev = $this->context->link->getModuleLink('projectproblog', 'list', $params, true, $idLang);
|
||||
}
|
||||
|
||||
if ($currentPage < $pagesCount) {
|
||||
$params = $slug !== '' ? ['slug' => $slug] : [];
|
||||
$params['page'] = $currentPage + 1;
|
||||
$next = $this->context->link->getModuleLink('projectproblog', 'list', $params, true, $idLang);
|
||||
}
|
||||
|
||||
return [
|
||||
'pages' => $links,
|
||||
'prev' => $prev,
|
||||
'next' => $next,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ustawia meta title/description strony listy.
|
||||
*/
|
||||
protected function setPageMeta($idLang)
|
||||
{
|
||||
if ($this->currentCategory) {
|
||||
$title = $this->currentCategory->meta_title ?: $this->currentCategory->name . ' — Blog';
|
||||
$desc = $this->currentCategory->meta_description ?: '';
|
||||
$kw = $this->currentCategory->meta_keywords ?: '';
|
||||
} else {
|
||||
$title = $this->l('Blog');
|
||||
$desc = '';
|
||||
$kw = '';
|
||||
}
|
||||
|
||||
$page = $this->context->smarty->getTemplateVars('page') ?: [];
|
||||
|
||||
if (!isset($page['meta'])) {
|
||||
$page['meta'] = [];
|
||||
}
|
||||
|
||||
$page['meta']['title'] = $title;
|
||||
$page['meta']['description'] = $desc;
|
||||
$page['meta']['keywords'] = $kw;
|
||||
|
||||
$this->context->smarty->assign('page', $page);
|
||||
}
|
||||
}
|
||||
225
modules/projectproblog/controllers/front/post.php
Normal file
225
modules/projectproblog/controllers/front/post.php
Normal file
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
/**
|
||||
* Project-Pro Blog — Front controller: szczegół wpisu
|
||||
*
|
||||
* @author Project-Pro <https://www.project-pro.pl>
|
||||
* @copyright 2024 Project-Pro
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
require_once _PS_MODULE_DIR_ . 'projectproblog/classes/BlogCategory.php';
|
||||
require_once _PS_MODULE_DIR_ . 'projectproblog/classes/BlogPost.php';
|
||||
|
||||
class ProjectproblogPostModuleFrontController extends ModuleFrontController
|
||||
{
|
||||
public $php_self = 'post';
|
||||
|
||||
/** @var BlogPost|null */
|
||||
protected $post = null;
|
||||
|
||||
/** @var string */
|
||||
protected $slug = '';
|
||||
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
|
||||
$idLang = (int) $this->context->language->id;
|
||||
$this->slug = Tools::getValue('slug', '');
|
||||
|
||||
if ($this->slug === '') {
|
||||
Tools::redirect('index.php?controller=404');
|
||||
}
|
||||
|
||||
$this->post = BlogPost::getByLinkRewrite($this->slug, $idLang);
|
||||
|
||||
if (!$this->post) {
|
||||
Tools::redirect('index.php?controller=404');
|
||||
}
|
||||
}
|
||||
|
||||
public function initContent()
|
||||
{
|
||||
parent::initContent();
|
||||
|
||||
$this->registerStylesheet(
|
||||
'module-projectproblog-blog',
|
||||
'modules/projectproblog/views/css/blog.css',
|
||||
['media' => 'all', 'priority' => 200]
|
||||
);
|
||||
|
||||
$idLang = (int) $this->context->language->id;
|
||||
|
||||
// Kategorie wpisu z URL-ami
|
||||
$categoryIds = BlogPost::getCategories((int) $this->post->id);
|
||||
$postCats = $this->loadCategories($categoryIds, $idLang);
|
||||
|
||||
// Pierwsza kategoria (do breadcrumba)
|
||||
$primaryCat = !empty($postCats) ? $postCats[0] : null;
|
||||
|
||||
// URL miniaturki
|
||||
$thumbnailUrl = $this->post->getThumbnailUrl();
|
||||
|
||||
// Powiązane data_upd — wyświetl tylko gdy różni się od data_add
|
||||
$showUpdated = $this->post->date_upd
|
||||
&& substr($this->post->date_upd, 0, 10) !== substr($this->post->date_add, 0, 10);
|
||||
|
||||
// Drzewo kategorii do sidebara
|
||||
$categoryTree = $this->enrichCategoryTree(
|
||||
BlogCategory::getCategoryTree($idLang),
|
||||
$idLang
|
||||
);
|
||||
|
||||
// Meta strony
|
||||
$this->setPageMeta();
|
||||
|
||||
$this->context->smarty->assign([
|
||||
'blog_post' => $this->post,
|
||||
'blog_post_cats' => $postCats,
|
||||
'blog_primary_cat' => $primaryCat,
|
||||
'blog_thumbnail' => $thumbnailUrl,
|
||||
'blog_show_updated' => $showUpdated,
|
||||
'blog_url' => Projectproblog::getBlogUrl($idLang),
|
||||
'blog_category_tree' => $categoryTree,
|
||||
'blog_current_cat' => null,
|
||||
]);
|
||||
|
||||
$this->setTemplate('module:projectproblog/views/templates/front/post.tpl');
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Breadcrumb */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
public function getBreadcrumbLinks()
|
||||
{
|
||||
$breadcrumb = parent::getBreadcrumbLinks();
|
||||
|
||||
$breadcrumb['links'][] = [
|
||||
'title' => $this->l('Blog'),
|
||||
'url' => Projectproblog::getBlogUrl(),
|
||||
];
|
||||
|
||||
// Jeśli wpis ma kategorię — dodaj ją do breadcrumba
|
||||
if ($this->post) {
|
||||
$idLang = (int) $this->context->language->id;
|
||||
$categoryIds = BlogPost::getCategories((int) $this->post->id);
|
||||
|
||||
if (!empty($categoryIds)) {
|
||||
$catRow = Db::getInstance()->getRow(
|
||||
'SELECT cl.`name`, cl.`link_rewrite`
|
||||
FROM `' . _DB_PREFIX_ . 'projectproblog_category_lang` cl
|
||||
WHERE cl.`id_category` = ' . (int) $categoryIds[0] . '
|
||||
AND cl.`id_lang` = ' . (int) $idLang
|
||||
);
|
||||
|
||||
if ($catRow) {
|
||||
$breadcrumb['links'][] = [
|
||||
'title' => $catRow['name'],
|
||||
'url' => Projectproblog::getCategoryUrl($catRow['link_rewrite']),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$breadcrumb['links'][] = [
|
||||
'title' => $this->post->title,
|
||||
'url' => '',
|
||||
];
|
||||
}
|
||||
|
||||
return $breadcrumb;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Canonical URL */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* Wyłącza canonical redirection — patrz komentarz w list.php.
|
||||
*/
|
||||
protected function canonicalRedirection($canonical_url = '')
|
||||
{
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Helpery prywatne */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* Rekurencyjnie dodaje url do węzłów drzewa kategorii (sidebar).
|
||||
*/
|
||||
protected function enrichCategoryTree(array $tree, $idLang)
|
||||
{
|
||||
foreach ($tree as &$node) {
|
||||
$node['category']['url'] = Projectproblog::getCategoryUrl(
|
||||
$node['category']['link_rewrite'],
|
||||
$idLang
|
||||
);
|
||||
$node['children'] = $this->enrichCategoryTree($node['children'], $idLang);
|
||||
}
|
||||
unset($node);
|
||||
|
||||
return $tree;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pobiera dane kategorii po ID-kach i dodaje URL-e.
|
||||
*/
|
||||
protected function loadCategories(array $categoryIds, $idLang)
|
||||
{
|
||||
if (empty($categoryIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$idList = implode(',', array_map('intval', $categoryIds));
|
||||
|
||||
$rows = Db::getInstance()->executeS(
|
||||
'SELECT c.`id_category`, cl.`name`, cl.`link_rewrite`
|
||||
FROM `' . _DB_PREFIX_ . 'projectproblog_category` c
|
||||
LEFT JOIN `' . _DB_PREFIX_ . 'projectproblog_category_lang` cl
|
||||
ON c.`id_category` = cl.`id_category` AND cl.`id_lang` = ' . (int) $idLang . '
|
||||
WHERE c.`id_category` IN (' . $idList . ') AND c.`active` = 1
|
||||
ORDER BY cl.`name` ASC'
|
||||
);
|
||||
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($rows as &$row) {
|
||||
$row['url'] = Projectproblog::getCategoryUrl($row['link_rewrite'], $idLang);
|
||||
}
|
||||
unset($row);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ustawia meta title / description / keywords dla layoutu.
|
||||
*/
|
||||
protected function setPageMeta()
|
||||
{
|
||||
if (!$this->post) {
|
||||
return;
|
||||
}
|
||||
|
||||
$metaTitle = $this->post->meta_title ?: $this->post->title;
|
||||
$metaDesc = $this->post->meta_description ?: '';
|
||||
$metaKw = $this->post->meta_keywords ?: '';
|
||||
|
||||
$page = $this->context->smarty->getTemplateVars('page') ?: [];
|
||||
|
||||
if (!isset($page['meta'])) {
|
||||
$page['meta'] = [];
|
||||
}
|
||||
|
||||
$page['meta']['title'] = $metaTitle;
|
||||
$page['meta']['description'] = $metaDesc;
|
||||
$page['meta']['keywords'] = $metaKw;
|
||||
|
||||
$this->context->smarty->assign('page', $page);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user