plateforme-tfjm2/server_files/services/mail.php

264 lines
11 KiB
PHP
Raw Normal View History

<?php
2019-09-08 10:45:48 +00:00
class Mailer
{
2019-09-08 10:45:48 +00:00
private static function sendMail($email, $subject, $content, $from = "contact")
{
global $MAIL_DOMAIN, $URL_BASE, $YEAR;
$content = preg_replace("#{URL_BASE}#", $URL_BASE, $content);
$content = preg_replace("#{YEAR}#", $YEAR, $content);
$headers = "From: \"Contact TFJM²\" <" . $from . "@" . $MAIL_DOMAIN . ">\r\n";
$headers .= "Reply-To: \"Contact TFJM²\" <contact@" . $MAIL_DOMAIN . ">\r\n";
2019-09-08 10:45:48 +00:00
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($email, $subject, $content, $headers);
}
2020-01-16 22:00:31 +00:00
private static function broadcastToTeam(Team $team, $subject, $content, $from = "contact")
{
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
foreach ($team->getEncadrants() as $participant_id) {
$participant = User::fromId($participant_id);
if ($participant == null)
continue;
$c = preg_replace("#{FIRST_NAME}#", $participant->getFirstName(), $content);
$c = preg_replace("#{SURNAME}#", $participant->getSurname(), $c);
self::sendMail($participant->getEmail(), $subject, $c, $from);
}
foreach ($team->getParticipants() as $participant_id) {
$participant = User::fromId($participant_id);
if ($participant == null)
continue;
$c = preg_replace("#{FIRST_NAME}#", $participant->getFirstName(), $content);
$c = preg_replace("#{SURNAME}#", $participant->getSurname(), $c);
self::sendMail($participant->getEmail(), $subject, $c, $from);
}
}
private static function broadcastToAdmins($subject, $content, $from = "contact")
{
/** @var User $admin */
foreach (User::getAdmins() as $admin) {
$c = preg_replace("#{FIRST_NAME}#", $admin->getFirstName(), $content);
$c = preg_replace("#{SURNAME}#", $admin->getSurname(), $c);
self::sendMail($admin->getEmail(), $subject, $c, $from);
}
}
private static function brodcastToOrgas(Tournament $tournament, $subject, $content, $from = "contact")
{
foreach ($tournament->getOrganizers() as $orga) {
if ($orga->getRole() == Role::ADMIN)
continue;
$c = preg_replace("#{FIRST_NAME}#", $orga->getFirstName(), $content);
$c = preg_replace("#{SURNAME}#", $orga->getSurname(), $c);
self::sendMail($orga->getEmail(), $subject, $c, $from);
}
self::broadcastToAdmins($subject, $content, $from);
}
2019-09-08 10:45:48 +00:00
private static function getTemplate($name)
{
global $LOCAL_PATH;
return file_get_contents("$LOCAL_PATH/server_files/services/mail_templates/$name.html");
}
public static function sendRegisterMail(NewUser $new_user)
{
global $YEAR;
$content = self::getTemplate("register");
$content = preg_replace("#{FIRST_NAME}#", $new_user->first_name, $content);
$content = preg_replace("#{SURNAME}#", $new_user->surname, $content);
$content = preg_replace("#{TOKEN}#", $new_user->confirm_email_token, $content);
self::sendMail($new_user->email, "Inscription au TFJM² $YEAR", $content);
}
public static function sendConfirmEmail(User $user)
{
global $YEAR;
$content = self::getTemplate("confirm_email");
2019-09-08 10:45:48 +00:00
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TOKEN}#", $user->getConfirmEmailToken(), $content);
self::sendMail($user->getEmail(), "Confirmation d'adresse e-mail TFJM² $YEAR", $content);
}
public static function sendChangeEmailAddressMail(User $user)
{
$content = self::getTemplate("change_email_address");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TOKEN}#", $user->getConfirmEmailToken(), $content);
self::sendMail($user->getEmail(), "Changement d'adresse e-mail TFJM²", $content);
}
public static function sendForgottenPasswordProcedureMail(User $user)
{
$content = self::getTemplate("forgotten_password");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TOKEN}#", $user->getForgottenPasswordToken(), $content);
self::sendMail($user->getEmail(), "Mot de passe oublié TFJM²", $content);
}
public static function sendChangePasswordMail(User $user)
{
$content = self::getTemplate("change_password");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
self::sendMail($user->getEmail(), "Mot de passe changé TFJM²", $content);
}
public static function sendAddTeamMail(User $user, Team $team, Tournament $tournament)
{
global $YEAR;
$content = self::getTemplate("add_team");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$content = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $content);
$content = preg_replace("#{ACCESS_CODE}#", $team->getAccessCode(), $content);
self::sendMail($user->getEmail(), "Ajout d'une équipe TFJM² $YEAR", $content);
}
public static function sendJoinTeamMail(User $user, Team $team, Tournament $tournament)
{
global $YEAR;
$content = self::getTemplate("join_team");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$content = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $content);
self::sendMail($user->getEmail(), "Équipe rejointe TFJM² $YEAR", $content);
}
public static function sendAddOrganizerMail(NewOrganizer $new_orga)
{
global $YEAR;
$content = self::getTemplate("add_organizer");
$content = preg_replace("#{FIRST_NAME}#", $new_orga->first_name, $content);
$content = preg_replace("#{SURNAME}#", $new_orga->surname, $content);
$content = preg_replace("#{PASSWORD}#", $new_orga->password, $content);
self::sendMail($new_orga->email, "Ajout d'un organisateur TFJM² $YEAR", $content);
}
public static function sendAddOrganizerForTournamentMail(User $organizer, Tournament $tournament)
{
global $YEAR;
$content = self::getTemplate("add_organizer_for_tournament");
$content = preg_replace("#{FIRST_NAME}#", $organizer->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $organizer->getSurname(), $content);
$content = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $content);
self::sendMail($organizer->getEmail(), "Ajout d'un organisateur pour le tournoi " . $tournament->getName() . " TFJM² $YEAR", $content);
}
2020-01-14 11:21:18 +00:00
public static function requestPaymentValidation(User $user, Team $team, Tournament $tournament, Payment $payment)
{
global $YEAR, $URL_BASE;
$content = self::getTemplate("request_payment_validation");
$content = preg_replace("#{USER_FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{USER_SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{USER_ID}#", $user->getId(), $content);
2020-01-14 11:21:18 +00:00
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$content = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $content);
$content = preg_replace("#{AMOUNT}#", $payment->getAmount(), $content);
$content = preg_replace("#{PAYMENT_METHOD}#", PaymentMethod::getTranslatedName($payment->getMethod()), $content);
if ($payment->getMethod() == PaymentMethod::SCHOLARSHIP)
$content = preg_replace("#{PAYMENT_INFOS}#", "<a href=\"$URL_BASE/file/" . $payment->getTransactionInfos() . "\">Voir la notification de bourse</a>", $content);
else
$content = preg_replace("#{PAYMENT_INFOS}#", $payment->getTransactionInfos(), $content);
2020-01-16 22:00:31 +00:00
self::broadcastToAdmins("Demande de validation de paiement pour le tournoi " . $tournament->getName() . " TFJM² $YEAR", $content);
}
public static function sendRequestValidationMail(Team $team, Tournament $tournament)
{
global $YEAR;
$content = self::getTemplate("request_validation");
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$content = preg_replace("#{TOURNAMENT}#", $tournament->getName(), $content);
$content = preg_replace("#{ACCESS_CODE}#", $team->getAccessCode(), $content);
self::brodcastToOrgas($tournament, "Demande de validation Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendValidateTeam($team, $message)
{
global $YEAR;
$content = self::getTemplate("validate_team");
if (strlen($message) > 0)
$message = " L'équipe d'organisation vous transmet le message suivant :\n\n" . $message;
$message = preg_replace("#\n#", "<br/>\n", $message);
$content = preg_replace("#{MESSAGE}#", $message, $content);
self::broadcastToTeam($team, "Équipe validée Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
}
public static function sendUnvalidateTeam($team, $message)
{
global $YEAR;
$content = self::getTemplate("unvalidate_team");
if (strlen($message) > 0)
$message = " L'équipe d'organisation vous transmet le message suivant :\n\n" . $message;
$message = preg_replace("#\n#", "<br/>\n", $message);
$content = preg_replace("#{MESSAGE}#", $message, $content);
self::broadcastToTeam($team, "Équipe non validée Correspondances des Jeunes Mathématicien·ne·s $YEAR - " . ($YEAR + 1), $content);
2020-01-14 11:21:18 +00:00
}
public static function sendValidatePayment(User $user, Team $team, Tournament $tournament, Payment $payment, $message)
{
global $YEAR, $URL_BASE;
$content = self::getTemplate($payment->getValidationStatus() == ValidationStatus::VALIDATED ? "validate_payment" : "unvalidate_payment");
$content = preg_replace("#{FIRST_NAME}#", $user->getFirstName(), $content);
$content = preg_replace("#{SURNAME}#", $user->getSurname(), $content);
$content = preg_replace("#{TEAM_NAME}#", $team->getName(), $content);
$content = preg_replace("#{TRIGRAM}#", $team->getTrigram(), $content);
$content = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $content);
$content = preg_replace("#{AMOUNT}#", $payment->getAmount(), $content);
$content = preg_replace("#{PAYMENT_METHOD}#", PaymentMethod::getTranslatedName($payment->getMethod()), $content);
if ($payment->getMethod() == PaymentMethod::SCHOLARSHIP)
$content = preg_replace("#{PAYMENT_INFOS}#", "<a href=\"$URL_BASE/file/" . $payment->getTransactionInfos() . "\">Voir la notification de bourse</a>", $content);
else
$content = preg_replace("#{PAYMENT_INFOS}#", $payment->getTransactionInfos(), $content);
if (isset($message) && strlen($message) > 0) {
$content = preg_replace("#{MESSAGE}#", "L'équipe d'organisation vous transmet les informations suivantes :<br /><br />" . $message . "<br />", $content);
}
self::sendMail($user->getEmail(), "Paiement pour le tournoi " . $tournament->getName() . " TFJM² $YEAR", $content);
}
2019-09-07 15:26:30 +00:00
}