Completed
Push — master ( dd728e...3face7 )
by Michael
11s
created

polls.php (22 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 (http://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
21
// rewrite by irmtfan and zyspec to accept xoopspoll 1.4 and all old xoopspoll and umfrage versions and all clones
22
23
include_once __DIR__ . '/header.php';
24
include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
25
include_once $GLOBALS['xoops']->path('class/xoopslists.php');
26
include_once $GLOBALS['xoops']->path('class/xoopsblock.php');
27
xoops_load('XoopsLocal');
28
$op      = 'add';
29
$goodOps = [
30
    'add',
31
    'save',
32
    'edit',
33
    'update',
34
    'addmore',
35
    'savemore',
36
    'delete',
37
    'delete_ok',
38
    'restart',
39
    'restart_ok',
40
    'log'
41
];
42
$op      = isset($_REQUEST['op']) ? $_REQUEST['op'] : 'add';
43
$op      = (!in_array($op, $goodOps)) ? 'add' : $op;
44
45
$poll_id  = Request::getInt('poll_id', Request::getInt('poll_id', 0, 'GET'), 'POST');
46
$topic_id = Request::getInt('topic_id', Request::getInt('topic_id', 0, 'GET'), 'POST');
47
48
// deal with permissions
49
/** @var \NewbbTopicHandler $topicHandler */
50
$topicHandler = xoops_getModuleHandler('topic', 'newbb');
51
$topicObject    = $topicHandler->get($topic_id);
52
// topic exist
53
if (is_object($topicObject)) {
54
    $forum_id = $topicObject->getVar('forum_id');
55
} else {
56
    redirect_header(XOOPS_URL . '/index.php', 2, _MD_NEWBB_POLLMODULE_ERROR . ': ' . _MD_NEWBB_FORUMNOEXIST);
57
}
58
// forum access permission
59
/** @var \NewbbForumHandler $forumHandler */
60
$forumHandler = xoops_getModuleHandler('forum', 'newbb');
61
$forumObject    = $forumHandler->get($forum_id);
62
if (!$forumHandler->getPermission($forumObject)) {
63
    redirect_header(XOOPS_URL . '/index.php', 2, _MD_NEWBB_NORIGHTTOACCESS);
64
}
65
// topic view permission
66
if (!$topicHandler->getPermission($forumObject, $topicObject->getVar('topic_status'), 'view')) {
67
    redirect_header('viewforum.php?forum=' . $forum_id, 2, _MD_NEWBB_NORIGHTTOVIEW);
68
}
69
// poll module
70
$pollModuleHandler = $moduleHandler->getByDirname($GLOBALS['xoopsModuleConfig']['poll_module']);
71
if (is_object($pollModuleHandler) && $pollModuleHandler->getVar('isactive')) {
72
    // new xoopspoll module
73
    if ($pollModuleHandler->getVar('version') >= 140) {
74
        xoops_load('constants', $GLOBALS['xoopsModuleConfig']['poll_module']);
75
        xoops_load('pollUtility', $GLOBALS['xoopsModuleConfig']['poll_module']);
76
        xoops_load('request', $GLOBALS['xoopsModuleConfig']['poll_module']);
77
        xoops_loadLanguage('admin', $GLOBALS['xoopsModuleConfig']['poll_module']);
78
        /** @var \XoopspollPollHandler $xpPollHandler */
79
        $xpPollHandler = xoops_getModuleHandler('poll', $GLOBALS['xoopsModuleConfig']['poll_module']);
80
        /** @var \XoopsPoll $pollObject */
81
        $pollObject = $xpPollHandler->get($poll_id); // will create poll if poll_id = 0 exist
82
        // old xoopspoll or umfrage or any clone from them
83 View Code Duplication
    } else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
        include $GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/include/constants.php');
85
        $classPoll = $topicObject->loadOldPoll();
86
        $pollObject  = new $classPoll($poll_id); // will create poll if poll_id = 0 exist
87
    }
88
} else {
89
    // irmtfan - issue with javascript:history.go(-1)
90
    redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_POLLMODULE_ERROR);
91
}
92
// include header
93
include $GLOBALS['xoops']->path('header.php');
94
95
// no admin user permission
96
if (is_object($GLOBALS['xoopsUser']) && !newbbIsAdmin($forumObject)) {
97
    $perm = false;
98
    if ($topicHandler->getPermission($forumObject, $topicObject->getVar('topic_status'), 'addpoll')) {
99
        if (('add' === $op || 'save' === $op || 'update' === $op) && !$topicObject->getVar('topic_haspoll')
100
            && ($GLOBALS['xoopsUser']->getVar('uid') == $topicObject->getVar('topic_poster'))) {
101
            $perm = true;
102
        } elseif (!empty($poll_id) && ($GLOBALS['xoopsUser']->getVar('uid') == $pollObject->getVar('user_id'))) {
103
            $perm = true;
104
        }
105
    }
106
    if (!$perm) {
107
        redirect_header("viewtopic.php?topic_id={$topic_id}", 2, _NOPERM);
108
    }
109
}
110
switch ($op) {
111
    case 'add':
112
        // new xoopspoll module
113
        if ($pollModuleHandler->getVar('version') >= 140) {
114
            echo '<h4>' . _MD_NEWBB_POLL_CREATNEWPOLL . "</h4>\n";
115
            $pollObject->renderForm(Request::getString('PHP_SELF', '', 'SERVER'), 'post', ['topic_id' => $topic_id]);
116
            // old xoopspoll or umfrage or any clone from them
117
        } else {
118
            $classOption  = $classPoll . 'Option';
119
            $poll_form    = new XoopsThemeForm(_MD_NEWBB_POLL_CREATNEWPOLL, 'poll_form', 'polls.php', 'post', true);
120
            $author_label = new XoopsFormLabel(_MD_NEWBB_POLL_AUTHOR, is_object($GLOBALS['xoopsUser']) ? ("<a href='"
121
                                                                                                          . XOOPS_URL
122
                                                                                                          . '/userinfo.php?uid='
123
                                                                                                          . $GLOBALS['xoopsUser']->getVar('uid')
124
                                                                                                          . "'>"
125
                                                                                                          . newbbGetUnameFromId($GLOBALS['xoopsUser']->getVar('uid'), $GLOBALS['xoopsModuleConfig']['show_realname'])
126
                                                                                                          . '</a>') : $GLOBALS['xoopsConfig']['anonymous']);
127
            $poll_form->addElement($author_label);
128
            $question_text = new XoopsFormText(_MD_NEWBB_POLL_POLLQUESTION, 'question', 50, 255);
129
            $poll_form->addElement($question_text);
130
            $desc_tarea = new XoopsFormTextarea(_MD_NEWBB_POLL_POLLDESC, 'description');
131
            $poll_form->addElement($desc_tarea);
132
            $currenttime = formatTimestamp(time(), 'Y-m-d H:i:s');
133
            $endtime     = formatTimestamp(time() + 604800, 'Y-m-d H:i:s');
134
            $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);
135
            $poll_form->addElement($expire_text);
136
137
            $weight_text = new XoopsFormText(_MD_NEWBB_POLL_DISPLAYORDER, 'weight', 6, 5, 0);
138
            $poll_form->addElement($weight_text);
139
140
            $multi_yn = new XoopsFormRadioYN(_MD_NEWBB_POLL_ALLOWMULTI, 'multiple', 0);
141
            $poll_form->addElement($multi_yn);
142
143
            $notify_yn = new XoopsFormRadioYN(_MD_NEWBB_POLL_NOTIFY, 'notify', 1);
144
            $poll_form->addElement($notify_yn);
145
146
            $option_tray    = new XoopsFormElementTray(_MD_NEWBB_POLL_POLLOPTIONS, '');
147
            $barcolor_array = XoopsLists::getImgListAsArray($GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/assets/images/colorbars/'));
148
            for ($i = 0; $i < 10; ++$i) {
149
                $current_bar = (current($barcolor_array) !== 'blank.gif') ? current($barcolor_array) : next($barcolor_array);
150
                $option_text = new XoopsFormText('', 'option_text[]', 50, 255);
151
                $option_tray->addElement($option_text);
152
                $color_select = new XoopsFormSelect('', "option_color[{$i}]", $current_bar);
153
                $color_select->addOptionArray($barcolor_array);
154
                $color_select->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[{$i}]\", \"modules/" . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/assets/images/colorbars", "", "' . XOOPS_URL . "\")'");
155
                $color_label = new XoopsFormLabel('', "<img src='"
156
                                                      . XOOPS_URL
157
                                                      . '/modules/'
158
                                                      . $GLOBALS['xoopsModuleConfig']['poll_module']
159
                                                      . '/assets/images/colorbars/'
160
                                                      . $current_bar
161
                                                      . "' name='option_color_image[{$i}]' id='option_color_image[{$i}]' width='30' align='bottom' height='15' alt='' /><br>");
162
                $option_tray->addElement($color_select);
163
                $option_tray->addElement($color_label);
164
                if (!next($barcolor_array)) {
165
                    reset($barcolor_array);
166
                }
167
                unset($color_select, $color_label);
168
            }
169
            $poll_form->addElement($option_tray);
170
171
            $poll_form->addElement(new XoopsFormHidden('op', 'save'));
172
            $poll_form->addElement(new XoopsFormHidden('topic_id', $topic_id));
173
            $poll_form->addElement(new XoopsFormHidden('poll_id', $poll_id));
174
            $poll_form->addElement(new XoopsFormHidden('user_id', is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0));
175
            $poll_form->addElement(new XoopsFormButtonTray('poll_submit', _SUBMIT, 'submit'));
176
            echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . '</h4>';
177
            $poll_form->display();
178
        }
179
        break; // op: add
180
181
    case 'edit':
182
        // new xoopspoll module
183
        if ($pollModuleHandler->getVar('version') >= 140) {
184
            echo '<h4>' . _MD_NEWBB_POLL_EDITPOLL . "</h4>\n";
185
            $pollObject->renderForm(Request::getString('PHP_SELF', '', 'SERVER'), 'post', ['topic_id' => $topic_id]);
186
            // old xoopspoll or umfrage or any clone from them
187
        } else {
188
            $classOption  = $classPoll . 'Option';
189
            $poll_form    = new XoopsThemeForm(_MD_NEWBB_POLL_EDITPOLL, 'poll_form', 'polls.php', 'post', true);
190
            $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>');
191
            $poll_form->addElement($author_label);
192
            $question_text = new XoopsFormText(_MD_NEWBB_POLL_POLLQUESTION, 'question', 50, 255, $pollObject->getVar('question', 'E'));
193
            $poll_form->addElement($question_text);
194
            $desc_tarea = new XoopsFormTextarea(_MD_NEWBB_POLL_POLLDESC, 'description', $pollObject->getVar('description', 'E'));
195
            $poll_form->addElement($desc_tarea);
196
            $date = formatTimestamp($pollObject->getVar('end_time'), 'Y-m-d H:i:s'); // important "Y-m-d H:i:s" use in jdf function
197
            if (!$pollObject->hasExpired()) {
198
                $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);
199
                $poll_form->addElement($expire_text);
200
            } else {
201
                // irmtfan full URL - add topic_id
202
                $restart_label = new XoopsFormLabel(_MD_NEWBB_POLL_EXPIRATION,
203
                                                    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>');
204
                $poll_form->addElement($restart_label);
205
            }
206
            $weight_text = new XoopsFormText(_MD_NEWBB_POLL_DISPLAYORDER, 'weight', 6, 5, $pollObject->getVar('weight'));
207
            $poll_form->addElement($weight_text);
208
            $multi_yn = new XoopsFormRadioYN(_MD_NEWBB_POLL_ALLOWMULTI, 'multiple', $pollObject->getVar('multiple'));
209
            $poll_form->addElement($multi_yn);
210
            $options_arr  =& $classOption::getAllByPollId($poll_id);
211
            $notify_value = 1;
212
            if (0 !== $pollObject->getVar('mail_status')) {
213
                $notify_value = 0;
214
            }
215
            $notify_yn = new XoopsFormRadioYN(_MD_NEWBB_POLL_NOTIFY, 'notify', $notify_value);
216
            $poll_form->addElement($notify_yn);
217
            $option_tray    = new XoopsFormElementTray(_MD_NEWBB_POLL_POLLOPTIONS, '');
218
            $barcolor_array = XoopsLists::getImgListAsArray($GLOBALS['xoops']->path("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars/"));
219
            $i              = 0;
220
            foreach ($options_arr as $option) {
221
                /** @var \XoopsPoll $option */
222
                $option_tray->addElement(new XoopsFormText('', 'option_text[]', 50, 255, $option->getVar('option_text')));
223
                $option_tray->addElement(new XoopsFormHidden('option_id[]', $option->getVar('option_id')));
224
                $color_select = new XoopsFormSelect('', 'option_color[{$i}]', $option->getVar('option_color'));
225
                $color_select->addOptionArray($barcolor_array);
226
                $color_select->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[" . $i . "]\", \"modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars\", \"\", \"" . XOOPS_URL . "\")'");
227
                $color_label = new XoopsFormLabel('', "<img src='"
228
                                                      . $GLOBALS['xoops']->url("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars/" . $option->getVar('option_color', 'E'))
229
                                                      . "' name='option_color_image[{$i}]' id='option_color_image[{$i}]' class='alignbottom' width='30' height='15' alt='' /><br>");
230
                $option_tray->addElement($color_select);
231
                $option_tray->addElement($color_label);
232
                unset($color_select, $color_label);
233
                ++$i;
234
            }
235
            // irmtfan full URL
236
            $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>');
237
            $option_tray->addElement($more_label);
238
            $poll_form->addElement($option_tray);
239
            $poll_form->addElement(new XoopsFormHidden('op', 'update'));
240
            $poll_form->addElement(new XoopsFormHidden('topic_id', $topic_id));
241
            $poll_form->addElement(new XoopsFormHidden('poll_id', $poll_id));
242
            $poll_form->addElement(new XoopsFormButtonTray('poll_submit', _SUBMIT, 'submit'));
243
244
            echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
245
            $poll_form->display();
246
        }
