NotificationService.php 4.63 KB
Newer Older
Antonin's avatar
Antonin committed
1
2
3
4
<?php

namespace Framadate\Services;

5
use \stdClass;
Antonin's avatar
Antonin committed
6
7
8
9
10
11
12
13
14
15
use Framadate\Services\MailService;
use Framadate\Utils;

class NotificationService {
    const UPDATE_VOTE = 1;
    const ADD_VOTE = 2;
    const ADD_COMMENT = 3;
    const UPDATE_POLL = 10;
    const DELETED_POLL = 11;

16
17
18
19
20
21
22
23
    const TEMPLATES_MAPPING = [
        self::UPDATE_VOTE => 'mail/updated_vote.html.tpl',
        self::ADD_VOTE => 'mail/added_vote.html.tpl',
        self::ADD_COMMENT => 'mail/added_comment.html.tpl',
        self::UPDATE_POLL => 'mail/updated_poll.html.tpl',
        self::DELETED_POLL => 'mail/deleted_poll.html.tpl'
    ];

Antonin's avatar
Antonin committed
24
    private $mailService;
25
    private $smarty;
Antonin's avatar
Antonin committed
26

27
    function __construct(MailService $mailService, \Smarty $smarty) {
Antonin's avatar
Antonin committed
28
        $this->mailService = $mailService;
29
        $this->smarty = $smarty;
Antonin's avatar
Antonin committed
30
31
32
33
34
35
36
37
    }

    /**
     * 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
38
     * @throws \SmartyException
Antonin's avatar
Antonin committed
39
40
41
42
43
44
     */
    function sendUpdateNotification(stdClass $poll, $type, $name='') {
        if (!isset($_SESSION['mail_sent'])) {
            $_SESSION['mail_sent'] = [];
        }

45
46
47
        $isVoteAndCanSendIt = ($type === self::UPDATE_VOTE || $type === self::ADD_VOTE) && $poll->receiveNewVotes;
        $isCommentAndCanSendIt = $type === self::ADD_COMMENT && $poll->receiveNewComments;
        $isOtherType = $type !== self::UPDATE_VOTE && $type !== self::ADD_VOTE && $type !== self::ADD_COMMENT;
48
49

        if ($isVoteAndCanSendIt || $isCommentAndCanSendIt || $isOtherType) {
Antonin's avatar
Antonin committed
50
            if (self::isParticipation($type)) {
Luc Didry's avatar
Luc Didry committed
51
                $translationString = 'Poll participation: %s';
Antonin's avatar
Antonin committed
52
            } else {
Antonin's avatar
Antonin committed
53
                $translationString = 'Notification of poll: %s';
Antonin's avatar
Antonin committed
54
            }
Antonin's avatar
Antonin committed
55

56
            $subject = '[' . NOMAPPLICATION . '] ' . n('Mail', $translationString, $poll->title);
Antonin's avatar
Antonin committed
57

58
59
60
            $this->smarty->assign('username', $name);
            $this->smarty->assign('poll_title', $poll->title);
            $this->smarty->assign('poll_url', Utils::getUrlSondage($poll->admin_id, true));
Antonin's avatar
Antonin committed
61

62
            $template_name = self::TEMPLATES_MAPPING[$type];
Antonin's avatar
Antonin committed
63

64
65
66
67
            if (!is_null($template_name)) {
                $message = $this->smarty->fetch($template_name);
            } else {
                $message = '';
Antonin's avatar
Antonin committed
68
69
70
71
72
73
74
75
76
77
            }
            $messageTypeKey = $type . '-' . $poll->id;
            $this->mailService->send($poll->admin_mail, $subject, $message, $messageTypeKey);
        }
    }

    function isParticipation($type)
    {
       return $type >= self::UPDATE_POLL;
    }
78
79

    function sendPollCreationMails($creator_mail, $creator_name, $poll_name, $poll_id, $admin_poll_id) {
80
81
82
83
        if (!$this->mailService->isEnabled() || !$this->mailService->isValidEmail($creator_mail)) {
            return null;
        }

84
85
86
87
        $this->smarty->assign('poll_creator_name', Utils::htmlMailEscape($creator_name));
        $this->smarty->assign('poll_name', Utils::htmlMailEscape($poll_name));
        $this->smarty->assign('poll_url', Utils::getUrlSondage($poll_id));
        $message_participants = $this->smarty->fetch('mail/participants_forward_email.html.tpl');
88
        $this->mailService->send($creator_mail, '[' . NOMAPPLICATION . '][' . t('Mail', 'Participant link') . '] ' . t('Generic', 'Poll') . ': ' . $poll_name, $message_participants);
89
90
91

        $this->smarty->assign('poll_admin_url', Utils::getUrlSondage($admin_poll_id, true));
        $message_admin = $this->smarty->fetch('mail/creation_notification_email.html.tpl');
92
        $this->mailService->send($creator_mail, '[' . NOMAPPLICATION . '][' . t('Mail', 'Message for the author') . '] ' . t('Generic', 'Poll') . ': ' . $poll_name, $message_admin);
93
    }
94
95
96
97
98
99
100
101
102

    function sendEditedVoteNotification($email, &$poll, $poll_id, $edited_vote_id) {
        $url = Utils::getUrlSondage($poll_id, false, $edited_vote_id);

        $this->smarty->assign('poll', $poll);
        $this->smarty->assign('poll_id', $poll_id);
        $this->smarty->assign('editedVoteUniqueId', $edited_vote_id);
        $body = $this->smarty->fetch('mail/remember_edit_link.tpl');

103
        $subject = '[' . NOMAPPLICATION . '][' . t('EditLink', 'REMINDER') . '] ' . n('EditLink', 'Edit link for poll "%s"', $poll->title);
104
105
106

        $this->mailService->send($email, $subject, $body);
    }
107
108
109
110
111

    function sendFindPollsByMailNotification($mail, &$polls) {
        $this->smarty->assign('polls', $polls);
        $body = $this->smarty->fetch('mail/find_polls.tpl');

112
        $this->mailService->send($mail, t('FindPolls', 'List of your polls') . ' - ' . NOMAPPLICATION, $body, 'SEND_POLLS');
113
    }
Thomas Citharel's avatar
Thomas Citharel committed
114
}