/
DashboardAttributesPageController.php
297 lines (260 loc) · 11.7 KB
/
DashboardAttributesPageController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace Concrete\Core\Page\Controller;
use Concrete\Core\Attribute\AttributeKeyInterface;
use Concrete\Core\Attribute\CategoryObjectInterface;
use Concrete\Core\Attribute\Set;
use Concrete\Core\Attribute\StandardSetManager;
use Concrete\Core\Entity\Attribute\SetKey;
use Concrete\Core\Entity\Attribute\Type;
use Concrete\Core\Error\UserMessageException;
use Symfony\Component\HttpFoundation\JsonResponse;
abstract class DashboardAttributesPageController extends DashboardPageController
{
/**
* Configure the data for the view so that it can render the list of the attributes.
*/
public function renderList()
{
$entity = $this->getCategoryObject();
$category = $entity->getAttributeKeyCategory();
$list = $this->elementManager->get('attribute/key_list');
/**
* @var \Concrete\Controller\Element\Attribute\KeyList $controller
*/
$controller = $list->getElementController();
$controller->setAttributeSets($category->getSetManager()->getAttributeSets());
$controller->setUnassignedAttributeKeys($category->getSetManager()->getUnassignedAttributeKeys());
$controller->setAttributeTypes($category->getAttributeTypes());
$controller->setDashboardPagePath($this->getPageObject()->getCollectionPath());
$controller->setDashboardPageParameters($this->getRequestActionParameters());
if (!$category->getSetManager()->allowAttributeSets()) {
$controller->setEnableSorting(false);
}
$this->set('headerMenu', $this->getHeaderMenu($entity));
$this->set('attributeView', $list);
}
/**
* Configure the data for the view so that it can render the "Add Attribute" page.
*
* @param \Concrete\Core\Entity\Attribute\Type $type The type of the new attribute
* @param \League\Url\UrlInterface|string $backURL the URL to be used when users hit the "Cancel Add" button
*/
public function renderAdd($type, $backURL)
{
$add = $this->elementManager->get('attribute/form', ['type' => $type]);
/**
* @var \Concrete\Controller\Element\Attribute\Form $controller
*/
$controller = $add->getElementController();
$controller->setBackButtonURL($backURL);
$controller->setCategory($this->getCategoryObject());
$controller->setDashboardPageParameters($this->getRequestActionParameters());
$this->set('attributeView', $add);
$this->set('pageTitle', t('Add Attribute'));
}
/**
* Configure the data for the view so that it can render the "Edit Attribute" page.
*
* @param \Concrete\Core\Attribute\AttributeKeyInterface $key the key to be modified
* @param \League\Url\UrlInterface|string $backURL the URL to be used when users hit the "Cancel Add" button
*/
public function renderEdit($key, $backURL)
{
$edit = $this->elementManager->get('attribute/edit_key', ['key' => $key]);
/**
* @var \Concrete\Controller\Element\Attribute\EditKey $controller
*/
$controller = $edit->getElementController();
$controller->setBackButtonURL($backURL);
$controller->setCategory($this->getCategoryObject());
$controller->setDashboardPageParameters($this->getRequestActionParameters());
$this->set('attributeView', $edit);
$this->set('pageTitle', t('Edit Attribute'));
$header = $this->elementManager->get('attribute/key_header', ['key' => $key]);
/**
* @var \Concrete\Controller\Element\Attribute\EditKey $headerController
*/
$headerController = $header->getElementController();
$headerController->setDashboardPageParameters($this->getRequestActionParameters());
$this->set('headerMenu', $headerController);
}
/**
* Sort the attributes belinging to a set, reading the data from the request.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function sort_attribute_set()
{
$entity = $this->getCategoryObject();
$category = $entity->getAttributeKeyCategory();
if ($category->getSetManager()->allowAttributeSets()) {
$keys = [];
foreach ((array) $this->request->request->get('akID') as $akID) {
$key = $category->getAttributeKeyByID($akID);
if (is_object($key)) {
$keys[] = $key;
}
}
foreach ($category->getSetManager()->getAttributeSets() as $set) {
if ($set->getAttributeSetID() == $this->request->request->get('asID') && count($keys)) {
// Clear the keys
foreach ($set->getAttributeKeyCollection() as $setKey) {
$this->entityManager->remove($setKey);
}
$this->entityManager->flush();
$i = 0;
foreach ($keys as $key) {
$setKey = new SetKey();
$setKey->setAttributeKey($key);
$setKey->setAttributeSet($set);
$setKey->setDisplayOrder($i);
$set->getAttributeKeyCollection()->add($setKey);
$i++;
}
break;
}
}
$this->entityManager->persist($set);
$this->entityManager->flush();
return new JsonResponse($set);
}
}
/**
* Get the attribute category we are working on.
*
* @return \Concrete\Core\Attribute\CategoryObjectInterface
*/
abstract protected function getCategoryObject();
/**
* Get the controller of the element to be placed in the header of the "Attribute List" page.
*
* @param \Concrete\Core\Attribute\CategoryObjectInterface $category
*
* @return \Concrete\Core\Controller\ElementController|null
*/
protected function getHeaderMenu(CategoryObjectInterface $category)
{
return $this->elementManager->get('attribute/standard_list_header', ['category' => $category])->getElementController();
}
/**
* Create a new attribute key for the specified type, reading the type-specific data from the current request.
*
* @param \Concrete\Core\Entity\Attribute\Type $type the type of the attribute to be created
* @param \League\Url\UrlInterface|string $successURL where to redirect the users when the operation succeedes
* @param callable|null $onComplete a callback function that's called right after the new attribute key is created
*/
protected function executeAdd(Type $type, $successURL, $onComplete = null)
{
$entity = $this->getCategoryObject();
$category = $entity->getAttributeKeyCategory();
$validator = $type->getController()->getValidator();
$response = $validator->validateAddKeyRequest($category, $type, $this->request);
if (!$response->isValid()) {
$this->error = $response->getErrorObject();
} else {
$key = $category->addFromRequest($type, $this->request);
$this->assignToSetFromRequest($key);
if ($onComplete instanceof \Closure) {
$onComplete();
}
$this->flash('success', t('Attribute created successfully.'));
$this->buildRedirect($successURL)->send();
$this->app->shutdown();
}
}
/**
* Assign an attribute key to the set (which is read from the request).
*
* @param \Concrete\Core\Attribute\AttributeKeyInterface $key
*/
protected function assignToSetFromRequest(AttributeKeyInterface $key)
{
$request = $this->request;
$entity = $this->getCategoryObject();
$category = $entity->getAttributeKeyCategory();
if ($category->getSetManager()->allowAttributeSets()) {
if ($request->request->has('asID')) {
$set = Set::getByID($request->request->get('asID'));
$setKeys = Set::getByAttributeKey($key);
if (in_array($set, $setKeys)) {
// The set is already a part of this key, so we return.
return;
}
}
if ($category->getSetManager()->allowAttributeSets() == StandardSetManager::ASET_ALLOW_SINGLE || !is_object($set)) {
$query = $this->entityManager->createQuery(
'delete from \Concrete\Core\Entity\Attribute\SetKey sk where sk.attribute_key = :attribute_key'
);
$query->setParameter('attribute_key', $key);
$query->execute();
}
if (is_object($set)) {
$this->entityManager->refresh($set);
// Refresh display order just in case.
$displayOrder = 0;
foreach ($set->getAttributeKeyCollection() as $setKey) {
$setKey->setDisplayOrder($displayOrder);
$this->entityManager->persist($setKey);
$displayOrder++;
}
$setKey = new SetKey();
$setKey->setAttributeKey($key);
$setKey->setAttributeSet($set);
$setKey->setDisplayOrder($displayOrder);
$this->entityManager->persist($setKey);
}
}
$this->entityManager->flush();
}
/**
* Update an existing attribute key, reading the type-specific data from the current request.
*
* @param \Concrete\Core\Attribute\AttributeKeyInterface $key the attribute key to be updated
* @param \League\Url\UrlInterface|string $successURL where to redirect the users when the operation succeedes
* @param callable|null $onComplete a callback function that's called right after the attribute key is updated
*/
protected function executeUpdate(AttributeKeyInterface $key, $successURL, $onComplete = null)
{
$entity = $this->getCategoryObject();
$category = $entity->getAttributeKeyCategory();
$validator = $key->getController()->getValidator();
$response = $validator->validateUpdateKeyRequest($category, $key, $this->request);
if (!$response->isValid()) {
$this->error = $response->getErrorObject();
} else {
$category->updateFromRequest($key, $this->request);
$this->assignToSetFromRequest($key);
if ($onComplete instanceof \Closure) {
$onComplete();
}
$this->flash('success', t('Attribute updated successfully.'));
$this->buildRedirect($successURL)->send();
$this->app->shutdown();
}
}
/**
* Delete an existing attribute key.
*
* @param \Concrete\Core\Attribute\AttributeKeyInterface $key the attribute key to be deleted
* @param \League\Url\UrlInterface|string $successURL where to redirect the users when the operation succeedes
* @param callable|null $onComplete a callback function that's called right after the attribute key is deleted
*/
protected function executeDelete(AttributeKeyInterface $key, $successURL, $onComplete = null)
{
try {
if (!$this->token->validate('delete_attribute')) {
throw new UserMessageException($this->token->getErrorMessage());
}
$this->entityManager->remove($key);
$this->entityManager->flush();
if ($onComplete instanceof \Closure) {
$onComplete();
}
$this->flash('success', t('Attribute deleted successfully.'));
$this->buildRedirect($successURL)->send();
$this->app->shutdown();
} catch (UserMessageException $e) {
$this->error = $e;
}
}
}