2015-10-13 01:03:41 +02:00
< ? php
namespace Framadate\Services ;
2018-02-19 00:18:43 +01:00
use \stdClass ;
2015-10-13 01:03:41 +02:00
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 ;
private $mailService ;
2019-04-17 16:49:03 +02:00
private $smarty ;
2015-10-13 01:03:41 +02:00
2019-04-17 16:49:03 +02:00
function __construct ( MailService $mailService , \Smarty $smarty ) {
2015-10-13 01:03:41 +02:00
$this -> mailService = $mailService ;
2019-04-17 16:49:03 +02:00
$this -> smarty = $smarty ;
2015-10-13 01:03:41 +02:00
}
/**
* 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' ] = [];
}
2018-02-19 00:18:43 +01:00
$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 ;
2016-11-14 23:35:04 +01:00
if ( $isVoteAndCanSendIt || $isCommentAndCanSendIt || $isOtherType ) {
2015-10-26 16:28:59 +01:00
if ( self :: isParticipation ( $type )) {
2018-07-06 18:06:35 +02:00
$translationString = 'Poll participation: %s' ;
2015-10-26 16:28:59 +01:00
} else {
2015-10-13 01:03:41 +02:00
$translationString = 'Notification of poll: %s' ;
2015-10-26 16:28:59 +01:00
}
2015-10-13 01:03:41 +02:00
$subject = '[' . NOMAPPLICATION . '] ' . __f ( 'Mail' , $translationString , $poll -> title );
$message = '' ;
$urlSondage = Utils :: getUrlSondage ( $poll -> admin_id , true );
$link = '<a href="' . $urlSondage . '">' . $urlSondage . '</a>' . " \n \n " ;
switch ( $type ) {
case self :: UPDATE_VOTE :
2015-10-26 16:28:59 +01:00
$message .= $name . ' ' ;
2018-07-06 18:06:35 +02:00
$message .= __ ( 'Mail' , " updated a vote.<br/>You can visit your poll at the link " ) . " : \n \n " ;
2015-10-13 01:03:41 +02:00
$message .= $link ;
break ;
case self :: ADD_VOTE :
2015-10-26 16:28:59 +01:00
$message .= $name . ' ' ;
2018-07-06 18:06:35 +02:00
$message .= __ ( 'Mail' , " added a vote.<br/>You can visit your poll at the link " ) . " : \n \n " ;
2015-10-13 01:03:41 +02:00
$message .= $link ;
break ;
case self :: ADD_COMMENT :
2015-10-26 16:28:59 +01:00
$message .= $name . ' ' ;
2018-07-06 18:06:35 +02:00
$message .= __ ( 'Mail' , " wrote a comment.<br/>You can visit your poll at the link " ) . " : \n \n " ;
2015-10-13 01:03:41 +02:00
$message .= $link ;
break ;
case self :: UPDATE_POLL :
2018-07-06 18:06:35 +02:00
$message = __f ( 'Mail' , 'Someone just changed your poll at the following link <a href=\"%1$s\">%1$s</a>.' , Utils :: getUrlSondage ( $poll -> admin_id , true )) . " \n \n " ;
2015-10-13 01:03:41 +02:00
break ;
case self :: DELETED_POLL :
2018-07-06 18:06:35 +02:00
$message = __f ( 'Mail' , 'Someone just deleted your poll "%s".' , Utils :: htmlEscape ( $poll -> title )) . " \n \n " ;
2015-10-13 01:03:41 +02:00
break ;
}
$messageTypeKey = $type . '-' . $poll -> id ;
$this -> mailService -> send ( $poll -> admin_mail , $subject , $message , $messageTypeKey );
}
}
function isParticipation ( $type )
{
return $type >= self :: UPDATE_POLL ;
}
2019-04-17 16:50:00 +02:00
function sendPollCreationMails ( $creator_mail , $creator_name , $poll_name , $poll_id , $admin_poll_id ) {
$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' );
$this -> mailService -> send ( $creator_mail , '[' . NOMAPPLICATION . '][' . __ ( 'Mail' , 'Participant link' ) . '] ' . __ ( 'Generic' , 'Poll' ) . ': ' . $poll_name , $message_participants );
$this -> smarty -> assign ( 'poll_admin_url' , Utils :: getUrlSondage ( $admin_poll_id , true ));
$message_admin = $this -> smarty -> fetch ( 'mail/creation_notification_email.html.tpl' );
$this -> mailService -> send ( $creator_mail , '[' . NOMAPPLICATION . '][' . __ ( 'Mail' , 'Message for the author' ) . '] ' . __ ( 'Generic' , 'Poll' ) . ': ' . $poll_name , $message_admin );
}
2019-04-17 17:31:06 +02:00
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' );
$subject = '[' . NOMAPPLICATION . '][' . __ ( 'EditLink' , 'REMINDER' ) . '] ' . __f ( 'EditLink' , 'Edit link for poll "%s"' , $poll -> title );
$this -> mailService -> send ( $email , $subject , $body );
}
2018-02-20 19:05:24 +01:00
}