Passed
Pull Request — master (#7)
by Michael
29:11 queued 12:55
created

addTicket.php (1 issue)

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\Request;
22
use XoopsModules\Xhelp\{
23
    EventService,
24
    DepartmentHandler,
25
    Helper,
26
    StaffHandler,
27
    TicketHandler,
28
    Utility,
29
    Validation
30
};
31
32
/** @var DepartmentHandler $departmentHandler */
33
/** @var Helper $helper */
34
35
require_once __DIR__ . '/header.php';
36
require_once XHELP_INCLUDE_PATH . '/events.php';
37
38
$helper = Helper::getInstance();
39
40
global $xoopsUser, $xhelp_isStaff, $xoopsTpl, $session, $staff, $xhelp_module_header, $allowed_mimetypes, $xoopsUserIsAdmin, $eventService, $xoopsModule;
41
42
if (Request::hasVar('deptid', 'GET')) {
43
    $dept_id = Request::getInt('deptid', 0, 'GET');
44
}
45
46
if (Request::hasVar('view_id', 'GET')) {
47
    $view_id = Request::getInt('view_id', 0, 'GET');
48
    setcookie('xhelp_logMode', (string)$view_id, time() + 60 * 60 * 24 * 30);
49
    if (isset($dept_id)) {
50
        $helper->redirect("addTicket.php&deptid=$dept_id");
51
    } else {
52
        $helper->redirect('addTicket.php');
53
    }
54
} else {
55
    if (isset($_COOKIE['xhelp_logMode'])) {
56
        setcookie('xhelp_logMode', $_COOKIE['xhelp_logMode'], time() + 60 * 60 * 24 * 30);
57
    } else {
58
        setcookie('xhelp_logMode', '1', time() + 60 * 60 * 24 * 30);
59
    }
60
}
61
62
$eventService = new EventService();
63
64
/*
65
$eventService->advise('new_ticket', xhelp_notificationService::getInstance());
66
$eventService->advise('new_ticket', xhelp_logService::getInstance());
67
$eventService->advise('new_ticket', xhelp_cacheService::getInstance());
68
$eventService->advise('new_response', xhelp_logService::getInstance());
69
$eventService->advise('new_response', xhelp_notificationService::getInstance());
70
$eventService->advise('update_owner', xhelp_notificationService::getInstance());
71
$eventService->advise('update_owner', xhelp_logService::getInstance());
72
*/
73
74
/** @var TicketHandler $ticketHandler */
75
/** @var \XoopsModules\Xhelp\TicketHandler $ticketHandler */
76
$ticketHandler = $helper->getHandler('Ticket');
77
/** @var StaffHandler $staffHandler */
78
/** @var \XoopsModules\Xhelp\StaffHandler $staffHandler */
79
$staffHandler = $helper->getHandler('Staff');
80
/** @var \XoopsGroupPermHandler $grouppermHandler */
81
$grouppermHandler = xoops_getHandler('groupperm');
82
/** @var \XoopsMemberHandler $memberHandler */
83
$memberHandler = xoops_getHandler('member');
84
/** @var \XoopsModules\Xhelp\MembershipHandler $membershipHandler */
85
$membershipHandler = $helper->getHandler('Membership');
86
/** @var \XoopsModules\Xhelp\TicketFieldDepartmentHandler $ticketFieldDepartmentHandler */
87
$ticketFieldDepartmentHandler = $helper->getHandler('TicketFieldDepartment');
88
89
$module_id = $xoopsModule->getVar('mid');
90
91
if ($xoopsUser) {
92
    if (!isset($dept_id)) {
93
        $dept_id = (int)Utility::getMeta('default_department');
94
    }
95
96
    if (Request::hasVar('saveTicket', 'GET') && 1 === $_GET['saveTicket']) {
97
        saveTicket();
98
    }
99
100
    //    if (!isset($_POST['addTicket'])) {                           // Initial load of page
101
    if (Request::hasVar('addTicket', 'POST')) {
102
        $dept_id = Request::getInt('departments', 0, 'POST');
103
104
        // require_once XHELP_CLASS_PATH . '/validator.php';
105
        $v                  = [];
106
        $v['subject'][]     = new Validation\ValidateLength(Request::getString('subject', '', 'POST'), 2, 255);
107
        $v['description'][] = new Validation\ValidateLength(Request::getString('description', '', 'POST'), 2);
108
109
        // Get current dept's custom fields
110
        $fields  = $ticketFieldDepartmentHandler->fieldsByDepartment($dept_id, true);
111
        $aFields = [];
112
113
        foreach ($fields as $field) {
114
            $values = $field->getVar('fieldvalues');
115
            if (XHELP_CONTROL_YESNO == $field->getVar('controltype')) {
116
                $values = [1 => _YES, 0 => _NO];
117
            }
118
            $fieldname = $field->getVar('fieldname');
119
120
            if (XHELP_CONTROL_FILE != $field->getVar('controltype')) {
121
                $checkField = Request::getString('fieldname', '', 'POST'); //array?
122
            } else {
123
                $checkField = $_FILES[$fieldname];
124
            }
125
126
            $v[$fieldname][] = new Validation\ValidateRegex($checkField, $field->getVar('validation'), $field->getVar('required'));
127
128
            $aFields[$field->getVar('id')] = [
129
                'name'         => $field->getVar('name'),
130
                'desc'         => $field->getVar('description'),
131
                'fieldname'    => $field->getVar('fieldname'),
132
                'defaultvalue' => $field->getVar('defaultvalue'),
133
                'controltype'  => $field->getVar('controltype'),
134
                'required'     => $field->getVar('required'),
135
                'fieldlength'  => $field->getVar('fieldlength'),
136
                'maxlength'    => $field->getVar('fieldlength') < 50 ? $field->getVar('fieldlength') : 50,
137
                'weight'       => $field->getVar('weight'),
138
                'fieldvalues'  => $values,
139
                'validation'   => $field->getVar('validation'),
140
            ];
141
        }
142
143
        saveTicket($aFields);      // Save ticket information in a session
144
145
        // Perform each validation
146
        $fields = [];
147
        $errors = [];
148
        foreach ($v as $fieldname => $validator) {
149
            if (Utility::checkRules($validator, $errors)) {
150
                $fields[$fieldname]['haserrors'] = false;
151
            } else {
152
                //Mark field with error
153
                $fields[$fieldname]['haserrors'] = true;
154
                $fields[$fieldname]['errors']    = $errors;
155
            }
156
        }
157
158
        if (!empty($errors)) {
159
            $session->set('xhelp_validateError', $fields);
160
            $message = _XHELP_MESSAGE_VALIDATE_ERROR;
161
            $helper->redirect('addTicket.php');
162
        }
163
164
        //        /** @var \XoopsModules\Xhelp\TicketHandler $ticketHandler */
165
        $ticketHandler = $helper->getHandler('Ticket');
166
        /** @var \XoopsModules\Xhelp\Ticket $ticket */
167
        $ticket = $ticketHandler->create();
168
        $ticket->setVar('uid', Request::getInt('user_id', 0, 'POST'));
169
        $ticket->setVar('subject', Request::getString('subject', '', 'POST'));
170
        $ticket->setVar('description', Request::getString('description', '', 'POST'));
171
        $ticket->setVar('department', $dept_id);
172
        $ticket->setVar('priority', Request::getInt('priority', 0, 'POST'));
173
        if ($xhelp_isStaff && 2 === $_COOKIE['xhelp_logMode']) {
174
            $ticket->setVar('status', Request::getInt('status', 0, 'POST'));    // Set status
175
            //            if (Request::hasVar('owner', 'POST')) {  //Check if user claimed ownership
176
            if (!Request::hasVar('owner', 'POST')) {
177
                if (Request::getInt('owner', 0, 'POST') > 0) {
178
                    $oldOwner = 0;
179
                    $session->set('xhelp_oldOwner', $oldOwner);
180
                    $ticket->setVar('ownership', Request::getInt('owner', 0, 'POST'));
181
                    $session->set('xhelp_changeOwner', true);
182
                }
183
            }
184
            $session->set('xhelp_ticket_ownership', Request::getInt('owner', 0, 'POST'));  // Store in session
185
        } else {
186
            $ticket->setVar('status', 1);
187
        }
188
        $ticket->setVar('posted', time());
189
        $ticket->setVar('userIP', getenv('REMOTE_ADDR'));
190
        $ticket->setVar('overdueTime', $ticket->getVar('posted') + ($helper->getConfig('xhelp_overdueTime') * 60 * 60));
191
192
        $aUploadFiles = [];
193
        if ($helper->getConfig('xhelp_allowUpload')) {
194
            foreach ($_FILES as $key => $aFile) {
195
                $pos = mb_strpos($key, 'userfile');
196
                if (false !== $pos
197
                    && is_uploaded_file($aFile['tmp_name'])) {     // In the userfile array and uploaded file?
198
                    if (false !== ($ret = $ticket->checkUpload($key, $allowed_mimetypes, $errors))) {
199
                        $aUploadFiles[$key] = $aFile;
200
                    } else {
201
                        $errorstxt = implode('<br>', $errors);
202
                        $message   = sprintf(_XHELP_MESSAGE_FILE_ERROR, $errorstxt);
203
                        $helper->redirect('addTicket.php', 5, $message);
204
                    }
205
                }
206
            }
207
        }
208
209
        if ($ticketHandler->insert($ticket)) {
210
            /** @var \XoopsMemberHandler $memberHandler */
211
            $memberHandler = xoops_getHandler('member');
212
            $newUser       = $memberHandler->getUser($ticket->getVar('uid'));
213
            $ticket->addSubmitter($newUser->getVar('email'), $newUser->getVar('uid'));
214
215
            if (count($aUploadFiles) > 0) {   // Has uploaded files?
216
                foreach ($aUploadFiles as $key => $aFile) {
217
                    $file = $ticket->storeUpload($key, null, $allowed_mimetypes);
218
                    $eventService->trigger('new_file', [&$ticket, &$file]);
219
                }
220
            }
221
222
            // Add custom field values to db
223
            /** @var \XoopsModules\Xhelp\TicketValuesHandler $ticketValuesHandler */
224
            $ticketValuesHandler = $helper->getHandler('TicketValues');
225
            /** @var \XoopsModules\Xhelp\TicketValues $ticketValues */
226
            $ticketValues = $ticketValuesHandler->create();
227
228
            foreach ($aFields as $field) {
229
                $fieldname = $field['fieldname'];
230
                $fieldtype = $field['controltype'];
231
232
                if (XHELP_CONTROL_FILE == $fieldtype) {               // If custom field was a file upload
233
                    if ($helper->getConfig('xhelp_allowUpload')) {    // If uploading is allowed
234
                        if (is_uploaded_file(($_FILES[$fieldname]['tmp_name'])??'')) {
235
                            if (!$ret = $ticket->checkUpload($fieldname, $allowed_mimetypes, $errors)) {
236
                                $errorstxt = implode('<br>', $errors);
237
238
                                $message = sprintf(_XHELP_MESSAGE_FILE_ERROR, $errorstxt);
239
                                $helper->redirect('addTicket.php', 5, $message);
240
                            }
241
                            if (false !== ($file = $ticket->storeUpload($fieldname, -1, $allowed_mimetypes))) {
242
                                $ticketValues->setVar($fieldname, $file->getVar('id') . '_' . $_FILES[$fieldname]['name']);
243
                            }
244
                        }
245
                    }
246
                } else {
247
                    $fieldvalue = Request::getString($fieldname, '', 'POST');
248
                    $ticketValues->setVar($fieldname, $fieldvalue);
249
                }
250
            }
251
            $ticketValues->setVar('ticketid', $ticket->getVar('id'));
252
253
            if (!$ticketValuesHandler->insert($ticketValues)) {
254
                $message = _XHELP_MESSAGE_NO_CUSTFLD_ADDED;
255
            }
256
257
            $eventService->trigger('new_ticket', [&$ticket]);
258
259
            if ($session->get('xhelp_changeOwner')) {
260
                $oldOwner = $session->get('xhelp_oldOwner');
261
                $eventService->trigger('update_owner', [&$ticket, $oldOwner, $xoopsUser->getVar('uid')]);
262
                $session->del('xhelp_changeOwner');
263
                $session->del('xhelp_oldOwner');
264
                $session->del('xhelp_ticket_ownership');
265
            }
266
267
            // Add response
268
            if ($xhelp_isStaff
269
                && 2 === $_COOKIE['xhelp_logMode']) {     // Make sure user is a staff member and is using advanced form
270
                // if ('' != $_POST['response']) {                   // Don't run if no value for response
271
                if (Request::hasVar('response', 'POST')) {
272
                    /** @var \XoopsModules\Xhelp\ResponseHandler $responseHandler */
273
                    $responseHandler = $helper->getHandler('Response');
274
                    /** @var \XoopsModules\Xhelp\Response $newResponse */
275
                    $newResponse = $responseHandler->create();
276
                    $newResponse->setVar('uid', $xoopsUser->getVar('uid'));
277
                    $newResponse->setVar('ticketid', $ticket->getVar('id'));
278
                    $newResponse->setVar('message', Request::getString('response', '', 'POST'));
279
                    $newResponse->setVar('timeSpent', Request::getInt('timespent', 0, 'POST'));
280
                    $newResponse->setVar('updateTime', $ticket->getVar('posted'));
281
                    $newResponse->setVar('userIP', $ticket->getVar('userIP'));
282
                    if (Request::hasVar('private', 'POST')) {
283
                        $newResponse->setVar('private', Request::getInt('private', 0, 'POST'));
284
                    }
285
                    if ($responseHandler->insert($newResponse)) {
286
                        $eventService->trigger('new_response', [&$ticket, &$newResponse]);
287
                        $session->del('xhelp_response');
288
                    }
289
                }
290
            }
291
292
            $session->del('xhelp_ticket');
293
            $session->del('xhelp_validateError');
294
            $session->del('xhelp_custFields');
295
296
            $message = _XHELP_MESSAGE_ADDTICKET;
297
        } else {
298
            //$session->set('xhelp_ticket', $ticket);
299
            $message = _XHELP_MESSAGE_ADDTICKET_ERROR . $ticket->getHtmlErrors();     // Unsuccessfully added new ticket
300
        }
301
        $helper->redirect('index.php', 5, $message);
302
    } else {
303
        $GLOBALS['xoopsOption']['template_main'] = 'xhelp_addTicket.tpl';             // Always set main template before including the header
304
        require_once XOOPS_ROOT_PATH . '/header.php';
305
306
        /** @var \XoopsModules\Xhelp\DepartmentHandler $departmentHandler */
307
        $departmentHandler = $helper->getHandler('Department');    // Department handler
308
        $criteria          = new \Criteria('', '');
309
        $criteria->setSort('department');
310
        $departments = $departmentHandler->getObjects($criteria);
311
        if (0 == count($departments)) {
312
            $message = _XHELP_MESSAGE_NO_DEPTS;
313
            $helper->redirect('index.php', 3, $message);
314
        }
315
        $aDept    = [];
316
        $myGroups = $memberHandler->getGroupsByUser($xoopsUser->getVar('uid'));
317
        if ($xhelp_isStaff && (0 == $helper->getConfig('xhelp_deptVisibility'))) {     // If staff are not applied
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $helper->getConfig('xhelp_deptVisibility') of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
318
            foreach ($departments as $dept) {
319
                $deptid  = $dept->getVar('id');
320
                $aDept[] = [
321
                    'id'         => $deptid,
322
                    'department' => $dept->getVar('department'),
323
                ];
324
            }
325
        } else {
326
            foreach ($departments as $dept) {
327
                $deptid = $dept->getVar('id');
328
                foreach ($myGroups as $group) {   // Check for user to be in multiple groups
329
                    if ($grouppermHandler->checkRight(_XHELP_GROUP_PERM_DEPT, $deptid, $group, $module_id)) {
330
                        //Assign the first value to $dept_id incase the default department property not set
331
                        if (null === $dept_id) {
332
                            $dept_id = $deptid;
333
                        }
334
                        $aDept[] = [
335
                            'id'         => $deptid,
336
                            'department' => $dept->getVar('department'),
337
                        ];
338
                        break;
339
                    }
340
                }
341
            }
342
        }
343
344
        // User Dept visibility check
345
        if (empty($aDept)) {
346
            $message = _XHELP_MESSAGE_NO_DEPTS;
347
            $helper->redirect('index.php', 3, $message);
348
        }
349
350
        $xoopsTpl->assign('xhelp_isUser', true);
351
352
        if ($xhelp_isStaff) {
353
            $checkStaff = $staffHandler->getByUid($xoopsUser->getVar('uid'));
354
            if (!$hasRights = $checkStaff->checkRoleRights(XHELP_SEC_TICKET_ADD)) {
355
                $message = _XHELP_MESSAGE_NO_ADD_TICKET;
356
                $helper->redirect('index.php', 3, $message);
357
            }
358
            unset($checkStaff);
359
360
            if (false !== ($hasRights = $staff->checkRoleRights(XHELP_SEC_TICKET_OWNERSHIP, $dept_id))) {
361
                $staff = $membershipHandler->xoopsUsersByDept($dept_id);
362
363
                $aOwnership    = [];
364
                $aOwnership[0] = _XHELP_NO_OWNER;
365
                foreach ($staff as $stf) {
366
                    $aOwnership[$stf->getVar('uid')] = $stf->getVar('uname');
367
                }
368
                $xoopsTpl->assign('xhelp_aOwnership', $aOwnership);
369
            } else {
370
                $xoopsTpl->assign('xhelp_aOwnership', false);
371
            }
372
        }
373
374
        $has_mimes = false;
375
        if ($helper->getConfig('xhelp_allowUpload')) {
376
            // Get available mimetypes for file uploading
377
            /** @var \XoopsModules\Xhelp\MimetypeHandler $mimetypeHandler */
378
            $mimetypeHandler = $helper->getHandler('Mimetype');
379
            $xhelp           = Utility::getModule();
380
            $mid             = $xhelp->getVar('mid');
381
            if ($xhelp_isStaff) {
382
                $criteria = new \Criteria('mime_admin', '1');
383
            } else {
384
                $criteria = new \Criteria('mime_user', '1');
385
            }
386
            $mimetypes = $mimetypeHandler->getObjects($criteria);
387
            $mimes     = '';
388
            foreach ($mimetypes as $mime) {
389
                if ('' === $mimes) {
390
                    $mimes = $mime->getVar('mime_ext');
391
                } else {
392
                    $mimes .= ', ' . $mime->getVar('mime_ext');
393
                }
394
            }
395
            $xoopsTpl->assign('xhelp_mimetypes', $mimes);
396
        }
397
398
        $xoopsTpl->assign('xhelp_has_logUser', false);
399
        if ($xhelp_isStaff) {
400
            $checkStaff = $staffHandler->getByUid($xoopsUser->getVar('uid'));
401
            if (false !== ($hasRights = $checkStaff->checkRoleRights(XHELP_SEC_TICKET_LOGUSER))) {
402
                $xoopsTpl->assign('xhelp_has_logUser', true);
403
            }
404
            unset($checkStaff);
405
        }
406
407
        // Get current dept's custom fields
408
        $fields = $ticketFieldDepartmentHandler->fieldsByDepartment($dept_id, true);
409
410
        if (!$savedFields = $session->get('xhelp_custFields')) {
411
            $savedFields = [];
412
        }
413
414
        $aFields = [];
415
        foreach ($fields as $field) {
416
            $values = $field->getVar('fieldvalues');
417
            if (XHELP_CONTROL_YESNO == $field->getVar('controltype')) {
418
                $values = [1 => _YES, 0 => _NO];
419
            }
420
421
            // Check for values already submitted, and fill those values in
422
            if (array_key_exists($field->getVar('fieldname'), $savedFields)) {
423
                $defaultValue = $savedFields[$field->getVar('fieldname')];
424
            } else {
425
                $defaultValue = $field->getVar('defaultvalue');
426
            }
427
428
            $aFields[$field->getVar('id')] = [
429
                'name'         => $field->getVar('name'),
430
                'desc'         => $field->getVar('description'),
431
                'fieldname'    => $field->getVar('fieldname'),
432
                'defaultvalue' => $defaultValue,
433
                'controltype'  => $field->getVar('controltype'),
434
                'required'     => $field->getVar('required'),
435
                'fieldlength'  => $field->getVar('fieldlength') < 50 ? $field->getVar('fieldlength') : 50,
436
                'maxlength'    => $field->getVar('fieldlength'),
437
                'weight'       => $field->getVar('weight'),
438
                'fieldvalues'  => $values,
439
                'validation'   => $field->getVar('validation'),
440
            ];
441
        }
442
        $xoopsTpl->assign('xhelp_custFields', $aFields);
443
        if (!empty($aFields)) {
444
            $xoopsTpl->assign('xhelp_hasCustFields', true);
445
        } else {
446
            $xoopsTpl->assign('xhelp_hasCustFields', false);
447
        }
448
449
        $javascript = '<script type="text/javascript" src="' . XHELP_BASE_URL . "/include/functions.js\"></script>
450
<script type=\"text/javascript\" src='" . XHELP_SCRIPT_URL . "/addTicketDeptChange.php?client'></script>
451
<script type=\"text/javascript\">
452
<!--
453
function departments_onchange()
454
{
455
    dept = xoopsGetElementById('departments');
456
    var wl = new Xhelp\WebLib(fieldHandler);
457
    wl.customFieldsByDept(dept.value);\n";
458
459
        if ($xhelp_isStaff) {
460
            $javascript .= "var w = new Xhelp\WebLib(staffHandler);
461
        w.staffbydept(dept.value);\n";
462
        }
463
        $javascript .= '}
464
465
var staffHandler = {
466
    staffbydept: function(result){';
467
        if ($xhelp_isStaff) {
468
            if (Request::hasVar('xhelp_logMode', 'COOKIE') && 2 === $_COOKIE['xhelp_logMode']
469
                && is_object($staff)
470
                && $staff->checkRoleRights(XHELP_SEC_TICKET_OWNERSHIP, $dept_id)) {
471
                $javascript .= "var sel = gE('owner');";
472
                $javascript .= "xhelpFillStaffSelect(sel, result);\n";
473
            }
474
        }
475
        $javascript .= "}
476
}
477
478
var fieldHandler = {
479
    customFieldsByDept: function(result){
480
        var tbl = gE('tblAddTicket');\n";
481
        if ($xhelp_isStaff && isset($_COOKIE['xhelp_logMode']) && 2 === $_COOKIE['xhelp_logMode']) {
482
            $javascript .= "var beforeele = gE('privResponse');\n";
483
        } else {
484
            $javascript .= "var beforeele = gE('addButtons');\n";
485
        }
486
        $javascript .= "tbody = tbl.tBodies[0];\n";
487
        $javascript .= "xhelpFillCustomFlds(tbody, result, beforeele);
488
    }
489
}
490
491
function window_onload()
492
{
493
    xhelpDOMAddEvent(xoopsGetElementById('departments'), 'change', departments_onchange, true);
494
}
495
496
window.setTimeout('window_onload()', 1500);
497
//-->
498
</script>";
499
500
        $xoopsTpl->assign('xhelp_baseURL', XHELP_BASE_URL);
501
        $xoopsTpl->assign('xhelp_includeURL', XHELP_INCLUDE_URL);
502
        $xoopsTpl->assign('xoops_module_header', $javascript . $xhelp_module_header);
503
        $xoopsTpl->assign('xhelp_allowUpload', $helper->getConfig('xhelp_allowUpload'));
504
        $xoopsTpl->assign('xhelp_text_lookup', _XHELP_TEXT_LOOKUP);
505
        $xoopsTpl->assign('xhelp_text_email', _XHELP_TEXT_EMAIL);
506
        $xoopsTpl->assign('xhelp_imagePath', XOOPS_URL . '/modules/xhelp/assets/images/');
507
        $xoopsTpl->assign('xhelp_departments', $aDept);
508
        $xoopsTpl->assign('xhelp_current_file', basename(__file__));
509
        $xoopsTpl->assign('xhelp_priorities', [5, 4, 3, 2, 1]);
510
        $xoopsTpl->assign('xhelp_priorities_desc', [
511
            5 => _XHELP_PRIORITY5,
512
            4 => _XHELP_PRIORITY4,
513
            3 => _XHELP_PRIORITY3,
514
            2 => _XHELP_PRIORITY2,
515
            1 => _XHELP_PRIORITY1,
516
        ]);
517
        $xoopsTpl->assign('xhelp_default_priority', XHELP_DEFAULT_PRIORITY);
518
        $xoopsTpl->assign('xhelp_currentUser', $xoopsUser->getVar('uid'));
519
        $xoopsTpl->assign('xhelp_numTicketUploads', $helper->getConfig('xhelp_numTicketUploads'));
520
        //        if (Request::hasVar('logFor', 'POST')) {
521
        if (Request::hasVar('logFor', 'POST')) {
522
            $uid      = Request::getInt('logFor', 0, 'POST');
523
            $username = $xoopsUser::getUnameFromId($uid);
524
            $xoopsTpl->assign('xhelp_username', $username);
525
            $xoopsTpl->assign('xhelp_user_id', $uid);
526
        } else {
527
            $uid      = $xoopsUser->getVar('uid');
528
            $username = $xoopsUser->getVar('uname');
529
            $xoopsTpl->assign('xhelp_username', $username);
530
            $xoopsTpl->assign('xhelp_user_id', $uid);
531
        }
532
        $xoopsTpl->assign('xhelp_isStaff', $xhelp_isStaff);
533
        if (isset($_COOKIE['xhelp_logMode'])) {
534
            $xoopsTpl->assign('xhelp_logMode', $_COOKIE['xhelp_logMode']);
535
        } else {
536
            $xoopsTpl->assign('xhelp_logMode', 1);
537
        }
538
539
        if ($xhelp_isStaff) {
540
            if (Request::hasVar('xhelp_logMode', 'COOKIE') && 2 === $_COOKIE['xhelp_logMode']) {
541
                /** @var \XoopsModules\Xhelp\StatusHandler $statusHandler */
542
                $statusHandler = $helper->getHandler('Status');
543
                $criteria      = new \Criteria('', '');
544
                $criteria->setSort('description');
545
                $criteria->setOrder('ASC');
546
                $statuses  = $statusHandler->getObjects($criteria);
547
                $aStatuses = [];
548
                foreach ($statuses as $status) {
549
                    $aStatuses[$status->getVar('id')] = [
550
                        'id'    => $status->getVar('id'),
551
                        'desc'  => $status->getVar('description'),
552
                        'state' => $status->getVar('state'),
553
                    ];
554
                }
555
556
                $xoopsTpl->assign('xhelp_statuses', $aStatuses);
557
            }
558
            $xoopsTpl->assign('xhelp_savedSearches', $aSavedSearches);
559
        }
560
561
        $errors    = [];
562
        $aElements = [];
563
        if (false !== ($validateErrors = $session->get('xhelp_validateError'))) {
564
            foreach ($validateErrors as $fieldname => $error) {
565
                if (!empty($error['errors'])) {
566
                    $aElements[] = $fieldname;
567
                    foreach ($error['errors'] as $err) {
568
                        $errors[$fieldname] = $err;
569
                    }
570
                }
571
            }
572
            $xoopsTpl->assign('xhelp_errors', $errors);
573
        } else {
574
            $xoopsTpl->assign('xhelp_errors', null);
575
        }
576
577
        $elements = ['subject', 'description'];
578
        foreach ($elements as $element) {         // Foreach element in the predefined list
579
            $xoopsTpl->assign("xhelp_element_$element", 'formButton');
580
            foreach ($aElements as $aElement) {   // Foreach that has an error
581
                if ($aElement == $element) {      // If the names are equal
582
                    $xoopsTpl->assign("xhelp_element_$element", 'validateError');
583
                    break;
584
                }
585
            }
586
        }
587
588
        if (false !== ($ticket = $session->get('xhelp_ticket'))) {
589
            $xoopsTpl->assign('xhelp_ticket_uid', $ticket['uid']);
590
            $xoopsTpl->assign('xhelp_ticket_username', $xoopsUser::getUnameFromId($ticket['uid']));
591
            $xoopsTpl->assign('xhelp_ticket_subject', stripslashes($ticket['subject']));
592
            $xoopsTpl->assign('xhelp_ticket_description', stripslashes($ticket['description']));
593
            $xoopsTpl->assign('xhelp_ticket_department', $ticket['department']);
594
            $xoopsTpl->assign('xhelp_ticket_priority', $ticket['priority']);
595
        } else {
596
            $xoopsTpl->assign('xhelp_ticket_uid', $uid);
597
            $xoopsTpl->assign('xhelp_ticket_username', $username);
598
            $xoopsTpl->assign('xhelp_ticket_subject', null);
599
            $xoopsTpl->assign('xhelp_ticket_description', null);
600
            $xoopsTpl->assign('xhelp_ticket_department', $dept_id);
601
            $xoopsTpl->assign('xhelp_ticket_priority', XHELP_DEFAULT_PRIORITY);
602
        }
603
604
        if (false !== ($response = $session->get('xhelp_response'))) {
605
            $xoopsTpl->assign('xhelp_response_uid', $response['uid']);
606
            $xoopsTpl->assign('xhelp_response_message', $response['message']);
607
            $xoopsTpl->assign('xhelp_response_timespent', $response['timeSpent']);
608
            $xoopsTpl->assign('xhelp_response_userIP', $response['userIP']);
609
            $xoopsTpl->assign('xhelp_response_private', $response['private']);
610
            $xoopsTpl->assign('xhelp_ticket_status', $response['status']);
611
            $xoopsTpl->assign('xhelp_ticket_ownership', $response['owner']);
612
        } else {
613
            $xoopsTpl->assign('xhelp_response_uid', null);
614
            $xoopsTpl->assign('xhelp_response_message', null);
615
            $xoopsTpl->assign('xhelp_response_timeSpent', 0);
616
            $xoopsTpl->assign('xhelp_response_userIP', null);
617
            $xoopsTpl->assign('xhelp_response_private', null);
618
            $xoopsTpl->assign('xhelp_ticket_status', 1);
619
            $xoopsTpl->assign('xhelp_ticket_ownership', 0);
620
        }
621
622
        require_once XOOPS_ROOT_PATH . '/footer.php';                             //Include the page footer
623
    }
624
} else {    // If not a user
625
    /** @var \XoopsConfigHandler $configHandler */
626
    $configHandler = xoops_getHandler('config');
627
    //$xoopsConfigUser = $configHandler->getConfigsByCat(XOOPS_CONF_USER);
628
    $xoopsConfigUser = [];
629
    $criteria        = new \CriteriaCompo(new \Criteria('conf_name', 'allow_register'), 'OR');
630
    $criteria->add(new \Criteria('conf_name', 'activation_type'), 'OR');
631
    $myConfigs = $configHandler->getConfigs($criteria);
632
633
    foreach ($myConfigs as $myConf) {
634
        $xoopsConfigUser[$myConf->getVar('conf_name')] = $myConf->getVar('conf_value');
635
    }
636
    if (0 == $xoopsConfigUser['allow_register']) {    // Use to doublecheck that anonymous users are allowed to register
637
        $helper->redirect('error.php');
638
    } else {
639
        $helper->redirect('anon_addTicket.php');
640
    }
641
    exit();
642
}
643
644
/**
645
 * @param string|array $fields
646
 * @return bool
647
 */
648
function saveTicket($fields = ''): bool
649
{
650
    global $session, $xhelp_isStaff;
651
    $session->set('xhelp_ticket', [
652
        'uid'         => Request::getInt('user_id', 0, 'POST'),
653
        'subject'     => Request::getString('subject', '', 'POST'),
654
        'description' => Request::getText('description', '', 'POST'), //htmlspecialchars($_POST['description'], ENT_QUOTES),
655
        'department'  => Request::getInt('departments', 0, 'POST'), //$_POST['departments'],
656
        'priority'    => Request::getInt('priority', 0, 'POST'),
657
    ]);
658
659
    if ($xhelp_isStaff && 2 === $_COOKIE['xhelp_logMode']) {
660
        $session->set('xhelp_response', [
661
            'uid'       => Request::getInt('user_id', 0, 'POST'),
662
            'message'   => Request::getString('response', '', 'POST'),
663
            'timeSpent' => Request::getInt('timespent', 0, 'POST'),
664
            'userIP'    => getenv('REMOTE_ADDR'),
665
            'private'   => Request::hasVar('private', 'POST') ? 1 : 0, //isset($_POST['private']) ? 1 : 0,
666
            'status'    => Request::getInt('status', 0, 'POST'),
667
            'owner'     => Request::getInt('owner', 0, 'POST'), //$_POST['owner']
668
        ]);
669
    }
670
671
    if ('' != $fields) {
672
        $session->set('xhelp_custFields', $fields);
673
    }
674
675
    return true;
676
}
677