Passed
Pull Request — master (#2)
by Goffy
03:21
created

question.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
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
13
/**
14
 * wgEvents module for xoops
15
 *
16
 * @copyright    2021 XOOPS Project (https://xoops.org)
17
 * @license      GPL 2.0 or later
18
 * @package      wgevents
19
 * @since        1.0.0
20
 * @min_xoops    2.5.11 Beta1
21
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
22
 */
23
24
use Xmf\Request;
25
use XoopsModules\Wgevents;
26
use XoopsModules\Wgevents\{
27
    Constants,
28
    Common
29
};
30
31
require __DIR__ . '/header.php';
32
$GLOBALS['xoopsOption']['template_main'] = 'wgevents_question.tpl';
33
require_once \XOOPS_ROOT_PATH . '/header.php';
34
35
$op      = Request::getCmd('op', 'list');
36
$queId   = Request::getInt('id');
37
$queEvid = Request::getInt('evid');
38
$start   = Request::getInt('start');
39
$limit   = Request::getInt('limit', $helper->getConfig('userpager'));
40
$GLOBALS['xoopsTpl']->assign('start', $start);
41
$GLOBALS['xoopsTpl']->assign('limit', $limit);
42
43
// Define Stylesheet
44
$GLOBALS['xoTheme']->addStylesheet($style, null);
45
// Paths
46
$GLOBALS['xoopsTpl']->assign('xoops_icons32_url', \XOOPS_ICONS32_URL);
47
$GLOBALS['xoopsTpl']->assign('wgevents_url', \WGEVENTS_URL);
48
// Keywords
49
$keywords = [];
50
// Breadcrumbs
51
$xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_INDEX, 'link' => 'index.php'];
52
53
$GLOBALS['xoopsTpl']->assign('addEvid', $queEvid);
54
55
switch ($op) {
56
    case 'show':
57
    case 'list':
58
    default:
59
        $GLOBALS['xoTheme']->addScript(\WGEVENTS_URL . '/assets/js/jquery-ui.min.js');
60
        $GLOBALS['xoTheme']->addScript(\WGEVENTS_URL . '/assets/js/sortables.js');
61
62
        // Breadcrumbs
63
        $xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_QUESTIONS_LIST];
64
65
        // check whether there are textblocks available
66
        $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0;
67
        $crTextblock = new \CriteriaCompo();
68
        $crTextblock->add(new \Criteria('class', Constants::TEXTBLOCK_CLASS_PUBLIC));
69
        $crTextblock->add(new \Criteria('submitter', $uidCurrent), 'OR');
70
        $textblocksCount = $textblockHandler->getCount($crTextblock);
71
        $GLOBALS['xoopsTpl']->assign('textblocksCount', $textblocksCount);
72
73
        // get default fields
74
        $regdefaults = [];
75
        $regdefaults[] = [
76
            'type_text' => \_MA_WGEVENTS_FIELD_TEXTBOX,
77
            'caption' => \_MA_WGEVENTS_REGISTRATION_FIRSTNAME,
78
            'value_list' => '',
79
            'placeholder' => \_MA_WGEVENTS_REGISTRATION_FIRSTNAME_PLACEHOLDER,
80
            'required' => \_YES,
81
            'print' => \_YES
82
        ];
83
        $regdefaults[] = [
84
            'type_text' => \_MA_WGEVENTS_FIELD_TEXTBOX,
85
            'caption' => \_MA_WGEVENTS_REGISTRATION_LASTNAME,
86
            'value_list' => '',
87
            'placeholder' => \_MA_WGEVENTS_REGISTRATION_LASTNAME_PLACEHOLDER,
88
            'required' => \_YES,
89
            'print' => \_YES
90
        ];
91
        $regdefaults[] = [
92
            'type_text' => \_MA_WGEVENTS_FIELD_TEXTBOX,
93
            'caption' => \_MA_WGEVENTS_REGISTRATION_EMAIL,
94
            'value_list' => '',
95
            'placeholder' => \_MA_WGEVENTS_REGISTRATION_EMAIL_PLACEHOLDER,
96
            'required' => \_YES,
97
            'print' => \_YES
98
        ];
