clearAddSessionVars()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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
 * @copyright    {@link https://xoops.org/ XOOPS Project}
15
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
16
 * @author       Brian Wahoff <[email protected]>
17
 * @author       Eric Juden <[email protected]>
18
 * @author       XOOPS Development Team
19
 */
20
21
use Xmf\Module\Admin;
22
use Xmf\Request;
23
use XoopsModules\Xhelp;
24
use XoopsModules\Xhelp\Helper;
25
26
require_once __DIR__ . '/admin_header.php';
27
xoops_load('XoopsPagenav');
28
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
29
// require_once XHELP_CLASS_PATH . '/Form.php';
30
// require_once XHELP_CLASS_PATH . '/FormRegex.php';
31
32
define('_XHELP_FIELD_MINLEN', 2);
33
define('_XHELP_FIELD_MAXLEN', 16777215);
34
35
global $xoopsModule;
36
$module_id = $xoopsModule->getVar('mid');
37
38
$op = 'default';
39
40
if (Request::hasVar('op', 'REQUEST')) {
41
    $op = $_REQUEST['op'];
42
}
43
44
switch ($op) {
45
    case 'delfield':
46
        deleteField();
47
        break;
48
    case 'editfield':
49
        editField();
50
        break;
51
    case 'clearAddSession':
52
        clearAddSession();
53
        break;
54
    case 'clearEditSession':
55
        clearEditSession();
56
        break;
57
    case 'setFieldRequired':
58
        setFieldRequired();
59
        break;
60
    case 'manageFields':
61
    default:
62
        manageFields();
63
        break;
64
}
65
66
/**
67
 *
68
 */
69
function manageFields()
70
{
71
    global $icons;
72
    $helper = Helper::getInstance();
73
    $errors = [];
74
75
    $session     = Xhelp\Session::getInstance();
76
    $regex_array = &getRegexArray();
77
    /** @var \XoopsModules\Xhelp\TicketFieldHandler $ticketFieldHandler */
78
    $ticketFieldHandler = $helper->getHandler('TicketField');
79
80
    $start = $limit = 0;
81
82
    if (Request::hasVar('limit', 'GET')) {
83
        $limit = Request::getInt('limit', 0, 'GET');
84
    }
85
86
    if (Request::hasVar('start', 'GET')) {
87
        $start = Request::getInt('start', 0, 'GET');
88
    }
89
90
    if (!$limit) {
91
        $limit = 15;
92
    }
93
94
    if (isset($_POST['addField'])) {
95
        //Validate Field Information
96
        $has_errors = false;
97
        /** @var \XoopsModules\Xhelp\TicketFieldHandler $ticketFieldHandler */
98
        $ticketFieldHandler = $helper->getHandler('TicketField');
99
100
        $values = parseValues($_POST['fld_values']);
101
102
        if (!$control = xhelpGetControl($_POST['fld_controltype'])) {
103
            $has_errors                  = true;
104
            $errors['fld_controltype'][] = _AM_XHELP_VALID_ERR_CONTROLTYPE;
105
        }
106
107
        $fld_needslength = $control['needs_length'];
108
        $fld_needsvalues = $control['needs_values'];
109
110
        //name field filled?
111
        if ('' === trim($_POST['fld_name'])) {
112
            $has_errors           = true;
113
            $errors['fld_name'][] = _AM_XHELP_VALID_ERR_NAME;
114
        }
115
116
        $fld_fieldname = sanitizeFieldName(\Xmf\Request::getString('fld_fieldname', '', 'POST'));
117
118
        //fieldname filled
119
        if ('' === trim($fld_fieldname)) {
120
            $has_errors                = true;
121
            $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME;
122
        }
123
124
        //fieldname unique?
125
        $criteria = new \CriteriaCompo(new \Criteria('fieldname', $fld_fieldname));
126
        if ($ticketFieldHandler->getCount($criteria)) {
127
            $has_errors                = true;
128
            $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME_UNIQUE;
129
        }
130
131
        //Length filled
132
        if (0 == Request::getInt('fld_length', 0, 'POST') && true === $fld_needslength) {
133
            $has_errors             = true;
134
            $errors['fld_length'][] = sprintf(_AM_XHELP_VALID_ERR_LENGTH, 2, 16777215);
135
        }
136
137
        //Departments Chosen?
138
139
        //default value in value set?
140
        if (count($values)) {
141
            if (!in_array($_POST['fld_defaultvalue'], $values)
142
                && !array_key_exists($_POST['fld_defaultvalue'], $values)) {
143
                $has_errors                   = true;
144
                $errors['fld_defaultvalue'][] = _AM_XHELP_VALID_ERR_DEFAULTVALUE;
145
            }
146
147
            //length larger than longest value?
148
            $length = Request::getInt('fld_length', 0, 'POST');
149
            foreach ($values as $key => $value) {
150
                if (mb_strlen($key) > $length) {
151
                    $has_errors             = true;
152
                    $errors['fld_values'][] = sprintf(_AM_XHELP_VALID_ERR_VALUE_LENGTH, htmlentities($key, ENT_QUOTES | ENT_HTML5), $length);
153
                }
154
            }
155
            //Values are all of the correct datatype?
156
        } elseif ($fld_needsvalues) {
157
            $has_errors             = true;
158
            $errors['fld_values'][] = _AM_XHELP_VALID_ERR_VALUE;
159
        }
160
161
        if ($has_errors) {
162
            $afield = [];
163
164
            $afield['name']         = \Xmf\Request::getString('fld_name', '', 'POST');
165
            $afield['description']  = \Xmf\Request::getString('fld_description', '', 'POST');
166
            $afield['fieldname']    = $fld_fieldname;
167
            $afield['departments']  = $_POST['fld_departments'];
168
            $afield['controltype']  = $_POST['fld_controltype'];
169
            $afield['datatype']     = $_POST['fld_datatype'];
170
            $afield['required']     = $_POST['fld_required'];
171
            $afield['weight']       = $_POST['fld_weight'];
172
            $afield['defaultvalue'] = $_POST['fld_defaultvalue'];
173
            $afield['values']       = $_POST['fld_values'];
174
            $afield['length']       = $_POST['fld_length'];
175
            $afield['validation']   = ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox']);
176
            $session->set('xhelp_addField', $afield);
177
            $session->set('xhelp_addFieldErrors', $errors);
178
            $helper->redirect('admin/fields.php');
179
        }
180
181
        //Save field
182
        /** @var \XoopsModules\Xhelp\TicketFieldHandler $ticketFieldHandler */
183
        $ticketFieldHandler = $helper->getHandler('TicketField');
184
        /** @var \XoopsModules\Xhelp\TicketField $ticketField */
185
        $ticketField = $ticketFieldHandler->create();
186
        $ticketField->setVar('name', \Xmf\Request::getString('fld_name', '', 'POST'));
187
        $ticketField->setVar('description', \Xmf\Request::getString('fld_description', '', 'POST'));
188
        $ticketField->setVar('fieldname', $fld_fieldname);
189
        $ticketField->setVar('controltype', $_POST['fld_controltype']);
190
        $ticketField->setVar('datatype', $_POST['fld_datatype']);
191
        $ticketField->setVar('fieldlength', $_POST['fld_length']);
192
        $ticketField->setVar('required', $_POST['fld_required']);
193
        $ticketField->setVar('weight', $_POST['fld_weight']);
194
        $ticketField->setVar('defaultvalue', $_POST['fld_defaultvalue']);
195
        $ticketField->setVar('validation', ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox']));
196
        $ticketField->addValues($values);
197
        $ticketField->addDepartments($_POST['fld_departments']);
198
199
        if ($ticketFieldHandler->insert($ticketField)) {
200
            clearAddSessionVars();
201
            $helper->redirect('admin/fields.php', 3, _AM_XHELP_MSG_FIELD_ADD_OK);
202
        } else {
203
            $errors = $ticketField->getHtmlErrors();
204
            $helper->redirect('admin/fields.php', 3, _AM_XHELP_MSG_FIELD_ADD_ERR . $errors);
205
        }
206
    } else {
207
        $criteria = new \Criteria('', '');
208
        $criteria->setLimit($limit);
209
        $criteria->setStart($start);
210
        $criteria->setSort('weight');
211
        $criteria->setOrder('ASC');
212
213
        $count       = $ticketFieldHandler->getCount($criteria);
214
        $fieldsArray = $ticketFieldHandler->getObjects($criteria);
215
216
        //Display List of Current Fields, form for new field
217
        xoops_cp_header();
218
        //echo $oAdminButton->renderButtons('manfields');
219
        $adminObject = Admin::getInstance();
220
        $adminObject->displayNavigation(basename(__FILE__));
221
222
        if ($count) {
223
            $nav = new \XoopsPageNav($count, $limit, $start, 'start', "op=manageFields&amp;limit=$limit");
224
225
            echo "<table width='100%' cellspacing='1' class='outer'>
226
                <tr><th colspan='7'><label>" . _AM_XHELP_TEXT_MANAGE_FIELDS . '</label></th></tr>';
227
            echo "<tr class='head'>
228
                <td>" . _AM_XHELP_TEXT_ID . '</td>
229
                <td>' . _AM_XHELP_TEXT_NAME . '</td>
230
                <td>' . _AM_XHELP_TEXT_DESCRIPTION . '</td>
231
                <td>' . _AM_XHELP_TEXT_FIELDNAME . '</td>
232
                <td>' . _AM_XHELP_TEXT_CONTROLTYPE . '</td>
233
                <td>' . _AM_XHELP_TEXT_REQUIRED . '</td>
234
                <td>' . _AM_XHELP_TEXT_ACTIONS . '</td>
235
            </tr>';
236
237
            $req_link_params = [
238
                'op'          => 'setFieldRequired',
239
                'setrequired' => 1,
240
                'id'          => 0,
241
            ];
242
243
            foreach ($fieldsArray as $field) {
244
                $req_link_params['id'] = $field->getVar('id');
245
246
                if ($field->getVar('required')) {
247
                    $req_link_params['setrequired'] = 0;
248
                    $req_img                        = $icons['online'];
249
                    $req_title                      = _AM_XHELP_MESSAGE_DEACTIVATE;
250
                } else {
251
                    $req_link_params['setrequired'] = 1;
252
                    $req_img                        = $icons['offline'] ?? '';
253
                    $req_title                      = _AM_XHELP_MESSAGE_ACTIVATE;
254
                }
255
256
                $edit_url = Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'editfield', 'id' => $field->getVar('id')]);
0 ignored issues
show
Bug introduced by
The constant XHELP_ADMIN_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
257
                $del_url  = Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'delfield', 'id' => $field->getVar('id')]);
258
259
                echo "<tr class='even'><td>" . $field->getVar('id') . '</td>
260
                    <td>' . $field->getVar('name') . '</td>
261
                    <td>' . $field->getVar('description') . '</td>
262
                    <td>' . $field->getVar('fieldname') . '</td>
263
                    <td>' . xhelpGetControlLabel((string)$field->getVar('controltype')) . "</td>
264
                    <td><a href='" . Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', $req_link_params) . "' title='$req_title'>$req_img</a></td>
265
                    <td><a href='$edit_url'>" . ($icons['edit'] ?? '') . "</a>
266
                        <a href='$del_url'>" . ($icons['delete'] ?? '') . '</a></td>
267
                    </tr>';
268
            }
