This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
TemplateStorageActiveRecord.php
57 lines (51 loc) · 1.75 KB
/
TemplateStorageActiveRecord.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
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\activemail;
/**
* TemplateStorageActiveRecord is an active mail template storage based on ActiveRecord.
* It uses ActiveRecord class for the actual template finding.
*
* @see \yii\db\ActiveRecordInterface
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
class TemplateStorageActiveRecord extends TemplateStorage
{
/**
* @var string name of the ActiveRecord class, which should be used for template finding.
* This class should match [[\yii\db\ActiveRecordInterface]] interface.
*/
public $activeRecordClass;
/**
* @var array list of ActiveRecord attributes, which should compose the template data.
* Only these fields will be selected while querying template row.
* You may adjust fields list according to the actual ActiveRecord class.
*/
public $templateDataAttributes = ['subject', 'bodyHtml'];
/**
* @var string name of the ActiveRecord attribute, which stores the template name.
*/
public $templateNameAttribute = 'name';
/**
* @inheritdoc
*/
protected function findTemplate($name)
{
/* @var $activeRecordClass \yii\db\ActiveRecordInterface */
$activeRecordClass = $this->activeRecordClass;
$templateModel = $activeRecordClass::findOne([$this->templateNameAttribute => $name]);
if (!is_object($templateModel)) {
return null;
}
$template = [];
foreach ($this->templateDataAttributes as $attribute) {
$template[$attribute] = $templateModel->$attribute;
}
return $template;
}
}