99
        $GLOBALS['xoopsTpl']->assign('regdefaults', $regdefaults);
100
101
        // get question fields
102
        $crQuestion = new \CriteriaCompo();
103
        $crQuestion->add(new \Criteria('evid', $queEvid));
104
        $questionsCount = $questionHandler->getCount($crQuestion);
105
        $GLOBALS['xoopsTpl']->assign('questionsCount', $questionsCount);
106
        $crQuestion->setSort('weight ASC, id');
107
        $crQuestion->setOrder('DESC');
108
        $crQuestion->setStart($start);
109
        $crQuestion->setLimit($limit);
110
        $questionsAll = $questionHandler->getAll($crQuestion);
111
        if ($questionsCount > 0) {
112
            $questions = [];
113
            $evName = '';
114
            $evSubmitter = 0;
115
            $evStatus = 0;
116
            // Get All Question
117
            foreach (\array_keys($questionsAll) as $i) {
118
                $questions[$i] = $questionsAll[$i]->getValuesQuestions();
119
                if ('' == $evName) {
120
                    $eventObj = $eventHandler->get($questionsAll[$i]->getVar('evid'));
121
                    $evName = $eventObj->getVar('name');
122
                    $evSubmitter = $eventObj->getVar('submitter');
123
                    $evStatus = $eventObj->getVar('status');
124
                    $keywords[$i] = $evName;
125
                }
126
            }
127
            $GLOBALS['xoopsTpl']->assign('questions', $questions);
128
            unset($questions);
129
            // Display Navigation
130
            if ($questionsCount > $limit) {
131
                require_once \XOOPS_ROOT_PATH . '/class/pagenav.php';
132
                $pagenav = new \XoopsPageNav($questionsCount, $limit, $start, 'start', 'op=list&limit=' . $limit);
133
                $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav());
134
            }
135
            $GLOBALS['xoopsTpl']->assign('eventName', $evName);
136
            $permEdit = $permissionsHandler->getPermQuestionsAdmin($evSubmitter, $evStatus);
137
            $GLOBALS['xoopsTpl']->assign('permEdit', $permEdit);
138
            $GLOBALS['xoopsTpl']->assign('wgevents_icons_url_16', \WGEVENTS_ICONS_URL_16);
139
140
            $GLOBALS['xoopsTpl']->assign('xoops_pagetitle', \strip_tags($evName . ' - ' . $GLOBALS['xoopsModule']->getVar('name')));
141
142
        }
143
        break;
144
    case 'save':
145
        // Security Check
