/
sets.php
207 lines (193 loc) · 8.23 KB
/
sets.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
<?php
namespace Concrete\Controller\SinglePage\Dashboard\System\Attributes;
use Concrete\Core\Attribute\SetManagerInterface;
use Concrete\Core\Entity\Attribute\Key\Key;
use Concrete\Core\Page\Controller\DashboardPageController;
use Loader;
use Concrete\Core\Attribute\Key\Category as AttributeKeyCategory;
use AttributeSet;
class Sets extends DashboardPageController
{
public $category;
public function view()
{
$this->set('categories', AttributeKeyCategory::getList());
}
public function category($categoryID = false, $mode = false)
{
//$this->addHeaderItem('<style type="text/css"> .ccm-attribute-sortable-set-list li a:hover { cursor:move;}</style>');
$this->addFooterItem('<script type="text/javascript">
$(function() {
$("ul.ccm-attribute-sortable-set-list").sortable({
opacity: 0.5,
stop: function() {
var ualist = $(this).sortable(\'serialize\');
ualist += \'&categoryID=' . $categoryID . '\';
$.post(\'' . REL_DIR_FILES_TOOLS_REQUIRED . '/dashboard/attribute_set_order_update\', ualist, function(r) {
});
}
});
});
</script>');
if (!intval($categoryID)) {
$this->redirect('/dashboard/system/attributes/sets');
}
$this->category = AttributeKeyCategory::getByID($categoryID);
if (is_object($this->category)) {
$sets = $this->category->getController()->getSetManager()->getAttributeSets();
$this->set('sets', $sets);
} else {
$this->redirect('/dashboard/system/attributes/sets');
}
$this->set('categoryID', $categoryID);
switch ($mode) {
case 'set_added':
$this->set('message', t('Attribute set added.'));
break;
case 'set_deleted':
$this->set('message', t('Attribute set deleted.'));
break;
case 'set_updated':
$this->set('message', t('Attribute set updated.'));
break;
}
}
public function add_set()
{
$this->category($this->post('categoryID'));
if ($this->token->validate('add_set')) {
if (!trim($this->post('asHandle'))) {
$this->error->add(t("Specify a handle for your attribute set."));
} else {
$as = AttributeSet::getByHandle($this->post('asHandle'));
if (is_object($as)) {
$this->error->add(t('That handle is in use.'));
}
}
if (!trim($this->post('asName'))) {
$this->error->add(t("Specify a name for your attribute set."));
} else {
if (preg_match('/[<>;{}?"`]/', trim($this->post('asName')))) {
$this->error->add(t('Name cannot contain the characters: %s',
Loader::helper('text')->entities('<>;{}?`')));
}
}
if (!$this->error->has()) {
/**
* @var $manager SetManagerInterface
*/
$manager = $this->category->getController()->getSetManager();
if ($manager->allowAttributeSets()) {
$manager->addSet($this->post('asHandle'), $this->post('asName'), false, 0);
$this->redirect('dashboard/system/attributes/sets', 'category',
$this->category->getAttributeKeyCategoryID(), 'set_added');
}
}
} else {
$this->error->add($this->token->getErrorMessage());
}
}
public function update_set()
{
$this->edit($this->post('asID'));
if ($this->token->validate('update_set')) {
$as = AttributeSet::getByID($this->post('asID'));
if (!is_object($as)) {
$this->error->add(t('Invalid attribute set.'));
} else {
if (!trim($this->post('asHandle')) && (!$as->isAttributeSetLocked())) {
$this->error->add(t("Specify a handle for your attribute set."));
} else {
$asx = AttributeSet::getByHandle($this->post('asHandle'));
if (is_object($asx) && $asx->getAttributeSetID() != $as->getAttributeSetID()) {
$this->error->add(t('That handle is in use.'));
}
}
if (!trim($this->post('asName'))) {
$this->error->add(t("Specify a name for your attribute set."));
}
}
if (!$this->error->has()) {
if (!$as->isAttributeSetLocked()) {
$as->setAttributeSetHandle($this->post('asHandle'));
}
$as->setAttributeSetName($this->post('asName'));
$this->entityManager->persist($as);
$this->entityManager->flush();
$this->redirect('dashboard/system/attributes/sets', 'category', $as->getAttributeSetKeyCategoryID(),
'set_updated');
}
} else {
$this->error->add($this->token->getErrorMessage());
}
}
public function update_set_attributes()
{
if ($this->token->validate('update_set_attributes')) {
$as = AttributeSet::getByID($this->post('asID'));
if (!is_object($as)) {
$this->error->add(t('Invalid attribute set.'));
}
if (!$this->error->has()) {
// go through and add all the attributes that aren't in another set
foreach ($as->getAttributeKeyCollection() as $setKey) {
$this->entityManager->remove($setKey);
}
$this->entityManager->flush();
$cat = AttributeKeyCategory::getByID($as->getAttributeSetKeyCategoryID());
$category = $cat->getAttributeKeyCategory();
$unassigned = $category->getSetManager()->getUnassignedAttributeKeys();
if (is_array($this->post('akID'))) {
foreach ($unassigned as $ak) {
if (in_array($ak->getAttributeKeyID(), $this->post('akID'))) {
// This is a lame hack but our legacy support is biting us here:
if (!($ak instanceof Key)) {
$ak = \Concrete\Core\Attribute\Key\Key::getByID($ak->getAttributeKeyID());
}
$as->addKey($ak);
}
}
$this->entityManager->persist($as);
$this->entityManager->flush();
}
$this->redirect('dashboard/system/attributes/sets', 'category', $cat->getAttributeKeyCategoryID(),
'set_updated');
}
} else {
$this->error->add($this->token->getErrorMessage());
}
$this->edit($this->post('asID'));
}
public function delete_set()
{
if ($this->token->validate('delete_set')) {
$as = AttributeSet::getByID($this->post('asID'));
if (!is_object($as)) {
$this->error->add(t('Invalid attribute set.'));
} else {
if ($as->isAttributeSetLocked()) {
$this->error->add(t('This attribute set is locked. That means it was added by a package and cannot be manually removed.'));
$this->edit($as->getAttributeSetID());
}
}
if (!$this->error->has()) {
$categoryID = $as->getAttributeSetKeyCategoryID();
$this->entityManager->remove($as);
$this->entityManager->flush();
$this->redirect('dashboard/system/attributes/sets', 'category', $categoryID, 'set_deleted');
}
} else {
$this->error->add($this->token->getErrorMessage());
}
$this->view();
}
public function edit($asID = false)
{
$as = AttributeSet::getByID($asID);
if (is_object($as)) {
$this->set('set', $as);
} else {
$this->redirect('/dashboard/system/attributes/sets');
}
}
}