Total Complexity | 220 |
Total Lines | 1796 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like NotificationService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NotificationService, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
37 | class NotificationService extends Service |
||
38 | { |
||
39 | /** |
||
40 | * Instance of the staff object |
||
41 | * |
||
42 | * @var object |
||
43 | */ |
||
44 | public $staffHandler; |
||
45 | /** |
||
46 | * Instance of the xoops text sanitizer |
||
47 | * |
||
48 | * @var object |
||
49 | */ |
||
50 | public $myTextSanitizer; |
||
51 | /** |
||
52 | * Path to the mail_template directory |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | public $templateDir = ''; |
||
57 | /** |
||
58 | * Instance of the module object |
||
59 | * |
||
60 | * @var object |
||
61 | */ |
||
62 | public $module; |
||
63 | /** |
||
64 | * Instance of the notification object |
||
65 | * |
||
66 | * @var object |
||
67 | */ |
||
68 | public $notificationHandler; |
||
69 | /** |
||
70 | * Instance of the status object |
||
71 | * |
||
72 | * @var object |
||
73 | */ |
||
74 | public $statusHandler; |
||
75 | |||
76 | /** |
||
77 | * Class Constructor |
||
78 | */ |
||
79 | public function __construct() |
||
80 | { |
||
81 | global $xoopsConfig, $xoopsModule; |
||
82 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
83 | $this->myTextSanitizer = \MyTextSanitizer::getInstance(); |
||
84 | $this->templateDir = $this->getTemplateDir($xoopsConfig['language']); |
||
85 | $this->module = Utility::getModule(); |
||
86 | $this->staffHandler = new StaffHandler($db); |
||
87 | $this->notificationHandler = new NotificationHandler($db); |
||
88 | $this->statusHandler = new StatusHandler($db); |
||
89 | $this->helper = Helper::getInstance(); |
||
|
|||
90 | $this->init(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Retrieve the email_template object that is requested |
||
95 | * |
||
96 | * @param string|int $category ID or name of item |
||
97 | * @param string $event name of event |
||
98 | * @param object $module $xoopsModule object |
||
99 | * @param mixed $template_id |
||
100 | * |
||
101 | * @return bool|array |
||
102 | */ |
||
103 | public function getEmailTpl($category, string $event, object $module, &$template_id) |
||
104 | { |
||
105 | $templates = $module->getInfo('_email_tpl'); // Gets $modversion['_email_tpl'] array from xoops_version.php |
||
106 | |||
107 | foreach ($templates as $tpl_id => $tpl) { |
||
108 | if ($tpl['category'] == $category && $tpl['name'] == $event) { |
||
109 | $template_id = $tpl_id; |
||
110 | |||
111 | return $tpl; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | return false; |
||
116 | } |
||
117 | |||
118 | /* |
||
119 | * Returns a group of $staffRole objects |
||
120 | * |
||
121 | * @access int $dept ID of department |
||
122 | * @access array $aMembers array of all possible staff members |
||
123 | * |
||
124 | * @access private |
||
125 | */ |
||
126 | |||
127 | /** |
||
128 | * @param int|string $dept |
||
129 | * @param array $aMembers |
||
130 | * @return array |
||
131 | */ |
||
132 | public function &getStaffRoles($dept, array $aMembers): array |
||
133 | { |
||
134 | $staffRoleHandler = $this->helper->getHandler('StaffRole'); |
||
135 | |||
136 | // Retrieve roles of all members |
||
137 | $criteria = new \CriteriaCompo(new \Criteria('uid', '(' . \implode(',', $aMembers) . ')', 'IN')); |
||
138 | $criteria->add(new \Criteria('deptid', $dept)); |
||
139 | $staffRoles = $staffRoleHandler->getObjects($criteria, false); // array of staff role objects |
||
140 | |||
141 | return $staffRoles; |
||
142 | } |
||
143 | |||
144 | /* |
||
145 | * Gets a list of staff members that have the notification selected |
||
146 | * |
||
147 | * @access object $staffRoles staffRole objects |
||
148 | * @access array $aMembers array of all possible staff members |
||
149 | * @access array $staff_options array of acceptable departments |
||
150 | * |
||
151 | * @access private |
||
152 | */ |
||
153 | |||
154 | /** |
||
155 | * @param array $staffRoles |
||
156 | * @param array $aMembers |
||
157 | * @param array $staff_options |
||
158 | * @return array |
||
159 | */ |
||
160 | public function &getEnabledStaff(array $staffRoles, array $aMembers, array $staff_options): array |
||
161 | { |
||
162 | // Get only staff members that have permission for this notification |
||
163 | $enabled_staff = []; |
||
164 | foreach ($aMembers as $aMember) { |
||
165 | foreach ($staffRoles as $staffRole) { |
||
166 | if ($staffRole->getVar('uid') == $aMember && \in_array($staffRole->getVar('roleid'), $staff_options)) { |
||
167 | $enabled_staff[$aMember] = $aMember; |
||
168 | break; |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | unset($aMembers); |
||
173 | |||
174 | return $enabled_staff; |
||
175 | } |
||
176 | |||
177 | /* |
||
178 | * Used to retrieve a list of xoops user objects |
||
179 | * |
||
180 | * @param array $enabled_staff array of staff members that have the notification enabled |
||
181 | * |
||
182 | * @access private |
||
183 | */ |
||
184 | |||
185 | /** |
||
186 | * @param array $enabled_staff |
||
187 | * @param bool $active_only |
||
188 | * @return array |
||
189 | */ |
||
190 | public function &getXoopsUsers(array $enabled_staff, bool $active_only = true): array |
||
191 | { |
||
192 | $xoopsUsers = []; |
||
193 | /** @var \XoopsMemberHandler $memberHandler */ |
||
194 | $memberHandler = \xoops_getHandler('member'); |
||
195 | if (\count($enabled_staff) > 0) { |
||
196 | $criteria = new \CriteriaCompo(new \Criteria('uid', '(' . \implode(',', $enabled_staff) . ')', 'IN')); |
||
197 | } else { |
||
198 | return $xoopsUsers; |
||
199 | } |
||
200 | if ($active_only) { |
||
201 | $criteria->add(new \Criteria('level', '0', '>')); |
||
202 | } |
||
203 | $xoopsUsers = $memberHandler->getUsers($criteria, true); // xoopsUser objects |
||
204 | unset($enabled_staff); |
||
205 | |||
206 | return $xoopsUsers; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Returns only the accepted staff members after having their permissions checked |
||
211 | * @param array $aMembers array of all possible staff members |
||
212 | * @param Ticket|int $ticket xhelp_ticket object |
||
213 | * @param Notification $settings xhelp_notification object |
||
214 | * @param int $submittedBy ID of ticket submitter |
||
215 | * @return array of XoopsUser objects |
||
216 | */ |
||
217 | public function &checkStaffSetting(array $aMembers, $ticket, Notification $settings, int $submittedBy): array |
||
218 | { |
||
219 | // $submittedBy = (int)$submittedBy; |
||
220 | $dept = $ticket; |
||
221 | if (\is_object($ticket)) { |
||
222 | $dept = $ticket->getVar('department'); |
||
223 | } |
||
224 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
225 | $staff_options = $settings->getVar('staff_options') ?? ''; |
||
226 | |||
227 | $staffRoles = $this->getStaffRoles($dept, $aMembers); // Get list of the staff members' roles |
||
228 | $enabled_staff = $this->getEnabledStaff($staffRoles, $aMembers, $staff_options); |
||
229 | $xoopsUsers = $this->getXoopsUsers($enabled_staff); |
||
230 | |||
231 | return $xoopsUsers; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Returns an array of staff UID's |
||
236 | * |
||
237 | * @param array $members xhelp_staff objects |
||
238 | * @param string|int $submittedBy |
||
239 | * @param bool $removeSubmitter |
||
240 | * @return array |
||
241 | */ |
||
242 | private function &makeMemberArray(array $members, $submittedBy, bool $removeSubmitter = false): array |
||
243 | { |
||
244 | $aMembers = []; |
||
245 | $submittedBy = (int)$submittedBy; |
||
246 | foreach ($members as $member) { // Full list of dept members |
||
247 | if ($removeSubmitter) { |
||
248 | if ($member->getVar('uid') == $submittedBy) { // Remove the staff member that submitted from the email list |
||
249 | continue; |
||
250 | } |
||
251 | |||
252 | $aMembers[$member->getVar('uid')] = $member->getVar('uid'); |
||
253 | } else { |
||
254 | $aMembers[$member->getVar('uid')] = $member->getVar('uid'); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | return $aMembers; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Returns emails of staff belonging to an event |
||
263 | * |
||
264 | * @param Ticket|int $ticket |
||
265 | * @param int $event_id bit_value of event |
||
266 | * @param Notification $settings |
||
267 | * @param int|null $submittedBy ID of user submitting event - should only be used when there is a response |
||
268 | * @return array |
||
269 | * @internal param int $dept ID of department |
||
270 | */ |
||
271 | public function &getSubscribedStaff($ticket, int $event_id, Notification $settings, int $submittedBy = null): array |
||
272 | { |
||
273 | global $xoopsUser; |
||
274 | |||
275 | $arr = []; |
||
276 | /** @var \XoopsModules\Xhelp\MembershipHandler $membershipHandler */ |
||
277 | $membershipHandler = $this->helper->getHandler('Membership'); |
||
278 | /** @var \XoopsMemberHandler $memberHandler */ |
||
279 | $memberHandler = \xoops_getHandler('member'); |
||
280 | |||
281 | if (\is_object($ticket)) { |
||
282 | if (!$submittedBy) { |
||
283 | $submittedBy = $ticket->getVar('uid'); |
||
284 | } |
||
285 | $owner = $ticket->getVar('ownership'); |
||
286 | $dept = $ticket->getVar('department'); |
||
287 | } else { |
||
288 | $dept = (int)$ticket; |
||
289 | } |
||
290 | $submittedBy = (int)$submittedBy; |
||
291 | |||
292 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
293 | $staff_options = $settings->getVar('staff_options') ?? ''; |
||
294 | switch ($staff_setting) { |
||
295 | case \XHELP_NOTIF_STAFF_DEPT: // Department Staff can receive notification |
||
296 | $members = $membershipHandler->membershipByDept($dept); // Staff objects |
||
297 | $aMembers = $this->makeMemberArray($members, $submittedBy, true); |
||
298 | $xoopsUsers = $this->checkStaffSetting($aMembers, $ticket, $settings, $submittedBy); |
||
299 | break; |
||
300 | case \XHELP_NOTIF_STAFF_OWNER: // Ticket Owner can receive notification |
||
301 | $members = $membershipHandler->membershipByDept($dept); |
||
302 | if (0 != $ticket->getVar('ownership')) { // If there is a ticket owner |
||
303 | $ticket_owner = $ticket->getVar('ownership'); |
||
304 | $aMembers[$ticket_owner] = $ticket_owner; |
||
305 | $criteria = new \Criteria('uid', '(' . \implode(',', $aMembers) . ')', 'IN'); |
||
306 | unset($aMembers); |
||
307 | $xoopsUsers = $memberHandler->getUsers($criteria, true); // xoopsUser objects |
||
308 | } else { // If no ticket owner, send to dept staff |
||
309 | $aMembers = $this->makeMemberArray($members, 0, true); |
||
310 | $xoopsUsers = $this->checkStaffSetting($aMembers, $ticket, $settings, $submittedBy); |
||
311 | } |
||
312 | break; |
||
313 | case \XHELP_NOTIF_STAFF_NONE: // Notification is turned off |
||
314 | default: |
||
315 | return $arr; |
||
316 | } |
||
317 | |||
318 | //Sort users based on Notification Preference |
||
319 | foreach ($xoopsUsers as $xUser) { |
||
320 | $cMember = $members[$xUser->getVar('uid')]; |
||
321 | |||
322 | if (!empty($cMember) && ($xUser->uid() != $xoopsUser->uid())) { |
||
323 | if ($this->isSubscribed($cMember, $event_id)) { |
||
324 | if (2 == $xUser->getVar('notify_method')) { // Send by email |
||
325 | $arr['email'][] = $members[$xUser->getVar('uid')]->getVar('email'); |
||
326 | } elseif (1 == $xUser->getVar('notify_method')) { // Send by pm |
||
327 | $arr['pm'][] = $xUser; |
||
328 | } |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | |||
333 | return $arr; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Returns emails of users belonging to a ticket |
||
338 | * |
||
339 | * @param int $ticketId ID of ticket |
||
340 | * @return array |
||
341 | */ |
||
342 | public function &getSubscribedUsers(int $ticketId): array |
||
343 | { |
||
344 | global $xoopsUser; |
||
345 | |||
346 | // $ticketId = (int)$ticketId; |
||
347 | |||
348 | /** @var \XoopsModules\Xhelp\TicketEmailsHandler $ticketEmailsHandler */ |
||
349 | $ticketEmailsHandler = $this->helper->getHandler('TicketEmails'); |
||
350 | /** @var \XoopsMemberHandler $memberHandler */ |
||
351 | $memberHandler = \xoops_getHandler('member'); |
||
352 | |||
353 | //Get all Subscribed users, except for the current user |
||
354 | $criteria = new \CriteriaCompo(new \Criteria('ticketid', (string)$ticketId)); |
||
355 | $criteria->add(new \Criteria('suppress', '0')); |
||
356 | $criteria->add(new \Criteria('email', $xoopsUser->email(), '<>')); |
||
357 | |||
358 | $users = $ticketEmailsHandler->getObjects($criteria); // xhelp_ticketEmail objects |
||
359 | |||
360 | $aUsers = []; |
||
361 | $arr = []; |
||
362 | foreach ($users as $user) { |
||
363 | if (0 != $user->getVar('uid')) { |
||
364 | $aUsers[$user->getVar('email')] = $user->getVar('uid'); |
||
365 | } else { |
||
366 | // Add users with just email to array |
||
367 | $arr['email'][] = $user->getVar('email'); |
||
368 | } |
||
369 | } |
||
370 | |||
371 | $xoopsUsers = []; |
||
372 | if (!empty($aUsers)) { |
||
373 | $criteria = new \Criteria('uid', '(' . \implode(',', $aUsers) . ')', 'IN'); |
||
374 | $xoopsUsers = $memberHandler->getUsers($criteria, true); // xoopsUser objects |
||
375 | } |
||
376 | unset($aUsers); |
||
377 | |||
378 | // @todo - replace notify_method integers with constants |
||
379 | // Add users with uid |
||
380 | foreach ($xoopsUsers as $xUser) { // Find which method user prefers for sending message |
||
381 | if (2 == $xUser->getVar('notify_method')) { |
||
382 | $arr['email'][] = $xUser->getVar('email'); |
||
383 | } elseif (1 == $xUser->getVar('notify_method')) { |
||
384 | $arr['pm'][] = $xUser; |
||
385 | } |
||
386 | } |
||
387 | |||
388 | return $arr; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Checks to see if the staff member is subscribed to receive the notification for this event |
||
393 | * |
||
394 | * @param int|Staff $user userid/staff object of staff member |
||
395 | * @param int $event_id value of the event |
||
396 | * @return bool true is suscribed, false if not |
||
397 | */ |
||
398 | public function isSubscribed($user, int $event_id): bool |
||
399 | { |
||
400 | if (!\is_object($user)) { //If user is not an object, retrieve a staff object using the uid |
||
401 | if (\is_numeric($user)) { |
||
402 | $uid = $user; |
||
403 | $staffHandler = $this->helper->getHandler('Staff'); |
||
404 | if (!$user = $staffHandler->getByUid($uid)) { |
||
405 | return false; |
||
406 | } |
||
407 | } |
||
408 | } |
||
409 | |||
410 | return ($user->getVar('notify') & (2 ** $event_id)) > 0; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Retrieve a user's email address |
||
415 | * |
||
416 | * @param int $uid user's id |
||
417 | * @return array array's email |
||
418 | */ |
||
419 | public function getUserEmail(int $uid): array |
||
420 | { |
||
421 | global $xoopsUser; |
||
422 | $arr = []; |
||
423 | $uid = $uid; |
||
424 | |||
425 | if ($uid == $xoopsUser->getVar('uid')) { // If $uid == current user's uid |
||
426 | if (2 == $xoopsUser->getVar('notify_method')) { |
||
427 | $arr['email'][] = $xoopsUser->getVar('email'); // return their email |
||
428 | } elseif (1 == $xoopsUser->getVar('notify_method')) { |
||
429 | $arr['pm'][] = $xoopsUser; |
||
430 | } |
||
431 | } else { |
||
432 | /** @var \XoopsMemberHandler $memberHandler */ |
||
433 | $memberHandler = \xoops_getHandler('member'); //otherwise... |
||
434 | $member = $memberHandler->getUser($uid); |
||
435 | if ($member) { |
||
436 | if (2 == $member->getVar('notify_method')) { |
||
437 | $arr['email'][] = $member->getVar('email'); |
||
438 | } elseif (1 == $member->getVar('notify_method')) { |
||
439 | $arr['pm'][] = $member; |
||
440 | } |
||
441 | } else { |
||
442 | $arr['email'][] = ''; |
||
443 | } |
||
444 | } |
||
445 | |||
446 | return $arr; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Retrieves a staff member's email address |
||
451 | * |
||
452 | * @param int $uid user's id |
||
453 | * @param int|string|null $dept |
||
454 | * @param array|null $staff_options |
||
455 | * @return array member's email |
||
456 | */ |
||
457 | public function getStaffEmail(int $uid, $dept = null, ?array $staff_options = null): array |
||
458 | { |
||
459 | $uid = $uid; |
||
460 | $dept = (int)$dept; |
||
461 | /** @var \XoopsMemberHandler $memberHandler */ |
||
462 | $memberHandler = \xoops_getHandler('member'); |
||
463 | $arr = []; |
||
464 | |||
465 | // Check staff roles to staff options making sure the staff has permission |
||
466 | $staffRoles = $this->staffHandler->getRolesByDept($uid, $dept, true); |
||
467 | $bFound = true; |
||
468 | foreach ($staff_options as $option) { |
||
469 | if (\array_key_exists($option, $staffRoles)) { |
||
470 | $bFound = true; |
||
471 | break; |
||
472 | } |
||
473 | |||
474 | $bFound = false; |
||
475 | } |
||
476 | if (!$bFound) { |
||
477 | return $arr; |
||
478 | } |
||
479 | |||
480 | $staff = $this->staffHandler->getByUid($uid); |
||
481 | if ($staff) { |
||
482 | $member = $memberHandler->getUser($uid); |
||
483 | if ($member) { |
||
484 | if (2 == $member->getVar('notify_method')) { |
||
485 | $arr['email'][] = $staff->getVar('email'); |
||
486 | } elseif (1 == $member->getVar('notify_method')) { |
||
487 | $arr['pm'][] = $member; |
||
488 | } |
||
489 | } else { |
||
490 | $arr['email'][] = ''; |
||
491 | } |
||
492 | } else { |
||
493 | $arr['email'][] = ''; |
||
494 | } |
||
495 | |||
496 | return $arr; |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Send pm and email notifications to selected users |
||
501 | * |
||
502 | * @param object|array $email_tpl object returned from getEmailTpl() function |
||
503 | * @param array $sendTo emails and xoopsUser objects |
||
504 | * @param array $tags array of notification information |
||
505 | * @param string $fromEmail |
||
506 | * @return bool TRUE if success, FALSE if no success |
||
507 | */ |
||
508 | public function sendEvents($email_tpl, array $sendTo, array $tags, string $fromEmail = ''): bool |
||
509 | { |
||
510 | $ret = true; |
||
511 | if (\array_key_exists('pm', $sendTo)) { |
||
512 | $ret = $ret && $this->sendEventPM($email_tpl, $sendTo, $tags, $fromEmail); |
||
513 | } |
||
514 | |||
515 | if (\array_key_exists('email', $sendTo)) { |
||
516 | $ret = $ret && $this->sendEventEmail($email_tpl, $sendTo, $tags, $fromEmail); |
||
517 | } |
||
518 | |||
519 | return $ret; |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * Send the pm notification to selected users |
||
524 | * |
||
525 | * @param object|array $email_tpl object returned from getEmailTpl() function |
||
526 | * @param array $sendTo xoopsUser objects |
||
527 | * @param array $tags array of notification information |
||
528 | * @param string $fromEmail |
||
529 | * @return bool TRUE if success, FALSE if no success |
||
530 | */ |
||
531 | public function sendEventPM($email_tpl, array $sendTo, array $tags, string $fromEmail = ''): bool |
||
532 | { |
||
533 | $notify_pm = ''; |
||
534 | global $xoopsConfig, $xoopsUser; |
||
535 | |||
536 | $notify_pm = $sendTo['pm']; |
||
537 | |||
538 | $tags = \array_merge($tags, $this->getCommonTplVars()); // Retrieve the common template vars and add to array |
||
539 | $xoopsMailer = \xoops_getMailer(); |
||
540 | $xoopsMailer->usePM(); |
||
541 | |||
542 | foreach ($tags as $k => $v) { |
||
543 | $xoopsMailer->assign($k, \preg_replace('/&/i', '&', $v)); |
||
544 | } |
||
545 | $xoopsMailer->setTemplateDir($this->templateDir); // Set template dir |
||
546 | $xoopsMailer->setTemplate($email_tpl['mail_template'] . '.tpl'); // Set the template to be used |
||
547 | |||
548 | /** @var \XoopsConfigHandler $configHandler */ |
||
549 | $configHandler = \xoops_getHandler('config'); |
||
550 | /** @var \XoopsMemberHandler $memberHandler */ |
||
551 | $memberHandler = \xoops_getHandler('member'); |
||
552 | $xoopsMailerConfig = $configHandler->getConfigsByCat(\XOOPS_CONF_MAILER); |
||
553 | $xoopsMailer->setFromUser($memberHandler->getUser($xoopsMailerConfig['fromuid'])); |
||
554 | $xoopsMailer->setToUsers($notify_pm); |
||
555 | $xoopsMailer->setSubject($email_tpl['mail_subject']); // Set the subject of the email |
||
556 | $xoopsMailer->setFromName($xoopsConfig['sitename']); // Set a from address |
||
557 | $success = $xoopsMailer->send(true); |
||
558 | |||
559 | return $success; |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * Send the mail notification to selected users |
||
564 | * |
||
565 | * @param object|array $email_tpl object returned from getEmailTpl() function |
||
566 | * @param array $sendTo emails returned from getSubscribedStaff() function |
||
567 | * @param array $tags array of notification information |
||
568 | * @param string $fromEmail |
||
569 | * @return bool TRUE if success, FALSE if no success |
||
570 | */ |
||
571 | public function sendEventEmail($email_tpl, array $sendTo, array $tags, string $fromEmail = ''): bool |
||
572 | { |
||
573 | $notify_email = ''; |
||
574 | global $xoopsConfig; |
||
575 | |||
576 | $notify_email = $sendTo['email']; |
||
577 | |||
578 | $tags = \array_merge($tags, $this->getCommonTplVars()); // Retrieve the common template vars and add to array |
||
579 | $xoopsMailer = \xoops_getMailer(); |
||
580 | $xoopsMailer->useMail(); |
||
581 | |||
582 | foreach ($tags as $k => $v) { |
||
583 | $xoopsMailer->assign($k, \preg_replace('/&/i', '&', $v)); |
||
584 | } |
||
585 | $xoopsMailer->setTemplateDir($this->templateDir); // Set template dir |
||
586 | $xoopsMailer->setTemplate($email_tpl['mail_template'] . '.tpl'); // Set the template to be used |
||
587 | if ('' !== $fromEmail) { |
||
588 | $xoopsMailer->setFromEmail($fromEmail); |
||
589 | } |
||
590 | $xoopsMailer->setToEmails($notify_email); // Set who the email goes to |
||
591 | $xoopsMailer->setSubject($email_tpl['mail_subject']); // Set the subject of the email |
||
592 | $xoopsMailer->setFromName($xoopsConfig['sitename']); // Set a from address |
||
593 | $success = $xoopsMailer->send(true); |
||
594 | |||
595 | return $success; |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * Get a list of the common constants required for notifications |
||
600 | * |
||
601 | * @return array |
||
602 | */ |
||
603 | public function &getCommonTplVars(): array |
||
604 | { |
||
605 | global $xoopsConfig; |
||
606 | $tags = []; |
||
607 | $tags['X_MODULE'] = $this->module->getVar('name'); |
||
608 | $tags['X_SITEURL'] = \XHELP_SITE_URL; |
||
609 | $tags['X_SITENAME'] = $xoopsConfig['sitename']; |
||
610 | $tags['X_ADMINMAIL'] = $xoopsConfig['adminmail']; |
||
611 | $tags['X_MODULE_URL'] = \XHELP_BASE_URL . '/'; |
||
612 | |||
613 | return $tags; |
||
614 | } |
||
615 | |||
616 | /** |
||
617 | * Retrieve the directory where mail templates are stored |
||
618 | * |
||
619 | * @param string $language language used for xoops |
||
620 | * @return string |
||
621 | */ |
||
622 | public function getTemplateDir(string $language): string |
||
623 | { |
||
624 | $path = XOOPS_ROOT_PATH . '/modules/xhelp/language/' . $language . '/mail_template'; |
||
625 | if (\is_dir($path)) { |
||
626 | return $path; |
||
627 | } |
||
628 | |||
629 | return XOOPS_ROOT_PATH . '/modules/xhelp/language/english/mail_template'; |
||
630 | } |
||
631 | |||
632 | /** |
||
633 | * Returns the number of department notifications |
||
634 | * |
||
635 | * @return int number of department notifications |
||
636 | */ |
||
637 | public function getNumDeptNotifications(): int |
||
638 | { |
||
639 | $num = 0; |
||
640 | $templates = $this->module->getInfo('_email_tpl'); |
||
641 | foreach ($templates as $template) { |
||
642 | if ('dept' === $template['category']) { |
||
643 | ++$num; |
||
644 | } |
||
645 | } |
||
646 | |||
647 | return $num; |
||
648 | } |
||
649 | |||
650 | /** |
||
651 | * Returns the email address of the person causing the fire of the event |
||
652 | * |
||
653 | * @param int $uid uid of the user |
||
654 | * @return array email of user |
||
655 | */ |
||
656 | public function getEmail(int $uid): array |
||
657 | { |
||
658 | if (!$isStaff = $this->staffHandler->isStaff($uid)) { |
||
659 | return $this->getUserEmail($uid); |
||
660 | } |
||
661 | |||
662 | return $this->getStaffEmail($uid); |
||
663 | } |
||
664 | |||
665 | /** |
||
666 | * Confirm submission to user and notify staff members when new_ticket is triggered |
||
667 | * @param Ticket $ticket Ticket that was added |
||
668 | */ |
||
669 | public function new_ticket(Ticket $ticket): void |
||
670 | { |
||
671 | global $xhelp_isStaff; |
||
672 | global $xoopsUser; |
||
673 | $helper = Helper::getInstance(); |
||
674 | |||
675 | $departmentHandler = $this->helper->getHandler('Department'); |
||
676 | $displayName = $helper->getConfig('xhelp_displayName'); // Determines if username or real name is displayed |
||
677 | |||
678 | $tags = []; |
||
679 | $tags['TICKET_ID'] = $ticket->getVar('id'); |
||
680 | $tags['TICKET_SUBJECT'] = ($ticket->getVar('subject', 'n')); |
||
681 | $tags['TICKET_DESCRIPTION'] = ($ticket->getVar('description', 'n')); |
||
682 | $tags['TICKET_PRIORITY'] = Utility::getPriority($ticket->getVar('priority')); |
||
683 | $tags['TICKET_POSTED'] = $ticket->posted(); |
||
684 | $tags['TICKET_CREATED'] = Utility::getUsername($ticket->getVar('uid'), $displayName); |
||
685 | $tags['TICKET_SUPPORT_KEY'] = ($ticket->getVar('serverid') ? '{' . $ticket->getVar('emailHash') . '}' : ''); |
||
686 | $tags['TICKET_URL'] = \XHELP_BASE_URL . '/ticket.php?id=' . $ticket->getVar('id'); |
||
687 | $tags['TICKET_DEPARTMENT'] = ($departmentHandler->getNameById($ticket->getVar('department'))); |
||
688 | |||
689 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_NEWTICKET); |
||
690 | // $settings = $this->notificationHandler->create(); |
||
691 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
692 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
693 | |||
694 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { // If staff notification is enabled |
||
695 | $email_tpl = $this->getEmailTpl('dept', 'new_ticket', $this->module, $template_id); |
||
696 | if ($email_tpl) { // Send email to dept members |
||
697 | $sendTo = $this->getSubscribedStaff($ticket, $email_tpl['bit_value'], $settings); |
||
698 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
699 | } |
||
700 | } |
||
701 | if (\XHELP_NOTIF_USER_NO != $user_setting) { // If user notification is enabled |
||
702 | if ($ticket->getVar('serverid') > 0) { |
||
703 | //this ticket has been submitted by email |
||
704 | //get department email address |
||
705 | $departmentMailBoxHandler = $this->helper->getHandler('DepartmentMailBox'); |
||
706 | $server = $departmentMailBoxHandler->get($ticket->getVar('serverid')); |
||
707 | |||
708 | $tags['TICKET_SUPPORT_EMAIL'] = $server->getVar('emailaddress'); |
||
709 | |||
710 | $email_tpl = $this->getEmailTpl('ticket', 'new_this_ticket_via_email', $this->module, $template_id); |
||
711 | if ($email_tpl) { |
||
712 | $sendTo = $this->getUserEmail($ticket->getVar('uid')); |
||
713 | $success = $this->sendEvents($email_tpl, $sendTo, $tags, $server->getVar('emailaddress')); |
||
714 | } |
||
715 | } else { //this ticket has been submitted via the website |
||
716 | if (!$xhelp_isStaff) { |
||
717 | $email_tpl = $this->getEmailTpl('ticket', 'new_this_ticket', $this->module, $template_id); |
||
718 | if ($email_tpl) { // Send confirm email to submitter |
||
719 | $sendTo = $this->getUserEmail( |
||
720 | $ticket->getVar('uid') |
||
721 | ); // Will be the only person subscribed |
||
722 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
723 | } |
||
724 | } |
||
725 | } |
||
726 | } |
||
727 | } |
||
728 | |||
729 | /** |
||
730 | * Event: new_user_by_email |
||
731 | * Triggered after new user account is created during ticket submission |
||
732 | * @param string $password Password for new account |
||
733 | * @param \XoopsUser $user XOOPS user object for new account |
||
734 | */ |
||
735 | public function new_user_by_email(string $password, \XoopsUser $user): void |
||
736 | { |
||
737 | // Send Welcome Email to submitter |
||
738 | //global $xoopsUser; |
||
739 | |||
740 | $tags = []; |
||
741 | $tags['XOOPS_USER_NAME'] = $user->getVar('uname'); |
||
742 | $tags['XOOPS_USER_EMAIL'] = $user->getVar('email'); |
||
743 | $tags['XOOPS_USER_ID'] = $user->getVar('uname'); |
||
744 | $tags['XOOPS_USER_PASSWORD'] = $password; |
||
745 | $tags['X_UACTLINK'] = \XHELP_SITE_URL . '/user.php?op=actv&id=' . $user->getVar('uid') . '&actkey=' . $user->getVar('actkey'); |
||
746 | |||
747 | $email_tpl = $this->getEmailTpl('ticket', 'new_user_byemail', $this->module, $template_id); |
||
748 | if ($email_tpl) { |
||
749 | $sendTo = $this->getUserEmail($user->getVar('uid')); |
||
750 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
751 | } |
||
752 | } |
||
753 | |||
754 | /** |
||
755 | * Event: new_user_by_email |
||
756 | * Triggered after new user account is created during ticket submission |
||
757 | * @param string $password Password for new account |
||
758 | * @param \XoopsUser $xoopsUser XOOPS user object for new account |
||
759 | */ |
||
760 | public function new_user_activation0(string $password, \XoopsUser $xoopsUser): void |
||
761 | { |
||
762 | global $xoopsConfig; |
||
763 | $newid = $newuser->getVar('uid'); |
||
764 | $uname = $newuser->getVar('uname'); |
||
765 | $email = $newuser->getVar('email'); |
||
766 | |||
767 | $tags = []; |
||
768 | $tags['XOOPS_USER_NAME'] = $newuser->getVar('uname'); |
||
769 | $tags['XOOPS_USER_EMAIL'] = $newuser->getVar('email'); |
||
770 | $tags['XOOPS_USER_ID'] = $newuser->getVar('uname'); |
||
771 | $tags['XOOPS_USER_PASSWORD'] = $password; |
||
772 | $tags['X_UACTLINK'] = \XHELP_SITE_URL . '/user.php?op=actv&id=' . $newuser->getVar('uid') . '&actkey=' . $newuser->getVar('actkey'); |
||
773 | |||
774 | $email_tpl = $this->getEmailTpl('ticket', 'new_user_byemail', $this->module, $template_id); |
||
775 | if ($email_tpl) { |
||
776 | $sendTo = $this->getUserEmail($newuser->getVar('uid')); |
||
777 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
778 | } |
||
779 | } |
||
780 | |||
781 | /** |
||
782 | * Event: new_user_by_email |
||
783 | * Triggered after new user account is created during ticket submission |
||
784 | * @param string $password Password for new account |
||
785 | * @param \XoopsUser $xoopsUser XOOPS user object for new account |
||
786 | */ |
||
787 | public function new_user_activation1(string $password, \XoopsUser $xoopsUser): void |
||
788 | { |
||
789 | $tags = []; |
||
790 | $tags['XOOPS_USER_NAME'] = $user->getVar('uname'); |
||
791 | $tags['XOOPS_USER_EMAIL'] = $user->getVar('email'); |
||
792 | $tags['XOOPS_USER_ID'] = $user->getVar('uname'); |
||
793 | $tags['XOOPS_USER_PASSWORD'] = $password; |
||
794 | |||
795 | $email_tpl = $this->getEmailTpl('ticket', 'new_user_activation1', $this->module, $template_id); |
||
796 | if ($email_tpl) { |
||
797 | $sendTo = $this->getUserEmail($user->getVar('uid')); |
||
798 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
799 | } |
||
800 | |||
801 | $_POST['uname'] = $user->getVar('uname'); |
||
802 | $_POST['pass'] = $password; |
||
803 | |||
804 | // For backward compatibility |
||
805 | $_POST['uname'] = $user->getVar('uname'); |
||
806 | $_POST['pass'] = $password; |
||
807 | |||
808 | // $filename = XOOPS_ROOT_PATH . '/kernel/authenticationservice.php'; |
||
809 | // $foldername = XOOPS_ROOT_PATH . '/include/authenticationservices'; |
||
810 | // if (\file_exists($filename) && \file_exists($foldername)) { // check for ldap authentication hack |
||
811 | // /** @var \XoopsAuthFactory $authentication_service */ |
||
812 | // $authentication_service = \xoops_getHandler('authenticationservice'); |
||
813 | // if ($authentication_service) { |
||
814 | // $authentication_service->checkLogin(); |
||
815 | // } else { |
||
816 | // require_once XOOPS_ROOT_PATH . '/include/checklogin.php'; |
||
817 | // } |
||
818 | // } else { |
||
819 | // require_once XOOPS_ROOT_PATH . '/include/checklogin.php'; |
||
820 | // } |
||
821 | require_once XOOPS_ROOT_PATH . '/include/checklogin.php'; |
||
822 | } |
||
823 | |||
824 | /** |
||
825 | * Event: new_user_by_email |
||
826 | * Triggered after new user account is created during ticket submission |
||
827 | * @param string $password Password for new account |
||
828 | * @param \XoopsUser $xoopsUser XOOPS user object for new account |
||
829 | */ |
||
830 | public function new_user_activation2(string $password, \XoopsUser $xoopsUser): void |
||
831 | { |
||
832 | global $xoopsConfig; |
||
833 | $newid = $user->getVar('uid'); |
||
834 | $uname = $user->getVar('uname'); |
||
835 | $email = $user->getVar('email'); |
||
836 | |||
837 | $tags = []; |
||
838 | $tags['XOOPS_USER_NAME'] = $user->getVar('uname'); |
||
839 | $tags['XOOPS_USER_EMAIL'] = $user->getVar('email'); |
||
840 | $tags['XOOPS_USER_ID'] = $user->getVar('uname'); |
||
841 | $tags['XOOPS_USER_PASSWORD'] = $password; |
||
842 | |||
843 | $email_tpl = $this->getEmailTpl('ticket', 'new_user_activation2', $this->module, $template_id); |
||
844 | if ($email_tpl) { |
||
845 | $sendTo = $this->getUserEmail($user->getVar('uid')); |
||
846 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
847 | } |
||
848 | } |
||
849 | |||
850 | /** |
||
851 | * Event: new_response |
||
852 | * Triggered after a response has been added to a ticket |
||
853 | * @param Ticket|int $ticket Ticket containing response |
||
854 | * @param Response $response Response that was added |
||
855 | */ |
||
856 | public function new_response($ticket, Response $response): void |
||
857 | { |
||
858 | // If response is from staff member, send message to ticket submitter |
||
859 | // If response is from submitter, send message to owner, if no owner, send to department |
||
860 | |||
861 | global $xoopsUser, $xoopsConfig; |
||
862 | $helper = Helper::getInstance(); |
||
863 | |||
864 | $departmentHandler = $this->helper->getHandler('Department'); |
||
865 | |||
866 | if (!\is_object($ticket) && 0 === $ticket) { |
||
867 | $ticketHandler = $this->helper->getHandler('Ticket'); |
||
868 | $ticket = $ticketHandler->get($response->getVar('ticketid')); |
||
869 | } |
||
870 | |||
871 | $b_email_ticket = false; |
||
872 | $from = ''; |
||
873 | |||
874 | $tags = []; |
||
875 | $tags['TICKET_ID'] = $ticket->getVar('id'); |
||
876 | $tags['TICKET_URL'] = \XHELP_BASE_URL . '/ticket.php?id=' . $ticket->getVar('id'); |
||
877 | $tags['TICKET_RESPONSE'] = ($response->getVar('message', 'n')); |
||
878 | $tags['TICKET_SUBJECT'] = ($ticket->getVar('subject', 'n')); |
||
879 | $tags['TICKET_TIMESPENT'] = $response->getVar('timeSpent'); |
||
880 | $tags['TICKET_STATUS'] = Utility::getStatus($ticket->getVar('status')); |
||
881 | $displayName = $helper->getConfig('xhelp_displayName'); // Determines if username or real name is displayed |
||
882 | $tags['TICKET_RESPONDER'] = Utility::getUsername($xoopsUser->getVar('uid'), $displayName); |
||
883 | $tags['TICKET_POSTED'] = $response->posted('m'); |
||
884 | $tags['TICKET_SUPPORT_KEY'] = ''; |
||
885 | $tags['TICKET_SUPPORT_EMAIL'] = $xoopsConfig['adminmail']; |
||
886 | |||
887 | if ($ticket->getVar('serverid') > 0) { |
||
888 | $departmentMailBoxHandler = $this->helper->getHandler('DepartmentMailBox'); |
||
889 | |||
890 | $server = $departmentMailBoxHandler->get($ticket->getVar('serverid')); |
||
891 | if ($server) { |
||
892 | $from = $server->getVar('emailaddress'); |
||
893 | $tags['TICKET_SUPPORT_KEY'] = '{' . $ticket->getVar('emailHash') . '}'; |
||
894 | $tags['TICKET_SUPPORT_EMAIL'] = $from; |
||
895 | } |
||
896 | } |
||
897 | $owner = $ticket->getVar('ownership'); |
||
898 | if (0 == $owner) { |
||
899 | $tags['TICKET_OWNERSHIP'] = \_XHELP_NO_OWNER; |
||
900 | } else { |
||
901 | $tags['TICKET_OWNERSHIP'] = Utility::getUsername($owner, $displayName); |
||
902 | } |
||
903 | $tags['TICKET_DEPARTMENT'] = ($departmentHandler->getNameById($ticket->getVar('department'))); |
||
904 | |||
905 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_NEWRESPONSE); |
||
906 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
907 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
908 | |||
909 | $sendTo = []; |
||
910 | /** @var \XoopsMemberHandler $memberHandler */ |
||
911 | $memberHandler = \xoops_getHandler('member'); |
||
912 | $response_user = $memberHandler->getUser($response->getVar('uid')); |
||
913 | $response_email = $response_user->getVar('email'); |
||
914 | |||
915 | $aUsers = $this->getSubscribedUsers($ticket->getVar('id')); |
||
916 | |||
917 | if (\in_array($response_email, $aUsers)) { // If response from a submitter, send to staff and other submitters |
||
918 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { // Staff notification is enabled |
||
919 | $email_tpl = $this->getEmailTpl('dept', 'new_response', $this->module, $template_id); |
||
920 | if ($email_tpl) { // Send to staff members |
||
921 | $sendTo = $this->getSubscribedStaff($ticket, $email_tpl['bit_value'], $settings, $response->getVar('uid')); |
||
922 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
923 | } |
||
924 | } |
||
925 | unset($aUsers[$ticket->getVar('uid')]); // Remove response submitter from array |
||
926 | $sendTo = $aUsers; // Get array of user emails to send |
||
927 | } else { // If response from staff, send to submitters |
||
928 | // Also send to staff members if no owner |
||
929 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { // If notification is on |
||
930 | $email_tpl = $this->getEmailTpl('dept', 'new_response', $this->module, $template_id); |
||
931 | if ($email_tpl) { // Send to staff members |
||
932 | if (0 == $ticket->getVar('ownership')) { |
||
933 | $sendTo = $this->getSubscribedStaff($ticket, $email_tpl['bit_value'], $settings, $response->getVar('uid')); |
||
934 | } |
||
935 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
936 | } |
||
937 | } |
||
938 | $sendTo = $aUsers; |
||
939 | } |
||
940 | if (2 != $user_setting && 0 === $response->getVar('private')) { |
||
941 | $email_tpl = $this->getEmailTpl('ticket', 'new_this_response', $this->module, $template_id); |
||
942 | if ($email_tpl) { // Send to users |
||
943 | $success = $this->sendEvents($email_tpl, $sendTo, $tags, $from); |
||
944 | } |
||
945 | } |
||
946 | } |
||
947 | |||
948 | /** |
||
949 | * Event: update_priority |
||
950 | * Triggered after a ticket priority is modified |
||
951 | * Also See: batch_priority |
||
952 | * @param Ticket $ticket Ticket that was modified |
||
953 | * @param int $oldpriority Previous ticket priority |
||
954 | */ |
||
955 | public function update_priority(Ticket $ticket, int $oldpriority): void |
||
987 | } |
||
988 | } |
||
989 | |||
990 | /** |
||
991 | * Event: update_status |
||
992 | * Triggered after a ticket status change |
||
993 | * Also See: batch_status, close_ticket, reopen_ticket |
||
994 | * @param Ticket $ticket The ticket that was modified |
||
995 | * @param Status $oldstatus The previous ticket status |
||
996 | * @param Status $newstatus The new ticket status |
||
997 | */ |
||
998 | public function update_status(Ticket $ticket, Status $oldstatus, Status $newstatus): void |
||
999 | { |
||
1000 | //notify staff department of change |
||
1001 | //notify submitter |
||
1002 | global $xoopsUser; |
||
1003 | $departmentHandler = $this->helper->getHandler('Department'); |
||
1004 | |||
1005 | $tags = []; |
||
1006 | $tags['TICKET_ID'] = $ticket->getVar('id'); |
||
1007 | $tags['TICKET_URL'] = \XHELP_BASE_URL . '/ticket.php?id=' . $ticket->getVar('id'); |
||
1008 | // Added by marcan to get the ticket's subject available in the mail template |
||
1009 | $tags['TICKET_SUBJECT'] = ($ticket->getVar('subject', 'n')); |
||
1010 | // End of addition by marcan |
||
1011 | $tags['TICKET_OLD_STATUS'] = $oldstatus->getVar('description'); |
||
1012 | $tags['TICKET_OLD_STATE'] = Utility::getState($oldstatus->getVar('state')); |
||
1013 | $tags['TICKET_STATUS'] = $newstatus->getVar('description'); |
||
1014 | $tags['TICKET_STATE'] = Utility::getState($newstatus->getVar('state')); |
||
1015 | $tags['TICKET_UPDATEDBY'] = $xoopsUser->getVar('uname'); |
||
1016 | $tags['TICKET_DEPARTMENT'] = ($departmentHandler->getNameById($ticket->getVar('department'))); |
||
1017 | |||
1018 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_EDITSTATUS); |
||
1019 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
1020 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
1021 | |||
1022 | $email_tpl = $this->getEmailTpl('dept', 'changed_status', $this->module, $template_id); |
||
1023 | if ($email_tpl) { |
||
1024 | $sendTo = $this->getSubscribedStaff($ticket, $email_tpl['bit_value'], $settings); |
||
1025 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1026 | } |
||
1027 | $email_tpl = $this->getEmailTpl('ticket', 'changed_this_status', $this->module, $template_id); |
||
1028 | if ($email_tpl) { |
||
1029 | //$sendTo = $this->getEmail($ticket->getVar('uid')); |
||
1030 | $sendTo = $this->getSubscribedUsers($ticket->getVar('id')); |
||
1031 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1032 | } |
||
1033 | } |
||
1034 | |||
1035 | /** |
||
1036 | * Event: update_owner |
||
1037 | * Triggered after ticket ownership change (Individual) |
||
1038 | * Also See: batch_owner |
||
1039 | * @param Ticket $ticket Ticket that was changed |
||
1040 | * @param int|string|null $oldOwner UID of previous owner |
||
1041 | * @param int $newOwner UID of new owner |
||
1042 | */ |
||
1043 | public function update_owner(Ticket $ticket, $oldOwner, int $newOwner): void |
||
1044 | { |
||
1045 | //notify old owner, if assigned |
||
1046 | //notify new owner |
||
1047 | //notify submitter |
||
1048 | global $xoopsUser; |
||
1049 | $helper = Helper::getInstance(); |
||
1050 | |||
1051 | $departmentHandler = $this->helper->getHandler('Department'); |
||
1052 | |||
1053 | $displayName = $helper->getConfig('xhelp_displayName'); // Determines if username or real name is displayed |
||
1054 | |||
1055 | $tags = []; |
||
1056 | $tags['TICKET_ID'] = $ticket->getVar('id'); |
||
1057 | $tags['TICKET_URL'] = \XHELP_BASE_URL . '/ticket.php?id=' . $ticket->getVar('id'); |
||
1058 | $tags['TICKET_SUBJECT'] = ($ticket->getVar('subject', 'n')); |
||
1059 | $tags['TICKET_DESCRIPTION'] = ($ticket->getVar('description', 'n')); |
||
1060 | $tags['TICKET_OWNER'] = Utility::getUsername($ticket->getVar('ownership'), $displayName); |
||
1061 | $tags['SUBMITTED_OWNER'] = $xoopsUser->getVar('uname'); |
||
1062 | $tags['TICKET_STATUS'] = Utility::getStatus($ticket->getVar('status')); |
||
1063 | $tags['TICKET_PRIORITY'] = Utility::getPriority($ticket->getVar('priority')); |
||
1064 | $tags['TICKET_DEPARTMENT'] = ($departmentHandler->getNameById($ticket->getVar('department'))); |
||
1065 | |||
1066 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_EDITOWNER); |
||
1067 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
1068 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
1069 | $staff_options = $settings->getVar('staff_options') ?? ''; |
||
1070 | |||
1071 | $sendTo = []; |
||
1072 | if (\XHELP_NOTIF_STAFF_OWNER == $staff_setting) { |
||
1073 | if (!empty($oldOwner) |
||
1074 | && \_XHELP_NO_OWNER != $oldOwner) { // If there was an owner |
||
1075 | $email_tpl = $this->getEmailTpl('dept', 'new_owner', $this->module, $template_id); |
||
1076 | if ($email_tpl) { // Send them an email |
||
1077 | if ($this->isSubscribed($oldOwner, $email_tpl['bit_value'])) { // Check if the owner is subscribed |
||
1078 | $sendTo = $this->getStaffEmail($oldOwner, $ticket->getVar('department'), $staff_options); |
||
1079 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1080 | } |
||
1081 | } |
||
1082 | } |
||
1083 | if (0 != $ticket->getVar('ownership') |
||
1084 | && $ticket->getVar('ownership') != $xoopsUser->getVar('uid')) { // If owner is not current user |
||
1085 | $email_tpl = $this->getEmailTpl('dept', 'new_owner', $this->module, $template_id); |
||
1086 | if ($email_tpl) { // Send new owner email |
||
1087 | if ($this->isSubscribed($ticket->getVar('ownership'), $email_tpl['bit_value'])) { // Check if the owner is subscribed |
||
1088 | $sendTo = $this->getStaffEmail($ticket->getVar('ownership'), $ticket->getVar('department'), $staff_options); |
||
1089 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1090 | } |
||
1091 | } |
||
1092 | } |
||
1093 | } elseif (\XHELP_NOTIF_STAFF_DEPT == $staff_setting) { // Notify entire department |
||
1094 | $email_tpl = $this->getEmailTpl('dept', 'new_owner', $this->module, $template_id); |
||
1095 | if ($email_tpl) { |
||
1096 | $sendTo = $this->getSubscribedStaff($ticket, $email_tpl['bit_value'], $settings); |
||
1097 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1098 | } |
||
1099 | } |
||
1100 | |||
1101 | if (\XHELP_NOTIF_USER_NO != $user_setting) { |
||
1102 | $email_tpl = $this->getEmailTpl('ticket', 'new_this_owner', $this->module, $template_id); |
||
1103 | if ($email_tpl) { // Send to ticket submitter |
||
1104 | $sendTo = $this->getSubscribedUsers($ticket->getVar('id')); |
||
1105 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1106 | } |
||
1107 | } |
||
1108 | } |
||
1109 | |||
1110 | /** |
||
1111 | * Event: close_ticket |
||
1112 | * Triggered after a ticket's status change from a status |
||
1113 | * with a state of XHELP_STATE_UNRESOLVED to a status |
||
1114 | * with a state of XHELP_STATE_RESOLVED |
||
1115 | * Also See: update_status, reopen_ticket |
||
1116 | * @param Ticket $ticket The ticket that was closed |
||
1117 | */ |
||
1118 | public function close_ticket(Ticket $ticket): void |
||
1152 | } |
||
1153 | } |
||
1154 | } |
||
1155 | } |
||
1156 | |||
1157 | /** |
||
1158 | * Event: delete_ticket |
||
1159 | * Triggered after a ticket is deleted |
||
1160 | * @param Ticket $ticket Ticket that was deleted |
||
1161 | */ |
||
1162 | public function delete_ticket(Ticket $ticket): void |
||
1163 | { |
||
1164 | //notify staff department |
||
1165 | //notify submitter |
||
1166 | global $xoopsUser, $xoopsModule; |
||
1167 | $departmentHandler = $this->helper->getHandler('Department'); |
||
1168 | |||
1169 | $tags = []; |
||
1170 | $tags['TICKET_ID'] = $ticket->getVar('id'); |
||
1171 | $tags['TICKET_SUBJECT'] = ($ticket->getVar('subject', 'n')); |
||
1172 | $tags['TICKET_DESCRIPTION'] = ($ticket->getVar('description', 'n')); |
||
1173 | $tags['TICKET_PRIORITY'] = Utility::getPriority($ticket->getVar('priority')); |
||
1174 | $tags['TICKET_STATUS'] = Utility::getStatus($ticket->getVar('status')); |
||
1175 | $tags['TICKET_POSTED'] = $ticket->posted(); |
||
1176 | $tags['TICKET_DELETEDBY'] = $xoopsUser->getVar('uname'); |
||
1177 | $tags['TICKET_DEPARTMENT'] = ($departmentHandler->getNameById($ticket->getVar('department'))); |
||
1178 | |||
1179 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_DELTICKET); |
||
1180 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
1181 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
1182 | |||
1183 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { |
||
1184 | $email_tpl = $this->getEmailTpl('dept', 'removed_ticket', $this->module, $template_id); |
||
1185 | if ($email_tpl) { // Send to dept staff |
||
1186 | $sendTo = $this->getSubscribedStaff($ticket, $email_tpl['bit_value'], $settings); |
||
1187 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1188 | } |
||
1189 | } |
||
1190 | |||
1191 | if (\XHELP_NOTIF_USER_NO != $user_setting) { |
||
1192 | $status = $this->statusHandler->get($ticket->getVar('status')); |
||
1193 | if (2 != $status->getVar('state')) { |
||
1194 | $email_tpl = $this->getEmailTpl('ticket', 'removed_this_ticket', $this->module, $template_id); |
||
1195 | if ($email_tpl) { // Send to submitter |
||
1196 | //$sendTo = $this->getEmail($ticket->getVar('uid')); |
||
1197 | $sendTo = $this->getSubscribedUsers($ticket->getVar('id')); |
||
1198 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1199 | } |
||
1200 | } |
||
1201 | } |
||
1202 | } |
||
1203 | |||
1204 | /** |
||
1205 | * Event: edit_ticket |
||
1206 | * Triggered after a ticket is modified |
||
1207 | * @param array|Ticket $oldTicket Ticket information before modifications |
||
1208 | * @param Ticket $ticketInfo Ticket information after modifications |
||
1209 | */ |
||
1210 | public function edit_ticket($oldTicket, Ticket $ticketInfo): void |
||
1211 | { |
||
1212 | //notify staff department of change |
||
1213 | //notify submitter |
||
1214 | global $xoopsUser; |
||
1215 | $departmentHandler = $this->helper->getHandler('Department'); |
||
1216 | |||
1217 | $tags = []; |
||
1218 | $tags['TICKET_URL'] = \XHELP_BASE_URL . '/ticket.php?id=' . $ticketInfo->getVar('id'); |
||
1219 | $tags['TICKET_OLD_SUBJECT'] = ($oldTicket['subject']); |
||
1220 | $tags['TICKET_OLD_DESCRIPTION'] = ($oldTicket['description']); |
||
1221 | $tags['TICKET_OLD_PRIORITY'] = Utility::getPriority($oldTicket['priority']); |
||
1222 | $tags['TICKET_OLD_STATUS'] = $oldTicket['status']; |
||
1223 | $tags['TICKET_OLD_DEPARTMENT'] = $oldTicket['department']; |
||
1224 | $tags['TICKET_OLD_DEPTID'] = $oldTicket['department_id']; |
||
1225 | |||
1226 | $tags['TICKET_ID'] = $ticketInfo->getVar('id'); |
||
1227 | $tags['TICKET_SUBJECT'] = ($ticketInfo->getVar('subject', 'n')); |
||
1228 | $tags['TICKET_DESCRIPTION'] = ($ticketInfo->getVar('description', 'n')); |
||
1229 | $tags['TICKET_PRIORITY'] = Utility::getPriority($ticketInfo->getVar('priority')); |
||
1230 | $tags['TICKET_STATUS'] = Utility::getStatus($ticketInfo->getVar('status')); |
||
1231 | $tags['TICKET_MODIFIED'] = $xoopsUser->getVar('uname'); |
||
1232 | if ($tags['TICKET_OLD_DEPTID'] != $ticketInfo->getVar('department')) { |
||
1233 | $department = $departmentHandler->get($ticketInfo->getVar('department')); |
||
1234 | $tags['TICKET_DEPARTMENT'] = $department->getVar('department'); |
||
1235 | } else { |
||
1236 | $tags['TICKET_DEPARTMENT'] = $tags['TICKET_OLD_DEPARTMENT']; |
||
1237 | } |
||
1238 | |||
1239 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_EDITTICKET); |
||
1240 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
1241 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
1242 | |||
1243 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { |
||
1244 | $email_tpl = $this->getEmailTpl('dept', 'modified_ticket', $this->module, $template_id); |
||
1245 | if ($email_tpl) { // Send to dept staff |
||
1246 | $sendTo = $this->getSubscribedStaff($ticketInfo, $email_tpl['bit_value'], $settings); |
||
1247 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1248 | } |
||
1249 | } |
||
1250 | if (\XHELP_NOTIF_USER_NO != $user_setting) { |
||
1251 | $email_tpl = $this->getEmailTpl('ticket', 'modified_this_ticket', $this->module, $template_id); |
||
1252 | if ($email_tpl) { // Send to ticket submitter |
||
1253 | $sendTo = $this->getSubscribedUsers($ticketInfo->getVar('id')); |
||
1254 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1255 | } |
||
1256 | } |
||
1257 | } |
||
1258 | |||
1259 | /** |
||
1260 | * Event: edit_response |
||
1261 | * Triggered after a response has been modified |
||
1262 | * Also See: new_response |
||
1263 | * @param Ticket $ticket |
||
1264 | * @param Response $response Modified response |
||
1265 | * @param Ticket $oldticket Ticket before modifications |
||
1266 | * @param Response $oldresponse Response modifications |
||
1267 | * @internal param Ticket $nticket Ticket after modifications |
||
1268 | */ |
||
1269 | public function edit_response(Ticket $ticket, Response $response, Ticket $oldticket, Response $oldresponse): void |
||
1270 | { |
||
1271 | //if not modified by response submitter, notify response submitter |
||
1272 | //notify ticket submitter |
||
1273 | global $xoopsUser; |
||
1274 | $helper = Helper::getInstance(); |
||
1275 | |||
1276 | $departmentHandler = $this->helper->getHandler('Department'); |
||
1277 | $displayName = $helper->getConfig('xhelp_displayName'); // Determines if username or real name is displayed |
||
1278 | |||
1279 | $tags = []; |
||
1280 | $tags['TICKET_URL'] = \XHELP_BASE_URL . '/ticket.php?id=' . $ticket->getVar('id'); |
||
1281 | $tags['TICKET_OLD_RESPONSE'] = ($oldresponse->getVar('message', 'n')); |
||
1282 | $tags['TICKET_OLD_TIMESPENT'] = $oldresponse->getVar('timeSpent'); |
||
1283 | $tags['TICKET_OLD_STATUS'] = Utility::getStatus($oldticket->getVar('status')); |
||
1284 | $tags['TICKET_OLD_RESPONDER'] = Utility::getUsername($oldresponse->getVar('uid'), $displayName); |
||
1285 | $owner = $oldticket->getVar('ownership'); |
||
1286 | $tags['TICKET_OLD_OWNERSHIP'] = ($owner = 0 ? \_XHELP_NO_OWNER : Utility::getUsername($owner, $displayName)); |
||
1287 | $tags['TICKET_ID'] = $ticket->getVar('id'); |
||
1288 | $tags['RESPONSE_ID'] = $response->getVar('id'); |
||
1289 | $tags['TICKET_RESPONSE'] = ($response->getVar('message', 'n')); |
||
1290 | $tags['TICKET_TIMESPENT'] = $response->getVar('timeSpent'); |
||
1291 | $tags['TICKET_STATUS'] = Utility::getStatus($ticket->getVar('status')); |
||
1292 | $tags['TICKET_RESPONDER'] = $xoopsUser->getVar('uname'); |
||
1293 | $tags['TICKET_POSTED'] = $response->posted(); |
||
1294 | $owner = $ticket->getVar('ownership'); |
||
1295 | $tags['TICKET_OWNERSHIP'] = ($owner = 0 ? \_XHELP_NO_OWNER : Utility::getUsername($owner, $displayName)); |
||
1296 | $tags['TICKET_DEPARTMENT'] = ($departmentHandler->getNameById($ticket->getVar('department'))); |
||
1297 | |||
1298 | // Added by marcan to get the ticket's subject available in the mail template |
||
1299 | $tags['TICKET_SUBJECT'] = ($ticket->getVar('subject', 'n')); |
||
1300 | // End of addition by marcan |
||
1301 | |||
1302 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_EDITRESPONSE); |
||
1303 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
1304 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
1305 | |||
1306 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { |
||
1307 | $email_tpl = $this->getEmailTpl('dept', 'modified_response', $this->module, $template_id); |
||
1308 | if ($email_tpl) { // Notify dept staff |
||
1309 | $sendTo = $this->getSubscribedStaff($ticket, $email_tpl['bit_value'], $settings, $response->getVar('uid')); |
||
1310 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1311 | } |
||
1312 | } |
||
1313 | |||
1314 | if (\XHELP_NOTIF_USER_NO != $user_setting) { |
||
1315 | if (0 == $response->getVar('private')) { // Make sure if response is private, don't sent to user |
||
1316 | $email_tpl = $this->getEmailTpl('ticket', 'modified_this_response', $this->module, $template_id); |
||
1317 | if ($email_tpl) { // Notify ticket submitter |
||
1318 | $sendTo = $this->getSubscribedUsers($ticket->getVar('id')); |
||
1319 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1320 | } |
||
1321 | } |
||
1322 | } |
||
1323 | } |
||
1324 | |||
1325 | /** |
||
1326 | * Event: batch_dept |
||
1327 | * Triggered after a batch ticket department change |
||
1328 | * @param array $oldTickets The Ticket objects that were modified |
||
1329 | * @param int $dept The new department for the tickets |
||
1330 | * @return bool |
||
1331 | */ |
||
1332 | public function batch_dept(array $oldTickets, int $dept): bool |
||
1333 | { |
||
1334 | global $xoopsUser; |
||
1335 | |||
1336 | $departmentHandler = $this->helper->getHandler('Department'); |
||
1337 | $sDept = $departmentHandler->getNameById($dept); |
||
1338 | |||
1339 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_EDITTICKET); |
||
1340 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
1341 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
1342 | |||
1343 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { |
||
1344 | $dept_email_tpl = $this->getEmailTpl('dept', 'modified_ticket', $this->module, $template_id); |
||
1345 | if ($dept_email_tpl) { // Send to dept staff |
||
1346 | $deptEmails = $this->getSubscribedStaff($dept, $dept_email_tpl['bit_value'], $settings, $xoopsUser->getVar('uid')); |
||
1347 | } |
||
1348 | } else { |
||
1349 | $dept_email_tpl = false; |
||
1350 | } |
||
1351 | |||
1352 | $user_email_tpl = false; |
||
1353 | if (\XHELP_NOTIF_USER_NO != $user_setting) { |
||
1354 | $user_email_tpl = $this->getEmailTpl('ticket', 'modified_this_ticket', $this->module, $template_id); |
||
1355 | } |
||
1356 | |||
1357 | foreach ($oldTickets as $oldTicket) { |
||
1358 | $tags = []; |
||
1359 | $tags['TICKET_OLD_SUBJECT'] = ($oldTicket->getVar('subject', 'n')); |
||
1360 | $tags['TICKET_OLD_DESCRIPTION'] = ($oldTicket->getVar('description', 'n')); |
||
1361 | $tags['TICKET_OLD_PRIORITY'] = Utility::getPriority($oldTicket->getVar('priority')); |
||
1362 | $tags['TICKET_OLD_STATUS'] = Utility::getStatus($oldTicket->getVar('status')); |
||
1363 | $tags['TICKET_OLD_DEPARTMENT'] = $departmentHandler->getNameById($oldTicket->getVar('department')); |
||
1364 | $tags['TICKET_OLD_DEPTID'] = $oldTicket->getVar('department'); |
||
1365 | |||
1366 | $tags['TICKET_ID'] = $oldTicket->getVar('id'); |
||
1367 | $tags['TICKET_SUBJECT'] = $tags['TICKET_OLD_SUBJECT']; |
||
1368 | $tags['TICKET_DESCRIPTION'] = $tags['TICKET_OLD_DESCRIPTION']; |
||
1369 | $tags['TICKET_PRIORITY'] = $tags['TICKET_OLD_PRIORITY']; |
||
1370 | $tags['TICKET_STATUS'] = $tags['TICKET_OLD_STATUS']; |
||
1371 | $tags['TICKET_MODIFIED'] = $xoopsUser->getVar('uname'); |
||
1372 | $tags['TICKET_DEPARTMENT'] = $sDept; |
||
1373 | $tags['TICKET_URL'] = \XHELP_BASE_URL . '/ticket.php?id=' . $oldTicket->getVar('id'); |
||
1374 | |||
1375 | if ($dept_email_tpl) { |
||
1376 | $deptEmails = $this->getSubscribedStaff($oldTicket, $dept_email_tpl['bit_value'], $settings, $xoopsUser->getVar('uid')); |
||
1377 | $success = $this->sendEvents($dept_email_tpl, $deptEmails, $tags); |
||
1378 | } |
||
1379 | if ($user_email_tpl) { |
||
1380 | //$sendTo = $this->getEmail($oldTicket->getVar('uid')); |
||
1381 | $sendTo = $this->getSubscribedUsers($oldTicket->getVar('id')); |
||
1382 | $success = $this->sendEvents($user_email_tpl, $sendTo, $tags); |
||
1383 | } |
||
1384 | } |
||
1385 | |||
1386 | return true; |
||
1387 | } |
||
1388 | |||
1389 | /** |
||
1390 | * Event: batch_priority |
||
1391 | * Triggered after a batch ticket priority change |
||
1392 | * @param array $tickets The Ticket objects that were modified |
||
1393 | * @param int $priority The new ticket priority |
||
1394 | */ |
||
1395 | public function batch_priority(array $tickets, int $priority): void |
||
1443 | } |
||
1444 | } |
||
1445 | |||
1446 | /** |
||
1447 | * Event: batch_owner |
||
1448 | * Triggered after a batch ticket ownership change |
||
1449 | * @param array $tickets The Ticket objects that were modified |
||
1450 | * @param int $owner The XOOPS UID of the new owner |
||
1451 | * @return bool |
||
1452 | */ |
||
1453 | public function batch_owner(array $tickets, int $owner): bool |
||
1454 | { |
||
1455 | //notify old owner, if assigned |
||
1456 | //notify new owner |
||
1457 | //notify submitter |
||
1458 | global $xoopsUser; |
||
1459 | $helper = Helper::getInstance(); |
||
1460 | |||
1461 | $departmentHandler = $this->helper->getHandler('Department'); |
||
1462 | |||
1463 | $displayName = $helper->getConfig('xhelp_displayName'); // Determines if username or real name is displayed |
||
1464 | |||
1465 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_EDITOWNER); |
||
1466 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
1467 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
1468 | $staff_options = $settings->getVar('staff_options') ?? ''; |
||
1469 | |||
1470 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { |
||
1471 | $dept_email_tpl = $this->getEmailTpl('dept', 'new_owner', $this->module, $template_id); |
||
1472 | } else { |
||
1473 | $dept_email_tpl = false; |
||
1474 | } |
||
1475 | if (\XHELP_NOTIF_USER_NO != $user_setting) { |
||
1476 | $user_email_tpl = $this->getEmailTpl('ticket', 'new_this_owner', $this->module, $template_id); |
||
1477 | } else { |
||
1478 | $user_email_tpl = false; |
||
1479 | } |
||
1480 | $new_owner = Utility::getUsername($owner, $displayName); |
||
1481 | $submitted_by = $xoopsUser->getVar('uname'); |
||
1482 | $uid = $xoopsUser->getVar('uid'); |
||
1483 | |||
1484 | foreach ($tickets as $ticket) { |
||
1485 | $tags = []; |
||
1486 | $tags['TICKET_ID'] = $ticket->getVar('id'); |
||
1487 | $tags['TICKET_SUBJECT'] = ($ticket->getVar('subject', 'n')); |
||
1488 | $tags['TICKET_DESCRIPTION'] = ($ticket->getVar('description', 'n')); |
||
1489 | $tags['TICKET_OWNER'] = $new_owner; |
||
1490 | $tags['SUBMITTED_OWNER'] = $submitted_by; |
||
1491 | $tags['TICKET_STATUS'] = Utility::getStatus($ticket->getVar('status')); |
||
1492 | $tags['TICKET_PRIORITY'] = Utility::getPriority($ticket->getVar('priority')); |
||
1493 | $tags['TICKET_URL'] = \XHELP_BASE_URL . '/ticket.php?id=' . $ticket->getVar('id'); |
||
1494 | $tags['TICKET_DEPARTMENT'] = ($departmentHandler->getNameById($ticket->getVar('department'))); |
||
1495 | |||
1496 | $sendTo = []; |
||
1497 | if (0 != $ticket->getVar('ownership')) { // If there was an owner |
||
1498 | if ($dept_email_tpl) { // Send them an email |
||
1499 | if ($this->isSubscribed($ticket->getVar('ownership'), $dept_email_tpl['bit_value'])) { // Check if the owner is subscribed |
||
1500 | $sendTo = $this->getStaffEmail($ticket->getVar('ownership'), $ticket->getVar('department'), $staff_options); |
||
1501 | $success = $this->sendEvents($dept_email_tpl, $sendTo, $tags); |
||
1502 | } |
||
1503 | } |
||
1504 | } |
||
1505 | if ($owner != $uid) { |
||
1506 | if ($dept_email_tpl) { // Send new owner email |
||
1507 | if ($this->isSubscribed($owner, $dept_email_tpl['bit_value'])) { // Check if the owner is subscribed |
||
1508 | $sendTo = $this->getStaffEmail($owner, $ticket->getVar('department'), $staff_options); |
||
1509 | $success = $this->sendEvents($dept_email_tpl, $sendTo, $tags); |
||
1510 | } |
||
1511 | } |
||
1512 | } |
||
1513 | if ($user_email_tpl) { |
||
1514 | //$sendTo = $this->getEmail($ticket->getVar('uid')); |
||
1515 | $sendTo = $this->getSubscribedUsers($ticket->getVar('id')); |
||
1516 | $success = $this->sendEvents($user_email_tpl, $sendTo, $tags); |
||
1517 | } |
||
1518 | } |
||
1519 | |||
1520 | return true; |
||
1521 | } |
||
1522 | |||
1523 | /** |
||
1524 | * Event: batch_status |
||
1525 | * Triggered after a batch ticket status change |
||
1526 | * @param array $tickets The Ticket objects that were modified |
||
1527 | * @param int $newstatus The new ticket status |
||
1528 | * @return bool |
||
1529 | */ |
||
1530 | public function batch_status(array $tickets, int $newstatus): bool |
||
1531 | { |
||
1532 | //notify staff department of change |
||
1533 | //notify submitter |
||
1534 | global $xoopsUser; |
||
1535 | $departmentHandler = $this->helper->getHandler('Department'); |
||
1536 | |||
1537 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_EDITSTATUS); |
||
1538 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
1539 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
1540 | |||
1541 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { |
||
1542 | $dept_email_tpl = $this->getEmailTpl('dept', 'changed_status', $this->module, $template_id); |
||
1543 | } else { |
||
1544 | $dept_email_tpl = false; |
||
1545 | } |
||
1546 | if (\XHELP_NOTIF_USER_NO != $user_setting) { |
||
1547 | $user_email_tpl = $this->getEmailTpl('ticket', 'changed_this_status', $this->module, $template_id); |
||
1548 | } else { |
||
1549 | $user_email_tpl = false; |
||
1550 | } |
||
1551 | $sStatus = Utility::getStatus($newstatus); |
||
1552 | $uname = $xoopsUser->getVar('uname'); |
||
1553 | $uid = $xoopsUser->getVar('uid'); |
||
1554 | |||
1555 | foreach ($tickets as $ticket) { |
||
1556 | $tags = []; |
||
1557 | $tags['TICKET_ID'] = $ticket->getVar('id'); |
||
1558 | $tags['TICKET_URL'] = \XHELP_BASE_URL . '/ticket.php?id=' . $ticket->getVar('id'); |
||
1559 | |||
1560 | // Added by marcan to get the ticket's subject available in the mail template |
||
1561 | $tags['TICKET_SUBJECT'] = ($ticket->getVar('subject', 'n')); |
||
1562 | // End of addition by marcan |
||
1563 | |||
1564 | $tags['TICKET_OLD_STATUS'] = Utility::getStatus($ticket->getVar('status')); |
||
1565 | $tags['TICKET_STATUS'] = $sStatus; |
||
1566 | $tags['TICKET_UPDATEDBY'] = $uname; |
||
1567 | $tags['TICKET_DEPARTMENT'] = ($departmentHandler->getNameById($ticket->getVar('department'))); |
||
1568 | |||
1569 | if ($dept_email_tpl) { |
||
1570 | $sendTo = $this->getSubscribedStaff($ticket, $dept_email_tpl['bit_value'], $settings); |
||
1571 | $success = $this->sendEvents($dept_email_tpl, $sendTo, $tags); |
||
1572 | } |
||
1573 | if ($user_email_tpl) { |
||
1574 | $sendTo = $this->getSubscribedUsers($ticket->getVar('id')); |
||
1575 | $success = $this->sendEvents($user_email_tpl, $sendTo, $tags); |
||
1576 | } |
||
1577 | } |
||
1578 | |||
1579 | return true; |
||
1580 | } |
||
1581 | |||
1582 | /** |
||
1583 | * Event: batch_delete_ticket |
||
1584 | * Triggered after a batch ticket deletion |
||
1585 | * @param array $tickets The Ticket objects that were deleted |
||
1586 | * @return bool |
||
1587 | */ |
||
1588 | public function batch_delete_ticket(array $tickets): bool |
||
1589 | { |
||
1590 | //notify staff department |
||
1591 | //notify submitter (if ticket is not closed) |
||
1592 | global $xoopsUser, $xoopsModule; |
||
1593 | |||
1594 | $uname = $xoopsUser->getVar('uname'); |
||
1595 | $uid = $xoopsUser->getVar('uid'); |
||
1596 | $staffHandler = $this->helper->getHandler('Staff'); |
||
1597 | $isStaff = $staffHandler->isStaff($uid); |
||
1598 | $departmentHandler = $this->helper->getHandler('Department'); |
||
1599 | |||
1600 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_DELTICKET); |
||
1601 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
1602 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
1603 | |||
1604 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { |
||
1605 | $dept_email_tpl = $this->getEmailTpl('dept', 'removed_ticket', $this->module, $template_id); |
||
1606 | } else { |
||
1607 | $dept_email_tpl = false; |
||
1608 | } |
||
1609 | if (\XHELP_NOTIF_USER_NO != $user_setting) { |
||
1610 | $user_email_tpl = $this->getEmailTpl('ticket', 'removed_this_ticket', $this->module, $template_id); |
||
1611 | } else { |
||
1612 | $user_email_tpl = false; |
||
1613 | } |
||
1614 | |||
1615 | foreach ($tickets as $ticket) { |
||
1616 | $tags = []; |
||
1617 | $tags['TICKET_ID'] = $ticket->getVar('id'); |
||
1618 | $tags['TICKET_SUBJECT'] = ($ticket->getVar('subject', 'n')); |
||
1619 | $tags['TICKET_DESCRIPTION'] = ($ticket->getVar('description', 'n')); |
||
1620 | $tags['TICKET_PRIORITY'] = Utility::getPriority($ticket->getVar('priority')); |
||
1621 | $tags['TICKET_STATUS'] = Utility::getStatus($ticket->getVar('status')); |
||
1622 | $tags['TICKET_POSTED'] = $ticket->posted(); |
||
1623 | $tags['TICKET_DELETEDBY'] = $uname; |
||
1624 | $tags['TICKET_DEPARTMENT'] = ($departmentHandler->getNameById($ticket->getVar('department'))); |
||
1625 | |||
1626 | if ($dept_email_tpl) { |
||
1627 | $sendTo = $this->getSubscribedStaff($ticket, $dept_email_tpl['bit_value'], $settings); |
||
1628 | $success = $this->sendEvents($dept_email_tpl, $sendTo, $tags); |
||
1629 | } |
||
1630 | |||
1631 | if ($user_email_tpl) { |
||
1632 | $status = $this->statusHandler->get($ticket->getVar('status')); |
||
1633 | if ($isStaff || (!$isStaff && 2 != $status->getVar('state'))) { // Send to ticket submitter |
||
1634 | //$sendTo = $this->getEmail($ticket->getVar('uid')); |
||
1635 | $sendTo = $this->getSubscribedUsers($ticket->getVar('id')); |
||
1636 | $success = $this->sendEvents($user_email_tpl, $sendTo, $tags); |
||
1637 | } |
||
1638 | } |
||
1639 | } |
||
1640 | |||
1641 | return true; |
||
1642 | } |
||
1643 | |||
1644 | /** |
||
1645 | * Event: batch_response |
||
1646 | * Triggered after a batch response addition |
||
1647 | * Note: the $response->getVar('ticketid') field is empty for this function |
||
1648 | * @param array $tickets The Ticket objects that were modified |
||
1649 | * @param Response $response The response added to each ticket |
||
1650 | */ |
||
1651 | public function batch_response(array $tickets, Response $response): void |
||
1744 | } |
||
1745 | } |
||
1746 | } |
||
1747 | |||
1748 | /** |
||
1749 | * Event: merge_tickets |
||
1750 | * Triggered after two tickets are merged |
||
1751 | * @param int $ticket1 First ticketid being merged |
||
1752 | * @param int $ticket2 Second ticketid being merged |
||
1753 | * @param int $newTicket Resulting ticketid after merge |
||
1754 | */ |
||
1755 | public function merge_tickets(int $ticket1, int $ticket2, int $newTicket): void |
||
1756 | { |
||
1757 | global $xoopsUser; |
||
1758 | /** @var \XoopsModules\Xhelp\TicketHandler $ticketHandler */ |
||
1759 | $ticketHandler = $this->helper->getHandler('Ticket'); |
||
1760 | /** @var \XoopsModules\Xhelp\Ticket $ticket */ |
||
1761 | $ticket = $ticketHandler->get($newTicket); |
||
1762 | |||
1763 | $tags = []; |
||
1764 | $tags['TICKET_MERGER'] = $xoopsUser->getVar('uname'); |
||
1765 | $tags['TICKET1'] = $ticket1; |
||
1766 | $tags['TICKET2'] = $ticket2; |
||
1767 | $tags['TICKET_URL'] = \XHELP_BASE_URL . '/ticket.php?id=' . $newTicket; |
||
1768 | |||
1769 | $settings = $this->notificationHandler->get(\XHELP_NOTIF_MERGETICKET); |
||
1770 | $staff_setting = $settings->getVar('staff_setting') ?? ''; |
||
1771 | $user_setting = $settings->getVar('user_setting') ?? ''; |
||
1772 | |||
1773 | if (\XHELP_NOTIF_STAFF_NONE != $staff_setting) { |
||
1774 | $email_tpl = $this->getEmailTpl('dept', 'merge_ticket', $this->module, $template_id); |
||
1775 | if ($email_tpl) { // Send email to dept members |
||
1776 | $sendTo = $this->getSubscribedStaff($ticket, $email_tpl['bit_value'], $settings); |
||
1777 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1778 | } |
||
1779 | } |
||
1780 | |||
1781 | if (\XHELP_NOTIF_USER_NO != $user_setting) { |
||
1782 | $email_tpl = $this->getEmailTpl('ticket', 'merge_this_ticket', $this->module, $template_id); |
||
1783 | if ($email_tpl) { // Send confirm email to submitter |
||
1784 | //$sendTo = $this->getEmail($ticket->getVar('uid')); |
||
1785 | $sendTo = $this->getSubscribedUsers($newTicket); |
||
1786 | $success = $this->sendEvents($email_tpl, $sendTo, $tags); |
||
1787 | } |
||
1788 | } |
||
1789 | } |
||
1790 | |||
1791 | /** |
||
1792 | * Event: new_faq |
||
1793 | * Triggered after FAQ addition |
||
1794 | * @param Ticket $ticket Ticket used as base for FAQ |
||
1795 | * @param Faq $faq FAQ that was added |
||
1796 | */ |
||
1797 | public function new_faq(Ticket $ticket, Faq $faq): void |
||
1798 | { |
||
1799 | } |
||
1800 | |||
1801 | /** |
||
1802 | * Only have 1 instance of class used |
||
1803 | * @return Service {@link Service} |
||
1804 | */ |
||
1805 | public static function getInstance(): Service |
||
1813 | } |
||
1814 | |||
1815 | public function attachEvents(): void |
||
1816 | { |
||
1817 | $this->attachEvent('batch_delete_ticket', $this); |
||
1818 | $this->attachEvent('batch_dept', $this); |
||
1819 | $this->attachEvent('batch_owner', $this); |
||
1820 | $this->attachEvent('batch_priority', $this); |
||
1821 | $this->attachEvent('batch_response', $this); |
||
1822 | $this->attachEvent('batch_status', $this); |
||
1823 | $this->attachEvent('close_ticket', $this); |
||
1824 | $this->attachEvent('delete_ticket', $this); |
||
1825 | $this->attachEvent('edit_response', $this); |
||
1833 | } |
||
1834 | } |
||
1835 |