146
        if (!$GLOBALS['xoopsSecurity']->check()) {
147
            \redirect_header('question.php', 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
148
        }
149
        $eventObj = $eventHandler->get($queEvid);
150
        // Check permissions
151
        if (!$permissionsHandler->getPermQuestionsAdmin($eventObj->getVar('submitter'), $eventObj->getVar('status'))) {
152
            \redirect_header('index.php?op=list', 3, \_NOPERM);
153
        }
154
        if ($queId > 0) {
155
            $questionObj = $questionHandler->get($queId);
156
        } else {
157
            $questionObj = $questionHandler->create();
158
        }
159
        $questionObj->setVar('evid', $queEvid);
160
        $queType = Request::getInt('type');
161
        $questionObj->setVar('fdid', $queType);
162
        $fieldObj = $fieldHandler->get($queType);
163
        $questionObj->setVar('type', $fieldObj->getVar('type'));
164
        $questionObj->setVar('caption', Request::getString('caption'));
165
        $questionObj->setVar('desc', Request::getText('desc'));
166
        $queValuesText = '';
167
        $queValues = Request::getString('values');
168
        if ('' != $queValues) {
169
            if (Constants::FIELD_COMBOBOX == $queType || Constants::FIELD_SELECTBOX == $queType || Constants::FIELD_RADIO == $queType) {
170
                $queValuesText = \serialize(\preg_split('/\r\n|\r|\n/', $queValues));
171
            } else {
172
                $tmpArr = [$queValues];
173
                $queValuesText = \serialize($tmpArr);
174
            }
175
        }
176
        $questionObj->setVar('values', $queValuesText);
177
        $questionObj->setVar('placeholder', Request::getString('placeholder'));
178
        $questionObj->setVar('required', Request::getInt('required'));
179
        $questionObj->setVar('print', Request::getInt('print'));
180
        $questionObj->setVar('weight', Request::getInt('weight'));
181
        if (Request::hasVar('datecreated_int')) {
182
            $questionObj->setVar('datecreated', Request::getInt('datecreated_int'));
183
        } else {
184
            $questionDatecreatedObj = \DateTime::createFromFormat(\_SHORTDATESTRING, Request::getString('datecreated'));
185
            $questionObj->setVar('datecreated', $questionDatecreatedObj->getTimestamp());
186
        }
187
        $questionObj->setVar('submitter', Request::getInt('submitter'));
188
        // Insert Data
189
        if ($questionHandler->insert($questionObj)) {
190
            // redirect after insert
191
            \redirect_header('question.php?op=list&amp;evid=' . $queEvid . '&amp;start=' . $start . '&amp;limit=' . $limit, 2, \_MA_WGEVENTS_FORM_OK);
192
        }
193
        // Get Form Error
194
        $GLOBALS['xoopsTpl']->assign('error', $questionObj->getHtmlErrors());
195
        $form = $questionObj->getForm();
196
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
197
        break;
198
    case 'save_textblock':
199
        // Security Check
200
        if (!$GLOBALS['xoopsSecurity']->check()) {
201
            \redirect_header('question.php', 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
202
        }
203
        $eventObj = $eventHandler->get($queEvid);
204
        // Check permissions
205
        if (!$permissionsHandler->getPermQuestionsAdmin($eventObj->getVar('submitter'), $eventObj->getVar('status'))) {
206
            \redirect_header('index.php?op=list', 3, \_NOPERM);
207
        }
208
        $weight = $questionHandler->getNextWeight($queEvid);
209
        $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0;
210
211
        $cbTextblocks = Request::getArray('cbTextblock');
212
        $errors = '';
213
        foreach (\array_keys($cbTextblocks) as $i) {
214
            $textblockObj = $textblockHandler->get($i);
215
216
            $questionObj = $questionHandler->create();
217
            $questionObj->setVar('evid', $queEvid);
218
            $questionObj->setVar('fdid', Constants::FIELD_LABEL);
219
            $fieldObj = $fieldHandler->get(Constants::FIELD_LABEL);
220
            $questionObj->setVar('type', $fieldObj->getVar('type'));
221
            $questionObj->setVar('caption', $textblockObj->getVar('name'));
222
            $questionObj->setVar('desc', $textblockObj->getVar('text'));
223
            $questionObj->setVar('values', '');
224
            $questionObj->setVar('placeholder', '');
225
            $questionObj->setVar('required', 0);
226
            $questionObj->setVar('print', 0);
227
            $questionObj->setVar('weight', $weight);
228
            $questionObj->setVar('datecreated', \time());
229
            $questionObj->setVar('submitter', $uidCurrent);
230
            // Insert Data
231
            if (!$questionHandler->insert($questionObj)) {
232
                $errors .= $questionHandler.getHtmlErrors();
0 ignored issues
show
The function getHtmlErrors was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
                $errors .= $questionHandler./** @scrutinizer ignore-call */ getHtmlErrors();
Loading history...
233
            }
234
            $weight++;
235
        }
236
        if ('' == $errors) {
237
            // redirect after insert
238
            \redirect_header('question.php?op=list&amp;evid=' . $queEvid . '&amp;start=' . $start . '&amp;limit=' . $limit, 2, \_MA_WGEVENTS_FORM_OK);
239
        } else {
240
            $GLOBALS['xoopsTpl']->assign('error', $errors);
241
        }
242
        break;
243
    case 'newset':
244
        $eventObj = $eventHandler->get($queEvid);
245
        // Check permissions
246
        if (!$permissionsHandler->getPermQuestionsAdmin($eventObj->getVar('submitter'), $eventObj->getVar('status'))) {
247
            \redirect_header('index.php?op=list', 3, \_NOPERM);
248
        }
249
        $questionHandler->createQuestionsDefaultset($queEvid);
250
        \redirect_header('question.php?op=list&amp;evid=' . $queEvid . '&amp;start=' . $start . '&amp;limit=' . $limit, 0, \_MA_WGEVENTS_FORM_OK);
251
        break;
252
    case 'add_textblock':
253
        $eventObj = $eventHandler->get($queEvid);
254
        // Check permissions
255
        if (!$permissionsHandler->getPermQuestionsAdmin($eventObj->getVar('submitter'), $eventObj->getVar('status'))) {
256
            \redirect_header('index.php?op=list', 3, \_NOPERM);
257
        }
258
        $xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_TEXTBLOCK_ADD];
259
260
        $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0;
261
        $crTextblock = new \CriteriaCompo();
262
        $crTextblock->add(new \Criteria('class', Constants::TEXTBLOCK_CLASS_PUBLIC));
263
        $crTextblock->add(new \Criteria('submitter', $uidCurrent), 'OR');
264
        $textblocksCount = $textblockHandler->getCount($crTextblock);
265
        $GLOBALS['xoopsTpl']->assign('textblocksCount', $textblocksCount);
266
        if ($textblocksCount > 0) {
267
            $crTextblock->setStart($start);
268
            $crTextblock->setLimit($limit);
269
            $textblocksAll = $textblockHandler->getAll($crTextblock);
270
            $formTextblockSelect = $textblockHandler->getFormSelect($textblocksAll);
271
272
            $GLOBALS['xoopsTpl']->assign('formTextblockSelect', $formTextblockSelect->render());
273
            unset($textblocks);
274
            // Display Navigation
275
            if ($textblocksCount > $limit) {
276
                require_once \XOOPS_ROOT_PATH . '/class/pagenav.php';
277
                $pagenav = new \XoopsPageNav($textblocksCount, $limit, $start, 'start', 'op=list&limit=' . $limit);
278
                $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav());
279
            }
280
        }
