From c64dfe207150990a46ace903bf3f9dbe4c1ade16 Mon Sep 17 00:00:00 2001 From: Roman Parpalak Date: Sun, 5 May 2024 20:07:26 +0300 Subject: [PATCH] Continuing refactoring outdated "Model" code. --- .../s2_blog/Controller/BlogController.php | 14 +++++--- .../s2_blog/Controller/DayPageController.php | 3 +- .../Controller/FavoritePageController.php | 13 ++++---- .../s2_blog/Controller/MainPageController.php | 13 +++++--- .../Controller/MonthPageController.php | 9 ++++-- .../s2_blog/Controller/PostPageController.php | 8 +++-- .../s2_blog/Controller/TagPageController.php | 22 ++++++------- .../s2_blog/Controller/TagsPageController.php | 5 ++- .../s2_blog/Controller/YearPageController.php | 8 +++-- _extensions/s2_blog/Extension.php | 14 ++++++++ .../Controller/SearchPageController.php | 10 ++++-- _include/src/CmsExtension.php | 5 +++ .../src/Controller/NotFoundController.php | 8 +++-- _include/src/Controller/PageFavorite.php | 2 +- _include/src/Controller/PageTag.php | 2 +- _include/src/Controller/PageTags.php | 4 ++- _include/src/Model/ArticleProvider.php | 17 ++++++++++ _include/src/Model/Model.php | 32 +++++-------------- 18 files changed, 118 insertions(+), 71 deletions(-) diff --git a/_extensions/s2_blog/Controller/BlogController.php b/_extensions/s2_blog/Controller/BlogController.php index 57879c1a..0d75eaca 100644 --- a/_extensions/s2_blog/Controller/BlogController.php +++ b/_extensions/s2_blog/Controller/BlogController.php @@ -11,6 +11,8 @@ use Lang; use S2\Cms\Framework\ControllerInterface; +use S2\Cms\Model\ArticleProvider; +use S2\Cms\Model\UrlBuilder; use S2\Cms\Pdo\DbLayer; use S2\Cms\Template\HtmlTemplate; use S2\Cms\Template\HtmlTemplateProvider; @@ -29,6 +31,8 @@ abstract public function body(Request $request, HtmlTemplate $template): ?Respon public function __construct( protected DbLayer $dbLayer, + protected ArticleProvider $articleProvider, + protected UrlBuilder $urlBuilder, protected HtmlTemplateProvider $templateProvider, protected Viewer $viewer, protected string $tagsUrl, @@ -36,7 +40,7 @@ public function __construct( protected string $blogTitle, // S2_BLOG_TITLE ) { - $this->blogPath = s2_link(str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . '/'); // S2_BLOG_PATH + $this->blogPath = $this->urlBuilder->link(str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . '/'); // S2_BLOG_PATH $this->blogTagsPath = $this->blogPath . urlencode($this->tagsUrl) . '/'; // S2_BLOG_TAGS_PATH Lang::load('s2_blog', function () { @@ -54,9 +58,11 @@ public function handle(Request $request): Response $template ->putInPlaceholder('commented', 0) ->putInPlaceholder('class', 's2_blog') - ->putInPlaceholder('rss_link', ['']) + ->putInPlaceholder('rss_link', [sprintf( + '', + s2_htmlencode(Lang::get('RSS link title', 's2_blog')), + $this->urlBuilder->link(str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . '/rss.xml') + )]) ; if ($template->hasPlaceholder('')) { diff --git a/_extensions/s2_blog/Controller/DayPageController.php b/_extensions/s2_blog/Controller/DayPageController.php index eca3f652..e81a3f82 100644 --- a/_extensions/s2_blog/Controller/DayPageController.php +++ b/_extensions/s2_blog/Controller/DayPageController.php @@ -15,7 +15,6 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; - class DayPageController extends BlogController { public function body(Request $request, HtmlTemplate $template): ?Response @@ -50,7 +49,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response ->putInPlaceholder('head_title', s2_date($startTime)) ; - $template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/')); + $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); if ($this->blogUrl !== '') { $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); } diff --git a/_extensions/s2_blog/Controller/FavoritePageController.php b/_extensions/s2_blog/Controller/FavoritePageController.php index f951a4f0..34b1df06 100644 --- a/_extensions/s2_blog/Controller/FavoritePageController.php +++ b/_extensions/s2_blog/Controller/FavoritePageController.php @@ -16,16 +16,15 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; - class FavoritePageController extends BlogController { - public function body (Request $request, HtmlTemplate $template): ?Response + public function body(Request $request, HtmlTemplate $template): ?Response { if ($request->attributes->get('slash') !== '/') { - return new RedirectResponse(s2_link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY); + return new RedirectResponse($this->urlBuilder->link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY); } - if ($template->hasPlaceholder('')) { + if ($template->hasPlaceholder('')) { $template->registerPlaceholder('', Lib::calendar(date('Y'), date('m'), '0')); } @@ -39,8 +38,8 @@ public function body (Request $request, HtmlTemplate $template): ?Response $template->markAsNotFound(); } - // Bread crumbs - $template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/')); + // Bread crumbs + $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); if ($this->blogUrl !== '') { $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); } @@ -55,5 +54,5 @@ public function body (Request $request, HtmlTemplate $template): ?Response $template->setLink('up', $this->blogPath); return null; - } + } } diff --git a/_extensions/s2_blog/Controller/MainPageController.php b/_extensions/s2_blog/Controller/MainPageController.php index 299c2e26..76a451e1 100644 --- a/_extensions/s2_blog/Controller/MainPageController.php +++ b/_extensions/s2_blog/Controller/MainPageController.php @@ -10,6 +10,8 @@ namespace s2_extensions\s2_blog\Controller; use Lang; +use S2\Cms\Model\ArticleProvider; +use S2\Cms\Model\UrlBuilder; use S2\Cms\Pdo\DbLayer; use S2\Cms\Template\HtmlTemplate; use S2\Cms\Template\HtmlTemplateProvider; @@ -19,11 +21,12 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; - class MainPageController extends BlogController { public function __construct( DbLayer $dbLayer, + ArticleProvider $articleProvider, + UrlBuilder $urlBuilder, HtmlTemplateProvider $templateProvider, Viewer $viewer, string $tagsUrl, @@ -31,7 +34,7 @@ public function __construct( string $blogTitle, private readonly int $itemsPerPage, ) { - parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); + parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); } public function handle(Request $request): Response @@ -45,7 +48,7 @@ public function handle(Request $request): Response public function body(Request $request, HtmlTemplate $template): ?Response { if ($request->attributes->get('slash') !== '/') { - return new RedirectResponse(s2_link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY); + return new RedirectResponse($this->urlBuilder->link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY); } $skipLastPostsNum = (int)$request->attributes->get('page', 0); @@ -90,7 +93,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response $template->putInPlaceholder('text', $output); - $template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/')); + $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); if ($this->blogUrl !== '') { $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $skipLastPostsNum > 0 ? $this->blogPath : null); } @@ -100,7 +103,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response } else { $template->putInPlaceholder('meta_description', $this->blogTitle); if ($this->blogUrl !== '') { - $template->setLink('up', s2_link('/')); + $template->setLink('up', $this->urlBuilder->link('/')); } } diff --git a/_extensions/s2_blog/Controller/MonthPageController.php b/_extensions/s2_blog/Controller/MonthPageController.php index 0d4cc7e2..141fae11 100644 --- a/_extensions/s2_blog/Controller/MonthPageController.php +++ b/_extensions/s2_blog/Controller/MonthPageController.php @@ -10,6 +10,8 @@ namespace s2_extensions\s2_blog\Controller; use Lang; +use S2\Cms\Model\ArticleProvider; +use S2\Cms\Model\UrlBuilder; use S2\Cms\Pdo\DbLayer; use S2\Cms\Template\HtmlTemplate; use S2\Cms\Template\HtmlTemplateProvider; @@ -18,11 +20,12 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; - class MonthPageController extends BlogController { public function __construct( DbLayer $dbLayer, + ArticleProvider $articleProvider, + UrlBuilder $urlBuilder, HtmlTemplateProvider $templateProvider, Viewer $viewer, string $tagsUrl, @@ -30,7 +33,7 @@ public function __construct( string $blogTitle, private readonly string $startYear, ) { - parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); + parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); } public function body(Request $request, HtmlTemplate $template): ?Response @@ -83,7 +86,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response ->putInPlaceholder('head_title', \Lang::month($month) . ', ' . $year) ; - $template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/')); + $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); if ($this->blogUrl !== '') { $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); } diff --git a/_extensions/s2_blog/Controller/PostPageController.php b/_extensions/s2_blog/Controller/PostPageController.php index fc4a6962..87209c39 100644 --- a/_extensions/s2_blog/Controller/PostPageController.php +++ b/_extensions/s2_blog/Controller/PostPageController.php @@ -10,6 +10,8 @@ namespace s2_extensions\s2_blog\Controller; use Lang; +use S2\Cms\Model\ArticleProvider; +use S2\Cms\Model\UrlBuilder; use S2\Cms\Pdo\DbLayer; use S2\Cms\Recommendation\RecommendationProvider; use S2\Cms\Template\HtmlTemplate; @@ -24,6 +26,8 @@ class PostPageController extends BlogController { public function __construct( DbLayer $dbLayer, + ArticleProvider $articleProvider, + UrlBuilder $urlBuilder, private readonly RecommendationProvider $recommendationProvider, HtmlTemplateProvider $templateProvider, Viewer $viewer, @@ -32,7 +36,7 @@ public function __construct( string $blogTitle, protected bool $showComments, ) { - parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); + parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); } public function body(Request $request, HtmlTemplate $template): ?Response @@ -49,7 +53,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response return $result; } - $template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/')); + $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); if ($this->blogUrl !== '') { $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); } diff --git a/_extensions/s2_blog/Controller/TagPageController.php b/_extensions/s2_blog/Controller/TagPageController.php index fa1e360d..05201c3d 100644 --- a/_extensions/s2_blog/Controller/TagPageController.php +++ b/_extensions/s2_blog/Controller/TagPageController.php @@ -26,17 +26,17 @@ class TagPageController extends BlogController { public function __construct( - DbLayer $dbLayer, - private readonly ArticleProvider $articleProvider, - private readonly UrlBuilder $urlBuilder, - HtmlTemplateProvider $templateProvider, - Viewer $viewer, - string $tagsUrl, - string $blogUrl, - string $blogTitle, - private readonly bool $useHierarchy + DbLayer $dbLayer, + ArticleProvider $articleProvider, + UrlBuilder $urlBuilder, + HtmlTemplateProvider $templateProvider, + Viewer $viewer, + string $tagsUrl, + string $blogUrl, + string $blogTitle, + private readonly bool $useHierarchy ) { - parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); + parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); } public function body(Request $request, HtmlTemplate $template): ?Response @@ -89,7 +89,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response throw new NotFoundException(); } - $template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), $this->urlBuilder->link('/')); + $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); if ($this->blogUrl !== '') { $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); } diff --git a/_extensions/s2_blog/Controller/TagsPageController.php b/_extensions/s2_blog/Controller/TagsPageController.php index d6e2579c..f7e7ce2e 100644 --- a/_extensions/s2_blog/Controller/TagsPageController.php +++ b/_extensions/s2_blog/Controller/TagsPageController.php @@ -16,13 +16,12 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; - class TagsPageController extends BlogController { public function body (Request $request, HtmlTemplate $template): ?Response { if ($request->attributes->get('slash') !== '/') { - return new RedirectResponse(s2_link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY); + return new RedirectResponse($this->urlBuilder->link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY); } $template->registerPlaceholder('', ''); @@ -76,7 +75,7 @@ public function body (Request $request, HtmlTemplate $template): ?Response $template->putInPlaceholder('text', $this->viewer->render('tags_list', ['tags' => $tags])); - $template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/')); + $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); if ($this->blogUrl !== '') { $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); } diff --git a/_extensions/s2_blog/Controller/YearPageController.php b/_extensions/s2_blog/Controller/YearPageController.php index df272233..e6c903e2 100644 --- a/_extensions/s2_blog/Controller/YearPageController.php +++ b/_extensions/s2_blog/Controller/YearPageController.php @@ -10,6 +10,8 @@ namespace s2_extensions\s2_blog\Controller; use Lang; +use S2\Cms\Model\ArticleProvider; +use S2\Cms\Model\UrlBuilder; use S2\Cms\Pdo\DbLayer; use S2\Cms\Template\HtmlTemplate; use S2\Cms\Template\HtmlTemplateProvider; @@ -23,6 +25,8 @@ class YearPageController extends BlogController { public function __construct( DbLayer $dbLayer, + ArticleProvider $articleProvider, + UrlBuilder $urlBuilder, HtmlTemplateProvider $templateProvider, Viewer $viewer, string $tagsUrl, @@ -30,7 +34,7 @@ public function __construct( string $blogTitle, private readonly string $startYear, ) { - parent::__construct($dbLayer, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); + parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); } public function body(Request $request, HtmlTemplate $template): ?Response @@ -82,7 +86,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response 'content' => $content ], 's2_blog')); - $template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/')); + $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); if ($this->blogUrl !== '') { $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); } diff --git a/_extensions/s2_blog/Extension.php b/_extensions/s2_blog/Extension.php index 47efef66..a4e7076c 100644 --- a/_extensions/s2_blog/Extension.php +++ b/_extensions/s2_blog/Extension.php @@ -44,6 +44,8 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new MainPageController( $container->get(DbLayer::class), + $container->get(ArticleProvider::class), + $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), $provider->get('S2_TAGS_URL'), @@ -57,6 +59,8 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new DayPageController( $container->get(DbLayer::class), + $container->get(ArticleProvider::class), + $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), $provider->get('S2_TAGS_URL'), @@ -69,6 +73,8 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new MonthPageController( $container->get(DbLayer::class), + $container->get(ArticleProvider::class), + $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), $provider->get('S2_TAGS_URL'), @@ -82,6 +88,8 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new YearPageController( $container->get(DbLayer::class), + $container->get(ArticleProvider::class), + $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), $provider->get('S2_TAGS_URL'), @@ -95,6 +103,8 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new PostPageController( $container->get(DbLayer::class), + $container->get(ArticleProvider::class), + $container->get(UrlBuilder::class), $container->get(RecommendationProvider::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), @@ -109,6 +119,8 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new TagsPageController( $container->get(DbLayer::class), + $container->get(ArticleProvider::class), + $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), $provider->get('S2_TAGS_URL'), @@ -136,6 +148,8 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new FavoritePageController( $container->get(DbLayer::class), + $container->get(ArticleProvider::class), + $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), $provider->get('S2_TAGS_URL'), diff --git a/_extensions/s2_search/Controller/SearchPageController.php b/_extensions/s2_search/Controller/SearchPageController.php index b5ae5226..d8e282fe 100644 --- a/_extensions/s2_search/Controller/SearchPageController.php +++ b/_extensions/s2_search/Controller/SearchPageController.php @@ -13,6 +13,8 @@ use Lang; use S2\Cms\Framework\ControllerInterface; +use S2\Cms\Model\ArticleProvider; +use S2\Cms\Model\UrlBuilder; use S2\Cms\Pdo\DbLayer; use S2\Cms\Template\HtmlTemplateProvider; use S2\Cms\Template\Viewer; @@ -31,6 +33,8 @@ public function __construct( private Finder $finder, private StemmerInterface $stemmer, private DbLayer $dbLayer, + private ArticleProvider $articleProvider, + private UrlBuilder $urlBuilder, private HtmlTemplateProvider $templateProvider, private Viewer $viewer, private bool $debugView, @@ -101,7 +105,7 @@ public function handle(Request $request): Response } $link_nav = []; - $content['paging'] = s2_paging($pageNum, $totalPages, s2_link('/search', ['q=' . str_replace('%', '%%', urlencode($query)), 'p=%d']), $link_nav); + $content['paging'] = s2_paging($pageNum, $totalPages, $this->urlBuilder->link('/search', ['q=' . str_replace('%', '%%', urlencode($query)), 'p=%d']), $link_nav); foreach ($link_nav as $rel => $href) { $template->setLink($rel, $href); } @@ -112,7 +116,7 @@ public function handle(Request $request): Response $template->putInPlaceholder('title', Lang::get('Search', 's2_search')); $template->registerPlaceholder('', ''); - $template->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/')); + $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); $template->addBreadCrumb(Lang::get('Search', 's2_search')); return $template->toHttpResponse(); @@ -156,7 +160,7 @@ private function findInTags(Query $query): string $tags = []; while ($row = $this->dbLayer->fetchAssoc($s2_search_result)) { if ($this->tagIsSimilarToWords($row['name'], $words)) { - $tags[] = '' . $row['name'] . ''; + $tags[] = '' . $row['name'] . ''; } } diff --git a/_include/src/CmsExtension.php b/_include/src/CmsExtension.php index a2671ccf..4143c8f6 100644 --- a/_include/src/CmsExtension.php +++ b/_include/src/CmsExtension.php @@ -145,6 +145,8 @@ public function buildContainer(Container $container): void $container->get(Finder::class), $container->get(StemmerInterface::class), $container->get(DbLayer::class), + $container->get(ArticleProvider::class), + $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), $container->getParameter('debug_view'), @@ -295,6 +297,8 @@ public function buildContainer(Container $container): void $container->set(NotFoundController::class, function (Container $container) { return new NotFoundController( + $container->get(ArticleProvider::class), + $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), ); }); @@ -312,6 +316,7 @@ public function buildContainer(Container $container): void $container->set(PageTags::class, function (Container $container) { return new PageTags( $container->get(TagsProvider::class), + $container->get(ArticleProvider::class), $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), diff --git a/_include/src/Controller/NotFoundController.php b/_include/src/Controller/NotFoundController.php index 7f4d078a..58d28951 100644 --- a/_include/src/Controller/NotFoundController.php +++ b/_include/src/Controller/NotFoundController.php @@ -10,6 +10,8 @@ namespace S2\Cms\Controller; use S2\Cms\Framework\ControllerInterface; +use S2\Cms\Model\ArticleProvider; +use S2\Cms\Model\UrlBuilder; use S2\Cms\Template\HtmlTemplateProvider; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -17,6 +19,8 @@ readonly class NotFoundController implements ControllerInterface { public function __construct( + private ArticleProvider $articleProvider, + private UrlBuilder $urlBuilder, private HtmlTemplateProvider $htmlTemplateProvider, ) { } @@ -29,8 +33,8 @@ public function handle(Request $request): Response ->markAsNotFound() ->putInPlaceholder('head_title', \Lang::get('Error 404')) ->putInPlaceholder('title', '

' . \Lang::get('Error 404') . '

') - ->putInPlaceholder('text', sprintf(\Lang::get('Error 404 text'), s2_link('/'))) - ->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), s2_link('/')) + ->putInPlaceholder('text', sprintf(\Lang::get('Error 404 text'), $this->urlBuilder->link('/'))) + ->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')) ; return $template->toHttpResponse(); diff --git a/_include/src/Controller/PageFavorite.php b/_include/src/Controller/PageFavorite.php index 657f4eaa..9e030c81 100644 --- a/_include/src/Controller/PageFavorite.php +++ b/_include/src/Controller/PageFavorite.php @@ -117,7 +117,7 @@ public function handle(Request $request): Response $template = $this->htmlTemplateProvider->getTemplate('site.php'); $template - ->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), $this->urlBuilder->link('/')) + ->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')) ->addBreadCrumb(\Lang::get('Favorite')) ->putInPlaceholder('title', \Lang::get('Favorite')) ->putInPlaceholder('date', '') diff --git a/_include/src/Controller/PageTag.php b/_include/src/Controller/PageTag.php index bdb83abe..38759b52 100644 --- a/_include/src/Controller/PageTag.php +++ b/_include/src/Controller/PageTag.php @@ -141,7 +141,7 @@ public function handle(Request $request): Response $template = $this->htmlTemplateProvider->getTemplate('site.php'); $template - ->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), $this->urlBuilder->link('/')) + ->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')) ->addBreadCrumb(\Lang::get('Tags'), $this->urlBuilder->link('/' . $this->tagsUrlFragment . '/')) ->addBreadCrumb($tagName) ->putInPlaceholder('title', $this->viewer->render('tag_title', ['title' => $tagName])) diff --git a/_include/src/Controller/PageTags.php b/_include/src/Controller/PageTags.php index cc569639..f923e0d8 100644 --- a/_include/src/Controller/PageTags.php +++ b/_include/src/Controller/PageTags.php @@ -12,6 +12,7 @@ namespace S2\Cms\Controller; use S2\Cms\Framework\ControllerInterface; +use S2\Cms\Model\ArticleProvider; use S2\Cms\Model\TagsProvider; use S2\Cms\Model\UrlBuilder; use S2\Cms\Template\HtmlTemplateProvider; @@ -24,6 +25,7 @@ { public function __construct( private TagsProvider $tagsProvider, + private ArticleProvider $articleProvider, private UrlBuilder $urlBuilder, private HtmlTemplateProvider $htmlTemplateProvider, private Viewer $viewer @@ -39,7 +41,7 @@ public function handle(Request $request): Response $template = $this->htmlTemplateProvider->getTemplate('site.php'); $template - ->addBreadCrumb(\S2\Cms\Model\Model::main_page_title(), $this->urlBuilder->link('/')) + ->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')) ->addBreadCrumb(\Lang::get('Tags')) ->putInPlaceholder('title', \Lang::get('Tags')) ->putInPlaceholder('date', '') diff --git a/_include/src/Model/ArticleProvider.php b/_include/src/Model/ArticleProvider.php index 017492e6..fe16ab5d 100644 --- a/_include/src/Model/ArticleProvider.php +++ b/_include/src/Model/ArticleProvider.php @@ -92,6 +92,23 @@ public function getFullUrlsForArticles(array $parentIds, array $urls): array return $urls; } + /** + * Returns the title of the main page. + * + * @throws \S2\Cms\Pdo\DbLayerException + */ + public function mainPageTitle(): string + { + // TODO cache? + $result = $this->dbLayer->buildAndQuery([ + 'SELECT' => 'title', + 'FROM' => 'articles', + 'WHERE' => 'parent_id = ' . self::ROOT_ID, + ]); + + return $this->dbLayer->result($result); + } + /** * Fetching last articles info (for template placeholders and RSS) * diff --git a/_include/src/Model/Model.php b/_include/src/Model/Model.php index 8baf4f9f..23f7a0b5 100644 --- a/_include/src/Model/Model.php +++ b/_include/src/Model/Model.php @@ -10,15 +10,20 @@ * @copyright (C) 2007-2014 Roman Parpalak * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher * @package S2 + * + * @deprecated Do not use static calls like this in a new code. */ class Model { const ROOT_ID = 0; - // - // Returns the full path for an article - // + /** + * Returns the full path for an article + * + * @deprecated Do not use static calls like this in a new code. + * @throws \S2\Cms\Pdo\DbLayerException + */ public static function path_from_id($id, $visible_for_all = false) { /** @var DbLayer $s2_db */ @@ -54,25 +59,4 @@ public static function path_from_id($id, $visible_for_all = false) return $prefix . '/' . urlencode($row[0]); } - - // - // Returns the title of the main page - // TODO cache - // - public static function main_page_title() - { - /** @var DbLayer $s2_db */ - $s2_db = \Container::get(DbLayer::class); - - $query = array( - 'SELECT' => 'title', - 'FROM' => 'articles', - 'WHERE' => 'parent_id = ' . self::ROOT_ID, - ); - - ($hook = s2_hook('fn_s2_main_page_title_qr')) ? eval($hook) : null; - - $result = $s2_db->buildAndQuery($query); - return $s2_db->result($result); - } }