247
        break; // op: edit
248
249
    case 'save':
0 ignored issues
show
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
250
        // old xoopspoll or umfrage or any clone from them
251
        if ($pollModuleHandler->getVar('version') < 140) {
252
            // check security token
253 View Code Duplication
            if (!$GLOBALS['xoopsSecurity']->check()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
                redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
255
            }
256
            /*
257
             * The option check should be done before submitting
258
             */
259
            $option_empty = true;
260
            if (!Request::getString('option_text', '', 'POST')) {
261
                // irmtfan - issue with javascript:history.go(-1)
262
                redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
263
            }
264
            $option_text = Request::getArray('option_text', '', 'POST');
265
            foreach ($option_text as $optxt) {
266
                if (trim($optxt) !== '') {
267
                    $option_empty = false;
268
                    break;
269
                }
270
            }
271 View Code Duplication
            if ($option_empty) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
272
                // irmtfan - issue with javascript:history.go(-1)
273
                redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
274
            }
275
            $pollObject->setVar('question', Request::getString('question', '', 'POST'));
276
            $pollObject->setVar('description', Request::getString('description', '', 'POST'));
277
            $end_time = Request::getString('end_time', '', 'POST'); // (empty($_POST['end_time'])) ? "" : $_POST['end_time'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
278 View Code Duplication
            if ('' !== $end_time) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
