events_email.class.php 11 KB

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