/
controller.php
495 lines (439 loc) · 16.6 KB
/
controller.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
namespace Concrete\Attribute\Topics;
use Concrete\Core\Attribute\Controller as AttributeTypeController;
use Concrete\Core\Attribute\FontAwesomeIconFormatter;
use Concrete\Core\Attribute\SimpleTextExportableAttributeInterface;
use Concrete\Core\Entity\Attribute\Key\Settings\TopicsSettings;
use Concrete\Core\Entity\Attribute\Value\Value\SelectedTopic;
use Concrete\Core\Entity\Attribute\Value\Value\TopicsValue;
use Concrete\Core\Error\ErrorList\ErrorList;
use Concrete\Core\Search\ItemList\Database\AttributedItemList;
use Concrete\Core\Tree\Node\Node;
use Concrete\Core\Tree\Node\Node as TreeNode;
use Concrete\Core\Tree\Node\Type\Category;
use Concrete\Core\Tree\Node\Type\Topic;
use Concrete\Core\Tree\Tree;
use Concrete\Core\Tree\Type\Topic as TopicTree;
use Concrete\Core\Utility\Service\Xml;
use Core;
use Database;
class Controller extends AttributeTypeController implements SimpleTextExportableAttributeInterface
{
public $akTopicParentNodeID;
public $akTopicTreeID;
public $helpers = ['form'];
protected $searchIndexFieldDefinition = ['type' => 'text', 'options' => ['default' => null, 'notnull' => false]];
private $akTopicAllowMultipleValues = true;
public function getIconFormatter()
{
return new FontAwesomeIconFormatter('tag');
}
public function getAttributeValueClass()
{
return TopicsValue::class;
}
public function filterByAttribute(AttributedItemList $list, $value, $comparison = '=')
{
if (is_array($value)) {
$topics = $value;
} else {
$topics = [$value];
}
$expressions = [];
$qb = $list->getQueryObject();
foreach ($topics as $value) {
if ($value instanceof TreeNode) {
$topic = $value;
} else {
$topic = Node::getByID((int) $value);
}
if (is_object($topic) && (
$topic instanceof \Concrete\Core\Tree\Node\Type\Topic ||
$topic instanceof Category)) {
$column = 'ak_' . $this->attributeKey->getAttributeKeyHandle();
$expressions[] = $qb->expr()->like($column, $qb->createNamedParameter('%||' . $topic->getTreeNodeDisplayPath() . '%||'));
}
}
$expr = $qb->expr();
$qb->andWhere(call_user_func_array([$expr, 'orX'], $expressions));
}
public function saveKey($data)
{
$type = $this->getAttributeKeySettings();
$data += [
'akTopicParentNodeID' => null,
'akTopicTreeID' => null,
];
$akTopicParentNodeID = $data['akTopicParentNodeID'];
$akTopicTreeID = $data['akTopicTreeID'];
if (isset($data['akTopicAllowMultipleValues']) && $data['akTopicAllowMultipleValues'] == 1) {
$akTopicAllowMultipleValues = 1;
} else {
$akTopicAllowMultipleValues = 0;
}
if ($akTopicParentNodeID) {
$type->setParentNodeID($akTopicParentNodeID);
}
if ($akTopicTreeID) {
$type->setTopicTreeID($akTopicTreeID);
}
$type->setAllowMultipleValues((bool) $akTopicAllowMultipleValues);
return $type;
}
/**
* @deprecated
*/
public function getSelectedOptions()
{
return $this->attributeValue->getValueObject()->getSelectedTopics();
}
public function exportValue(\SimpleXMLElement $akn)
{
/** @var Xml $xml */
$xml = \Core::make('helper/xml');
$avn = $akn->addChild('topics');
$nodes = $this->attributeValue->getValue();
foreach ($nodes as $topic) {
$xml->createCDataNode($avn, 'topic', $topic->getTreeNodeDisplayPath());
}
}
public function importValue(\SimpleXMLElement $akn)
{
$selected = [];
if (isset($akn->topics)) {
foreach ($akn->topics->topic as $topicPath) {
$selected[] = (string) $topicPath;
}
return $this->createAttributeValue($selected);
}
}
/**
* @deprecated
*
* @param mixed $akTopicParentNodeID
* @param mixed $akTopicTreeID
*/
public function setNodes($akTopicParentNodeID, $akTopicTreeID)
{
/** @var TopicsSettings $type */
$type = $this->getAttributeKey()->getAttributeKeySettings();
$type->setParentNodeID($akTopicParentNodeID);
$type->setTopicTreeID($akTopicTreeID);
$this->entityManager->persist($type);
$this->entityManager->flush();
}
public function createAttributeValue($nodes)
{
$selected = [];
$this->load();
$tree = Tree::getByID($this->akTopicTreeID);
if ($nodes instanceof Topic) {
$selected[] = $nodes->getTreeNodeID();
} else {
foreach ($nodes as $topicPath) {
if ($topicPath instanceof Topic) {
$selected[] = $topicPath->getTreeNodeID();
} else {
$node = $tree->getNodeByDisplayPath($topicPath);
if (is_object($node)) {
$selected[] = $node->getTreeNodeID();
}
}
}
}
$topicsValue = new TopicsValue();
foreach ($selected as $treeNodeID) {
$topicsValueNode = new SelectedTopic();
$topicsValueNode->setAttributeValue($topicsValue);
$topicsValueNode->setTreeNodeID($treeNodeID);
$topicsValue->getSelectedTopics()->add($topicsValueNode);
}
return $topicsValue;
}
public function exportKey($key)
{
$this->load();
$tree = Tree::getByID($this->akTopicTreeID);
$node = Node::getByID($this->akTopicParentNodeID);
$path = $node->getTreeNodeDisplayPath();
$treeNode = $key->addChild('tree');
$treeNode->addAttribute('name', $tree->getTreeName());
$treeNode->addAttribute('path', $path);
$treeNode->addAttribute('allow-multiple-values', $this->akTopicAllowMultipleValues);
return $key;
}
public function importKey(\SimpleXMLElement $key)
{
$type = $this->getAttributeKeySettings();
$name = (string) $key->tree['name'];
$tree = \Concrete\Core\Tree\Type\Topic::getByName($name);
$node = $tree->getNodeByDisplayPath((string) $key->tree['path']);
$allowMultipleValues = $key->tree['allow-multiple-values'];
$type->setTopicTreeID($tree->getTreeID());
$type->setParentNodeID($node->getTreeNodeID());
$type->setAllowMultipleValues(((string) $allowMultipleValues) == '1' ? true : false);
return $type;
}
public function form($additionalClass = false)
{
$this->load();
$valueIDs = [];
if ($this->request->isPost()) {
$valueIDs = (array) $this->request->request->get('topics_' . $this->attributeKey->getAttributeKeyID());
} else {
if (is_object($this->attributeValue)) {
$valueIDs = [];
$valueObject = $this->attributeValue->getValueObject();
if ($valueObject) {
foreach ($valueObject->getSelectedTopics() as $value) {
$valueID = $value->getTreeNodeID();
$withinParentScope = false;
$nodeObj = TreeNode::getByID($value->getTreeNodeID());
if (is_object($nodeObj)) {
$parentNodeArray = $nodeObj->getTreeNodeParentArray();
// check to see if selected node is still within parent scope, in case it has been changed.
foreach ($parentNodeArray as $parent) {
if ($parent->treeNodeID == $this->akTopicParentNodeID) {
$withinParentScope = true;
break;
}
}
if ($withinParentScope) {
$valueIDs[] = $valueID;
}
}
}
}
}
}
$this->set('valueIDs', implode(',', $valueIDs));
$this->set('valueIDArray', $valueIDs);
$ak = $this->getAttributeKey();
$this->set('akID', $ak->getAttributeKeyID());
$this->set('parentNode', $this->akTopicParentNodeID);
$this->set('treeID', $this->akTopicTreeID);
$this->set('allowMultipleValues', $this->akTopicAllowMultipleValues);
}
public function searchForm($list)
{
$list->filterByAttribute($this->attributeKey->getAttributeKeyHandle(), $this->request('treeNodeID'));
return $list;
}
public function getSearchIndexValue()
{
$str = '||';
$nodeKeys = $this->attributeValue->getValue();
if ($nodeKeys) {
foreach ($nodeKeys as $nodeObj) {
$str .= $nodeObj->getTreeNodeDisplayPath() . '||';
}
}
if ($str == "||") {
return '';
}
return $str;
}
public function search()
{
$this->load();
$tree = TopicTree::getByID(Core::make('helper/security')->sanitizeInt($this->akTopicTreeID));
$this->set('tree', $tree);
$treeNodeID = $this->request('treeNodeID');
if (!$treeNodeID) {
$treeNodeID = $this->akTopicParentNodeID;
}
$this->set('selectedNode', $treeNodeID);
$this->set('attributeKey', $this->attributeKey);
}
public function createAttributeValueFromRequest()
{
$sh = Core::make('helper/security');
$av = new TopicsValue();
$cleanIDs = [];
$topicsArray = $this->request->request->get('topics_' . $this->attributeKey->getAttributeKeyID());
if (is_array($topicsArray) && count($topicsArray) > 0) {
foreach ($topicsArray as $topicID) {
$cleanIDs[] = $sh->sanitizeInt($topicID);
}
foreach ($cleanIDs as $topID) {
$topic = new SelectedTopic();
$topic->setAttributeValue($av);
$topic->setTreeNodeID($topID);
$av->getSelectedTopics()->add($topic);
}
}
return $av;
}
public function type_form()
{
$this->load();
$tt = new TopicTree();
$defaultTree = $tt->getDefault();
$topicTreeList = $tt->getList();
$tree = $tt->getByID(Core::make('helper/security')->sanitizeInt($this->akTopicTreeID));
if (!$tree) {
$tree = $defaultTree;
}
$this->set('tree', $tree);
$trees = [];
if (is_object($defaultTree)) {
$trees[] = $defaultTree;
foreach ($topicTreeList as $ctree) {
if ($ctree->getTreeID() != $defaultTree->getTreeID()) {
$trees[] = $ctree;
}
}
}
$this->set('trees', $trees);
$this->set('parentNode', $this->akTopicParentNodeID);
$this->set('allowMultipleValues', $this->akTopicAllowMultipleValues);
}
public function validateKey($data = false)
{
if ($data == false) {
$data = $this->post();
}
$e = $this->app->make('error');
if (!$data['akTopicParentNodeID'] || !$data['akTopicTreeID']) {
$e->add(t('You must specify a valid topic tree parent node ID and topic tree ID.'));
}
return $e;
}
public function validateValue()
{
$val = $this->getAttributeValue()->getValue();
return is_array($val) && count($val) > 0;
}
public function validateForm($p)
{
$topicsArray = $_POST['topics_' . $this->attributeKey->getAttributeKeyID()] ?? null;
return is_array($topicsArray) && count($topicsArray) > 0;
}
public function getTopicParentNode()
{
$this->load();
return $this->akTopicParentNodeID;
}
public function getTopicTreeID()
{
$this->load();
return $this->akTopicTreeID;
}
public function allowMultipleValues()
{
$this->load();
return $this->akTopicAllowMultipleValues;
}
public function duplicateKey($newAK)
{ // TODO this is going to need some work to function with the child options table...
$this->load();
$db = Database::get();
$db->Replace(
'atTopicSettings',
[
'akID' => $newAK->getAttributeKeyID(),
'akTopicParentNodeID' => $this->akTopicParentNodeID,
'akTopicTreeID' => $this->akTopicTreeID,
'akTopicAllowMultipleValues' => $this->akTopicAllowMultipleValues,
],
['akID'],
true
);
}
public function getAttributeKeySettingsClass()
{
return TopicsSettings::class;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Attribute\SimpleTextExportableAttributeInterface::getAttributeValueTextRepresentation()
*/
public function getAttributeValueTextRepresentation()
{
$result = '';
$value = $this->getAttributeValueObject();
if ($value !== null) {
$topics = $value->getSelectedTopics();
if (!empty($topics)) {
$ids = [];
foreach ($topics as $topic) {
/* @var \\Concrete\Core\Entity\Attribute\Value\Value\SelectedTopic $topic */
$ids[] = 'tid:' . $topic->getTreeNodeID();
}
$result = implode('|', $ids);
}
}
return $result;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Attribute\SimpleTextExportableAttributeInterface::updateAttributeValueFromTextRepresentation()
*/
public function updateAttributeValueFromTextRepresentation($textRepresentation, ErrorList $warnings)
{
$value = $this->getAttributeValueObject();
$textRepresentation = trim($textRepresentation);
if ($textRepresentation === '') {
if ($value !== null) {
$value->getSelectedTopics()->clear();
}
} elseif (!preg_match('/^tid:\d+(,tid:\d+)*$/', $textRepresentation)) {
$warnings->add(t('"%1$s" does not represent a valid value for the Topics attribute with handle %2$s', $textRepresentation, $this->attributeKey->getAttributeKeyHandle()));
} else {
if (!isset($this->akTopicParentNodeID)) {
$this->load();
}
$initialized = false;
preg_match_all('/tid:(\d+)$/', $textRepresentation, $matches);
$nodeIDs = array_unique(array_map('intval', $matches[1]));
foreach ($nodeIDs as $nodeID) {
$node = TreeNode::getByID($nodeID);
if ($node === null) {
$warnings->add(t('"%1$s" is not a valid node identifier for the Topics attribute with handle %2$s', $nodeID, $this->attributeKey->getAttributeKeyHandle()));
continue;
}
$withinParentScope = false;
$parentNodes = $node->getTreeNodeParentArray();
foreach ($parentNodes as $parentNode) {
if ($parentNode->getTreeNodeID() == $this->akTopicParentNodeID) {
$withinParentScope = true;
break;
}
}
if ($withinParentScope === false) {
$warnings->add(t('The Topic node with ID "%1$s" is not a child of the root node of the Topics attribute with handle %2$s', $nodeID, $this->attributeKey->getAttributeKeyHandle()));
continue;
}
if ($initialized === false) {
$initialized = true;
if ($value === null) {
$value = new TopicsValue();
} else {
$value->getSelectedTopics()->clear();
}
}
$topic = new SelectedTopic();
$topic->setAttributeValue($value);
$topic->setTreeNodeID($nodeID);
$value->getSelectedTopics()->add($topic);
}
}
return $value;
}
protected function load()
{
$ak = $this->getAttributeKey();
if (!is_object($ak)) {
return false;
}
/** @var TopicsSettings $type */
$type = $ak->getAttributeKeySettings();
if (is_object($type)) {
$this->akTopicParentNodeID = $type->getParentNodeID();
$this->akTopicTreeID = $type->getTopicTreeID();
$this->akTopicAllowMultipleValues = $type->allowMultipleValues();
}
}
}