279
                $timezone = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('timezone') : null;
280
                $pollObject->setVar('end_time', userTimeToServerTime(method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime($end_time) : strtotime($end_time), $timezone));
281
            } else {
282
                // if expiration date is not set, set it to 10 days from now
283
                $pollObject->setVar('end_time', time() + (86400 * 10));
284
            }
285
286
            $pollObject->setVar('display', 0);
287
            $pollObject->setVar('weight', Request::getInt('weight', 0, 'POST'));
288
            $pollObject->setVar('multiple', Request::getInt('multiple', 0, 'POST'));
289
            $pollObject->setVar('user_id', Request::getInt('user_id', 0, 'POST'));
290 View Code Duplication
            if (Request::getInt('notify', 0, 'POST') && $end_time > time()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
291
                // if notify, set mail status to "not mailed"
292
                $pollObject->setVar('mail_status', POLL_NOTMAILED);
293
            } else {
294
                // if not notify, set mail status to already "mailed"
295
                $pollObject->setVar('mail_status', POLL_MAILED);
296
            }
297
            $new_poll_id = $pollObject->store();
298
            if (empty($new_poll_id)) {
299
                xoops_error($pollObject->getHtmlErrors);
300
                break;
301
            }
302
            $i            = 0;
303
            $option_color = Request::getArray('option_color', null, 'POST');
