From cbc6cc2518c6f87ca29d17e87815aee13dad4496 Mon Sep 17 00:00:00 2001 From: Roman Parpalak Date: Fri, 10 May 2024 14:46:34 +0300 Subject: [PATCH] Started refactoring in blog model classes. --- _extensions/s2_blog/BlogUrlBuilder.php | 86 ++++++++++ _extensions/s2_blog/CalendarBuilder.php | 157 ++++++++++++++++++ .../s2_blog/Controller/BlogController.php | 26 ++- _extensions/s2_blog/Controller/BlogRss.php | 7 +- .../s2_blog/Controller/DayPageController.php | 27 ++- .../Controller/FavoritePageController.php | 9 +- .../s2_blog/Controller/MainPageController.php | 22 +-- .../Controller/MonthPageController.php | 49 +++--- .../s2_blog/Controller/PostPageController.php | 40 +++-- _extensions/s2_blog/Controller/Sitemap.php | 11 +- .../s2_blog/Controller/TagPageController.php | 21 +-- .../s2_blog/Controller/TagsPageController.php | 39 +++-- .../s2_blog/Controller/YearPageController.php | 45 +++-- _extensions/s2_blog/Extension.php | 59 ++++--- _extensions/s2_blog/Lib.php | 116 +------------ 15 files changed, 432 insertions(+), 282 deletions(-) create mode 100644 _extensions/s2_blog/BlogUrlBuilder.php create mode 100644 _extensions/s2_blog/CalendarBuilder.php diff --git a/_extensions/s2_blog/BlogUrlBuilder.php b/_extensions/s2_blog/BlogUrlBuilder.php new file mode 100644 index 00000000..43ec2bfa --- /dev/null +++ b/_extensions/s2_blog/BlogUrlBuilder.php @@ -0,0 +1,86 @@ +blogPath ?? $this->blogPath = $this->urlBuilder->link(str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . '/'); + } + + public function favorite(): string + { + return $this->main() . urlencode($this->favoriteUrl) . '/'; + } + + public function tags(): string + { + return $this->blogTagsPath ?? $this->blogTagsPath = $this->main() . urlencode($this->tagsUrl) . '/'; + } + + public function tag(string $tagUrl): string + { + return $this->tags() . urlencode($tagUrl) . '/'; + } + + public function year(int $year): string + { + return $this->main() . $year . '/'; + } + + public function month(int $year, int $month): string + { + return $this->main() . $year . '/' . self::extendNumber($month) . '/'; + } + + public function monthFromTimestamp(int $timestamp): string + { + return $this->main() . date('Y/m/', $timestamp); + } + + public function day(int $year, int $month, int $day): string + { + return $this->main() . $year . '/' . self::extendNumber($month) . '/' . self::extendNumber($day) . '/'; + } + + public function post(int $year, int $month, int $day, string $url): string + { + return $this->main() . $year . '/' . self::extendNumber($month) . '/' . self::extendNumber($day) . '/' . urlencode($url); + } + + public function postFromTimestamp(int $create_time, string $url): string + { + return $this->main() . date('Y/m/d/', $create_time) . urlencode($url); + } + + public function blogIsOnTheSiteRoot(): bool + { + return $this->blogUrl === ''; + } + + private static function extendNumber(int $month): string + { + return str_pad((string)$month, 2, '0', STR_PAD_LEFT); + } +} diff --git a/_extensions/s2_blog/CalendarBuilder.php b/_extensions/s2_blog/CalendarBuilder.php new file mode 100644 index 00000000..f1a9bb03 --- /dev/null +++ b/_extensions/s2_blog/CalendarBuilder.php @@ -0,0 +1,157 @@ + 'create_time, url', + 'FROM' => 's2_blog_posts', + 'WHERE' => 'create_time < ' . $endTime . ' AND create_time >= ' . $startTime . ' AND published = 1' + ]; + $result = $this->dbLayer->buildAndQuery($query); + while ($row = $this->dbLayer->fetchRow($result)) { + $dayUrls[1 + (int)(($row[0] - $startTime) / 86400)][] = $row[1]; + } + } + + // Header + $monthName = \Lang::month($month); + if ($day === null) { + // One of 12 year tables + if ($startTime < time()) { + $monthName = '' . $monthName . ''; + } + $header = '' . $monthName . ''; + } else { + if ($day !== 0) { + $monthName = '' . $monthName . ''; + } + + // Links in the header + $next_month = $endTime < time() ? '' : '→'; + + $prevTime = mktime(0, 0, 0, $month - 1, 1, $year); + $prevMonth = $prevTime >= mktime(0, 0, 0, 1, 1, $this->startYear) ? '' : '←'; + + $header = '' . $prevMonth . '' + . $monthName . ', ' . $year . '' . $next_month . ''; + } + + // Titles + $output = '' . $header . ''; + + // Empty cells before + for ($i = 0; $i < $currentColumnIndex; $i++) { + $output .= 'isWeekend($i) ? ' class="sun"' : '') . '>'; + } + + // Days + for ($currentDayInMonth = 1; $currentDayInMonth <= $daysInThisMonth; $currentDayInMonth++) { + $currentColumnIndex++; + $cellContent = $currentDayInMonth; // Simple text content + if (isset($dayUrls[$currentDayInMonth])) { + if (\count($dayUrls[$currentDayInMonth]) !== 1 && ($currentDayInMonth !== $day || $url !== '')) { + // Several posts, link to the day page (if this is not the day selected) + $cellContent = '' . $currentDayInMonth . ''; + } + if (\count($dayUrls[$currentDayInMonth]) === 1 && ($currentDayInMonth !== $day || $url === '')) { + // One post, link to it (if this is not the post selected) + $cellContent = '' . $currentDayInMonth . ''; + } + } + + $classes = []; + if ($currentDayInMonth === $day) { + // Current day + $classes[] = 'cur'; + } + + if ($this->isWeekend($currentColumnIndex)) { + // Weekend + $classes[] = 'sun'; + } + + $output .= '' + . $cellContent + . ''; + + if (!($currentColumnIndex % 7) && ($currentDayInMonth !== $daysInThisMonth)) { + $output .= ''; + } + } + + // Empty cells in the end + while ($currentColumnIndex % 7) { + $currentColumnIndex++; + $output .= 'isWeekend($currentColumnIndex) ? ' class="sun"' : '') . '>'; + } + + $output .= '
'; + return $output; + } + + + private function isWeekend(int $n): bool + { + if ($n % 7 === 0) { + return true; + } + if ($n % 7 === 6 && \Lang::get('Sunday starts week', 's2_blog') != '1') { + return true; + } + if ($n % 7 === 1 && \Lang::get('Sunday starts week', 's2_blog') == '1') { + return true; + } + return false; + } +} diff --git a/_extensions/s2_blog/Controller/BlogController.php b/_extensions/s2_blog/Controller/BlogController.php index 0d75eaca..d06d6cf5 100644 --- a/_extensions/s2_blog/Controller/BlogController.php +++ b/_extensions/s2_blog/Controller/BlogController.php @@ -17,6 +17,8 @@ use S2\Cms\Template\HtmlTemplate; use S2\Cms\Template\HtmlTemplateProvider; use S2\Cms\Template\Viewer; +use s2_extensions\s2_blog\BlogUrlBuilder; +use s2_extensions\s2_blog\CalendarBuilder; use s2_extensions\s2_blog\Lib; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -24,25 +26,20 @@ abstract class BlogController implements ControllerInterface { protected string $template_id = 'blog.php'; - protected string $blogPath; - protected string $blogTagsPath; abstract public function body(Request $request, HtmlTemplate $template): ?Response; public function __construct( protected DbLayer $dbLayer, + protected CalendarBuilder $calendarBuilder, + protected BlogUrlBuilder $blogUrlBuilder, protected ArticleProvider $articleProvider, protected UrlBuilder $urlBuilder, protected HtmlTemplateProvider $templateProvider, protected Viewer $viewer, - protected string $tagsUrl, - protected string $blogUrl, // S2_BLOG_URL protected string $blogTitle, // S2_BLOG_TITLE ) { - $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 () { if (file_exists(__DIR__ . '/../lang/' . S2_LANGUAGE . '.php')) return require __DIR__ . '/../lang/' . S2_LANGUAGE . '.php'; @@ -61,7 +58,7 @@ public function handle(Request $request): Response ->putInPlaceholder('rss_link', [sprintf( '', s2_htmlencode(Lang::get('RSS link title', 's2_blog')), - $this->urlBuilder->link(str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . '/rss.xml') + $this->blogUrlBuilder->main() . 'rss.xml' )]) ; @@ -134,7 +131,7 @@ public function getPosts(array $additionalQueryParts, bool $sortAsc = true, stri $output = ''; foreach ($ids as $id) { $post = &$posts[$id]; - $link = $this->blogPath . date('Y/m/d/', $post['create_time']) . urlencode($post['url']); + $link = $this->blogUrlBuilder->postFromTimestamp((int)$post['create_time'], $post['url']); $post['link'] = $link; $post['title_link'] = $link; $post['time'] = s2_date_time($post['create_time']); @@ -172,8 +169,8 @@ public function blog_navigation(Request $request) // Last posts on the blog main page $s2_blog_navigation['last'] = array( - 'title' => sprintf(Lang::get('Nav last', 's2_blog'), S2_MAX_ITEMS ? S2_MAX_ITEMS : 10), - 'link' => S2_BLOG_PATH, + 'title' => sprintf(Lang::get('Nav last', 's2_blog'), S2_MAX_ITEMS ?: 10), + 'link' => $this->blogUrlBuilder->main(), ); // Check for favorite posts @@ -186,16 +183,17 @@ public function blog_navigation(Request $request) ($hook = s2_hook('fn_s2_blog_navigation_pre_is_favorite_qr')) ? eval($hook) : null; $result = $this->dbLayer->buildAndQuery($query); - if ($this->dbLayer->fetchRow($result)) + if ($this->dbLayer->fetchRow($result)) { $s2_blog_navigation['favorite'] = array( 'title' => Lang::get('Nav favorite', 's2_blog'), - 'link' => S2_BLOG_PATH . urlencode(S2_FAVORITE_URL) . '/', + 'link' => $this->blogUrlBuilder->favorite(), ); + } // Fetch important tags $s2_blog_navigation['tags_header'] = array( 'title' => Lang::get('Nav tags', 's2_blog'), - 'link' => S2_BLOG_TAGS_PATH, + 'link' => $this->blogUrlBuilder->tags(), ); $query = array( diff --git a/_extensions/s2_blog/Controller/BlogRss.php b/_extensions/s2_blog/Controller/BlogRss.php index dc41c898..2506ecc1 100644 --- a/_extensions/s2_blog/Controller/BlogRss.php +++ b/_extensions/s2_blog/Controller/BlogRss.php @@ -14,16 +14,17 @@ use Lang; use S2\Cms\Controller\Rss; use S2\Cms\Template\Viewer; +use s2_extensions\s2_blog\BlogUrlBuilder; use s2_extensions\s2_blog\Lib; readonly class BlogRss extends Rss { public function __construct( + protected BlogUrlBuilder $blogUrlBuilder, protected Viewer $viewer, protected string $baseUrl, protected string $webmaster, protected string $siteName, - protected string $blogUrl, protected string $blogTitle, ) { @@ -53,7 +54,7 @@ protected function content(): array ], 's2_blog')), 'time' => $post['create_time'], 'modify_time' => $post['modify_time'], - 'rel_path' => str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . date('/Y/m/d/', $post['create_time']) . urlencode($post['url']), + 'rel_path' => $this->blogUrlBuilder->postFromTimestamp($post['create_time'], $post['url']), 'author' => $post['author'], ]; } @@ -68,7 +69,7 @@ protected function title(): string protected function link(): string { - return str_replace(urlencode('/'), '/', urlencode($this->blogUrl)) . '/'; + return $this->blogUrlBuilder->main(); } protected function description(): string diff --git a/_extensions/s2_blog/Controller/DayPageController.php b/_extensions/s2_blog/Controller/DayPageController.php index e81a3f82..418d5a26 100644 --- a/_extensions/s2_blog/Controller/DayPageController.php +++ b/_extensions/s2_blog/Controller/DayPageController.php @@ -7,11 +7,12 @@ * @package s2_blog */ +declare(strict_types=1); + namespace s2_extensions\s2_blog\Controller; use Lang; use S2\Cms\Template\HtmlTemplate; -use s2_extensions\s2_blog\Lib; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -19,20 +20,18 @@ class DayPageController extends BlogController { public function body(Request $request, HtmlTemplate $template): ?Response { - $params = $request->attributes->all(); - - $year = $params['year']; - $month = $params['month']; - $day = $params['day']; + $year = (int)($textYear = $request->attributes->get('year')); + $month = (int)($textMonth = $request->attributes->get('month')); + $day = (int)($textDay = $request->attributes->get('day')); if ($template->hasPlaceholder('')) { - $template->registerPlaceholder('', Lib::calendar($year, $month, $day)); + $template->registerPlaceholder('', $this->calendarBuilder->calendar($year, $month, $day)); } $template->putInPlaceholder('title', ''); $startTime = mktime(0, 0, 0, $month, $day, $year); - $endTime = mktime(0, 0, 0, $month, $day + 1, $year); + $endTime = $startTime + 60 * 60 * 24; $output = $this->getPosts([ 'WHERE' => 'p.create_time < ' . $endTime . ' AND p.create_time >= ' . $startTime @@ -45,18 +44,18 @@ public function body(Request $request, HtmlTemplate $template): ?Response $template ->putInPlaceholder('text', $output) - ->setLink('up', $this->blogPath . date('Y/m/', $startTime)) + ->setLink('up', $this->blogUrlBuilder->monthFromTimestamp($startTime)) ->putInPlaceholder('head_title', s2_date($startTime)) ; $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); - if ($this->blogUrl !== '') { - $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); + if (!$this->blogUrlBuilder->blogIsOnTheSiteRoot()) { + $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogUrlBuilder->main()); } $template - ->addBreadCrumb($year, $this->blogPath . $year . '/') - ->addBreadCrumb($month, $this->blogPath . $year . '/' . $month . '/') - ->addBreadCrumb($day) + ->addBreadCrumb($textYear, $this->blogUrlBuilder->year($year)) + ->addBreadCrumb($textMonth, $this->blogUrlBuilder->month($year, $month)) + ->addBreadCrumb($textDay) ; return null; diff --git a/_extensions/s2_blog/Controller/FavoritePageController.php b/_extensions/s2_blog/Controller/FavoritePageController.php index 34b1df06..892e83dc 100644 --- a/_extensions/s2_blog/Controller/FavoritePageController.php +++ b/_extensions/s2_blog/Controller/FavoritePageController.php @@ -11,7 +11,6 @@ use Lang; use S2\Cms\Template\HtmlTemplate; -use s2_extensions\s2_blog\Lib; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -25,7 +24,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response } if ($template->hasPlaceholder('')) { - $template->registerPlaceholder('', Lib::calendar(date('Y'), date('m'), '0')); + $template->registerPlaceholder('', $this->calendarBuilder->calendar()); } $output = $this->getPosts([ @@ -40,8 +39,8 @@ public function body(Request $request, HtmlTemplate $template): ?Response // Bread crumbs $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); - if ($this->blogUrl !== '') { - $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); + if (!$this->blogUrlBuilder->blogIsOnTheSiteRoot()) { + $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogUrlBuilder->main()); } $template->addBreadCrumb(Lang::get('Favorite')); @@ -51,7 +50,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response ->putInPlaceholder('text', $output) ; - $template->setLink('up', $this->blogPath); + $template->setLink('up', $this->blogUrlBuilder->main()); return null; } diff --git a/_extensions/s2_blog/Controller/MainPageController.php b/_extensions/s2_blog/Controller/MainPageController.php index 76a451e1..5cce3aa5 100644 --- a/_extensions/s2_blog/Controller/MainPageController.php +++ b/_extensions/s2_blog/Controller/MainPageController.php @@ -16,6 +16,8 @@ use S2\Cms\Template\HtmlTemplate; use S2\Cms\Template\HtmlTemplateProvider; use S2\Cms\Template\Viewer; +use s2_extensions\s2_blog\BlogUrlBuilder; +use s2_extensions\s2_blog\CalendarBuilder; use s2_extensions\s2_blog\Lib; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -25,16 +27,16 @@ class MainPageController extends BlogController { public function __construct( DbLayer $dbLayer, + CalendarBuilder $calendarBuilder, + BlogUrlBuilder $blogUrlBuilder, ArticleProvider $articleProvider, UrlBuilder $urlBuilder, HtmlTemplateProvider $templateProvider, Viewer $viewer, - string $tagsUrl, - string $blogUrl, string $blogTitle, private readonly int $itemsPerPage, ) { - parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); + parent::__construct($dbLayer, $calendarBuilder, $blogUrlBuilder, $articleProvider, $urlBuilder, $templateProvider, $viewer, $blogTitle); } public function handle(Request $request): Response @@ -57,7 +59,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response } if ($template->hasPlaceholder('')) { - $template->registerPlaceholder('', Lib::calendar(date('Y'), date('m'), '0')); + $template->registerPlaceholder('', $this->calendarBuilder->calendar()); } $postsPerPage = $this->itemsPerPage ?: 10; @@ -76,13 +78,13 @@ public function body(Request $request, HtmlTemplate $template): ?Response $paging = ''; if ($skipLastPostsNum > 0) { - $prevLink = $this->blogPath . ($skipLastPostsNum > $postsPerPage ? 'skip/' . ($skipLastPostsNum - $postsPerPage) : ''); + $prevLink = $this->blogUrlBuilder->main() . ($skipLastPostsNum > $postsPerPage ? 'skip/' . ($skipLastPostsNum - $postsPerPage) : ''); $template->setLink('prev', $prevLink); $paging = '' . Lang::get('Here') . ' '; // TODO think about back_forward } if ($i > $postsPerPage) { - $nextLink = S2_BLOG_PATH . 'skip/' . ($skipLastPostsNum + $postsPerPage); + $nextLink = $this->blogUrlBuilder->main() . 'skip/' . ($skipLastPostsNum + $postsPerPage); $template->setLink('next', $nextLink); $paging .= '' . Lang::get('There') . ''; } @@ -94,15 +96,15 @@ public function body(Request $request, HtmlTemplate $template): ?Response $template->putInPlaceholder('text', $output); $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); - if ($this->blogUrl !== '') { - $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $skipLastPostsNum > 0 ? $this->blogPath : null); + if (!$this->blogUrlBuilder->blogIsOnTheSiteRoot()) { + $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $skipLastPostsNum > 0 ? $this->blogUrlBuilder->main() : null); } if ($skipLastPostsNum > 0) { - $template->setLink('up', $this->blogPath); + $template->setLink('up', $this->blogUrlBuilder->main()); } else { $template->putInPlaceholder('meta_description', $this->blogTitle); - if ($this->blogUrl !== '') { + if (!$this->blogUrlBuilder->blogIsOnTheSiteRoot()) { $template->setLink('up', $this->urlBuilder->link('/')); } } diff --git a/_extensions/s2_blog/Controller/MonthPageController.php b/_extensions/s2_blog/Controller/MonthPageController.php index 141fae11..0ddc9fb7 100644 --- a/_extensions/s2_blog/Controller/MonthPageController.php +++ b/_extensions/s2_blog/Controller/MonthPageController.php @@ -16,53 +16,52 @@ use S2\Cms\Template\HtmlTemplate; use S2\Cms\Template\HtmlTemplateProvider; use S2\Cms\Template\Viewer; -use s2_extensions\s2_blog\Lib; +use s2_extensions\s2_blog\BlogUrlBuilder; +use s2_extensions\s2_blog\CalendarBuilder; 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, - string $blogUrl, - string $blogTitle, - private readonly string $startYear, + DbLayer $dbLayer, + CalendarBuilder $calendarBuilder, + BlogUrlBuilder $blogUrlBuilder, + ArticleProvider $articleProvider, + UrlBuilder $urlBuilder, + HtmlTemplateProvider $templateProvider, + Viewer $viewer, + string $blogTitle, + private readonly int $startYear, ) { - parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); + parent::__construct($dbLayer, $calendarBuilder, $blogUrlBuilder, $articleProvider, $urlBuilder, $templateProvider, $viewer, $blogTitle); } public function body(Request $request, HtmlTemplate $template): ?Response { - $params = $request->attributes->all(); - - $year = $params['year']; - $month = $params['month']; + $year = $request->attributes->get('year'); + $month = $request->attributes->get('month'); if ($template->hasPlaceholder('')) { - $template->registerPlaceholder('', Lib::calendar($year, $month, 0)); + $template->registerPlaceholder('', $this->calendarBuilder->calendar((int)$year, (int)$month, 0)); } $template->putInPlaceholder('title', ''); - $startTime = mktime(0, 0, 0, $month, 1, $year); - $endTime = mktime(0, 0, 0, $month + 1, 1, $year); - $prevTime = mktime(0, 0, 0, $month - 1, 1, $year); + $startTime = mktime(0, 0, 0, (int)$month, 1, (int)$year); + $endTime = mktime(0, 0, 0, (int)$month + 1, 1, (int)$year); + $prevTime = mktime(0, 0, 0, (int)$month - 1, 1, (int)$year); - $template->setLink('up', $this->blogPath . date('Y/', $startTime)); + $template->setLink('up', $this->blogUrlBuilder->year((int)$year)); $paging = ''; if ($prevTime >= mktime(0, 0, 0, 1, 1, $this->startYear)) { - $prevLink = $this->blogPath . date('Y/m/', $prevTime); + $prevLink = $this->blogUrlBuilder->monthFromTimestamp($prevTime); $template->setLink('prev', $prevLink); $paging = '' . Lang::get('Here') . ' '; } if ($endTime < time()) { - $nextLink = $this->blogPath . date('Y/m/', $endTime); + $nextLink = $this->blogUrlBuilder->monthFromTimestamp($endTime); $template->setLink('next', $nextLink); $paging .= '' . Lang::get('There') . ''; // TODO think about back_forward template @@ -87,11 +86,11 @@ public function body(Request $request, HtmlTemplate $template): ?Response ; $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); - if ($this->blogUrl !== '') { - $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); + if (!$this->blogUrlBuilder->blogIsOnTheSiteRoot()) { + $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogUrlBuilder->main()); } $template - ->addBreadCrumb($year, $this->blogPath . $year . '/') + ->addBreadCrumb($year, $this->blogUrlBuilder->year((int)$year)) ->addBreadCrumb($month) ; diff --git a/_extensions/s2_blog/Controller/PostPageController.php b/_extensions/s2_blog/Controller/PostPageController.php index 87209c39..236c021f 100644 --- a/_extensions/s2_blog/Controller/PostPageController.php +++ b/_extensions/s2_blog/Controller/PostPageController.php @@ -18,7 +18,8 @@ use S2\Cms\Template\HtmlTemplateProvider; use S2\Cms\Template\Viewer; use S2\Rose\Entity\ExternalId; -use s2_extensions\s2_blog\Lib; +use s2_extensions\s2_blog\BlogUrlBuilder; +use s2_extensions\s2_blog\CalendarBuilder; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -26,52 +27,55 @@ class PostPageController extends BlogController { public function __construct( DbLayer $dbLayer, + CalendarBuilder $calendarBuilder, + BlogUrlBuilder $blogUrlBuilder, ArticleProvider $articleProvider, UrlBuilder $urlBuilder, private readonly RecommendationProvider $recommendationProvider, HtmlTemplateProvider $templateProvider, Viewer $viewer, - string $tagsUrl, - string $blogUrl, string $blogTitle, protected bool $showComments, ) { - parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); + parent::__construct($dbLayer, $calendarBuilder, $blogUrlBuilder, $articleProvider, $urlBuilder, $templateProvider, $viewer, $blogTitle); } public function body(Request $request, HtmlTemplate $template): ?Response { - $params = $request->attributes->all(); + $year = (int)($textYear = $request->attributes->get('year')); + $month = (int)($textMonth = $request->attributes->get('month')); // Note: "01" is not parsed with getInt() correctly + $day = (int)($textDay = $request->attributes->get('day')); + $url = $request->attributes->get('url'); if ($template->hasPlaceholder('')) { - $template->registerPlaceholder('', Lib::calendar($params['year'], $params['month'], $params['day'], $params['url'])); + $template->registerPlaceholder('', $this->calendarBuilder->calendar($year, $month, $day, $url)); } $template->putInPlaceholder('title', ''); - if (($result = $this->get_post($request, $template, $params['year'], $params['month'], $params['day'], $params['url'])) !== null) { + if (($result = $this->get_post($request, $template, $year, $month, $day, $url)) !== null) { return $result; } $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); - if ($this->blogUrl !== '') { - $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); + if (!$this->blogUrlBuilder->blogIsOnTheSiteRoot()) { + $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogUrlBuilder->main()); } $template - ->addBreadCrumb($params['year'], $this->blogPath . $params['year'] . '/') - ->addBreadCrumb($params['month'], $this->blogPath . $params['year'] . '/' . $params['month'] . '/') - ->addBreadCrumb($params['day'], $this->blogPath . $params['year'] . '/' . $params['month'] . '/' . $params['day'] . '/') + ->addBreadCrumb($textYear, $this->blogUrlBuilder->year($year)) + ->addBreadCrumb($textMonth, $this->blogUrlBuilder->month($year, $month)) + ->addBreadCrumb($textDay, $this->blogUrlBuilder->day($year, $month, $day)) ; return null; } - private function get_post(Request $request, HtmlTemplate $template, $year, $month, $day, $url): ?Response + private function get_post(Request $request, HtmlTemplate $template, int $year, int $month, int $day, string $url): ?Response { $start_time = mktime(0, 0, 0, $month, $day, $year); - $end_time = mktime(0, 0, 0, $month, $day + 1, $year); + $end_time = $start_time + 86400; - $template->setLink('up', $this->blogPath . date('Y/m/d/', $start_time)); + $template->setLink('up', $this->blogUrlBuilder->day($year, $month, $day)); $sub_query = [ 'SELECT' => 'u.name', @@ -99,7 +103,7 @@ private function get_post(Request $request, HtmlTemplate $template, $year, $mont $post_id = $row['id']; $label = $row['label']; - $template->putInPlaceholder('canonical_path', $this->blogPath . date('Y/m/d/', $row['create_time']) . $row['url']); + $template->putInPlaceholder('canonical_path', $this->blogUrlBuilder->postFromTimestamp((int)$row['create_time'], $row['url'])); $is_back_forward = $template->hasPlaceholder(''); @@ -141,7 +145,7 @@ private function get_post(Request $request, HtmlTemplate $template, $year, $mont while ($result && $row1 = $this->dbLayer->fetchAssoc($result)) { $post_info = [ 'title' => $row1['title'], - 'link' => S2_BLOG_PATH . date('Y/m/d/', $row1['create_time']) . urlencode($row1['url']), + 'link' => $this->blogUrlBuilder->postFromTimestamp((int)$row1['create_time'], $row1['url']), ]; if ($row1['type'] === 'label') { @@ -178,7 +182,7 @@ private function get_post(Request $request, HtmlTemplate $template, $year, $mont while ($tag = $this->dbLayer->fetchAssoc($result)) { $tags[] = [ 'title' => $tag['name'], - 'link' => $this->blogTagsPath . urlencode($tag['url']) . '/', + 'link' => $this->blogUrlBuilder->tag($tag['url']), ]; } diff --git a/_extensions/s2_blog/Controller/Sitemap.php b/_extensions/s2_blog/Controller/Sitemap.php index 1a11b4e4..b154259e 100644 --- a/_extensions/s2_blog/Controller/Sitemap.php +++ b/_extensions/s2_blog/Controller/Sitemap.php @@ -14,14 +14,15 @@ use S2\Cms\Model\UrlBuilder; use S2\Cms\Pdo\DbLayer; use S2\Cms\Template\Viewer; +use s2_extensions\s2_blog\BlogUrlBuilder; class Sitemap extends \S2\Cms\Controller\Sitemap { public function __construct( - protected DbLayer $dbLayer, - protected UrlBuilder $urlBuilder, - protected Viewer $viewer, - private readonly string $blogUrl + protected DbLayer $dbLayer, + protected BlogUrlBuilder $blogUrlBuilder, + protected UrlBuilder $urlBuilder, + protected Viewer $viewer, ) { } @@ -40,7 +41,7 @@ protected function getItems(): array $posts = []; while ($row = $this->dbLayer->fetchAssoc($result)) { - $row['rel_path'] = $this->blogUrl . date('/Y/m/d/', $row['time']) . urlencode($row['url']); + $row['rel_path'] = $this->blogUrlBuilder->postFromTimestamp($row['time'], $row['url']); $posts[] = $row; } diff --git a/_extensions/s2_blog/Controller/TagPageController.php b/_extensions/s2_blog/Controller/TagPageController.php index 05201c3d..ddf94990 100644 --- a/_extensions/s2_blog/Controller/TagPageController.php +++ b/_extensions/s2_blog/Controller/TagPageController.php @@ -17,7 +17,8 @@ use S2\Cms\Template\HtmlTemplate; use S2\Cms\Template\HtmlTemplateProvider; use S2\Cms\Template\Viewer; -use s2_extensions\s2_blog\Lib; +use s2_extensions\s2_blog\BlogUrlBuilder; +use s2_extensions\s2_blog\CalendarBuilder; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -27,16 +28,16 @@ class TagPageController extends BlogController { public function __construct( DbLayer $dbLayer, + CalendarBuilder $calendarBuilder, + BlogUrlBuilder $blogUrlBuilder, ArticleProvider $articleProvider, UrlBuilder $urlBuilder, HtmlTemplateProvider $templateProvider, Viewer $viewer, - string $tagsUrl, - string $blogUrl, string $blogTitle, private readonly bool $useHierarchy ) { - parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); + parent::__construct($dbLayer, $calendarBuilder, $blogUrlBuilder, $articleProvider, $urlBuilder, $templateProvider, $viewer, $blogTitle); } public function body(Request $request, HtmlTemplate $template): ?Response @@ -44,7 +45,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response $params = $request->attributes->all(); if ($template->hasPlaceholder('')) { - $template->registerPlaceholder('', Lib::calendar(date('Y'), date('m'), '0')); + $template->registerPlaceholder('', $this->calendarBuilder->calendar()); } $tag = $params['tag']; @@ -63,7 +64,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response [$tagId, $tagDescription, $tagName, $tagUrl] = $row; if ($params['slash'] !== '/') { - return new RedirectResponse($this->blogTagsPath . urlencode($tagUrl) . '/', Response::HTTP_MOVED_PERMANENTLY); + return new RedirectResponse($this->blogUrlBuilder->tag($tagUrl), Response::HTTP_MOVED_PERMANENTLY); } $art_links = $this->articles_by_tag($tagId); @@ -90,10 +91,10 @@ public function body(Request $request, HtmlTemplate $template): ?Response } $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); - if ($this->blogUrl !== '') { - $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); + if (!$this->blogUrlBuilder->blogIsOnTheSiteRoot()) { + $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogUrlBuilder->main()); } - $template->addBreadCrumb(Lang::get('Tags'), $this->blogTagsPath); + $template->addBreadCrumb(Lang::get('Tags'), $this->blogUrlBuilder->tags()); $template->addBreadCrumb($tagName); $template @@ -102,7 +103,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response ->putInPlaceholder('text', $tagDescription . $output) ; - $template->setLink('up', $this->blogTagsPath); + $template->setLink('up', $this->blogUrlBuilder->tags()); return null; } diff --git a/_extensions/s2_blog/Controller/TagsPageController.php b/_extensions/s2_blog/Controller/TagsPageController.php index f7e7ce2e..43ba18d8 100644 --- a/_extensions/s2_blog/Controller/TagsPageController.php +++ b/_extensions/s2_blog/Controller/TagsPageController.php @@ -11,14 +11,13 @@ use Lang; use S2\Cms\Template\HtmlTemplate; -use s2_extensions\s2_blog\Lib; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class TagsPageController 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($this->urlBuilder->link($request->getPathInfo() . '/'), Response::HTTP_MOVED_PERMANENTLY); @@ -26,33 +25,33 @@ public function body (Request $request, HtmlTemplate $template): ?Response $template->registerPlaceholder('', ''); - if ($template->hasPlaceholder('')) { - $template->registerPlaceholder('', Lib::calendar(date('Y'), date('m'), '0')); + if ($template->hasPlaceholder('')) { + $template->registerPlaceholder('', $this->calendarBuilder->calendar()); } - $query = [ - 'SELECT' => 'tag_id, name, url', - 'FROM' => 'tags' + $query = [ + 'SELECT' => 'tag_id, name, url', + 'FROM' => 'tags' ]; $result = $this->dbLayer->buildAndQuery($query); $tag_name = $tag_url = $tag_count = []; while ($row = $this->dbLayer->fetchAssoc($result)) { - $tag_name[$row['tag_id']] = $row['name']; - $tag_url[$row['tag_id']] = $row['url']; + $tag_name[$row['tag_id']] = $row['name']; + $tag_url[$row['tag_id']] = $row['url']; $tag_count[$row['tag_id']] = 0; } - $query = [ - 'SELECT' => 'pt.tag_id', - 'FROM' => 's2_blog_post_tag AS pt', - 'JOINS' => [ + $query = [ + 'SELECT' => 'pt.tag_id', + 'FROM' => 's2_blog_post_tag AS pt', + 'JOINS' => [ [ - 'INNER JOIN' => 's2_blog_posts AS p', - 'ON' => 'p.id = pt.post_id' + 'INNER JOIN' => 's2_blog_posts AS p', + 'ON' => 'p.id = pt.post_id' ] ], - 'WHERE' => 'p.published = 1' + 'WHERE' => 'p.published = 1' ]; $result = $this->dbLayer->buildAndQuery($query); @@ -76,17 +75,17 @@ public function body (Request $request, HtmlTemplate $template): ?Response $template->putInPlaceholder('text', $this->viewer->render('tags_list', ['tags' => $tags])); $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); - if ($this->blogUrl !== '') { - $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); + if (!$this->blogUrlBuilder->blogIsOnTheSiteRoot()) { + $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogUrlBuilder->main()); } $template->addBreadCrumb(Lang::get('Tags')); $template ->putInPlaceholder('head_title', Lang::get('Tags')) ->putInPlaceholder('title', Lang::get('Tags')) - ->setLink('up', $this->blogPath) + ->setLink('up', $this->blogUrlBuilder->main()) ; return null; - } + } } diff --git a/_extensions/s2_blog/Controller/YearPageController.php b/_extensions/s2_blog/Controller/YearPageController.php index e6c903e2..485a6181 100644 --- a/_extensions/s2_blog/Controller/YearPageController.php +++ b/_extensions/s2_blog/Controller/YearPageController.php @@ -16,7 +16,8 @@ use S2\Cms\Template\HtmlTemplate; use S2\Cms\Template\HtmlTemplateProvider; use S2\Cms\Template\Viewer; -use s2_extensions\s2_blog\Lib; +use s2_extensions\s2_blog\BlogUrlBuilder; +use s2_extensions\s2_blog\CalendarBuilder; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -24,27 +25,25 @@ class YearPageController extends BlogController { public function __construct( - DbLayer $dbLayer, - ArticleProvider $articleProvider, - UrlBuilder $urlBuilder, - HtmlTemplateProvider $templateProvider, - Viewer $viewer, - string $tagsUrl, - string $blogUrl, - string $blogTitle, - private readonly string $startYear, + DbLayer $dbLayer, + CalendarBuilder $calendarBuilder, + BlogUrlBuilder $blogUrlBuilder, + ArticleProvider $articleProvider, + UrlBuilder $urlBuilder, + HtmlTemplateProvider $templateProvider, + Viewer $viewer, + string $blogTitle, + private readonly int $startYear, ) { - parent::__construct($dbLayer, $articleProvider, $urlBuilder, $templateProvider, $viewer, $tagsUrl, $blogUrl, $blogTitle); + parent::__construct($dbLayer, $calendarBuilder, $blogUrlBuilder, $articleProvider, $urlBuilder, $templateProvider, $viewer, $blogTitle); } public function body(Request $request, HtmlTemplate $template): ?Response { - $params = $request->attributes->all(); - - $year = $params['year']; + $year = $request->attributes->getInt('year'); if ($template->hasPlaceholder('')) { - $template->registerPlaceholder('', Lib::calendar($year, '', 0)); + $template->registerPlaceholder('', ''); } $start_time = mktime(0, 0, 0, 1, 1, $year); @@ -54,14 +53,14 @@ public function body(Request $request, HtmlTemplate $template): ?Response $template->putInPlaceholder('head_title', $title); $pageTitle = $title; - $template->setLink('up', $this->blogPath); + $template->setLink('up', $this->blogUrlBuilder->main()); if ($year > $this->startYear) { - $pageTitle = ' ' . $pageTitle; - $template->setLink('prev', $this->blogPath . ($year - 1) . '/'); + $pageTitle = ' ' . $pageTitle; + $template->setLink('prev', $this->blogUrlBuilder->year($year - 1) . '/'); } if ($year < date('Y')) { - $pageTitle .= ' '; - $template->setLink('next', $this->blogPath . ($year + 1) . '/'); + $pageTitle .= ' '; + $template->setLink('next', $this->blogUrlBuilder->year($year + 1) . '/'); } $template->putInPlaceholder('title', $pageTitle); @@ -79,7 +78,7 @@ public function body(Request $request, HtmlTemplate $template): ?Response $content = []; for ($i = 1; $i <= 12; $i++) { - $content[] = Lib::calendar($year, Lib::extend_number($i), '-1', '', $dayUrlsArray[$i]); + $content[] = $this->calendarBuilder->calendar($year, $i, null, '', $dayUrlsArray[$i]); } $template->putInPlaceholder('text', $this->viewer->render('year', [ @@ -87,8 +86,8 @@ public function body(Request $request, HtmlTemplate $template): ?Response ], 's2_blog')); $template->addBreadCrumb($this->articleProvider->mainPageTitle(), $this->urlBuilder->link('/')); - if ($this->blogUrl !== '') { - $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogPath); + if (!$this->blogUrlBuilder->blogIsOnTheSiteRoot()) { + $template->addBreadCrumb(Lang::get('Blog', 's2_blog'), $this->blogUrlBuilder->main()); } $template->addBreadCrumb($year); diff --git a/_extensions/s2_blog/Extension.php b/_extensions/s2_blog/Extension.php index a4e7076c..fc8e32ab 100644 --- a/_extensions/s2_blog/Extension.php +++ b/_extensions/s2_blog/Extension.php @@ -39,17 +39,36 @@ class Extension implements ExtensionInterface { public function buildContainer(Container $container): void { + $container->set(BlogUrlBuilder::class, function (Container $container) { + /** @var DynamicConfigProvider $provider */ + $provider = $container->get(DynamicConfigProvider::class); + return new BlogUrlBuilder( + $container->get(UrlBuilder::class), + $provider->get('S2_TAGS_URL'), + $provider->get('S2_FAVORITE_URL'), + $provider->get('S2_BLOG_URL'), + ); + }); + $container->set(CalendarBuilder::class, function (Container $container) { + /** @var DynamicConfigProvider $provider */ + $provider = $container->get(DynamicConfigProvider::class); + return new CalendarBuilder( + $container->get(DbLayer::class), + $container->get(BlogUrlBuilder::class), + (int) $provider->get('S2_START_YEAR'), + ); + }); $container->set(MainPageController::class, function (Container $container) { /** @var DynamicConfigProvider $provider */ $provider = $container->get(DynamicConfigProvider::class); return new MainPageController( $container->get(DbLayer::class), + $container->get(CalendarBuilder::class), + $container->get(BlogUrlBuilder::class), $container->get(ArticleProvider::class), $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), - $provider->get('S2_TAGS_URL'), - $provider->get('S2_BLOG_URL'), $provider->get('S2_BLOG_TITLE'), (int)$provider->get('S2_MAX_ITEMS') ); @@ -59,12 +78,12 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new DayPageController( $container->get(DbLayer::class), + $container->get(CalendarBuilder::class), + $container->get(BlogUrlBuilder::class), $container->get(ArticleProvider::class), $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), - $provider->get('S2_TAGS_URL'), - $provider->get('S2_BLOG_URL'), $provider->get('S2_BLOG_TITLE'), ); }); @@ -73,14 +92,14 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new MonthPageController( $container->get(DbLayer::class), + $container->get(CalendarBuilder::class), + $container->get(BlogUrlBuilder::class), $container->get(ArticleProvider::class), $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), - $provider->get('S2_TAGS_URL'), - $provider->get('S2_BLOG_URL'), $provider->get('S2_BLOG_TITLE'), - $provider->get('S2_START_YEAR'), + (int)$provider->get('S2_START_YEAR'), ); }); $container->set(YearPageController::class, function (Container $container) { @@ -88,14 +107,14 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new YearPageController( $container->get(DbLayer::class), + $container->get(CalendarBuilder::class), + $container->get(BlogUrlBuilder::class), $container->get(ArticleProvider::class), $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), - $provider->get('S2_TAGS_URL'), - $provider->get('S2_BLOG_URL'), $provider->get('S2_BLOG_TITLE'), - $provider->get('S2_START_YEAR'), + (int)$provider->get('S2_START_YEAR'), ); }); $container->set(PostPageController::class, function (Container $container) { @@ -103,13 +122,13 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new PostPageController( $container->get(DbLayer::class), + $container->get(CalendarBuilder::class), + $container->get(BlogUrlBuilder::class), $container->get(ArticleProvider::class), $container->get(UrlBuilder::class), $container->get(RecommendationProvider::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), - $provider->get('S2_TAGS_URL'), - $provider->get('S2_BLOG_URL'), $provider->get('S2_BLOG_TITLE'), $provider->get('S2_SHOW_COMMENTS') === '1', ); @@ -119,12 +138,12 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new TagsPageController( $container->get(DbLayer::class), + $container->get(CalendarBuilder::class), + $container->get(BlogUrlBuilder::class), $container->get(ArticleProvider::class), $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), - $provider->get('S2_TAGS_URL'), - $provider->get('S2_BLOG_URL'), $provider->get('S2_BLOG_TITLE'), ); }); @@ -133,12 +152,12 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new TagPageController( $container->get(DbLayer::class), + $container->get(CalendarBuilder::class), + $container->get(BlogUrlBuilder::class), $container->get(ArticleProvider::class), $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), - $provider->get('S2_TAGS_URL'), - $provider->get('S2_BLOG_URL'), $provider->get('S2_BLOG_TITLE'), $provider->get('S2_USE_HIERARCHY') === '1', ); @@ -148,12 +167,12 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new FavoritePageController( $container->get(DbLayer::class), + $container->get(CalendarBuilder::class), + $container->get(BlogUrlBuilder::class), $container->get(ArticleProvider::class), $container->get(UrlBuilder::class), $container->get(HtmlTemplateProvider::class), $container->get(Viewer::class), - $provider->get('S2_TAGS_URL'), - $provider->get('S2_BLOG_URL'), $provider->get('S2_BLOG_TITLE'), ); }); @@ -161,11 +180,11 @@ public function buildContainer(Container $container): void /** @var DynamicConfigProvider $provider */ $provider = $container->get(DynamicConfigProvider::class); return new BlogRss( + $container->get(BlogUrlBuilder::class), $container->get('strict_viewer'), $container->getParameter('base_url'), $provider->get('S2_WEBMASTER'), $provider->get('S2_SITE_NAME'), - $provider->get('S2_BLOG_URL'), $provider->get('S2_BLOG_TITLE'), ); }); @@ -174,9 +193,9 @@ public function buildContainer(Container $container): void $provider = $container->get(DynamicConfigProvider::class); return new Sitemap( $container->get(DbLayer::class), + $container->get(BlogUrlBuilder::class), $container->get(UrlBuilder::class), $container->get('strict_viewer'), - $provider->get('S2_BLOG_URL'), ); }); } diff --git a/_extensions/s2_blog/Lib.php b/_extensions/s2_blog/Lib.php index 3902523a..de70653d 100644 --- a/_extensions/s2_blog/Lib.php +++ b/_extensions/s2_blog/Lib.php @@ -8,125 +8,11 @@ */ namespace s2_extensions\s2_blog; -use \Lang; -use S2\Cms\Pdo\DbLayer; +use S2\Cms\Pdo\DbLayer; class Lib { - // '1' -> '01' - public static function extend_number ($n) - { - return \strlen($n) === 1 ? '0'.$n : $n; - } - - public static function calendar($year, $month, $day, $url = '', $dayUrls = null) - { - /** @var DbLayer $s2_db */ - $s2_db = \Container::get(DbLayer::class); - - if ($month === '') - $month = 1; - - $start_time = mktime(0, 0, 0, $month, 1, $year); - $end_time = mktime(0, 0, 0, $month + 1, 1, $year); - - // Dealing with week days - $n = (int)date('w', $start_time); - if (Lang::get('Sunday starts week', 's2_blog') !== '1') { - --$n; - if ($n === -1) { - $n = 6; - } - } - - // How many days have the month? - $day_count = (int) date('j', mktime(0, 0, 0, $month + 1, 0, $year)); // day = 0 - - // Flags for the days when posts have been written - if ($dayUrls === null) { - $dayUrls = []; - $query = array( - 'SELECT' => 'create_time, url', - 'FROM' => 's2_blog_posts', - 'WHERE' => 'create_time < '.$end_time.' AND create_time >= '.$start_time.' AND published = 1' - ); - ($hook = s2_hook('fn_s2_blog_calendar_pre_get_days_qr')) ? eval($hook) : null; - $result = $s2_db->buildAndQuery($query); - while ($row = $s2_db->fetchRow($result)) { - $dayUrls[1 + (int)(($row[0] - $start_time) / 86400)][] = $row[1]; - } - } - - // Header - $month_name = \Lang::month($month); - if ($day == '-1') - { - // One of 12 year tables - if ($start_time < time()) { - $month_name = '' . $month_name . ''; - } - $header = ''.$month_name.''; - } - else - { - if ($day != '') - $month_name = ''.$month_name.''; - - // Links in the header - $next_month = $end_time < time() ? '' : '→'; - - $prev_time = mktime(0, 0, 0, $month - 1, 1, $year); - $prev_month = $prev_time >= mktime(0, 0, 0, 1, 1, S2_START_YEAR) ? '' : '←'; - - $header = ''.$prev_month.'' - .$month_name.', '.$year.''.$next_month.''; - } - - // Titles - $output = ''.$header.''; - - // Empty cells before - for ($i = 0; $i < $n; $i++) - $output .= ''; - - // Days - for ($i = 1; $i <= $day_count; $i++) - { - $n++; - if (!isset($dayUrls[$i])) { - // No posts - $b = $i; - } elseif (\count($dayUrls[$i]) !== 1) { - // Several posts, link to the day page - $b = '' . $i . ''; - } else { - // One post, link to it - $b = '' . $i . ''; - } - $classes = array(); - if ($i == $day) - // Current day - $classes[] = 'cur'; - if ($n % 7 == 0 || ($n % 7 == 6) && Lang::get('Sunday starts week', 's2_blog') != '1' || ($n % 7 == 1) && Lang::get('Sunday starts week', 's2_blog') == '1') - // Weekend - $classes[] = 'sun'; - $output .= ''.($i == $day && !$url ? $i : $b).''; - if (!($n % 7) && ($i != $day_count)) - $output .=''; - } - - // Empty cells in the end - while ($n % 7) - { - $n++; - $output .= ''; - } - - $output .= '
'; - return $output; - } - // Returns an array containing info about 10 last posts public static function last_posts_array ($num_posts = 10, $skip = 0, $fake_last_post = false) {