1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/yiimaker/yii2-email-templates |
4
|
|
|
* @copyright Copyright (c) 2017 Yii Maker |
5
|
|
|
* @license BSD 3-Clause License |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace ymaker\email\templates\services; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Object; |
12
|
|
|
use yii\data\ActiveDataProvider; |
13
|
|
|
use yii\db\Connection; |
14
|
|
|
use yii\di\Instance; |
15
|
|
|
use yii\web\NotFoundHttpException; |
16
|
|
|
use ymaker\email\templates\models\entities\EmailTemplate; |
17
|
|
|
use ymaker\email\templates\models\entities\EmailTemplateTranslation; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Email template service. |
21
|
|
|
* |
22
|
|
|
* @author Vladimir Kuprienko <[email protected]> |
23
|
|
|
* @since 3.0 |
24
|
|
|
*/ |
25
|
|
|
class EmailTemplateService extends Object implements ServiceInterface |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string|array|Connection |
29
|
|
|
*/ |
30
|
|
|
private $_db = 'db'; |
31
|
|
|
/** |
32
|
|
|
* @var EmailTemplate |
33
|
|
|
*/ |
34
|
|
|
private $_model; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string|array|Connection $db |
39
|
|
|
*/ |
40
|
|
|
public function setDb($db) |
41
|
|
|
{ |
42
|
|
|
$this->_db = $db; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function init() |
49
|
|
|
{ |
50
|
|
|
$this->_db = Instance::ensure($this->_db, Connection::class); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return \yii\data\ActiveDataProvider |
55
|
|
|
*/ |
56
|
|
|
public function getDataProvider() |
57
|
|
|
{ |
58
|
|
|
return new ActiveDataProvider([ |
59
|
|
|
'db' => $this->_db, |
60
|
|
|
'query' => EmailTemplate::find()->with('translations'), |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param int $id |
66
|
|
|
* @return EmailTemplate |
67
|
|
|
* @throws NotFoundHttpException |
68
|
|
|
*/ |
69
|
|
|
private function findModel($id) |
70
|
|
|
{ |
71
|
|
|
if ($model = EmailTemplate::findOne($id)) { |
72
|
|
|
return $model; |
73
|
|
|
} |
74
|
|
|
throw new NotFoundHttpException(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns primary model object. |
79
|
|
|
* |
80
|
|
|
* @param null|int $id |
81
|
|
|
* @return EmailTemplate |
82
|
|
|
* @throws NotFoundHttpException |
83
|
|
|
*/ |
84
|
|
|
public function getModel($id = null) |
85
|
|
|
{ |
86
|
|
|
if ($id === null) { |
87
|
|
|
$model = new EmailTemplate(); |
88
|
|
|
$model->loadDefaultValues(); |
89
|
|
|
$this->_model = $model; |
90
|
|
|
} else { |
91
|
|
|
$this->_model = $this->findModel($id); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->_model; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Save banner to database. |
99
|
|
|
* |
100
|
|
|
* @param array $data |
101
|
|
|
* @throws \DomainException |
102
|
|
|
* @throws \RuntimeException |
103
|
|
|
*/ |
104
|
|
|
protected function saveInternal(array $data) |
105
|
|
|
{ |
106
|
|
|
if ($this->_model->getIsNewRecord() && !$this->_model->load($data)) { |
107
|
|
|
throw new \DomainException('Cannot load data to primary model'); |
108
|
|
|
} |
109
|
|
|
foreach ($data[EmailTemplateTranslation::internalFormName()] as $language => $dataSet) { |
110
|
|
|
$model = $this->_model->getTranslation($language); |
111
|
|
|
foreach ($dataSet as $attribute => $translation) { |
112
|
|
|
$model->$attribute = $translation; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!$this->_model->save()) { |
117
|
|
|
throw new \RuntimeException(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Save banner and log exceptions. |
123
|
|
|
* |
124
|
|
|
* @param array $data |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function save(array $data) |
128
|
|
|
{ |
129
|
|
|
try { |
130
|
|
|
$this->saveInternal($data); |
131
|
|
|
return true; |
132
|
|
|
} catch (\Exception $ex) { |
133
|
|
|
Yii::$app->getErrorHandler()->logException($ex); |
134
|
|
|
} |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Removes banner. |
140
|
|
|
* |
141
|
|
|
* @param int $id |
142
|
|
|
* @return bool |
143
|
|
|
* @throws NotFoundHttpException |
144
|
|
|
*/ |
145
|
|
|
public function delete($id) |
146
|
|
|
{ |
147
|
|
|
$model = $this->findModel($id); |
148
|
|
|
try { |
149
|
|
|
return (bool)$model->delete(); |
150
|
|
|
} catch (\Exception $ex) { |
151
|
|
|
Yii::$app->getErrorHandler()->logException($ex); |
152
|
|
|
} |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|