304
            $classOption  = $classPoll . 'Option';
305
            foreach ($option_text as $optxt) {
306
                $optxt = trim($optxt);
307
                /** @var XoopspollOption $optionObject */
308
                $optionObject = new $classOption();
309
                if ($optxt !== '') {
310
                    $optionObject->setVar('option_text', $optxt);
311
                    $optionObject->setVar('option_color', $option_color[$i]);
312
                    $optionObject->setVar('poll_id', $new_poll_id);
313
                    $optionObject->store();
314
                }
315
                ++$i;
316
            }
317
            // clear the template cache so changes take effect immediately
318
            include_once $GLOBALS['xoops']->path('class/template.php');
319
            xoops_template_clear_module_cache($GLOBALS['xoopsModule']->getVar('mid'));
320
            xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
321
322
            // update topic to indicate it has a poll
323
            $topicObject->setVar('topic_haspoll', 1);
324
            $topicObject->setVar('poll_id', $new_poll_id);
325
            $success = $topicHandler->insert($topicObject);
326
            if (!$success) {
327
                xoops_error($topicHandler->getHtmlErrors());
328
            } else {
329
                redirect_header("viewtopic.php?topic_id={$topic_id}", 2, _MD_NEWBB_POLL_DBUPDATED);
330
            }
331
            break;// op: save
332
        }
333
    case 'update':
334
        // check security token
335 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
336
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
337
        }
338
        /* make sure there's at least one option */
339
        $option_text   = Request::getString('option_text', '', 'POST');
340
        $option_string = is_array($option_text) ? implode('', $option_text) : $option_text;
341
        $option_string = trim($option_string);
342 View Code Duplication
        if ('' === $option_string) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
343
            redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
344
        }
345
346
        // new xoopspoll module
