/
controller.php
179 lines (161 loc) · 5.48 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
<?php
namespace Concrete\Block\TopicList;
use Concrete\Core\Attribute\Key\CollectionKey;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Feature\Features;
use Concrete\Core\Feature\UsesFeatureInterface;
use Concrete\Core\Tree\Tree;
use Concrete\Core\Tree\Type\Topic as TopicTree;
use Concrete\Core\Tree\Type\Topic;
use Core;
defined('C5_EXECUTE') or die("Access Denied.");
class Controller extends BlockController implements UsesFeatureInterface
{
public $helpers = array('form');
protected $btInterfaceWidth = 400;
protected $btInterfaceHeight = 400;
protected $btTable = 'btTopicList';
protected $btExportPageColumns = ['cParentID'];
public function getBlockTypeDescription()
{
return t("Displays a list of your site's topics, allowing you to click on them to filter a page list.");
}
public function getBlockTypeName()
{
return t("Topic List");
}
public function add()
{
$this->edit();
$this->set('title', t('Topics'));
$this->set('titleFormat', 'h5');
}
public function getRequiredFeatures(): array
{
return [
Features::TAXONOMY
];
}
public function edit()
{
$tt = new TopicTree();
$defaultTree = $tt->getDefault();
$tree = $tt->getByID(Core::make('helper/security')->sanitizeInt($this->topicTreeID));
if (!$tree) {
$tree = $defaultTree;
}
$trees = $tt->getList();
$keys = CollectionKey::getList();
$attributeKeys = array();
foreach ($keys as $ak) {
if ($ak->getAttributeTypeHandle() == 'topics') {
$attributeKeys[] = $ak;
}
}
$this->set('attributeKeys', $attributeKeys);
$this->set('tree', $tree);
$this->set('trees', $trees);
}
public function view()
{
if ($this->mode == 'P') {
$page = \Page::getCurrentPage();
$topics = $page->getAttribute($this->topicAttributeKeyHandle);
if (is_array($topics)) {
$this->set('topics', $topics);
}
} else {
$tt = new TopicTree();
$tree = $tt->getByID(Core::make('helper/security')->sanitizeInt($this->topicTreeID));
$this->set('tree', $tree);
}
}
public function action_topic($treeNodeID = false, $topic = false)
{
$this->set('selectedTopicID', intval($treeNodeID));
$this->view();
}
public function getTopicLink(\Concrete\Core\Tree\Node\Node $topic = null)
{
if ($this->cParentID) {
$c = \Page::getByID($this->cParentID);
} else {
$c = \Page::getCurrentPage();
}
if ($topic) {
$nodeName = $topic->getTreeNodeName();
$nodeName = strtolower($nodeName); // convert to lowercase
$nodeName = preg_replace('/[[:space:]]+/', '-', $nodeName);
$nodeName = Core::make('helper/text')->encodePath($nodeName); // urlencode
return \URL::page($c, 'topic', $topic->getTreeNodeID(), $nodeName);
} else {
return \URL::page($c);
}
}
public static function replaceTreeWithPlaceHolder($treeID)
{
if ($treeID > 0) {
$tree = Tree::getByID($treeID);
if (is_object($tree)) {
return '{ccm:export:tree:' . $tree->getTreeName() . '}';
}
}
}
public function export(\SimpleXMLElement $blockNode)
{
$tree = Tree::getByID($this->topicTreeID);
$data = $blockNode->addChild('data');
$data->addChild('mode', $this->mode);
$data->addChild("title", $this->title);
$data->addChild('topicAttributeKeyHandle', $this->topicAttributeKeyHandle);
if (is_object($tree)) {
$data->addChild('tree', $tree->getTreeName());
}
$path = null;
if ($this->cParentID) {
$parent = \Page::getByID($this->cParentID);
$path = '{ccm:export:page:' . $parent->getCollectionPath() . '}';
}
$data->addChild('cParentID', $path);
}
public function getImportData($blockNode, $page)
{
$args = array();
$treeName = (string) $blockNode->data->tree;
$page = (string) $blockNode->data->cParentID;
$tree = Topic::getByName($treeName);
$args['topicTreeID'] = $tree->getTreeID();
$args['cParentID'] = 0;
$args['title'] = (string) $blockNode->data->title;
$args['mode'] = (string) $blockNode->data->mode;
$args['titleFormat'] = (string) $blockNode->data->titleFormat;
if (!$args['mode']) {
$args['mode'] = 'S';
}
if (!$args['titleFormat']) {
$args['titleFormat'] = 'h5';
}
$args['topicAttributeKeyHandle'] = (string) $blockNode->data->topicAttributeKeyHandle;
if ($page) {
if (preg_match('/\{ccm:export:page:(.*?)\}/i', $page, $matches)) {
$c = \Page::getByPath($matches[1]);
$args['externalTarget'] = 1;
$args['cParentID'] = $c->getCollectionID();
}
}
return $args;
}
public function save($data)
{
$data += array(
'externalTarget' => 0,
);
$externalTarget = intval($data['externalTarget']);
if ($externalTarget === 0) {
$data['cParentID'] = 0;
} else {
$data['cParentID'] = intval($data['cParentID']);
}
parent::save($data);
}
}