281
        break;
282
    case 'new':
283
        $eventObj = $eventHandler->get($queEvid);
284
        // Check permissions
285
        if (!$permissionsHandler->getPermQuestionsAdmin($eventObj->getVar('submitter'), $eventObj->getVar('status'))) {
286
            \redirect_header('index.php?op=list', 3, \_NOPERM);
287
        }
288
        $GLOBALS['xoTheme']->addScript(\WGEVENTS_URL . '/assets/js/forms.js');
289
        // Breadcrumbs
290
        $xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_QUESTION_ADD];
291
        // Form Create
292
        $questionObj = $questionHandler->create();
293
        $questionObj->setVar('evid', $queEvid);
294
        $form = $questionObj->getForm();
295
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
296
        break;
297
    case 'test':
298
        $eventObj = $eventHandler->get($queEvid);
299
        // Check permissions
300
        if (!$permissionsHandler->getPermQuestionsAdmin($eventObj->getVar('submitter'), $eventObj->getVar('status'))) {
301
            \redirect_header('index.php?op=list', 3, \_NOPERM);
302
        }
303
        // Breadcrumbs
304
        $xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_QUESTION_ADD];
305
        // Form Create
306
        $registrationObj = $registrationHandler->create();
307
        $registrationObj->setVar('evid', $queEvid);
308
        $form = $registrationObj->getForm('', true);
309
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
310
        break;
311
    case 'edit':
312
        $eventObj = $eventHandler->get($queEvid);
313
        // Check permissions
314
        if (!$permissionsHandler->getPermQuestionsAdmin($eventObj->getVar('submitter'), $eventObj->getVar('status'))) {
315
            \redirect_header('index.php?op=list', 3, \_NOPERM);
316
        }
317
        $GLOBALS['xoTheme']->addScript(\WGEVENTS_URL . '/assets/js/forms.js');
318
        // Breadcrumbs
319
        $xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_QUESTION_EDIT];
320
        // Check params
321
        if (0 == $queId) {
322
            \redirect_header('question.php?op=list', 3, \_MA_WGEVENTS_INVALID_PARAM);
323
        }
324
        // Get Form
325
        $questionObj = $questionHandler->get($queId);
326
        $questionObj->start = $start;
327
        $questionObj->limit = $limit;
328
        $form = $questionObj->getForm();
329
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
330
        break;
331
    case 'clone':
332
        // Breadcrumbs
333
        $xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_QUESTION_CLONE];
