| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Reminders_CommandSms |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Settings $settings, |
| 7 |
HC3_Time $t, |
| 8 |
HC3_Translate $translate, |
| 9 |
|
| 10 |
SH4_Reminders_Sms $sms, |
| 11 |
|
| 12 |
SH4_Reminders_QueryLog $remindersQueryLog, |
| 13 |
SH4_Reminders_CommandLog $remindersCommandLog, |
| 14 |
|
| 15 |
HC3_Notificator $notificator, |
| 16 |
SH4_Notifications_Service $notificationsService, |
| 17 |
SH4_Notifications_Template $notificationsTemplate, |
| 18 |
HC3_Hooks $hooks |
| 19 |
) |
| 20 |
{ |
| 21 |
$this->t = $t; |
| 22 |
$this->translate = $translate; |
| 23 |
$this->settings = $hooks->wrap( $settings ); |
| 24 |
|
| 25 |
$this->sms = $hooks->wrap( $sms ); |
| 26 |
|
| 27 |
$this->remindersQueryLog = $hooks->wrap( $remindersQueryLog ); |
| 28 |
$this->remindersCommandLog = $hooks->wrap( $remindersCommandLog ); |
| 29 |
|
| 30 |
$this->notificationsService = $hooks->wrap( $notificationsService ); |
| 31 |
$this->notificationsTemplate = $hooks->wrap( $notificationsTemplate ); |
| 32 |
$this->notificator = $notificator; |
| 33 |
} |
| 34 |
|
| 35 |
public function send( SH4_Reminders_Model $model ) |
| 36 |
{ |
| 37 |
// already sent? |
| 38 |
if( $model->log ) return; |
| 39 |
|
| 40 |
$employee = $model->employee; |
| 41 |
$toNumber = $this->sms->getEmployeePhone( $employee->getId() ); |
| 42 |
|
| 43 |
if( ! $toNumber ){ |
| 44 |
$user = $model->user; |
| 45 |
if( $user ){ |
| 46 |
$toNumber = $this->sms->getUserPhone( $user->getId() ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if( ! $toNumber ){ |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$shifts = $model->shifts; |
| 55 |
|
| 56 |
// do send |
| 57 |
$wasOff = $this->notificator->isOn() ? FALSE : TRUE; |
| 58 |
if( $wasOff ){ |
| 59 |
$this->notificator->setOn(); |
| 60 |
} |
| 61 |
|
| 62 |
$templateSubject = $this->settings->get( 'reminders_template_subject_dailysms' ); |
| 63 |
|
| 64 |
if( $model::RANGE_DAILY_SMS == $model->range ){ |
| 65 |
$dateLabel = $this->t->setDateDb( $model->date )->formatDateWithWeekday(); |
| 66 |
} |
| 67 |
|
| 68 |
$templateSubject = str_ireplace( '{DATELABEL}', $dateLabel, $templateSubject ); |
| 69 |
|
| 70 |
$employeeId = $model->employee->getId(); |
| 71 |
$employeeTitle = $model->employee->getTitle(); |
| 72 |
|
| 73 |
$templateSubject = str_ireplace( '{EMPLOYEE_ID}', $employeeId, $templateSubject ); |
| 74 |
$templateSubject = str_ireplace( '{EMPLOYEE_NAME}', $employeeTitle, $templateSubject ); |
| 75 |
|
| 76 |
foreach( $shifts as $shift ){ |
| 77 |
$msg = $templateSubject; |
| 78 |
$msg = $this->notificationsTemplate->parse( $msg, $shift ); |
| 79 |
$msg = $this->translate->translate( $msg ); |
| 80 |
|
| 81 |
$this->sms->send( $toNumber, $msg ); |
| 82 |
} |
| 83 |
|
| 84 |
if( $wasOff ){ |
| 85 |
$this->notificator->setOff(); |
| 86 |
} |
| 87 |
|
| 88 |
$modelLog = new SH4_Reminders_ModelLog; |
| 89 |
$modelLog->range = $model->range; |
| 90 |
$modelLog->date = $model->date; |
| 91 |
$modelLog->employee_id = $model->employee->getId(); |
| 92 |
|
| 93 |
$this->remindersCommandLog->create( $modelLog ); |
| 94 |
} |
| 95 |
} |