Skip to content

Commit

Permalink
#186 Correction du module blog.
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Oct 12, 2020
1 parent a117ddd commit 8710ee2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
15 changes: 15 additions & 0 deletions core/modules/News/Config/routes.php
Expand Up @@ -10,15 +10,30 @@
':year' => '\d{4}',
':id' => '(/page/[1-9]\d*)?'
]);
R::get('news.years.page', 'news/:year/page/:id', 'News@viewYears', [
':year' => '\d{4}',
':id' => '[1-9]\d*'
]);
R::get('news.month', 'news/:year/:month:id', 'News@viewMonth', [
':year' => '\d{4}',
':month' => '0[1-9]|1[0-2]',
':id' => '(/page/[1-9]\d*)?'
]);
R::get('news.month.page', 'news/:year/:month/page/:id', 'News@viewMonth', [
':year' => '\d{4}',
':month' => '0[1-9]|1[0-2]',
':id' => '[1-9]\d*'
]);
R::get('news.day', 'news/:year/:month/:day:id', 'News@viewDay', [
':year' => '\d{4}',
':month' => '0[1-9]|1[0-2]',
':day' => '[0-2][1-9]|3[0-1]',
':id' => '(/page/[1-9]\d*)?'
]);
R::get('news.day.page', 'news/:year/:month/:day/page/:id', 'News@viewDay', [
':year' => '\d{4}',
':month' => '0[1-9]|1[0-2]',
':day' => '[0-2][1-9]|3[0-1]',
':id' => '[1-9]\d*'
]);
R::get('news.rss', 'feed/news/rss', 'News@viewRss');
47 changes: 28 additions & 19 deletions core/modules/News/Controller/News.php
Expand Up @@ -91,7 +91,7 @@ public function viewYears($years, $page, $req)
$this->dateCurrent = strtotime($date);
$this->dateNext = strtotime($date . ' +1 year -1 seconds');
$this->titleMain = t('Articles from :date', [ ':date' => $years ]);
$this->link = self::router()->getRoute('news.years', [ ':year' => $years ], false);
$this->link = self::router()->getRoute('news.years.page', [ ':year' => $years ], false);

