MailTemplateManager.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /**
  22. */
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. $this->table = 'mail_template';
  27. }
  28. /**
  29. * @return int
  30. */
  31. public function get_count()
  32. {
  33. $row = Database::select(
  34. 'count(*) as count',
  35. $this->table,
  36. ['where' => ['url_id = ? ' => api_get_current_access_url_id()]],
  37. 'first'
  38. );
  39. return $row['count'];
  40. }
  41. /**
  42. * Displays the title + grid.
  43. *
  44. * @return string html code
  45. */
  46. public function display()
  47. {
  48. // Action links
  49. $html = '<div class="actions" style="margin-bottom:20px">';
  50. $html .= '<a href="'.api_get_path(WEB_CODE_PATH).'admin">'.
  51. Display::return_icon(
  52. 'back.png',
  53. get_lang('Back'),
  54. '',
  55. '32'
  56. )
  57. .'</a>';
  58. $html .= '<a href="'.api_get_self().'?action=add">'.
  59. Display::return_icon(
  60. 'add.png',
  61. get_lang('Add'),
  62. '',
  63. '32'
  64. ).'</a>';
  65. $html .='</div>';
  66. $html .= Display::grid_html('mail_template');
  67. return $html;
  68. }
  69. /**
  70. * Returns a Form validator Obj.
  71. *
  72. * @param string $url
  73. * @param string $action
  74. *
  75. * @return FormValidator
  76. */
  77. public function returnForm($url, $action = 'add')
  78. {
  79. $form = new FormValidator('template', 'post', $url);
  80. // Setting the form elements
  81. $header = get_lang('Add');
  82. if ($action === 'edit') {
  83. $header = get_lang('Modify');
  84. }
  85. $id = isset($_GET['id']) ? (int) $_GET['id'] : '';
  86. $form->addElement('header', '', $header);
  87. $form->addElement('hidden', 'id', $id);
  88. $form->addElement(
  89. 'text',
  90. 'name',
  91. get_lang('Name'),
  92. ['size' => '70', 'id' => 'name']
  93. );
  94. /*$form->addHtmlEditor(
  95. 'email_template',
  96. get_lang('Template'),
  97. false,
  98. false,
  99. [
  100. 'ToolbarSet' => 'Careers',
  101. 'Width' => '100%',
  102. 'Height' => '250',
  103. ]
  104. );*/
  105. $form->addTextarea(
  106. 'email_template',
  107. get_lang('Template')
  108. );
  109. $finder = new Finder();
  110. $files = $finder
  111. ->files()
  112. ->in(api_get_path(SYS_CODE_PATH).'template/default/mail')
  113. ->sort(
  114. function ($a, $b) {
  115. return strcmp($a->getRealpath(), $b->getRealpath());
  116. }
  117. );
  118. $options = [];
  119. /** @var SplFileInfo $file */
  120. foreach ($files as $file) {
  121. $options[$file->getFilename()] = $file->getFilename();
  122. }
  123. $form->addSelect(
  124. 'type',
  125. get_lang('Type'),
  126. $options
  127. );
  128. $defaults = $this->get($id);
  129. if ($action === 'edit') {
  130. $form->addLabel(get_lang('CreatedAt'), Display::dateToStringAgoAndLongDate($defaults['created_at']));
  131. $form->addLabel(get_lang('UpdatedAt'), Display::dateToStringAgoAndLongDate($defaults['updated_at']));
  132. $form->addButtonSave(get_lang('Modify'), 'submit');
  133. } else {
  134. $form->addButtonCreate(get_lang('Add'), 'submit');
  135. }
  136. // Setting the defaults
  137. if (!empty($defaults)) {
  138. $defaults['email_template'] = $defaults['template'];
  139. }
  140. $form->setDefaults($defaults);
  141. // Setting the rules
  142. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  143. return $form;
  144. }
  145. /**
  146. * @param int $id
  147. *
  148. * @return bool
  149. */
  150. public function setDefault($id)
  151. {
  152. $template = $this->get($id);
  153. if (empty($template)) {
  154. return false;
  155. }
  156. $type = $template['type'];
  157. $urlId = api_get_current_access_url_id();
  158. $sql = "UPDATE {$this->table} SET default_template = 0
  159. WHERE type = '$type' AND url_id = $urlId";
  160. Database::query($sql);
  161. $sql = "UPDATE {$this->table} SET default_template = 1
  162. WHERE id = $id";
  163. Database::query($sql);
  164. return true;
  165. }
  166. }