events_email.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class EventsMail
  5. * manages the e-mail sending action when a event requires it
  6. */
  7. class EventsMail
  8. {
  9. /**
  10. * Sends email according to an event
  11. *
  12. * @param string $event_name the name of the event that was triggered
  13. * @param array $event_data what to put in the mail
  14. *
  15. * Possible key :
  16. * - $event_data["about_user"] (= $user_id)
  17. * - $event_data["prior_lang"]
  18. *
  19. * Warning :
  20. * - $event_data["send_to"] MUST BE an array
  21. */
  22. public static function send_mail($event_name, $event_data)
  23. {
  24. /**
  25. * Global explanation :
  26. * 1. we get information about the user that fired the event (in $event_data["about_user"])
  27. * 2. we send mail to people that are in the $event_data["send_to"]
  28. * 2b. if a language was specified, we use that one to send the mail,
  29. * else we get the user's language, if there isn't any, we get the english one
  30. * 3. we do the same with the people associated to the event through the admin panel
  31. */
  32. global $event_config;
  33. // common variable for every mail sent
  34. $sender_name = api_get_person_name(
  35. api_get_setting('administratorName'),
  36. api_get_setting('administratorSurname'),
  37. null,
  38. PERSON_NAME_EMAIL_ADDRESS
  39. );
  40. $email_admin = api_get_setting('emailAdministrator');
  41. // basic keys
  42. $event_data["sitename"] = api_get_setting('siteName');
  43. $event_data["administrator_name"] = api_get_setting('administratorName');
  44. $event_data["administrator_surname"] = api_get_setting('administratorSurname');
  45. $event_data["administrator_phone"] = api_get_setting('administratorTelephone');
  46. $event_data["administrator_email"] = api_get_setting('emailAdministrator');
  47. $event_data["portal"] = api_get_path(WEB_PATH);
  48. // Fill the array's cells with info regarding the user that fired the event
  49. // (for the keys in the template)
  50. if (isset($event_data["about_user"])) {
  51. $about_user = api_get_user_info($event_data["about_user"]);
  52. $event_data["firstname"] = $about_user["firstname"];
  53. $event_data["lastname"] = $about_user["lastname"];
  54. $event_data["username"] = $about_user["username"];
  55. $event_data["usermail"] = $about_user["mail"];
  56. $event_data["language"] = $about_user["language"];
  57. $event_data["user_id"] = $about_user["user_id"];
  58. }
  59. // First, we send the mail to people we put in the $event_data["send_to"]
  60. if ($event_data["send_to"] != null) {
  61. // the users we precised need to receive the mail
  62. foreach ($event_data["send_to"] as $id) {
  63. // for every member put in the array
  64. // get user's info (to know where to send)
  65. $user_info = api_get_user_info($id);
  66. // get the language the email will be in
  67. if ($event_data["prior_lang"] != null) {
  68. // if $lang is not null, we use that lang
  69. $language = $event_data["prior_lang"];
  70. } else {
  71. // else we use the user's language
  72. $sql = 'SELECT language
  73. FROM '.Database::get_main_table(TABLE_MAIN_USER).' u
  74. WHERE u.user_id = "'.$id.'"
  75. ';
  76. $language = Database::store_result(
  77. Database::query($sql),
  78. 'ASSOC'
  79. );
  80. $language = $language[0]["language"];
  81. }
  82. // we get the message in the correct language (or in english if doesn't exist)
  83. $result = self::getMessage($event_name, $language);
  84. $message = "";
  85. $subject = "";
  86. self::getCorrectMessage($message, $subject, $language, $result);
  87. // replace the keycodes used in the message
  88. self::formatMessage($message, $subject, $event_config, $event_name, $event_data);
  89. // sending email
  90. $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  91. // checks if there's a file we need to join to the mail
  92. if (isset($values["certificate_pdf_file"])) {
  93. $message = str_replace("\n", "<br />", $message);
  94. api_mail_html(
  95. $recipient_name,
  96. $user_info["mail"],
  97. $subject,
  98. $message,
  99. $sender_name,
  100. $email_admin,
  101. null,
  102. array($values['certificate_pdf_file'])
  103. );
  104. } else {
  105. api_mail_html(
  106. $recipient_name,
  107. $user_info["mail"],
  108. $subject,
  109. $message,
  110. $sender_name,
  111. $email_admin
  112. );
  113. }
  114. // If the mail only need to be send once (we know that thanks to the events.conf), we log it in the table
  115. if ($event_config[$event_name]["sending_mail_once"]) {
  116. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_SENT).' (user_from, user_to, event_type_name)
  117. VALUES ('.$event_data["user_id"].', '.$id.' ,"'.Database::escape_string($event_name).'")
  118. ';
  119. Database::query($sql);
  120. }
  121. }
  122. }
  123. // Second, we send to people linked to the event
  124. // So, we get everyone
  125. $sql = 'SELECT u.user_id, u.language, u.email, u.firstname, u.lastname
  126. FROM ' . Database::get_main_table(TABLE_EVENT_TYPE_REL_USER).' ue
  127. INNER JOIN '.Database::get_main_table(TABLE_MAIN_USER).' u ON u.user_id = ue.user_id
  128. WHERE event_type_name = "' . $event_name.'"';
  129. $result = Database::store_result(Database::query($sql), 'ASSOC');
  130. // for each of the linked users
  131. foreach ($result as $key => $value) {
  132. // we get the language
  133. if ($event_data["prior_lang"] != null) {
  134. // if $lang is not null, we use that lang
  135. $language = $event_data["prior_lang"];
  136. } else {
  137. // else we get the user's lang
  138. $sql = 'SELECT language FROM '.Database::get_main_table(TABLE_MAIN_USER).'
  139. where user_id = '.$value["user_id"].' ';
  140. $result = Database::store_result(Database::query($sql), 'ASSOC');
  141. $language = $result[0]["language"];
  142. }
  143. // we get the message in the correct language (or in english if doesn't exist)
  144. $result = self::getMessage($event_name, $language);
  145. $message = '';
  146. $subject = '';
  147. self::getCorrectMessage($message, $subject, $language, $result);
  148. // replace the keycodes used in the message
  149. self::formatMessage($message, $subject, $event_config, $event_name, $event_data);
  150. // we send the mail
  151. $recipient_name = api_get_person_name($value['firstname'], $value['lastname']);
  152. api_mail_html(
  153. $recipient_name,
  154. $value["email"],
  155. $subject,
  156. $message,
  157. $sender_name,
  158. $email_admin
  159. );
  160. // If the mail only need to be send once (we know that thanks to the events.conf, we log it in the table
  161. if ($event_config[$event_name]["sending_mail_once"]) {
  162. $sql = 'INSERT INTO '.Database::get_main_table(TABLE_EVENT_SENT).'
  163. (user_from, user_to, event_type_name)
  164. VALUES ('.$event_data["user_id"].', '.$value["user_id"].' , "'.Database::escape_string($event_name).'");
  165. ';
  166. Database::query($sql);
  167. }
  168. }
  169. }
  170. /**
  171. * Checks if a message in a language exists, if the event is activated
  172. * and if "manage event" is checked in admin panel.
  173. * If yes to three, we can use this class, else we still use api_mail.
  174. *
  175. * @param string $event_name
  176. * @return boolean
  177. */
  178. public static function check_if_using_class($event_name)
  179. {
  180. if (api_get_setting('activate_email_template') === 'false') {
  181. return false;
  182. }
  183. $current_language = api_get_interface_language();
  184. $sql = 'SELECT COUNT(*) as total
  185. FROM ' . Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' em
  186. INNER JOIN ' . Database::get_main_table(TABLE_MAIN_LANGUAGE).' l
  187. ON em.language_id = l.id
  188. WHERE
  189. em.event_type_name = "' . $event_name.'" and
  190. l.dokeos_folder = "'.$current_language.'" and
  191. em.activated = 1';
  192. $exists = Database::store_result(Database::query($sql), 'ASSOC');
  193. if ($exists[0]["total"]) {
  194. return true;
  195. } else {
  196. return false;
  197. }
  198. }
  199. /**
  200. * Get the record containing the good message and subject
  201. *
  202. * @param string $event_name
  203. * @param string $language
  204. * @return array
  205. */
  206. private static function getMessage($event_name, $language)
  207. {
  208. $sql = 'SELECT message, subject, l.dokeos_folder
  209. FROM ' . Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE).' em
  210. INNER JOIN ' . Database::get_main_table(TABLE_MAIN_LANGUAGE).' l
  211. ON em.language_id = l.id
  212. WHERE
  213. em.event_type_name = "' . $event_name.'" AND
  214. (l.dokeos_folder = "' . $language.'" OR l.dokeos_folder = "english") AND
  215. em.message <> ""
  216. ';
  217. return Database::store_result(Database::query($sql), 'ASSOC');
  218. }
  219. /**
  220. * Get the correct message, meaning in the specified language or in english if previous one doesn't exist
  221. *
  222. * @param string $message
  223. * @param string $subject
  224. * @param string $language
  225. * @param array $result
  226. */
  227. private static function getCorrectMessage(&$message, &$subject, $language, $result)
  228. {
  229. foreach ($result as $msg) {
  230. if ($msg["dokeos_folder"] == $language) {
  231. $message = $msg["message"];
  232. $subject = $msg["subject"];
  233. break;
  234. } else {
  235. if ($msg["dokeos_folder"] == "english") {
  236. $message = $msg["message"];
  237. $subject = $msg["subject"];
  238. }
  239. }
  240. }
  241. }
  242. /**
  243. * Replaces the ((key)) by the good values
  244. *
  245. * @param string $message
  246. * @param string $subject
  247. * @param array $event_config
  248. * @param string $event_name
  249. */
  250. private static function formatMessage(&$message, &$subject, $event_config, $event_name, &$event_data)
  251. {
  252. foreach ($event_config[$event_name]["available_keyvars"] as $key => $word) {
  253. $message = str_replace('(('.$key.'))', $event_data[$word], $message);
  254. $subject = str_replace('(('.$key.'))', $event_data[$word], $subject);
  255. }
  256. }
  257. }