1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbterPhp\Website\Template\Builder\PageCategory; |
4
|
|
|
|
5
|
|
|
use AbterPhp\Framework\Constant\Html5; |
6
|
|
|
use AbterPhp\Framework\Html\Component; |
7
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
8
|
|
|
use AbterPhp\Framework\Template\Data; |
9
|
|
|
use AbterPhp\Framework\Template\IBuilder; |
10
|
|
|
use AbterPhp\Framework\Template\IData; |
11
|
|
|
use AbterPhp\Website\Constant\Event; |
12
|
|
|
use AbterPhp\Website\Constant\Routes; |
13
|
|
|
use AbterPhp\Website\Domain\Entities\Page as Entity; |
14
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
15
|
|
|
use Opulence\Routing\Urls\UrlGenerator; |
16
|
|
|
|
17
|
|
|
class Detailed implements IBuilder |
18
|
|
|
{ |
19
|
|
|
const IDENTIFIER = 'detailed'; |
20
|
|
|
|
21
|
|
|
const LIST_ITEM_TEMPLATE = '<li><a href="%s">%s</a></li>'; |
22
|
|
|
const LIST_TEMPLATE = '<ul>%s</ul>'; |
23
|
|
|
const LIST_CONTAINER_TEMPLATE = '<div class="page-categories"><h2>%s</h2>%s</div>'; |
24
|
|
|
|
25
|
|
|
const MORE_BTN_CONTAINER_CLASS = 'more-btn-container'; |
26
|
|
|
|
27
|
|
|
/** @var IEventDispatcher */ |
28
|
|
|
protected $dispatcher; |
29
|
|
|
|
30
|
|
|
/** @var UrlGenerator */ |
31
|
|
|
protected $urlGenerator; |
32
|
|
|
|
33
|
|
|
/** @var ITranslator */ |
34
|
|
|
protected $translator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* PageCategory constructor. |
38
|
|
|
* |
39
|
|
|
* @param IEventDispatcher $dispatcher |
40
|
|
|
* @param UrlGenerator $urlGenerator |
41
|
|
|
* @param ITranslator $translator |
42
|
|
|
*/ |
43
|
|
|
public function __construct(IEventDispatcher $dispatcher, UrlGenerator $urlGenerator, ITranslator $translator) |
44
|
|
|
{ |
45
|
|
|
$this->dispatcher = $dispatcher; |
46
|
|
|
$this->urlGenerator = $urlGenerator; |
47
|
|
|
$this->translator = $translator; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getIdentifier(): string |
54
|
|
|
{ |
55
|
|
|
return static::IDENTIFIER; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Entity[] $pages |
60
|
|
|
* |
61
|
|
|
* @return Data |
62
|
|
|
*/ |
63
|
|
|
public function build(array $pages): IData |
64
|
|
|
{ |
65
|
|
|
$category = $pages[0]->getCategory(); |
66
|
|
|
|
67
|
|
|
$body = $this->buildCategory($pages, $category->getName(), $category->getIdentifier()); |
68
|
|
|
|
69
|
|
|
$this->dispatcher->dispatch(Event::PAGE_CATEGORY_READY, $body); |
70
|
|
|
|
71
|
|
|
return new Data( |
72
|
|
|
$category->getIdentifier(), |
73
|
|
|
[], |
74
|
|
|
['body' => (string)$body] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Entity[] $pages |
80
|
|
|
* @param string $categoryName |
81
|
|
|
* @param string $categoryIdentifier |
82
|
|
|
* |
83
|
|
|
* @return Component |
84
|
|
|
*/ |
85
|
|
|
protected function buildCategory(array $pages, string $categoryName, string $categoryIdentifier): Component |
86
|
|
|
{ |
87
|
|
|
$container = new Component(null, [], [Html5::ATTR_CLASS => 'page-category'], Html5::TAG_SECTION); |
88
|
|
|
|
89
|
|
|
if (count($pages) === 0) { |
90
|
|
|
return $container; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$list = new Component(null, [], [Html5::ATTR_CLASS => 'page-container'], Html5::TAG_DIV); |
94
|
|
|
foreach ($pages as $page) { |
95
|
|
|
$list[] = $this->buildPage($page); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$url = $this->urlGenerator->createFromName(Routes::ROUTE_FALLBACK, $categoryIdentifier); |
99
|
|
|
$a = new Component($categoryName, [], [Html5::ATTR_HREF => $url], Html5::TAG_A); |
100
|
|
|
|
101
|
|
|
$container[] = new Component($a, [], [], Html5::TAG_H2); |
102
|
|
|
$container[] = $list; |
103
|
|
|
|
104
|
|
|
return $container; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Entity $page |
109
|
|
|
* |
110
|
|
|
* @return Component |
111
|
|
|
*/ |
112
|
|
|
protected function buildPage(Entity $page): Component |
113
|
|
|
{ |
114
|
|
|
$item = new Component(null, [], [], Html5::TAG_ARTICLE); |
115
|
|
|
|
116
|
|
|
$url = $this->urlGenerator->createFromName(Routes::ROUTE_FALLBACK, $page->getIdentifier()); |
117
|
|
|
|
118
|
|
|
$item[] = $this->buildPageTitle($page, $url); |
119
|
|
|
$item[] = $this->buildPageLead($page); |
120
|
|
|
$item[] = $this->buildPageButtons($page, $url); |
121
|
|
|
|
122
|
|
|
return $item; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param Entity $page |
127
|
|
|
* @param string $url |
128
|
|
|
* |
129
|
|
|
* @return Component |
130
|
|
|
*/ |
131
|
|
|
protected function buildPageTitle(Entity $page, string $url): Component |
132
|
|
|
{ |
133
|
|
|
$a = new Component($page->getTitle(), [], [Html5::ATTR_HREF => $url], Html5::TAG_A); |
134
|
|
|
|
135
|
|
|
return new Component($a, [], [], Html5::TAG_H3); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param Entity $page |
140
|
|
|
* |
141
|
|
|
* @return Component |
142
|
|
|
*/ |
143
|
|
|
protected function buildPageLead(Entity $page): Component |
144
|
|
|
{ |
145
|
|
|
$lead = new Component(null, [], [], Html5::TAG_DIV); |
146
|
|
|
foreach (explode("\n", $page->getLead()) as $paragraph) { |
147
|
|
|
if (trim($paragraph) === '') { |
148
|
|
|
continue; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$lead[] = new Component($paragraph, [], [], Html5::TAG_P); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $lead; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param Entity $page |
159
|
|
|
* @param string $url |
160
|
|
|
* |
161
|
|
|
* @return Component |
162
|
|
|
*/ |
163
|
|
|
protected function buildPageButtons(Entity $page, string $url): Component |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$iconHtml = '<i class="fas fa-angle-right"></i>'; |
166
|
|
|
$aContent = sprintf('%s %s', $this->translator->translate('website:more'), $iconHtml); |
167
|
|
|
|
168
|
|
|
$a = new Component($aContent, [], [Html5::ATTR_HREF => $url], Html5::TAG_A); |
169
|
|
|
$p = new Component($a, [], [], Html5::TAG_P); |
170
|
|
|
$buttons = new Component($p, [], [Html5::ATTR_CLASS => static::MORE_BTN_CONTAINER_CLASS], Html5::TAG_DIV); |
171
|
|
|
|
172
|
|
|
return $buttons; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.