1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\mailTemplate\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\behaviors\TimestampBehavior; |
7
|
|
|
use yii\db\ActiveRecord; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is the model class for table "mail_templates". |
11
|
|
|
* |
12
|
|
|
* @property integer $id |
13
|
|
|
* @property string $body |
14
|
|
|
* @property string $key |
15
|
|
|
* @property string $name |
16
|
|
|
* @property string $updated_at |
17
|
|
|
* @property string $subject |
18
|
|
|
*/ |
19
|
|
|
class MailTemplate extends ActiveRecord |
20
|
|
|
{ |
21
|
|
|
const REGISTER_CONFIRM = 'REGISTER_CONFIRM'; |
22
|
|
|
|
23
|
|
|
const RECOVERY_PASSWORD = 'RECOVERY_PASSWORD'; |
24
|
|
|
|
25
|
|
|
const CHANGE_PASSWORD = 'CHANGE_PASSWORD'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
10 |
View Code Duplication |
public function behaviors() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
'timestamp' => [ |
34
|
10 |
|
'class' => TimestampBehavior::className(), |
35
|
|
|
'attributes' => [ |
36
|
10 |
|
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'], |
37
|
|
|
], |
38
|
10 |
|
'value' => date('Y-m-d H:i:s'), |
39
|
10 |
|
], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
11 |
|
public static function tableName() |
47
|
|
|
{ |
48
|
11 |
|
return 'mail_templates'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
2 |
|
public function rules() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
2 |
|
[['body'], 'string'], |
58
|
|
|
[['name',], 'required'], |
59
|
|
|
[['updated_at'], 'safe'], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
4 |
View Code Duplication |
public function attributeLabels() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
4 |
|
'id' => Yii::t('mailTemplate', 'ID'), |
70
|
4 |
|
'key' => Yii::t('mailTemplate', 'Key'), |
71
|
4 |
|
'body' => Yii::t('mailTemplate', 'Body'), |
72
|
4 |
|
'name' => Yii::t('mailTemplate', 'Name'), |
73
|
4 |
|
'updated_at' => Yii::t('mailTemplate', 'Updated At'), |
74
|
4 |
|
'subject' => Yii::t('mailTemplate', 'Subject'), |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Find template by key |
80
|
|
|
* |
81
|
|
|
* @param $key |
82
|
|
|
* @return $this|null |
83
|
|
|
*/ |
84
|
6 |
|
public static function findByKey($key) |
85
|
|
|
{ |
86
|
6 |
|
return static::findOne(['key' => $key]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Replace placeholders in template to concrete data |
91
|
|
|
* |
92
|
|
|
* @param array $placeholders |
93
|
|
|
*/ |
94
|
6 |
|
public function replacePlaceholders(array $placeholders) |
95
|
|
|
{ |
96
|
6 |
|
foreach ($placeholders as $placeholderName => $value) { |
97
|
6 |
|
$this->body = str_replace("{{{$placeholderName}}}", $value, $this->body); |
98
|
|
|
} |
99
|
6 |
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.