", $message); api_mail_html( $recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin, null, [$values['certificate_pdf_file']] ); } else { api_mail_html( $recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin ); } // If the mail only need to be send once (we know that thanks to the events.conf), we log it in the table if ($event_config[$event_name]["sending_mail_once"]) { $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_SENT).' (user_from, user_to, event_type_name) VALUES ('.$event_data["user_id"].', '.$id.' ,"'.Database::escape_string($event_name).'") '; Database::query($sql); } } } // Second, we send to people linked to the event // So, we get everyone $sql = 'SELECT u.user_id, u.language, u.email, u.firstname, u.lastname FROM '.Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' ue INNER JOIN '.Database::get_main_table(TABLE_MAIN_USER).' u ON u.user_id = ue.user_id WHERE event_type_name = "'.$event_name.'"'; $result = Database::store_result(Database::query($sql), 'ASSOC'); // for each of the linked users foreach ($result as $key => $value) { // we get the language if ($event_data["prior_lang"] != null) { // if $lang is not null, we use that lang $language = $event_data["prior_lang"]; } else { // else we get the user's lang $sql = 'SELECT language FROM '.Database::get_main_table(TABLE_MAIN_USER).' where user_id = '.$value["user_id"].' '; $result = Database::store_result(Database::query($sql), 'ASSOC'); $language = $result[0]["language"]; } // we get the message in the correct language (or in english if doesn't exist) $result = self::getMessage($event_name, $language); $message = ''; $subject = ''; self::getCorrectMessage($message, $subject, $language, $result); // replace the keycodes used in the message self::formatMessage($message, $subject, $event_config, $event_name, $event_data); // we send the mail $recipient_name = api_get_person_name($value['firstname'], $value['lastname']); api_mail_html( $recipient_name, $value["email"], $subject, $message, $sender_name, $email_admin ); // If the mail only need to be send once (we know that thanks to the events.conf, we log it in the table if ($event_config[$event_name]["sending_mail_once"]) { $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_SENT).' (user_from, user_to, event_type_name) VALUES ('.$event_data["user_id"].', '.$value["user_id"].' , "'.Database::escape_string($event_name).'"); '; Database::query($sql); } } } /** * Checks if a message in a language exists, if the event is activated * and if "manage event" is checked in admin panel. * If yes to three, we can use this class, else we still use api_mail. * * @param string $event_name * * @return bool */ public static function check_if_using_class($event_name) { if (api_get_setting('activate_email_template') === 'false') { return false; } $current_language = api_get_interface_language(); $sql = 'SELECT COUNT(*) as total FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' em INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l ON em.language_id = l.id WHERE em.event_type_name = "'.$event_name.'" and l.dokeos_folder = "'.$current_language.'" and em.activated = 1'; $exists = Database::store_result(Database::query($sql), 'ASSOC'); if ($exists[0]["total"]) { return true; } else { return false; } } /** * Get the record containing the good message and subject. * * @param string $event_name * @param string $language * * @return array */ private static function getMessage($event_name, $language) { $sql = 'SELECT message, subject, l.dokeos_folder FROM '.Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' em INNER JOIN '.Database::get_main_table(TABLE_MAIN_LANGUAGE).' l ON em.language_id = l.id WHERE em.event_type_name = "'.$event_name.'" AND (l.dokeos_folder = "'.$language.'" OR l.dokeos_folder = "english") AND em.message <> "" '; return Database::store_result(Database::query($sql), 'ASSOC'); } /** * Get the correct message, meaning in the specified language or in english if previous one doesn't exist. * * @param string $message * @param string $subject * @param string $language * @param array $result */ private static function getCorrectMessage(&$message, &$subject, $language, $result) { foreach ($result as $msg) { if ($msg["dokeos_folder"] == $language) { $message = $msg["message"]; $subject = $msg["subject"]; break; } else { if ($msg["dokeos_folder"] == "english") { $message = $msg["message"]; $subject = $msg["subject"]; } } } } /** * Replaces the ((key)) by the good values. * * @param string $message * @param string $subject * @param array $event_config * @param string $event_name */ private static function formatMessage(&$message, &$subject, $event_config, $event_name, &$event_data) { foreach ($event_config[$event_name]["available_keyvars"] as $key => $word) { $message = str_replace('(('.$key.'))', $event_data[$word], $message); $subject = str_replace('(('.$key.'))', $event_data[$word], $subject); } } }