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