|
1
|
|
|
<?php |
|
2
|
|
|
namespace Waca\DataObjects; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use PDO; |
|
6
|
|
|
use Waca\DataObject; |
|
7
|
|
|
use Waca\Exceptions\OptimisticLockFailedException; |
|
8
|
|
|
use Waca\PdoDatabase; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Email template data object |
|
12
|
|
|
* |
|
13
|
|
|
* This is the close reasons thing. |
|
14
|
|
|
*/ |
|
15
|
|
|
class EmailTemplate extends DataObject |
|
16
|
|
|
{ |
|
17
|
|
|
const CREATED = "created"; |
|
18
|
|
|
const NOT_CREATED = "not created"; |
|
19
|
|
|
const NONE = null; |
|
20
|
|
|
private $name; |
|
21
|
|
|
private $text; |
|
22
|
|
|
/** @var string|null */ |
|
23
|
|
|
private $jsquestion; |
|
24
|
|
|
private $active = 1; |
|
25
|
|
|
private $preloadonly = 0; |
|
26
|
|
|
private $defaultaction = self::NOT_CREATED; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Gets active non-preload templates |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $defaultAction Default action to take (EmailTemplate::CREATED or EmailTemplate::NOT_CREATED) |
|
32
|
|
|
* @param PdoDatabase $database |
|
33
|
|
|
* |
|
34
|
|
|
* @return array|false |
|
35
|
|
|
*/ |
|
36
|
|
View Code Duplication |
public static function getActiveTemplates($defaultAction, PdoDatabase $database) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
global $createdid; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
$statement = $database->prepare(<<<SQL |
|
41
|
|
|
SELECT * FROM `emailtemplate` |
|
42
|
|
|
WHERE defaultaction = :forcreated AND active = 1 AND preloadonly = 0 AND id != :createdid; |
|
43
|
|
|
SQL |
|
44
|
|
|
); |
|
45
|
|
|
$statement->bindValue(":createdid", $createdid); |
|
46
|
|
|
$statement->bindValue(":forcreated", $defaultAction); |
|
47
|
|
|
|
|
48
|
|
|
$statement->execute(); |
|
49
|
|
|
|
|
50
|
|
|
$resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class()); |
|
51
|
|
|
|
|
52
|
|
|
/** @var EmailTemplate $t */ |
|
53
|
|
|
foreach ($resultObject as $t) { |
|
54
|
|
|
$t->setDatabase($database); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $resultObject; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Gets active non-preload and preload templates, optionally filtered by the default action. |
|
62
|
|
|
* |
|
63
|
|
|
* @param null|bool|string $defaultAction Default action to take (EmailTemplate::CREATED, |
|
64
|
|
|
* EmailTemplate::NOT_CREATED, or EmailTemplate::NONE), or optionally null to |
|
65
|
|
|
* just get everything. |
|
66
|
|
|
* @param PdoDatabase $database |
|
67
|
|
|
* |
|
68
|
|
|
* @return array|false |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function getAllActiveTemplates($defaultAction, PdoDatabase $database) |
|
71
|
|
|
{ |
|
72
|
|
|
$statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE defaultaction = :forcreated AND active = 1;"); |
|
73
|
|
|
|
|
74
|
|
|
if ($defaultAction === false) { |
|
75
|
|
|
$statement = $database->prepare( |
|
76
|
|
|
"SELECT * FROM `emailtemplate` WHERE defaultaction NOT IN ('created', 'not created') AND active = 1;"); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($defaultAction === null) { |
|
80
|
|
|
$statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE active = 1;"); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$statement->bindValue(":forcreated", $defaultAction); |
|
84
|
|
|
|
|
85
|
|
|
$statement->execute(); |
|
86
|
|
|
|
|
87
|
|
|
$resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class()); |
|
88
|
|
|
|
|
89
|
|
|
/** @var EmailTemplate $t */ |
|
90
|
|
|
foreach ($resultObject as $t) { |
|
91
|
|
|
$t->setDatabase($database); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $resultObject; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Gets all the unactive templates |
|
99
|
|
|
* |
|
100
|
|
|
* @param PdoDatabase $database |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
View Code Duplication |
public static function getAllInactiveTemplates(PdoDatabase $database) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE active = 0;"); |
|
107
|
|
|
$statement->execute(); |
|
108
|
|
|
|
|
109
|
|
|
$resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class()); |
|
110
|
|
|
|
|
111
|
|
|
/** @var EmailTemplate $t */ |
|
112
|
|
|
foreach ($resultObject as $t) { |
|
113
|
|
|
$t->setDatabase($database); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $resultObject; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $name |
|
121
|
|
|
* @param PdoDatabase $database |
|
122
|
|
|
* |
|
123
|
|
|
* @return EmailTemplate|false |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function getByName($name, PdoDatabase $database) |
|
126
|
|
|
{ |
|
127
|
|
|
$statement = $database->prepare("SELECT * FROM `emailtemplate` WHERE name = :name LIMIT 1;"); |
|
128
|
|
|
$statement->bindValue(":name", $name); |
|
129
|
|
|
|
|
130
|
|
|
$statement->execute(); |
|
131
|
|
|
|
|
132
|
|
|
$resultObject = $statement->fetchObject(get_called_class()); |
|
133
|
|
|
|
|
134
|
|
|
if ($resultObject != false) { |
|
135
|
|
|
$resultObject->setDatabase($database); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $resultObject; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return EmailTemplate |
|
143
|
|
|
*/ |
|
144
|
|
|
public static function getDroppedTemplate() |
|
145
|
|
|
{ |
|
146
|
|
|
$t = new EmailTemplate(); |
|
147
|
|
|
$t->id = 0; |
|
148
|
|
|
$t->active = 1; |
|
149
|
|
|
$t->name = 'Dropped'; |
|
150
|
|
|
|
|
151
|
|
|
return $t; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @throws Exception |
|
156
|
|
|
*/ |
|
157
|
|
|
public function save() |
|
158
|
|
|
{ |
|
159
|
|
|
if ($this->isNew()) { |
|
160
|
|
|
// insert |
|
161
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
|
162
|
|
|
INSERT INTO `emailtemplate` (name, text, jsquestion, defaultaction, active, preloadonly) |
|
163
|
|
|
VALUES (:name, :text, :jsquestion, :defaultaction, :active, :preloadonly); |
|
164
|
|
|
SQL |
|
165
|
|
|
); |
|
166
|
|
|
$statement->bindValue(":name", $this->name); |
|
167
|
|
|
$statement->bindValue(":text", $this->text); |
|
168
|
|
|
$statement->bindValue(":jsquestion", $this->jsquestion); |
|
169
|
|
|
$statement->bindValue(":defaultaction", $this->defaultaction); |
|
170
|
|
|
$statement->bindValue(":active", $this->active); |
|
171
|
|
|
$statement->bindValue(":preloadonly", $this->preloadonly); |
|
172
|
|
|
|
|
173
|
|
|
if ($statement->execute()) { |
|
174
|
|
|
$this->id = $this->dbObject->lastInsertId(); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
else { |
|
177
|
|
|
throw new Exception($statement->errorInfo()); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
else { |
|
181
|
|
|
// update |
|
182
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
|
183
|
|
|
UPDATE `emailtemplate` |
|
184
|
|
|
SET name = :name, |
|
185
|
|
|
text = :text, |
|
186
|
|
|
jsquestion = :jsquestion, |
|
187
|
|
|
defaultaction = :defaultaction, |
|
188
|
|
|
active = :active, |
|
189
|
|
|
preloadonly = :preloadonly, |
|
190
|
|
|
updateversion = updateversion + 1 |
|
191
|
|
|
WHERE id = :id AND updateversion = :updateversion |
|
192
|
|
|
LIMIT 1; |
|
193
|
|
|
SQL |
|
194
|
|
|
); |
|
195
|
|
|
$statement->bindValue(':id', $this->id); |
|
196
|
|
|
$statement->bindValue(':updateversion', $this->updateversion); |
|
197
|
|
|
|
|
198
|
|
|
$statement->bindValue(':name', $this->name); |
|
199
|
|
|
$statement->bindValue(":text", $this->text); |
|
200
|
|
|
$statement->bindValue(":jsquestion", $this->jsquestion); |
|
201
|
|
|
$statement->bindValue(":defaultaction", $this->defaultaction); |
|
202
|
|
|
$statement->bindValue(":active", $this->active); |
|
203
|
|
|
$statement->bindValue(":preloadonly", $this->preloadonly); |
|
204
|
|
|
|
|
205
|
|
|
if (!$statement->execute()) { |
|
206
|
|
|
throw new Exception($statement->errorInfo()); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
if ($statement->rowCount() !== 1) { |
|
210
|
|
|
throw new OptimisticLockFailedException(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$this->updateversion++; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Override delete() from DataObject |
|
219
|
|
|
*/ |
|
220
|
|
|
public function delete() |
|
221
|
|
|
{ |
|
222
|
|
|
throw new Exception("You shouldn't be doing that, you'll break logs."); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @return string |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getName() |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->name; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param string $name |
|
235
|
|
|
*/ |
|
236
|
|
|
public function setName($name) |
|
237
|
|
|
{ |
|
238
|
|
|
$this->name = $name; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return string |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getText() |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->text; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param string $text |
|
251
|
|
|
*/ |
|
252
|
|
|
public function setText($text) |
|
253
|
|
|
{ |
|
254
|
|
|
$this->text = $text; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @return string|null |
|
259
|
|
|
*/ |
|
260
|
|
|
public function getJsquestion() |
|
261
|
|
|
{ |
|
262
|
|
|
return $this->jsquestion; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param string $jsquestion |
|
267
|
|
|
*/ |
|
268
|
|
|
public function setJsquestion($jsquestion) |
|
269
|
|
|
{ |
|
270
|
|
|
$this->jsquestion = $jsquestion; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @return string |
|
275
|
|
|
*/ |
|
276
|
|
|
public function getDefaultAction() |
|
277
|
|
|
{ |
|
278
|
|
|
return $this->defaultaction; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @param string $defaultAction |
|
283
|
|
|
*/ |
|
284
|
|
|
public function setDefaultAction($defaultAction) |
|
285
|
|
|
{ |
|
286
|
|
|
$this->defaultaction = $defaultAction; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @return bool |
|
291
|
|
|
*/ |
|
292
|
|
|
public function getActive() |
|
293
|
|
|
{ |
|
294
|
|
|
return $this->active == 1; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @param bool $active |
|
299
|
|
|
*/ |
|
300
|
|
|
public function setActive($active) |
|
301
|
|
|
{ |
|
302
|
|
|
$this->active = $active ? 1 : 0; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @return bool |
|
307
|
|
|
*/ |
|
308
|
|
|
public function getPreloadOnly() |
|
309
|
|
|
{ |
|
310
|
|
|
return $this->preloadonly == 1; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* @param bool $preloadonly |
|
315
|
|
|
*/ |
|
316
|
|
|
public function setPreloadOnly($preloadonly) |
|
317
|
|
|
{ |
|
318
|
|
|
$this->preloadonly = $preloadonly ? 1 : 0; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.