Add SMS event to Secure Messaging (#6594)
[openemr.git] / interface / modules / custom_modules / oe-module-faxsms / src / Events / NotificationEventListener.php
blob39788b7ac8647d2197dc3c011977cb34074de449
1 <?php
3 /**
4 * Fax SMS Module Member
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Jerry Padgett <sjpadgett@gmail.com>
9 * @copyright Copyright (c) 2023 Jerry Padgett <sjpadgett@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General public License 3
13 namespace OpenEMR\Modules\FaxSMS\Events;
15 use MyMailer;
16 use OpenEMR\Events\Messaging\SendNotificationEvent;
17 use OpenEMR\Modules\FaxSMS\Controller\AppDispatch;
18 use OpenEMR\Modules\FaxSMS\Controller\TwilioSMSClient;
19 use OpenEMR\Services\DocumentTemplates\DocumentTemplateService;
20 use Symfony\Component\EventDispatcher\EventDispatcher;
21 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23 class NotificationEventListener implements EventSubscriberInterface
25 private null|TwilioSMSClient $clientApp;
26 private DocumentTemplateService $docClient;
28 public function __construct()
32 public static function getSubscribedEvents(): array
34 return [
35 SendNotificationEvent::class => 'onNotifyEvent',
39 public function subscribeToEvents(EventDispatcher $eventDispatcher)
41 $eventDispatcher->addListener('sendNotification.send', [$this, 'onNotifyEvent']);
44 public function onNotifyEvent(SendNotificationEvent $event)
46 $this->clientApp = AppDispatch::getApiService('sms');
47 $this->docClient = new DocumentTemplateService();
48 $id = $event->getPid();
49 $data = $event->getEventData() ?? [];
50 $patient = $event->fetchPatientDetails($id);
51 $data['recipient_phone'] = $data['recipient_phone'] ?? null;
52 $recipientPhone = $data['recipient_phone'] ?: $patient['phone'];
53 $message_template = $data['template_name'] ?? 'Default Notification';
55 if (empty($data['alt_content'] ?? '')) {
56 $message = $this->docClient->getTemplateListByCategory('notification_template', '-1', $message_template)['template_content'] ?? '';
57 } else {
58 $message = $data['alt_content'];
61 if ($patient['hipaa_allowsms'] == 'YES') {
62 $this->clientApp->sendSMS(
63 $recipientPhone,
64 "",
65 $message,
66 null // will get from phone from credentials
70 if (!empty($patient['email']) && ($data['include_email'] ?? false) && ($patient['hipaa_allowemail'] == 'YES')) {
71 $this->emailNotification($patient['email'], $message);
75 public function emailNotification($email, $body, $file = null): string
77 $from_name = ($user['fname'] ?? '') . ' ' . ($user['lname'] ?? '');
78 $mail = new MyMailer();
79 $from_name = text($from_name);
80 $from = $GLOBALS["practice_return_email_path"];
81 $mail->AddReplyTo($from, $from_name);
82 $mail->SetFrom($from, $from);
83 $to = $email;
84 $to_name = $email;
85 $mail->AddAddress($to, $to_name);
86 $subject = xlt("Your clinic ask for your attention.");
87 $mail->Subject = $subject;
88 $mail->Body = $body;
89 if (!empty($file)) {
90 $mail->AddAttachment($file);
92 return $mail->Send();