/
controller.php
131 lines (112 loc) · 3.8 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
<?php
namespace Concrete\Block\Faq;
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;
class Controller extends BlockController implements UsesFeatureInterface
{
protected $btInterfaceWidth = 600;
protected $btInterfaceHeight = 465;
protected $btTable = 'btFaq';
protected $btExportTables = ['btFaq', 'btFaqEntries'];
protected $btWrapperClass = 'ccm-ui';
protected $btCacheBlockOutput = true;
protected $btCacheBlockOutputOnPost = true;
protected $btCacheBlockOutputForRegisteredUsers = true;
public function getBlockTypeName()
{
return t('FAQ');
}
public function getBlockTypeDescription()
{
return t('Frequently Asked Questions Block');
}
public function getRequiredFeatures(): array
{
return [
Features::FAQ,
];
}
public function getSearchableContent()
{
$content = '';
$db = $this->app->make('database')->connection();
$v = [$this->bID];
$q = 'SELECT * FROM btFaqEntries WHERE bID = ?';
$r = $db->executeQuery($q, $v);
foreach ($r as $row) {
$content .= $row['title'] . ' ' . $row['linkTitle'] . ' ' . $row['description'];
}
return $content;
}
public function add()
{
$this->set('rows', []);
}
public function edit()
{
$db = $this->app->make('database')->connection();
$rows = $db->fetchAll('SELECT * FROM btFaqEntries WHERE bID = ? ORDER BY sortOrder', [$this->bID]);
$query = [];
foreach ($rows as $q) {
$q['description'] = LinkAbstractor::translateFromEditMode($q['description']);
$query[] = $q;
}
$this->set('rows', $query);
}
public function view()
{
$db = $this->app->make('database')->connection();
$query = $db->fetchAll('SELECT * FROM btFaqEntries WHERE bID = ? ORDER BY sortOrder', [$this->bID]);
$rows = [];
foreach ($query as $row) {
$row['description'] = LinkAbstractor::translateFrom($row['description']);
$rows[] = $row;
}
$this->set('rows', $rows);
}
public function duplicate($newBID)
{
$db = $this->app->make(Connection::class);
$copyFields = 'title, linkTitle, description, sortOrder';
$db->executeUpdate(
"INSERT INTO btFaqEntries (bID, {$copyFields}) SELECT ?, {$copyFields} FROM btFaqEntries WHERE bID = ?",
[
$newBID,
$this->bID,
]
);
}
public function delete()
{
$db = $this->app->make('database')->connection();
$db->executeQuery('DELETE FROM btFaqEntries WHERE bID = ?', [$this->bID]);
parent::delete();
}
public function save($args)
{
$db = $this->app->make('database')->connection();
$db->executeQuery('DELETE FROM btFaqEntries WHERE bID = ?', [$this->bID]);
parent::save($args);
$count = isset($args['sortOrder']) ? count($args['sortOrder']) : 0;
$i = 0;
while ($i < $count) {
if (isset($args['description'][$i])) {
$args['description'][$i] = LinkAbstractor::translateTo($args['description'][$i]);
}
$db->executeQuery(
'INSERT INTO btFaqEntries (bID, title, linkTitle, description, sortOrder) VALUES(?,?,?,?,?)',
[
$this->bID,
$args['title'][$i],
$args['linkTitle'][$i],
$args['description'][$i],
$args['sortOrder'][$i],
]
);
++$i;
}
}
}