127 lines
4.6 KiB
PHP
127 lines
4.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* This file is part of the package t3g/blog.
|
|
*
|
|
* For the full copyright and license information, please read the
|
|
* LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Evoweb\EwBloggy\Controller;
|
|
|
|
use DateTimeImmutable;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Evoweb\EwBloggy\Domain\Repository\PostRepository;
|
|
use Evoweb\EwBloggy\Pagination\BlogPagination;
|
|
use T3G\AgencyPack\Blog\Service\MetaTagService;
|
|
use T3G\AgencyPack\Blog\Utility\ArchiveUtility;
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
|
|
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
|
|
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
|
|
|
|
class PostController extends ActionController
|
|
{
|
|
public function __construct(protected PostRepository $postRepository)
|
|
{
|
|
}
|
|
|
|
protected function initializeView(): void
|
|
{
|
|
foreach (($this->settings['flexform'] ?? []) as $key => $value) {
|
|
if (!empty($value)) {
|
|
$this->settings[$key] = $value;
|
|
}
|
|
}
|
|
if (isset($this->settings['flexform'])) {
|
|
unset($this->settings['flexform']);
|
|
}
|
|
$this->view->assign('settings', $this->settings);
|
|
$this->view->assign('data', $this->request->getAttribute('currentContentObject')?->data ?? null);
|
|
}
|
|
|
|
/**
|
|
* Show a list of recent posts.
|
|
*/
|
|
public function listRecentPostsAction(int $currentPage = 1): ResponseInterface
|
|
{
|
|
$maximumItems = (int) ($this->settings['lists']['posts']['maximumDisplayedItems'] ?? 0);
|
|
$posts = (0 === $maximumItems)
|
|
? $this->postRepository->findAll()
|
|
: $this->postRepository->findAllWithLimit($maximumItems);
|
|
$pagination = $this->getPagination($posts, $currentPage);
|
|
|
|
$this->view->assign('posts', $posts);
|
|
$this->view->assign('pagination', $pagination);
|
|
return $this->htmlResponse();
|
|
}
|
|
|
|
/**
|
|
* Show a number of latest posts.
|
|
*/
|
|
public function listLatestPostsAction(): ResponseInterface
|
|
{
|
|
$limit = (int) ($this->settings['limit'] ?? 3);
|
|
$posts = $this->postRepository->findAllWithLimit($limit);
|
|
$this->view->assign('posts', $posts);
|
|
return $this->htmlResponse();
|
|
}
|
|
|
|
/**
|
|
* Show posts beyond a certain date
|
|
*/
|
|
public function listPostsByDateAction(
|
|
?int $year = null,
|
|
?int $month = null,
|
|
int $currentPage = 1
|
|
): ResponseInterface {
|
|
if ($year === null) {
|
|
$posts = $this->postRepository->findMonthsAndYearsWithPosts();
|
|
$this->view->assign('archiveData', ArchiveUtility::extractDataFromPosts($posts));
|
|
} else {
|
|
$dateTime = new DateTimeImmutable(sprintf('%d-%d-1', $year, $month ?? 1));
|
|
$posts = $this->postRepository->findByMonthAndYear($year, $month);
|
|
$pagination = $this->getPagination($posts, $currentPage);
|
|
$this->view->assign('type', 'bydate');
|
|
$this->view->assign('month', $month);
|
|
$this->view->assign('year', $year);
|
|
$this->view->assign('timestamp', $dateTime->getTimestamp());
|
|
$this->view->assign('posts', $posts);
|
|
$this->view->assign('pagination', $pagination);
|
|
$title = str_replace(
|
|
[
|
|
'###MONTH###',
|
|
'###MONTH_NAME###',
|
|
'###YEAR###',
|
|
],
|
|
[
|
|
$month,
|
|
$dateTime->format('F'),
|
|
$year,
|
|
],
|
|
(string) LocalizationUtility::translate('meta.title.listPostsByDate', 'ew_bloggy')
|
|
);
|
|
MetaTagService::set(MetaTagService::META_TITLE, (string) $title);
|
|
MetaTagService::set(
|
|
MetaTagService::META_DESCRIPTION,
|
|
(string) LocalizationUtility::translate('meta.description.listPostsByDate', 'ew_bloggy')
|
|
);
|
|
}
|
|
return $this->htmlResponse();
|
|
}
|
|
|
|
protected function getPagination(QueryResultInterface $objects, int $currentPage = 1): ?BlogPagination
|
|
{
|
|
$maximumNumberOfLinks = (int) ($this->settings['lists']['pagination']['maximumNumberOfLinks'] ?? 0);
|
|
$itemsPerPage = 10;
|
|
if ($this->request->getFormat() === 'html') {
|
|
$itemsPerPage = (int) ($this->settings['lists']['pagination']['itemsPerPage'] ?? 10);
|
|
}
|
|
|
|
$paginator = new QueryResultPaginator($objects, $currentPage, $itemsPerPage);
|
|
return new BlogPagination($paginator, $maximumNumberOfLinks);
|
|
}
|
|
}
|