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\repositories; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\BaseObject; |
12
|
|
|
use yii\data\ActiveDataProvider; |
13
|
|
|
use yii\db\Connection; |
14
|
|
|
use yii\di\Instance; |
15
|
|
|
use ymaker\email\templates\entities\EmailTemplate; |
16
|
|
|
use ymaker\email\templates\entities\EmailTemplateTranslation; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Repository for email templates. |
20
|
|
|
* |
21
|
|
|
* @author Volodymyr Kupriienko <[email protected]> |
22
|
|
|
* @since 4.0 |
23
|
|
|
*/ |
24
|
|
|
class EmailTemplatesRepository extends BaseObject implements EmailTemplatesRepositoryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array|Connection|string |
28
|
|
|
*/ |
29
|
|
|
private $_db = 'db'; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array|Connection|string $db |
34
|
|
|
*/ |
35
|
|
|
public function setDb($db) |
36
|
|
|
{ |
37
|
|
|
$this->_db = $db; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Initialize connection to database. |
42
|
|
|
* |
43
|
|
|
* @throws \yii\base\InvalidConfigException |
44
|
|
|
*/ |
45
|
|
|
public function init() |
46
|
|
|
{ |
47
|
|
|
$this->_db = Instance::ensure($this->_db, Connection::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Find email template entity by ID. |
52
|
|
|
* |
53
|
|
|
* @param int $id |
54
|
|
|
* |
55
|
|
|
* @return null|EmailTemplate |
56
|
|
|
*/ |
57
|
|
|
public function getById($id) |
58
|
|
|
{ |
59
|
|
|
return EmailTemplate::find() |
60
|
|
|
->where(['id' => $id]) |
61
|
|
|
->with('translations') |
62
|
|
|
->one(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Find template by key with translation. |
67
|
|
|
* |
68
|
|
|
* @param string $key |
69
|
|
|
* @param string $language |
70
|
|
|
* |
71
|
|
|
* @return null|EmailTemplate |
72
|
|
|
*/ |
73
|
|
|
public function getByKeyWithTranslation($key, $language) |
74
|
|
|
{ |
75
|
|
|
return EmailTemplate::find() |
76
|
|
|
->byKey($key) |
77
|
|
|
->withTranslation($language) |
78
|
|
|
->one(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Find all language versions of template by key. |
83
|
|
|
* |
84
|
|
|
* @param string $key |
85
|
|
|
* |
86
|
|
|
* @return null|EmailTemplateTranslation[] |
87
|
|
|
*/ |
88
|
|
|
public function getAll($key) |
89
|
|
|
{ |
90
|
|
|
$template = EmailTemplate::find() |
91
|
|
|
->byKey($key) |
92
|
|
|
->with('translations') |
93
|
|
|
->one(); |
94
|
|
|
|
95
|
|
|
return empty($template->translations) ? null : $template->translations; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns data provider for email template entity. |
100
|
|
|
* |
101
|
|
|
* @return ActiveDataProvider |
102
|
|
|
*/ |
103
|
|
|
public function getDataProvider() |
104
|
|
|
{ |
105
|
|
|
return new ActiveDataProvider([ |
106
|
|
|
'db' => $this->_db, |
107
|
|
|
'query' => EmailTemplate::find()->with('translations'), |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function has($key) |
115
|
|
|
{ |
116
|
|
|
return EmailTemplate::find()->byKey($key)->exists(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Creates new email template model. |
121
|
|
|
* |
122
|
|
|
* @return EmailTemplate |
123
|
|
|
*/ |
124
|
|
|
public function create() |
125
|
|
|
{ |
126
|
|
|
return (new EmailTemplate())->loadDefaultValues(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Save entity. |
131
|
|
|
* |
132
|
|
|
* @param EmailTemplate $entity |
133
|
|
|
* @param array $data |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function save($entity, array $data = []) |
138
|
|
|
{ |
139
|
|
|
try { |
140
|
|
|
if (empty($data)) { |
141
|
|
|
return $entity->save(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->saveInternal($entity, $data); |
145
|
|
|
|
146
|
|
|
return true; |
147
|
|
|
} catch (\Exception $ex) { |
148
|
|
|
Yii::$app->getErrorHandler()->logException($ex); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function delete($id) |
158
|
|
|
{ |
159
|
|
|
try { |
160
|
|
|
if ($model = $this->getById($id)) { |
161
|
|
|
return (bool) $model->delete(); |
162
|
|
|
} |
163
|
|
|
} catch (\Exception $ex) { |
164
|
|
|
Yii::$app->getErrorHandler()->logException($ex); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Removes email template object. |
172
|
|
|
* |
173
|
|
|
* @param EmailTemplate $entity |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function deleteObject($entity) |
178
|
|
|
{ |
179
|
|
|
try { |
180
|
|
|
return (bool) $entity->delete(); |
181
|
|
|
} catch (\Exception $ex) { |
182
|
|
|
Yii::$app->getErrorHandler()->logException($ex); |
183
|
|
|
|
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Save entity to database. |
190
|
|
|
* |
191
|
|
|
* @param EmailTemplate $entity |
192
|
|
|
* @param array $data |
193
|
|
|
* |
194
|
|
|
* @throws \DomainException |
195
|
|
|
* @throws \RuntimeException |
196
|
|
|
*/ |
197
|
|
|
protected function saveInternal(EmailTemplate $entity, array $data) |
198
|
|
|
{ |
199
|
|
|
if ($entity->getIsNewRecord() && !$entity->load($data)) { |
200
|
|
|
throw new \DomainException('Cannot load data to primary model'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
foreach ($data[EmailTemplateTranslation::internalFormName()] as $language => $dataSet) { |
204
|
|
|
$translationEntity = $entity->getTranslation($language); |
205
|
|
|
|
206
|
|
|
foreach ($dataSet as $attribute => $translation) { |
207
|
|
|
$translationEntity->$attribute = $translation; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (!$entity->save()) { |
212
|
|
|
throw new \RuntimeException(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|