269
            echo '</table>';
270
            //Render Page Nav
271
            echo "<div id='pagenav'>" . $nav->renderNav() . '</div><br>';
272
        }
273
274
        //Get Custom Field From session (if exists)
275
        $field_info   = $session->get('xhelp_addField');
276
        $field_errors = $session->get('xhelp_addFieldErrors');
277
278
        /** @var \XoopsModules\Xhelp\DepartmentHandler $departmentHandler */
279
        $departmentHandler = $helper->getHandler('Department');
280
        $depts             = $departmentHandler->getObjects();
281
        $deptarr           = [];
282
283
        foreach ($depts as $obj) {
284
            $deptarr[$obj->getVar('id')] = $obj->getVar('department');
285
        }
286
287
        if (false === !$field_info) {
288
            //extract($field_info , EXTR_PREFIX_ALL , 'fld_');
289
            $fld_controltype  = $field_info['controltype'];
290
            $fld_datatype     = $field_info['datatype'];
291
            $fld_departments  = $field_info['departments'];
292
            $fld_name         = $field_info['name'];
293
            $fld_fieldname    = $field_info['fieldname'];
294
            $fld_description  = $field_info['description'];
295
            $fld_required     = $field_info['required'];
296
            $fld_length       = $field_info['length'];
297
            $fld_weight       = $field_info['weight'];
298
            $fld_defaultvalue = $field_info['defaultvalue'];
299
            $fld_values       = $field_info['values'];
300
            $fld_validation   = $field_info['validation'];
301
        } else {
302
            $fld_controltype  = '';
303
            $fld_datatype     = '';
304
            $fld_departments  = array_keys($deptarr);
305
            $fld_name         = '';
306
            $fld_fieldname    = '';
307
            $fld_description  = '';
308
            $fld_required     = '';
309
            $fld_length       = '';
310
            $fld_weight       = '';
311
            $fld_defaultvalue = '';
312
            $fld_values       = '';
313
            $fld_validation   = '';
314
        }
