/
attributes.php
158 lines (135 loc) · 4.51 KB
/
attributes.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
<?php
namespace Concrete\Controller\Dialog\Type;
use Concrete\Controller\Backend\UserInterface as BackendInterfaceController;
use Concrete\Core\Application\EditResponse;
use Concrete\Core\Attribute\Category\CategoryInterface;
use Concrete\Core\Attribute\Category\CategoryService;
use Concrete\Core\Attribute\Key\Component\KeySelector\ControllerTrait as KeySelectorControllerTrait;
use Concrete\Core\Error\ErrorList\ErrorList;
use Concrete\Core\Error\UserMessageException;
use Concrete\Core\Filesystem\ElementManager;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Type\Type as PageType;
use Concrete\Core\Permission\Checker;
use Concrete\Core\Permission\Key\Key;
use Symfony\Component\HttpFoundation\JsonResponse;
class Attributes extends BackendInterfaceController
{
use KeySelectorControllerTrait;
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Controller\Controller::$viewPath
*/
protected $viewPath = '/dialogs/type/attributes';
/**
* @var CategoryInterface
*/
protected $category;
/**
* @var PageType|null
*/
protected $requestPageType;
/**
* @var int[]
*/
protected $allowedEditAttributes;
/**
* @var Page
*/
private $defaultPage;
public function __construct(CategoryService $attributeCategoryService)
{
parent::__construct();
$categoryEntity = $attributeCategoryService->getByHandle('collection');
$this->category = $categoryEntity->getAttributeKeyCategory();
}
/**
* {@inheritdoc}
*
* @see \Concrete\Controller\Backend\UserInterface::canAccess()
*/
public function canAccess()
{
$permissions = new Checker($this->getPageTypeDefaultPage());
if ($permissions->canEditPageProperties()) {
$pk = Key::getByHandle('edit_page_properties');
$assignment = $pk->getMyAssignment();
if ($assignment) {
$this->allowedEditAttributes = $assignment->getAttributesAllowedArray();
return count($this->allowedEditAttributes) > 0;
}
}
return false;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Attribute\Key\Component\KeySelector\ControllerTrait::getObjects()
*/
public function getObjects(): array
{
return [$this->getPageTypeDefaultPage()];
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Attribute\Key\Component\KeySelector\ControllerTrait::getCategory()
*/
public function getCategory(): CategoryInterface
{
return $this->category;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Attribute\Key\Component\KeySelector\ControllerTrait::canEditAttributeKey()
*/
public function canEditAttributeKey(int $akID): bool
{
return in_array($akID, $this->allowedEditAttributes);
}
public function view($ptID)
{
$keySelector = $this->app->make(ElementManager::class)->get('attribute/component/key_selector', [
'category' => $this->getCategory()
]);
/** @var \Concrete\Controller\Element\Attribute\Component\KeySelector $controller */
$controller = $keySelector->getElementController();
$controller->setSelectAttributeUrl($this->action('get_attribute'));
$controller->setObjects($this->getObjects());
$this->set('keySelector', $keySelector);
}
public function submit()
{
if ($this->validateAction()) {
$attributesResponse = $this->saveAttributes();
$message = new EditResponse();
if ($attributesResponse instanceof ErrorList) {
$message->setError($attributesResponse);
} else {
$message->setMessage(t('Attributes updated successfully.'));
}
return new JsonResponse($message);
}
}
protected function getPageTypeDefaultPage(): Page
{
if (!$this->defaultPage) {
$this->defaultPage = $this->getPageTypeFromRequest()->getPageTypePageTemplateDefaultPageObject();
}
return $this->defaultPage;
}
protected function getPageTypeFromRequest()
{
if (!isset($this->requestPageType)) {
if ($this->request->attributes->has('ptID')) {
$this->requestPageType = PageType::getByID($this->request->attributes->get('ptID'));
}
}
if (!$this->requestPageType) {
throw new UserMessageException(t('Invalid page type id.'));
}
return $this->requestPageType;
}
}