-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimplePoll.php
408 lines (339 loc) · 11.4 KB
/
SimplePoll.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
<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
/**
* Contao Open Source CMS
* Copyright (C) 2005-2011 Leo Feyer
*
* Formerly known as TYPOlight Open Source CMS.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, please visit the Free
* Software Foundation website at <http://www.gnu.org/licenses/>.
*
* PHP version 5
* @copyright Kamil Kuzminski 2011-2012
* @author Kamil Kuzminski <kamil.kuzminski@gmail.com>
* @author Andreas Schempp <andreas@schempp.ch>
* @package SimplePoll
* @license LGPL
* @filesource
*/
/**
* Class SimplePoll
*
* Provide methods to handle polls.
* @copyright Kamil Kuzminski 2011-2012
* @author Kamil Kuzminski <kamil.kuzminski@gmail.com>
* @package SimplePoll
*/
class SimplePoll extends Hybrid
{
/**
* Key
* @var string
*/
protected $strKey = 'simplepoll';
/**
* Table
* @var string
*/
protected $strTable = 'tl_simplepoll';
/**
* Template
* @var string
*/
protected $strTemplate = 'simplepoll';
/**
* Cookie name prefix
* @var string
*/
protected $strCookie = 'SIMPLEPOLL_';
/**
* Module configuration
* @var array
*/
protected $arrConfig = array();
/**
* Check if there is DC_Multilingual installed
* @return boolean
*/
public static function checkMultilingual()
{
return (file_exists(TL_ROOT . '/system/drivers/DC_Multilingual.php') && count(self::getAvailableLanguages()) > 1) ? true : false;
}
/**
* Return a list of available languages
* @return array
*/
public static function getAvailableLanguages()
{
$objDatabase = Database::getInstance();
return $objDatabase->execute("SELECT DISTINCT language FROM tl_page WHERE type='root'")->fetchEach('language');
}
/**
* Get a fallback language
* @return string
*/
public static function getFallbackLanguage()
{
$objDatabase = Database::getInstance();
return $objDatabase->execute("SELECT language FROM tl_page WHERE type='root' AND fallback=1")->language;
}
/**
* Store the module configuration
* @param Database_Result
* @return string
*/
public function __construct(Database_Result $objElement)
{
$this->arrConfig = $objElement->row();
parent::__construct($objElement);
}
/**
* Display a wildcard in the back end
* @return string
*/
public function generate()
{
if (TL_MODE == 'BE')
{
$this->Template = new BackendTemplate('be_wildcard');
$this->Template->wildcard = '### SIMPLE POLL ###';
$this->Template->title = $this->headline;
$this->Template->id = $this->id;
$this->Template->link = $this->title;
$this->Template->href = 'contao/main.php?do=simplepoll&table=tl_simplepoll_option&id=' . $this->id;
return $this->Template->parse();
}
// Find the newest poll
if ($this->arrConfig['simplepoll_current'])
{
$time = time();
$objPoll = $this->Database->prepare("SELECT id, start, stop FROM tl_simplepoll WHERE (start='' OR start<?) AND (stop='' OR stop>?)" . (!BE_USER_LOGGED_IN ? " AND published=1" : "") . " ORDER BY start DESC")
->limit(1)
->execute($time, $time);
if ($objPoll->numRows)
{
$this->id = $objPoll->id;
}
}
return parent::generate();
}
/**
* Generate module
*/
protected function compile()
{
$this->Template->message = '';
$this->Template->showResults = false;
$this->Template->showForm = false;
$objPoll = $this->Database->prepare($this->getPollQuery('tl_simplepoll'))
->limit(1)
->execute($this->id);
// Return if poll does not exist
if (!$objPoll->numRows || !$objPoll->options)
{
return;
}
// Display a "login to vote" message
if ($objPoll->protected && !FE_USER_LOGGED_IN)
{
$this->Template->mclass = 'info';
$this->Template->message = $GLOBALS['TL_LANG']['MSC']['loginToVote'];
}
$time = time();
$arrIps = deserialize($objPoll->ips, true);
$blnClosed = ($objPoll->closed || (($objPoll->start != '' && $objPoll->start > $time) || ($objPoll->stop != '' && $objPoll->stop < $time))) ? true : false;
$blnHasVoted = $this->hasVoted($arrIps, $objPoll->id);
$strFormId = 'poll_' . $this->id;
$this->Template->title = $objPoll->title;
$this->Template->featured = $objPoll->featured ? true : false;
// Display a message if the poll is closed
if ($blnClosed)
{
$this->Template->mclass = 'info';
$this->Template->message = $GLOBALS['TL_LANG']['MSC']['pollClosed'];
}
// Display a confirmation message
if ($_SESSION['SIMPLEPOLL'][$this->id] != '')
{
$this->Template->mclass = 'confirm';
$this->Template->message = $_SESSION['SIMPLEPOLL'][$this->id];
unset($_SESSION['SIMPLEPOLL'][$this->id]);
}
$objOptions = $this->Database->prepare($this->getPollQuery('tl_simplepoll_option'))
->execute($this->id);
// Display results under certain circumstances
if ((!$blnHasVoted && $this->behaviorNotVoted == 'opt1' && $this->Input->get('results') == $objPoll->id) || ($blnHasVoted && $this->behaviorVoted == 'opt1') || ($blnHasVoted && $this->behaviorVoted == 'opt2' && $this->Input->get('results') == $objPoll->id))
{
$arrResults = array();
$intVotes = array_sum($objOptions->fetchEach('votes'));
$objOptions->reset();
// Generate results
while ($objOptions->next())
{
$arrResults[] = array
(
'title' => $objOptions->title,
'votes' => $objOptions->votes,
'prcnt' => ($intVotes > 0 ) ? (round(($objOptions->votes / $intVotes), 2) * 100) : 0
);
}
$this->Template->showResults = true;
$this->Template->total = $intVotes;
$this->Template->results = $arrResults;
return;
}
// Generate options
while ($objOptions->next())
{
$arrOptions[$objOptions->id] = $objOptions->title;
}
// Options form field
$arrField = array
(
'name' => 'options',
'options' => $arrOptions,
'inputType' => 'radio',
'eval' => array('mandatory'=>true, 'disabled'=>$blnClosed)
);
$doNotSubmit = false;
$objWidget = new $GLOBALS['TL_FFL'][$arrField['inputType']]($this->prepareForWidget($arrField, $arrField['name']));
// Validate the widget
if ($this->Input->post('FORM_SUBMIT') == $strFormId && !$this->Input->post('results'))
{
$objWidget->validate();
if ($objWidget->hasErrors())
{
$doNotSubmit = true;
}
}
$this->Template->showForm = true;
$this->Template->options = $objWidget;
$this->Template->submit = ($blnClosed || $blnHasVoted || $objPoll->protected && !FE_USER_LOGGED_IN) ? '' : $GLOBALS['TL_LANG']['MSC']['vote'];
$this->Template->action = ampersand($this->Environment->request);
$this->Template->formId = $strFormId;
$this->Template->hasError = $doNotSubmit;
$this->Template->resultsLink = '';
// Display the results link
if ((!$blnHasVoted && $this->behaviorNotVoted == 'opt1') || ($blnHasVoted && $this->behaviorVoted == 'opt2'))
{
$this->Template->resultsLink = sprintf('<a href="%s" title="%s">%s</a>', $this->Environment->request . (($GLOBALS['TL_CONFIG']['disableAlias'] || strpos($this->Environment->request, '?') !== false) ? '&' : '?') . 'results=' . $objPoll->id, specialchars($GLOBALS['TL_LANG']['MSC']['showResults']), $GLOBALS['TL_LANG']['MSC']['showResults']);
}
// Add the vote
if ($this->Input->post('FORM_SUBMIT') == $strFormId && !$doNotSubmit)
{
if ($blnClosed || $blnHasVoted || $objPoll->protected && !FE_USER_LOGGED_IN)
{
$this->reload();
}
$this->import('FrontendUser', 'User');
$intExpires = ($objPoll->voteInterval > 0) ? ($time + $objPoll->voteInterval) : ($time + (30 * 86400));
// Set the cookie
$this->setCookie($this->strCookie . $objPoll->id, true, $intExpires);
// Save the IP address or user ID
$arrIps[] = array
(
'id' => FE_USER_LOGGED_IN ? $this->User->id : $this->Environment->remoteAddr,
'expires' => $intExpires,
'option' => $objWidget->value
);
// Update the database
$this->Database->prepare("UPDATE tl_simplepoll_option SET votes=votes + 1 WHERE id=?")->execute($objWidget->value);
$this->Database->prepare("UPDATE tl_simplepoll SET ips=? WHERE id=?")->execute(serialize($arrIps), $objPoll->id);
$this->log('Submitted vote ID ' . $objWidget->value . ' in poll ID ' . $objPoll->id, 'SimplePoll addPollToTemplate()', TL_GENERAL);
// Redirect or reload the page
$_SESSION['SIMPLEPOLL'][$this->id] = $GLOBALS['TL_LANG']['MSC']['voteSubmitted'];
$this->jumpToOrReload($this->jumpTo);
}
}
/**
* Determine whether the current user has already voted
* @param mixed
* @param integer
* @return boolean
*/
protected function hasVoted($arrIps, $intId)
{
if ($this->Input->cookie($this->strCookie . $intId))
{
return true;
}
$arrIps = deserialize($arrIps);
// No votes have been made yet
if (!is_array($arrIps) || empty($arrIps))
{
return false;
}
$varNeedle = $this->Environment->remoteAddr;
// Search for the current user
if (FE_USER_LOGGED_IN)
{
$this->import('FrontendUser', 'User');
$varNeedle = $this->User->id;
}
$time = time();
// Find the needle
foreach ($arrIps as $arrEntry)
{
if ($arrEntry['id'] == $varNeedle && $arrEntry['expires'] > $time)
{
return true;
}
}
return false;
}
/**
* Generate a select statement that includes translated fields (thanks to Andreas Schempp)
* @param string
* @param string
* @return string
*/
protected function getPollQuery($strTable)
{
$blnMultilingual = self::checkMultilingual();
// Multilingual settings
if ($blnMultilingual)
{
$arrFields = array();
$this->loadDataContainer($strTable);
// Get translatable fields
foreach ($GLOBALS['TL_DCA'][$strTable]['fields'] as $field => $arrData)
{
if ($arrData['eval']['translatableFor'] != '')
{
$arrFields[] = "IFNULL(t2." . $field . ", t1." . $field . ") AS " . $field;
}
}
}
// Build the query
switch ($strTable)
{
case 'tl_simplepoll':
$strQuery = "SELECT *, (SELECT COUNT(*) FROM tl_simplepoll_option WHERE pid=tl_simplepoll.id) AS options FROM tl_simplepoll WHERE id=?" . (!BE_USER_LOGGED_IN ? " AND published=1" : "");
if ($blnMultilingual)
{
$strQuery = "SELECT t1.*, " . implode(', ', $arrFields) . ", (SELECT COUNT(*) FROM tl_simplepoll_option WHERE pid=t1.id) AS options FROM tl_simplepoll t1 LEFT OUTER JOIN tl_simplepoll t2 ON t1.id=t2.lid AND t2.language='" . $GLOBALS['TL_LANGUAGE'] . "' WHERE t1.lid=0 AND t1.id=?" . (!BE_USER_LOGGED_IN ? " AND t1.published=1" : "");
}
break;
case 'tl_simplepoll_option':
$strQuery = "SELECT * FROM tl_simplepoll_option WHERE pid=?" . (!BE_USER_LOGGED_IN ? " AND published=1" : "") . " ORDER BY sorting";
if ($blnMultilingual)
{
$strQuery = "SELECT t1.*, " . implode(', ', $arrFields) . " FROM tl_simplepoll_option t1 LEFT OUTER JOIN tl_simplepoll_option t2 ON t1.id=t2.lid AND t2.language='" . $GLOBALS['TL_LANGUAGE'] . "' WHERE t1.lid=0 AND t1.pid=?" . (!BE_USER_LOGGED_IN ? " AND t1.published=1" : "") . " ORDER BY t1.sorting";
}
break;
}
return $strQuery;
}
}
?>