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