MailTemplateManager.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Symfony\Component\Finder\Finder;
  4. /**
  5. * Class MailTemplateManager.
  6. */
  7. class MailTemplateManager extends Model
  8. {
  9. public $columns = [
  10. 'id',
  11. 'name',
  12. 'template',
  13. 'type',
  14. 'system',
  15. 'url_id',
  16. 'default_template',
  17. 'created_at',
  18. 'updated_at',
  19. 'author_id',
  20. ];
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->table = 'mail_template';
  25. }
  26. /**
  27. * @return int
  28. */
  29. public function get_count()
  30. {
  31. $row = Database::select(
  32. 'count(*) as count',
  33. $this->table,
  34. ['where' => ['url_id = ? ' => api_get_current_access_url_id()]],
  35. 'first'
  36. );
  37. return $row['count'];
  38. }
  39. /**
  40. * Displays the title + grid.
  41. *
  42. * @return string html code
  43. */
  44. public function display()
  45. {
  46. // Action links
  47. $html = '<div class="actions" style="margin-bottom:20px">';
  48. $html .= '<a href="'.api_get_path(WEB_CODE_PATH).'admin">'.
  49. Display::return_icon(
  50. 'back.png',
  51. get_lang('Back'),
  52. '',
  53. '32'
  54. )
  55. .'</a>';
  56. $html .= '<a href="'.api_get_self().'?action=add">'.
  57. Display::return_icon(
  58. 'add.png',
  59. get_lang('Add'),
  60. '',
  61. '32'
  62. ).'</a>';
  63. $html .= '</div>';
  64. $html .= Display::grid_html('mail_template');
  65. return $html;
  66. }
  67. /**
  68. * Returns a Form validator Obj.
  69. *
  70. * @param string $url
  71. * @param string $action
  72. *
  73. * @return FormValidator
  74. */
  75. public function returnForm($url, $action = 'add')
  76. {
  77. $form = new FormValidator('template', 'post', $url);
  78. // Setting the form elements
  79. $header = get_lang('Add');
  80. if ($action === 'edit') {
  81. $header = get_lang('Edit');
  82. }
  83. $id = isset($_GET['id']) ? (int) $_GET['id'] : '';
  84. $form->addElement('header', '', $header);
  85. $form->addElement('hidden', 'id', $id);
  86. $form->addElement(
  87. 'text',
  88. 'name',
  89. get_lang('Name'),
  90. ['size' => '70', 'id' => 'name']
  91. );
  92. /*$form->addHtmlEditor(
  93. 'email_template',
  94. get_lang('Template'),
  95. false,
  96. false,
  97. [
  98. 'ToolbarSet' => 'Careers',
  99. 'Width' => '100%',
  100. 'Height' => '250',
  101. ]
  102. );*/
  103. $form->addTextarea(
  104. 'email_template',
  105. get_lang('Template')
  106. );
  107. $finder = new Finder();
  108. $files = $finder
  109. ->files()
  110. ->in(api_get_path(SYS_CODE_PATH).'template/default/mail')
  111. ->sort(
  112. function ($a, $b) {
  113. return strcmp($a->getRealpath(), $b->getRealpath());
  114. }
  115. );
  116. $options = [];
  117. /** @var SplFileInfo $file */
  118. foreach ($files as $file) {
  119. $options[$file->getFilename()] = $file->getFilename();
  120. }
  121. $form->addSelect(
  122. 'type',
  123. get_lang('Type'),
  124. $options
  125. );
  126. $defaults = $this->get($id);
  127. if ($action === 'edit') {
  128. $form->addLabel(get_lang('Created at'), Display::dateToStringAgoAndLongDate($defaults['created_at']));
  129. $form->addLabel(get_lang('Updated at'), Display::dateToStringAgoAndLongDate($defaults['updated_at']));
  130. $form->addButtonSave(get_lang('Edit'), 'submit');
  131. } else {
  132. $form->addButtonCreate(get_lang('Add'), 'submit');
  133. }
  134. // Setting the defaults
  135. if (!empty($defaults)) {
  136. $defaults['email_template'] = $defaults['template'];
  137. }
  138. $form->setDefaults($defaults);
  139. // Setting the rules
  140. $form->addRule('name', get_lang('Required field'), 'required');
  141. return $form;
  142. }
  143. /**
  144. * @param int $id
  145. *
  146. * @return bool
  147. */
  148. public function setDefault($id)
  149. {
  150. $template = $this->get($id);
  151. if (empty($template)) {
  152. return false;
  153. }
  154. $type = $template['type'];
  155. $urlId = api_get_current_access_url_id();
  156. $sql = "UPDATE {$this->table} SET default_template = 0
  157. WHERE type = '$type' AND url_id = $urlId";
  158. Database::query($sql);
  159. $sql = "UPDATE {$this->table} SET default_template = 1
  160. WHERE id = $id";
  161. Database::query($sql);
  162. return true;
  163. }
  164. /**
  165. * @param int $templateId
  166. * @param array $userInfo
  167. *
  168. * @return string|false
  169. */
  170. public function parseTemplate($templateId, $userInfo)
  171. {
  172. $templateInfo = $this->get($templateId);
  173. if (!empty($templateInfo)) {
  174. $emailTemplate = $templateInfo['template'];
  175. $keys = array_keys($userInfo);
  176. foreach ($keys as $key) {
  177. $emailTemplate = str_replace("{{user.$key}}", $userInfo[$key], $emailTemplate);
  178. }
  179. $template = new Template();
  180. $template->twig->setLoader(new \Twig_Loader_String());
  181. $emailBody = $template->twig->render($emailTemplate);
  182. return $emailBody;
  183. }
  184. return false;
  185. }
  186. }