315
316
        if (false === !$field_errors) {
317
            xhelpRenderErrors($field_errors, Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'clearAddSession']));
0 ignored issues
show
Bug introduced by
It seems like $field_errors can also be of type boolean and string; however, parameter $err_arr of xhelpRenderErrors() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

317
            xhelpRenderErrors(/** @scrutinizer ignore-type */ $field_errors, Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'clearAddSession']));
Loading history...
318
        }
319
320
        //Add Field Form
321
        $controls       = xhelpGetControlArray();
322
        $control_select = new \XoopsFormSelect(_AM_XHELP_TEXT_CONTROLTYPE, 'fld_controltype', $fld_controltype);
323
        foreach ($controls as $key => $control) {
324
            $control_select->addOption($key, $control['label']);
325
        }
326
327
        $datatypes = [
328
            _XHELP_DATATYPE_TEXT       => _XHELP_DATATYPE_TEXT,
329
            _XHELP_DATATYPE_NUMBER_INT => _XHELP_DATATYPE_NUMBER_INT,
330
            _XHELP_DATATYPE_NUMBER_DEC => _XHELP_DATATYPE_NUMBER_DEC,
331
        ];
332
333
        $datatype_select = new \XoopsFormSelect(_AM_XHELP_TEXT_DATATYPE, 'fld_datatype', $fld_datatype);
334
        $datatype_select->addOptionArray($datatypes);
335
336
        $dept_select = new \XoopsFormSelect(_AM_XHELP_TEXT_DEPARTMENTS, 'fld_departments', $fld_departments, 5, true);
337
        foreach ($depts as $obj) {
338
            $dept_select->addOptionArray($deptarr);
339
        }
340
        unset($depts);
341
342
        $form    = new Xhelp\Form(_AM_XHELP_ADD_FIELD, 'add_field', Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'managefields']));
343
        $nameEle = new \XoopsFormText(_AM_XHELP_TEXT_NAME, 'fld_name', 30, 64, $fld_name);
344
        $nameEle->setDescription(_AM_XHELP_TEXT_NAME_DESC);
345
        $form->addElement($nameEle);
