Skip to content

Commit

Permalink
Refactoring in PageCommon controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
parpalak committed Feb 27, 2024
1 parent 64a0d9d commit c0652a4
Show file tree
Hide file tree
Showing 17 changed files with 752 additions and 644 deletions.
2 changes: 1 addition & 1 deletion _extensions/s2_blog/_include/Page/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function body (Request $request): ?Response
{
$params = $request->attributes->all();

if ($this->inTemplate('<!-- s2_blog_calendar -->')) {
if ($this->hasPlaceholder('<!-- s2_blog_calendar -->')) {
$this->page['s2_blog_calendar'] = Lib::calendar($params['year'], $params['month'], $params['day']);
}

Expand Down
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 @@ -25,7 +25,7 @@ public function body (Request $request): ?Response

$this->ensureTemplateIsLoaded();

if ($this->inTemplate('<!-- s2_blog_calendar -->'))
if ($this->hasPlaceholder('<!-- s2_blog_calendar -->'))
$this->page['s2_blog_calendar'] = Lib::calendar(date('Y'), date('m'), '0');

$this->favorite_posts();
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 @@ -49,7 +49,7 @@ public function handle(Request $request): ?Response

$this->page['head_title'] = empty($this->page['head_title']) ? S2_BLOG_TITLE : $this->page['head_title'] . ' - ' . S2_BLOG_TITLE;

if (!isset($this->page['s2_blog_navigation']) && $this->inTemplate('<!-- s2_blog_navigation -->')) {
if (!isset($this->page['s2_blog_navigation']) && $this->hasPlaceholder('<!-- s2_blog_navigation -->')) {
$this->page['s2_blog_navigation'] = $this->blog_navigation();
}

Expand Down
2 changes: 1 addition & 1 deletion _extensions/s2_blog/_include/Page/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function body (Request $request): ?Response

$this->template_id = $s2_blog_skip ? 'blog.php' : 'blog_main.php';

if ($this->inTemplate('<!-- s2_blog_calendar -->'))
if ($this->hasPlaceholder('<!-- s2_blog_calendar -->'))
$this->page['s2_blog_calendar'] = Lib::calendar(date('Y'), date('m'), '0');

$this->last_posts($s2_blog_skip);
Expand Down
2 changes: 1 addition & 1 deletion _extensions/s2_blog/_include/Page/Month.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function body (Request $request): ?Response
{
$params = $request->attributes->all();

if ($this->inTemplate('<!-- s2_blog_calendar -->'))
if ($this->hasPlaceholder('<!-- s2_blog_calendar -->'))
$this->page['s2_blog_calendar'] = Lib::calendar($params['year'], $params['month'], 0);

$this->page['title'] = '';
Expand Down
8 changes: 4 additions & 4 deletions _extensions/s2_blog/_include/Page/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function body (Request $request): ?Response
{
$params = $request->attributes->all();

if ($this->inTemplate('<!-- s2_blog_calendar -->'))
if ($this->hasPlaceholder('<!-- s2_blog_calendar -->'))
$this->page['s2_blog_calendar'] = Lib::calendar($params['year'], $params['month'], $params['day'], $params['url']);

$this->page['title'] = '';
Expand Down Expand Up @@ -95,7 +95,7 @@ private function get_post($year, $month, $day, $url)

$this->page['canonical_path'] = S2_BLOG_PATH . date('Y/m/d/', $row['create_time']) . $row['url'];

$is_back_forward = $this->inTemplate('<!-- s2_blog_back_forward -->');
$is_back_forward = $this->hasPlaceholder('<!-- s2_blog_back_forward -->');

$queries = [];
if ($label) {
Expand Down Expand Up @@ -180,7 +180,7 @@ private function get_post($year, $month, $day, $url)
];

$this->page['commented'] = $row['commented'];
if ($row['commented'] && S2_SHOW_COMMENTS && $this->inTemplate('<!-- s2_comments -->'))
if ($row['commented'] && S2_SHOW_COMMENTS && $this->hasPlaceholder('<!-- s2_comments -->'))
$this->page['comments'] = $this->get_comments($post_id);

$row['time'] = s2_date_time($row['create_time']);
Expand All @@ -191,7 +191,7 @@ private function get_post($year, $month, $day, $url)

$this->page['text'] = $this->renderPartial('post', $row);

if ($this->inTemplate('<!-- s2_recommendations -->')) {
if ($this->hasPlaceholder('<!-- s2_recommendations -->')) {
/** @var RecommendationProvider $recommendationProvider */
$recommendationProvider = \Container::get(RecommendationProvider::class);
global $request_uri;
Expand Down
2 changes: 1 addition & 1 deletion _extensions/s2_blog/_include/Page/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function body (Request $request): ?Response
{
$params = $request->attributes->all();

if ($this->inTemplate('<!-- s2_blog_calendar -->')) {
if ($this->hasPlaceholder('<!-- 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/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function body (Request $request): ?Response
return new RedirectResponse(s2_link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY);
}

if ($this->inTemplate('<!-- s2_blog_calendar -->'))
if ($this->hasPlaceholder('<!-- s2_blog_calendar -->'))
$this->page['s2_blog_calendar'] = Lib::calendar(date('Y'), date('m'), '0');

// The list of tags
Expand Down
2 changes: 1 addition & 1 deletion _extensions/s2_blog/_include/Page/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function body (Request $request): ?Response
{
$params = $request->attributes->all();

if ($this->inTemplate('<!-- s2_blog_calendar -->'))
if ($this->hasPlaceholder('<!-- s2_blog_calendar -->'))
$this->page['s2_blog_calendar'] = Lib::calendar($params['year'], '', 0);

$this->page['title'] = '';
Expand Down
41 changes: 22 additions & 19 deletions _extensions/s2_blog/hooks/fn_s2_parse_page_url_end.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@
/**
* Hook fn_s2_parse_page_url_end
*
* @copyright (C) 2023 Roman Parpalak
* @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
* @copyright 2023-2024 Roman Parpalak
* @license MIT
* @package s2_blog
*
* @var int $articleId
* @var \S2\Cms\Controller\PageCommon $this
* @var \S2\Cms\Template\HtmlTemplate $template
*/

if (!defined('S2_ROOT')) {
die;
if (!defined('S2_ROOT')) {
die;
}

if ($this->inTemplate('<!-- s2_blog_tags -->'))
{
Lang::load('s2_blog', function ()
{
if (file_exists(S2_ROOT.'/_extensions/s2_blog'.'/lang/'.S2_LANGUAGE.'.php'))
return require S2_ROOT.'/_extensions/s2_blog'.'/lang/'.S2_LANGUAGE.'.php';
else
return require S2_ROOT.'/_extensions/s2_blog'.'/lang/English.php';
});
if ($template->hasPlaceholder('<!-- s2_blog_tags -->')) {
Lang::load('s2_blog', static function () {
if (file_exists(S2_ROOT . '/_extensions/s2_blog' . '/lang/' . S2_LANGUAGE . '.php')) {
return require S2_ROOT . '/_extensions/s2_blog' . '/lang/' . S2_LANGUAGE . '.php';
}

return require S2_ROOT . '/_extensions/s2_blog' . '/lang/English.php';
});

$s2_blog_tags = s2_extensions\s2_blog\Placeholder::blog_tags($id);
$page['s2_blog_tags'] = empty($s2_blog_tags) ? '' : $this->renderPartial('menu_block', array(
'title' => Lang::get('See in blog', 's2_blog'),
'menu' => $s2_blog_tags,
'class' => 's2_blog_tags',
));
$s2_blog_tags = s2_extensions\s2_blog\Placeholder::blog_tags($articleId);
$template->putInPlaceholder('s2_blog_tags', empty($s2_blog_tags) ? '' : $this->viewer->render('menu_block', [
'title' => Lang::get('See in blog', 's2_blog'),
'menu' => $s2_blog_tags,
'class' => 's2_blog_tags',
]));
}
14 changes: 7 additions & 7 deletions _extensions/s2_blog/hooks/idx_pre_get_queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$s2_blog_placehoders = array();

foreach (array('s2_blog_last_comments', 's2_blog_last_discussions', 's2_blog_last_post') as $s2_blog_placehoder)
if ($this->inTemplate('<!-- ' . $s2_blog_placehoder . ' -->'))
if ($this->hasPlaceholder('<!-- ' . $s2_blog_placehoder . ' -->'))
$s2_blog_placehoders[$s2_blog_placehoder] = 1;

if (!empty($s2_blog_placehoders))
Expand All @@ -29,15 +29,15 @@
if (isset($s2_blog_placehoders['s2_blog_last_comments']))
{
$s2_blog_recent_comments = s2_extensions\s2_blog\Placeholder::recent_comments();
$replace['<!-- s2_blog_last_comments -->'] = empty($s2_blog_recent_comments) ? '' : $this->renderPartial('menu_comments', array(
$replace['<!-- s2_blog_last_comments -->'] = empty($s2_blog_recent_comments) ? '' : $this->viewer->render('menu_comments', array(
'title' => Lang::get('Last comments', 's2_blog'),
'menu' => $s2_blog_recent_comments,
));
}
if (isset($s2_blog_placehoders['s2_blog_last_discussions']))
{
$s2_blog_last_discussions = s2_extensions\s2_blog\Placeholder::recent_discussions();
$replace['<!-- s2_blog_last_discussions -->'] = empty($s2_blog_last_discussions) ? '' : $this->renderPartial('menu_block', array(
$replace['<!-- s2_blog_last_discussions -->'] = empty($s2_blog_last_discussions) ? '' : $this->viewer->render('menu_block', array(
'title' => Lang::get('Last discussions', 's2_blog'),
'menu' => $s2_blog_last_discussions,
'class' => 's2_blog_last_discussions',
Expand All @@ -52,7 +52,7 @@
unset($s2_blog_post);
$replace['<!-- s2_blog_last_post -->'] = implode('', $s2_blog_data);
}
$replace['<!-- s2_blog_tags -->'] = $page['s2_blog_tags'] ?? '';
$replace['<!-- s2_blog_calendar -->'] = $page['s2_blog_calendar'] ?? '';
$replace['<!-- s2_blog_navigation -->'] = $page['s2_blog_navigation'] ?? '';
$replace['<!-- s2_blog_back_forward -->'] = $page['s2_blog_back_forward'] ?? '';
$replace['<!-- s2_blog_tags -->'] = $this->page['s2_blog_tags'] ?? '';
$replace['<!-- s2_blog_calendar -->'] = $this->page['s2_blog_calendar'] ?? '';
$replace['<!-- s2_blog_navigation -->'] = $this->page['s2_blog_navigation'] ?? '';
$replace['<!-- s2_blog_back_forward -->'] = $this->page['s2_blog_back_forward'] ?? '';
2 changes: 1 addition & 1 deletion _include/Page/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function ensureTemplateIsLoaded(): void
}
}

public function inTemplate($placeholder): bool
public function hasPlaceholder($placeholder): bool
{
$this->ensureTemplateIsLoaded();

Expand Down
Loading

0 comments on commit c0652a4

Please sign in to comment.