347
        if ($pollModuleHandler->getVar('version') >= 140) {
348
            /** @var \XoopspollOptionHandler $xpOptHandler */
349
            $xpOptHandler = xoops_getModuleHandler('option', $GLOBALS['xoopsModuleConfig']['poll_module']);
350
            /** @var \XoopspollLogHandler $xpLogHandler */
351
            $xpLogHandler = xoops_getModuleHandler('log', $GLOBALS['xoopsModuleConfig']['poll_module']);
352
            //            $classRequest = ucfirst($GLOBALS['xoopsModuleConfig']["poll_module"]) . "Request";
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
353
            $classConstants   = ucfirst($GLOBALS['xoopsModuleConfig']['poll_module']) . 'Constants';
354
            $notify           = Request::getInt('notify', $classConstants::NOTIFICATION_ENABLED, 'POST');
355
            $currentTimestamp = time();
356
            //$xuEndTimestamp   = method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime(Request::getString('xu_end_time', null, 'POST'))
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
357
            //                                                             : strtotime(Request::getString('xu_end_time', null, 'POST'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
358
            $xuEndTimestamp = strtotime(Request::getString('xu_end_time', null, 'POST'));
359
            $endTimestamp   = (!Request::getString('xu_end_time', null, 'POST')) ? ($currentTimestamp + $classConstants::DEFAULT_POLL_DURATION) : userTimeToServerTime($xuEndTimestamp);
360
            //$xuStartTimestamp = method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime(Request::getString('xu_start_time', null, 'POST'))
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
361
            //                                                             : strtotime(Request::getString('xu_start_time', null, 'POST'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
362
            $xuStartTimestamp = strtotime(Request::getString('xu_start_time', null, 'POST'));
363
            $startTimestamp   = (!Request::getString('xu_start_time', null, 'POST')) ? ($endTimestamp - $classConstants::DEFAULT_POLL_DURATION) : userTimeToServerTime($xuStartTimestamp);
364
365
            //  don't allow changing start time if there are votes in the log
366
            if (($startTimestamp < $pollObject->getVar('start_time'))
367
                && ($xpLogHandler->getTotalVotesByPollId($poll_id) > 0)) {
368
                $startTimestamp = $pollObject->getVar('start_time'); //don't change start time
369
            }
370
371
            $poll_vars = [
372
                'user_id'     => Request::getInt('user_id', $GLOBALS['xoopsUser']->uid(), 'POST'),
373
                'question'    => Request::getString('question', null, 'POST'),
374
                'description' => Request::getText('description', null, 'POST'),
375
                'mail_status' => ($classConstants::NOTIFICATION_ENABLED == $notify) ? $classConstants::POLL_NOT_MAILED : $classConstants::POLL_MAILED,
376
                'mail_voter'  => Request::getInt('mail_voter', $classConstants::NOT_MAIL_POLL_TO_VOTER, 'POST'),
377
                'start_time'  => $startTimestamp,
378
                'end_time'    => $endTimestamp,
379
                'display'     => Request::getInt('display', $classConstants::DO_NOT_DISPLAY_POLL_IN_BLOCK, 'POST'),
380
                'visibility'  => Request::getInt('visibility', $classConstants::HIDE_NEVER, 'POST'),
381
                'weight'      => Request::getInt('weight', $classConstants::DEFAULT_WEIGHT, 'POST'),
382
                'multiple'    => Request::getInt('multiple', $classConstants::NOT_MULTIPLE_SELECT_POLL, 'POST'),
383
                'multilimit'  => Request::getInt('multilimit', $classConstants::MULTIPLE_SELECT_LIMITLESS, 'POST'),
384
                'anonymous'   => Request::getInt('anonymous', $classConstants::ANONYMOUS_VOTING_DISALLOWED, 'POST')
385
            ];
386
            $pollObject->setVars($poll_vars);
387
            $poll_id = $xpPollHandler->insert($pollObject);
388
            if (!$poll_id) {
389
                $err = $pollObject->getHtmlErrors();
390
                exit($err);
391
            }
392
393
            // now get the options
394
            $optionIdArray    = Request::getArray('option_id', [], 'POST');
395
            $optionIdArray    = array_map('intval', $optionIdArray);
396
            $optionTextArray  = Request::getArray('option_text', [], 'POST');
397
            $optionColorArray = Request::getArray('option_color', [], 'POST');
398
399
            foreach ($optionIdArray as $key => $oId) {
400
                if (!empty($oId) && ($optionObject = $xpOptHandler->get($oId))) {
401
                    // existing option object so need to update it
402
                    $optionTextArray[$key] = trim($optionTextArray[$key]);
403
                    if ('' === $optionTextArray[$key]) {
404
                        // want to delete this option
405
                        if (false !== $xpOptHandler->delete($optionObject)) {
406
                            // now remove it from the log
407
                            $xpLogHandler->deleteByOptionId($optionObject->getVar('option_id'));
408
                            //update vote count in poll
409
                            $xpPollHandler->updateCount($pollObject);
410
                        } else {
411
                            xoops_error($xpLogHandler->getHtmlErrors());
412
                            break;
413
                        }
414 View Code Duplication
                    } else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
415
                        $optionObject->setVar('option_text', $optionTextArray[$key]);
416
                        $optionObject->setVar('option_color', $optionColorArray[$key]);
417
                        $optionObject->setVar('poll_id', $poll_id);
418
                        $xpOptHandler->insert($optionObject);
419
                    }
420
                } else {
421
                    // new option object
422
                    $optionObject            = $xpOptHandler->create();
423
                    $optionTextArray[$key] = trim($optionTextArray[$key]);
424 View Code Duplication
                    if ('' !== $optionTextArray[$key]) { // ignore if text is empty
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
425
                        $optionObject->setVar('option_text', $optionTextArray[$key]);
426
                        $optionObject->setVar('option_color', $optionColorArray[$key]);
427
                        $optionObject->setVar('poll_id', $poll_id);
428
                        $xpOptHandler->insert($optionObject);
429
                    }
430
                    unset($optionObject);
431
                }
432
            }
433
            // old xoopspoll or umfrage or any clone from them
434
        } else {
435
            $pollObject->setVar('question', Request::getString('question', '', 'POST'));
436
            $pollObject->setVar('description', Request::getString('description', '', 'POST'));
437
438
            $end_time = Request::getString('end_time', '', 'POST');
439
            if ('' !== $end_time) {
440
                $timezone = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('timezone') : null;
441
                $pollObject->setVar('end_time', userTimeToServerTime(method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime($end_time) : strtotime($end_time), $timezone));
442
            }
443
            $pollObject->setVar('display', 0);
444
            $pollObject->setVar('weight', Request::getInt('weight', 0, 'POST'));
445
            $pollObject->setVar('multiple', Request::getInt('multiple', 0, 'POST'));
446
            $pollObject->setVar('user_id', Request::getInt('user_id', 0, 'POST'));
447 View Code Duplication
            if (Request::getInt('notify', 0, 'POST') && $end_time > time()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
448
                // if notify, set mail status to "not mailed"
449
                $pollObject->setVar('mail_status', POLL_NOTMAILED);
450
            } else {
451
                // if not notify, set mail status to already "mailed"
452
                $pollObject->setVar('mail_status', POLL_MAILED);
453
            }
454
455
            if (!$pollObject->store()) {
456
                xoops_error($pollObject->getHtmlErrors);
457
                break;
458
            }
459
            $i            = 0;
460
            $option_id    = Request::getArray('option_id', null, 'POST');
461
            $option_color = Request::getArray('option_color', null, 'POST');
462
            $classOption  = $classPoll . 'Option';
463
            $classLog     = $classPoll . 'Log';
464
            foreach ($option_id as $opid) {
465
                $optionObject      = new $classOption($opid);
466
                $option_text[$i] = trim($option_text[$i]);
467
                if ($option_text[$i] !== '') {
468
                    $optionObject->setVar('option_text', $option_text[$i]);
469
                    $optionObject->setVar('option_color', $option_color[$i]);
470
                    $optionObject->store();
471
                } else {
472
                    if ($optionObject->delete() !== false) {
473
                        $classLog::deleteByOptionId($option->getVar('option_id'));
474
                    }
475
                }
476
                ++$i;
477
            }
478
            $pollObject->updateCount();
479
        }
