|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\helpers\ArrayHelper; |
|
7
|
|
|
use Itstructure\AdminModule\interfaces\ModelInterface; |
|
8
|
|
|
use Itstructure\AdminModule\models\ActiveRecord; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This is the model class for table "feedback". |
|
12
|
|
|
* |
|
13
|
|
|
* @property int $id |
|
14
|
|
|
* @property string $name |
|
15
|
|
|
* @property string $email |
|
16
|
|
|
* @property string $phone |
|
17
|
|
|
* @property string $subject |
|
18
|
|
|
* @property string $message |
|
19
|
|
|
* @property int $read |
|
20
|
|
|
* |
|
21
|
|
|
* @package app\models |
|
22
|
|
|
*/ |
|
23
|
|
|
class Feedback extends ActiveRecord implements ModelInterface |
|
24
|
|
|
{ |
|
25
|
|
|
const SCENARIO_CONTACT = 'contact'; |
|
26
|
|
|
const SCENARIO_FEEDBACK = 'feedback'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
public $verifyCode; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function tableName() |
|
37
|
|
|
{ |
|
38
|
|
|
return 'feedback'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function rules() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
[ |
|
48
|
|
|
[ |
|
49
|
|
|
'name', |
|
50
|
|
|
'email', |
|
51
|
|
|
'subject', |
|
52
|
|
|
'message', |
|
53
|
|
|
], |
|
54
|
|
|
'required' |
|
55
|
|
|
], |
|
56
|
|
|
[ |
|
57
|
|
|
'message', |
|
58
|
|
|
'string', |
|
59
|
|
|
'max' => 2048 |
|
60
|
|
|
], |
|
61
|
|
|
[ |
|
62
|
|
|
[ |
|
63
|
|
|
'name', |
|
64
|
|
|
'email' |
|
65
|
|
|
], |
|
66
|
|
|
'string', |
|
67
|
|
|
'max' => 64 |
|
68
|
|
|
], |
|
69
|
|
|
[ |
|
70
|
|
|
'phone', |
|
71
|
|
|
'string', |
|
72
|
|
|
'max' => 32 |
|
73
|
|
|
], |
|
74
|
|
|
[ |
|
75
|
|
|
'subject', |
|
76
|
|
|
'string', |
|
77
|
|
|
'max' => 255 |
|
78
|
|
|
], |
|
79
|
|
|
[ |
|
80
|
|
|
'email', |
|
81
|
|
|
'email' |
|
82
|
|
|
], |
|
83
|
|
|
[ |
|
84
|
|
|
'read', |
|
85
|
|
|
'integer' |
|
86
|
|
|
], |
|
87
|
|
|
[ |
|
88
|
|
|
[ |
|
89
|
|
|
'created_at', |
|
90
|
|
|
'updated_at', |
|
91
|
|
|
], |
|
92
|
|
|
'safe', |
|
93
|
|
|
], |
|
94
|
|
|
[ |
|
95
|
|
|
'verifyCode', |
|
96
|
|
|
'captcha', |
|
97
|
|
|
'on' => [ |
|
98
|
|
|
self::SCENARIO_CONTACT |
|
99
|
|
|
], |
|
100
|
|
|
'captchaAction' => 'contact/captcha' |
|
101
|
|
|
], |
|
102
|
|
|
[ |
|
103
|
|
|
'verifyCode', |
|
104
|
|
|
'captcha', |
|
105
|
|
|
'on' => [ |
|
106
|
|
|
self::SCENARIO_FEEDBACK |
|
107
|
|
|
], |
|
108
|
|
|
'captchaAction' => 'feedback/captcha' |
|
109
|
|
|
] |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Scenarios. |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function scenarios() |
|
119
|
|
|
{ |
|
120
|
|
|
return [ |
|
|
|
|
|
|
121
|
|
|
self::SCENARIO_CONTACT => ArrayHelper::merge($this->attributes(), [ |
|
122
|
|
|
'verifyCode' |
|
123
|
|
|
]), |
|
124
|
|
|
self::SCENARIO_FEEDBACK => $this->attributes(), |
|
125
|
|
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
*/ |
|
131
|
|
|
public function attributeLabels() |
|
132
|
|
|
{ |
|
133
|
|
|
return [ |
|
134
|
|
|
'id' => 'ID', |
|
135
|
|
|
'name' => 'Name', |
|
136
|
|
|
'email' => 'Email', |
|
137
|
|
|
'phone' => 'Phone', |
|
138
|
|
|
'subject' => 'Subject', |
|
139
|
|
|
'message' => 'Message', |
|
140
|
|
|
'read' => 'Read', |
|
141
|
|
|
'verifyCode' => 'Verification Code', |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns id of the model. |
|
147
|
|
|
* |
|
148
|
|
|
* @return int |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getId() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->id; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Set read status to "1" after view record. |
|
157
|
|
|
* |
|
158
|
|
|
* @param int $id |
|
159
|
|
|
*/ |
|
160
|
|
|
public static function fixReadStatus(int $id): void |
|
161
|
|
|
{ |
|
162
|
|
|
static::updateAll([ |
|
163
|
|
|
'read' => 1 |
|
164
|
|
|
], [ |
|
165
|
|
|
'id' => $id |
|
166
|
|
|
]); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Sends an email to the specified email address using the information collected by this model. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $email the target email address |
|
173
|
|
|
* |
|
174
|
|
|
* @return bool whether the model passes validation |
|
175
|
|
|
*/ |
|
176
|
|
View Code Duplication |
public function contact($email) |
|
|
|
|
|
|
177
|
|
|
{ |
|
178
|
|
|
if ($this->save()) { |
|
179
|
|
|
Yii::$app->mailer->compose() |
|
180
|
|
|
->setTo($email) |
|
181
|
|
|
->setFrom([$this->email => $this->name]) |
|
182
|
|
|
->setSubject('New message from bizness-develop feedback. ' . $this->subject) |
|
183
|
|
|
->setTextBody($this->message) |
|
184
|
|
|
->send(); |
|
185
|
|
|
|
|
186
|
|
|
return true; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return false; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.