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\Section. |
23
|
|
|
*/ |
24
|
|
|
class Section extends AbstractGenerator implements GeneratorInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
1 |
|
public function generate(): void |
30
|
|
|
{ |
31
|
1 |
|
$sections = []; |
32
|
|
|
|
33
|
|
|
// identifying sections from pages collection |
34
|
|
|
/** @var Page $page */ |
35
|
1 |
|
foreach ($this->builder->getPages() as $page) { |
36
|
|
|
// top level (root) sections |
37
|
1 |
|
if ($page->getSection()) { |
38
|
|
|
// do not add "draft" and "excluded" pages to its section |
39
|
1 |
|
if ($page->getVariable('published') !== true || $page->getVariable('exclude')) { |
40
|
1 |
|
continue; |
41
|
|
|
} |
42
|
|
|
// $sections[section][language][] = $page |
43
|
1 |
|
$sections[$page->getSection()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; |
44
|
|
|
// nested sections |
45
|
|
|
/*if ($page->getParent() !== null) { |
46
|
|
|
$sections[$page->getParent()->getId()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; |
47
|
|
|
}*/ |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// adds each section to pages collection |
52
|
1 |
|
if (\count($sections) > 0) { |
53
|
1 |
|
$menuWeight = 100; |
54
|
|
|
|
55
|
1 |
|
foreach ($sections as $section => $languages) { |
56
|
1 |
|
foreach ($languages as $language => $pagesAsArray) { |
57
|
1 |
|
$pageId = $path = Page::slugify($section); |
58
|
1 |
|
if ($language != $this->config->getLanguageDefault()) { |
59
|
1 |
|
$pageId = "$language/$pageId"; |
60
|
|
|
} |
61
|
1 |
|
$page = (new Page($pageId))->setVariable('title', ucfirst($section)) |
62
|
1 |
|
->setPath($path); |
63
|
1 |
|
if ($this->builder->getPages()->has($pageId)) { |
64
|
1 |
|
$page = clone $this->builder->getPages()->get($pageId); |
65
|
|
|
} |
66
|
1 |
|
$subPages = new PagesCollection("section-$pageId", $pagesAsArray); |
67
|
|
|
// cascade variables |
68
|
1 |
|
if ($page->hasVariable('cascade')) { |
69
|
1 |
|
$cascade = $page->getVariable('cascade'); |
70
|
1 |
|
$subPages->map(function (Page $page) use ($cascade) { |
71
|
1 |
|
foreach ($cascade as $key => $value) { |
72
|
1 |
|
if (!$page->hasVariable($key)) { |
73
|
1 |
|
$page->setVariable($key, $value); |
74
|
|
|
} |
75
|
|
|
} |
76
|
1 |
|
}); |
77
|
|
|
} |
78
|
|
|
// sorts pages |
79
|
1 |
|
$pages = Section::sortSubPages($this->config, $page, $subPages); |
80
|
|
|
// adds navigation links (excludes taxonomy pages) |
81
|
1 |
|
$sortBy = $page->getVariable('sortby')['variable'] ?? $page->getVariable('sortby') ?? $this->config->get('pages.sortby')['variable'] ?? $this->config->get('pages.sortby') ?? 'date'; |
82
|
1 |
|
if (!\in_array($page->getId(), array_keys((array) $this->config->get('taxonomies')))) { |
83
|
1 |
|
$this->addNavigationLinks($pages, $sortBy, $page->getVariable('circular') ?? false); |
84
|
|
|
} |
85
|
|
|
// creates page for each section |
86
|
1 |
|
$page->setType(Type::SECTION->value) |
87
|
1 |
|
->setSection($path) |
88
|
1 |
|
->setPages($pages) |
89
|
1 |
|
->setVariable('language', $language) |
90
|
1 |
|
->setVariable('date', $pages->first()->getVariable('date')) |
91
|
1 |
|
->setVariable('langref', $path); |
92
|
|
|
// human readable title |
93
|
1 |
|
if ($page->getVariable('title') == 'index') { |
94
|
|
|
$page->setVariable('title', $section); |
95
|
|
|
} |
96
|
|
|
// default menu |
97
|
1 |
|
if (!$page->getVariable('menu')) { |
98
|
1 |
|
$page->setVariable('menu', ['main' => ['weight' => $menuWeight]]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
try { |
102
|
1 |
|
$this->generatedPages->add($page); |
103
|
|
|
} catch (\DomainException) { |
104
|
|
|
$this->generatedPages->replace($page->getId(), $page); |
105
|
|
|
} |
106
|
|
|
} |
107
|
1 |
|
$menuWeight += 10; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sorts subpages. |
114
|
|
|
*/ |
115
|
1 |
|
public static function sortSubPages(\Cecil\Config $config, Page $page, PagesCollection $subPages): PagesCollection |
116
|
|
|
{ |
117
|
1 |
|
$subPages = $subPages->sortBy($config->get('pages.sortby')); |
118
|
1 |
|
if ($page->hasVariable('sortby')) { |
119
|
|
|
try { |
120
|
1 |
|
$subPages = $subPages->sortBy($page->getVariable('sortby')); |
121
|
|
|
} catch (RuntimeException $e) { |
122
|
|
|
throw new RuntimeException(sprintf('In page "%s", %s', $page->getId(), $e->getMessage())); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return $subPages; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Adds navigation (next and prev) to section subpages. |
131
|
|
|
*/ |
132
|
1 |
|
protected function addNavigationLinks(PagesCollection $pages, string|null $sortBy = null, bool $circular = false): void |
133
|
|
|
{ |
134
|
1 |
|
$pagesAsArray = $pages->toArray(); |
135
|
1 |
|
if ($sortBy === null || $sortBy == 'date' || $sortBy == 'updated') { |
136
|
1 |
|
$pagesAsArray = array_reverse($pagesAsArray); |
137
|
|
|
} |
138
|
1 |
|
$count = \count($pagesAsArray); |
139
|
1 |
|
if ($count > 1) { |
140
|
1 |
|
foreach ($pagesAsArray as $position => $page) { |
141
|
|
|
switch ($position) { |
142
|
1 |
|
case 0: // first |
143
|
1 |
|
if ($circular) { |
144
|
1 |
|
$page->setVariables([ |
145
|
1 |
|
'prev' => $pagesAsArray[$count - 1], |
146
|
1 |
|
]); |
147
|
|
|
} |
148
|
1 |
|
$page->setVariables([ |
149
|
1 |
|
'next' => $pagesAsArray[$position + 1], |
150
|
1 |
|
]); |
151
|
1 |
|
break; |
152
|
1 |
|
case $count - 1: // last |
153
|
1 |
|
$page->setVariables([ |
154
|
1 |
|
'prev' => $pagesAsArray[$position - 1], |
155
|
1 |
|
]); |
156
|
1 |
|
if ($circular) { |
157
|
1 |
|
$page->setVariables([ |
158
|
1 |
|
'next' => $pagesAsArray[0], |
159
|
1 |
|
]); |
160
|
|
|
} |
161
|
1 |
|
break; |
162
|
|
|
default: |
163
|
1 |
|
$page->setVariables([ |
164
|
1 |
|
'prev' => $pagesAsArray[$position - 1], |
165
|
1 |
|
'next' => $pagesAsArray[$position + 1], |
166
|
1 |
|
]); |
167
|
1 |
|
break; |
168
|
|
|
} |
169
|
1 |
|
$this->generatedPages->add($page); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|