480
        // clear the template cache so changes take effect immediately
481
        include_once $GLOBALS['xoops']->path('class/template.php');
482
        xoops_template_clear_module_cache($GLOBALS['xoopsModule']->getVar('mid'));
483
        xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
484
485
        // update topic to indicate it has a poll
486
        $topicObject->setVar('topic_haspoll', 1);
487
        $topicObject->setVar('poll_id', $pollObject->getVar('poll_id'));
488
        $success = $topicHandler->insert($topicObject);
489
        if (!$success) {
490
            xoops_error($topicHandler->getHtmlErrors());
491
        } else {
492
            redirect_header("viewtopic.php?topic_id={$topic_id}", 2, _MD_NEWBB_POLL_DBUPDATED);
493
        }
494
        break;// op: save | update
495
496
    case 'addmore':
497
        $question = $pollObject->getVar('question');
498
        unset($pollObject);
499
        $poll_form = new XoopsThemeForm(_MD_NEWBB_POLL_ADDMORE, 'poll_form', 'polls.php', 'post', true);
500
        $poll_form->addElement(new XoopsFormLabel(_MD_NEWBB_POLL_POLLQUESTION, $question));
501
        // new xoopspoll module
502
        if ($pollModuleHandler->getVar('version') >= 140) {
503
            $xpOptHandler = xoops_getModuleHandler('option', $GLOBALS['xoopsModuleConfig']['poll_module']);
504
            $option_tray  = $xpOptHandler->renderOptionFormTray($poll_id);
505
            // old xoopspoll or umfrage or any clone from them
506
        } else {
507
            $option_tray    = new XoopsFormElementTray(_MD_NEWBB_POLL_POLLOPTIONS, '');
508
            $barcolor_array = XoopsLists::getImgListAsArray($GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModuleConfig']['poll_module'] . '/assets/images/colorbars/'));
509
            for ($i = 0; $i < 10; ++$i) {
510
                $current_bar = (current($barcolor_array) !== 'blank.gif') ? current($barcolor_array) : next($barcolor_array);
511
                $option_text = new XoopsFormText('', 'option_text[]', 50, 255);
512
                $option_tray->addElement($option_text);
513
                $color_select = new XoopsFormSelect('', "option_color[{$i}]", $current_bar);
514
                $color_select->addOptionArray($barcolor_array);
515
                $color_select->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[{$i}]\", \"modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars\", \"\", \"" . XOOPS_URL . "\")'");
516
                $color_label = new XoopsFormLabel('', "<img src='"
517
                                                      . $GLOBALS['xoops']->url("modules/{$GLOBALS['xoopsModuleConfig']['poll_module']}/assets/images/colorbars/{$current_bar}")
518
                                                      . "' name='option_color_image[{$i}]' id='option_color_image[{$i}]' class='alignbottom' width='30' height='15' alt='' /><br>");
519
                $option_tray->addElement($color_select);
520
                $option_tray->addElement($color_label);
521
                unset($color_select, $color_label, $option_text);
522
                if (!next($barcolor_array)) {
523
                    reset($barcolor_array);
524
                }
525
            }
526
        }
527
        $poll_form->addElement($option_tray);
528
        $poll_form->addElement(new XoopsFormButtonTray('poll_submit', _SUBMIT, 'submit'));
529
        $poll_form->addElement(new XoopsFormHidden('op', 'savemore'));
530
        $poll_form->addElement(new XoopsFormHidden('topic_id', $topic_id));
531
        $poll_form->addElement(new XoopsFormHidden('poll_id', $poll_id));
532
533
        echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
534
        $poll_form->display();
535
        break;
536
537
    case 'savemore':
538
        // check security token
539 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
540
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
541
        }
542
543
        $option_text   = Request::getString('option_text', '', 'POST');
544
        $option_string = is_array($option_text) ? implode('', $option_text) : $option_text;
545
        $option_string = trim($option_string);
546 View Code Duplication
        if ('' === $option_string) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
547
            // irmtfan - issue with javascript:history.go(-1)
548
            redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), 2, _MD_NEWBB_ERROROCCURED . ': ' . _MD_NEWBB_POLL_POLLOPTIONS . ' !');
