-
Notifications
You must be signed in to change notification settings - Fork 454
/
controller.php
245 lines (205 loc) · 5.97 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
<?php
namespace Concrete\Block\Accordion;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Editor\LinkAbstractor;
use Concrete\Core\Feature\Features;
use Concrete\Core\Feature\UsesFeatureInterface;
use InvalidArgumentException;
class AccordionEntry implements \JsonSerializable
{
/**
* @var int
*/
protected $id;
/**
* @var string
*/
protected $title;
/**
* @var string
*/
protected $description;
/**
* AccordionEntry constructor.
* @param int $id
* @param string $title
* @param string $description
*/
public function __construct($id, $title, $description)
{
$this->id = $id;
$this->title = $title;
$this->description = $description;
}
/**
* @return string
*/
public function getTitle(): string
{
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title): void
{
$this->title = $title;
}
/**
* @return string
*/
public function getDescription(): string
{
return $this->description;
}
/**
* @param string $description
*/
public function setDescription($description): void
{
$this->description = $description;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id): void
{
$this->id = $id;
}
public function jsonSerialize()
{
return [
'id' => $this->getId(),
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'expanded' => false,
];
}
}
class Controller extends BlockController implements UsesFeatureInterface
{
protected $btInterfaceWidth = 720;
protected $btInterfaceHeight = 580;
protected $btTable = 'btAccordion';
protected $btExportTables = ['btAccordion', 'btAccordionEntries'];
protected $btWrapperClass = 'ccm-ui';
protected $btDefaultSet = 'navigation';
protected $btCacheBlockOutput = true;
protected $btCacheBlockOutputOnPost = true;
protected $btCacheBlockOutputForRegisteredUsers = true;
public function getBlockTypeName()
{
return t('Accordion');
}
public function getBlockTypeDescription()
{
return t('Collapsible content block.');
}
public function getRequiredFeatures(): array
{
return [
Features::ACCORDIONS,
];
}
public function getSearchableContent()
{
$content = '';
$db = $this->app->make('database')->connection();
$v = [$this->bID];
$q = 'SELECT * FROM btAccordionEntries WHERE bID = ? order by sortOrde';
$r = $db->executeQuery($q, $v);
foreach ($r as $row) {
$content .= $row['title'] . ' ' . $row['description'];
}
return $content;
}
public function edit()
{
$db = $this->app->make('database')->connection();
$query = $db->fetchAll('SELECT * FROM btAccordionEntries WHERE bID = ? ORDER BY sortOrder', [$this->bID]);
$entries = [];
foreach ($query as $row) {
$entry = new AccordionEntry($row['id'], $row['title'], LinkAbstractor::translateFromEditMode($row['description']));
$entries[] = $entry;
}
$this->set('entries', $entries);
}
public function view()
{
$db = $this->app->make('database')->connection();
$query = $db->fetchAll('SELECT * FROM btAccordionEntries WHERE bID = ? ORDER BY sortOrder', [$this->bID]);
$entries = [];
foreach ($query as $row) {
$entry = new AccordionEntry($row['id'], $row['title'], LinkAbstractor::translateFrom($row['description']));
$entries[] = $entry;
}
$this->set('entries', $entries);
}
public function duplicate($newBID)
{
$db = $this->app->make(Connection::class);
$copyFields = 'title, description, sortOrder';
$db->executeUpdate(
"INSERT INTO btAccordionEntries (bID, {$copyFields}) SELECT ?, {$copyFields} FROM btAccordionEntries WHERE bID = ?",
[
$newBID,
$this->bID,
]
);
}
public function add()
{
$this->set('entries', []);
}
public function delete()
{
$db = $this->app->make('database')->connection();
$db->executeQuery('DELETE FROM btAccordionEntries WHERE bID = ?', [$this->bID]);
parent::delete();
}
/**
* Process an inputted json into a proper json object
*
* @param array $args The equivalent to the $_POST submitted
*
* @throws InvalidArgumentException If the $args or the json are invalid
*
* @return array
*/
protected function processJson(array $args): array
{
$json = trim($args['accordionBlockData'] ?? '[]');
if (!$json || $json[0] !== '[') {
throw new InvalidArgumentException('Invalid request.');
}
$data = json_decode($json, true);
if (!is_array($data)) {
throw new InvalidArgumentException('Invalid request.');
}
return $data;
}
public function save($args)
{
parent::save($args);
/** @var Connection $db */
$db = $this->app->make(Connection::class);
$db->executeStatement("DELETE FROM btAccordionEntries WHERE bID = ?", [$this->bID]);
$entries = $this->processJson($args);
if ($entries) {
$sortOrder = 0;
foreach ($entries as $entry) {
// Add the entry row
$db->executeStatement("INSERT INTO btAccordionEntries (bID, sortOrder, title, description) VALUES (?, ?, ?, ?)",
[(int)$this->bID, $sortOrder++, $entry['title'], $entry['description']]);
}
}
}
}