Passed
Push — master ( 882d2a...8f67b7 )
by Michael
03:09
created

polls.php (3 issues)

Labels
Severity
1
<?php
2
/**
3
 * Newbb module
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       XOOPS Project (https://xoops.org)
13
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package         newbb
15
 * @since           4.0
16
 * @author          Taiwen Jiang <[email protected]>
17
 */
18
19
use Xmf\Request;
20
use XoopsModules\Newbb;
21
22
// rewrite by irmtfan and zyspec to accept xoopspoll 1.4 and all old xoopspoll and umfrage versions and all clones
23
24
require_once __DIR__ . '/header.php';
25
require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
26
require_once $GLOBALS['xoops']->path('class/xoopslists.php');
27
require_once $GLOBALS['xoops']->path('class/xoopsblock.php');
28
xoops_load('XoopsLocal');
29
$op      = 'add';
30
$goodOps = [
31
    'add',
32
    'save',
33
    'edit',
34
    'update',
35
    'addmore',
36
    'savemore',
37
    'delete',
38
    'delete_ok',
39
    'restart',
40
    'restart_ok',
41
    'log'
42
];
43
$op      = isset($_REQUEST['op']) ? $_REQUEST['op'] : 'add';
44
$op      = (!in_array($op, $goodOps)) ? 'add' : $op;
45
46
$poll_id  = Request::getInt('poll_id', Request::getInt('poll_id', 0, 'GET'), 'POST');
47
$topic_id = Request::getInt('topic_id', Request::getInt('topic_id', 0, 'GET'), 'POST');
48
49
// deal with permissions
50
/** @var Newbb\TopicHandler $topicHandler */
51
$topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
52
$topicObject  = $topicHandler->get($topic_id);
53
// topic exist
54
if (is_object($topicObject)) {
55
    $forum_id = $topicObject->getVar('forum_id');
56
} else {
57
    redirect_header(XOOPS_URL . '/index.php', 2, _MD_NEWBB_POLLMODULE_ERROR . ': ' . _MD_NEWBB_FORUMNOEXIST);
58
}
59
// forum access permission
60
/** @var Newbb\ForumHandler $forumHandler */
61
$forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
62
$forumObject  = $forumHandler->get($forum_id);
63
if (!$forumHandler->getPermission($forumObject)) {
64
    redirect_header(XOOPS_URL . '/index.php', 2, _MD_NEWBB_NORIGHTTOACCESS);
65
}
66
// topic view permission
67
if (!$topicHandler->getPermission($forumObject, $topicObject->getVar('topic_status'), 'view')) {
68
    redirect_header('viewforum.php?forum=' . $forum_id, 2, _MD_NEWBB_NORIGHTTOVIEW);
69
}
70
// poll module
71
$pollModuleHandler = $moduleHandler->getByDirname($GLOBALS['xoopsModuleConfig']['poll_module']);
72
if (is_object($pollModuleHandler) && $pollModuleHandler->getVar('isactive')) {
73
    // new xoopspoll module
74
    if ($pollModuleHandler->getVar('version') >= 140) {
75
        xoops_load('constants', $GLOBALS['xoopsModuleConfig']['poll_module']);
76
        xoops_load('pollUtility', $GLOBALS['xoopsModuleConfig']['poll_module']);
77
        xoops_load('request', $GLOBALS['xoopsModuleConfig']['poll_module']);
78
        xoops_loadLanguage('admin', $GLOBALS['xoopsModuleConfig']['poll_module']);
79
        /** @var \XoopspollPollHandler $xpPollHandler */
80
        $xpPollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll');
0 ignored issues
show
The type Xoopspoll\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
        /** @var \XoopsPoll $pollObject */
82
        $pollObject = $xpPollHandler->get($poll_id); // will create poll if poll_id = 0 exist
83
        // old xoopspoll or umfrage or any clone from them
84
    } else {
85
        include $GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/include/constants.php');
86
        $classPoll  = $topicObject->loadOldPoll();
87
        $pollObject = new $classPoll($poll_id); // will create poll if poll_id = 0 exist
88
    }
89
} else {
90
    // irmtfan - issue with javascript:history.go(-1)
91
    redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_POLLMODULE_ERROR);
