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\entities; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\db\ActiveRecord; |
12
|
|
|
use ymaker\email\templates\queries\EmailTemplateQuery; |
13
|
|
|
use ymaker\translatable\TranslatableBehavior; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This is the model class for table "{{%email_template}}". |
17
|
|
|
* |
18
|
|
|
* @property int $id |
19
|
|
|
* @property string $key |
20
|
|
|
* @property EmailTemplateTranslation[] $translations |
21
|
|
|
* |
22
|
|
|
* @method EmailTemplateTranslation getTranslation($language = null) |
23
|
|
|
* |
24
|
|
|
* @author Volodymyr Kupriienko <[email protected]> |
25
|
|
|
* @since 1.0 |
26
|
|
|
*/ |
27
|
|
|
class EmailTemplate extends ActiveRecord |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public static function tableName() |
33
|
|
|
{ |
34
|
|
|
return '{{%email_template}}'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
* |
40
|
|
|
* @return EmailTemplateQuery the newly created [[EmailTemplateQuery]] instance. |
41
|
|
|
*/ |
42
|
|
|
public static function find() |
43
|
|
|
{ |
44
|
|
|
return Yii::createObject(EmailTemplateQuery::class, [\get_called_class()]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Find model ID by key. |
49
|
|
|
* |
50
|
|
|
* @param string $key Model key. |
51
|
|
|
* |
52
|
|
|
* @return null|false|string |
53
|
|
|
*/ |
54
|
|
|
public static function findId($key) |
55
|
|
|
{ |
56
|
|
|
return static::find() |
57
|
|
|
->select('id') |
58
|
|
|
->byKey($key) |
59
|
|
|
->scalar(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function behaviors() |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
'translateable' => [ |
69
|
|
|
'class' => TranslatableBehavior::class, |
70
|
|
|
'translationAttributeList' => $this->getTranslationAttributes(), |
71
|
|
|
], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns list of translation attributes. |
77
|
|
|
* |
78
|
|
|
* @return string[] |
79
|
|
|
*/ |
80
|
|
|
public function getTranslationAttributes() |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
|
|
'language', |
84
|
|
|
'subject', |
85
|
|
|
'body', |
86
|
|
|
'hint', |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function rules() |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
[['key'], 'required'], |
97
|
|
|
[['key'], 'string', 'max' => 255], |
98
|
|
|
[['key'], 'unique'], |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function attributeLabels() |
106
|
|
|
{ |
107
|
|
|
return [ |
108
|
|
|
'id' => Yii::t('email-templates/entity', 'ID'), |
109
|
|
|
'key' => Yii::t('email-templates/entity', 'Key'), |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function transactions() |
117
|
|
|
{ |
118
|
|
|
return [ |
119
|
|
|
'default' => self::OP_ALL, |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return \yii\db\ActiveQuery |
125
|
|
|
*/ |
126
|
|
|
public function getTranslations() |
127
|
|
|
{ |
128
|
|
|
return $this->hasMany(EmailTemplateTranslation::class, ['templateId' => 'id']); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|