mirror of
https://github.com/processwire/processwire.git
synced 2025-08-08 15:57:01 +02:00
Some upgrades and code consolidation in MarkupPagerNav, plus make it populate a $config->pagerHeadTags that one can output in a document head after rendering pagination, containing the <link rel='next|prev'.../>
tags when desired.
This commit is contained in:
@@ -220,6 +220,14 @@ class MarkupPagerNav extends Wire implements Module {
|
|||||||
*/
|
*/
|
||||||
protected $isLastPage = null;
|
protected $isLastPage = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prefix to identify page numbers in URL, i.e. page123 or page=123
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected $pageNumUrlPrefix = 'page';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct
|
* Construct
|
||||||
*
|
*
|
||||||
@@ -272,12 +280,21 @@ class MarkupPagerNav extends Wire implements Module {
|
|||||||
if($limit) $this->itemsPerPage = $limit;
|
if($limit) $this->itemsPerPage = $limit;
|
||||||
$this->pageNum = $items->getStart() < $this->itemsPerPage ? 1 : ceil($items->getStart() / $this->itemsPerPage)+1;
|
$this->pageNum = $items->getStart() < $this->itemsPerPage ? 1 : ceil($items->getStart() / $this->itemsPerPage)+1;
|
||||||
|
|
||||||
if(is_null($this->options['page'])) $this->options['page'] = $this->wire('page');
|
if(is_null($this->options['page'])) {
|
||||||
|
$this->options['page'] = $this->wire('page');
|
||||||
|
}
|
||||||
|
|
||||||
if(!strlen($this->queryString)) {
|
if(!strlen($this->queryString)) {
|
||||||
$whitelist = $this->wire('input')->whitelist->getArray();
|
$whitelist = $this->wire('input')->whitelist->getArray();
|
||||||
if(!count($this->options['getVars']) && count($whitelist)) $this->setGetVars($whitelist);
|
if(!count($this->options['getVars']) && count($whitelist)) {
|
||||||
else if(count($this->options['getVars'])) $this->setGetVars($this->getVars);
|
$this->setGetVars($whitelist);
|
||||||
|
} else if(count($this->options['getVars'])) {
|
||||||
|
$this->setGetVars($this->getVars);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if($config->pageNumUrlPrefix) {
|
||||||
|
$this->pageNumUrlPrefix = $config->pageNumUrlPrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
$pagerNav = new PagerNav($this->totalItems, $this->itemsPerPage, $this->pageNum);
|
$pagerNav = new PagerNav($this->totalItems, $this->itemsPerPage, $this->pageNum);
|
||||||
@@ -286,13 +303,6 @@ class MarkupPagerNav extends Wire implements Module {
|
|||||||
$pager = $pagerNav->getPager();
|
$pager = $pagerNav->getPager();
|
||||||
$out = '';
|
$out = '';
|
||||||
|
|
||||||
// if allowPageNum is true, then we can use urlSegment style page numbers rather than GET var page numbers
|
|
||||||
$allowPageNum = $this->options['page']->template->allowPageNum;
|
|
||||||
$slashUrls = $this->options['page']->template->slashUrls;
|
|
||||||
$slashPageNum = $this->options['page']->template->slashPageNum;
|
|
||||||
$pageNumUrlPrefix = $config->pageNumUrlPrefix;
|
|
||||||
if(!$pageNumUrlPrefix) $pageNumUrlPrefix = 'page';
|
|
||||||
|
|
||||||
$pagerCount = count($pager);
|
$pagerCount = count($pager);
|
||||||
if($pagerCount > 1) $this->isLastPage = false;
|
if($pagerCount > 1) $this->isLastPage = false;
|
||||||
$firstNumberKey = null;
|
$firstNumberKey = null;
|
||||||
@@ -317,22 +327,7 @@ class MarkupPagerNav extends Wire implements Module {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = $this->baseUrl;
|
$url = $this->getURL($item->pageNum);
|
||||||
if(!$url) $url = $this->options['page']->url;
|
|
||||||
|
|
||||||
if($item->pageNum > 1) {
|
|
||||||
if($allowPageNum) {
|
|
||||||
if($slashUrls === 0) $url .= '/'; // enforce a trailing slash, regardless of slashUrls template setting
|
|
||||||
$url .= "$pageNumUrlPrefix{$item->pageNum}";
|
|
||||||
if($slashPageNum > 0) $url .= '/';
|
|
||||||
$url .= $this->queryString;
|
|
||||||
} else {
|
|
||||||
$url .= $this->queryString . ($this->queryString ? "&" : "?") . "$pageNumUrlPrefix=" . $item->pageNum;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$url .= $this->queryString;
|
|
||||||
}
|
|
||||||
|
|
||||||
$classes = array();
|
$classes = array();
|
||||||
|
|
||||||
if($item->type != 'first' && $item->type != 'last' && isset($this->options[$item->type . 'ItemClass'])) {
|
if($item->type != 'first' && $item->type != 'last' && isset($this->options[$item->type . 'ItemClass'])) {
|
||||||
@@ -418,8 +413,7 @@ class MarkupPagerNav extends Wire implements Module {
|
|||||||
''
|
''
|
||||||
), $this->options['listMarkup']);
|
), $this->options['listMarkup']);
|
||||||
|
|
||||||
if($nextURL) $config->urls->next = $nextURL;
|
if($nextURL || $prevURL) $this->updateConfigVars($nextURL, $prevURL);
|
||||||
if($prevURL) $config->urls->prev = $prevURL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $out;
|
return $out;
|
||||||
@@ -637,6 +631,74 @@ class MarkupPagerNav extends Wire implements Module {
|
|||||||
$this->options['previousItemLabel'] = $prev;
|
$this->options['previousItemLabel'] = $prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get URL for given pagination number
|
||||||
|
*
|
||||||
|
* Requires that render() method has already started or been previously called.
|
||||||
|
*
|
||||||
|
* @param int $pageNum
|
||||||
|
* @param bool $http Include scheme and hostname?
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function getURL($pageNum, $http = false) {
|
||||||
|
|
||||||
|
/** @var Page $page */
|
||||||
|
$page = $this->options['page'];
|
||||||
|
$template = $page->template;
|
||||||
|
if($this->baseUrl) {
|
||||||
|
$url = $this->baseUrl;
|
||||||
|
} else if($http) {
|
||||||
|
$url = $page->httpUrl();
|
||||||
|
} else {
|
||||||
|
$url = $page->url();
|
||||||
|
}
|
||||||
|
|
||||||
|
if($pageNum > 1) {
|
||||||
|
if($template->allowPageNum) {
|
||||||
|
// if allowPageNum is true, then we can use urlSegment style page numbers rather than GET var page numbers
|
||||||
|
if($template->slashUrls === 0) $url .= '/'; // enforce a trailing slash, regardless of slashUrls template setting
|
||||||
|
$url .= $this->pageNumUrlPrefix . $pageNum;
|
||||||
|
if($template->slashPageNum > 0) $url .= '/';
|
||||||
|
$url .= $this->queryString;
|
||||||
|
} else {
|
||||||
|
// query string style pagination
|
||||||
|
$url .= $this->queryString . (strlen($this->queryString) ? "&" : "?") . "$this->pageNumUrlPrefix=$pageNum";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$url .= $this->queryString;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populate $config->urls->next, $config->urls->prev, and $config->pagerHeadTags
|
||||||
|
*
|
||||||
|
* @param string $nextURL
|
||||||
|
* @param string $prevURL
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected function updateConfigVars($nextURL, $prevURL) {
|
||||||
|
|
||||||
|
/** @var Config $config */
|
||||||
|
$config = $this->wire('config');
|
||||||
|
$httpRoot = $this->wire('input')->httpHostUrl();
|
||||||
|
$pagerHeadTags = '';
|
||||||
|
|
||||||
|
if($nextURL) {
|
||||||
|
$config->urls->next = $nextURL;
|
||||||
|
$pagerHeadTags .= "<link rel=\"next\" href=\"$httpRoot$nextURL\" />";
|
||||||
|
}
|
||||||
|
|
||||||
|
if($prevURL) {
|
||||||
|
$config->urls->prev = $prevURL;
|
||||||
|
$pagerHeadTags .= "<link rel=\"prev\" href=\"$httpRoot$prevURL\" />";
|
||||||
|
}
|
||||||
|
|
||||||
|
$config->set('pagerHeadTags', $pagerHeadTags);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The following methods are specific to the Module interface
|
* The following methods are specific to the Module interface
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user