Skip to content

Commit

Permalink
[BUGFIX] Resolve endless loop when calling archive without params (#35)
Browse files Browse the repository at this point in the history
Releases: master, 9.1, 9.0
Fixes: #27
  • Loading branch information
benjaminkott committed Feb 14, 2019
1 parent bceec79 commit a7cf507
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 107 deletions.
52 changes: 27 additions & 25 deletions Classes/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use T3G\AgencyPack\Blog\Domain\Repository\TagRepository;
use T3G\AgencyPack\Blog\Service\CacheService;
use T3G\AgencyPack\Blog\Service\MetaService;
use T3G\AgencyPack\Blog\Utility\ArchiveUtility;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
Expand Down Expand Up @@ -186,32 +187,33 @@ public function listRecentPostsAction(): void
*/
public function listPostsByDateAction(int $year = null, int $month = null): void
{
if (null === $year) {
// we need at least the year
$this->redirect('listRecentPosts');
}
$dateTime = new \DateTimeImmutable(sprintf('%d-%d-1', $year, $month ?? 1));
$posts = $this->postRepository->findByMonthAndYear($year, $month);
foreach ($posts as $post) {
$this->blogCacheService->addTagsForPost($post);
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);
foreach ($posts as $post) {
$this->blogCacheService->addTagsForPost($post);
}
$this->view->assignMultiple([
'month' => $month,
'year' => $year,
'timestamp' => $dateTime->getTimestamp(),
'posts' => $posts,
]);
$title = str_replace([
'###MONTH###',
'###MONTH_NAME###',
'###YEAR###',
], [
$month,
$dateTime->format('F'),
$year,
], LocalizationUtility::translate('meta.title.listPostsByDate', 'blog'));
MetaService::set(MetaService::META_TITLE, $title);
MetaService::set(MetaService::META_DESCRIPTION, LocalizationUtility::translate('meta.description.listPostsByDate', 'blog'));
}
$this->view->assignMultiple([
'month' => $month,
'year' => $year,
'timestamp' => $dateTime->getTimestamp(),
'posts' => $posts,
]);
$title = str_replace([
'###MONTH###',
'###MONTH_NAME###',
'###YEAR###',
], [
$month,
$dateTime->format('F'),
$year,
], LocalizationUtility::translate('meta.title.listPostsByDate', 'blog'));
MetaService::set(MetaService::META_TITLE, $title);
MetaService::set(MetaService::META_DESCRIPTION, LocalizationUtility::translate('meta.description.listPostsByDate', 'blog'));
}

/**
Expand Down
38 changes: 2 additions & 36 deletions Classes/Controller/WidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use T3G\AgencyPack\Blog\Domain\Repository\PostRepository;
use T3G\AgencyPack\Blog\Domain\Repository\TagRepository;
use T3G\AgencyPack\Blog\Service\CacheService;
use T3G\AgencyPack\Blog\Utility\ArchiveUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

Expand Down Expand Up @@ -189,45 +190,10 @@ public function commentsAction(): void
public function archiveAction(): void
{
$posts = $this->postRepository->findMonthsAndYearsWithPosts();
$this->view->assign('archiveData', $this->resortArchiveData($posts));
$this->view->assign('archiveData', ArchiveUtility::extractDataFromPosts($posts));
}

public function feedAction(): void
{
}

/**
* This method resort the database result and create a nested array
* in the form:
* [
* 2015 => [
* [
* 'year' => 2015,
* 'month' => 3,
* 'count' => 9
* 'timestamp' => 123456789
* ]
* ...
* ]
* ...
* ].
*
* @param array $data
* @return array
* @throws \Exception
*/
protected function resortArchiveData(array $data): array
{
$archiveData = [];
foreach ($data as $result) {
if (empty($archiveData[$result['year']])) {
$archiveData[$result['year']] = [];
}
$dateTime = new \DateTimeImmutable(sprintf('%d-%d-1', (int)$result['year'], (int)$result['month']));
$result['timestamp'] = $dateTime->getTimestamp();
$archiveData[$result['year']][] = $result;
}

return $archiveData;
}
}
49 changes: 49 additions & 0 deletions Classes/Utility/ArchiveUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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 T3G\AgencyPack\Blog\Utility;

class ArchiveUtility
{
/**
* This method extracts and sorts the database result and create a nested array
* in the form:
* [
* 2015 => [
* [
* 'year' => 2015,
* 'month' => 3,
* 'count' => 9
* 'timestamp' => 123456789
* ]
* ...
* ]
* ...
* ]
*
* @param array $data
* @return array
* @throws \Exception
*/
public static function extractDataFromPosts(array $data): array
{
$archiveData = [];
foreach ($data as $result) {
if (empty($archiveData[$result['year']])) {
$archiveData[$result['year']] = [];
}
$dateTime = new \DateTimeImmutable(sprintf('%d-%d-1', (int)$result['year'], (int)$result['month']));
$result['timestamp'] = $dateTime->getTimestamp();
$archiveData[$result['year']][] = $result;
}

return $archiveData;
}
}
50 changes: 50 additions & 0 deletions Resources/Private/Partials/Archive/Menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" xmlns:blog="http://typo3.org/ns/T3G/AgencyPack/Blog/ViewHelpers" data-namespace-typo3-fluid="true">
<f:if condition="{items}">
<ul class="archive">
<f:for each="{items}" as="months" key="year">
<f:if condition="{settings.widgets.archive.groupByYear} == 1">
<f:then>
<li class="archive-item archive-item-year archive-item-{year}">
<blog:link.archive class="archive-item-year-link" year="{year}" title="{year}">
{year}
</blog:link.archive>
<blog:link.archive class="archive-item-year-rss" rss="true" year="{year}" title="{year}">
<i class="fa fa-rss-square"></i>
</blog:link.archive>
<f:if condition="{settings.widgets.archive.groupByMonth} == 1">
<ul>
<f:for each="{months}" as="month">
<f:render section="Month" arguments="{settings: settings, year: year, month: month}" />
</f:for>
</ul>
</f:if>
</li>
</f:then>
<f:else>
<f:for each="{months}" as="month">
<f:render section="Month" arguments="{settings: settings, year: year, month: month}" />
</f:for>
</f:else>
</f:if>
</f:for>
</ul>
</f:if>

