Skip to content

Commit

Permalink
Add options to pagination helper; add ability to change the page key
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsafley committed Apr 4, 2024
1 parent bb58b9d commit b555470
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions application/src/View/Helper/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Pagination extends AbstractHelper
*/
protected $fragment;

protected $options;

/**
* Construct the helper.
*
Expand All @@ -44,14 +46,18 @@ public function __construct(Paginator $paginator)
/**
* Configure the pagination.
*
* Options:
* - page_key: The page # key passed in the URL query (defualt: "page")
*
* @param string|null $partialName Name of view script
* @param int|null $totalCount The total record count
* @param int|null $currentPage The current page number
* @param int|null $perPage The number of records per page
* @param array $options
* @return self
*/
public function __invoke($partialName = null, $totalCount = null, $currentPage = null,
$perPage = null
$perPage = null, array $options = []
) {
if (null !== $totalCount) {
$this->getPaginator()->setTotalCount($totalCount);
Expand All @@ -63,6 +69,7 @@ public function __invoke($partialName = null, $totalCount = null, $currentPage =
$this->getPaginator()->setPerPage($perPage);
}
$this->partialName = $partialName ?: self::PARTIAL_NAME;
$this->options = $options;
return $this;
}

Expand Down Expand Up @@ -99,6 +106,7 @@ public function __toString()
'lastPageUrl' => $this->getUrl($pageCount),
'pagelessUrl' => $this->getPagelessUrl(),
'offset' => $paginator->getOffset(),
'options' => $this->options,
]
);
}
Expand Down Expand Up @@ -127,7 +135,7 @@ public function setFragment($fragment)
protected function getUrl($page)
{
$query = $this->getView()->params()->fromQuery();
$query['page'] = (int) $page;
$query[$this->options['page_key'] ?? 'page'] = (int) $page;
if (isset($query['sort_by_default'])) {
// Do not emit sort_by if the sort_by_default flag is set.
unset($query['sort_by']);
Expand Down
2 changes: 1 addition & 1 deletion application/view/common/linked-resources.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use Laminas\Form\Element\Select;

// Set up pagination.
$pagination = $this->pagination(null, $totalCount, $page, $perPage);
$pagination = $this->pagination(null, $totalCount, $page, $perPage, ['page_key' => 'linked_resources_page']);
$fragment = 'resources-linked';
$pagination->setFragment($fragment);

Expand Down
2 changes: 1 addition & 1 deletion application/view/common/pagination.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (isset($query['sort_by_default'])) {
<?php if ($totalCount): ?>
<form method="GET" action="">
<?php echo $this->queryToHiddenInputs($removeQueries); ?>
<input type="text" name="page" class="page-input-top" value="<?php echo $currentPage; ?>" size="4" <?php echo ($pageCount == 1) ? 'readonly' : ''; ?> aria-label="<?php echo $translate('Page'); ?>">
<input type="text" name="<?php echo $this->escapeHtml($options['page_key'] ?? 'page'); ?>" class="page-input-top" value="<?php echo $currentPage; ?>" size="4" <?php echo ($pageCount == 1) ? 'readonly' : ''; ?> aria-label="<?php echo $translate('Page'); ?>">
<span class="page-count"><?php echo sprintf($translate('of %s'), $pageCount); ?></span>
</form>

Expand Down
2 changes: 1 addition & 1 deletion application/view/omeka/admin/item/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $itemMedia = $item->media();
<div id="resources-linked" class="section">
<?php if ($item->subjectValueTotalCount()): ?>
<?php echo $item->displaySubjectValues([
'page' => $this->params()->fromQuery('page', 1),
'page' => $this->params()->fromQuery('linked_resources_page', 1),
'perPage' => 25,
'resourceProperty' => $this->params()->fromQuery('resource_property'),
]); ?>
Expand Down

0 comments on commit b555470

Please sign in to comment.