Skip to content

Commit

Permalink
Add pagination to the show-action of the article archive
Browse files Browse the repository at this point in the history
  • Loading branch information
blackcoder87 committed May 11, 2017
1 parent eaeb1db commit 94901bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
7 changes: 6 additions & 1 deletion application/modules/article/controllers/Archive.php
Expand Up @@ -62,6 +62,7 @@ public function showAction()
$categoryMapper = new CategoryMapper();
$commentMapper = new CommentMapper();
$userMapper = new UserMapper();
$pagination = new \Ilch\Pagination();

$date = new \Ilch\Date($this->getRequest()->getParam('year').'-'.$this->getRequest()->getParam('month').'-01');

Expand All @@ -76,9 +77,13 @@ public function showAction()
->add($this->getTranslator()->trans('menuArchives'), ['action' => 'index'])
->add($date->format('F Y', true), ['action' => 'show', 'year' => $this->getRequest()->getParam('year'), 'month' => $this->getRequest()->getParam('month')]);

$pagination->setRowsPerPage($this->getConfig()->get('defaultPaginationObjects'));
$pagination->setPage($this->getRequest()->getParam('page'));

$this->getView()->set('categoryMapper', $categoryMapper)
->set('commentMapper', $commentMapper)
->set('userMapper', $userMapper)
->set('articles', $articleMapper->getArticlesByDate($date));
->set('articles', $articleMapper->getArticlesByDate($date, $pagination))
->set('pagination', $pagination);
}
}
12 changes: 10 additions & 2 deletions application/modules/article/mappers/Article.php
Expand Up @@ -121,9 +121,10 @@ public function getArticlesByCats($catId, $locale = '', $pagination = null)
* Get articles of the month of the given date
*
* @param \DateTime $date
* @param \Ilch\Pagination|null $pagination
* @return ArticleModel[]|array
*/
public function getArticlesByDate(\DateTime $date)
public function getArticlesByDate(\DateTime $date, $pagination = null)
{
$db = $this->db();

Expand All @@ -139,7 +140,14 @@ public function getArticlesByDate(\DateTime $date)
->where(['p.date_created >=' => $dateFrom, 'p.date_created <' => $dateTo])
->group(['p.id' => 'DESC', 'p.cat_id', 'p.date_created', 'pc.article_id', 'pc.author_id', 'pc.visits', 'pc.content', 'pc.description', 'pc.keywords', 'pc.locale', 'pc.title', 'pc.teaser', 'pc.perma', 'pc.img', 'pc.img_source']);

$result = $select->execute();
if ($pagination !== null) {
$select->limit($pagination->getLimit())
->useFoundRows();
$result = $select->execute();
$pagination->setRows($result->getFoundRows());
} else {
$result = $select->execute();
}
$articleArray = $result->fetchRows();

if (empty($articleArray)) {
Expand Down
1 change: 1 addition & 0 deletions application/modules/article/views/archive/show.php
Expand Up @@ -63,6 +63,7 @@
</div>
<br /><br /><br />
<?php endforeach; ?>
<?=$this->get('pagination')->getHtml($this, ['action' => 'show', 'year' => $this->getRequest()->getParam('year'), 'month' => $this->getRequest()->getParam('month')]) ?>
<?php else: ?>
<?=$this->getTrans('noArticles') ?>
<?php endif; ?>

0 comments on commit 94901bc

Please sign in to comment.