<f:section name="Month">
<li class="archive-item archive-item-month archive-item-{year}-{month.month}">
<blog:link.archive class="archive-item-month-link" year="{year}" month="{month.month}" title="{f:format.date(format: '{settings.widgets.archive.monthDateFormat} {settings.widgets.archive.yearDateFormat}', date: month.timestamp)}">
<f:if condition="{settings.widgets.archive.groupByYear} == 1">
<f:then>
{f:format.date(format: '{settings.widgets.archive.monthDateFormat}', date: month.timestamp)} <f:if condition="{settings.widgets.archive.showCounter} == 1">({month.count})</f:if>
</f:then>
<f:else>
{f:format.date(format: '{settings.widgets.archive.monthDateFormat} {settings.widgets.archive.yearDateFormat}', date: month.timestamp)} <f:if condition="{settings.widgets.archive.showCounter} == 1">({month.count})</f:if>
</f:else>
</f:if>
</blog:link.archive>
<blog:link.archive class="archive-item-month-rss" rss="true" year="{year}" month="{month.month}" title="{year}">
<i class="fa fa-rss-square"></i>
</blog:link.archive>
</li>
</f:section>
</html>
19 changes: 16 additions & 3 deletions Resources/Private/Templates/Post/ListPostsByDate.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
{namespace blog=T3G\AgencyPack\Blog\ViewHelpers}

<f:layout name="Default" />

<f:section name="content">
<h1 class="page-title"><f:if condition="{month}">{f:format.date(format: '{settings.widgets.archive.monthDateFormat}', date: timestamp)}</f:if> {year} <blog:link.archive rss="true" year="{year}" month="{month}"><i class="fa fa-rss-square"></i></blog:link.archive></h1>
<f:render partial="List" arguments="{_all}" />
<f:if condition="{archiveData}">
<f:then>

<f:comment>If year is missing show an overview of the archive.</f:comment>
<h1 class="page-title"><f:translate key="headline.archive"/></h1>
<f:render partial="Archive/Menu" arguments="{settings: settings, items: archiveData}" />

</f:then>
<f:else>

<f:comment>If a year/+month is selected show posts matching.</f:comment>
<h1 class="page-title"><f:if condition="{month}">{f:format.date(format: '{settings.widgets.archive.monthDateFormat}', date: timestamp)}</f:if> {year} <blog:link.archive rss="true" year="{year}" month="{month}"><i class="fa fa-rss-square"></i></blog:link.archive></h1>
<f:render partial="List" arguments="{_all}" />

</f:else>
</f:if>
</f:section>

<f:section name="FooterAssets">
Expand Down
46 changes: 3 additions & 43 deletions Resources/Private/Templates/Widget/Archive.html
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
{namespace blog=T3G\AgencyPack\Blog\ViewHelpers}
<f:layout name="Widget" />

<f:section name="content">

<h3 class="widget-title"><f:translate key="headline.archive"/></h3>
<ul class="archive">
<f:for each="{archiveData}" as="rows" key="year">
<f:if condition="{settings.widgets.archive.groupByYear} == 1">
<f:then>
<li class="list-item archive-item archive-item-{year}">
<blog:link.archive year="{year}" title="{year}">{year}</blog:link.archive> <blog:link.archive rss="true" year="{year}" title="{year}"><i class="fa fa-rss-square"></i></blog:link.archive>
<f:if condition="{settings.widgets.archive.groupByMonth} == 1">
<ul>
<f:for each="{rows}" as="row">
<f:render section="monthLink" arguments="{settings: settings, year: year, row: row}" />
</f:for>
</ul>
</f:if>
</li>
</f:then>
<f:else>
<f:if condition="{settings.widgets.archive.groupByMonth} == 1">
<f:for each="{rows}" as="row">
<f:render section="monthLink" arguments="{settings: settings, year: year, row: row}" />
</f:for>
</f:if>
</f:else>
</f:if>
</f:for>
</ul>
</f:section>
<f:render partial="Archive/Menu" arguments="{settings: settings, items: archiveData}" />

<f:section name="monthLink">
<li class="list-item archive-item archive-item-{year}-{row.month}">
<blog:link.archive year="{year}" month="{row.month}" title="{f:format.date(format: '{settings.widgets.archive.monthDateFormat} {settings.widgets.archive.yearDateFormat}', date: row.timestamp)}">
<f:if condition="{settings.widgets.archive.groupByYear} == 1">
<f:then>
{f:format.date(format: '{settings.widgets.archive.monthDateFormat}', date: row.timestamp)} <f:if condition="{settings.widgets.archive.showCounter} == 1">({row.count})</f:if>
</f:then>
<f:else>
{f:format.date(format: '{settings.widgets.archive.monthDateFormat} {settings.widgets.archive.yearDateFormat}', date: row.timestamp)} <f:if condition="{settings.widgets.archive.showCounter} == 1">({row.count})</f:if>
</f:else>
</f:if>
</blog:link.archive>
<blog:link.archive rss="true" year="{year}" month="{row.month}" title="{year}"><i class="fa fa-rss-square"></i></blog:link.archive>
</li>
</f:section>
</f:section>

0 comments on commit a7cf507

Please sign in to comment.