1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cecil/Cecil package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cecil\Generator; |
12
|
|
|
|
13
|
|
|
use Cecil\Collection\Page\Collection as PagesCollection; |
14
|
|
|
use Cecil\Collection\Page\Page; |
15
|
|
|
use Cecil\Collection\Page\Type; |
16
|
|
|
use Cecil\Exception\Exception; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Generator\Section. |
20
|
|
|
*/ |
21
|
|
|
class Section extends AbstractGenerator implements GeneratorInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function generate(): void |
27
|
|
|
{ |
28
|
|
|
$sections = []; |
29
|
|
|
|
30
|
|
|
// identifying sections |
31
|
|
|
/** @var Page $page */ |
32
|
|
|
foreach ($this->builder->getPages() as $page) { |
33
|
|
|
if ($page->getSection()) { |
34
|
|
|
// excludes page from section |
35
|
|
|
if ($page->getVariable('exclude')) { |
36
|
|
|
$alteredPage = clone $page; |
37
|
|
|
$alteredPage->setSection(''); |
38
|
|
|
$this->builder->getPages()->replace($page->getId(), $alteredPage); |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
$sections[$page->getSection()][] = $page; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// adds section to pages collection |
46
|
|
|
if (count($sections) > 0) { |
47
|
|
|
$menuWeight = 100; |
48
|
|
|
foreach ($sections as $section => $pagesAsArray) { |
49
|
|
|
$pageId = $path = Page::slugify($section); |
50
|
|
|
$page = (new Page($pageId))->setVariable('title', ucfirst($section)); |
51
|
|
|
if ($this->builder->getPages()->has($pageId)) { |
52
|
|
|
$page = clone $this->builder->getPages()->get($pageId); |
53
|
|
|
} |
54
|
|
|
$pages = new PagesCollection($section, $pagesAsArray); |
55
|
|
|
// sorts |
56
|
|
|
$pages = $pages->sortByDate(); |
57
|
|
|
/** @var \Cecil\Collection\Page\Page $page */ |
58
|
|
|
if ($page->getVariable('sortby')) { |
59
|
|
|
$sortMethod = sprintf('sortBy%s', ucfirst($page->getVariable('sortby'))); |
60
|
|
|
if (!method_exists($pages, $sortMethod)) { |
61
|
|
|
throw new Exception(sprintf( |
62
|
|
|
'In page "%s" the value "%s" is not valid for "sortby" variable.', |
63
|
|
|
$page->getId(), |
64
|
|
|
$page->getVariable('sortby') |
65
|
|
|
)); |
66
|
|
|
} |
67
|
|
|
$pages = $pages->$sortMethod(); |
68
|
|
|
} |
69
|
|
|
// adds navigation links |
70
|
|
|
$this->addNavigationLinks($pages, $page->getVariable('sortby')); |
71
|
|
|
// creates page for each section |
72
|
|
|
$page->setPath($path) |
73
|
|
|
->setType(Type::SECTION) |
74
|
|
|
->setVariable('pages', $pages) |
75
|
|
|
->setVariable('date', $pages->first()->getVariable('date')); |
76
|
|
|
// default menu |
77
|
|
|
if (!$page->getVariable('menu')) { |
78
|
|
|
$page->setVariable('menu', [ |
79
|
|
|
'main' => ['weight' => $menuWeight], |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
$this->generatedPages->add($page); |
83
|
|
|
$menuWeight += 10; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Adds navigation (next and prev) to section sub pages. |
90
|
|
|
* |
91
|
|
|
* @param PagesCollection $pages |
92
|
|
|
* @param string $sort |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
protected function addNavigationLinks(PagesCollection $pages, string $sort = null): void |
97
|
|
|
{ |
98
|
|
|
$pagesAsArray = $pages->toArray(); |
99
|
|
|
if ($sort === null || $sort == 'date') { |
100
|
|
|
$pagesAsArray = array_reverse($pagesAsArray); |
101
|
|
|
} |
102
|
|
|
if (count($pagesAsArray) > 1) { |
103
|
|
|
foreach ($pagesAsArray as $position => $page) { |
104
|
|
|
switch ($position) { |
105
|
|
|
// first |
106
|
|
|
case 0: |
107
|
|
|
$page->setVariables([ |
108
|
|
|
'next' => [ |
109
|
|
|
'id' => $pagesAsArray[$position + 1]->getId(), |
110
|
|
|
'path' => $pagesAsArray[$position + 1]->getPath(), |
111
|
|
|
'title' => $pagesAsArray[$position + 1]->getVariable('title'), |
112
|
|
|
], |
113
|
|
|
]); |
114
|
|
|
break; |
115
|
|
|
// last |
116
|
|
|
case count($pagesAsArray) - 1: |
117
|
|
|
$page->setVariables([ |
118
|
|
|
'prev' => [ |
119
|
|
|
'id' => $pagesAsArray[$position - 1]->getId(), |
120
|
|
|
'path' => $pagesAsArray[$position - 1]->getPath(), |
121
|
|
|
'title' => $pagesAsArray[$position - 1]->getVariable('title'), |
122
|
|
|
], |
123
|
|
|
]); |
124
|
|
|
break; |
125
|
|
|
default: |
126
|
|
|
$page->setVariables([ |
127
|
|
|
'prev' => [ |
128
|
|
|
'id' => $pagesAsArray[$position - 1]->getId(), |
129
|
|
|
'path' => $pagesAsArray[$position - 1]->getPath(), |
130
|
|
|
'title' => $pagesAsArray[$position - 1]->getVariable('title'), |
131
|
|
|
], |
132
|
|
|
'next' => [ |
133
|
|
|
'id' => $pagesAsArray[$position + 1]->getId(), |
134
|
|
|
'path' => $pagesAsArray[$position + 1]->getPath(), |
135
|
|
|
'title' => $pagesAsArray[$position + 1]->getVariable('title'), |
136
|
|
|
], |
137
|
|
|
]); |
138
|
|
|
break; |
139
|
|
|
} |
140
|
|
|
$this->generatedPages->add($page); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|