334
        // Request source
335
        $queIdSource = Request::getInt('id_source');
336
        // Check params
337
        if (0 == $queIdSource) {
338
            \redirect_header('question.php?op=list', 3, \_MA_WGEVENTS_INVALID_PARAM);
339
        }
340
        // Get Form
341
        $questionObjSource = $questionHandler->get($queIdSource);
342
        $questionObj = $questionHandler->create();
343
        $questionObj->setVar('evid', $questionObjSource->getVar('evid'));
344
        $questionObj->setVar('fdid', $questionObjSource->getVar('fdid'));
345
        $questionObj->setVar('type', $questionObjSource->getVar('type'));
346
        $questionObj->setVar('caption', $questionObjSource->getVar('caption'));
347
        $questionObj->setVar('desc', $questionObjSource->getVar('desc'));
348
        $questionObj->setVar('values', $questionObjSource->getVar('values'));
349
        $questionObj->setVar('placeholder', $questionObjSource->getVar('placeholder'));
350
        $questionObj->setVar('required', $questionObjSource->getVar('required'));
351
        $questionObj->setVar('print', $questionObjSource->getVar('print'));
352
        $questionObj->setVar('weight', $questionObjSource->getVar('weight'));
353
        $form = $questionObj->getForm('question.php?op=save');
354
        $GLOBALS['xoopsTpl']->assign('form', $form->render());
355
        unset($questionObjSource);
356
        break;
357
    case 'delete':
358
        // Breadcrumbs
359
        $xoBreadcrumbs[] = ['title' => \_MA_WGEVENTS_QUESTION_DELETE];
360
        // Check params
361
        if (0 == $queId) {
362
            \redirect_header('question.php?op=list', 3, \_MA_WGEVENTS_INVALID_PARAM);
363
        }
364
        $questionObj = $questionHandler->get($queId);
365
        $queEvid = $questionObj->getVar('evid');
366
        if (isset($_REQUEST['ok']) && 1 == $_REQUEST['ok']) {
367
            if (!$GLOBALS['xoopsSecurity']->check()) {
368
                \redirect_header('question.php', 3, \implode(', ', $GLOBALS['xoopsSecurity']->getErrors()));
369
            }
370
            if ($questionHandler->delete($questionObj)) {
371
                \redirect_header('question.php?list&amp;evid=' . $queEvid, 3, \_MA_WGEVENTS_FORM_DELETE_OK);
372
            } else {
373
                $GLOBALS['xoopsTpl']->assign('error', $questionObj->getHtmlErrors());
374
            }
375
        } else {
376
            $customConfirm = new Common\Confirm(
377
                ['ok' => 1, 'id' => $queId, 'evid' => $queEvid, 'op' => 'delete'],
378
                $_SERVER['REQUEST_URI'],
379
                \sprintf(\_MA_WGEVENTS_CONFIRMDELETE_QUESTION, $questionObj->getVar('caption')), \_MA_WGEVENTS_CONFIRMDELETE_TITLE, \_MA_WGEVENTS_CONFIRMDELETE_LABEL);
380
            $form = $customConfirm->getFormConfirm();
381
            $GLOBALS['xoopsTpl']->assign('form', $form->render());
382
        }
383
        break;
384
    case 'order':
385
        $order = $_POST['order'];
386
        for ($i = 0, $iMax = \count($order); $i < $iMax; $i++) {
387
            $questionObj = $questionHandler->get($order[$i]);
388
            $questionObj->setVar('weight', $i + 1);
389
            $questionHandler->insert($questionObj);
390
        }
391
        break;
392
}
393
394
// Keywords
395
wgeventsMetaKeywords($helper->getConfig('keywords') . ', ' . \implode(',', $keywords));
396
unset($keywords);
397
398
// Description
399
wgeventsMetaDescription(\_MA_WGEVENTS_QUESTIONS_DESC);
400
$GLOBALS['xoopsTpl']->assign('xoops_mpageurl', \WGEVENTS_URL.'/question.php');
401
$GLOBALS['xoopsTpl']->assign('wgevents_upload_url', \WGEVENTS_UPLOAD_URL);
402
403
require __DIR__ . '/footer.php';
404