346
347
        $fieldnameEle = new \XoopsFormText(_AM_XHELP_TEXT_FIELDNAME, 'fld_fieldname', 30, 64, $fld_fieldname);
348
        $fieldnameEle->setDescription(_AM_XHELP_TEXT_FIELDNAME_DESC);
349
        $form->addElement($fieldnameEle);
350
351
        $descriptionEle = new \XoopsFormTextArea(_AM_XHELP_TEXT_DESCRIPTION, 'fld_description', $fld_description, 5, 60);
352
        $descriptionEle->setDescription(_AM_XHELP_TEXT_DESCRIPTION_DESC);
353
        $form->addElement($descriptionEle);
354
355
        $dept_select->setDescription(_AM_XHELP_TEXT_DEPT_DESC);
356
        $control_select->setDescription(_AM_XHELP_TEXT_CONTROLTYPE_DESC);
357
        $datatype_select->setDescription(_AM_XHELP_TEXT_DATATYPE_DESC);
358
359
        $form->addElement($dept_select);
360
        $form->addElement($control_select);
361
        $form->addElement($datatype_select);
362
363
        $required = new \XoopsFormRadioYN(_AM_XHELP_TEXT_REQUIRED, 'fld_required', $fld_required);
364
        $required->setDescription(_AM_XHELP_TEXT_REQUIRED_DESC);
365
        $form->addElement($required);
366
367
        $lengthEle = new \XoopsFormText(_AM_XHELP_TEXT_LENGTH, 'fld_length', 5, 5, $fld_length);
368
        $lengthEle->setDescription(_AM_XHELP_TEXT_LENGTH_DESC);
369
        $weightEle = new \XoopsFormText(_AM_XHELP_TEXT_WEIGHT, 'fld_weight', 5, 5, $fld_weight);
370
        $weightEle->setDescription(_AM_XHELP_TEXT_WEIGHT_DESC);
371
372
        $form->addElement($lengthEle);
373
        $form->addElement($weightEle);
374
375
        $regex_control = new Xhelp\FormRegex(_AM_XHELP_TEXT_VALIDATION, 'fld_valid', $fld_validation);
376
        $regex_control->addOptionArray($regex_array);
377
        $regex_control->setDescription(_AM_XHELP_TEXT_VALIDATION_DESC);
378
379
        $form->addElement($regex_control);
380
381
        $defaultValueEle = new \XoopsFormText(_AM_XHELP_TEXT_DEFAULTVALUE, 'fld_defaultvalue', 30, 100, $fld_defaultvalue);
382
        $defaultValueEle->setDescription(_AM_XHELP_TEXT_DEFAULTVALUE_DESC);
383
        $form->addElement($defaultValueEle);
384
        $values = new \XoopsFormTextArea(_AM_XHELP_TEXT_FIELDVALUES, 'fld_values', $fld_values, 5, 60);
385
        $values->setDescription(_AM_XHELP_TEXT_FIELDVALUES_DESC);
386
        $form->addElement($values);
387
388
        $btn_tray = new \XoopsFormElementTray('');
389
        $btn_tray->addElement(new \XoopsFormButton('', 'addField', _SUBMIT, 'submit'));
390
391
        $form->addElement($btn_tray);
392
        echo $form->render();
393
394
        require_once __DIR__ . '/admin_footer.php';
395
    }
396
}
397
398
/**
399
 * @param array $values_arr
400
 * @return string
401
 */
402
function formatValues(array $values_arr): string
403
{
404
    $ret = '';
405
    foreach ($values_arr as $key => $value) {
406
        $ret .= "$key=$value\r\n";
407
    }
408
409
    return $ret;
410
}
411
412
/**
413
 * @param string $raw_values
414
 * @return array
415
 */
416
function &parseValues(string $raw_values): array
417
{
418
    $_inValue = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $_inValue is dead and can be removed.
Loading history...
419
    $values   = [];
420
421
    if ('' === $raw_values) {
422
        return $values;
423
    }
424
425
    //Split values into name/value pairs
426
    $lines = explode("\r\n", $raw_values);
427
428
    //Parse each line into name=value
429
    foreach ($lines as $line) {
430
        if ('' === trim($line)) {
431
            continue;
432
        }
433
        $name     = $value = '';
434
        $_inValue = false;
435
        $chrs     = mb_strlen($line);
436
        for ($i = 0; $i <= $chrs; ++$i) {
437
            $chr = mb_substr($line, $i, 1);
438
            if ('=' === $chr && !$_inValue) {
439
                $_inValue = true;
440
            } elseif ($_inValue) {
441
                $name .= $chr;
442
            } else {
443
                $value .= $chr;
444
            }
445
        }
446
        //Add value to array
447
        if ('' === $value) {
448
            $values[$name] = $name;
449
        } else {
450
            $values[$value] = $name;
451
        }
452
453
        //Reset name / value vars
454
        $name = $value = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
455
    }
456
457
    return $values;
458
}
459
460
/**
461
 *
462
 */
463
function deleteField()
464
{
465
    global $eventService;
466
    $helper = Xhelp\Helper::getInstance();
467
    if (!isset($_REQUEST['id'])) {
468
        redirect_header(Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'manageDepartments'], false), 3, _AM_XHELP_MESSAGE_NO_FIELD);