92
}
93
// include header
94
include $GLOBALS['xoops']->path('header.php');
95
96
// no admin user permission
97
if (is_object($GLOBALS['xoopsUser']) && !newbbIsAdmin($forumObject)) {
98
    $perm = false;
99
    if ($topicHandler->getPermission($forumObject, $topicObject->getVar('topic_status'), 'addpoll')) {
100
        if (('add' === $op || 'save' === $op || 'update' === $op) && !$topicObject->getVar('topic_haspoll')
101
            && ($GLOBALS['xoopsUser']->getVar('uid') == $topicObject->getVar('topic_poster'))) {
102
            $perm = true;
103
        } elseif (!empty($poll_id) && ($GLOBALS['xoopsUser']->getVar('uid') == $pollObject->getVar('user_id'))) {
104
            $perm = true;
105
        }
106
    }
107
    if (!$perm) {
108
        redirect_header("viewtopic.php?topic_id={$topic_id}", 2, _NOPERM);
109
    }
110
}
111
switch ($op) {
112
    case 'add':
113
        // new xoopspoll module
114
        if ($pollModuleHandler->getVar('version') >= 140) {
115
            echo '<h4>' . _MD_NEWBB_POLL_CREATNEWPOLL . "</h4>\n";
116
            $pollObject->renderForm(Request::getString('PHP_SELF', '', 'SERVER'), 'post', ['topic_id' => $topic_id]);
117
        // old xoopspoll or umfrage or any clone from them
118
        } else {
119
            $classOption  = $classPoll . 'Option';
120
            $poll_form    = new \XoopsThemeForm(_MD_NEWBB_POLL_CREATNEWPOLL, 'poll_form', 'polls.php', 'post', true);
121
            $author_label = new \XoopsFormLabel(_MD_NEWBB_POLL_AUTHOR, is_object($GLOBALS['xoopsUser']) ? ("<a href='"
122
                                                                                                          . XOOPS_URL
123
                                                                                                          . '/userinfo.php?uid='
124
                                                                                                          . $GLOBALS['xoopsUser']->getVar('uid')
125
                                                                                                          . "'>"
126
                                                                                                          . newbbGetUnameFromId($GLOBALS['xoopsUser']->getVar('uid'), $GLOBALS['xoopsModuleConfig']['show_realname'])
127
                                                                                                          . '</a>') : $GLOBALS['xoopsConfig']['anonymous']);
128
            $poll_form->addElement($author_label);
129
            $question_text = new \XoopsFormText(_MD_NEWBB_POLL_POLLQUESTION, 'question', 50, 255);
130
            $poll_form->addElement($question_text);
131
            $desc_tarea = new \XoopsFormTextarea(_MD_NEWBB_POLL_POLLDESC, 'description');
132
            $poll_form->addElement($desc_tarea);
133
            $currenttime = formatTimestamp(time(), 'Y-m-d H:i:s');
134
            $endtime     = formatTimestamp(time() + 604800, 'Y-m-d H:i:s');
135
            $expire_text = new \XoopsFormText(_MD_NEWBB_POLL_EXPIRATION . '<br><small>' . _MD_NEWBB_POLL_FORMAT . '<br>' . sprintf(_MD_NEWBB_POLL_CURRENTTIME, $currenttime) . '</small>', 'end_time', 30, 19, $endtime);
136
            $poll_form->addElement($expire_text);
137
138
            $weight_text = new \XoopsFormText(_MD_NEWBB_POLL_DISPLAYORDER, 'weight', 6, 5, 0);
139
            $poll_form->addElement($weight_text);
140
141
            $multi_yn = new \XoopsFormRadioYN(_MD_NEWBB_POLL_ALLOWMULTI, 'multiple', 0);
142
            $poll_form->addElement($multi_yn);
143
144
            $notify_yn = new \XoopsFormRadioYN(_MD_NEWBB_POLL_NOTIFY, 'notify', 1);
145
            $poll_form->addElement($notify_yn);
146
147
            $option_tray    = new \XoopsFormElementTray(_MD_NEWBB_POLL_POLLOPTIONS, '');
148
            $barcolor_array = \XoopsLists::getImgListAsArray($GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/assets/images/colorbars/'));
149
            for ($i = 0; $i < 10; ++$i) {
150
                $current_bar = ('blank.gif' !== current($barcolor_array)) ? current($barcolor_array) : next($barcolor_array);
151
                $option_text = new \XoopsFormText('', 'option_text[]', 50, 255);
152
                $option_tray->addElement($option_text);
153
                $color_select = new \XoopsFormSelect('', "option_color[{$i}]", $current_bar);
154
                $color_select->addOptionArray($barcolor_array);
155
                $color_select->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[{$i}]\", \"modules/" . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/assets/images/colorbars", "", "' . XOOPS_URL . "\")'");
156
                $color_label = new \XoopsFormLabel('', "<img src='"
157
                                                      . XOOPS_URL
158
                                                      . '/modules/'
159
                                                      . $GLOBALS['xoopsModuleConfig']['poll_module']
160
                                                      . '/assets/images/colorbars/'
161
                                                      . $current_bar
162
                                                      . "' name='option_color_image[{$i}]' id='option_color_image[{$i}]' width='30' align='bottom' height='15' alt='' /><br>");
163
                $option_tray->addElement($color_select);
164
                $option_tray->addElement($color_label);
165
                if (!next($barcolor_array)) {
166
                    reset($barcolor_array);
167
                }
168
                unset($color_select, $color_label);
169
            }
170
            $poll_form->addElement($option_tray);
171
172
            $poll_form->addElement(new \XoopsFormHidden('op', 'save'));
173
            $poll_form->addElement(new \XoopsFormHidden('topic_id', $topic_id));
174
            $poll_form->addElement(new \XoopsFormHidden('poll_id', $poll_id));
175
            $poll_form->addElement(new \XoopsFormHidden('user_id', is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0));
176
            $poll_form->addElement(new \XoopsFormButtonTray('poll_submit', _SUBMIT, 'submit'));
177
            echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . '</h4>';
178
            $poll_form->display();
179
        }
180
        break; // op: add
181
182
    case 'edit':
183
        // new xoopspoll module
184
        if ($pollModuleHandler->getVar('version') >= 140) {
185
            echo '<h4>' . _MD_NEWBB_POLL_EDITPOLL . "</h4>\n";
186
            $pollObject->renderForm(Request::getString('PHP_SELF', '', 'SERVER'), 'post', ['topic_id' => $topic_id]);
187
        // old xoopspoll or umfrage or any clone from them
188
        } else {
189
            $classOption  = $classPoll . 'Option';
190
            $poll_form    = new \XoopsThemeForm(_MD_NEWBB_POLL_EDITPOLL, 'poll_form', 'polls.php', 'post', true);
191
            $author_label = new \XoopsFormLabel(_MD_NEWBB_POLL_AUTHOR, "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $pollObject->getVar('user_id') . "'>" . newbbGetUnameFromId($pollObject->getVar('user_id'), $GLOBALS['xoopsModuleConfig']['show_realname']) . '</a>');
192
            $poll_form->addElement($author_label);
193
            $question_text = new \XoopsFormText(_MD_NEWBB_POLL_POLLQUESTION, 'question', 50, 255, $pollObject->getVar('question', 'E'));
194
            $poll_form->addElement($question_text);
195
            $desc_tarea = new \XoopsFormTextarea(_MD_NEWBB_POLL_POLLDESC, 'description', $pollObject->getVar('description', 'E'));
196
            $poll_form->addElement($desc_tarea);
197
            $date = formatTimestamp($pollObject->getVar('end_time'), 'Y-m-d H:i:s'); // important "Y-m-d H:i:s" use in jdf function
198
            if (!$pollObject->hasExpired()) {
199
                $expire_text = new \XoopsFormText(_MD_NEWBB_POLL_EXPIRATION . '<br><small>' . _MD_NEWBB_POLL_FORMAT . '<br>' . sprintf(_MD_NEWBB_POLL_CURRENTTIME, formatTimestamp(time(), 'Y-m-d H:i:s')) . '</small>', 'end_time', 20, 19, $date);
200
                $poll_form->addElement($expire_text);
201
            } else {
202
                // irmtfan full URL - add topic_id
203
                $restart_label = new \XoopsFormLabel(
204
                    _MD_NEWBB_POLL_EXPIRATION,
205
                                                    sprintf(_MD_NEWBB_POLL_EXPIREDAT, $date) . "<br><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/polls.php?op=restart&amp;poll_id={$poll_id}&amp;topic_id={$topic_id}'>" . _MD_NEWBB_POLL_RESTART . '</a>'
206
                );
207
                $poll_form->addElement($restart_label);
208
            }
209
            $weight_text = new \XoopsFormText(_MD_NEWBB_POLL_DISPLAYORDER, 'weight', 6, 5, $pollObject->getVar('weight'));
210
            $poll_form->addElement($weight_text);
211
            $multi_yn = new \XoopsFormRadioYN(_MD_NEWBB_POLL_ALLOWMULTI, 'multiple', $pollObject->getVar('multiple'));
212
            $poll_form->addElement($multi_yn);
213
            $options_arr  =& $classOption::getAllByPollId($poll_id);
214
            $notify_value = 1;
215
            if (0 !== $pollObject->getVar('mail_status')) {
216
                $notify_value = 0;
217
            }
218
            $notify_yn = new \XoopsFormRadioYN(_MD_NEWBB_POLL_NOTIFY, 'notify', $notify_value);
219
            $poll_form->addElement($notify_yn);
220
            $option_tray    = new \XoopsFormElementTray(_MD_NEWBB_POLL_POLLOPTIONS, '');
221
            $barcolor_array = \XoopsLists::getImgListAsArray($GLOBALS['xoops']->path("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars/"));
222
            $i              = 0;
223
            foreach ($options_arr as $option) {
224
                /** @var \XoopsPoll $option */
225
                $option_tray->addElement(new \XoopsFormText('', 'option_text[]', 50, 255, $option->getVar('option_text')));
226
                $option_tray->addElement(new \XoopsFormHidden('option_id[]', $option->getVar('option_id')));
227
                $color_select = new \XoopsFormSelect('', 'option_color[{$i}]', $option->getVar('option_color'));
228
                $color_select->addOptionArray($barcolor_array);
229
                $color_select->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[" . $i . "]\", \"modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars\", \"\", \"" . XOOPS_URL . "\")'");
230
                $color_label = new \XoopsFormLabel('', "<img src='"
231
                                                      . $GLOBALS['xoops']->url("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars/" . $option->getVar('option_color', 'E'))
232
                                                      . "' name='option_color_image[{$i}]' id='option_color_image[{$i}]' class='alignbottom' width='30' height='15' alt='' /><br>");
233
                $option_tray->addElement($color_select);
234
                $option_tray->addElement($color_label);
235
                unset($color_select, $color_label);
236
                ++$i;
237
            }
238
            // irmtfan full URL
239
            $more_label = new \XoopsFormLabel('', "<br><a href='" . XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/polls.php?op=addmore&amp;poll_id={$poll_id}&amp;topic_id={$topic_id}'>" . _MD_NEWBB_POLL_ADDMORE . '</a>');
240
            $option_tray->addElement($more_label);
241
            $poll_form->addElement($option_tray);
242
            $poll_form->addElement(new \XoopsFormHidden('op', 'update'));
243
            $poll_form->addElement(new \XoopsFormHidden('topic_id', $topic_id));
244
            $poll_form->addElement(new \XoopsFormHidden('poll_id', $poll_id));
245
            $poll_form->addElement(new \XoopsFormButtonTray('poll_submit', _SUBMIT, 'submit'));
246
247
            echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
248
            $poll_form->display();
249
        }
250
        break; // op: edit
251
252
    case 'save':
253
        // old xoopspoll or umfrage or any clone from them
254
        if ($pollModuleHandler->getVar('version') < 140) {
255
            // check security token
256
            if (!$GLOBALS['xoopsSecurity']->check()) {
257
                redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
258
            }
259
            /*
260
             * The option check should be done before submitting
261
             */
262
            $option_empty = true;
263
            if (!Request::getString('option_text', '', 'POST')) {
264
                // irmtfan - issue with javascript:history.go(-1)
265
                redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
266
            }
267
            $option_text = Request::getArray('option_text', '', 'POST');
268
            foreach ($option_text as $optxt) {
269
                if ('' !== trim($optxt)) {
270
                    $option_empty = false;
271
                    break;
272
                }
273
            }
274
            if ($option_empty) {
275
                // irmtfan - issue with javascript:history.go(-1)
276
                redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
277
            }
278
            $pollObject->setVar('question', Request::getString('question', '', 'POST'));
279
            $pollObject->setVar('description', Request::getString('description', '', 'POST'));
280
            $end_time = Request::getString('end_time', '', 'POST'); // (empty($_POST['end_time'])) ? "" : $_POST['end_time'];
281
            if ('' !== $end_time) {
282
                $timezone = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('timezone') : null;
283
                $pollObject->setVar('end_time', userTimeToServerTime(method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime($end_time) : strtotime($end_time), $timezone));
284
            } else {
285
                // if expiration date is not set, set it to 10 days from now
286
                $pollObject->setVar('end_time', time() + (86400 * 10));
287
            }
288
289
            $pollObject->setVar('display', 0);
290
            $pollObject->setVar('weight', Request::getInt('weight', 0, 'POST'));
291
            $pollObject->setVar('multiple', Request::getInt('multiple', 0, 'POST'));
292
            $pollObject->setVar('user_id', Request::getInt('user_id', 0, 'POST'));
293
            if (Request::getInt('notify', 0, 'POST') && $end_time > time()) {
294
                // if notify, set mail status to "not mailed"
295
                $pollObject->setVar('mail_status', POLL_NOTMAILED);
0 ignored issues
show
The constant POLL_NOTMAILED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
296
            } else {
297
                // if not notify, set mail status to already "mailed"
298
                $pollObject->setVar('mail_status', POLL_MAILED);
0 ignored issues
show
The constant POLL_MAILED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
299
            }
300
            $new_poll_id = $pollObject->store();
301
            if (empty($new_poll_id)) {
302
                xoops_error($pollObject->getHtmlErrors);
303
                break;
304
            }
305
            $i            = 0;
306
            $option_color = Request::getArray('option_color', null, 'POST');
307
            $classOption  = $classPoll . 'Option';
308
            foreach ($option_text as $optxt) {
309
                $optxt = trim($optxt);
310
                /** @var XoopspollOption $optionObject */
311
                $optionObject = new $classOption();
312
                if ('' !== $optxt) {
313
                    $optionObject->setVar('option_text', $optxt);
314
                    $optionObject->setVar('option_color', $option_color[$i]);
315
                    $optionObject->setVar('poll_id', $new_poll_id);
316
                    $optionObject->store();
317
                }
318
                ++$i;
319
            }
320
            // clear the template cache so changes take effect immediately
321
            require_once $GLOBALS['xoops']->path('class/template.php');
322
            xoops_template_clear_module_cache($GLOBALS['xoopsModule']->getVar('mid'));
323
            xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
324
325
            // update topic to indicate it has a poll
326
            $topicObject->setVar('topic_haspoll', 1);
327
            $topicObject->setVar('poll_id', $new_poll_id);
328
            $success = $topicHandler->insert($topicObject);
329
            if (!$success) {
330
                xoops_error($topicHandler->getHtmlErrors());
331
            } else {
332
                redirect_header("viewtopic.php?topic_id={$topic_id}", 2, _MD_NEWBB_POLL_DBUPDATED);
333
            }
334
            break;// op: save
335
        }
336
    // no break
337
    case 'update':
338
        // check security token
339
        if (!$GLOBALS['xoopsSecurity']->check()) {
340
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
341
        }
342
        /* make sure there's at least one option */
343
        $option_text   = Request::getString('option_text', '', 'POST');
344
        $option_string = is_array($option_text) ? implode('', $option_text) : $option_text;
345
        $option_string = trim($option_string);
346
        if ('' === $option_string) {
347
            redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
348
        }
349
350
        // new xoopspoll module
351
        if ($pollModuleHandler->getVar('version') >= 140) {
352
            /** @var \XoopspollOptionHandler $xpOptHandler */
353
            $xpOptHandler = Xoopspoll\Helper::getInstance()->getHandler('Option');
354
            /** @var \XoopspollLogHandler $xpLogHandler */
355
            $xpLogHandler = Xoopspoll\Helper::getInstance()->getHandler('Log');
356
            //            $classRequest = ucfirst($GLOBALS['xoopsModuleConfig']["poll_module"]) . "Request";
357
            $classConstants   = ucfirst($GLOBALS['xoopsModuleConfig']['poll_module']) . 'Constants';
358
            $notify           = Request::getInt('notify', $classConstants::NOTIFICATION_ENABLED, 'POST');
359
            $currentTimestamp = time();
360
            //$xuEndTimestamp   = method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime(Request::getString('xu_end_time', null, 'POST'))
361
            //                                                             : strtotime(Request::getString('xu_end_time', null, 'POST'));
362
            $xuEndTimestamp = strtotime(Request::getString('xu_end_time', null, 'POST'));
363
            $endTimestamp   = (!Request::getString('xu_end_time', null, 'POST')) ? ($currentTimestamp + $classConstants::DEFAULT_POLL_DURATION) : userTimeToServerTime($xuEndTimestamp);
364
            //$xuStartTimestamp = method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime(Request::getString('xu_start_time', null, 'POST'))
365
            //                                                             : strtotime(Request::getString('xu_start_time', null, 'POST'));
366
            $xuStartTimestamp = strtotime(Request::getString('xu_start_time', null, 'POST'));
367
            $startTimestamp   = (!Request::getString('xu_start_time', null, 'POST')) ? ($endTimestamp - $classConstants::DEFAULT_POLL_DURATION) : userTimeToServerTime($xuStartTimestamp);
368
369
            //  don't allow changing start time if there are votes in the log
370
            if (($startTimestamp < $pollObject->getVar('start_time'))
371
                && ($xpLogHandler->getTotalVotesByPollId($poll_id) > 0)) {
372
                $startTimestamp = $pollObject->getVar('start_time'); //don't change start time
373
            }
374
375
            $poll_vars = [
376
                'user_id'     => Request::getInt('user_id', $GLOBALS['xoopsUser']->uid(), 'POST'),
377
                'question'    => Request::getString('question', null, 'POST'),
378
                'description' => Request::getText('description', null, 'POST'),
379
                'mail_status' => ($classConstants::NOTIFICATION_ENABLED == $notify) ? $classConstants::POLL_NOT_MAILED : $classConstants::POLL_MAILED,
380
                'mail_voter'  => Request::getInt('mail_voter', $classConstants::NOT_MAIL_POLL_TO_VOTER, 'POST'),
381
                'start_time'  => $startTimestamp,
382
                'end_time'    => $endTimestamp,
383
                'display'     => Request::getInt('display', $classConstants::DO_NOT_DISPLAY_POLL_IN_BLOCK, 'POST'),
384
                'visibility'  => Request::getInt('visibility', $classConstants::HIDE_NEVER, 'POST'),
385
                'weight'      => Request::getInt('weight', $classConstants::DEFAULT_WEIGHT, 'POST'),
386
                'multiple'    => Request::getInt('multiple', $classConstants::NOT_MULTIPLE_SELECT_POLL, 'POST'),
387
                'multilimit'  => Request::getInt('multilimit', $classConstants::MULTIPLE_SELECT_LIMITLESS, 'POST'),
388
                'anonymous'   => Request::getInt('anonymous', $classConstants::ANONYMOUS_VOTING_DISALLOWED, 'POST')
389
            ];
390
            $pollObject->setVars($poll_vars);
391
            $poll_id = $xpPollHandler->insert($pollObject);
392
            if (!$poll_id) {
393
                $err = $pollObject->getHtmlErrors();
394
                exit($err);
395
            }
396
397
            // now get the options
398
            $optionIdArray    = Request::getArray('option_id', [], 'POST');
399
            $optionIdArray    = array_map('intval', $optionIdArray);
400
            $optionTextArray  = Request::getArray('option_text', [], 'POST');
401
            $optionColorArray = Request::getArray('option_color', [], 'POST');
402
403
            foreach ($optionIdArray as $key => $oId) {
404
                if (!empty($oId) && ($optionObject = $xpOptHandler->get($oId))) {
405
                    // existing option object so need to update it
406
                    $optionTextArray[$key] = trim($optionTextArray[$key]);
407
                    if ('' === $optionTextArray[$key]) {
408
                        // want to delete this option
409
                        if (false !== $xpOptHandler->delete($optionObject)) {
410
                            // now remove it from the log
411
                            $xpLogHandler->deleteByOptionId($optionObject->getVar('option_id'));
412
                            //update vote count in poll
413
                            $xpPollHandler->updateCount($pollObject);
414
                        } else {
415
                            xoops_error($xpLogHandler->getHtmlErrors());
416
                            break;
417
                        }
418
                    } else {
419
                        $optionObject->setVar('option_text', $optionTextArray[$key]);
420
                        $optionObject->setVar('option_color', $optionColorArray[$key]);
421
                        $optionObject->setVar('poll_id', $poll_id);
422
                        $xpOptHandler->insert($optionObject);
423
                    }
424
                } else {
425
                    // new option object
426
                    $optionObject          = $xpOptHandler->create();
427
                    $optionTextArray[$key] = trim($optionTextArray[$key]);
428
                    if ('' !== $optionTextArray[$key]) { // ignore if text is empty
429
                        $optionObject->setVar('option_text', $optionTextArray[$key]);
430
                        $optionObject->setVar('option_color', $optionColorArray[$key]);
431
                        $optionObject->setVar('poll_id', $poll_id);
432
                        $xpOptHandler->insert($optionObject);
433
                    }
434
                    unset($optionObject);
435
                }
436
            }
437
            // old xoopspoll or umfrage or any clone from them
438
        } else {
439
            $pollObject->setVar('question', Request::getString('question', '', 'POST'));
440
            $pollObject->setVar('description', Request::getString('description', '', 'POST'));
441
442
            $end_time = Request::getString('end_time', '', 'POST');
443
            if ('' !== $end_time) {
444
                $timezone = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('timezone') : null;
445
                $pollObject->setVar('end_time', userTimeToServerTime(method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime($end_time) : strtotime($end_time), $timezone));
446
            }
447
            $pollObject->setVar('display', 0);
448
            $pollObject->setVar('weight', Request::getInt('weight', 0, 'POST'));
449
            $pollObject->setVar('multiple', Request::getInt('multiple', 0, 'POST'));
450
            $pollObject->setVar('user_id', Request::getInt('user_id', 0, 'POST'));
451
            if (Request::getInt('notify', 0, 'POST') && $end_time > time()) {
452
                // if notify, set mail status to "not mailed"
453
                $pollObject->setVar('mail_status', POLL_NOTMAILED);
454
            } else {
455
                // if not notify, set mail status to already "mailed"
456
                $pollObject->setVar('mail_status', POLL_MAILED);
457
            }
458
459
            if (!$pollObject->store()) {
460
                xoops_error($pollObject->getHtmlErrors);
461
                break;
462
            }
463
            $i            = 0;
464
            $option_id    = Request::getArray('option_id', null, 'POST');
465
            $option_color = Request::getArray('option_color', null, 'POST');
466
            $classOption  = $classPoll . 'Option';
467
            $classLog     = $classPoll . 'Log';
468
            foreach ($option_id as $opid) {
469
                $optionObject    = new $classOption($opid);
470
                $option_text[$i] = trim($option_text[$i]);
471
                if ('' !== $option_text[$i]) {
472
                    $optionObject->setVar('option_text', $option_text[$i]);
473
                    $optionObject->setVar('option_color', $option_color[$i]);
474
                    $optionObject->store();
475
                } else {
476
                    if (false !== $optionObject->delete()) {
477
                        $classLog::deleteByOptionId($option->getVar('option_id'));
478
                    }
479
                }
480
                ++$i;
481
            }
482
            $pollObject->updateCount();
483
        }
484
        // clear the template cache so changes take effect immediately
485
        require_once $GLOBALS['xoops']->path('class/template.php');
486
        xoops_template_clear_module_cache($GLOBALS['xoopsModule']->getVar('mid'));
487
        xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
488
489
        // update topic to indicate it has a poll
490
        $topicObject->setVar('topic_haspoll', 1);
491
        $topicObject->setVar('poll_id', $pollObject->getVar('poll_id'));
492
        $success = $topicHandler->insert($topicObject);
493
        if (!$success) {
494
            xoops_error($topicHandler->getHtmlErrors());
495
        } else {
496
            redirect_header("viewtopic.php?topic_id={$topic_id}", 2, _MD_NEWBB_POLL_DBUPDATED);
497
        }
498
        break;// op: save | update
499
500
    case 'addmore':
501
        $question = $pollObject->getVar('question');
502
        unset($pollObject);
503
        $poll_form = new \XoopsThemeForm(_MD_NEWBB_POLL_ADDMORE, 'poll_form', 'polls.php', 'post', true);
504
        $poll_form->addElement(new \XoopsFormLabel(_MD_NEWBB_POLL_POLLQUESTION, $question));
505
        // new xoopspoll module
506
        if ($pollModuleHandler->getVar('version') >= 140) {
507
            $xpOptHandler = Xoopspoll\Helper::getInstance()->getHandler('Option');
508
            $option_tray  = $xpOptHandler->renderOptionFormTray($poll_id);
509
        // old xoopspoll or umfrage or any clone from them
510
        } else {
511
            $option_tray    = new \XoopsFormElementTray(_MD_NEWBB_POLL_POLLOPTIONS, '');
512
            $barcolor_array = \XoopsLists::getImgListAsArray($GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/assets/images/colorbars/'));
513
            for ($i = 0; $i < 10; ++$i) {
514
                $current_bar = ('blank.gif' !== current($barcolor_array)) ? current($barcolor_array) : next($barcolor_array);
515
                $option_text = new \XoopsFormText('', 'option_text[]', 50, 255);
516
                $option_tray->addElement($option_text);
517
                $color_select = new \XoopsFormSelect('', "option_color[{$i}]", $current_bar);
518
                $color_select->addOptionArray($barcolor_array);
519
                $color_select->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[{$i}]\", \"modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars\", \"\", \"" . XOOPS_URL . "\")'");
520
                $color_label = new \XoopsFormLabel('', "<img src='"
521
                                                      . $GLOBALS['xoops']->url("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars/{$current_bar}")
522
                                                      . "' name='option_color_image[{$i}]' id='option_color_image[{$i}]' class='alignbottom' width='30' height='15' alt='' /><br>");
523
                $option_tray->addElement($color_select);
524
                $option_tray->addElement($color_label);
525
                unset($color_select, $color_label, $option_text);
526
                if (!next($barcolor_array)) {
527
                    reset($barcolor_array);
528
                }
529
            }
530
        }
531
        $poll_form->addElement($option_tray);
532
        $poll_form->addElement(new \XoopsFormButtonTray('poll_submit', _SUBMIT, 'submit'));
533
        $poll_form->addElement(new \XoopsFormHidden('op', 'savemore'));
534
        $poll_form->addElement(new \XoopsFormHidden('topic_id', $topic_id));
535
        $poll_form->addElement(new \XoopsFormHidden('poll_id', $poll_id));
536
537
        echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
538
        $poll_form->display();
539
        break;
540
541
    case 'savemore':
542
        // check security token
543
        if (!$GLOBALS['xoopsSecurity']->check()) {
544
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
545
        }
546
547
        $option_text   = Request::getString('option_text', '', 'POST');
548
        $option_string = is_array($option_text) ? implode('', $option_text) : $option_text;
549
        $option_string = trim($option_string);
550
        if ('' === $option_string) {
551
            // irmtfan - issue with javascript:history.go(-1)
552
            redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
553
        }
554
        $i            = 0;
555
        $option_color = Request::getArray('option_color', null, 'POST');
556
        foreach ($option_text as $optxt) {
557
            $optxt = trim($optxt);
558
            if ('' !== $optxt) {
559
                // new xoopspoll module
560
                if ($pollModuleHandler->getVar('version') >= 140) {
561
                    $xpOptHandler = Xoopspoll\Helper::getInstance()->getHandler('Option');
562
                    $optionObject = $xpOptHandler->create();
563
                    $optionObject->setVar('option_text', $optxt);
564
                    $optionObject->setVar('poll_id', $poll_id);
565
                    $optionObject->setVar('option_color', $option_color[$i]);
566
                    $xpOptHandler->insert($optionObject);
567
                // old xoopspoll or umfrage or any clone from them
568
                } else {
569
                    $classOption  = $classPoll . 'Option';
570
                    $optionObject = new $classOption();
571
                    $optionObject->setVar('option_text', $optxt);
572
                    $optionObject->setVar('poll_id', $poll_id);
573
                    $optionObject->setVar('option_color', $option_color[$i]);
574
                    $optionObject->store();
575
                }
576
                unset($optionObject);
577
            }
578
            ++$i;
579
        }
580
        require_once $GLOBALS['xoops']->path('class/template.php');
581
        xoops_template_clear_module_cache($GLOBALS['xoopsModule']->getVar('mid'));
582
        xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
583
        redirect_header("polls.php?op=edit&amp;poll_id={$poll_id}&amp;topic_id={$topic_id}", 2, _MD_NEWBB_POLL_DBUPDATED);
584
        break;
585
586
    case 'delete':
587
        echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
588
        xoops_confirm(['op' => 'delete_ok', 'topic_id' => $topic_id, 'poll_id' => $poll_id], 'polls.php', sprintf(_MD_NEWBB_POLL_RUSUREDEL, $pollObject->getVar('question')));
589
        break;
590
591
    case 'delete_ok':
592
        // check security token
593
        if (!$GLOBALS['xoopsSecurity']->check()) {
594
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
595
        }
596
        //try and delete the poll
597
        // new xoopspoll module
598
        if ($pollModuleHandler->getVar('version') >= 140) {
599
            $status = $xpPollHandler->delete($pollObject);
600
            if (false !== $status) {
601
                $xpOptHandler = Xoopspoll\Helper::getInstance()->getHandler('Option');
602
                $xpLogHandler = Xoopspoll\Helper::getInstance()->getHandler('Log');
603
                $xpOptHandler->deleteByPollId($poll_id);
604
                $xpLogHandler->deleteByPollId($poll_id);
605
            } else {
606
                $msg = $xpPollHandler->getHtmlErrors();
607
            }
608
            // old xoopspoll or umfrage or any clone from them
609
        } else {
610
            $status      = $pollObject->delete();
611
            $classOption = $classPoll . 'Option';
612
            $classLog    = $classPoll . 'Log';
613
            if (false !== $status) {
614
                $classOption::deleteByPollId($poll_id);
615
                $classLog::deleteByPollId($poll_id);
616
            } else {
617
                $msg = $pollObject->getHtmlErrors();
618
            }
619
        }
620
        if (false !== $status) {
621
            require_once $GLOBALS['xoops']->path('class/template.php');
622
            xoops_template_clear_module_cache($xoopsModule->getVar('mid'));
623
            xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
624
            // delete comments for this poll
625
            xoops_comment_delete($xoopsModule->getVar('mid'), $poll_id);
626
627
            $topicObject->setVar('votes', 0); // not sure why we want to clear votes too... but I left it alone
628
            $topicObject->setVar('topic_haspoll', 0);
629
            $topicObject->setVar('poll_id', 0);
630
            $success = $topicHandler->insert($topicObject);
631
            if (!$success) {
632
                xoops_error($topicHandler->getHtmlErrors());
633
                break;
634
            }
635
        } else {
636
            xoops_error($msg);
637
            break;
638
        }
639
        redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?topic_id={$topic_id}", 1, _MD_NEWBB_POLL_DBUPDATED);
640
        break;
641
642
    case 'restart':
643
        // new xoopspoll module
644
        if ($pollModuleHandler->getVar('version') >= 140) {
645
            $classConstants        = ucfirst($GLOBALS['xoopsModuleConfig']['poll_module']) . 'Constants';
646
            $default_poll_duration = $classConstants::DEFAULT_POLL_DURATION;
647
        // old xoopspoll or umfrage or any clone from them
648
        } else {
649
            $default_poll_duration = (86400 * 10);
650
        }
651
        $poll_form   = new \XoopsThemeForm(_MD_NEWBB_POLL_RESTARTPOLL, 'poll_form', 'polls.php', 'post', true);
652
        $expire_text = new \XoopsFormText(
653
            _MD_NEWBB_POLL_EXPIRATION . '<br><small>' . _MD_NEWBB_POLL_FORMAT . '<br>' . sprintf(_MD_NEWBB_POLL_CURRENTTIME, formatTimestamp(time(), 'Y-m-d H:i:s')) . '<br>' . sprintf(
654
            _MD_NEWBB_POLL_EXPIREDAT,
655
                                                                                                                                                                                                                     formatTimestamp($pollObject->getVar('end_time'), 'Y-m-d H:i:s')
656
        ) . '</small>',
657
                                         'end_time',
658
            20,
659
            19,
660
            formatTimestamp(time() + $default_poll_duration, 'Y-m-d H:i:s')
661
        );
662
        $poll_form->addElement($expire_text);
663
        $poll_form->addElement(new \XoopsFormRadioYN(_MD_NEWBB_POLL_NOTIFY, 'notify', 1));
664
        $poll_form->addElement(new \XoopsFormRadioYN(_MD_NEWBB_POLL_RESET, 'reset', 0));
665
        $poll_form->addElement(new \XoopsFormHidden('op', 'restart_ok'));
666
        $poll_form->addElement(new \XoopsFormHidden('topic_id', $topic_id));
667
        $poll_form->addElement(new \XoopsFormHidden('poll_id', $poll_id));
668
        $poll_form->addElement(new \XoopsFormButton('', 'poll_submit', _MD_NEWBB_POLL_RESTART, 'submit'));
669
670
        echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
671
        $poll_form->display();
672
        break;
673
674
    case 'restart_ok':
675
        // check security token
676
        if (!$GLOBALS['xoopsSecurity']->check()) {
677
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
678
        }
679
680
        // new xoopspoll module
681
        if ($pollModuleHandler->getVar('version') >= 140) {
682
            $classConstants        = ucfirst($GLOBALS['xoopsModuleConfig']['poll_module']) . 'Constants';
683
            $default_poll_duration = $classConstants::DEFAULT_POLL_DURATION;
684
            $poll_not_mailed       = $classConstants::POLL_NOT_MAILED;
685
            $poll_mailed           = $classConstants::POLL_MAILED;
686
        // old xoopspoll or umfrage or any clone from them
687
        } else {
688
            $default_poll_duration = (86400 * 10);
689
            $poll_not_mailed       = POLL_NOTMAILED;
690
            $poll_mailed           = POLL_MAILED;
691
        }
692
693
        $end_time = !Request::getInt('end_time', 0, 'POST');
694
        if (0 !== $end_time) {
695
            $timezone = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('timezone') : null;
696
            $pollObject->setVar('end_time', userTimeToServerTime(method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime($end_time) : strtotime($end_time), $timezone));
697
        } else {
698
            $pollObject->setVar('end_time', time() + $default_poll_duration);
699
        }
700
701
        $isNotify = Request::getInt('notify', 0, 'POST');
702
        if (!empty($isNotify) && ($end_time > time())) {
703
            // if notify, set mail status to "not mailed"
704
            $pollObject->setVar('mail_status', $poll_not_mailed);
705
        } else {
706
            // if not notify, set mail status to already "mailed"
707
            $pollObject->setVar('mail_status', $poll_mailed);
708
        }
709
710
        // new xoopspoll module
711
        if ($pollModuleHandler->getVar('version') >= 140) {
712
            if (!$xpPollHandler->insert($pollObject)) {  // update the poll
713
                xoops_error($pollObject->getHtmlErrors());
714
                exit();
715
            }
716
            if (Request::getInt('reset', 0, 'POST')) { // reset all vote/voter counters
717
                /** @var \XoopspollOptionHandler $xpOptHandler */
718
                $xpOptHandler = Xoopspoll\Helper::getInstance()->getHandler('Option');
719
                /** @var \XoopspollLogHandler $xpLogHandler */
720
                $xpLogHandler = Xoopspoll\Helper::getInstance()->getHandler('Log');
721
                $xpLogHandler->deleteByPollId($poll_id);
722
                $xpOptHandler->resetCountByPollId($poll_id);
723
                $xpPollHandler->updateCount($pollObject);
724
            }
725
            // old xoopspoll or umfrage or any clone from them
726
        } else {
727
            if (!$pollObject->store()) { // update the poll
728
                xoops_error($pollObject->getHtmlErrors());
729
                exit();
730
            }
731
            if (Request::getInt('reset', 0, 'POST')) { // reset all logs
732
                $classOption = $classPoll . 'Option';
733
                $classLog    = $classPoll . 'Log';
734
                $classLog::deleteByPollId($poll_id);
735
                $classOption::resetCountByPollId($poll_id);
736
                $pollObject->updateCount();
737
            }
738
        }
739
        require_once $GLOBALS['xoops']->path('class/template.php');
740
        xoops_template_clear_module_cache($xoopsModule->getVar('mid'));
741
        xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
742
        redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?topic_id={$topic_id}", 1, _MD_NEWBB_POLL_DBUPDATED);
743
        break;
744
745
    case 'log':
746
        // new xoopspoll module
747
        if ($pollModuleHandler->getVar('version') >= 140) {
748
            redirect_header($GLOBALS['xoops']->url("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/admin/main.php?op=log&amp;poll_id={$poll_id}"), 2, _MD_NEWBB_POLL_VIEWLOG);
749
        // old xoopspoll or umfrage or any clone from them
750
        } else {
751
            redirect_header($GLOBALS['xoops']->url("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/admin/index.php?op=log&amp;poll_id={$poll_id}"), 2, _MD_NEWBB_POLL_VIEWLOG);
752
        }
753
        break;
754
} // switch
755
756
// irmtfan move to footer.php
757
require_once __DIR__ . '/footer.php';
758
include $GLOBALS['xoops']->path('footer.php');
759