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

Commit

Permalink
Issue #77: De-couple paginator helper from router
Browse files Browse the repository at this point in the history
- Add 'url' config option to pass a HttpUrl object to the helper used to calculate the url for each page
- Set the url for each page using the url object and offset and limit
- Rename link() to page()

BREAKING! The paginator link() method has been renamed to page() and the paginator now has a 'url' config option that needs to be used to create the url for each page. To migrate your code all template calls to the paginator should include array('url' => route());
  • Loading branch information
johanjanssens committed May 3, 2016
1 parent efda0e3 commit 1184bf0
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions code/template/helper/paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ class TemplateHelperPaginator extends TemplateHelperSelect
* @param array $config An optional array with configuration options
* @return string Html
*/
public function pagination($config = array(), TemplateInterface $template)
public function pagination($config = array())
{
$config = new ModelPaginator($config);
$config->append(array(
'url' => null,
'total' => 0,
'display' => 4,
'offset' => 0,
Expand All @@ -52,7 +53,7 @@ public function pagination($config = array(), TemplateInterface $template)
}

if($config->show_pages) {
$html .= $this->pages($config, $template);
$html .= $this->pages($config);
}

if($config->show_count) {
Expand All @@ -73,7 +74,7 @@ public function limit($config = array())
{
$config = new ObjectConfigJson($config);
$config->append(array(
'limit' => 0,
'limit' => 0,
'attribs' => array(),
'page_rows' => array(10, 20, 50, 100)
));
Expand Down Expand Up @@ -112,10 +113,11 @@ public function limit($config = array())
* @param array $config An optional array with configuration options
* @return string Html
*/
public function pages($config = array(), TemplateInterface $template)
public function pages($config = array())
{
$config = new ModelPaginator($config);
$config->append(array(
'url' => null,
'total' => 0,
'display' => 4,
'offset' => 0,
Expand All @@ -125,15 +127,15 @@ public function pages($config = array(), TemplateInterface $template)

$html = '<ul class="pagination">';
if($config->offset) {
$html .= $this->link($config->pages->prev, $template);
$html .= $this->page($config->pages->prev, $config->url);
}

foreach($config->pages->offsets as $offset) {
$html .= $this->link($offset, $template);
$html .= $this->page($offset, $config->url);
}

if($config->total > ($config->offset + $config->limit)) {
$html .= $this->link($config->pages->next, $template);
$html .= $this->page($config->pages->next, $config->url);
}
$html .= '</ul>';

Expand All @@ -143,13 +145,13 @@ public function pages($config = array(), TemplateInterface $template)
/**
* Render a page link
*
* @param array $config An optional array with configuration options
* @param ObjectConfigInterface $page The page data
* @param HttpUrlInterface $url The base url to create the link
* @return string Html
*/
public function link($config, TemplateInterface $template)
public function page(ObjectConfigInterface $page, HttpUrlInterface $url)
{
$config = new ObjectConfig($config);
$config->append(array(
$page->append(array(
'title' => '',
'current' => false,
'active' => false,
Expand All @@ -159,11 +161,14 @@ public function link($config, TemplateInterface $template)
'attribs' => array(),
));

$route = $template->route('limit='.$config->limit.'&offset='.$config->offset);
$rel = !empty($config->rel) ? 'rel="'.$config->rel.'"' : '';
//Set the offset and limit
$url->query['limit'] = $page->limit;
$url->query['offset'] = $page->offset;

$rel = !empty($page->rel) ? 'rel="'.$page->rel.'"' : '';

$html = '<li '.$this->buildAttributes($config->attribs).'>';
$html .= '<a href="'.$route.'" '.$rel.'>'.$this->getObject('translator')->translate($config->title).'</a>';
$html = '<li '.$this->buildAttributes($page->attribs).'>';
$html .= '<a href="'.$url.'" '.$rel.'>'.$this->getObject('translator')->translate($page->title).'</a>';
$html .= '</li>';

return $html;
Expand Down

0 comments on commit 1184bf0

Please sign in to comment.