0 ignored issues
show
Bug introduced by
The constant XHELP_ADMIN_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
469
    }
470
471
    $id = Request::getInt('id', 0, 'REQUEST');
472
473
    if (isset($_POST['ok'])) {
474
        /** @var \XoopsModules\Xhelp\TicketFieldHandler $ticketFieldHandler */
475
        $ticketFieldHandler = $helper->getHandler('TicketField');
476
        $ticketField        = $ticketFieldHandler->get($id);
477
        if ($ticketFieldHandler->delete($ticketField, true)) {
478
            $eventService->trigger('delete_field', [&$ticketField]);
479
            redirect_header(Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'manageFields'], false));
480
        }
481
482
        redirect_header(Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'manageFields'], false), 3, $message);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message seems to be never defined.
Loading history...
483
    } else {
484
        xoops_cp_header();
485
        //echo $oAdminButton->renderButtons('manfields');
486
        $adminObject = Admin::getInstance();
487
        $adminObject->displayNavigation(basename(__FILE__));
488
489
        xoops_confirm(['op' => 'delfield', 'id' => $id, 'ok' => 1], XHELP_ADMIN_URL . '/fields.php', sprintf(_AM_XHELP_MSG_FIELD_DEL_CFRM, $id));
490
        xoops_cp_footer();
491
    }
492
}
493
494
/**
495
 *
496
 */
497
function editField()
498
{
499
    $eventsrv    = Xhelp\EventService::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $eventsrv is dead and can be removed.
Loading history...
500
    $session     = Xhelp\Session::getInstance();
501
    $regex_array = getRegexArray();
502
    $helper      = Helper::getInstance();
503
504
    if (!isset($_REQUEST['id'])) {
505
        redirect_header(Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'manageDepartments'], false), 3, _AM_XHELP_MESSAGE_NO_FIELD);
0 ignored issues
show
Bug introduced by
The constant XHELP_ADMIN_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
506
    }
507
508
    $fld_id = Request::getInt('id', 0, 'REQUEST');
509
    /** @var \XoopsModules\Xhelp\TicketFieldHandler $ticketFieldHandler */
510
    $ticketFieldHandler = $helper->getHandler('TicketField');
511
    if (!$ticketField = $ticketFieldHandler->get($fld_id)) {
512
        redirect_header(Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'manageDepartments'], false), 3, _AM_XHELP_MESSAGE_NO_FIELD);
513
    }
514
515
    if (isset($_POST['editField'])) {
516
        //Validate Field Information
517
        $has_errors = false;
518
        $errors     = [];
519
        $values     = parseValues($_POST['fld_values']);
520
521
        if (!$control = xhelpGetControl($_POST['fld_controltype'])) {
522
            $has_errors                  = true;
523
            $errors['fld_controltype'][] = _AM_XHELP_VALID_ERR_CONTROLTYPE;
524
        }
525
526
        $fld_needslength = $control['needs_length'];
527
        $fld_needsvalues = $control['needs_values'];
528
529
        //name field filled?
530
        if ('' === trim(\Xmf\Request::getString('fld_name', '', 'POST'))) {
531
            $has_errors           = true;
532
            $errors['fld_name'][] = _AM_XHELP_VALID_ERR_NAME;
533
        }
534
535
        //fieldname filled
536
        if ('' === trim(\Xmf\Request::getString('fld_fieldname', '', 'POST'))) {
537
            $has_errors                = true;
538
            $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME;
539
        }
540
541
        //fieldname unique?
542
        $criteria = new \CriteriaCompo(new \Criteria('id', (string)$fld_id, '!='));
543
        $criteria->add(new \Criteria('fieldname', \Xmf\Request::getString('fld_fieldname', '', 'POST')));
544
        if ($ticketFieldHandler->getCount($criteria)) {
545
            $has_errors                = true;
546
            $errors['fld_fieldname'][] = _AM_XHELP_VALID_ERR_FIELDNAME_UNIQUE;
547
        }
548
549
        //Length filled
550
        if (0 == Request::getInt('fld_length', 0, 'POST') && true === $fld_needslength) {
551
            $has_errors             = true;
552
            $errors['fld_length'][] = sprintf(_AM_XHELP_VALID_ERR_LENGTH, _XHELP_FIELD_MINLEN, _XHELP_FIELD_MAXLEN);
553
        }
554
555
        //default value in value set?
556
        if (count($values)) {
557
            if (!in_array($_POST['fld_defaultvalue'], $values)
558
                && !array_key_exists($_POST['fld_defaultvalue'], $values)) {
559
                $has_errors                   = true;
560
                $errors['fld_defaultvalue'][] = _AM_XHELP_VALID_ERR_DEFAULTVALUE;
561
            }
562
563
            //length larger than longest value?
564
            $length = Request::getInt('fld_length', 0, 'POST');
565
            foreach ($values as $key => $value) {
566
                if (mb_strlen($key) > $length) {
567
                    $has_errors             = true;
568
                    $errors['fld_values'][] = sprintf(_AM_XHELP_VALID_ERR_VALUE_LENGTH, htmlentities($key, ENT_QUOTES | ENT_HTML5), $length);
569
                }
570
            }
571
        } elseif ($fld_needsvalues) {
572
            $has_errors             = true;
573
            $errors['fld_values'][] = _AM_XHELP_VALID_ERR_VALUE;
574
        }
575
576
        if ($has_errors) {
577
            $afield                 = [];
578
            $afield['name']         = \Xmf\Request::getString('fld_name', '', 'POST');
579
            $afield['description']  = \Xmf\Request::getString('fld_description', '', 'POST');
580
            $afield['fieldname']    = \Xmf\Request::getString('fld_fieldname', '', 'POST');
581
            $afield['departments']  = $_POST['fld_departments'];
582
            $afield['controltype']  = $_POST['fld_controltype'];
583
            $afield['datatype']     = $_POST['fld_datatype'];
584
            $afield['required']     = $_POST['fld_required'];
585
            $afield['weight']       = \Xmf\Request::getInt('fld_weight', 0, 'POST');
586
            $afield['defaultvalue'] = \Xmf\Request::getInt('fld_defaultvalue', 0, 'POST');
587
            $afield['values']       = $_POST['fld_values'];
588
            $afield['length']       = \Xmf\Request::getInt('fld_length', 0, 'POST');
589
            $afield['validation']   = ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox']);
590
            $session->set('xhelp_editField_' . $fld_id, $afield);
591
            $session->set('xhelp_editFieldErrors_' . $fld_id, $errors);
592
            //Redirect to edit page (display errors);
593
            redirect_header(Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'editfield', 'id' => $fld_id], false));
