1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Generator; |
10
|
|
|
|
11
|
|
|
use Cecil\Collection\Page\Collection as PagesCollection; |
12
|
|
|
use Cecil\Collection\Page\Page; |
13
|
|
|
use Cecil\Page\Type; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Pagination. |
17
|
|
|
*/ |
18
|
|
|
class Pagination extends AbstractGenerator implements GeneratorInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function generate(PagesCollection $pagesCollection, \Closure $messageCallback) |
24
|
|
|
{ |
25
|
|
|
$generatedPages = new PagesCollection(); |
26
|
|
|
|
27
|
|
|
$filteredPages = $pagesCollection->filter(function (Page $page) { |
28
|
|
|
return in_array($page->getType(), [Type::HOMEPAGE, Type::SECTION]); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
/* @var $page Page */ |
32
|
|
|
foreach ($filteredPages as $page) { |
33
|
|
|
if ($this->config->get('site.paginate.disabled')) { |
34
|
|
|
return $generatedPages; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$paginateMax = intval($this->config->get('site.paginate.max')); |
38
|
|
|
$paginatePath = $this->config->get('site.paginate.path'); |
39
|
|
|
$pages = $page->getVariable('pages'); |
40
|
|
|
$path = $page->getPathname(); |
41
|
|
|
|
42
|
|
|
// paginate |
43
|
|
|
$totalpages = count($pages); |
44
|
|
|
if ($totalpages > $paginateMax) { |
45
|
|
|
$paginateCount = ceil($totalpages / $paginateMax); |
46
|
|
|
for ($i = 0; $i < $paginateCount; $i++) { |
47
|
|
|
$pagesInPagination = array_slice($pages, ($i * $paginateMax), $paginateMax); |
48
|
|
|
$alteredPage = clone $page; |
49
|
|
|
// first page |
50
|
|
|
$firstPath = Page::slugify(sprintf('%s', $path)); |
51
|
|
|
if ($i == 0) { |
52
|
|
|
// ie: blog/page/1 -> blog |
53
|
|
|
$pageId = Page::slugify(sprintf('%s', $path)); |
54
|
|
|
// homepage special case |
55
|
|
|
if ($path == '') { |
56
|
|
|
$pageId = 'index'; |
57
|
|
|
} |
58
|
|
|
$currentPath = $firstPath; |
59
|
|
|
$alteredPage |
60
|
|
|
->setId($pageId) |
61
|
|
|
->setPathname($currentPath) |
62
|
|
|
->setVariable('aliases', [ |
63
|
|
|
sprintf('%s/%s/%s', $path, $paginatePath, 1), |
64
|
|
|
]); |
65
|
|
|
} else { |
66
|
|
|
// ie: blog/page/2 |
67
|
|
|
$pageId = Page::slugify(sprintf('%s/%s/%s', $path, $paginatePath, $i + 1)); |
68
|
|
|
$currentPath = $pageId; |
69
|
|
|
$alteredPage |
70
|
|
|
->setId($pageId) |
71
|
|
|
->setPathname($currentPath) |
72
|
|
|
->unVariable('menu'); |
73
|
|
|
} |
74
|
|
|
$alteredPage->setVariable('url', $currentPath.'/'); |
75
|
|
|
$alteredPage->setVariable('totalpages', $totalpages); |
76
|
|
|
$alteredPage->setVariable('pages', $pagesInPagination); |
77
|
|
|
// links |
78
|
|
|
$pagination = ['self' => $currentPath]; |
79
|
|
|
$pagination += ['first' => $firstPath]; |
80
|
|
View Code Duplication |
if ($i > 0) { |
|
|
|
|
81
|
|
|
$pagination += ['prev' => Page::slugify(sprintf('%s/%s/%s', $path, $paginatePath, $i))]; |
82
|
|
|
} |
83
|
|
View Code Duplication |
if ($i < $paginateCount - 1) { |
|
|
|
|
84
|
|
|
$pagination += ['next' => Page::slugify(sprintf('%s/%s/%s', $path, $paginatePath, $i + 2))]; |
85
|
|
|
} |
86
|
|
|
$pagination += ['last' => Page::slugify(sprintf('%s/%s/%s', $path, $paginatePath, $paginateCount))]; |
87
|
|
|
$alteredPage |
88
|
|
|
->setVariable('pagination', $pagination) |
89
|
|
|
->setVariable('date', reset($pagesInPagination)->getVariable('date')); |
90
|
|
|
|
91
|
|
|
$generatedPages->add($alteredPage); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $generatedPages; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.