Skip to content

Commit 31902b6

Browse files
committed
Add legacy Paginate variable back
1 parent 9b0c7d3 commit 31902b6

1 file changed

Lines changed: 276 additions & 0 deletions

File tree

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
<?php
2+
/**
3+
* @link https://craftcms.com/
4+
* @copyright Copyright (c) Pixel & Tonic, Inc.
5+
* @license https://craftcms.github.io/license/
6+
*/
7+
8+
namespace craft\web\twig\variables;
9+
10+
use Craft;
11+
use craft\db\Paginator;
12+
use craft\helpers\UrlHelper;
13+
use yii\base\BaseObject;
14+
15+
/**
16+
* Paginate variable class.
17+
*
18+
* @property string $basePath
19+
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
20+
* @since 3.0.0
21+
* @deprecated 6.0.0 use {@see \CraftCms\Cms\Twig\Variables\Paginate} instead.
22+
*/
23+
class Paginate extends BaseObject
24+
{
25+
/**
26+
* Creates a new instance based on a Paginator object
27+
*
28+
* @param Paginator $paginator
29+
* @return static
30+
* @since 3.1.19
31+
*/
32+
public static function create(Paginator $paginator): self
33+
{
34+
$pageResults = $paginator->getPageResults();
35+
$pageOffset = $paginator->getPageOffset();
36+
37+
return Craft::createObject([
38+
'class' => static::class,
39+
'first' => $pageOffset + 1,
40+
'last' => $pageOffset + count($pageResults),
41+
'total' => $paginator->getTotalResults(),
42+
'currentPage' => $paginator->getCurrentPage(),
43+
'totalPages' => $paginator->getTotalPages(),
44+
]);
45+
}
46+
47+
/**
48+
* @var int
49+
*/
50+
public int $first;
51+
52+
/**
53+
* @var int
54+
*/
55+
public int $last;
56+
57+
/**
58+
* @var int
59+
*/
60+
public int $total = 0;
61+
62+
/**
63+
* @var int
64+
*/
65+
public int $currentPage;
66+
67+
/**
68+
* @var int
69+
*/
70+
public int $totalPages = 0;
71+
72+
/**
73+
* @var string
74+
* @since 3.7.64
75+
*/
76+
public string $pageTrigger;
77+
78+
/**
79+
* @var string Base path
80+
* @see getBasePath()
81+
* @see setBasePath()
82+
*/
83+
private string $_basePath;
84+
85+
/**
86+
* @inheritdoc
87+
*/
88+
public function init()
89+
{
90+
parent::init();
91+
92+
if (!isset($this->pageTrigger)) {
93+
$this->pageTrigger = Craft::$app->getRequest()->getIsCpRequest()
94+
? 'p'
95+
: Craft::$app->getConfig()->getGeneral()->getPageTrigger();
96+
}
97+
}
98+
99+
/**
100+
* Returns the base path that should be used for pagination URLs.
101+
*
102+
* @return string
103+
*/
104+
public function getBasePath(): string
105+
{
106+
return $this->_basePath ?? ($this->_basePath = Craft::$app->getRequest()->getPathInfo());
107+
}
108+
109+
/**
110+
* Sets the base path that should be used for pagination URLs.
111+
*
112+
* @param string $basePath
113+
* @since 3.1.28
114+
*/
115+
public function setBasePath(string $basePath): void
116+
{
117+
$this->_basePath = $basePath;
118+
}
119+
120+
/**
121+
* Returns the URL to a specific page
122+
*
123+
* @param int $page
124+
* @return string|null
125+
*/
126+
public function getPageUrl(int $page): ?string
127+
{
128+
if ($page < 1 || $page > $this->totalPages) {
129+
return null;
130+
}
131+
132+
$useQueryParam = str_starts_with($this->pageTrigger, '?');
133+
134+
$path = $this->getBasePath();
135+
136+
// If not using a query param, append the page to the path
137+
if (!$useQueryParam && $page != 1) {
138+
if ($path) {
139+
$path .= '/';
140+
}
141+
142+
$path .= $this->pageTrigger . $page;
143+
}
144+
145+
// Build the URL with the same query string as the current request
146+
$url = UrlHelper::url($path, Craft::$app->getRequest()->getQueryStringWithoutPath());
147+
148+
// If using a query param, append or remove it
149+
if ($useQueryParam) {
150+
$param = trim($this->pageTrigger, '?=');
151+
if ($page != 1) {
152+
$url = UrlHelper::urlWithParams($url, [$param => $page]);
153+
} else {
154+
$url = UrlHelper::removeParam($url, $param);
155+
}
156+
}
157+
158+
return $url;
159+
}
160+
161+
/**
162+
* Returns the URL to the first page.
163+
*
164+
* @return string|null
165+
*/
166+
public function getFirstUrl(): ?string
167+
{
168+
return $this->getPageUrl(1);
169+
}
170+
171+
/**
172+
* Returns the URL to the next page.
173+
*
174+
* @return string|null
175+
*/
176+
public function getLastUrl(): ?string
177+
{
178+
return $this->getPageUrl($this->totalPages);
179+
}
180+
181+
/**
182+
* Returns the URL to the previous page.
183+
*
184+
* @return string|null
185+
*/
186+
public function getPrevUrl(): ?string
187+
{
188+
return $this->getPageUrl($this->currentPage - 1);
189+
}
190+
191+
/**
192+
* Returns the URL to the next page.
193+
*
194+
* @return string|null
195+
*/
196+
public function getNextUrl(): ?string
197+
{
198+
return $this->getPageUrl($this->currentPage + 1);
199+
}
200+
201+
/**
202+
* Returns previous page URLs up to a certain distance from the current page.
203+
*
204+
* @param int|null $dist
205+
* @return array
206+
*/
207+
public function getPrevUrls(?int $dist = null): array
208+
{
209+
if ($dist !== null) {
210+
$start = $this->currentPage - $dist;
211+
} else {
212+
$start = 1;
213+
}
214+
215+
return $this->getRangeUrls($start, $this->currentPage - 1);
216+
}
217+
218+
/**
219+
* Returns next page URLs up to a certain distance from the current page.
220+
*
221+
* @param int|null $dist
222+
* @return array
223+
*/
224+
public function getNextUrls(?int $dist = null): array
225+
{
226+
if ($dist !== null) {
227+
$end = $this->currentPage + $dist;
228+
} else {
229+
$end = $this->totalPages;
230+
}
231+
232+
return $this->getRangeUrls($this->currentPage + 1, $end);
233+
}
234+
235+
/**
236+
* Returns a range of page URLs.
237+
*
238+
* @param int $start
239+
* @param int $end
240+
* @return string[]
241+
*/
242+
public function getRangeUrls(int $start, int $end): array
243+
{
244+
if ($start < 1) {
245+
$start = 1;
246+
}
247+
248+
if ($end > $this->totalPages) {
249+
$end = $this->totalPages;
250+
}
251+
252+
$urls = [];
253+
254+
for ($page = $start; $page <= $end; $page++) {
255+
$urls[$page] = $this->getPageUrl($page);
256+
}
257+
258+
return $urls;
259+
}
260+
261+
/**
262+
* Returns a dynamic range of page URLs that surround (and include) the current page.
263+
*
264+
* @param int $max The maximum number of links to return
265+
* @return string[]
266+
*/
267+
public function getDynamicRangeUrls(int $max = 10): array
268+
{
269+
$start = max(1, $this->currentPage - floor($max / 2));
270+
$end = min($this->totalPages, $start + $max - 1);
271+
if ($end - $start < $max) {
272+
$start = max(1, $end - $max + 1);
273+
}
274+
return $this->getRangeUrls($start, $end);
275+
}
276+
}

0 commit comments

Comments
 (0)