594
        }
595
        //Store Modified Field info
596
597
        $ticketField->setVar('name', \Xmf\Request::getString('fld_name', '', 'POST'));
598
        $ticketField->setVar('description', \Xmf\Request::getString('fld_description', '', 'POST'));
599
        $ticketField->setVar('fieldname', \Xmf\Request::getString('fld_fieldname', '', 'POST'));
600
        $ticketField->setVar('controltype', $_POST['fld_controltype']);
601
        $ticketField->setVar('datatype', $_POST['fld_datatype']);
602
        $ticketField->setVar('fieldlength', \Xmf\Request::getInt('fld_length', 0, 'POST'));
603
        $ticketField->setVar('required', $_POST['fld_required']);
604
        $ticketField->setVar('weight', \Xmf\Request::getInt('fld_weight', 0, 'POST'));
605
        $ticketField->setVar('defaultvalue', $_POST['fld_defaultvalue']);
606
        $ticketField->setVar('validation', ($_POST['fld_valid_select'] == $_POST['fld_valid_txtbox'] ? $_POST['fld_valid_select'] : $_POST['fld_valid_txtbox']));
607
        $ticketField->setValues($values);
0 ignored issues
show
Bug introduced by
The method setValues() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as XoopsModules\Xhelp\TicketField. ( Ignorable by Annotation )

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

607
        $ticketField->/** @scrutinizer ignore-call */ 
608
                      setValues($values);
Loading history...
608
        $ticketField->addDepartments($_POST['fld_departments']);
0 ignored issues
show
Bug introduced by
The method addDepartments() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as XoopsModules\Xhelp\TicketField. ( Ignorable by Annotation )

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

608
        $ticketField->/** @scrutinizer ignore-call */ 
609
                      addDepartments($_POST['fld_departments']);
Loading history...
609
610
        if ($ticketFieldHandler->insert($ticketField)) {
611
            clearEditSessionVars($fld_id);
612
            $helper->redirect('admin/fields.php', 3, _AM_XHELP_MSG_FIELD_UPD_OK);
613
        } else {
614
            redirect_header(Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'editfield', 'id' => $fld_id], false), 3, _AM_XHELP_MSG_FIELD_UPD_ERR);
615
        }
