diff --git a/adminstuds.php b/adminstuds.php index 3f30a41..5315422 100644 --- a/adminstuds.php +++ b/adminstuds.php @@ -23,15 +23,11 @@ use Framadate\Services\InputService; use Framadate\Services\LogService; use Framadate\Services\MailService; use Framadate\Services\PollService; +use Framadate\Services\NotificationService; use Framadate\Utils; include_once __DIR__ . '/app/inc/init.php'; -/* Constants */ -/* --------- */ -const UPDATE_POLL = 1; -const DELETED_POLL = 2; - /* Variables */ /* --------- */ @@ -49,40 +45,7 @@ $pollService = new PollService($connect, $logService); $adminPollService = new AdminPollService($connect, $pollService, $logService); $inputService = new InputService(); $mailService = new MailService($config['use_smtp']); - -/* Functions */ -/*-----------*/ - -/** - * Send a notification to the poll admin to notify him about an update. - * - * @param stdClass $poll The poll - * @param MailService $mailService The mail service - * @param int $type cf: Constants on the top of this page - */ -function sendUpdateNotification($poll, $mailService, $type) { - if (!isset($_SESSION['mail_sent'])) { - $_SESSION['mail_sent'] = []; - } - - if ($poll->receiveNewVotes) { - - $subject = '[' . NOMAPPLICATION . '] ' . __f('Mail', 'Notification of poll: %s', $poll->title); - - $message = ''; - switch ($type) { - case UPDATE_POLL: - $message = __f('Mail', 'Someone just change your poll available at the following link %s.', Utils::getUrlSondage($poll->admin_id, true)) . "\n\n"; - break; - case DELETED_POLL: - $message = __f('Mail', 'Someone just delete your poll %s.', Utils::htmlEscape($poll->title)) . "\n\n"; - break; - } - - $messageTypeKey = $type . '-' . $poll->id; - $mailService->send($poll->admin_mail, $subject, $message, $messageTypeKey); - } -} +$notificationService = new NotificationService($mailService); /* PAGE */ /* ---- */ @@ -176,7 +139,7 @@ if (isset($_POST['update_poll_info'])) { // Update poll in database if ($updated && $adminPollService->updatePoll($poll)) { $message = new Message('success', __('adminstuds', 'Poll saved')); - sendUpdateNotification($poll, $mailService, UPDATE_POLL); + $notificationService->sendUpdateNotification($poll, $notificationService::UPDATE_POLL); } else { $message = new Message('danger', __('Error', 'Failed to save poll')); $poll = $pollService->findById($poll_id); @@ -341,7 +304,7 @@ if (isset($_POST['delete_poll'])) { if (isset($_POST['confirm_delete_poll'])) { if ($adminPollService->deleteEntirePoll($poll_id)) { $message = new Message('success', __('adminstuds', 'Poll fully deleted')); - sendUpdateNotification($poll, $mailService, DELETED_POLL); + $notificationService->sendUpdateNotification($poll, $notificationService::DELETED_POLL); } else { $message = new Message('danger', __('Error', 'Failed to delete the poll')); } diff --git a/app/classes/Framadate/Services/NotificationService.php b/app/classes/Framadate/Services/NotificationService.php new file mode 100644 index 0000000..7944c7f --- /dev/null +++ b/app/classes/Framadate/Services/NotificationService.php @@ -0,0 +1,86 @@ +mailService = $mailService; + } + + /** + * Send a notification to the poll admin to notify him about an update. + * + * @param $poll stdClass The poll + * @param $name string The name user who triggered the notification + * @param $type int cf: Constants on the top of this page + */ + function sendUpdateNotification(stdClass $poll, $type, $name='') { + if (!isset($_SESSION['mail_sent'])) { + $_SESSION['mail_sent'] = []; + } + + if ($poll->receiveNewVotes) { + + if (self::isParticipation($type)) + $translationString = 'Poll\'s participation: %s'; + else + $translationString = 'Notification of poll: %s'; + + $subject = '[' . NOMAPPLICATION . '] ' . __f('Mail', $translationString, $poll->title); + + + $message = ''; + if (self::isParticipation($type)) + $message .= $name . ' '; + + $urlSondage = Utils::getUrlSondage($poll->admin_id, true); + $link = '' . $urlSondage . '' . "\n\n"; + + switch ($type) { + case self::UPDATE_VOTE: + $message .= __('Mail', "updated a vote.\nYou can find your poll at the link") . " :\n\n"; + $message .= $link; + break; + case self::ADD_VOTE: + $message .= __('Mail', "filled a vote.\nYou can find your poll at the link") . " :\n\n"; + $message .= $link; + break; + case self::ADD_COMMENT: + $message .= __('Mail', "wrote a comment.\nYou can find your poll at the link") . " :\n\n"; + $message .= $link; + break; + case self::UPDATE_POLL: + $message = __f('Mail', 'Someone just change your poll available at the following link %s.', Utils::getUrlSondage($poll->admin_id, true)) . "\n\n"; + break; + case self::DELETED_POLL: + $message = __f('Mail', 'Someone just delete your poll %s.', Utils::htmlEscape($poll->title)) . "\n\n"; + break; + + } + + $messageTypeKey = $type . '-' . $poll->id; + $this->mailService->send($poll->admin_mail, $subject, $message, $messageTypeKey); + } + } + + function isParticipation($type) + { + return $type >= self::UPDATE_POLL; + } + +} \ No newline at end of file diff --git a/studs.php b/studs.php index 52b9096..661b1e0 100644 --- a/studs.php +++ b/studs.php @@ -20,18 +20,13 @@ use Framadate\Services\LogService; use Framadate\Services\PollService; use Framadate\Services\InputService; use Framadate\Services\MailService; +use Framadate\Services\NotificationService; use Framadate\Message; use Framadate\Utils; use Framadate\Editable; include_once __DIR__ . '/app/inc/init.php'; -/* Constants */ -/* --------- */ -const UPDATE_VOTE = 1; -const ADD_VOTE = 2; -const ADD_COMMENT = 3; - /* Variables */ /* --------- */ @@ -47,46 +42,8 @@ $logService = new LogService(); $pollService = new PollService($connect, $logService); $inputService = new InputService(); $mailService = new MailService($config['use_smtp']); +$notificationService = new NotificationService($mailService); -/* Functions */ -/*-----------*/ - -/** - * Send a notification to the poll admin to notify him about an update. - * - * @param $poll stdClass The poll - * @param $mailService MailService The mail service - * @param $name string The name user who triggered the notification - * @param $type int cf: Constants on the top of this page - */ -function sendUpdateNotification($poll, $mailService, $name, $type) { - if (!isset($_SESSION['mail_sent'])) { - $_SESSION['mail_sent'] = []; - } - - if ($poll->receiveNewVotes) { - - $subject = '[' . NOMAPPLICATION . '] ' . __f('Mail', 'Poll\'s participation: %s', $poll->title); - - $message = $name . ' '; - switch ($type) { - case UPDATE_VOTE: - $message .= __('Mail', "updated a vote.\nYou can find your poll at the link") . " :\n\n"; - break; - case ADD_VOTE: - $message .= __('Mail', "filled a vote.\nYou can find your poll at the link") . " :\n\n"; - break; - case ADD_COMMENT: - $message .= __('Mail', "wrote a comment.\nYou can find your poll at the link") . " :\n\n"; - break; - } - $urlSondage = Utils::getUrlSondage($poll->admin_id, true); - $message .= '' . $urlSondage . '' . "\n\n"; - - $messageTypeKey = $type . '-' . $poll->id; - $mailService->send($poll->admin_mail, $subject, $message, $messageTypeKey); - } -} /* PAGE */ /* ---- */ @@ -139,7 +96,7 @@ if (!empty($_POST['save'])) { // Save edition of an old vote } else { $message = new Message('success', __('studs', 'Update vote succeeded')); } - sendUpdateNotification($poll, $mailService, $name, UPDATE_VOTE); + $notificationService->sendUpdateNotification($poll, $notificationService::UPDATE_VOTE, $name); } else { $message = new Message('danger', __('Error', 'Update vote failed')); } @@ -165,7 +122,7 @@ if (!empty($_POST['save'])) { // Save edition of an old vote } else { $message = new Message('success', __('studs', 'Adding the vote succeeded')); } - sendUpdateNotification($poll, $mailService, $name, ADD_VOTE); + $notificationService->sendUpdateNotification($poll, $notificationService::ADD_VOTE, $name); } else { $message = new Message('danger', __('Error', 'Adding vote failed')); } @@ -189,12 +146,11 @@ if (isset($_POST['add_comment'])) { $result = $pollService->addComment($poll_id, $name, $comment); if ($result) { $message = new Message('success', __('Comments', 'Comment added')); - sendUpdateNotification($poll, $mailService, $name, ADD_COMMENT); + $notificationService->sendUpdateNotification($poll, $notificationService::ADD_COMMENT, $name); } else { $message = new Message('danger', __('Error', 'Comment failed')); } } - } // Retrieve data