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\Collection\Page\Type; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Section. |
17
|
|
|
*/ |
18
|
|
|
class Section extends AbstractGenerator implements GeneratorInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function generate(): void |
24
|
|
|
{ |
25
|
|
|
$sections = []; |
26
|
|
|
|
27
|
|
|
// identify sections |
28
|
|
|
/* @var $page Page */ |
29
|
|
|
foreach ($this->pagesCollection as $page) { |
30
|
|
|
if ($page->getSection()) { |
31
|
|
|
$sections[$page->getSection()][] = $page; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// adds section to pages collection |
36
|
|
|
if (count($sections) > 0) { |
37
|
|
|
$menuWeight = 100; |
38
|
|
|
foreach ($sections as $section => $pagesAsArray) { |
39
|
|
|
$pageId = $path = Page::slugify($section); |
40
|
|
|
$page = (new Page($pageId))->setVariable('title', ucfirst($section)); |
41
|
|
|
if ($this->pagesCollection->has($pageId)) { |
42
|
|
|
$page = clone $this->pagesCollection->get($pageId); |
43
|
|
|
} |
44
|
|
|
$pages = new PagesCollection($section, $pagesAsArray); |
45
|
|
|
// sort |
46
|
|
|
$pages = $pages->sortByDate(); |
47
|
|
|
if ($page->getVariable('sortby')) { |
|
|
|
|
48
|
|
|
$sortMethod = sprintf('sortBy%s', ucfirst($page->getVariable('sortby'))); |
|
|
|
|
49
|
|
|
if (method_exists($pages, $sortMethod)) { |
50
|
|
|
$pages = $pages->$sortMethod(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
// add navigation links |
54
|
|
|
$this->addNavigationLinks($pages); |
55
|
|
|
// create page for each section |
56
|
|
|
$page->setPath($path) |
|
|
|
|
57
|
|
|
->setType(Type::SECTION) |
|
|
|
|
58
|
|
|
->setVariable('pages', $pages) |
|
|
|
|
59
|
|
|
->setVariable('date', $pages->first()->getVariable('date')); |
60
|
|
|
// default menu |
61
|
|
|
if (!$page->getVariable('menu')) { |
|
|
|
|
62
|
|
|
$page->setVariable('menu', [ |
63
|
|
|
'main' => ['weight' => $menuWeight], |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
$this->generatedPages->add($page); |
67
|
|
|
$menuWeight += 10; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Add navigation (next and prev) to section sub pages. |
74
|
|
|
* |
75
|
|
|
* @param PagesCollection $pages |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
protected function addNavigationLinks(PagesCollection $pages): void |
80
|
|
|
{ |
81
|
|
|
$pagesAsArray = $pages->toArray(); |
82
|
|
|
if (count($pagesAsArray) > 1) { |
83
|
|
|
foreach ($pagesAsArray as $position => $page) { |
84
|
|
|
switch ($position) { |
85
|
|
|
// first |
86
|
|
|
case 0: |
87
|
|
|
$page->setVariables([ |
88
|
|
|
'next' => [ |
89
|
|
|
'id' => $pagesAsArray[$position + 1]->getId(), |
90
|
|
|
'path' => $pagesAsArray[$position + 1]->getPath(), |
91
|
|
|
'title' => $pagesAsArray[$position + 1]->getVariable('title'), |
92
|
|
|
], |
93
|
|
|
]); |
94
|
|
|
break; |
95
|
|
|
// last |
96
|
|
|
case count($pagesAsArray) - 1: |
97
|
|
|
$page->setVariables([ |
98
|
|
|
'prev' => [ |
99
|
|
|
'id' => $pagesAsArray[$position - 1]->getId(), |
100
|
|
|
'path' => $pagesAsArray[$position - 1]->getPath(), |
101
|
|
|
'title' => $pagesAsArray[$position - 1]->getVariable('title'), |
102
|
|
|
], |
103
|
|
|
]); |
104
|
|
|
break; |
105
|
|
|
default: |
106
|
|
|
$page->setVariables([ |
107
|
|
|
'prev' => [ |
108
|
|
|
'id' => $pagesAsArray[$position - 1]->getId(), |
109
|
|
|
'path' => $pagesAsArray[$position - 1]->getPath(), |
110
|
|
|
'title' => $pagesAsArray[$position - 1]->getVariable('title'), |
111
|
|
|
], |
112
|
|
|
'next' => [ |
113
|
|
|
'id' => $pagesAsArray[$position + 1]->getId(), |
114
|
|
|
'path' => $pagesAsArray[$position + 1]->getPath(), |
115
|
|
|
'title' => $pagesAsArray[$position + 1]->getVariable('title'), |
116
|
|
|
], |
117
|
|
|
]); |
118
|
|
|
break; |
119
|
|
|
} |
120
|
|
|
$this->generatedPages->add($page); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|