616
    } else {
617
        //Get Custom Field From session (if exists)
618
        $field_info   = $session->get('xhelp_editField_' . $fld_id);
619
        $field_errors = $session->get('xhelp_editFieldErrors_' . $fld_id);
620
621
        if (false === !$field_info) {
622
            $fld_controltype  = $field_info['controltype'];
623
            $fld_datatype     = $field_info['datatype'];
624
            $fld_departments  = $field_info['departments'];
625
            $fld_name         = $field_info['name'];
626
            $fld_fieldname    = $field_info['fieldname'];
627
            $fld_description  = $field_info['description'];
628
            $fld_required     = $field_info['required'];
629
            $fld_length       = $field_info['length'];
630
            $fld_weight       = $field_info['weight'];
631
            $fld_defaultvalue = $field_info['defaultvalue'];
632
            $fld_values       = $field_info['values'];
633
            $fld_validation   = $field_info['validation'];
634
        } else {
635
            /** @var \XoopsModules\Xhelp\TicketFieldDepartmentHandler $ticketFieldDepartmentHandler */
636
            $ticketFieldDepartmentHandler = $helper->getHandler('TicketFieldDepartment');
637
            $depts                        = $ticketFieldDepartmentHandler->departmentsByField($ticketField->getVar('id'), true);
638
639
            $fld_controltype  = $ticketField->getVar('controltype');
640
            $fld_datatype     = $ticketField->getVar('datatype');
641
            $fld_departments  = array_keys($depts);
642
            $fld_name         = $ticketField->getVar('name');
643
            $fld_fieldname    = $ticketField->getVar('fieldname');
644
            $fld_description  = $ticketField->getVar('description');
645
            $fld_required     = $ticketField->getVar('required');
646
            $fld_length       = $ticketField->getVar('fieldlength');
647
            $fld_weight       = $ticketField->getVar('weight');
648
            $fld_defaultvalue = $ticketField->getVar('defaultvalue');
649
            $fld_values       = formatValues($ticketField->getVar('fieldvalues'));
0 ignored issues
show
Bug introduced by
It seems like $ticketField->getVar('fieldvalues') can also be of type boolean and null and string; however, parameter $values_arr of formatValues() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

649
            $fld_values       = formatValues(/** @scrutinizer ignore-type */ $ticketField->getVar('fieldvalues'));
Loading history...
650
            $fld_validation   = $ticketField->getVar('validation');
651
        }
652
653
        //Display Field modification
654
        xoops_cp_header();
655
        //echo $oAdminButton->renderButtons('manfields');
656
        $adminObject = Admin::getInstance();
657
        $adminObject->displayNavigation(basename(__FILE__));
658
659
        //Edit Field Form
660
661
        $controls       = xhelpGetControlArray();
662
        $control_select = new \XoopsFormSelect(_AM_XHELP_TEXT_CONTROLTYPE, 'fld_controltype', $fld_controltype);
663
        $control_select->setDescription(_AM_XHELP_TEXT_CONTROLTYPE_DESC);
664
        foreach ($controls as $key => $control) {
665
            $control_select->addOption($key, $control['label']);
666
        }
667
668
        $datatypes = [
669
            _XHELP_DATATYPE_TEXT       => _XHELP_DATATYPE_TEXT,
670
            _XHELP_DATATYPE_NUMBER_INT => _XHELP_DATATYPE_NUMBER_INT,
671
            _XHELP_DATATYPE_NUMBER_DEC => _XHELP_DATATYPE_NUMBER_DEC,
672
        ];
673
674
        $datatype_select = new \XoopsFormSelect(_AM_XHELP_TEXT_DATATYPE, 'fld_datatype', $fld_datatype);
675
        $datatype_select->setDescription(_AM_XHELP_TEXT_DATATYPE_DESC);
676
        $datatype_select->addOptionArray($datatypes);
677
678
        /** @var \XoopsModules\Xhelp\DepartmentHandler $departmentHandler */
679
        $departmentHandler = $helper->getHandler('Department');
680
        $depts             = $departmentHandler->getObjects();
681
        $dept_select       = new \XoopsFormSelect(_AM_XHELP_TEXT_DEPARTMENTS, 'fld_departments', $fld_departments, 5, true);
682
        $dept_select->setDescription(_AM_XHELP_TEXT_DEPT_DESC);
683
        foreach ($depts as $obj) {
684
            $dept_select->addOption($obj->getVar('id'), $obj->getVar('department'));
685
        }
686
        unset($depts);
687
688
        if (false === !$field_errors) {
689
            xhelpRenderErrors($field_errors, Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'clearEditSession', 'id' => $fld_id]));
0 ignored issues
show
Bug introduced by
It seems like $field_errors can also be of type boolean and string; however, parameter $err_arr of xhelpRenderErrors() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

689
            xhelpRenderErrors(/** @scrutinizer ignore-type */ $field_errors, Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'clearEditSession', 'id' => $fld_id]));
Loading history...
690
        }
691
692
        $form = new Xhelp\Form(
693
            _AM_XHELP_EDIT_FIELD, 'edit_field', Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', [
694
            'op' => 'editfield',
695
            'id' => $fld_id,
696
        ])
697
        );
698
699
        $nameEle = new \XoopsFormText(_AM_XHELP_TEXT_NAME, 'fld_name', 30, 64, $fld_name);
700
        $nameEle->setDescription(_AM_XHELP_TEXT_NAME_DESC);
701
        $form->addElement($nameEle);
702
703
        $fieldnameEle = new \XoopsFormText(_AM_XHELP_TEXT_FIELDNAME, 'fld_fieldname', 30, 64, $fld_fieldname);
704
        $fieldnameEle->setDescription(_AM_XHELP_TEXT_FIELDNAME_DESC);
705
        $form->addElement($fieldnameEle);
706
707
        $descriptionEle = new \XoopsFormTextArea(_AM_XHELP_TEXT_DESCRIPTION, 'fld_description', $fld_description, 5, 60);
708
        $descriptionEle->setDescription(_AM_XHELP_TEXT_DESCRIPTION_DESC);
709
        $form->addElement($descriptionEle);
710
711
        $form->addElement($dept_select);
712
        $form->addElement($control_select);
713
        $form->addElement($datatype_select);
714
715
        $required = new \XoopsFormRadioYN(_AM_XHELP_TEXT_REQUIRED, 'fld_required', $fld_required);
716
        $required->setDescription(_AM_XHELP_TEXT_REQUIRED_DESC);
717
        $form->addElement($required);
718
719
        $lengthEle = new \XoopsFormText(_AM_XHELP_TEXT_LENGTH, 'fld_length', 5, 5, $fld_length);