return $this->renderNews($page, $req);
}
Expand All @@ -101,8 +101,8 @@ public function viewMonth($years, $month, $page, $req)
$date = $month . '/01/' . $years;
$this->dateCurrent = strtotime($date);
$this->dateNext = strtotime($date . ' +1 month -1 seconds');
$this->titleMain = t('Articles from :date', [ ':date' => date('M Y', $this->dateCurrent) ]);
$this->link = self::router()->getRoute('news.month', [
$this->titleMain = t('Articles from :date', [ ':date' => strftime('%B %Y', $this->dateCurrent) ]);
$this->link = self::router()->getRoute('news.month.page', [
':year' => $years,
':month' => $month
], false);
Expand All @@ -115,8 +115,8 @@ public function viewDay($years, $month, $day, $page, $req)
$date = $month . '/' . $day . '/' . $years;
$this->dateCurrent = strtotime($date);
$this->dateNext = strtotime($date . ' +1 day -1 seconds');
$this->titleMain = t('Articles from :date', [ ':date' => date('d M Y', $this->dateCurrent) ]);
$this->link = self::router()->getRoute('news.day', [
$this->titleMain = t('Articles from :date', [ ':date' => strftime('%d %B %Y', $this->dateCurrent) ]);
$this->link = self::router()->getRoute('news.day.page', [
':year' => $years,
':month' => $month,
':day' => $day
Expand All @@ -139,7 +139,14 @@ public function viewRss($req)

foreach ($items as &$item) {
$item[ 'field' ] = self::node()->makeFieldsById('article', $item[ 'entity_id' ]);
$item[ 'link' ] = self::router()->getRoute('node.show', [ ':id_node' => $item[ 'id' ] ]);

if ($alias = self::alias()->getAlias('node/' . $item[ 'id' ])) {
$item[ 'link' ] = self::router()->makeRoute($alias);
} else {
$item[ 'link' ] = self::router()->getRoute('node.show', [
':id_node' => $item[ 'id' ]
]);
}
}
unset($item);

Expand Down Expand Up @@ -172,27 +179,29 @@ protected function renderNews($page, $req)
$page = !empty($page)
? substr(strrchr($page, '/'), 1)
: 1;

self::$limit = self::config()->get('settings.news_pagination', 6);
$offset = self::$limit * ($page - 1);
$news = $this->getNews($this->dateCurrent, $this->dateNext, $offset);

$offset = self::$limit * ($page - 1);
$news = $this->getNews($this->dateCurrent, $this->dateNext, $offset);

$isCurrent = (time() >= $this->dateCurrent && time() <= $this->dateNext);

$default = '';
if (!$news) {
if ($page == 1 && $isCurrent) {
$default = t('No articles for the moment');
} else {
return $this->get404($req);
}
$default = t('No articles for the moment');
if (!$news && !($page == 1 && $isCurrent)) {
return $this->get404($req);
}

foreach ($news as &$new) {
$new[ 'link_view' ] = self::router()->getRoute('node.show', [
':id_node' => $new[ 'id' ]
]);
$new[ 'field' ] = self::node()->makeFieldsById('article', $new[ 'entity_id' ]);

if ($alias = self::alias()->getAlias('node/' . $new[ 'id' ])) {
$new[ 'link_view' ] = self::router()->makeRoute($alias);
} else {
$new[ 'link_view' ] = self::router()->getRoute('node.show', [
':id_node' => $new[ 'id' ]
]);
}
}
unset($new);

Expand Down
6 changes: 2 additions & 4 deletions core/modules/News/Installer.php
Expand Up @@ -101,15 +101,13 @@ public function seeders(ContainerInterface $ci)
->insertInto('entity_article', [ 'image', 'summary', 'body', 'reading_time' ])
->values([
'https://picsum.photos/id/1/650/300',
'<p>Un article se met en valeur par un résumé qui décrit brièvement '
. 'son contenu avec un nombre de caractères limité (maximum 255 caractères).</p>',
'<p>Un article se met en valeur par un résumé qui décrit brièvement son contenu avec un nombre de caractères limité (maximum 255 caractères).</p>',
(new Template('article_1.php', $this->pathContent))->render(),
1
])
->values([
'https://picsum.photos/id/11/650/300',
'<p>Consectetur adipiscing elit. Etiam orci nulla, dignissim eu hendrerit ullamcorper, blandit et arcu. '
. 'Vivamus imperdiet, felis eget suscipit pellentesque, est tortor rutrum tortor.</p>',
'<p>Consectetur adipiscing elit. Etiam orci nulla, dignissim eu hendrerit ullamcorper, blandit et arcu.</p>',
(new Template('article_2.php', $this->pathContent))->render(),
1
])
Expand Down
18 changes: 12 additions & 6 deletions core/modules/News/Services/HookBlock.php
Expand Up @@ -92,7 +92,9 @@ public function hookBlockNewsMonth($tpl)
$year = date('Y', $value[ 'date_created' ]);
$month = date('m', $value[ 'date_created' ]);

if (!isset($output[ $year ])) {
if (isset($output[ $year ])) {
++$output[ $year ][ 'number' ];
} else {
$output[ $year ] = [
'number' => 1,
'year' => $year,
Expand All @@ -103,22 +105,26 @@ public function hookBlockNewsMonth($tpl)
];
}

if (!isset($output[ $year ][ 'months' ][ $month ])) {
if (isset($output[ $year ][ 'months' ][ $month ])) {
++$output[ $year ][ 'months' ][ $month ][ 'number' ];
} else {
$output[ $year ][ 'months' ][ $month ] = [
'number' => 1,
'year' => $year,
'month' => date('M', $value[ 'date_created' ]),
'month' => strftime('%b', $value[ 'date_created' ]),
'link' => $this->router->getRoute('news.month', [
':year' => $year,
':month' => $month,
':id' => ''
])
];
} else {
$output[ $year ][ 'number' ]++;
$output[ $year ][ 'months' ][ $month ][ 'number' ]++;
}
}
$output[ 'all' ] = [
'number' => count($data),
'year' => t('All'),
'link' => $this->router->getRoute('news.index')
];

return $tpl->addVar('years', $output);
}
Expand Down

0 comments on commit 8710ee2

Please sign in to comment.