| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_Reminders_Command_ |
| 3 |
{ |
| 4 |
public function send( SH4_Reminders_Model $model ); |
| 5 |
} |
| 6 |
|
| 7 |
#[AllowDynamicProperties] |
| 8 |
class SH4_Reminders_Command implements SH4_Reminders_Command_ |
| 9 |
{ |
| 10 |
public function __construct( |
| 11 |
HC3_Settings $settings, |
| 12 |
HC3_Time $t, |
| 13 |
|
| 14 |
SH4_Reminders_QueryLog $remindersQueryLog, |
| 15 |
SH4_Reminders_CommandLog $remindersCommandLog, |
| 16 |
|
| 17 |
HC3_Notificator $notificator, |
| 18 |
SH4_Notifications_Service $notificationsService, |
| 19 |
SH4_Notifications_Template $notificationsTemplate, |
| 20 |
HC3_Hooks $hooks |
| 21 |
) |
| 22 |
{ |
| 23 |
$this->t = $t; |
| 24 |
$this->settings = $hooks->wrap( $settings ); |
| 25 |
|
| 26 |
$this->remindersQueryLog = $hooks->wrap( $remindersQueryLog ); |
| 27 |
$this->remindersCommandLog = $hooks->wrap( $remindersCommandLog ); |
| 28 |
|
| 29 |
$this->notificationsService = $hooks->wrap( $notificationsService ); |
| 30 |
$this->notificationsTemplate = $hooks->wrap( $notificationsTemplate ); |
| 31 |
$this->notificator = $notificator; |
| 32 |
} |
| 33 |
|
| 34 |
public function send( SH4_Reminders_Model $model ) |
| 35 |
{ |
| 36 |
// already sent? |
| 37 |
if( $model->log ) return; |
| 38 |
|
| 39 |
$user = $model->user; |
| 40 |
$shifts = $model->shifts; |
| 41 |
|
| 42 |
// do send |
| 43 |
$wasOff = $this->notificator->isOn() ? FALSE : TRUE; |
| 44 |
if( $wasOff ){ |
| 45 |
$this->notificator->setOn(); |
| 46 |
} |
| 47 |
|
| 48 |
$templateSubjectDaily = $this->settings->get( 'reminders_template_subject_daily' ); |
| 49 |
$templateSubjectWeekly = $this->settings->get( 'reminders_template_subject_weekly' ); |
| 50 |
$templateSubjectMonthly = $this->settings->get( 'reminders_template_subject_monthly' ); |
| 51 |
|
| 52 |
$templateSubject = NULL; |
| 53 |
|
| 54 |
if( $model::RANGE_DAILY == $model->range ){ |
| 55 |
$templateSubject = $templateSubjectDaily; |
| 56 |
$dateLabel = $this->t->setDateDb( $model->date )->formatDateWithWeekday(); |
| 57 |
} |
| 58 |
|
| 59 |
if( $model::RANGE_WEEKLY == $model->range ){ |
| 60 |
$templateSubject = $templateSubjectWeekly; |
| 61 |
$startDate = $this->t->setDateDb( $model->date )->setStartWeek()->formatDateDb(); |
| 62 |
$endDate = $this->t->modify('+1 week')->modify('-1 day')->formatDateDb(); |
| 63 |
$dateLabel = $this->t->formatDateRange( $startDate, $endDate ); |
| 64 |
} |
| 65 |
|
| 66 |
if( $model::RANGE_MONTHLY == $model->range ){ |
| 67 |
$templateSubject = $templateSubjectMonthly; |
| 68 |
$startDate = $this->t->setDateDb( $model->date )->setStartMonth()->formatDateDb(); |
| 69 |
$endDate = $this->t->modify('+1 month')->modify('-1 day')->formatDateDb(); |
| 70 |
$dateLabel = $this->t->formatDateRange( $startDate, $endDate ); |
| 71 |
} |
| 72 |
|
| 73 |
$templateSubject = str_ireplace( '{DATELABEL}', $dateLabel, $templateSubject ); |
| 74 |
|
| 75 |
$employeeId = $model->employee->getId(); |
| 76 |
$employeeTitle = $model->employee->getTitle(); |
| 77 |
|
| 78 |
$templateSubject = str_ireplace( '{EMPLOYEE_ID}', $employeeId, $templateSubject ); |
| 79 |
$templateSubject = str_ireplace( '{EMPLOYEE_NAME}', $employeeTitle, $templateSubject ); |
| 80 |
|
| 81 |
$msg = array(); |
| 82 |
$msg[] = $templateSubject; |
| 83 |
|
| 84 |
foreach( $shifts as $shift ){ |
| 85 |
$calendar = $shift->getCalendar(); |
| 86 |
$oneTemplate = $this->notificationsService->getTemplate( $calendar, 'email_employee_publish' ); |
| 87 |
|
| 88 |
// remove subject |
| 89 |
$oneTemplate = explode( "\n", $oneTemplate ); |
| 90 |
array_shift( $oneTemplate ); |
| 91 |
$oneTemplate = join( "\n", $oneTemplate ); |
| 92 |
|
| 93 |
$msg[] = $this->notificationsTemplate->parse( $oneTemplate, $shift ); |
| 94 |
$msg[] = ''; |
| 95 |
} |
| 96 |
$msg = join( "\n", $msg ); |
| 97 |
|
| 98 |
$this->notificator |
| 99 |
->queue( $user, 'email_employee_reminder', $msg ) |
| 100 |
; |
| 101 |
|
| 102 |
$this->notificator->send(); |
| 103 |
|
| 104 |
if( $wasOff ){ |
| 105 |
$this->notificator->setOff(); |
| 106 |
} |
| 107 |
|
| 108 |
$modelLog = new SH4_Reminders_ModelLog; |
| 109 |
$modelLog->range = $model->range; |
| 110 |
$modelLog->date = $model->date; |
| 111 |
$modelLog->employee_id = $model->employee->getId(); |
| 112 |
|
| 113 |
$this->remindersCommandLog->create( $modelLog ); |
| 114 |
} |
| 115 |
} |