Completed
Push — master ( 644961...accf68 )
by Michael
13s
created

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