Chris@76
|
1 <?php
|
Chris@76
|
2
|
Chris@76
|
3 /**
|
Chris@76
|
4 * Simple Machines Forum (SMF)
|
Chris@76
|
5 *
|
Chris@76
|
6 * @package SMF
|
Chris@76
|
7 * @author Simple Machines http://www.simplemachines.org
|
Chris@76
|
8 * @copyright 2011 Simple Machines
|
Chris@76
|
9 * @license http://www.simplemachines.org/about/smf/license.php BSD
|
Chris@76
|
10 *
|
Chris@76
|
11 * @version 2.0
|
Chris@76
|
12 */
|
Chris@76
|
13
|
Chris@76
|
14 if (!defined('SMF'))
|
Chris@76
|
15 die('Hacking attempt...');
|
Chris@76
|
16
|
Chris@76
|
17 /* This file contains the functions for voting, locking, removing and editing
|
Chris@76
|
18 polls. Note that that posting polls is done in Post.php.
|
Chris@76
|
19
|
Chris@76
|
20 void Vote()
|
Chris@76
|
21 - is called to register a vote in a poll.
|
Chris@76
|
22 - must be called with a topic and option specified.
|
Chris@76
|
23 - uses the Post language file.
|
Chris@76
|
24 - requires the poll_vote permission.
|
Chris@76
|
25 - upon successful completion of action will direct user back to topic.
|
Chris@76
|
26 - is accessed via ?action=vote.
|
Chris@76
|
27
|
Chris@76
|
28 void LockVoting()
|
Chris@76
|
29 - is called to lock or unlock voting on a poll.
|
Chris@76
|
30 - must be called with a topic specified in the URL.
|
Chris@76
|
31 - an admin always has over riding permission to lock a poll.
|
Chris@76
|
32 - if not an admin must have poll_lock_any permission.
|
Chris@76
|
33 - otherwise must be poll starter with poll_lock_own permission.
|
Chris@76
|
34 - upon successful completion of action will direct user back to topic.
|
Chris@76
|
35 - is accessed via ?action=lockvoting.
|
Chris@76
|
36
|
Chris@76
|
37 void EditPoll()
|
Chris@76
|
38 - is called to display screen for editing or adding a poll.
|
Chris@76
|
39 - must be called with a topic specified in the URL.
|
Chris@76
|
40 - if the user is adding a poll to a topic, must contain the variable
|
Chris@76
|
41 'add' in the url.
|
Chris@76
|
42 - uses the Post language file.
|
Chris@76
|
43 - uses the Poll template (main sub template.).
|
Chris@76
|
44 - user must have poll_edit_any/poll_add_any permission for the relevant
|
Chris@76
|
45 action.
|
Chris@76
|
46 - otherwise must be poll starter with poll_edit_own permission for
|
Chris@76
|
47 editing, or be topic starter with poll_add_any permission for adding.
|
Chris@76
|
48 - is accessed via ?action=editpoll.
|
Chris@76
|
49
|
Chris@76
|
50 void EditPoll2()
|
Chris@76
|
51 - is called to update the settings for a poll, or add a new one.
|
Chris@76
|
52 - must be called with a topic specified in the URL.
|
Chris@76
|
53 - user must have poll_edit_any/poll_add_any permission for the relevant
|
Chris@76
|
54 action.
|
Chris@76
|
55 - otherwise must be poll starter with poll_edit_own permission for
|
Chris@76
|
56 editing, or be topic starter with poll_add_any permission for adding.
|
Chris@76
|
57 - in the case of an error will redirect back to EditPoll and display
|
Chris@76
|
58 the relevant error message.
|
Chris@76
|
59 - upon successful completion of action will direct user back to topic.
|
Chris@76
|
60 - is accessed via ?action=editpoll2.
|
Chris@76
|
61
|
Chris@76
|
62 void RemovePoll()
|
Chris@76
|
63 - is called to remove a poll from a topic.
|
Chris@76
|
64 - must be called with a topic specified in the URL.
|
Chris@76
|
65 - user must have poll_remove_any permission.
|
Chris@76
|
66 - otherwise must be poll starter with poll_remove_own permission.
|
Chris@76
|
67 - upon successful completion of action will direct user back to topic.
|
Chris@76
|
68 - is accessed via ?action=removepoll.
|
Chris@76
|
69 */
|
Chris@76
|
70
|
Chris@76
|
71 // Allow the user to vote.
|
Chris@76
|
72 function Vote()
|
Chris@76
|
73 {
|
Chris@76
|
74 global $topic, $txt, $user_info, $smcFunc, $sourcedir, $modSettings;
|
Chris@76
|
75
|
Chris@76
|
76 // Make sure you can vote.
|
Chris@76
|
77 isAllowedTo('poll_vote');
|
Chris@76
|
78
|
Chris@76
|
79 loadLanguage('Post');
|
Chris@76
|
80
|
Chris@76
|
81 // Check if they have already voted, or voting is locked.
|
Chris@76
|
82 $request = $smcFunc['db_query']('', '
|
Chris@76
|
83 SELECT IFNULL(lp.id_choice, -1) AS selected, p.voting_locked, p.id_poll, p.expire_time, p.max_votes, p.change_vote,
|
Chris@76
|
84 p.guest_vote, p.reset_poll, p.num_guest_voters
|
Chris@76
|
85 FROM {db_prefix}topics AS t
|
Chris@76
|
86 INNER JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
|
Chris@76
|
87 LEFT JOIN {db_prefix}log_polls AS lp ON (p.id_poll = lp.id_poll AND lp.id_member = {int:current_member} AND lp.id_member != {int:not_guest})
|
Chris@76
|
88 WHERE t.id_topic = {int:current_topic}
|
Chris@76
|
89 LIMIT 1',
|
Chris@76
|
90 array(
|
Chris@76
|
91 'current_member' => $user_info['id'],
|
Chris@76
|
92 'current_topic' => $topic,
|
Chris@76
|
93 'not_guest' => 0,
|
Chris@76
|
94 )
|
Chris@76
|
95 );
|
Chris@76
|
96 if ($smcFunc['db_num_rows']($request) == 0)
|
Chris@76
|
97 fatal_lang_error('poll_error', false);
|
Chris@76
|
98 $row = $smcFunc['db_fetch_assoc']($request);
|
Chris@76
|
99 $smcFunc['db_free_result']($request);
|
Chris@76
|
100
|
Chris@76
|
101 // If this is a guest can they vote?
|
Chris@76
|
102 if ($user_info['is_guest'])
|
Chris@76
|
103 {
|
Chris@76
|
104 // Guest voting disabled?
|
Chris@76
|
105 if (!$row['guest_vote'])
|
Chris@76
|
106 fatal_lang_error('guest_vote_disabled');
|
Chris@76
|
107 // Guest already voted?
|
Chris@76
|
108 elseif (!empty($_COOKIE['guest_poll_vote']) && preg_match('~^[0-9,;]+$~', $_COOKIE['guest_poll_vote']) && strpos($_COOKIE['guest_poll_vote'], ';' . $row['id_poll'] . ',') !== false)
|
Chris@76
|
109 {
|
Chris@76
|
110 // ;id,timestamp,[vote,vote...]; etc
|
Chris@76
|
111 $guestinfo = explode(';', $_COOKIE['guest_poll_vote']);
|
Chris@76
|
112 // Find the poll we're after.
|
Chris@76
|
113 foreach ($guestinfo as $i => $guestvoted)
|
Chris@76
|
114 {
|
Chris@76
|
115 $guestvoted = explode(',', $guestvoted);
|
Chris@76
|
116 if ($guestvoted[0] == $row['id_poll'])
|
Chris@76
|
117 break;
|
Chris@76
|
118 }
|
Chris@76
|
119 // Has the poll been reset since guest voted?
|
Chris@76
|
120 if ($row['reset_poll'] > $guestvoted[1])
|
Chris@76
|
121 {
|
Chris@76
|
122 // Remove the poll info from the cookie to allow guest to vote again
|
Chris@76
|
123 unset($guestinfo[$i]);
|
Chris@76
|
124 if (!empty($guestinfo))
|
Chris@76
|
125 $_COOKIE['guest_poll_vote'] = ';' . implode(';', $guestinfo);
|
Chris@76
|
126 else
|
Chris@76
|
127 unset($_COOKIE['guest_poll_vote']);
|
Chris@76
|
128 }
|
Chris@76
|
129 else
|
Chris@76
|
130 fatal_lang_error('poll_error', false);
|
Chris@76
|
131 unset($guestinfo, $guestvoted, $i);
|
Chris@76
|
132 }
|
Chris@76
|
133 }
|
Chris@76
|
134
|
Chris@76
|
135 // Is voting locked or has it expired?
|
Chris@76
|
136 if (!empty($row['voting_locked']) || (!empty($row['expire_time']) && time() > $row['expire_time']))
|
Chris@76
|
137 fatal_lang_error('poll_error', false);
|
Chris@76
|
138
|
Chris@76
|
139 // If they have already voted and aren't allowed to change their vote - hence they are outta here!
|
Chris@76
|
140 if (!$user_info['is_guest'] && $row['selected'] != -1 && empty($row['change_vote']))
|
Chris@76
|
141 fatal_lang_error('poll_error', false);
|
Chris@76
|
142 // Otherwise if they can change their vote yet they haven't sent any options... remove their vote and redirect.
|
Chris@76
|
143 elseif (!empty($row['change_vote']) && !$user_info['is_guest'])
|
Chris@76
|
144 {
|
Chris@76
|
145 checkSession('request');
|
Chris@76
|
146 $pollOptions = array();
|
Chris@76
|
147
|
Chris@76
|
148 // Find out what they voted for before.
|
Chris@76
|
149 $request = $smcFunc['db_query']('', '
|
Chris@76
|
150 SELECT id_choice
|
Chris@76
|
151 FROM {db_prefix}log_polls
|
Chris@76
|
152 WHERE id_member = {int:current_member}
|
Chris@76
|
153 AND id_poll = {int:id_poll}',
|
Chris@76
|
154 array(
|
Chris@76
|
155 'current_member' => $user_info['id'],
|
Chris@76
|
156 'id_poll' => $row['id_poll'],
|
Chris@76
|
157 )
|
Chris@76
|
158 );
|
Chris@76
|
159 while ($choice = $smcFunc['db_fetch_row']($request))
|
Chris@76
|
160 $pollOptions[] = $choice[0];
|
Chris@76
|
161 $smcFunc['db_free_result']($request);
|
Chris@76
|
162
|
Chris@76
|
163 // Just skip it if they had voted for nothing before.
|
Chris@76
|
164 if (!empty($pollOptions))
|
Chris@76
|
165 {
|
Chris@76
|
166 // Update the poll totals.
|
Chris@76
|
167 $smcFunc['db_query']('', '
|
Chris@76
|
168 UPDATE {db_prefix}poll_choices
|
Chris@76
|
169 SET votes = votes - 1
|
Chris@76
|
170 WHERE id_poll = {int:id_poll}
|
Chris@76
|
171 AND id_choice IN ({array_int:poll_options})
|
Chris@76
|
172 AND votes > {int:votes}',
|
Chris@76
|
173 array(
|
Chris@76
|
174 'poll_options' => $pollOptions,
|
Chris@76
|
175 'id_poll' => $row['id_poll'],
|
Chris@76
|
176 'votes' => 0,
|
Chris@76
|
177 )
|
Chris@76
|
178 );
|
Chris@76
|
179
|
Chris@76
|
180 // Delete off the log.
|
Chris@76
|
181 $smcFunc['db_query']('', '
|
Chris@76
|
182 DELETE FROM {db_prefix}log_polls
|
Chris@76
|
183 WHERE id_member = {int:current_member}
|
Chris@76
|
184 AND id_poll = {int:id_poll}',
|
Chris@76
|
185 array(
|
Chris@76
|
186 'current_member' => $user_info['id'],
|
Chris@76
|
187 'id_poll' => $row['id_poll'],
|
Chris@76
|
188 )
|
Chris@76
|
189 );
|
Chris@76
|
190 }
|
Chris@76
|
191
|
Chris@76
|
192 // Redirect back to the topic so the user can vote again!
|
Chris@76
|
193 if (empty($_POST['options']))
|
Chris@76
|
194 redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
|
Chris@76
|
195 }
|
Chris@76
|
196
|
Chris@76
|
197 checkSession('request');
|
Chris@76
|
198
|
Chris@76
|
199 // Make sure the option(s) are valid.
|
Chris@76
|
200 if (empty($_POST['options']))
|
Chris@76
|
201 fatal_lang_error('didnt_select_vote', false);
|
Chris@76
|
202
|
Chris@76
|
203 // Too many options checked!
|
Chris@76
|
204 if (count($_REQUEST['options']) > $row['max_votes'])
|
Chris@76
|
205 fatal_lang_error('poll_too_many_votes', false, array($row['max_votes']));
|
Chris@76
|
206
|
Chris@76
|
207 $pollOptions = array();
|
Chris@76
|
208 $inserts = array();
|
Chris@76
|
209 foreach ($_REQUEST['options'] as $id)
|
Chris@76
|
210 {
|
Chris@76
|
211 $id = (int) $id;
|
Chris@76
|
212
|
Chris@76
|
213 $pollOptions[] = $id;
|
Chris@76
|
214 $inserts[] = array($row['id_poll'], $user_info['id'], $id);
|
Chris@76
|
215 }
|
Chris@76
|
216
|
Chris@76
|
217 // Add their vote to the tally.
|
Chris@76
|
218 $smcFunc['db_insert']('insert',
|
Chris@76
|
219 '{db_prefix}log_polls',
|
Chris@76
|
220 array('id_poll' => 'int', 'id_member' => 'int', 'id_choice' => 'int'),
|
Chris@76
|
221 $inserts,
|
Chris@76
|
222 array('id_poll', 'id_member', 'id_choice')
|
Chris@76
|
223 );
|
Chris@76
|
224
|
Chris@76
|
225 $smcFunc['db_query']('', '
|
Chris@76
|
226 UPDATE {db_prefix}poll_choices
|
Chris@76
|
227 SET votes = votes + 1
|
Chris@76
|
228 WHERE id_poll = {int:id_poll}
|
Chris@76
|
229 AND id_choice IN ({array_int:poll_options})',
|
Chris@76
|
230 array(
|
Chris@76
|
231 'poll_options' => $pollOptions,
|
Chris@76
|
232 'id_poll' => $row['id_poll'],
|
Chris@76
|
233 )
|
Chris@76
|
234 );
|
Chris@76
|
235
|
Chris@76
|
236 // If it's a guest don't let them vote again.
|
Chris@76
|
237 if ($user_info['is_guest'] && count($pollOptions) > 0)
|
Chris@76
|
238 {
|
Chris@76
|
239 // Time is stored in case the poll is reset later, plus what they voted for.
|
Chris@76
|
240 $_COOKIE['guest_poll_vote'] = empty($_COOKIE['guest_poll_vote']) ? '' : $_COOKIE['guest_poll_vote'];
|
Chris@76
|
241 // ;id,timestamp,[vote,vote...]; etc
|
Chris@76
|
242 $_COOKIE['guest_poll_vote'] .= ';' . $row['id_poll'] . ',' . time() . ',' . (count($pollOptions) > 1 ? explode(',' . $pollOptions) : $pollOptions[0]);
|
Chris@76
|
243
|
Chris@76
|
244 // Increase num guest voters count by 1
|
Chris@76
|
245 $smcFunc['db_query']('', '
|
Chris@76
|
246 UPDATE {db_prefix}polls
|
Chris@76
|
247 SET num_guest_voters = num_guest_voters + 1
|
Chris@76
|
248 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
249 array(
|
Chris@76
|
250 'id_poll' => $row['id_poll'],
|
Chris@76
|
251 )
|
Chris@76
|
252 );
|
Chris@76
|
253
|
Chris@76
|
254 require_once($sourcedir . '/Subs-Auth.php');
|
Chris@76
|
255 $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']));
|
Chris@76
|
256 setcookie('guest_poll_vote', $_COOKIE['guest_poll_vote'], time() + 2500000, $cookie_url[1], $cookie_url[0], 0);
|
Chris@76
|
257 }
|
Chris@76
|
258
|
Chris@76
|
259 // Return to the post...
|
Chris@76
|
260 redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
|
Chris@76
|
261 }
|
Chris@76
|
262
|
Chris@76
|
263 // Lock the voting for a poll.
|
Chris@76
|
264 function LockVoting()
|
Chris@76
|
265 {
|
Chris@76
|
266 global $topic, $user_info, $smcFunc;
|
Chris@76
|
267
|
Chris@76
|
268 checkSession('get');
|
Chris@76
|
269
|
Chris@76
|
270 // Get the poll starter, ID, and whether or not it is locked.
|
Chris@76
|
271 $request = $smcFunc['db_query']('', '
|
Chris@76
|
272 SELECT t.id_member_started, t.id_poll, p.voting_locked
|
Chris@76
|
273 FROM {db_prefix}topics AS t
|
Chris@76
|
274 INNER JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
|
Chris@76
|
275 WHERE t.id_topic = {int:current_topic}
|
Chris@76
|
276 LIMIT 1',
|
Chris@76
|
277 array(
|
Chris@76
|
278 'current_topic' => $topic,
|
Chris@76
|
279 )
|
Chris@76
|
280 );
|
Chris@76
|
281 list ($memberID, $pollID, $voting_locked) = $smcFunc['db_fetch_row']($request);
|
Chris@76
|
282
|
Chris@76
|
283 // If the user _can_ modify the poll....
|
Chris@76
|
284 if (!allowedTo('poll_lock_any'))
|
Chris@76
|
285 isAllowedTo('poll_lock_' . ($user_info['id'] == $memberID ? 'own' : 'any'));
|
Chris@76
|
286
|
Chris@76
|
287 // It's been locked by a non-moderator.
|
Chris@76
|
288 if ($voting_locked == '1')
|
Chris@76
|
289 $voting_locked = '0';
|
Chris@76
|
290 // Locked by a moderator, and this is a moderator.
|
Chris@76
|
291 elseif ($voting_locked == '2' && allowedTo('moderate_board'))
|
Chris@76
|
292 $voting_locked = '0';
|
Chris@76
|
293 // Sorry, a moderator locked it.
|
Chris@76
|
294 elseif ($voting_locked == '2' && !allowedTo('moderate_board'))
|
Chris@76
|
295 fatal_lang_error('locked_by_admin', 'user');
|
Chris@76
|
296 // A moderator *is* locking it.
|
Chris@76
|
297 elseif ($voting_locked == '0' && allowedTo('moderate_board'))
|
Chris@76
|
298 $voting_locked = '2';
|
Chris@76
|
299 // Well, it's gonna be locked one way or another otherwise...
|
Chris@76
|
300 else
|
Chris@76
|
301 $voting_locked = '1';
|
Chris@76
|
302
|
Chris@76
|
303 // Lock! *Poof* - no one can vote.
|
Chris@76
|
304 $smcFunc['db_query']('', '
|
Chris@76
|
305 UPDATE {db_prefix}polls
|
Chris@76
|
306 SET voting_locked = {int:voting_locked}
|
Chris@76
|
307 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
308 array(
|
Chris@76
|
309 'voting_locked' => $voting_locked,
|
Chris@76
|
310 'id_poll' => $pollID,
|
Chris@76
|
311 )
|
Chris@76
|
312 );
|
Chris@76
|
313
|
Chris@76
|
314 redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
|
Chris@76
|
315 }
|
Chris@76
|
316
|
Chris@76
|
317 // Ask what to change in a poll.
|
Chris@76
|
318 function EditPoll()
|
Chris@76
|
319 {
|
Chris@76
|
320 global $txt, $user_info, $context, $topic, $board, $smcFunc, $sourcedir, $scripturl;
|
Chris@76
|
321
|
Chris@76
|
322 if (empty($topic))
|
Chris@76
|
323 fatal_lang_error('no_access', false);
|
Chris@76
|
324
|
Chris@76
|
325 loadLanguage('Post');
|
Chris@76
|
326 loadTemplate('Poll');
|
Chris@76
|
327
|
Chris@76
|
328 $context['can_moderate_poll'] = isset($_REQUEST['add']) ? 1 : allowedTo('moderate_board');
|
Chris@76
|
329 $context['start'] = (int) $_REQUEST['start'];
|
Chris@76
|
330 $context['is_edit'] = isset($_REQUEST['add']) ? 0 : 1;
|
Chris@76
|
331
|
Chris@76
|
332 // Check if a poll currently exists on this topic, and get the id, question and starter.
|
Chris@76
|
333 $request = $smcFunc['db_query']('', '
|
Chris@76
|
334 SELECT
|
Chris@76
|
335 t.id_member_started, p.id_poll, p.question, p.hide_results, p.expire_time, p.max_votes, p.change_vote,
|
Chris@76
|
336 m.subject, p.guest_vote, p.id_member AS poll_starter
|
Chris@76
|
337 FROM {db_prefix}topics AS t
|
Chris@76
|
338 INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
|
Chris@76
|
339 LEFT JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
|
Chris@76
|
340 WHERE t.id_topic = {int:current_topic}
|
Chris@76
|
341 LIMIT 1',
|
Chris@76
|
342 array(
|
Chris@76
|
343 'current_topic' => $topic,
|
Chris@76
|
344 )
|
Chris@76
|
345 );
|
Chris@76
|
346
|
Chris@76
|
347 // Assume the the topic exists, right?
|
Chris@76
|
348 if ($smcFunc['db_num_rows']($request) == 0)
|
Chris@76
|
349 fatal_lang_error('no_board');
|
Chris@76
|
350 // Get the poll information.
|
Chris@76
|
351 $pollinfo = $smcFunc['db_fetch_assoc']($request);
|
Chris@76
|
352 $smcFunc['db_free_result']($request);
|
Chris@76
|
353
|
Chris@76
|
354 // If we are adding a new poll - make sure that there isn't already a poll there.
|
Chris@76
|
355 if (!$context['is_edit'] && !empty($pollinfo['id_poll']))
|
Chris@76
|
356 fatal_lang_error('poll_already_exists');
|
Chris@76
|
357 // Otherwise, if we're editing it, it does exist I assume?
|
Chris@76
|
358 elseif ($context['is_edit'] && empty($pollinfo['id_poll']))
|
Chris@76
|
359 fatal_lang_error('poll_not_found');
|
Chris@76
|
360
|
Chris@76
|
361 // Can you do this?
|
Chris@76
|
362 if ($context['is_edit'] && !allowedTo('poll_edit_any'))
|
Chris@76
|
363 isAllowedTo('poll_edit_' . ($user_info['id'] == $pollinfo['id_member_started'] || ($pollinfo['poll_starter'] != 0 && $user_info['id'] == $pollinfo['poll_starter']) ? 'own' : 'any'));
|
Chris@76
|
364 elseif (!$context['is_edit'] && !allowedTo('poll_add_any'))
|
Chris@76
|
365 isAllowedTo('poll_add_' . ($user_info['id'] == $pollinfo['id_member_started'] ? 'own' : 'any'));
|
Chris@76
|
366
|
Chris@76
|
367 // Do we enable guest voting?
|
Chris@76
|
368 require_once($sourcedir . '/Subs-Members.php');
|
Chris@76
|
369 $groupsAllowedVote = groupsAllowedTo('poll_vote', $board);
|
Chris@76
|
370
|
Chris@76
|
371 // Want to make sure before you actually submit? Must be a lot of options, or something.
|
Chris@76
|
372 if (isset($_POST['preview']))
|
Chris@76
|
373 {
|
Chris@76
|
374 $question = $smcFunc['htmlspecialchars']($_POST['question']);
|
Chris@76
|
375
|
Chris@76
|
376 // Basic theme info...
|
Chris@76
|
377 $context['poll'] = array(
|
Chris@76
|
378 'id' => $pollinfo['id_poll'],
|
Chris@76
|
379 'question' => $question,
|
Chris@76
|
380 'hide_results' => empty($_POST['poll_hide']) ? 0 : $_POST['poll_hide'],
|
Chris@76
|
381 'change_vote' => isset($_POST['poll_change_vote']),
|
Chris@76
|
382 'guest_vote' => isset($_POST['poll_guest_vote']),
|
Chris@76
|
383 'guest_vote_allowed' => in_array(-1, $groupsAllowedVote['allowed']),
|
Chris@76
|
384 'max_votes' => empty($_POST['poll_max_votes']) ? '1' : max(1, $_POST['poll_max_votes']),
|
Chris@76
|
385 );
|
Chris@76
|
386
|
Chris@76
|
387 // Start at number one with no last id to speak of.
|
Chris@76
|
388 $number = 1;
|
Chris@76
|
389 $last_id = 0;
|
Chris@76
|
390
|
Chris@76
|
391 // Get all the choices - if this is an edit.
|
Chris@76
|
392 if ($context['is_edit'])
|
Chris@76
|
393 {
|
Chris@76
|
394 $request = $smcFunc['db_query']('', '
|
Chris@76
|
395 SELECT label, votes, id_choice
|
Chris@76
|
396 FROM {db_prefix}poll_choices
|
Chris@76
|
397 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
398 array(
|
Chris@76
|
399 'id_poll' => $pollinfo['id_poll'],
|
Chris@76
|
400 )
|
Chris@76
|
401 );
|
Chris@76
|
402 $context['choices'] = array();
|
Chris@76
|
403 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
404 {
|
Chris@76
|
405 // Get the highest id so we can add more without reusing.
|
Chris@76
|
406 if ($row['id_choice'] >= $last_id)
|
Chris@76
|
407 $last_id = $row['id_choice'] + 1;
|
Chris@76
|
408
|
Chris@76
|
409 // They cleared this by either omitting it or emptying it.
|
Chris@76
|
410 if (!isset($_POST['options'][$row['id_choice']]) || $_POST['options'][$row['id_choice']] == '')
|
Chris@76
|
411 continue;
|
Chris@76
|
412
|
Chris@76
|
413 censorText($row['label']);
|
Chris@76
|
414
|
Chris@76
|
415 // Add the choice!
|
Chris@76
|
416 $context['choices'][$row['id_choice']] = array(
|
Chris@76
|
417 'id' => $row['id_choice'],
|
Chris@76
|
418 'number' => $number++,
|
Chris@76
|
419 'votes' => $row['votes'],
|
Chris@76
|
420 'label' => $row['label'],
|
Chris@76
|
421 'is_last' => false
|
Chris@76
|
422 );
|
Chris@76
|
423 }
|
Chris@76
|
424 $smcFunc['db_free_result']($request);
|
Chris@76
|
425 }
|
Chris@76
|
426
|
Chris@76
|
427 // Work out how many options we have, so we get the 'is_last' field right...
|
Chris@76
|
428 $totalPostOptions = 0;
|
Chris@76
|
429 foreach ($_POST['options'] as $id => $label)
|
Chris@76
|
430 if ($label != '')
|
Chris@76
|
431 $totalPostOptions++;
|
Chris@76
|
432
|
Chris@76
|
433 $count = 1;
|
Chris@76
|
434 // If an option exists, update it. If it is new, add it - but don't reuse ids!
|
Chris@76
|
435 foreach ($_POST['options'] as $id => $label)
|
Chris@76
|
436 {
|
Chris@76
|
437 $label = $smcFunc['htmlspecialchars']($label);
|
Chris@76
|
438 censorText($label);
|
Chris@76
|
439
|
Chris@76
|
440 if (isset($context['choices'][$id]))
|
Chris@76
|
441 $context['choices'][$id]['label'] = $label;
|
Chris@76
|
442 elseif ($label != '')
|
Chris@76
|
443 $context['choices'][] = array(
|
Chris@76
|
444 'id' => $last_id++,
|
Chris@76
|
445 'number' => $number++,
|
Chris@76
|
446 'label' => $label,
|
Chris@76
|
447 'votes' => -1,
|
Chris@76
|
448 'is_last' => $count++ == $totalPostOptions && $totalPostOptions > 1 ? true : false,
|
Chris@76
|
449 );
|
Chris@76
|
450 }
|
Chris@76
|
451
|
Chris@76
|
452 // Make sure we have two choices for sure!
|
Chris@76
|
453 if ($totalPostOptions < 2)
|
Chris@76
|
454 {
|
Chris@76
|
455 // Need two?
|
Chris@76
|
456 if ($totalPostOptions == 0)
|
Chris@76
|
457 $context['choices'][] = array(
|
Chris@76
|
458 'id' => $last_id++,
|
Chris@76
|
459 'number' => $number++,
|
Chris@76
|
460 'label' => '',
|
Chris@76
|
461 'votes' => -1,
|
Chris@76
|
462 'is_last' => false
|
Chris@76
|
463 );
|
Chris@76
|
464 $poll_errors[] = 'poll_few';
|
Chris@76
|
465 }
|
Chris@76
|
466
|
Chris@76
|
467 // Always show one extra box...
|
Chris@76
|
468 $context['choices'][] = array(
|
Chris@76
|
469 'id' => $last_id++,
|
Chris@76
|
470 'number' => $number++,
|
Chris@76
|
471 'label' => '',
|
Chris@76
|
472 'votes' => -1,
|
Chris@76
|
473 'is_last' => true
|
Chris@76
|
474 );
|
Chris@76
|
475
|
Chris@76
|
476 if ($context['can_moderate_poll'])
|
Chris@76
|
477 $context['poll']['expiration'] = $_POST['poll_expire'];
|
Chris@76
|
478
|
Chris@76
|
479 // Check the question/option count for errors.
|
Chris@76
|
480 if (trim($_POST['question']) == '' && empty($context['poll_error']))
|
Chris@76
|
481 $poll_errors[] = 'no_question';
|
Chris@76
|
482
|
Chris@76
|
483 // No check is needed, since nothing is really posted.
|
Chris@76
|
484 checkSubmitOnce('free');
|
Chris@76
|
485
|
Chris@76
|
486 // Take a check for any errors... assuming we haven't already done so!
|
Chris@76
|
487 if (!empty($poll_errors) && empty($context['poll_error']))
|
Chris@76
|
488 {
|
Chris@76
|
489 loadLanguage('Errors');
|
Chris@76
|
490
|
Chris@76
|
491 $context['poll_error'] = array('messages' => array());
|
Chris@76
|
492 foreach ($poll_errors as $poll_error)
|
Chris@76
|
493 {
|
Chris@76
|
494 $context['poll_error'][$poll_error] = true;
|
Chris@76
|
495 $context['poll_error']['messages'][] = $txt['error_' . $poll_error];
|
Chris@76
|
496 }
|
Chris@76
|
497 }
|
Chris@76
|
498 }
|
Chris@76
|
499 else
|
Chris@76
|
500 {
|
Chris@76
|
501 // Basic theme info...
|
Chris@76
|
502 $context['poll'] = array(
|
Chris@76
|
503 'id' => $pollinfo['id_poll'],
|
Chris@76
|
504 'question' => $pollinfo['question'],
|
Chris@76
|
505 'hide_results' => $pollinfo['hide_results'],
|
Chris@76
|
506 'max_votes' => $pollinfo['max_votes'],
|
Chris@76
|
507 'change_vote' => !empty($pollinfo['change_vote']),
|
Chris@76
|
508 'guest_vote' => !empty($pollinfo['guest_vote']),
|
Chris@76
|
509 'guest_vote_allowed' => in_array(-1, $groupsAllowedVote['allowed']),
|
Chris@76
|
510 );
|
Chris@76
|
511
|
Chris@76
|
512 // Poll expiration time?
|
Chris@76
|
513 $context['poll']['expiration'] = empty($pollinfo['expire_time']) || !allowedTo('moderate_board') ? '' : ceil($pollinfo['expire_time'] <= time() ? -1 : ($pollinfo['expire_time'] - time()) / (3600 * 24));
|
Chris@76
|
514
|
Chris@76
|
515 // Get all the choices - if this is an edit.
|
Chris@76
|
516 if ($context['is_edit'])
|
Chris@76
|
517 {
|
Chris@76
|
518 $request = $smcFunc['db_query']('', '
|
Chris@76
|
519 SELECT label, votes, id_choice
|
Chris@76
|
520 FROM {db_prefix}poll_choices
|
Chris@76
|
521 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
522 array(
|
Chris@76
|
523 'id_poll' => $pollinfo['id_poll'],
|
Chris@76
|
524 )
|
Chris@76
|
525 );
|
Chris@76
|
526 $context['choices'] = array();
|
Chris@76
|
527 $number = 1;
|
Chris@76
|
528 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
529 {
|
Chris@76
|
530 censorText($row['label']);
|
Chris@76
|
531
|
Chris@76
|
532 $context['choices'][$row['id_choice']] = array(
|
Chris@76
|
533 'id' => $row['id_choice'],
|
Chris@76
|
534 'number' => $number++,
|
Chris@76
|
535 'votes' => $row['votes'],
|
Chris@76
|
536 'label' => $row['label'],
|
Chris@76
|
537 'is_last' => false
|
Chris@76
|
538 );
|
Chris@76
|
539 }
|
Chris@76
|
540 $smcFunc['db_free_result']($request);
|
Chris@76
|
541
|
Chris@76
|
542 $last_id = max(array_keys($context['choices'])) + 1;
|
Chris@76
|
543
|
Chris@76
|
544 // Add an extra choice...
|
Chris@76
|
545 $context['choices'][] = array(
|
Chris@76
|
546 'id' => $last_id,
|
Chris@76
|
547 'number' => $number,
|
Chris@76
|
548 'votes' => -1,
|
Chris@76
|
549 'label' => '',
|
Chris@76
|
550 'is_last' => true
|
Chris@76
|
551 );
|
Chris@76
|
552 }
|
Chris@76
|
553 // New poll?
|
Chris@76
|
554 else
|
Chris@76
|
555 {
|
Chris@76
|
556 // Setup the default poll options.
|
Chris@76
|
557 $context['poll'] = array(
|
Chris@76
|
558 'id' => 0,
|
Chris@76
|
559 'question' => '',
|
Chris@76
|
560 'hide_results' => 0,
|
Chris@76
|
561 'max_votes' => 1,
|
Chris@76
|
562 'change_vote' => 0,
|
Chris@76
|
563 'guest_vote' => 0,
|
Chris@76
|
564 'guest_vote_allowed' => in_array(-1, $groupsAllowedVote['allowed']),
|
Chris@76
|
565 'expiration' => '',
|
Chris@76
|
566 );
|
Chris@76
|
567
|
Chris@76
|
568 // Make all five poll choices empty.
|
Chris@76
|
569 $context['choices'] = array(
|
Chris@76
|
570 array('id' => 0, 'number' => 1, 'votes' => -1, 'label' => '', 'is_last' => false),
|
Chris@76
|
571 array('id' => 1, 'number' => 2, 'votes' => -1, 'label' => '', 'is_last' => false),
|
Chris@76
|
572 array('id' => 2, 'number' => 3, 'votes' => -1, 'label' => '', 'is_last' => false),
|
Chris@76
|
573 array('id' => 3, 'number' => 4, 'votes' => -1, 'label' => '', 'is_last' => false),
|
Chris@76
|
574 array('id' => 4, 'number' => 5, 'votes' => -1, 'label' => '', 'is_last' => true)
|
Chris@76
|
575 );
|
Chris@76
|
576 }
|
Chris@76
|
577 }
|
Chris@76
|
578 $context['page_title'] = $context['is_edit'] ? $txt['poll_edit'] : $txt['add_poll'];
|
Chris@76
|
579
|
Chris@76
|
580 // Build the link tree.
|
Chris@76
|
581 censorText($pollinfo['subject']);
|
Chris@76
|
582 $context['linktree'][] = array(
|
Chris@76
|
583 'url' => $scripturl . '?topic=' . $topic . '.0',
|
Chris@76
|
584 'name' => $pollinfo['subject'],
|
Chris@76
|
585 );
|
Chris@76
|
586 $context['linktree'][] = array(
|
Chris@76
|
587 'name' => $context['page_title'],
|
Chris@76
|
588 );
|
Chris@76
|
589
|
Chris@76
|
590 // Register this form in the session variables.
|
Chris@76
|
591 checkSubmitOnce('register');
|
Chris@76
|
592 }
|
Chris@76
|
593
|
Chris@76
|
594 // Change a poll...
|
Chris@76
|
595 function EditPoll2()
|
Chris@76
|
596 {
|
Chris@76
|
597 global $txt, $topic, $board, $context;
|
Chris@76
|
598 global $modSettings, $user_info, $smcFunc, $sourcedir;
|
Chris@76
|
599
|
Chris@76
|
600 // Sneaking off, are we?
|
Chris@76
|
601 if (empty($_POST))
|
Chris@76
|
602 redirectexit('action=editpoll;topic=' . $topic . '.0');
|
Chris@76
|
603
|
Chris@76
|
604 if (checkSession('post', '', false) != '')
|
Chris@76
|
605 $poll_errors[] = 'session_timeout';
|
Chris@76
|
606
|
Chris@76
|
607 if (isset($_POST['preview']))
|
Chris@76
|
608 return EditPoll();
|
Chris@76
|
609
|
Chris@76
|
610 // HACKERS (!!) can't edit :P.
|
Chris@76
|
611 if (empty($topic))
|
Chris@76
|
612 fatal_lang_error('no_access', false);
|
Chris@76
|
613
|
Chris@76
|
614 // Is this a new poll, or editing an existing?
|
Chris@76
|
615 $isEdit = isset($_REQUEST['add']) ? 0 : 1;
|
Chris@76
|
616
|
Chris@76
|
617 // Get the starter and the poll's ID - if it's an edit.
|
Chris@76
|
618 $request = $smcFunc['db_query']('', '
|
Chris@76
|
619 SELECT t.id_member_started, t.id_poll, p.id_member AS poll_starter, p.expire_time
|
Chris@76
|
620 FROM {db_prefix}topics AS t
|
Chris@76
|
621 LEFT JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
|
Chris@76
|
622 WHERE t.id_topic = {int:current_topic}
|
Chris@76
|
623 LIMIT 1',
|
Chris@76
|
624 array(
|
Chris@76
|
625 'current_topic' => $topic,
|
Chris@76
|
626 )
|
Chris@76
|
627 );
|
Chris@76
|
628 if ($smcFunc['db_num_rows']($request) == 0)
|
Chris@76
|
629 fatal_lang_error('no_board');
|
Chris@76
|
630 $bcinfo = $smcFunc['db_fetch_assoc']($request);
|
Chris@76
|
631 $smcFunc['db_free_result']($request);
|
Chris@76
|
632
|
Chris@76
|
633 // Check their adding/editing is valid.
|
Chris@76
|
634 if (!$isEdit && !empty($bcinfo['id_poll']))
|
Chris@76
|
635 fatal_lang_error('poll_already_exists');
|
Chris@76
|
636 // Are we editing a poll which doesn't exist?
|
Chris@76
|
637 elseif ($isEdit && empty($bcinfo['id_poll']))
|
Chris@76
|
638 fatal_lang_error('poll_not_found');
|
Chris@76
|
639
|
Chris@76
|
640 // Check if they have the power to add or edit the poll.
|
Chris@76
|
641 if ($isEdit && !allowedTo('poll_edit_any'))
|
Chris@76
|
642 isAllowedTo('poll_edit_' . ($user_info['id'] == $bcinfo['id_member_started'] || ($bcinfo['poll_starter'] != 0 && $user_info['id'] == $bcinfo['poll_starter']) ? 'own' : 'any'));
|
Chris@76
|
643 elseif (!$isEdit && !allowedTo('poll_add_any'))
|
Chris@76
|
644 isAllowedTo('poll_add_' . ($user_info['id'] == $bcinfo['id_member_started'] ? 'own' : 'any'));
|
Chris@76
|
645
|
Chris@76
|
646 $optionCount = 0;
|
Chris@76
|
647 // Ensure the user is leaving a valid amount of options - there must be at least two.
|
Chris@76
|
648 foreach ($_POST['options'] as $k => $option)
|
Chris@76
|
649 {
|
Chris@76
|
650 if (trim($option) != '')
|
Chris@76
|
651 $optionCount++;
|
Chris@76
|
652 }
|
Chris@76
|
653 if ($optionCount < 2)
|
Chris@76
|
654 $poll_errors[] = 'poll_few';
|
Chris@76
|
655
|
Chris@76
|
656 // Also - ensure they are not removing the question.
|
Chris@76
|
657 if (trim($_POST['question']) == '')
|
Chris@76
|
658 $poll_errors[] = 'no_question';
|
Chris@76
|
659
|
Chris@76
|
660 // Got any errors to report?
|
Chris@76
|
661 if (!empty($poll_errors))
|
Chris@76
|
662 {
|
Chris@76
|
663 loadLanguage('Errors');
|
Chris@76
|
664 // Previewing.
|
Chris@76
|
665 $_POST['preview'] = true;
|
Chris@76
|
666
|
Chris@76
|
667 $context['poll_error'] = array('messages' => array());
|
Chris@76
|
668 foreach ($poll_errors as $poll_error)
|
Chris@76
|
669 {
|
Chris@76
|
670 $context['poll_error'][$poll_error] = true;
|
Chris@76
|
671 $context['poll_error']['messages'][] = $txt['error_' . $poll_error];
|
Chris@76
|
672 }
|
Chris@76
|
673
|
Chris@76
|
674 return EditPoll();
|
Chris@76
|
675 }
|
Chris@76
|
676
|
Chris@76
|
677 // Prevent double submission of this form.
|
Chris@76
|
678 checkSubmitOnce('check');
|
Chris@76
|
679
|
Chris@76
|
680 // Now we've done all our error checking, let's get the core poll information cleaned... question first.
|
Chris@76
|
681 $_POST['question'] = $smcFunc['htmlspecialchars']($_POST['question']);
|
Chris@76
|
682 $_POST['question'] = $smcFunc['truncate']($_POST['question'], 255);
|
Chris@76
|
683
|
Chris@76
|
684 $_POST['poll_hide'] = (int) $_POST['poll_hide'];
|
Chris@76
|
685 $_POST['poll_expire'] = isset($_POST['poll_expire']) ? (int) $_POST['poll_expire'] : 0;
|
Chris@76
|
686 $_POST['poll_change_vote'] = isset($_POST['poll_change_vote']) ? 1 : 0;
|
Chris@76
|
687 $_POST['poll_guest_vote'] = isset($_POST['poll_guest_vote']) ? 1 : 0;
|
Chris@76
|
688
|
Chris@76
|
689 // Make sure guests are actually allowed to vote generally.
|
Chris@76
|
690 if ($_POST['poll_guest_vote'])
|
Chris@76
|
691 {
|
Chris@76
|
692 require_once($sourcedir . '/Subs-Members.php');
|
Chris@76
|
693 $allowedGroups = groupsAllowedTo('poll_vote', $board);
|
Chris@76
|
694 if (!in_array(-1, $allowedGroups['allowed']))
|
Chris@76
|
695 $_POST['poll_guest_vote'] = 0;
|
Chris@76
|
696 }
|
Chris@76
|
697
|
Chris@76
|
698 // Ensure that the number options allowed makes sense, and the expiration date is valid.
|
Chris@76
|
699 if (!$isEdit || allowedTo('moderate_board'))
|
Chris@76
|
700 {
|
Chris@76
|
701 $_POST['poll_expire'] = $_POST['poll_expire'] > 9999 ? 9999 : ($_POST['poll_expire'] < 0 ? 0 : $_POST['poll_expire']);
|
Chris@76
|
702
|
Chris@76
|
703 if (empty($_POST['poll_expire']) && $_POST['poll_hide'] == 2)
|
Chris@76
|
704 $_POST['poll_hide'] = 1;
|
Chris@76
|
705 elseif (!$isEdit || $_POST['poll_expire'] != ceil($bcinfo['expire_time'] <= time() ? -1 : ($bcinfo['expire_time'] - time()) / (3600 * 24)))
|
Chris@76
|
706 $_POST['poll_expire'] = empty($_POST['poll_expire']) ? '0' : time() + $_POST['poll_expire'] * 3600 * 24;
|
Chris@76
|
707 else
|
Chris@76
|
708 $_POST['poll_expire'] = $bcinfo['expire_time'];
|
Chris@76
|
709
|
Chris@76
|
710 if (empty($_POST['poll_max_votes']) || $_POST['poll_max_votes'] <= 0)
|
Chris@76
|
711 $_POST['poll_max_votes'] = 1;
|
Chris@76
|
712 else
|
Chris@76
|
713 $_POST['poll_max_votes'] = (int) $_POST['poll_max_votes'];
|
Chris@76
|
714 }
|
Chris@76
|
715
|
Chris@76
|
716 // If we're editing, let's commit the changes.
|
Chris@76
|
717 if ($isEdit)
|
Chris@76
|
718 {
|
Chris@76
|
719 $smcFunc['db_query']('', '
|
Chris@76
|
720 UPDATE {db_prefix}polls
|
Chris@76
|
721 SET question = {string:question}, change_vote = {int:change_vote},' . (allowedTo('moderate_board') ? '
|
Chris@76
|
722 hide_results = {int:hide_results}, expire_time = {int:expire_time}, max_votes = {int:max_votes},
|
Chris@76
|
723 guest_vote = {int:guest_vote}' : '
|
Chris@76
|
724 hide_results = CASE WHEN expire_time = {int:expire_time_zero} AND {int:hide_results} = 2 THEN 1 ELSE {int:hide_results} END') . '
|
Chris@76
|
725 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
726 array(
|
Chris@76
|
727 'change_vote' => $_POST['poll_change_vote'],
|
Chris@76
|
728 'hide_results' => $_POST['poll_hide'],
|
Chris@76
|
729 'expire_time' => !empty($_POST['poll_expire']) ? $_POST['poll_expire'] : 0,
|
Chris@76
|
730 'max_votes' => !empty($_POST['poll_max_votes']) ? $_POST['poll_max_votes'] : 0,
|
Chris@76
|
731 'guest_vote' => $_POST['poll_guest_vote'],
|
Chris@76
|
732 'expire_time_zero' => 0,
|
Chris@76
|
733 'id_poll' => $bcinfo['id_poll'],
|
Chris@76
|
734 'question' => $_POST['question'],
|
Chris@76
|
735 )
|
Chris@76
|
736 );
|
Chris@76
|
737 }
|
Chris@76
|
738 // Otherwise, let's get our poll going!
|
Chris@76
|
739 else
|
Chris@76
|
740 {
|
Chris@76
|
741 // Create the poll.
|
Chris@76
|
742 $smcFunc['db_insert']('',
|
Chris@76
|
743 '{db_prefix}polls',
|
Chris@76
|
744 array(
|
Chris@76
|
745 'question' => 'string-255', 'hide_results' => 'int', 'max_votes' => 'int', 'expire_time' => 'int', 'id_member' => 'int',
|
Chris@76
|
746 'poster_name' => 'string-255', 'change_vote' => 'int', 'guest_vote' => 'int'
|
Chris@76
|
747 ),
|
Chris@76
|
748 array(
|
Chris@76
|
749 $_POST['question'], $_POST['poll_hide'], $_POST['poll_max_votes'], $_POST['poll_expire'], $user_info['id'],
|
Chris@76
|
750 $user_info['username'], $_POST['poll_change_vote'], $_POST['poll_guest_vote'],
|
Chris@76
|
751 ),
|
Chris@76
|
752 array('id_poll')
|
Chris@76
|
753 );
|
Chris@76
|
754
|
Chris@76
|
755 // Set the poll ID.
|
Chris@76
|
756 $bcinfo['id_poll'] = $smcFunc['db_insert_id']('{db_prefix}polls', 'id_poll');
|
Chris@76
|
757
|
Chris@76
|
758 // Link the poll to the topic
|
Chris@76
|
759 $smcFunc['db_query']('', '
|
Chris@76
|
760 UPDATE {db_prefix}topics
|
Chris@76
|
761 SET id_poll = {int:id_poll}
|
Chris@76
|
762 WHERE id_topic = {int:current_topic}',
|
Chris@76
|
763 array(
|
Chris@76
|
764 'current_topic' => $topic,
|
Chris@76
|
765 'id_poll' => $bcinfo['id_poll'],
|
Chris@76
|
766 )
|
Chris@76
|
767 );
|
Chris@76
|
768 }
|
Chris@76
|
769
|
Chris@76
|
770 // Get all the choices. (no better way to remove all emptied and add previously non-existent ones.)
|
Chris@76
|
771 $request = $smcFunc['db_query']('', '
|
Chris@76
|
772 SELECT id_choice
|
Chris@76
|
773 FROM {db_prefix}poll_choices
|
Chris@76
|
774 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
775 array(
|
Chris@76
|
776 'id_poll' => $bcinfo['id_poll'],
|
Chris@76
|
777 )
|
Chris@76
|
778 );
|
Chris@76
|
779 $choices = array();
|
Chris@76
|
780 while ($row = $smcFunc['db_fetch_assoc']($request))
|
Chris@76
|
781 $choices[] = $row['id_choice'];
|
Chris@76
|
782 $smcFunc['db_free_result']($request);
|
Chris@76
|
783
|
Chris@76
|
784 $delete_options = array();
|
Chris@76
|
785 foreach ($_POST['options'] as $k => $option)
|
Chris@76
|
786 {
|
Chris@76
|
787 // Make sure the key is numeric for sanity's sake.
|
Chris@76
|
788 $k = (int) $k;
|
Chris@76
|
789
|
Chris@76
|
790 // They've cleared the box. Either they want it deleted, or it never existed.
|
Chris@76
|
791 if (trim($option) == '')
|
Chris@76
|
792 {
|
Chris@76
|
793 // They want it deleted. Bye.
|
Chris@76
|
794 if (in_array($k, $choices))
|
Chris@76
|
795 $delete_options[] = $k;
|
Chris@76
|
796
|
Chris@76
|
797 // Skip the rest...
|
Chris@76
|
798 continue;
|
Chris@76
|
799 }
|
Chris@76
|
800
|
Chris@76
|
801 // Dress the option up for its big date with the database.
|
Chris@76
|
802 $option = $smcFunc['htmlspecialchars']($option);
|
Chris@76
|
803
|
Chris@76
|
804 // If it's already there, update it. If it's not... add it.
|
Chris@76
|
805 if (in_array($k, $choices))
|
Chris@76
|
806 $smcFunc['db_query']('', '
|
Chris@76
|
807 UPDATE {db_prefix}poll_choices
|
Chris@76
|
808 SET label = {string:option_name}
|
Chris@76
|
809 WHERE id_poll = {int:id_poll}
|
Chris@76
|
810 AND id_choice = {int:id_choice}',
|
Chris@76
|
811 array(
|
Chris@76
|
812 'id_poll' => $bcinfo['id_poll'],
|
Chris@76
|
813 'id_choice' => $k,
|
Chris@76
|
814 'option_name' => $option,
|
Chris@76
|
815 )
|
Chris@76
|
816 );
|
Chris@76
|
817 else
|
Chris@76
|
818 $smcFunc['db_insert']('',
|
Chris@76
|
819 '{db_prefix}poll_choices',
|
Chris@76
|
820 array(
|
Chris@76
|
821 'id_poll' => 'int', 'id_choice' => 'int', 'label' => 'string-255', 'votes' => 'int',
|
Chris@76
|
822 ),
|
Chris@76
|
823 array(
|
Chris@76
|
824 $bcinfo['id_poll'], $k, $option, 0,
|
Chris@76
|
825 ),
|
Chris@76
|
826 array()
|
Chris@76
|
827 );
|
Chris@76
|
828 }
|
Chris@76
|
829
|
Chris@76
|
830 // I'm sorry, but... well, no one was choosing you. Poor options, I'll put you out of your misery.
|
Chris@76
|
831 if (!empty($delete_options))
|
Chris@76
|
832 {
|
Chris@76
|
833 $smcFunc['db_query']('', '
|
Chris@76
|
834 DELETE FROM {db_prefix}log_polls
|
Chris@76
|
835 WHERE id_poll = {int:id_poll}
|
Chris@76
|
836 AND id_choice IN ({array_int:delete_options})',
|
Chris@76
|
837 array(
|
Chris@76
|
838 'delete_options' => $delete_options,
|
Chris@76
|
839 'id_poll' => $bcinfo['id_poll'],
|
Chris@76
|
840 )
|
Chris@76
|
841 );
|
Chris@76
|
842 $smcFunc['db_query']('', '
|
Chris@76
|
843 DELETE FROM {db_prefix}poll_choices
|
Chris@76
|
844 WHERE id_poll = {int:id_poll}
|
Chris@76
|
845 AND id_choice IN ({array_int:delete_options})',
|
Chris@76
|
846 array(
|
Chris@76
|
847 'delete_options' => $delete_options,
|
Chris@76
|
848 'id_poll' => $bcinfo['id_poll'],
|
Chris@76
|
849 )
|
Chris@76
|
850 );
|
Chris@76
|
851 }
|
Chris@76
|
852
|
Chris@76
|
853 // Shall I reset the vote count, sir?
|
Chris@76
|
854 if (isset($_POST['resetVoteCount']))
|
Chris@76
|
855 {
|
Chris@76
|
856 $smcFunc['db_query']('', '
|
Chris@76
|
857 UPDATE {db_prefix}polls
|
Chris@76
|
858 SET num_guest_voters = {int:no_votes}, reset_poll = {int:time}
|
Chris@76
|
859 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
860 array(
|
Chris@76
|
861 'no_votes' => 0,
|
Chris@76
|
862 'id_poll' => $bcinfo['id_poll'],
|
Chris@76
|
863 'time' => time(),
|
Chris@76
|
864 )
|
Chris@76
|
865 );
|
Chris@76
|
866 $smcFunc['db_query']('', '
|
Chris@76
|
867 UPDATE {db_prefix}poll_choices
|
Chris@76
|
868 SET votes = {int:no_votes}
|
Chris@76
|
869 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
870 array(
|
Chris@76
|
871 'no_votes' => 0,
|
Chris@76
|
872 'id_poll' => $bcinfo['id_poll'],
|
Chris@76
|
873 )
|
Chris@76
|
874 );
|
Chris@76
|
875 $smcFunc['db_query']('', '
|
Chris@76
|
876 DELETE FROM {db_prefix}log_polls
|
Chris@76
|
877 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
878 array(
|
Chris@76
|
879 'id_poll' => $bcinfo['id_poll'],
|
Chris@76
|
880 )
|
Chris@76
|
881 );
|
Chris@76
|
882 }
|
Chris@76
|
883
|
Chris@76
|
884 // Off we go.
|
Chris@76
|
885 redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
|
Chris@76
|
886 }
|
Chris@76
|
887
|
Chris@76
|
888 // Remove a poll from a topic without removing the topic.
|
Chris@76
|
889 function RemovePoll()
|
Chris@76
|
890 {
|
Chris@76
|
891 global $topic, $user_info, $smcFunc;
|
Chris@76
|
892
|
Chris@76
|
893 // Make sure the topic is not empty.
|
Chris@76
|
894 if (empty($topic))
|
Chris@76
|
895 fatal_lang_error('no_access', false);
|
Chris@76
|
896
|
Chris@76
|
897 // Verify the session.
|
Chris@76
|
898 checkSession('get');
|
Chris@76
|
899
|
Chris@76
|
900 // Check permissions.
|
Chris@76
|
901 if (!allowedTo('poll_remove_any'))
|
Chris@76
|
902 {
|
Chris@76
|
903 $request = $smcFunc['db_query']('', '
|
Chris@76
|
904 SELECT t.id_member_started, p.id_member AS poll_starter
|
Chris@76
|
905 FROM {db_prefix}topics AS t
|
Chris@76
|
906 INNER JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
|
Chris@76
|
907 WHERE t.id_topic = {int:current_topic}
|
Chris@76
|
908 LIMIT 1',
|
Chris@76
|
909 array(
|
Chris@76
|
910 'current_topic' => $topic,
|
Chris@76
|
911 )
|
Chris@76
|
912 );
|
Chris@76
|
913 if ($smcFunc['db_num_rows']($request) == 0)
|
Chris@76
|
914 fatal_lang_error('no_access', false);
|
Chris@76
|
915 list ($topicStarter, $pollStarter) = $smcFunc['db_fetch_row']($request);
|
Chris@76
|
916 $smcFunc['db_free_result']($request);
|
Chris@76
|
917
|
Chris@76
|
918 isAllowedTo('poll_remove_' . ($topicStarter == $user_info['id'] || ($pollStarter != 0 && $user_info['id'] == $pollStarter) ? 'own' : 'any'));
|
Chris@76
|
919 }
|
Chris@76
|
920
|
Chris@76
|
921 // Retrieve the poll ID.
|
Chris@76
|
922 $request = $smcFunc['db_query']('', '
|
Chris@76
|
923 SELECT id_poll
|
Chris@76
|
924 FROM {db_prefix}topics
|
Chris@76
|
925 WHERE id_topic = {int:current_topic}
|
Chris@76
|
926 LIMIT 1',
|
Chris@76
|
927 array(
|
Chris@76
|
928 'current_topic' => $topic,
|
Chris@76
|
929 )
|
Chris@76
|
930 );
|
Chris@76
|
931 list ($pollID) = $smcFunc['db_fetch_row']($request);
|
Chris@76
|
932 $smcFunc['db_free_result']($request);
|
Chris@76
|
933
|
Chris@76
|
934 // Remove all user logs for this poll.
|
Chris@76
|
935 $smcFunc['db_query']('', '
|
Chris@76
|
936 DELETE FROM {db_prefix}log_polls
|
Chris@76
|
937 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
938 array(
|
Chris@76
|
939 'id_poll' => $pollID,
|
Chris@76
|
940 )
|
Chris@76
|
941 );
|
Chris@76
|
942 // Remove all poll choices.
|
Chris@76
|
943 $smcFunc['db_query']('', '
|
Chris@76
|
944 DELETE FROM {db_prefix}poll_choices
|
Chris@76
|
945 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
946 array(
|
Chris@76
|
947 'id_poll' => $pollID,
|
Chris@76
|
948 )
|
Chris@76
|
949 );
|
Chris@76
|
950 // Remove the poll itself.
|
Chris@76
|
951 $smcFunc['db_query']('', '
|
Chris@76
|
952 DELETE FROM {db_prefix}polls
|
Chris@76
|
953 WHERE id_poll = {int:id_poll}',
|
Chris@76
|
954 array(
|
Chris@76
|
955 'id_poll' => $pollID,
|
Chris@76
|
956 )
|
Chris@76
|
957 );
|
Chris@76
|
958 // Finally set the topic poll ID back to 0!
|
Chris@76
|
959 $smcFunc['db_query']('', '
|
Chris@76
|
960 UPDATE {db_prefix}topics
|
Chris@76
|
961 SET id_poll = {int:no_poll}
|
Chris@76
|
962 WHERE id_topic = {int:current_topic}',
|
Chris@76
|
963 array(
|
Chris@76
|
964 'current_topic' => $topic,
|
Chris@76
|
965 'no_poll' => 0,
|
Chris@76
|
966 )
|
Chris@76
|
967 );
|
Chris@76
|
968
|
Chris@76
|
969 // Take the moderator back to the topic.
|
Chris@76
|
970 redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
|
Chris@76
|
971 }
|
Chris@76
|
972
|
Chris@76
|
973 ?> |