-
Notifications
You must be signed in to change notification settings - Fork 0
/
PKPEmailTemplateDAO.inc.php
747 lines (678 loc) · 22.9 KB
/
PKPEmailTemplateDAO.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
<?php
/**
* @file classes/mail/PKPEmailTemplateDAO.inc.php
*
* Copyright (c) 2000-2013 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class PKPEmailTemplateDAO
* @ingroup mail
* @see PKPEmailTemplate
*
* @brief Operations for retrieving and modifying Email Template objects.
*/
class PKPEmailTemplateDAO extends DAO {
/**
* Constructor
*/
function PKPEmailTemplateDAO() {
parent::DAO();
}
/**
* Retrieve a base email template by key.
* @param $emailKey string
* @param assocType int
* @param $assocId int
* @return BaseEmailTemplate
*/
function &getBaseEmailTemplate($emailKey, $assocType, $assocId) {
$result =& $this->retrieve(
'SELECT d.email_key,
d.can_edit,
d.can_disable,
COALESCE(e.enabled, 1) AS enabled,
e.email_id,
e.assoc_type,
e.assoc_id,
d.from_role_id,
d.to_role_id
FROM email_templates_default d
LEFT JOIN email_templates e ON (d.email_key = e.email_key AND e.assoc_type = ? AND e.assoc_id = ?)
WHERE d.email_key = ?',
array($assocType, $assocId, $emailKey)
);
$returner = null;
if ($result->RecordCount() != 0) {
$returner =& $this->_returnBaseEmailTemplateFromRow($result->GetRowAssoc(false));
}
$result->Close();
unset($result);
return $returner;
}
/**
* Retrieve localized email template by key.
* @param $emailKey string
* @param $assocType int
* @param $assocId int
* @return LocaleEmailTemplate
*/
function &getLocaleEmailTemplate($emailKey, $assocType, $assocId) {
$result =& $this->retrieve(
'SELECT d.email_key,
d.can_edit,
d.can_disable,
COALESCE(e.enabled, 1) AS enabled,
e.email_id,
e.assoc_type,
e.assoc_id,
d.from_role_id,
d.to_role_id
FROM email_templates_default d
LEFT JOIN email_templates e ON (d.email_key = e.email_key AND e.assoc_type = ? AND e.assoc_id = ?)
WHERE d.email_key = ?',
array($assocType, $assocId, $emailKey)
);
$returner = null;
if ($result->RecordCount() != 0) {
$returner =& $this->_returnLocaleEmailTemplateFromRow($result->GetRowAssoc(false));
} else {
$result->Close();
unset($result);
// Check to see if there's a custom email template. This is done in PHP to avoid
// having to do a full outer join or union in SQL.
$result =& $this->retrieve(
'SELECT e.email_key,
1 AS can_edit,
1 AS can_disable,
e.enabled,
e.email_id,
e.assoc_type,
e.assoc_id,
NULL AS from_role_id,
NULL AS to_role_id
FROM email_templates e
LEFT JOIN email_templates_default d ON (e.email_key = d.email_key)
WHERE d.email_key IS NULL AND
e.assoc_type = ? AND
e.assoc_id = ? AND
e.email_key = ?',
array($assocType, $assocId, $emailKey)
);
if ($result->RecordCount() != 0) {
$returner =& $this->_returnLocaleEmailTemplateFromRow($result->GetRowAssoc(false));
}
}
$result->Close();
unset($result);
return $returner;
}
/**
* Retrieve an email template by key.
* @param $emailKey string
* @param $locale string
* @param $assocType int
* @param $assocId int
* @return EmailTemplate
*/
function &getEmailTemplate($emailKey, $locale, $assocType, $assocId) {
$primaryLocale = AppLocale::getPrimaryLocale();
$result =& $this->retrieve(
'SELECT COALESCE(edl.subject, ddl.subject, edpl.subject, ddpl.subject) AS subject,
COALESCE(edl.body, ddl.body, edpl.body, ddpl.body) AS body,
COALESCE(e.enabled, 1) AS enabled,
d.email_key, d.can_edit, d.can_disable,
e.assoc_type, e.assoc_id, e.email_id,
COALESCE(ddl.locale, ddpl.locale) AS locale,
d.from_role_id, d.to_role_id
FROM email_templates_default d
LEFT JOIN email_templates_default_data ddpl ON (ddpl.email_key = d.email_key AND ddpl.locale = ?)
LEFT JOIN email_templates_default_data ddl ON (ddl.email_key = d.email_key AND ddl.locale = ?)
LEFT JOIN email_templates e ON (d.email_key = e.email_key AND e.assoc_type = ? AND e.assoc_id = ?)
LEFT JOIN email_templates_data edpl ON (edpl.email_key = e.email_key AND edpl.assoc_type = e.assoc_type AND edpl.assoc_id = e.assoc_id AND edpl.locale = ?)
LEFT JOIN email_templates_data edl ON (edl.email_key = e.email_key AND edl.assoc_type = e.assoc_type AND edl.assoc_id = e.assoc_id AND edl.locale = ?)
WHERE d.email_key = ?',
array($primaryLocale, $locale, $assocType, $assocId, $primaryLocale, $locale, $emailKey)
);
$returner = null;
if ($result->RecordCount() != 0) {
$returner =& $this->_returnEmailTemplateFromRow($result->GetRowAssoc(false));
$returner->setCustomTemplate(false);
} else {
$result->Close();
unset($result);
// Check to see if there's a custom email template. This is done in PHP to avoid
// having to do a full outer join or union in SQL.
$result =& $this->retrieve(
'SELECT ed.subject,
ed.body,
1 AS enabled,
e.email_key,
1 AS can_edit,
0 AS can_disable,
e.assoc_type,
e.assoc_id,
e.email_id,
ed.locale,
NULL AS from_role_id,
NULL AS to_role_id
FROM email_templates e
LEFT JOIN email_templates_data ed ON (ed.email_key = e.email_key AND ed.assoc_type = e.assoc_type AND ed.assoc_id = e.assoc_id)
LEFT JOIN email_templates_default d ON (e.email_key = d.email_key)
WHERE d.email_key IS NULL AND
e.assoc_type = ? AND
e.assoc_id = ? AND
e.email_key = ? AND
ed.locale = ?',
array($assocType, $assocId, $emailKey, $locale)
);
if ($result->RecordCount() != 0) {
$returner =& $this->_returnEmailTemplateFromRow($result->GetRowAssoc(false));
$returner->setCustomTemplate(true);
}
}
$result->Close();
unset($result);
return $returner;
}
/**
* Internal function to return an email template object from a row.
* @param $row array
* @return BaseEmailTemplate
*/
function &_returnBaseEmailTemplateFromRow(&$row) {
$emailTemplate = new BaseEmailTemplate();
$emailTemplate->setEmailId($row['email_id']);
$emailTemplate->setAssocType($row['assoc_type']);
$emailTemplate->setAssocId($row['assoc_id']);
$emailTemplate->setEmailKey($row['email_key']);
$emailTemplate->setEnabled($row['enabled'] == null ? 1 : $row['enabled']);
$emailTemplate->setCanDisable($row['can_disable']);
$emailTemplate->setFromRoleId($row['from_role_id']);
$emailTemplate->setToRoleId($row['to_role_id']);
HookRegistry::call('EmailTemplateDAO::_returnBaseEmailTemplateFromRow', array(&$emailTemplate, &$row));
return $emailTemplate;
}
/**
* Internal function to return an email template object from a row.
* @param $row array
* @return LocaleEmailTemplate
*/
function &_returnLocaleEmailTemplateFromRow(&$row) {
$emailTemplate = new LocaleEmailTemplate();
$emailTemplate->setEmailId($row['email_id']);
$emailTemplate->setAssocType($row['assoc_type']);
$emailTemplate->setAssocId($row['assoc_id']);
$emailTemplate->setEmailKey($row['email_key']);
$emailTemplate->setEnabled($row['enabled'] == null ? 1 : $row['enabled']);
$emailTemplate->setCanDisable($row['can_disable']);
$emailTemplate->setFromRoleId($row['from_role_id']);
$emailTemplate->setToRoleId($row['to_role_id']);
$emailTemplate->setCustomTemplate(false);
if (!HookRegistry::call('EmailTemplateDAO::_returnLocaleEmailTemplateFromRow', array(&$emailTemplate, &$row))) {
$result =& $this->retrieve(
'SELECT dd.locale,
dd.description,
COALESCE(ed.subject, dd.subject) AS subject,
COALESCE(ed.body, dd.body) AS body
FROM email_templates_default_data dd
LEFT JOIN email_templates_data ed ON (dd.email_key = ed.email_key AND dd.locale = ed.locale AND ed.assoc_type = ? AND ed.assoc_id = ?)
WHERE dd.email_key = ?',
array($row['assoc_type'], $row['assoc_id'], $row['email_key'])
);
while (!$result->EOF) {
$dataRow =& $result->GetRowAssoc(false);
$emailTemplate->addLocale($dataRow['locale']);
$emailTemplate->setSubject($dataRow['locale'], $dataRow['subject']);
$emailTemplate->setBody($dataRow['locale'], $dataRow['body']);
$emailTemplate->setDescription($dataRow['locale'], $dataRow['description']);
$result->MoveNext();
}
$result->Close();
unset($result);
// Retrieve custom email contents as well; this is done in PHP to avoid
// using a SQL outer join or union.
$result =& $this->retrieve(
'SELECT ed.locale,
ed.subject,
ed.body
FROM email_templates_data ed
LEFT JOIN email_templates_default_data dd ON (ed.email_key = dd.email_key AND dd.locale = ed.locale)
WHERE ed.assoc_type = ? AND
ed.assoc_id = ? AND
ed.email_key = ? AND
dd.email_key IS NULL',
array($row['assoc_type'], $row['assoc_id'], $row['email_key'])
);
while (!$result->EOF) {
$dataRow =& $result->GetRowAssoc(false);
$emailTemplate->addLocale($dataRow['locale']);
$emailTemplate->setSubject($dataRow['locale'], $dataRow['subject']);
$emailTemplate->setBody($dataRow['locale'], $dataRow['body']);
$result->MoveNext();
$emailTemplate->setCustomTemplate(true);
}
$result->Close();
unset($result);
}
return $emailTemplate;
}
/**
* Internal function to return an email template object from a row.
* @param $row array
* @return EmailTemplate
*/
function &_returnEmailTemplateFromRow(&$row, $isCustomTemplate=null) {
$emailTemplate = new EmailTemplate();
$emailTemplate->setEmailId($row['email_id']);
$emailTemplate->setAssocType($row['assoc_type']);
$emailTemplate->setAssocId($row['assoc_id']);
$emailTemplate->setEmailKey($row['email_key']);
$emailTemplate->setLocale($row['locale']);
$emailTemplate->setSubject($row['subject']);
$emailTemplate->setBody($row['body']);
$emailTemplate->setEnabled($row['enabled'] == null ? 1 : $row['enabled']);
$emailTemplate->setCanDisable($row['can_disable']);
$emailTemplate->setFromRoleId($row['from_role_id']);
$emailTemplate->setToRoleId($row['to_role_id']);
if ($isCustomTemplate !== null) {
$emailTemplate->setCustomTemplate($isCustomTemplate);
}
HookRegistry::call('EmailTemplateDAO::_returnEmailTemplateFromRow', array(&$emailTemplate, &$row));
return $emailTemplate;
}
/**
* Insert a new base email template.
* @param $emailTemplate BaseEmailTemplate
*/
function insertBaseEmailTemplate(&$emailTemplate) {
return $this->update(
'INSERT INTO email_templates
(assoc_type, assoc_id, email_key, enabled)
VALUES
(?, ?, ?, ?)',
array(
$emailTemplate->getAssocType(),
$emailTemplate->getAssocId(),
$emailTemplate->getEmailKey(),
$emailTemplate->getEnabled() == null ? 0 : 1
)
);
$emailTemplate->setEmailId($this->getInsertEmailId());
return $emailTemplate->getEmailId();
}
/**
* Update an existing base email template.
* @param $emailTemplate BaseEmailTemplate
*/
function updateBaseEmailTemplate(&$emailTemplate) {
return $this->update(
'UPDATE email_templates
SET enabled = ?
WHERE email_id = ?',
array(
$emailTemplate->getEnabled() == null ? 0 : 1,
$emailTemplate->getEmailId()
)
);
}
/**
* Insert a new localized email template.
* @param $emailTemplate LocaleEmailTemplate
*/
function insertLocaleEmailTemplate(&$emailTemplate) {
$this->insertBaseEmailTemplate($emailTemplate);
return $this->updateLocaleEmailTemplateData($emailTemplate);
}
/**
* Update an existing localized email template.
* @param $emailTemplate LocaleEmailTemplate
*/
function updateLocaleEmailTemplate(&$emailTemplate) {
$this->updateBaseEmailTemplate($emailTemplate);
return $this->updateLocaleEmailTemplateData($emailTemplate);
}
/**
* Insert/update locale-specific email template data.
* @param $emailTemplate LocaleEmailTemplate
*/
function updateLocaleEmailTemplateData(&$emailTemplate) {
foreach ($emailTemplate->getLocales() as $locale) {
$result =& $this->retrieve(
'SELECT COUNT(*)
FROM email_templates_data
WHERE email_key = ? AND
locale = ? AND
assoc_type = ? AND
assoc_id = ?',
array(
$emailTemplate->getEmailKey(),
$locale,
$emailTemplate->getAssocType(),
$emailTemplate->getAssocId()
)
);
if ($result->fields[0] == 0) {
$this->update(
'INSERT INTO email_templates_data
(email_key, locale, assoc_type, assoc_id, subject, body)
VALUES
(?, ?, ?, ?, ?, ?)',
array(
$emailTemplate->getEmailKey(),
$locale,
$emailTemplate->getAssocType(),
$emailTemplate->getAssocId(),
$emailTemplate->getSubject($locale),
$emailTemplate->getBody($locale)
)
);
} else {
$this->update(
'UPDATE email_templates_data
SET subject = ?,
body = ?
WHERE email_key = ? AND
locale = ? AND
assoc_type = ? AND
assoc_id = ?',
array(
$emailTemplate->getSubject($locale),
$emailTemplate->getBody($locale),
$emailTemplate->getEmailKey(),
$locale,
$emailTemplate->getAssocType(),
$emailTemplate->getAssocId()
)
);
}
$result->Close();
unset($result);
}
}
/**
* Delete an email template by key.
* @param $emailKey string
* @param $assocType int
* @param $assocId int
*/
function deleteEmailTemplateByKey($emailKey, $assocType, $assocId) {
$this->update(
'DELETE FROM email_templates_data WHERE email_key = ? AND assoc_type = ? AND assoc_id = ?',
array($emailKey, $assocType, $assocId)
);
return $this->update(
'DELETE FROM email_templates WHERE email_key = ? AND assoc_type = ? AND assoc_id = ?',
array($emailKey, $assocType, $assocId)
);
}
/**
* Retrieve all email templates.
* @param $locale string
* @param $assocType int
* @param $assocId int
* @param $rangeInfo object optional
* @return array Email templates
*/
function &getEmailTemplates($locale, $assocType, $assocId, $rangeInfo = null) {
$emailTemplates = array();
$result =& $this->retrieveRange(
'SELECT COALESCE(ed.subject, dd.subject) AS subject,
COALESCE(ed.body, dd.body) AS body,
COALESCE(e.enabled, 1) AS enabled,
d.email_key, d.can_edit, d.can_disable,
e.assoc_type, e.assoc_id, e.email_id,
dd.locale,
d.from_role_id, d.to_role_id
FROM email_templates_default d
LEFT JOIN email_templates_default_data dd ON (dd.email_key = d.email_key)
LEFT JOIN email_templates e ON (d.email_key = e.email_key AND e.assoc_type = ? AND e.assoc_id = ?)
LEFT JOIN email_templates_data ed ON (ed.email_key = e.email_key AND ed.assoc_type = e.assoc_type AND ed.assoc_id = e.assoc_id AND ed.locale = dd.locale)
WHERE dd.locale = ?',
array($assocType, $assocId, $locale),
$rangeInfo
);
while (!$result->EOF) {
$emailTemplates[] =& $this->_returnEmailTemplateFromRow($result->GetRowAssoc(false), false);
$result->MoveNext();
}
$result->Close();
unset($result);
// Fetch custom email templates as well; this is done in PHP
// to avoid a union or full outer join call in SQL.
$result =& $this->retrieve(
'SELECT ed.subject,
ed.body,
e.enabled,
e.email_key,
1 AS can_edit,
1 AS can_disable,
e.assoc_type,
e.assoc_id,
e.email_id,
ed.locale,
NULL AS from_role_id,
NULL AS to_role_id
FROM email_templates e
LEFT JOIN email_templates_data ed ON (e.email_key = ed.email_key AND ed.assoc_type = e.assoc_type AND ed.assoc_id = e.assoc_id AND ed.locale = ?)
LEFT JOIN email_templates_default d ON (e.email_key = d.email_key)
WHERE e.assoc_type = ? AND
e.assoc_id = ? AND
d.email_key IS NULL',
array($locale, $assocType, $assocId)
);
while (!$result->EOF) {
$row = $result->getRowAssoc(false);
$emailTemplates[] =& $this->_returnEmailTemplateFromRow($result->GetRowAssoc(false), true);
$result->MoveNext();
}
$result->Close();
unset($result);
// Sort all templates by email key.
$compare = create_function('$t1, $t2', 'return strcmp($t1->getEmailKey(), $t2->getEmailKey());');
usort ($emailTemplates, $compare);
return $emailTemplates;
}
/**
* Get the ID of the last inserted email template.
* @return int
*/
function getInsertEmailId() {
return $this->getInsertId('email_templates', 'emailId');
}
/**
* Delete all email templates for a specific journal/conference/...
* @param $assocType int
* @param $assocId int
*/
function deleteEmailTemplatesByAssoc($assocType, $assocId) {
$this->update(
'DELETE FROM email_templates_data WHERE assoc_type = ? AND assoc_id = ?', array($assocType, $assocId)
);
return $this->update(
'DELETE FROM email_templates WHERE assoc_type = ? AND assoc_id = ?', array($assocType, $assocId)
);
}
/**
* Delete all email templates for a specific locale.
* @param $locale string
*/
function deleteEmailTemplatesByLocale($locale) {
$this->update(
'DELETE FROM email_templates_data WHERE locale = ?', $locale
);
}
/**
* Delete all default email templates for a specific locale.
* @param $locale string
*/
function deleteDefaultEmailTemplatesByLocale($locale) {
$this->update(
'DELETE FROM email_templates_default_data WHERE locale = ?', $locale
);
}
/**
* Check if a template exists with the given email key for a journal/
* conference/...
* @param $emailKey string
* @param $assocType int optional
* @param $assocId int optional
* @return boolean
*/
function templateExistsByKey($emailKey, $assocType = null, $assocId = null) {
if ($assocType !== null) {
$result =& $this->retrieve(
'SELECT COUNT(*)
FROM email_templates
WHERE email_key = ? AND
assoc_type = ? AND
assoc_id = ?',
array(
$emailKey,
$assocType,
$assocId
)
);
if (isset($result->fields[0]) && $result->fields[0] != 0) {
$result->Close();
unset($result);
return true;
}
$result->Close();
unset($result);
}
$result =& $this->retrieve(
'SELECT COUNT(*)
FROM email_templates_default
WHERE email_key = ?',
$emailKey
);
if (isset($result->fields[0]) && $result->fields[0] != 0) {
$returner = true;
} else {
$returner = false;
}
$result->Close();
unset($result);
return $returner;
}
/**
* Check if a custom template exists with the given email key for a
* journal/conference/...
* @param $emailKey string
* @param $assocType int
* @param $assocId int
* @return boolean
*/
function customTemplateExistsByKey($emailKey, $assocType, $assocId) {
$result =& $this->retrieve(
'SELECT COUNT(*)
FROM email_templates e
LEFT JOIN email_templates_default d ON (e.email_key = d.email_key)
WHERE e.email_key = ? AND
d.email_key IS NULL AND
e.assoc_type = ? AND
e.assoc_id = ?',
array(
$emailKey,
$assocType,
$assocId
)
);
$returner = (isset($result->fields[0]) && $result->fields[0] != 0);
$result->Close();
unset($result);
return $returner;
}
function getMainEmailTemplatesFilename() {
return 'registry/emailTemplates.xml';
}
function getMainEmailTemplateDataFilename($locale = null) {
if ($locale !== null && !PKPLocale::isLocaleValid($locale)) return null;
if ($locale === null) $locale = '{$installedLocale}';
return "locale/$locale/emailTemplates.xml";
}
/**
* Install email templates from an XML file.
* NOTE: Uses qstr instead of ? bindings so that SQL can be fetched
* rather than executed.
* @param $templatesFile string Filename to install
* @param $returnSql boolean Whether or not to return SQL rather than
* executing it
* @param $emailKey string Optional name of single email key to install,
* skipping others
* @param $skipExisting boolean If true, do not install email templates
* that already exist in the database
* @param $emailKey string If specified, the key of the single template
* to install (otherwise all are installed)
* @return array
*/
function installEmailTemplates($templatesFile, $returnSql = false, $emailKey = null, $skipExisting = false) {
$xmlDao = new XMLDAO();
$sql = array();
$data = $xmlDao->parseStruct($templatesFile, array('email'));
if (!isset($data['email'])) return false;
foreach ($data['email'] as $entry) {
$attrs = $entry['attributes'];
if ($emailKey && $emailKey != $attrs['key']) continue;
if ($skipExisting && $this->templateExistsByKey($attrs['key'])) continue;
$dataSource = $this->getDataSource();
$sql[] = 'INSERT INTO email_templates_default
(email_key, can_disable, can_edit, from_role_id, to_role_id)
VALUES
(' .
$dataSource->qstr($attrs['key']) . ', ' .
($attrs['can_disable']?1:0) . ', ' .
($attrs['can_edit']?1:0) . ', ' .
(isset($attrs['from_role_id'])?((int) $attrs['from_role_id']):'null') . ', ' .
(isset($attrs['to_role_id'])?((int) $attrs['to_role_id']):'null') .
")";
if (!$returnSql) {
$this->update(array_shift($sql));
}
}
if ($returnSql) return $sql;
return true;
}
/**
* Install email template localized data from an XML file.
* NOTE: Uses qstr instead of ? bindings so that SQL can be fetched
* rather than executed.
* @param $templateDataFile string Filename to install
* @param $returnSql boolean Whether or not to return SQL rather than
* executing it
* @param $emailKey string If specified, the key of the single template
* to install (otherwise all are installed)
* @return array
*/
function installEmailTemplateData($templateDataFile, $returnSql = false, $emailKey = null) {
$xmlDao = new XMLDAO();
$sql = array();
$data = $xmlDao->parse($templateDataFile, array('email_texts', 'email_text', 'subject', 'body', 'description'));
if (!$data) return false;
$locale = $data->getAttribute('locale');
foreach ($data->getChildren() as $emailNode) {
if ($emailKey && $emailKey != $emailNode->getAttribute('key')) continue;
$dataSource = $this->getDataSource();
$sql[] = 'DELETE FROM email_templates_default_data WHERE email_key = ' . $dataSource->qstr($emailNode->getAttribute('key')) . ' AND locale = ' . $dataSource->qstr($locale);
if (!$returnSql) {
$this->update(array_shift($sql));
}
$sql[] = 'INSERT INTO email_templates_default_data
(email_key, locale, subject, body, description)
VALUES
(' .
$dataSource->qstr($emailNode->getAttribute('key')) . ', ' .
$dataSource->qstr($locale) . ', ' .
$dataSource->qstr($emailNode->getChildValue('subject')) . ', ' .
$dataSource->qstr($emailNode->getChildValue('body')) . ', ' .
$dataSource->qstr($emailNode->getChildValue('description')) .
")";
if (!$returnSql) {
$this->update(array_shift($sql));
}
}
if ($returnSql) return $sql;
return true;
}
}
?>