events_email.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. {
  52. $about_user = api_get_user_info($event_data["about_user"]);
  53. $event_data["firstname"] = $about_user["firstname"];
  54. $event_data["lastname"] = $about_user["lastname"];
  55. $event_data["username"] = $about_user["username"];
  56. $event_data["usermail"] = $about_user["mail"];
  57. $event_data["language"] = $about_user["language"];
  58. $event_data["user_id"] = $about_user["user_id"];
  59. }
  60. // First, we send the mail to people we put in the $event_data["send_to"]
  61. if ($event_data["send_to"] != null) // the users we precised need to receive the mail
  62. {
  63. foreach ($event_data["send_to"] as $id) // for every member put in the array
  64. {
  65. // get user's info (to know where to send)
  66. $user_info = api_get_user_info($id);
  67. // get the language the email will be in
  68. if ($event_data["prior_lang"] != null) // if $lang is not null, we use that lang
  69. {
  70. $language = $event_data["prior_lang"];
  71. }
  72. else // else we use the user's language
  73. {
  74. $sql = 'SELECT language FROM ' . Database::get_main_table(TABLE_MAIN_USER) . ' u
  75. WHERE u.user_id = "' . $id . '"
  76. ';
  77. $language = Database::store_result(Database::query($sql), 'ASSOC');
  78. $language = $language[0]["language"];
  79. }
  80. // we get the message in the correct language (or in english if doesn't exist)
  81. $result = self::getMessage($event_name, $language);
  82. $message = "";
  83. $subject = "";
  84. self::getCorrectMessage($message, $subject, $language, $result);
  85. // replace the keycodes used in the message
  86. self::formatMessage($message, $subject, $event_config, $event_name, $event_data);
  87. // sending email
  88. $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  89. // checks if there's a file we need to join to the mail
  90. if (isset($values["certificate_pdf_file"])) {
  91. $message = str_replace("\n", "<br />", $message);
  92. api_mail_html($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin, null, array($values['certificate_pdf_file']));
  93. } else {
  94. api_mail_html($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin);
  95. }
  96. // If the mail only need to be send once (we know that thanks to the events.conf), we log it in the table
  97. if ($event_config[$event_name]["sending_mail_once"])
  98. {
  99. $sql = 'INSERT INTO ' . Database::get_main_table(TABLE_EVENT_SENT) . '
  100. (user_from, user_to, event_type_name)
  101. VALUES ('.$event_data["user_id"].', '.$id.' ,"'.Database::escape_string($event_name).'");
  102. ';
  103. Database::query($sql);
  104. }
  105. }
  106. }
  107. // Second, we send to people linked to the event
  108. // So, we get everyone
  109. $sql = 'SELECT u.user_id, u.language, u.email, u.firstname, u.lastname FROM ' . Database::get_main_table(TABLE_EVENT_TYPE_REL_USER) . ' ue
  110. INNER JOIN '.Database::get_main_table(TABLE_MAIN_USER).' u ON u.user_id = ue.user_id
  111. WHERE event_type_name = "' . $event_name . '"';
  112. $result = Database::store_result(Database::query($sql), 'ASSOC');
  113. foreach ($result as $key => $value) // for each of the linked users
  114. {
  115. // we get the language
  116. if ($event_data["prior_lang"] != null) // if $lang is not null, we use that lang
  117. {
  118. $language = $event_data["prior_lang"];
  119. }
  120. else // else we get the user's lang
  121. {
  122. $sql = 'SELECT language FROM '.Database::get_main_table(TABLE_MAIN_USER).'
  123. where user_id = '.$value["user_id"].' ';
  124. $result = Database::store_result(Database::query($sql), 'ASSOC');
  125. $language = $result[0]["language"];
  126. }
  127. // we get the message in the correct language (or in english if doesn't exist)
  128. $result = self::getMessage($event_name, $language);
  129. $message = "";
  130. $subject = "";
  131. self::getCorrectMessage($message, $subject, $language, $result);
  132. // replace the keycodes used in the message
  133. self::formatMessage($message, $subject, $event_config, $event_name, $event_data);
  134. // we send the mail
  135. $recipient_name = api_get_person_name($value['firstname'], $value['lastname']);
  136. api_mail_html($recipient_name, $value["email"], $subject, $message, $sender_name, $email_admin);
  137. // If the mail only need to be send once (we know that thanks to the events.conf, we log it in the table
  138. if ($event_config[$event_name]["sending_mail_once"])
  139. {
  140. $sql = 'INSERT INTO ' . Database::get_main_table(TABLE_EVENT_SENT) . '
  141. (user_from, user_to, event_type_name)
  142. VALUES ('.$event_data["user_id"].', '.$value["user_id"].' , "'.Database::escape_string($event_name).'");
  143. ';
  144. Database::query($sql);
  145. }
  146. }
  147. }
  148. /**
  149. * Checks if a message in a language exists, if the event is activated
  150. * and if "manage event" is checked in admin panel.
  151. * If yes to three, we can use this class, else we still use api_mail.
  152. *
  153. * @param string $event_name
  154. * @return boolean
  155. */
  156. public static function check_if_using_class($event_name)
  157. {
  158. if (api_get_setting('activate_email_template') === 'false') {
  159. return false;
  160. }
  161. $current_language = api_get_interface_language();
  162. $sql = 'SELECT COUNT(*) as total
  163. FROM ' . Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE) . ' em
  164. INNER JOIN ' . Database::get_main_table(TABLE_MAIN_LANGUAGE) . ' l
  165. ON em.language_id = l.id
  166. WHERE
  167. em.event_type_name = "' . $event_name . '" and
  168. l.dokeos_folder = "'.$current_language.'" and
  169. em.activated = 1';
  170. $exists = Database::store_result(Database::query($sql), 'ASSOC');
  171. if ($exists[0]["total"]) {
  172. return true;
  173. } else {
  174. return false;
  175. }
  176. }
  177. /**
  178. * Get the record containing the good message and subject
  179. *
  180. * @param string $event_name
  181. * @param string $language
  182. * @return array
  183. */
  184. private static function getMessage($event_name, $language)
  185. {
  186. $sql = 'SELECT message, subject, l.dokeos_folder FROM ' . Database::get_main_table(TABLE_EVENT_EMAIL_TEMPLATE) . ' em
  187. INNER JOIN ' . Database::get_main_table(TABLE_MAIN_LANGUAGE) . ' l
  188. ON em.language_id = l.id
  189. WHERE em.event_type_name = "' . $event_name . '" and (l.dokeos_folder = "' . $language . '" OR l.dokeos_folder = "english") and em.message <> ""
  190. ';
  191. return Database::store_result(Database::query($sql), 'ASSOC');
  192. }
  193. /**
  194. * Get the correct message, meaning in the specified language or in english if previous one doesn't exist
  195. *
  196. * @param string $message
  197. * @param string $subject
  198. * @param string $language
  199. * @param array $result
  200. */
  201. private static function getCorrectMessage(&$message, &$subject, $language, $result)
  202. {
  203. foreach ($result as $msg)
  204. {
  205. if ($msg["dokeos_folder"] == $language)
  206. {
  207. $message = $msg["message"];
  208. $subject = $msg["subject"];
  209. break;
  210. }
  211. else if ($msg["dokeos_folder"] == "english")
  212. {
  213. $message = $msg["message"];
  214. $subject = $msg["subject"];
  215. }
  216. }
  217. }
  218. /**
  219. * Replaces the ((key)) by the good values
  220. *
  221. * @param string $message
  222. * @param string $subject
  223. * @param array $event_config
  224. * @param string $event_name
  225. */
  226. private static function formatMessage(&$message, &$subject, $event_config, $event_name, &$event_data)
  227. {
  228. foreach ($event_config[$event_name]["available_keyvars"] as $key => $word)
  229. {
  230. $message = str_replace('((' . $key . '))', $event_data[$word], $message);
  231. $subject = str_replace('((' . $key . '))', $event_data[$word], $subject);
  232. }
  233. }
  234. }