1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/yiimaker/yii2-email-templates |
4
|
|
|
* @copyright Copyright (c) 2017-2019 Yii Maker |
5
|
|
|
* @license BSD 3-Clause License |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace ymaker\email\templates\behaviors; |
9
|
|
|
|
10
|
|
|
use yii\base\Behavior; |
11
|
|
|
use yii\db\BaseActiveRecord; |
12
|
|
|
use yii\helpers\Json; |
13
|
|
|
use ymaker\email\templates\repositories\EmailTemplatesRepositoryInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Behavior for appending of email templates to ActiveRecord models. |
17
|
|
|
* |
18
|
|
|
* @property string $letterSubject |
19
|
|
|
* @property string $letterBody |
20
|
|
|
* @property string $emailTemplateHint |
21
|
|
|
* |
22
|
|
|
* @author Volodymyr Kupriienko <[email protected]> |
23
|
|
|
* @since 3.0 |
24
|
|
|
*/ |
25
|
|
|
class EmailTemplateBehavior extends Behavior |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var BaseActiveRecord |
29
|
|
|
*/ |
30
|
|
|
public $owner; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var EmailTemplatesRepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $_repository; |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $_key = 'default'; |
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $_languageAttribute = 'language'; |
44
|
|
|
/** |
45
|
|
|
* @var \ymaker\email\templates\entities\EmailTemplate |
46
|
|
|
*/ |
47
|
|
|
private $_template; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function __construct(EmailTemplatesRepositoryInterface $repository, $config = []) |
54
|
|
|
{ |
55
|
|
|
$this->_repository = $repository; |
56
|
|
|
|
57
|
|
|
parent::__construct($config); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function init() |
64
|
|
|
{ |
65
|
|
|
$this->fetchEmailTemplate(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function events() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
BaseActiveRecord::EVENT_AFTER_FIND => 'fetchEmailTemplate', |
75
|
|
|
BaseActiveRecord::EVENT_AFTER_INSERT => 'saveEmailTemplate', |
76
|
|
|
BaseActiveRecord::EVENT_AFTER_UPDATE => 'saveEmailTemplate', |
77
|
|
|
BaseActiveRecord::EVENT_AFTER_DELETE => 'deleteEmailTemplate', |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $key |
83
|
|
|
*/ |
84
|
|
|
public function setKey($key) |
85
|
|
|
{ |
86
|
|
|
$this->_key = $key; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $value |
91
|
|
|
*/ |
92
|
|
|
public function setLanguageAttribute($value) |
93
|
|
|
{ |
94
|
|
|
$this->_languageAttribute = $value; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getLetterSubject() |
101
|
|
|
{ |
102
|
|
|
return $this->getTranslation()->subject; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $subject |
107
|
|
|
*/ |
108
|
|
|
public function setLetterSubject($subject) |
109
|
|
|
{ |
110
|
|
|
$this->getTranslation()->subject = $subject; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getLetterBody() |
117
|
|
|
{ |
118
|
|
|
return $this->getTranslation()->body; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $body |
123
|
|
|
*/ |
124
|
|
|
public function setLetterBody($body) |
125
|
|
|
{ |
126
|
|
|
$this->getTranslation()->body = $body; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getEmailTemplateHint() |
133
|
|
|
{ |
134
|
|
|
return $this->getTranslation()->hint; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $hint |
139
|
|
|
*/ |
140
|
|
|
public function setEmailTemplateHint($hint) |
141
|
|
|
{ |
142
|
|
|
$this->getTranslation()->hint = $hint; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Fetch email template from database. |
147
|
|
|
*/ |
148
|
|
|
public function fetchEmailTemplate() |
149
|
|
|
{ |
150
|
|
|
if (null !== $this->owner) { |
151
|
|
|
$this->_template = $this->_repository->getByKeyWithTranslation( |
152
|
|
|
$this->generateKey(), |
153
|
|
|
$this->owner->{$this->_languageAttribute} |
154
|
|
|
); |
155
|
|
|
} else { |
156
|
|
|
$this->_template = $this->_repository->create(); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Save email template to database. |
162
|
|
|
*/ |
163
|
|
|
public function saveEmailTemplate() |
164
|
|
|
{ |
165
|
|
|
if (null === $this->_template->key) { |
166
|
|
|
$this->_template->key = $this->generateKey(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->_repository->save($this->_template); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Delete email template. |
174
|
|
|
*/ |
175
|
|
|
public function deleteEmailTemplate() |
176
|
|
|
{ |
177
|
|
|
$this->_repository->deleteObject($this->_template); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Generates key for email template. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
protected function generateKey() |
186
|
|
|
{ |
187
|
|
|
return Json::encode([ |
188
|
|
|
'model' => \get_class($this->owner), |
189
|
|
|
'id' => $this->owner->getPrimaryKey(), |
190
|
|
|
'key' => $this->_key, |
191
|
|
|
]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return \ymaker\email\templates\entities\EmailTemplateTranslation |
196
|
|
|
*/ |
197
|
|
|
private function getTranslation() |
198
|
|
|
{ |
199
|
|
|
$language = $this->owner->{$this->_languageAttribute}; |
200
|
|
|
|
201
|
|
|
return $this->_template->getTranslation($language); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|