From 2576bcfaf2ee983c87db629a2d7a56f00ddf3b2c Mon Sep 17 00:00:00 2001 From: Roman Parpalak Date: Thu, 15 Feb 2024 00:09:29 +0200 Subject: [PATCH] Added tag and comment icons. --- .../s2_blog/_include/Page/Favorite.php | 2 +- _extensions/s2_blog/_include/Page/HTML.php | 2 +- _extensions/s2_blog/_include/Page/Tag.php | 18 ++- _extensions/s2_search/_include/Page.php | 2 +- _include/Page/Abstract.php | 144 +++++++++--------- _include/Page/Service.php | 2 +- _include/Page/Tag.php | 4 +- _include/Viewer.php | 23 ++- _include/functions.php | 7 +- _include/src/Asset/AssetMerge.php | 1 + _include/views/tag_title.php | 8 + 11 files changed, 113 insertions(+), 100 deletions(-) create mode 100644 _include/views/tag_title.php diff --git a/_extensions/s2_blog/_include/Page/Favorite.php b/_extensions/s2_blog/_include/Page/Favorite.php index 7771b638..64715524 100644 --- a/_extensions/s2_blog/_include/Page/Favorite.php +++ b/_extensions/s2_blog/_include/Page/Favorite.php @@ -24,7 +24,7 @@ public function __construct (array $params = array()) public function body (array $params = array()) { - $this->obtainTemplate(__DIR__.'/../../templates/'); + $this->ensureTemplateIsLoaded(); if ($this->inTemplate('')) $this->page['s2_blog_calendar'] = Lib::calendar(date('Y'), date('m'), '0'); diff --git a/_extensions/s2_blog/_include/Page/HTML.php b/_extensions/s2_blog/_include/Page/HTML.php index c9d84fbb..9026c5a8 100644 --- a/_extensions/s2_blog/_include/Page/HTML.php +++ b/_extensions/s2_blog/_include/Page/HTML.php @@ -14,7 +14,7 @@ abstract class Page_HTML extends \Page_HTML { - public $template_id = 'blog.php'; + protected string $template_id = 'blog.php'; abstract public function body (array $params); diff --git a/_extensions/s2_blog/_include/Page/Tag.php b/_extensions/s2_blog/_include/Page/Tag.php index ffaf8a65..4cbf624e 100644 --- a/_extensions/s2_blog/_include/Page/Tag.php +++ b/_extensions/s2_blog/_include/Page/Tag.php @@ -1,8 +1,8 @@ inTemplate('')) - $this->page['s2_blog_calendar'] = Lib::calendar(date('Y'), date('m'), '0'); + if ($this->inTemplate('')) { + $this->page['s2_blog_calendar'] = Lib::calendar(date('Y'), date('m'), '0'); + } // A tag $this->posts_by_tag($params['tag'], !empty($params['slash'])); - $this->page['title'] = $this->page['head_title'] = s2_htmlencode($this->page['title']); // Bread crumbs $this->page['path'][] = array( 'title' => \Model::main_page_title(), 'link' => s2_link('/'), ); - if (S2_BLOG_URL) - { + + if (S2_BLOG_URL) { $this->page['path'][] = array( 'title' => Lang::get('Blog', 's2_blog'), 'link' => S2_BLOG_PATH, @@ -44,6 +44,10 @@ public function body (array $params = array()) 'title' => $this->page['title'], ); + + $this->page['head_title'] = s2_htmlencode($this->page['title']); + $this->page['title'] = $this->renderPartial('tag_title', ['title' => $this->page['title']]); + $this->page['link_navigation']['up'] = S2_BLOG_TAGS_PATH; } diff --git a/_extensions/s2_search/_include/Page.php b/_extensions/s2_search/_include/Page.php index 41190dfa..212a7158 100644 --- a/_extensions/s2_search/_include/Page.php +++ b/_extensions/s2_search/_include/Page.php @@ -21,7 +21,7 @@ class Page extends \Page_HTML implements \Page_Routable { - protected $template_id = 'service.php'; + protected string $template_id = 'service.php'; private int $page_num; private StemmerInterface $stemmer; diff --git a/_include/Page/Abstract.php b/_include/Page/Abstract.php index 3908e17c..95434c2d 100644 --- a/_include/Page/Abstract.php +++ b/_include/Page/Abstract.php @@ -5,90 +5,94 @@ /** * Abstract page controller class. Renders content for the browser * - * @copyright (C) 2014 Roman Parpalak + * @copyright (C) 2014-2024 Roman Parpalak * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher * @package S2 */ - abstract class Page_Abstract { - protected $template_id = 'site.php'; - protected $template = null; - protected $page = array(); - protected $etag = null; - protected $viewer = null; - - public function __construct (array $params = array()) - { - if (empty($this->viewer)) - $this->viewer = new Viewer(); - } - - protected function renderPartial($name, $vars) - { - return $this->viewer->render($name, $vars); - } - - public function obtainTemplate () - { - if ($this->template !== null) - return; - - $ext_dir = s2_ext_dir_from_ns(get_class($this)); - $path = $ext_dir ? $ext_dir . '/templates/' : false; - - try { - $this->template = s2_get_template($this->template_id, $path); - } - catch (Exception $e) { - error($e->getMessage()); - } - } - - public function inTemplate ($placeholder) - { - $this->obtainTemplate(); - return strpos($this->template, $placeholder) !== false; - } - - /** - * Outputs content to browser - */ - public function render () - { - /** @var ?DbLayer $s2_db */ - $s2_db = \Container::getIfInstantiated(DbLayer::class); + protected string $template_id = 'site.php'; + protected ?string $template = null; + /** + * @deprecated Come up with a DTO for page + * @note Somewhere this var can hold non-array values like false + */ + protected $page = array(); + protected ?string $etag = null; + protected Viewer $viewer; + + public function __construct(array $params = []) + { + if (empty($this->viewer)) { + $this->viewer = new Viewer(); + } // Might already be overridden in child classes. + } + + protected function renderPartial(string $name, array $vars): string + { + return $this->viewer->render($name, $vars); + } + + public function ensureTemplateIsLoaded(): void + { + if ($this->template !== null) { + return; + } - $this->obtainTemplate(); + $ext_dir = s2_ext_dir_from_ns(get_class($this)); + $path = $ext_dir ? $ext_dir . '/templates/' : false; - if ($this instanceof Page_HTML) { - $this->process_template(); + try { + $this->template = s2_get_template($this->template_id, $path); + } catch (Exception $e) { + error($e->getMessage()); } + } + + public function inTemplate($placeholder): bool + { + $this->ensureTemplateIsLoaded(); + + return str_contains($this->template, $placeholder); + } + + /** + * Outputs content to browser + */ + public function render(): void + { + $this->ensureTemplateIsLoaded(); - if ($s2_db !== null) { - $s2_db->close(); + if ($this instanceof Page_HTML) { + $this->process_template(); } - if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $this->etag) - { - header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified'); - exit; - } + /** @var ?DbLayer $s2_db */ + $s2_db = Container::getIfInstantiated(DbLayer::class); + $s2_db?->close(); - ob_start(); - if (S2_COMPRESS) - ob_start('ob_gzhandler'); + if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $this->etag) { + header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified'); + exit; + } - echo $this->template; + ob_start(); + if (S2_COMPRESS) { + ob_start('ob_gzhandler'); + } - if (S2_COMPRESS) - ob_end_flush(); + echo $this->template; - if (!empty($this->etag)) - header('ETag: '.$this->etag); - header('Content-Length: '.ob_get_length()); - header('Content-Type: text/html; charset=utf-8'); + if (S2_COMPRESS) { + ob_end_flush(); + } + + if ($this->etag !== null) { + header('ETag: ' . $this->etag); + } + header('Content-Length: ' . ob_get_length()); + header('Content-Type: text/html; charset=utf-8'); - ob_end_flush(); - } + ob_end_flush(); + } } diff --git a/_include/Page/Service.php b/_include/Page/Service.php index 3db2ecf9..57d6b908 100644 --- a/_include/Page/Service.php +++ b/_include/Page/Service.php @@ -10,7 +10,7 @@ class Page_Service extends Page_HTML { - protected $template_id = 'service.php'; + protected string $template_id = 'service.php'; public function __construct (array $params = array()) { diff --git a/_include/Page/Tag.php b/_include/Page/Tag.php index aaec3974..23aa843f 100644 --- a/_include/Page/Tag.php +++ b/_include/Page/Tag.php @@ -5,7 +5,7 @@ /** * Displays the list of pages and excerpts for a specified tag. * - * @copyright (C) 2007-2014 Roman Parpalak + * @copyright (C) 2007-2024 Roman Parpalak * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher * @package S2 */ @@ -154,7 +154,7 @@ private function make_tags_pages ($tag_name, $is_slash) 'title' => $tag_name, ), ), - 'title' => s2_htmlencode($tag_name), + 'title' => $this->renderPartial('tag_title', ['title' => $tag_name]), 'date' => '', 'text' => $this->renderPartial('list_text', array( 'description' => $tag_description, diff --git a/_include/Viewer.php b/_include/Viewer.php index 8a0c6eeb..e134e736 100644 --- a/_include/Viewer.php +++ b/_include/Viewer.php @@ -2,15 +2,15 @@ /** * Renders views. * - * @copyright (C) 2014 Roman Parpalak + * @copyright (C) 2014-2024 Roman Parpalak * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher * @package S2 */ class Viewer { - private $dirs = array(); - private $debug = false; + private array $dirs = []; + private bool $debug = false; public function __construct($that = null) { @@ -84,20 +84,15 @@ private static function jsonFormat($vars, int $level = 0): string return $str; } - /** - * @param $name - * @param array $vars - * @returns string - */ - public function render($name, array $vars) + public function render(string $name, array $vars): string { $name = preg_replace('#[^0-9a-zA-Z._\-]#', '', $name); $filename = $name . '.php'; - $found_file = ''; + $foundFile = null; foreach ($this->dirs as $dir) { if (file_exists($dir . $filename)) { - $found_file = $dir . $filename; + $foundFile = $dir . $filename; break; } } @@ -112,8 +107,8 @@ public function render($name, array $vars) echo ''; } - if ($found_file) { - $this->include_file($found_file, $vars); + if ($foundFile !== null) { + $this->includeFile($foundFile, $vars); } elseif ($this->debug) { echo 'View file not found in ', s2_htmlencode(var_export($this->dirs, true)); } @@ -125,7 +120,7 @@ public function render($name, array $vars) return ob_get_clean(); } - private function include_file($_found_file, $_vars): void + private function includeFile(string $_found_file, array $_vars): void { extract($_vars, EXTR_OVERWRITE); include $_found_file; diff --git a/_include/functions.php b/_include/functions.php index 73e727c8..87e902c9 100644 --- a/_include/functions.php +++ b/_include/functions.php @@ -167,7 +167,7 @@ function s2_process_multipart_mixed(&$src, &$dest, $dir = false) * * @throws Exception */ -function s2_get_template($rawTemplateId, $defaultPath = false) +function s2_get_template($rawTemplateId, $defaultPath = false): string { global $request_uri; @@ -621,8 +621,9 @@ function error() close(); + /** @var ?\S2\Cms\Pdo\DbLayer $s2_db */ + $s2_db = Container::getIfInstantiated(\S2\Cms\Pdo\DbLayer::class); + $s2_db?->close(); exit; } diff --git a/_include/src/Asset/AssetMerge.php b/_include/src/Asset/AssetMerge.php index d1833997..6a51eeb9 100644 --- a/_include/src/Asset/AssetMerge.php +++ b/_include/src/Asset/AssetMerge.php @@ -91,6 +91,7 @@ private function needToDump(): bool } if ($this->devEnv) { + // TODO add images embedding in CSS $dumpModifiedAt = filemtime($this->getDumpFilename()); foreach ($this->filesToMerge as $fileToMerge) { if (filemtime($fileToMerge) > $dumpModifiedAt) { diff --git a/_include/views/tag_title.php b/_include/views/tag_title.php new file mode 100644 index 00000000..5306ae32 --- /dev/null +++ b/_include/views/tag_title.php @@ -0,0 +1,8 @@ + +