1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Form\Factory\ContentList; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Constant\Html5; |
8
|
|
|
use AbterPhp\Framework\Form\Container\CheckboxGroup; |
9
|
|
|
use AbterPhp\Framework\Form\Container\FormGroup; |
10
|
|
|
use AbterPhp\Framework\Form\Element\Input; |
11
|
|
|
use AbterPhp\Framework\Form\Element\Textarea; |
12
|
|
|
use AbterPhp\Framework\Form\Extra\Help; |
13
|
|
|
use AbterPhp\Framework\Form\Label\Label; |
14
|
|
|
use AbterPhp\Framework\Html\Component; |
15
|
|
|
use AbterPhp\Framework\Html\INode; |
16
|
|
|
use AbterPhp\Website\Domain\Entities\ContentListItem as Entity; |
17
|
|
|
|
18
|
|
|
class Item |
19
|
|
|
{ |
20
|
|
|
/** @var int */ |
21
|
|
|
protected static $existingCount = 1; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
protected $protected; |
25
|
|
|
|
26
|
|
|
/** @var bool */ |
27
|
|
|
protected $withImage; |
28
|
|
|
|
29
|
|
|
/** @var bool */ |
30
|
|
|
protected $withLinks; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
protected $id; |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
protected $hiddenAttribs = [Html5::ATTR_TYPE => Input::TYPE_HIDDEN]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Item constructor. |
40
|
|
|
* |
41
|
|
|
* @param bool $protected |
42
|
|
|
* @param bool $withImage |
43
|
|
|
* @param bool $withLinks |
44
|
|
|
*/ |
45
|
|
|
public function __construct(bool $protected, bool $withImage, bool $withLinks) |
46
|
|
|
{ |
47
|
|
|
$this->protected = $protected; |
48
|
|
|
$this->withImage = $withImage; |
49
|
|
|
$this->withLinks = $withLinks; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Entity|null $entity |
54
|
|
|
* |
55
|
|
|
* @return INode[] |
56
|
|
|
*/ |
57
|
|
|
public function create(?Entity $entity = null): array |
58
|
|
|
{ |
59
|
|
|
$count = static::$existingCount++; |
60
|
|
|
$this->id = $entity ? "existing{$count}" : 'new'; |
61
|
|
|
|
62
|
|
|
$components = []; |
63
|
|
|
|
64
|
|
|
if ($entity) { |
65
|
|
|
$components[] = $this->addId($entity); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$components[] = $this->addName($entity); |
69
|
|
|
$components[] = $this->addNameHref($entity); |
70
|
|
|
$components[] = $this->addBody($entity); |
71
|
|
|
$components[] = $this->addBodyHref($entity); |
72
|
|
|
$components[] = $this->addImgSrc($entity); |
73
|
|
|
$components[] = $this->addImgAlt($entity); |
74
|
|
|
$components[] = $this->addImgHref($entity); |
75
|
|
|
$components[] = $this->addIsDeleted(); |
76
|
|
|
|
77
|
|
|
return $components; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Entity $entity |
82
|
|
|
* |
83
|
|
|
* @return INode |
84
|
|
|
*/ |
85
|
|
|
protected function addId(Entity $entity): INode |
86
|
|
|
{ |
87
|
|
|
return new Input("item_id_{$this->id}", "{$this->id}[id]", $entity->getId(), [], $this->hiddenAttribs); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Entity|null $entity |
92
|
|
|
* |
93
|
|
|
* @return INode |
94
|
|
|
*/ |
95
|
|
|
protected function addName(?Entity $entity): INode |
96
|
|
|
{ |
97
|
|
|
$name = $entity ? $entity->getName() : ''; |
98
|
|
|
|
99
|
|
|
$input = new Input("item_name_{$this->id}", "{$this->id}[name]", $name); |
100
|
|
|
$label = new Label("item_name_{$this->id}", 'website:contentListItemName'); |
101
|
|
|
$help = new Help('website:contentListItemNameHelp'); |
102
|
|
|
|
103
|
|
|
return new FormGroup($input, $label, $help); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Entity|null $entity |
108
|
|
|
* |
109
|
|
|
* @return INode |
110
|
|
|
*/ |
111
|
|
|
protected function addNameHref(?Entity $entity): INode |
112
|
|
|
{ |
113
|
|
|
if (!$this->withLinks) { |
114
|
|
|
return new Input( |
115
|
|
|
"item_name_href_{$this->id}", |
116
|
|
|
"item_name_href[{$this->id}]", |
117
|
|
|
'', |
118
|
|
|
[], |
119
|
|
|
$this->hiddenAttribs |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$nameHref = $entity ? $entity->getNameHref() : ''; |
124
|
|
|
|
125
|
|
|
$input = new Input("item_name_href_{$this->id}", "{$this->id}[name_href]", $nameHref); |
126
|
|
|
$label = new Label("item_name_href_{$this->id}", 'website:contentListItemNameHref'); |
127
|
|
|
$help = new Help('website:contentListItemNameHrefHelp'); |
128
|
|
|
|
129
|
|
|
return new FormGroup($input, $label, $help); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param Entity|null $entity |
134
|
|
|
* |
135
|
|
|
* @return INode |
136
|
|
|
*/ |
137
|
|
|
protected function addBody(?Entity $entity): INode |
138
|
|
|
{ |
139
|
|
|
$body = $entity ? $entity->getBody() : ''; |
140
|
|
|
|
141
|
|
|
$input = new Textarea("item_body_{$this->id}", "{$this->id}[body]", $body); |
142
|
|
|
$label = new Label("item_body_{$this->id}", 'website:contentListItemBody'); |
143
|
|
|
$help = new Help('website:contentListItemBodyHelp'); |
144
|
|
|
|
145
|
|
|
return new FormGroup($input, $label, $help); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param Entity|null $entity |
150
|
|
|
* |
151
|
|
|
* @return INode |
152
|
|
|
*/ |
153
|
|
|
protected function addBodyHref(?Entity $entity): INode |
154
|
|
|
{ |
155
|
|
|
if (!$this->withLinks) { |
156
|
|
|
return new Input("item_body_href_{$this->id}", "{$this->id}[body_href]", '', [], $this->hiddenAttribs); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$bodyHref = $entity ? $entity->getBodyHref() : ''; |
160
|
|
|
|
161
|
|
|
$input = new Input("item_body_href_{$this->id}", "{$this->id}[body_href]", $bodyHref); |
162
|
|
|
$label = new Label("item_body_href_{$this->id}", 'website:contentListItemBodyHref'); |
163
|
|
|
$help = new Help('website:contentListItemBodyHrefHelp'); |
164
|
|
|
|
165
|
|
|
return new FormGroup($input, $label, $help); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param Entity|null $entity |
170
|
|
|
* |
171
|
|
|
* @return INode |
172
|
|
|
*/ |
173
|
|
|
protected function addImgSrc(?Entity $entity): INode |
174
|
|
|
{ |
175
|
|
|
if (!$this->withImage) { |
176
|
|
|
return new Input("item_img_src_{$this->id}", "{$this->id}[img_src]", '', [], $this->hiddenAttribs); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$imgSrc = $entity ? $entity->getImgSrc() : ''; |
180
|
|
|
|
181
|
|
|
$input = new Input("item_img_src_{$this->id}", "{$this->id}[img_src]", $imgSrc); |
182
|
|
|
$label = new Label("item_img_src_{$this->id}", 'website:contentListItemImgSrc'); |
183
|
|
|
$help = new Help('website:contentListItemImgSrcHelp'); |
184
|
|
|
|
185
|
|
|
return new FormGroup($input, $label, $help); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param Entity|null $entity |
190
|
|
|
* |
191
|
|
|
* @return INode |
192
|
|
|
*/ |
193
|
|
|
protected function addImgAlt(?Entity $entity): INode |
194
|
|
|
{ |
195
|
|
|
if (!$this->withImage) { |
196
|
|
|
return new Input("item_img_alt_{$this->id}", "{$this->id}[img_alt]", '', [], $this->hiddenAttribs); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$imgAlt = $entity ? $entity->getImgAlt() : ''; |
200
|
|
|
|
201
|
|
|
$input = new Input("item_img_alt_{$this->id}", "{$this->id}[img_alt]", $imgAlt); |
202
|
|
|
$label = new Label("item_img_alt_{$this->id}", 'website:contentListItemImgAlt'); |
203
|
|
|
$help = new Help('website:contentListItemImgAltHelp'); |
204
|
|
|
|
205
|
|
|
return new FormGroup($input, $label, $help); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param Entity|null $entity |
210
|
|
|
* |
211
|
|
|
* @return INode |
212
|
|
|
*/ |
213
|
|
|
protected function addImgHref(?Entity $entity): INode |
214
|
|
|
{ |
215
|
|
|
if (!$this->withImage || !$this->withLinks) { |
216
|
|
|
return new Input("item_img_href_{$this->id}", "{$this->id}[img_href]", '', [], $this->hiddenAttribs); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$imgHref = $entity ? $entity->getImgHref() : ''; |
220
|
|
|
|
221
|
|
|
$input = new Input("item_img_href_{$this->id}", "{$this->id}[img_href]", $imgHref); |
222
|
|
|
$label = new Label("item_img_href_{$this->id}", 'website:contentListItemImgHref'); |
223
|
|
|
$help = new Help('website:contentListItemImgHrefHelp'); |
224
|
|
|
|
225
|
|
|
return new FormGroup($input, $label, $help); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return INode |
230
|
|
|
*/ |
231
|
|
|
protected function addIsDeleted(): INode |
232
|
|
|
{ |
233
|
|
|
$attributes = [Html5::ATTR_TYPE => Input::TYPE_CHECKBOX, Html5::ATTR_WRAP => ['item_is_deleted']]; |
234
|
|
|
|
235
|
|
|
$input = new Input( |
236
|
|
|
"item_is_deleted_{$this->id}", |
237
|
|
|
"{$this->id}[is_deleted]", |
238
|
|
|
'1', |
239
|
|
|
[], |
240
|
|
|
$attributes |
241
|
|
|
); |
242
|
|
|
$label = new Label("item_is_deleted_{$this->id}", 'website:contentListItemIsDeleted'); |
243
|
|
|
$help = new Component('website:contentListItemIsDeletedHelp'); |
244
|
|
|
|
245
|
|
|
return new CheckboxGroup($input, $label, $help, [], [Html5::ATTR_CLASS => 'is-deleted-container']); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|