Skip to content

Commit

Permalink
Added tag and comment icons.
Browse files Browse the repository at this point in the history
  • Loading branch information
parpalak committed Feb 14, 2024
1 parent 72b64a2 commit 2576bcf
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 100 deletions.
2 changes: 1 addition & 1 deletion _extensions/s2_blog/_include/Page/Favorite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<!-- s2_blog_calendar -->'))
$this->page['s2_blog_calendar'] = Lib::calendar(date('Y'), date('m'), '0');
Expand Down
2 changes: 1 addition & 1 deletion _extensions/s2_blog/_include/Page/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 11 additions & 7 deletions _extensions/s2_blog/_include/Page/Tag.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Blog posts for a tag.
* Blog posts 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_blog
*/
Expand All @@ -16,20 +16,20 @@ class Page_Tag extends Page_HTML implements \Page_Routable
{
public function body (array $params = array())
{
if ($this->inTemplate('<!-- s2_blog_calendar -->'))
$this->page['s2_blog_calendar'] = Lib::calendar(date('Y'), date('m'), '0');
if ($this->inTemplate('<!-- s2_blog_calendar -->')) {
$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,
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion _extensions/s2_search/_include/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
144 changes: 74 additions & 70 deletions _include/Page/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion _include/Page/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
4 changes: 2 additions & 2 deletions _include/Page/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 9 additions & 14 deletions _include/Viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -112,8 +107,8 @@ public function render($name, array $vars)
echo '</pre>';
}

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));
}
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions _include/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -621,8 +621,9 @@ function error()
<?php

// If a database connection was established (before this error) we close it
if (isset($GLOBALS['s2_db']))
$GLOBALS['s2_db']->close();
/** @var ?\S2\Cms\Pdo\DbLayer $s2_db */
$s2_db = Container::getIfInstantiated(\S2\Cms\Pdo\DbLayer::class);
$s2_db?->close();

exit;
}
Expand Down
1 change: 1 addition & 0 deletions _include/src/Asset/AssetMerge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions _include/views/tag_title.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* Content of the title of a tag page.
*
* @var string $title
*/
?>
<span class="tag-title"><?php echo s2_htmlencode($title); ?></span>

0 comments on commit 2576bcf

Please sign in to comment.