/
controller.php
299 lines (249 loc) · 9.47 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
<?php
namespace Concrete\Block\Survey;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Error\ErrorList\ErrorList;
use Concrete\Core\User\User;
use Core;
use Database;
use Doctrine\DBAL\Types\Type;
use Page;
class Controller extends BlockController
{
public $options = [];
protected $btTable = 'btSurvey';
protected $btInterfaceWidth = 500;
protected $btInterfaceHeight = 500;
protected $btExportTables = ['btSurvey', 'btSurveyOptions', 'btSurveyResults'];
/**
* Used for localization. If we want to localize the name/description we have to include this.
*/
public function getBlockTypeDescription()
{
return t('Provide a simple survey, along with results in a pie chart format.');
}
public function getBlockTypeName()
{
return t('Survey');
}
public function getQuestion()
{
return $this->question;
}
public function getShowResults()
{
return $this->showResults;
}
public function getCustomMessage()
{
return $this->customMessage;
}
public function getPollOptions()
{
return $this->options;
}
public function setPollOptions()
{
$this->cID = null;
$c = Page::getCurrentPage();
if (is_object($c)) {
$this->cID = $c->getCollectionID();
}
if ($this->bID) {
$db = Database::connection();
$v = [$this->bID];
$q = 'SELECT optionID, optionName, displayOrder FROM btSurveyOptions WHERE bID = ? ORDER BY displayOrder ASC';
$r = $db->query($q, $v);
$this->options = [];
if ($r) {
while ($row = $r->fetchRow()) {
$opt = new Option();
$opt->optionID = $row['optionID'];
$opt->cID = $this->cID;
$opt->optionName = $row['optionName'];
$opt->displayOrder = $row['displayOrder'];
$this->options[] = $opt;
}
}
}
}
public function edit()
{
$this->setPollOptions();
}
public function view()
{
$this->setPollOptions();
}
public function delete()
{
$db = Database::connection();
$v = [$this->bID];
$q = 'DELETE FROM btSurveyOptions WHERE bID = ?';
$db->query($q, $v);
$q = 'DELETE FROM btSurveyResults WHERE bID = ?';
$db->query($q, $v);
parent::delete();
}
public function action_form_save_vote($bID = false)
{
if ($this->bID != $bID) {
return false;
}
$u = $this->app->make(User::class);
$db = Database::connection();
$bo = $this->getBlockObject();
if ($this->post('rcID')) {
// we pass the rcID through the form so we can deal with stacks
$c = Page::getByID($this->post('rcID'));
} else {
$c = $this->getCollectionObject();
}
if (is_object($c)) {
$this->cID = $c->getCollectionID();
}
if ($this->requiresRegistration()) {
if (!$u->isRegistered()) {
$this->redirect('/login');
}
}
if (!$this->hasVoted()) {
$antispam = Core::make('helper/validation/antispam');
if ($antispam->check('', 'survey_block')) { // we do a blank check which will still check IP and UserAgent's
$duID = 0;
if ($u->getUserID() > 0) {
$duID = $u->getUserID();
}
/** @var \Concrete\Core\Permission\IPService $iph */
$iph = Core::make('helper/validation/ip');
$ip = $iph->getRequestIP();
$ip = ($ip === false) ? ('') : ($ip->getIp($ip::FORMAT_IP_STRING));
$v = [
$_REQUEST['optionID'],
$this->bID,
$duID,
$ip,
$this->cID,
];
$q = 'INSERT INTO btSurveyResults (optionID, bID, uID, ipAddress, cID) VALUES (?, ?, ?, ?, ?)';
$db->query($q, $v);
setcookie('ccmPoll' . $this->bID . '-' . $this->cID, 'voted', time() + 1296000, DIR_REL . '/');
$this->redirect($c->getCollectionPath() . '?survey_voted=1');
}
}
}
public function requiresRegistration()
{
return $this->requiresRegistration;
}
public function hasVoted()
{
$u = $this->app->make(User::class);
if ($u->isRegistered()) {
$db = Database::connection();
$v = [$u->getUserID(), $this->bID, $this->cID];
$q = 'SELECT count(resultID) AS total FROM btSurveyResults WHERE uID = ? AND bID = ? AND cID = ?';
$result = $db->getOne($q, $v);
if ($result > 0) {
return true;
}
} elseif ($_COOKIE['ccmPoll' . $this->bID . '-' . $this->cID] == 'voted') {
return true;
}
return false;
}
public function duplicate($newBID)
{
$this->setPollOptions();
/** @var \Concrete\Core\Database\Connection\Connection $db */
$db = $this->app->make('database/connection');
/** @var Option[] $opt */
foreach ($this->options as $opt) {
$db->insert('btSurveyOptions', ['bID' => $newBID, 'optionName' => $opt->getOptionName(), 'displayOrder' => $opt->getOptionDisplayOrder()]);
$newOptionID = $db->lastInsertId();
$results = $db->executeQuery('SELECT uID, ipAddress, timestamp FROM btSurveyResults WHERE optionID = :optID', ['optID' => $newOptionID])->fetchAll();
if (!empty($results)) {
foreach ($results as $row) {
$db->insert('btSurveyResults', ['optionID' => $newOptionID, 'uID' => $row['uID'], 'ipAddress' => $row['ipAddress'], 'timestamp' => $row['timestamp']]);
}
}
}
return parent::duplicate($newBID);
}
/**
* Validates the survey block data, requiring at least survey options.
*
* @param array|string|null $args
*
* @version 8.5.5 Method added for survey block
*
* @return ErrorList
*/
public function validate($args)
{
/** @var ErrorList $e */
$e = $this->app->make(ErrorList::class);
$sanitizer = $this->app->make('helper/security');
if (!isset($args['question']) || empty($sanitizer->sanitizeString($args['question']))) {
$e->addError(t('Question must not be blank.'), false, 'question');
}
if ((!isset($args['survivingOptionNames']) || !is_array($args['survivingOptionNames'])) && (!isset($args['pollOption']) || !is_array($args['pollOption']))) {
$e->addError(t('Survey must have at least 1 answer.'), false, 'optionValue');
}
return $e;
}
public function save($args)
{
$sanitizer = $this->app->make('helper/security');
if (!$args['showResults']) {
$args['showResults'] = 0;
$args['customMessage'] = '';
}
$args['customMessage'] = $sanitizer->sanitizeString($args['customMessage']);
$args['question'] = $sanitizer->sanitizeString($args['question']);
parent::save($args);
/** @var \Concrete\Core\Database\Connection\Connection $db */
$db = $this->app->make('database/connection');
if (!isset($args['survivingOptionNames']) || !is_array($args['survivingOptionNames'])) {
$args['survivingOptionNames'] = [];
}
$sanitizedArgs = [];
foreach ($args['survivingOptionNames'] as $arg) {
$sanitizedArgs[] = $sanitizer->sanitizeString($arg);
}
$queryBuilder = $db->createQueryBuilder();
$queryBuilder->delete('btSurveyOptions')
->andWhere($queryBuilder->expr()->eq('bID', ':blockID'))
->setParameter('blockID', (int) $this->bID, Type::INTEGER);
if (!empty($sanitizedArgs)) {
$queryBuilder->andWhere($queryBuilder->expr()->notIn('optionName', ':names'))->setParameter('names', $sanitizedArgs, Connection::PARAM_STR_ARRAY);
}
$queryBuilder->execute();
$max = $db->fetchColumn(
'SELECT MAX(displayOrder) AS maxDisplayOrder FROM btSurveyOptions WHERE bID = :bID',
['bID' => (int) $this->bID]
);
$displayOrder = $max ? (int) $max + 1 : 0;
if (isset($args['pollOption']) && is_array($args['pollOption'])) {
$db->beginTransaction();
foreach ($args['pollOption'] as $optionName) {
$optionName = $sanitizer->sanitizeString($optionName);
// Dont add if the sanitized string is empty
if (!empty($optionName)) {
$db->insert(
'btSurveyOptions',
['bID' => (int) $this->bID, 'optionName' => $optionName, 'displayOrder' => $displayOrder]
);
++$displayOrder;
}
}
$db->commit();
}
$queryBuilder = $db->createQueryBuilder();
$queryBuilder->delete('btSurveyResults')->where($queryBuilder->expr()->notIn(
'optionID',
'SELECT optionID from btSurveyOptions WHERE bID = :bID'
))->andWhere($queryBuilder->expr()->eq('bID', ':bID'))
->setParameter('bID', (int) $this->bID, Type::INTEGER)->execute();
}
}