diff --git a/bin/scheduler.php b/bin/scheduler.php index f69b4c844b1e612e018b2ed3d8cc9668f70b7b28..287aa80c02230fef2c4bb68737c90282b8467a23 100644 --- a/bin/scheduler.php +++ b/bin/scheduler.php @@ -25,7 +25,7 @@ if (empty($restUser[0])) { $restUser = $restUser[0]; $GLOBALS['id'] = $restUser['id']; -$applicationUrl = UrlController::getCoreUrl(); // TODO get url from configuration ? +$applicationUrl = UrlController::getCoreUrl(); // TODO get url from configuration $schedules = NotificationsScheduleModel::get(); @@ -61,7 +61,7 @@ foreach ($schedules as $schedule) { continue; } elseif (!empty($schedule['frequency']) && $schedule['frequency_mode'] == 'minutes' && $difference->i < $schedule['frequency']) { continue; - } elseif(!empty($schedule['frequency']) && $schedule['frequency_mode'] == 'minutes' && $difference->h < $schedule['frequency']) { + } elseif(!empty($schedule['frequency']) && $schedule['frequency_mode'] == 'hours' && $difference->h < $schedule['frequency']) { continue; } } @@ -71,7 +71,7 @@ foreach ($schedules as $schedule) { if ($currentDate < $startTime) { continue; - } elseif (!empty($schedule['frequency']) && $lastExecution < $currentDate) { + } elseif (!empty($schedule['frequency']) && $endTime < $currentDate) { continue; } @@ -88,10 +88,13 @@ foreach ($schedules as $schedule) { 'data' => [$schedule['type']] ]); - if ($schedule['type'] == 'next_user') { - $result = runNextUserNotification(['stack' => $notificationStack, 'restUser' => $restUser, 'applicationUrl' => $applicationUrl]); - } elseif ($schedule['type'] == 'typist_END') { - $result = runTypistEnd(['stack' => $notificationStack, 'restUser' => $restUser, 'applicationUrl' => $applicationUrl]); + if (!empty($notificationStack)) { + $result = sendNotifications([ + 'type' => $schedule['type'], + 'stack' => $notificationStack, + 'restUser' => $restUser, + 'applicationUrl' => $applicationUrl + ]); } NotificationsScheduleModel::update([ @@ -101,11 +104,31 @@ foreach ($schedules as $schedule) { ]); } -function runNextUserNotification(array $args) +function sendNotifications(array $args) { $notificationStack = $args['stack']; $restUser = $args['restUser']; + if ($args['type'] == 'next_user') { + $subjectKey = 'notificationDocumentsAddedSubject'; + $bodyKey = 'notificationDocumentsAddedBody'; + } elseif ($args['type'] == 'typist_END') { + $subjectKey = 'notificationEndOfWorkflowsSubject'; + $bodyKey = 'notificationEndOfWorkflowsBody'; + } elseif ($args['type'] == 'typist_INT') { + $subjectKey = 'notificationInterruptsSubject'; + $bodyKey = 'notificationInterruptsBody'; + } elseif ($args['type'] == 'typist_DEL') { + $subjectKey = 'notificationInterruptsSubject'; + $bodyKey = 'notificationDeletedUsersBody'; + } elseif ($args['type'] == 'typist_REF') { + $subjectKey = 'notificationRefusedWorkflowsSubject'; + $bodyKey = 'notificationDeletedUsersBody'; + } else { + $subjectKey = ''; + $bodyKey = ''; + } + $recipients = array_unique(array_column($notificationStack, 'recipient_id')); foreach ($recipients as $recipientId) { @@ -113,13 +136,11 @@ function runNextUserNotification(array $args) return $notification['recipient_id'] == $recipientId; }); - $recipient = UserModel::getById(['id' => $recipientId]); - + $recipient = UserModel::getById(['id' => $recipientId, 'select' => ['email', 'preferences']]); $recipient['preferences'] = json_decode($recipient['preferences'], true); - $lang = LanguageController::get(['lang' => $recipient['preferences']['lang']]); - $emailBody = $lang['notificationDocumentsAddedBody']; + $emailBody = $lang[$bodyKey]; $rows = ''; foreach ($recipientNotifications as $recipientNotification) { @@ -138,17 +159,21 @@ function runNextUserNotification(array $args) 'data' => [ 'sender' => 'Notification', 'recipients' => [$recipient['email']], - 'subject' => $lang['notificationDocumentsAddedSubject'], + 'subject' => $lang[$subjectKey], 'body' => $emailBody . $lang['notificationFooter'], - 'isHtml' => true + 'isHtml' => true, + 'status' => 'EXPRESS' ] ]); } - return true; -} +// $stackIds = array_column($notificationStack, 'id'); +// NotificationsScheduleModel::delete([ +// 'where' => ['id in (?)'], +// 'data' => [$stackIds] +// ]); -function runTypistEnd(array $args) -{ return true; } + + diff --git a/lang/fr.json b/lang/fr.json index 01c345d4c52d82a4dbbb1b8ef4ccd4b32345488e..06fd5fa8e2367abcc1f09819d98f69540ca95399 100755 --- a/lang/fr.json +++ b/lang/fr.json @@ -581,6 +581,13 @@ "manage_notificationsAdmin": "Planifier les notifications", "frquencyMonthly": "Chaque mois de <small class=\"primary\">{{months}}</small> le(s) <small class=\"primary\">{{daysOfMonth}}</small>", "frequencyDaily": "Tous les {{0}}", - "frequencyHourly": "Toute les <small class=\"primary\">{{frequency}}</small> de <small class=\"primary\">{{startTime}}</small> à <small class=\"primary\">{{endTime}}</small>" + "frequencyHourly": "Toute les <small class=\"primary\">{{frequency}}</small> de <small class=\"primary\">{{startTime}}</small> à <small class=\"primary\">{{endTime}}</small>", + "notificationEndOfWorkflowsSubject": "[Maarch Parapheur] Circuits de visa terminés", + "notificationEndOfWorkflowsBody": "Bonjour,<br/><br/>Des circuits de visa que vous avez initié se sont terminés avec succès.<br/><br/>Cliquez sur les liens ci-dessous pour les consulter :<br/>", + "notificationInterruptsSubject": "[Maarch Parapheur] Circuits de visa interrompus", + "notificationInterruptsBody": "Bonjour,<br/><br/>Des circuits de visa que vous avez initié ont été interrompu.<br/><br/>Cliquez sur les liens ci-dessous pour les consulter :<br/>", + "notificationDeletedUsersBody": "Bonjour,<br/><br/>Des circuits de visa que vous avez initié ont été interrompu car un utilisateur a été supprimé.<br/><br/>Cliquez sur les lien ci-dessous pour les consulter :<br/>", + "notificationRefusedWorkflowsSubject": "[Maarch Parapheur] Circuits de visa refusés", + "notificationRefusedWorkflowsBody": "Bonjour,<br/><br/>Des circuits de visa que vous avez initié ont été interrompu car un utilisateur a fait une action de refus.<br/><br/>Cliquez sur les liens ci-dessous pour les consulter :<br/>" } }