549
        }
550
        $i            = 0;
551
        $option_color = Request::getArray('option_color', null, 'POST');
552
        foreach ($option_text as $optxt) {
553
            $optxt = trim($optxt);
554
            if ('' !== $optxt) {
555
                // new xoopspoll module
556
                if ($pollModuleHandler->getVar('version') >= 140) {
557
                    $xpOptHandler = xoops_getModuleHandler('option', $GLOBALS['xoopsModuleConfig']['poll_module']);
558
                    $optionObject   = $xpOptHandler->create();
559
                    $optionObject->setVar('option_text', $optxt);
560
                    $optionObject->setVar('poll_id', $poll_id);
561
                    $optionObject->setVar('option_color', $option_color[$i]);
562
                    $xpOptHandler->insert($optionObject);
563
                    // old xoopspoll or umfrage or any clone from them
564
                } else {
565
                    $classOption = $classPoll . 'Option';
566
                    $optionObject  = new $classOption();
567
                    $optionObject->setVar('option_text', $optxt);
568
                    $optionObject->setVar('poll_id', $poll_id);
569
                    $optionObject->setVar('option_color', $option_color[$i]);
570
                    $optionObject->store();
571
                }
572
                unset($optionObject);
573
            }
574
            ++$i;
575
        }
576
        include_once $GLOBALS['xoops']->path('class/template.php');
577
        xoops_template_clear_module_cache($GLOBALS['xoopsModule']->getVar('mid'));
578
        xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
579
        redirect_header("polls.php?op=edit&amp;poll_id={$poll_id}&amp;topic_id={$topic_id}", 2, _MD_NEWBB_POLL_DBUPDATED);
580
        break;
581
582
    case 'delete':
583
        echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
584
        xoops_confirm(['op' => 'delete_ok', 'topic_id' => $topic_id, 'poll_id' => $poll_id], 'polls.php', sprintf(_MD_NEWBB_POLL_RUSUREDEL, $pollObject->getVar('question')));
585
        break;
586
587
    case 'delete_ok':
588
        // check security token
589 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
590
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
591
        }
592
        //try and delete the poll
593
        // new xoopspoll module
594
        if ($pollModuleHandler->getVar('version') >= 140) {
595
            $status = $xpPollHandler->delete($pollObject);
596
            if (false !== $status) {
597
                $xpOptHandler = xoops_getModuleHandler('option', $GLOBALS['xoopsModuleConfig']['poll_module']);
598
                $xpLogHandler = xoops_getModuleHandler('log', $GLOBALS['xoopsModuleConfig']['poll_module']);
599
                $xpOptHandler->deleteByPollId($poll_id);
600
                $xpLogHandler->deleteByPollId($poll_id);
601
            } else {
602
                $msg = $xpPollHandler->getHtmlErrors();
603
            }
604
            // old xoopspoll or umfrage or any clone from them
605
        } else {
606
            $status      = $pollObject->delete();
607
            $classOption = $classPoll . 'Option';
608
            $classLog    = $classPoll . 'Log';
609
            if (false !== $status) {
610
                $classOption::deleteByPollId($poll_id);
611
                $classLog::deleteByPollId($poll_id);
612
            } else {
613
                $msg = $pollObject->getHtmlErrors();
614
            }
615
        }
616
        if (false !== $status) {
617
            include_once $GLOBALS['xoops']->path('class/template.php');
618
            xoops_template_clear_module_cache($xoopsModule->getVar('mid'));
619
            xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
620
            // delete comments for this poll
621
            xoops_comment_delete($xoopsModule->getVar('mid'), $poll_id);
622
623
            $topicObject->setVar('votes', 0); // not sure why we want to clear votes too... but I left it alone
624
            $topicObject->setVar('topic_haspoll', 0);
625
            $topicObject->setVar('poll_id', 0);
626
            $success = $topicHandler->insert($topicObject);
627
            if (!$success) {
628
                xoops_error($topicHandler->getHtmlErrors());
629
                break;
630
            }
631
        } else {
632
            xoops_error($msg);
633
            break;
634
        }
635
        redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?topic_id={$topic_id}", 1, _MD_NEWBB_POLL_DBUPDATED);
636
        break;
637
638
    case 'restart':
639
        // new xoopspoll module
640
        if ($pollModuleHandler->getVar('version') >= 140) {
641
            $classConstants        = ucfirst($GLOBALS['xoopsModuleConfig']['poll_module']) . 'Constants';
642
            $default_poll_duration = $classConstants::DEFAULT_POLL_DURATION;
643
            // old xoopspoll or umfrage or any clone from them
644
        } else {
645
            $default_poll_duration = (86400 * 10);
646
        }
647
        $poll_form   = new XoopsThemeForm(_MD_NEWBB_POLL_RESTARTPOLL, 'poll_form', 'polls.php', 'post', true);
648
        $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')) . '<br>' . sprintf(_MD_NEWBB_POLL_EXPIREDAT,
649
                                                                                                                                                                                                                     formatTimestamp($pollObject->getVar('end_time'), 'Y-m-d H:i:s')) . '</small>',
650
                                         'end_time', 20, 19, formatTimestamp(time() + $default_poll_duration, 'Y-m-d H:i:s'));
