1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cecil. |
5
|
|
|
* |
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cecil\Generator; |
15
|
|
|
|
16
|
|
|
use Cecil\Collection\Page\Collection as PagesCollection; |
17
|
|
|
use Cecil\Collection\Page\Page; |
18
|
|
|
use Cecil\Collection\Page\Type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Pagination generator. |
22
|
|
|
* |
23
|
|
|
* This generator creates paginated pages for sections, terms, and the homepage. |
24
|
|
|
* It filters the pages based on their type, checks for pagination settings, |
25
|
|
|
* and generates paginated pages accordingly. |
26
|
|
|
*/ |
27
|
|
|
class Pagination extends AbstractGenerator implements GeneratorInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
1 |
|
public function generate(): void |
33
|
|
|
{ |
34
|
1 |
|
if (!$this->config->isEnabled('pages.pagination')) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// filters list pages (home, sections and terms) |
39
|
1 |
|
$filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
40
|
1 |
|
return \in_array($page->getType(), [Type::HOMEPAGE->value, Type::SECTION->value, Type::TERM->value]); |
41
|
1 |
|
}); |
42
|
|
|
/** @var Page $page */ |
43
|
1 |
|
foreach ($filteredPages as $page) { |
44
|
|
|
// if no sub-pages: by-pass |
45
|
1 |
|
if ($page->getPages() === null) { |
46
|
1 |
|
continue; |
47
|
|
|
} |
48
|
1 |
|
$pages = $page->getPages()->filter(function (Page $page) { |
49
|
1 |
|
return $page->getType() == Type::PAGE->value && $page->getVariable('published'); |
50
|
1 |
|
}); |
51
|
|
|
// if no published sub-pages: by-pass |
52
|
1 |
|
if ($pages === null) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
1 |
|
$path = $page->getPath(); |
56
|
|
|
// site pagination configuration |
57
|
1 |
|
$paginationPerPage = \intval($this->config->get('pages.pagination.max') ?? 5); |
58
|
1 |
|
$paginationPath = $this->config->get('pages.pagination.path') ?? 'page'; |
59
|
|
|
// page pagination configuration |
60
|
1 |
|
$pagePagination = $page->getVariable('pagination'); |
61
|
1 |
|
if ($pagePagination) { |
62
|
1 |
|
if (isset($pagePagination['enabled']) && $pagePagination['enabled'] === false) { |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
1 |
|
if (isset($pagePagination['max'])) { |
66
|
1 |
|
$paginationPerPage = \intval($pagePagination['max']); |
67
|
|
|
} |
68
|
1 |
|
if (isset($pagePagination['path'])) { |
69
|
1 |
|
$paginationPath = (string) $pagePagination['path']; |
70
|
|
|
} |
71
|
|
|
} |
72
|
1 |
|
$pagesTotal = \count($pages); |
73
|
|
|
// is pagination not necessary? |
74
|
1 |
|
if ($pagesTotal <= $paginationPerPage) { |
75
|
1 |
|
continue; |
76
|
|
|
} |
77
|
|
|
// sorts pages |
78
|
1 |
|
$pages = Section::sortSubPages($this->config, $page, $pages); |
79
|
|
|
// builds paginator |
80
|
1 |
|
$paginatorPagesCount = \intval(ceil($pagesTotal / $paginationPerPage)); |
81
|
1 |
|
for ($i = 0; $i < $paginatorPagesCount; $i++) { |
82
|
1 |
|
$itPagesInPagination = new \LimitIterator($pages->getIterator(), $i * $paginationPerPage, $paginationPerPage); |
83
|
1 |
|
$pagesInPagination = new PagesCollection( |
84
|
1 |
|
$page->getId() . '-page-' . ($i + 1), |
85
|
1 |
|
iterator_to_array($itPagesInPagination) |
86
|
1 |
|
); |
87
|
1 |
|
$alteredPage = clone $page; |
88
|
|
|
// first page (ie: blog/page/1 -> blog) |
89
|
1 |
|
$pageId = $page->getId(); |
90
|
1 |
|
$alteredPage |
91
|
1 |
|
->setVariable('alias', [ |
92
|
1 |
|
\sprintf('%s/%s/%s', $path, $paginationPath, 1), |
93
|
1 |
|
]); |
94
|
|
|
// others pages (ie: blog/page/X) |
95
|
1 |
|
if ($i > 0) { |
96
|
1 |
|
$pageId = Page::slugify(\sprintf('%s/%s/%s', $page->getId(), $paginationPath, $i + 1)); |
97
|
1 |
|
$alteredPage |
98
|
1 |
|
->setId($pageId) |
99
|
1 |
|
->setVirtual(true) |
100
|
1 |
|
->setPath(Page::slugify(\sprintf('%s/%s/%s', $path, $paginationPath, $i + 1))) |
101
|
1 |
|
->unVariable('menu') |
102
|
1 |
|
->unVariable('alias') |
103
|
1 |
|
->unVariable('aliases') // backward compatibility |
104
|
1 |
|
->unVariable('langref') |
105
|
1 |
|
->setVariable('paginated', true); |
106
|
|
|
} |
107
|
|
|
// set paginator values |
108
|
1 |
|
$paginator = [ |
109
|
1 |
|
'pages' => $pagesInPagination, |
110
|
1 |
|
'pages_total' => $pagesTotal, |
111
|
1 |
|
'totalpages' => $pagesTotal, // backward compatibility |
112
|
1 |
|
'count' => $paginatorPagesCount, |
113
|
1 |
|
'current' => $i + 1, |
114
|
1 |
|
]; |
115
|
|
|
// adds links |
116
|
1 |
|
$paginator['links'] = ['first' => $page->getId() ?: 'index']; |
117
|
1 |
|
if ($i == 1) { |
118
|
1 |
|
$paginator['links'] += ['prev' => $page->getId() ?: 'index']; |
119
|
|
|
} |
120
|
1 |
|
if ($i > 1) { |
121
|
1 |
|
$paginator['links'] += ['prev' => Page::slugify(\sprintf( |
122
|
1 |
|
'%s/%s/%s', |
123
|
1 |
|
$page->getId(), |
124
|
1 |
|
$paginationPath, |
125
|
1 |
|
$i |
126
|
1 |
|
))]; |
127
|
|
|
} |
128
|
1 |
|
$paginator['links'] += ['self' => $pageId ?: 'index']; |
129
|
1 |
|
if ($i < $paginatorPagesCount - 1) { |
130
|
1 |
|
$paginator['links'] += ['next' => Page::slugify(\sprintf( |
131
|
1 |
|
'%s/%s/%s', |
132
|
1 |
|
$page->getId(), |
133
|
1 |
|
$paginationPath, |
134
|
1 |
|
$i + 2 |
135
|
1 |
|
))]; |
136
|
|
|
} |
137
|
1 |
|
$paginator['links'] += ['last' => Page::slugify(\sprintf( |
138
|
1 |
|
'%s/%s/%s', |
139
|
1 |
|
$page->getId(), |
140
|
1 |
|
$paginationPath, |
141
|
1 |
|
$paginatorPagesCount |
142
|
1 |
|
))]; |
143
|
1 |
|
$paginator['links'] += ['path' => Page::slugify(\sprintf('%s/%s', $page->getId(), $paginationPath))]; |
144
|
|
|
// set paginator to cloned page |
145
|
1 |
|
$alteredPage->setPaginator($paginator); |
146
|
1 |
|
$alteredPage->setVariable('pagination', $paginator); // backward compatibility |
147
|
|
|
// updates date with the first element of the collection |
148
|
1 |
|
$alteredPage->setVariable('date', $pagesInPagination->first()->getVariable('date')); |
149
|
|
|
|
150
|
1 |
|
$this->generatedPages->add($alteredPage); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|