Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Added daily archive (#461)
Browse files Browse the repository at this point in the history
* Added daily archive

* Added test
  • Loading branch information
core23 committed Jul 8, 2018
1 parent 662d006 commit 4ba7fac
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/Action/DailyPostArchiveAction.php
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\NewsBundle\Action;

use Sonata\IntlBundle\Templating\Helper\DateTimeHelper;
use Sonata\NewsBundle\Model\BlogInterface;
use Sonata\NewsBundle\Model\PostManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;

final class DailyPostArchiveAction extends AbstractPostArchiveAction
{
/**
* @var DateTimeHelper
*/
private $dateTimeHelper;

public function __construct(
BlogInterface $blog,
PostManagerInterface $postManager,
TranslatorInterface $translator,
DateTimeHelper $dateTimeHelper
) {
parent::__construct($blog, $postManager, $translator);

$this->dateTimeHelper = $dateTimeHelper;
}

/**
* @param string $year
* @param string $month
* @param string $day
*
* @return Response
*/
public function __invoke(Request $request, $year, $month, $day)
{
$date = $this->getPostManager()->getPublicationDateQueryParts(sprintf('%d-%d-%d', $year, $month, $day), 'day');

if ($seoPage = $this->getSeoPage()) {
$seoDescription = $this->getBlog()->getDescription();
$seoFormat = $this->dateTimeHelper->format($date, 'MMMM');
$seoDate = $this->dataTimeHelper->formatDate($date);

$seoPage
->setTitle($this->trans('archive_day.meta_title', [
'%title%' => $this->getBlog()->getTitle(),
'%year%' => $year,
'%month%' => $seoFormat,
'%day%' => $day,
'%date%' => $seoDate,
]))
->addMeta('property', 'og:title', $this->trans('archive_day.meta_title', [
'%title%' => $this->getBlog()->getTitle(),
'%year%' => $year,
'%month%' => $seoFormat,
'%day%' => $day,
'%date%' => $seoDate,
]))
->addMeta('name', 'description', $this->trans('archive_day.meta_description', [
'%title%' => $this->getBlog()->getTitle(),
'%year%' => $year,
'%month%' => $seoFormat,
'%day%' => $day,
'%description%' => $seoDescription,
'%date%' => $seoDate,
]))
->addMeta('property', 'og:description', $this->trans('archive_day.meta_description', [
'%title%' => $this->getBlog()->getTitle(),
'%year%' => $year,
'%month%' => $seoFormat,
'%day%' => $day,
'%date%' => $seoDate,
'%description%' => $seoDescription,
]));
}

return $this->renderArchive($request, [
'date' => $date,
], []);
}
}
12 changes: 12 additions & 0 deletions src/Resources/config/actions.xml
Expand Up @@ -49,6 +49,18 @@
<argument type="service" id="service_container"/>
</call>
</service>
<service id="Sonata\NewsBundle\Action\DailyPostArchiveAction" class="Sonata\NewsBundle\Action\DailyPostArchiveAction" public="true">
<argument type="service" id="sonata.news.blog"/>
<argument type="service" id="sonata.news.manager.post"/>
<argument type="service" id="translator"/>
<argument type="service" id="sonata.intl.templating.helper.datetime"/>
<call method="setSeoPage">
<argument type="service" id="sonata.seo.page" on-invalid="null"/>
</call>
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
<service id="Sonata\NewsBundle\Action\PostArchiveAction" class="Sonata\NewsBundle\Action\PostArchiveAction" public="true">
<argument type="service" id="sonata.news.blog"/>
<argument type="service" id="sonata.news.manager.post"/>
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/config/routing/news.xml
Expand Up @@ -3,6 +3,14 @@
<route id="sonata_news_add_comment" path="/add-comment/{id}">
<default key="_controller">Sonata\NewsBundle\Action\CreateCommentAction</default>
</route>
<route id="sonata_news_archive_daily" path="/archive/{year}/{month}/{day}.{_format}">
<default key="_controller">Sonata\NewsBundle\Action\DailyPostArchiveAction</default>
<default key="_format">html</default>
<requirement key="_format">html|rss</requirement>
<requirement key="year">\d+</requirement>
<requirement key="month">\d+</requirement>
<requirement key="day">\d+</requirement>
</route>
<route id="sonata_news_archive_monthly" path="/archive/{year}/{month}.{_format}">
<default key="_controller">Sonata\NewsBundle\Action\MonthlyPostArchiveAction</default>
<default key="_format">html</default>
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/translations/SonataNewsBundle.de.xliff
Expand Up @@ -535,6 +535,14 @@
<source>archive_tag.meta_description</source>
<target>Archiv für %tag%</target>
</trans-unit>
<trans-unit id="archive_day.meta_title">
<source>archive_day.meta_title</source>
<target>Archiv für den %date%</target>
</trans-unit>
<trans-unit id="archive_day.meta_description">
<source>archive_day.meta_description</source>
<target>Archiv für den %date%</target>
</trans-unit>
</body>
</file>
</xliff>
8 changes: 8 additions & 0 deletions src/Resources/translations/SonataNewsBundle.en.xliff
Expand Up @@ -527,6 +527,14 @@ Quick moderation links :
<source>post_is_disabled</source>
<target>Post is not public.</target>
</trans-unit>
<trans-unit id="archive_day.meta_title">
<source>archive_day.meta_title</source>
<target>Archive of %date%</target>
</trans-unit>
<trans-unit id="archive_day.meta_description">
<source>archive_day.meta_description</source>
<target>Archive of %date%</target>
</trans-unit>
</body>
</file>
</xliff>
73 changes: 73 additions & 0 deletions tests/DailyPostArchiveActionTest.php
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\Tests\Action;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sonata\DatagridBundle\Pager\PagerInterface;
use Sonata\IntlBundle\Templating\Helper\DateTimeHelper;
use Sonata\NewsBundle\Action\DailyPostArchiveAction;
use Sonata\NewsBundle\Entity\PostManager;
use Sonata\NewsBundle\Model\BlogInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
use Twig\Environment;

class DailyPostArchiveActionTest extends TestCase
{
public function testInvoke()
{
$blog = $this->prophesize(BlogInterface::class);
$translator = $this->prophesize(TranslatorInterface::class);
$dateTimeHelper = $this->prophesize(DateTimeHelper::class);

$dataParams = [
'query' => 'foo.publicationDateStart >= :startDate AND bar.publicationDateStart < :endDate',
'params' => [
'startDate' => new \DateTime(),
'endDate' => new \DateTime('tomorrow'),
],
];

$postManager = $this->prophesize(PostManager::class);
$postManager->getPublicationDateQueryParts('2018-7-8', 'day')
->willReturn($dataParams);
$postManager->getPager([
'date' => $dataParams,
], 1)
->willReturn($this->prophesize(PagerInterface::class));

$twig = $this->prophesize(Environment::class);
$twig->render('@SonataNews/Post/archive.html.twig', Argument::any())
->willReturn('HTML CONTENT');

$container = new Container();
$container->set('twig', $twig->reveal());

$action = new DailyPostArchiveAction(
$blog->reveal(),
$postManager->reveal(),
$translator->reveal(),
$dateTimeHelper->reveal()
);
$action->setContainer($container);

$request = new Request();
$request->query->set('page', 1);

$response = $action($request, 2018, 7, 8);

$this->assertInstanceOf(Response::class, $response);
}
}

0 comments on commit 4ba7fac

Please sign in to comment.