651
        $poll_form->addElement($expire_text);
652
        $poll_form->addElement(new XoopsFormRadioYN(_MD_NEWBB_POLL_NOTIFY, 'notify', 1));
653
        $poll_form->addElement(new XoopsFormRadioYN(_MD_NEWBB_POLL_RESET, 'reset', 0));
654
        $poll_form->addElement(new XoopsFormHidden('op', 'restart_ok'));
655
        $poll_form->addElement(new XoopsFormHidden('topic_id', $topic_id));
656
        $poll_form->addElement(new XoopsFormHidden('poll_id', $poll_id));
657
        $poll_form->addElement(new XoopsFormButton('', 'poll_submit', _MD_NEWBB_POLL_RESTART, 'submit'));
658
659
        echo '<h4>' . _MD_NEWBB_POLL_POLLCONF . "</h4>\n";
660
        $poll_form->display();
661
        break;
662
663
    case 'restart_ok':
664
        // check security token
665 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
666
            redirect_header(Request::getString('PHP_SELF', '', 'SERVER'), 2, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
667
        }
668
669
        // new xoopspoll module
670
        if ($pollModuleHandler->getVar('version') >= 140) {
671
            $classConstants        = ucfirst($GLOBALS['xoopsModuleConfig']['poll_module']) . 'Constants';
672
            $default_poll_duration = $classConstants::DEFAULT_POLL_DURATION;
673
            $poll_not_mailed       = $classConstants::POLL_NOT_MAILED;
674
            $poll_mailed           = $classConstants::POLL_MAILED;
675
            // old xoopspoll or umfrage or any clone from them
676
        } else {
677
            $default_poll_duration = (86400 * 10);
678
            $poll_not_mailed       = POLL_NOTMAILED;
679
            $poll_mailed           = POLL_MAILED;
680
        }
681
682
        $end_time = !Request::getInt('end_time', 0, 'POST');
683 View Code Duplication
        if (0 !== $end_time) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
684
            $timezone = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('timezone') : null;
685
            $pollObject->setVar('end_time', userTimeToServerTime(method_exists('XoopsLocal', 'strtotime') ? XoopsLocal::strtotime($end_time) : strtotime($end_time), $timezone));
686
        } else {
687
            $pollObject->setVar('end_time', time() + $default_poll_duration);
688
        }
689
690
        $isNotify = Request::getInt('notify', 0, 'POST');
691
        if (!empty($isNotify) && ($end_time > time())) {
692
            // if notify, set mail status to "not mailed"
693
            $pollObject->setVar('mail_status', $poll_not_mailed);
694
        } else {
695
            // if not notify, set mail status to already "mailed"
696
            $pollObject->setVar('mail_status', $poll_mailed);
697
        }
698
699
        // new xoopspoll module
700
        if ($pollModuleHandler->getVar('version') >= 140) {
701
            if (!$xpPollHandler->insert($pollObject)) {  // update the poll
702
                xoops_error($pollObject->getHtmlErrors());
703
                exit();
704
            }
705
            if (Request::getInt('reset', 0, 'POST')) { // reset all vote/voter counters
706
                /** @var \XoopspollOptionHandler $xpOptHandler */
707
                $xpOptHandler = xoops_getModuleHandler('option', $GLOBALS['xoopsModuleConfig']['poll_module']);
708
                /** @var \XoopspollLogHandler $xpLogHandler */
709
                $xpLogHandler = xoops_getModuleHandler('log', $GLOBALS['xoopsModuleConfig']['poll_module']);
710
                $xpLogHandler->deleteByPollId($poll_id);
711
                $xpOptHandler->resetCountByPollId($poll_id);
712
                $xpPollHandler->updateCount($pollObject);
713
            }
714
            // old xoopspoll or umfrage or any clone from them
715
        } else {
716
            if (!$pollObject->store()) { // update the poll
717
                xoops_error($pollObject->getHtmlErrors());
718
                exit();
719
            }
720
            if (Request::getInt('reset', 0, 'POST')) { // reset all logs
721
                $classOption = $classPoll . 'Option';
722
                $classLog    = $classPoll . 'Log';
723
                $classLog::deleteByPollId($poll_id);
724
                $classOption::resetCountByPollId($poll_id);
725
                $pollObject->updateCount();
726
            }
727
        }
728
        include_once $GLOBALS['xoops']->path('class/template.php');
729
        xoops_template_clear_module_cache($xoopsModule->getVar('mid'));
730
        xoops_template_clear_module_cache($pollModuleHandler->getVar('mid'));
731
        redirect_header(XOOPS_URL . "/modules/newbb/viewtopic.php?topic_id={$topic_id}", 1, _MD_NEWBB_POLL_DBUPDATED);
732
        break;
733
734
    case 'log':
735
        // new xoopspoll module
736
        if ($pollModuleHandler->getVar('version') >= 140) {
737
            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);
738
            // old xoopspoll or umfrage or any clone from them
739
        } else {
740
            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);
741
        }
742
        break;
743
} // switch
744
745
// irmtfan move to footer.php
746
include_once __DIR__ . '/footer.php';
747
include $GLOBALS['xoops']->path('footer.php');
748