720
        $lengthEle->setDescription(_AM_XHELP_TEXT_LENGTH_DESC);
721
        $form->addElement($lengthEle);
722
723
        $widthEle = new \XoopsFormText(_AM_XHELP_TEXT_WEIGHT, 'fld_weight', 5, 5, $fld_weight);
724
        $widthEle->setDescription(_AM_XHELP_TEXT_WEIGHT_DESC);
725
        $form->addElement($widthEle);
726
727
        $regex_control = new Xhelp\FormRegex(_AM_XHELP_TEXT_VALIDATION, 'fld_valid', $fld_validation);
728
        $regex_control->setDescription(_AM_XHELP_TEXT_VALIDATION_DESC);
729
        $regex_control->addOptionArray($regex_array);
730
731
        $form->addElement($regex_control);
732
733
        $defaultValueEle = new \XoopsFormText(_AM_XHELP_TEXT_DEFAULTVALUE, 'fld_defaultvalue', 30, 100, $fld_defaultvalue);
734
        $defaultValueEle->setDescription(_AM_XHELP_TEXT_DEFAULTVALUE_DESC);
735
        $form->addElement($defaultValueEle);
736
        $values = new \XoopsFormTextArea(_AM_XHELP_TEXT_FIELDVALUES, 'fld_values', $fld_values, 5, 60);
737
        $values->setDescription(_AM_XHELP_TEXT_FIELDVALUES_DESC);
738
        $form->addElement($values);
739
740
        $btn_tray = new \XoopsFormElementTray('');
741
        $btn_tray->addElement(new \XoopsFormButton('', 'editField', _SUBMIT, 'submit'));
742
        $btn_tray->addElement(new \XoopsFormButton('', 'cancel', _CANCEL));
743
        $btn_tray->addElement(new \XoopsFormHidden('id', (string)$fld_id));
744
745
        $form->addElement($btn_tray);
746
        echo $form->render();
747
748
        require_once __DIR__ . '/admin_footer.php';
749
    }
750
}
751
752
/**
753
 * @return array
754
 */
755
function &getRegexArray(): array
756
{
757
    $regex_array = [
758
        ''                                                       => _AM_XHELP_TEXT_REGEX_CUSTOM,
759
        '^\d{3}-\d{3}-\d{4}$'                                    => _AM_XHELP_TEXT_REGEX_USPHONE,
760
        '^\d{5}(-\d{4})?'                                        => _AM_XHELP_TEXT_REGEX_USZIP,
761
        '^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}$' => _AM_XHELP_TEXT_REGEX_EMAIL,
762
    ];
763
764
    return $regex_array;
765
}
766
767
/**
768
 *
769
 */
770
function setFieldRequired()
771
{
772
    $helper      = Helper::getInstance();
773
    $setRequired = Request::getInt('setrequired', 0, 'GET');
774
    $id          = Request::getInt('id', 0, 'GET');
775
776
    $setRequired = (0 != $setRequired ? 1 : 0);
777
778
    /** @var \XoopsModules\Xhelp\TicketFieldHandler $ticketFieldHandler */
779
    $ticketFieldHandler = $helper->getHandler('TicketField');
780
781
    $ticketField = $ticketFieldHandler->get($id);
782
    if ($ticketField) {
0 ignored issues
show
introduced by
$ticketField is of type XoopsObject, thus it always evaluated to true.
Loading history...
783
        $ticketField->setVar('required', $setRequired);
784
        $ret = $ticketFieldHandler->insert($ticketField, true);
785
        if ($ret) {
786
            $helper->redirect('admin/fields.php');
787
        } else {
788
            $helper->redirect('admin/fields.php', 3, _AM_XHELP_MSG_FIELD_UPD_ERR);
789
        }
790
    } else {
791
        $helper->redirect('admin/fields.php', 3, _AM_XHELP_MESSAGE_NO_FIELD);
792
    }
793
}
794
795
/**
796
 *
797
 */
798
function clearAddSession()
799
{
800
    $helper = Helper::getInstance();
801
    clearAddSessionVars();
802
    $helper->redirect('admin/fields.php');
803
}
804
805
/**
806
 *
807
 */
808
function clearEditSession()
809
{
810
    $fieldid = \Xmf\Request::getInt('id', 0, 'REQUEST');
811
    clearEditSessionVars($fieldid);
812
    redirect_header(Xhelp\Utility::createURI(XHELP_ADMIN_URL . '/fields.php', ['op' => 'editfield', 'id' => $fieldid], false));
0 ignored issues
show
Bug introduced by
The constant XHELP_ADMIN_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
813
}
814
815
/**
816
 *
817
 */
818
function clearAddSessionVars()
819
{
820
    $session = Xhelp\Session::getInstance();
821
    $session->del('xhelp_addField');
822
    $session->del('xhelp_addFieldErrors');
823
}
824
825
/**
826
 * @param int $id
827
 */
828
function clearEditSessionVars(int $id)
829
{
830
    $id      = $id;
831
    $session = Xhelp\Session::getInstance();
832
    $session->del("xhelp_editField_$id");
833
    $session->del("xhelp_editFieldErrors_$id");
834
}
835