|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Website\Service\Execute; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Service\Execute\RepoServiceAbstract; |
|
8
|
|
|
use AbterPhp\Framework\Constant\Session; |
|
9
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
|
10
|
|
|
use AbterPhp\Website\Constant\Authorization; |
|
11
|
|
|
use AbterPhp\Website\Domain\Entities\ContentList as Entity; |
|
12
|
|
|
use AbterPhp\Website\Domain\Entities\ContentListItem as Item; |
|
13
|
|
|
use AbterPhp\Website\Orm\ContentListRepo as GridRepo; |
|
14
|
|
|
use AbterPhp\Website\Validation\Factory\ContentList as ValidatorFactory; |
|
15
|
|
|
use Casbin\Enforcer; |
|
16
|
|
|
use Cocur\Slugify\Slugify; |
|
17
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
|
18
|
|
|
use Opulence\Http\Requests\UploadedFile; |
|
19
|
|
|
use Opulence\Orm\IUnitOfWork; |
|
20
|
|
|
use Opulence\Sessions\ISession; |
|
21
|
|
|
|
|
22
|
|
|
class ContentList extends RepoServiceAbstract |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var Slugify */ |
|
25
|
|
|
protected $slugify; |
|
26
|
|
|
|
|
27
|
|
|
/** @var GridRepo */ |
|
28
|
|
|
protected $repo; |
|
29
|
|
|
|
|
30
|
|
|
/** @var ISession */ |
|
31
|
|
|
protected $session; |
|
32
|
|
|
|
|
33
|
|
|
/** @var Enforcer */ |
|
34
|
|
|
protected $enforcer; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* ContentList constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param GridRepo $repo |
|
40
|
|
|
* @param ValidatorFactory $validatorFactory |
|
41
|
|
|
* @param IUnitOfWork $unitOfWork |
|
42
|
|
|
* @param IEventDispatcher $eventDispatcher |
|
43
|
|
|
* @param Slugify $slugify |
|
44
|
|
|
* @param ISession $session |
|
45
|
|
|
* @param Enforcer $enforcer |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
GridRepo $repo, |
|
49
|
|
|
ValidatorFactory $validatorFactory, |
|
50
|
|
|
IUnitOfWork $unitOfWork, |
|
51
|
|
|
IEventDispatcher $eventDispatcher, |
|
52
|
|
|
Slugify $slugify, |
|
53
|
|
|
ISession $session, |
|
54
|
|
|
Enforcer $enforcer |
|
55
|
|
|
) { |
|
56
|
|
|
parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher); |
|
57
|
|
|
|
|
58
|
|
|
$this->slugify = $slugify; |
|
59
|
|
|
$this->session = $session; |
|
60
|
|
|
$this->enforcer = $enforcer; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $entityId |
|
65
|
|
|
* |
|
66
|
|
|
* @return Entity |
|
67
|
|
|
*/ |
|
68
|
|
|
public function createEntity(string $entityId): IStringerEntity |
|
69
|
|
|
{ |
|
70
|
|
|
return new Entity($entityId, '', '', '', false, false, false, false, false, false); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
75
|
|
|
* |
|
76
|
|
|
* @param IStringerEntity $entity |
|
77
|
|
|
* @param array $postData |
|
78
|
|
|
* @param UploadedFile[] $fileData |
|
79
|
|
|
* |
|
80
|
|
|
* @return Entity |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity |
|
83
|
|
|
{ |
|
84
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException()); |
|
85
|
|
|
|
|
86
|
|
|
$postData = $this->protectPostData($entity, $postData); |
|
87
|
|
|
|
|
88
|
|
|
$name = (string)$postData['name']; |
|
89
|
|
|
|
|
90
|
|
|
$identifier = $postData['identifier'] ?? $entity->getIdentifier(); |
|
91
|
|
|
$identifier = $identifier ?: $name; |
|
92
|
|
|
$identifier = $this->slugify->slugify($identifier); |
|
93
|
|
|
|
|
94
|
|
|
$classes = $postData['classes']; |
|
95
|
|
|
|
|
96
|
|
|
$protected = !empty($postData['protected']); |
|
97
|
|
|
$withLinks = !empty($postData['with_links']); |
|
98
|
|
|
$withLabelLinks = !empty($postData['with_label_links']); |
|
99
|
|
|
$withHtml = !empty($postData['with_html']); |
|
100
|
|
|
$withImages = !empty($postData['with_images']); |
|
101
|
|
|
$withClasses = !empty($postData['with_classes']); |
|
102
|
|
|
|
|
103
|
|
|
$items = $this->createItems($postData, $entity->getId()); |
|
104
|
|
|
|
|
105
|
|
|
$entity |
|
106
|
|
|
->setIdentifier($identifier) |
|
107
|
|
|
->setClasses($classes) |
|
108
|
|
|
->setName($name) |
|
109
|
|
|
->setProtected($protected) |
|
110
|
|
|
->setWithLinks($withLinks) |
|
111
|
|
|
->setWithLabelLinks($withLabelLinks) |
|
112
|
|
|
->setWithHtml($withHtml) |
|
113
|
|
|
->setWithImages($withImages) |
|
114
|
|
|
->setWithClasses($withClasses) |
|
115
|
|
|
->setItems($items); |
|
116
|
|
|
|
|
117
|
|
|
return $entity; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param Entity $entity |
|
122
|
|
|
* @param array $postData |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
* @throws \Casbin\Exceptions\CasbinException |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function protectPostData(Entity $entity, array $postData): array |
|
128
|
|
|
{ |
|
129
|
|
|
if (!$entity->isProtected()) { |
|
130
|
|
|
return $postData; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$username = $this->session->get(Session::USERNAME); |
|
134
|
|
|
$advancedAllowed = $this->enforcer->enforce( |
|
135
|
|
|
$username, |
|
136
|
|
|
Authorization::RESOURCE_LISTS, |
|
137
|
|
|
Authorization::ROLE_ADVANCED_WRITE |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
|
|
if ($advancedAllowed) { |
|
141
|
|
|
return $postData; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$postData['identifier'] = $entity->getIdentifier(); |
|
145
|
|
|
$postData['protected'] = $entity->isProtected(); |
|
146
|
|
|
$postData['with_links'] = $entity->isWithLinks(); |
|
147
|
|
|
$postData['with_label_links'] = $entity->isWithLabelLinks(); |
|
148
|
|
|
$postData['with_html'] = $entity->isWithHtml(); |
|
149
|
|
|
$postData['with_images'] = $entity->isWithImages(); |
|
150
|
|
|
$postData['with_classes'] = $entity->isWithClasses(); |
|
151
|
|
|
|
|
152
|
|
|
return $postData; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param array $postData |
|
157
|
|
|
* @param string $listId |
|
158
|
|
|
* |
|
159
|
|
|
* @return Item[] |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function createItems(array $postData, string $listId): array |
|
162
|
|
|
{ |
|
163
|
|
|
$items = []; |
|
164
|
|
|
|
|
165
|
|
|
$i = 1; |
|
166
|
|
|
while (isset($postData["new$i"])) { |
|
167
|
|
|
$itemData = $postData["new$i"]; |
|
168
|
|
|
|
|
169
|
|
|
$i++; |
|
170
|
|
|
|
|
171
|
|
|
if (!empty($itemData['is_deleted'])) { |
|
172
|
|
|
continue; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$items[] = $this->createItem($itemData, $listId); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$i = 1; |
|
179
|
|
|
while (isset($postData["existing$i"])) { |
|
180
|
|
|
$items[] = $this->createItem($postData["existing$i"], $listId); |
|
181
|
|
|
|
|
182
|
|
|
$i++; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $items; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param array $itemData |
|
190
|
|
|
* @param string $listId |
|
191
|
|
|
* |
|
192
|
|
|
* @return Item |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function createItem(array $itemData, string $listId): Item |
|
195
|
|
|
{ |
|
196
|
|
|
$itemId = $itemData['id'] ?? ''; |
|
197
|
|
|
$label = $itemData['label'] ?? ''; |
|
198
|
|
|
$labelHref = $itemData['label_href'] ?? ''; |
|
199
|
|
|
$content = $itemData['content'] ?? ''; |
|
200
|
|
|
$contentHref = $itemData['content_href'] ?? ''; |
|
201
|
|
|
$imgSrc = $itemData['img_src'] ?? ''; |
|
202
|
|
|
$imgAlt = $itemData['img_alt'] ?? ''; |
|
203
|
|
|
$imgHref = $itemData['img_href'] ?? ''; |
|
204
|
|
|
$classes = $itemData['classes'] ?? ''; |
|
205
|
|
|
$deleted = !empty($itemData['is_deleted']); |
|
206
|
|
|
|
|
207
|
|
|
return new Item( |
|
208
|
|
|
$itemId, |
|
209
|
|
|
$listId, |
|
210
|
|
|
$label, |
|
211
|
|
|
$labelHref, |
|
212
|
|
|
$content, |
|
213
|
|
|
$contentHref, |
|
214
|
|
|
$imgSrc, |
|
215
|
|
|
$imgAlt, |
|
216
|
|
|
$imgHref, |
|
217
|
|
|
$classes, |
|
218
|
|
|
$deleted |
|
219
|
|
|
); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|