1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dominus77\maintenance\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\data\ArrayDataProvider; |
7
|
|
|
use yii\helpers\ArrayHelper; |
8
|
|
|
use dominus77\maintenance\interfaces\SubscribeFormInterface; |
9
|
|
|
use dominus77\maintenance\BackendMaintenance; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class SubscribeForm |
13
|
|
|
* @package dominus77\maintenance\models |
14
|
|
|
* |
15
|
|
|
* @property array $emails |
16
|
|
|
* @property string $path |
17
|
|
|
* @property array $followers |
18
|
|
|
* @property int $pageSize |
19
|
|
|
* @property \yii\data\ArrayDataProvider $dataProvider |
20
|
|
|
*/ |
21
|
|
|
class SubscribeForm extends BaseForm implements SubscribeFormInterface |
22
|
|
|
{ |
23
|
|
|
const SUBSCRIBE_SUCCESS = 'subscribeSuccess'; |
24
|
|
|
const SUBSCRIBE_INFO = 'subscribeInfo'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $email; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $subscribeOptions; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Path to file |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $_path; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
45
|
|
|
public function init() |
46
|
|
|
{ |
47
|
|
|
parent::init(); |
48
|
|
|
$this->_path = $this->state->getSubscribePath(); |
49
|
|
|
$urlManager = Yii::$app->urlManager; |
50
|
|
|
$subscribeOptions = [ |
51
|
|
|
'template' => $this->state->getSubscribeOptionsTemplate(), |
52
|
|
|
'backLink' => $urlManager->hostInfo, // Link in a letter to the site |
53
|
|
|
'from' => $this->getFrom('[email protected]'), |
54
|
|
|
'subject' => BackendMaintenance::t('app', 'Notification of completion of technical work') |
55
|
|
|
]; |
56
|
|
|
$this->subscribeOptions = ArrayHelper::merge($subscribeOptions, $this->state->getSubscribeOptions()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function rules() |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
['email', 'trim'], |
67
|
|
|
['email', 'required'], |
68
|
|
|
['email', 'email'], |
69
|
|
|
['email', 'string', 'max' => 255] |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function attributeLabels() |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
|
|
'email' => BackendMaintenance::t('app', 'Your email'), |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sending notifications to followers |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function send() |
90
|
|
|
{ |
91
|
|
|
$emails = $this->getEmails(); |
92
|
|
|
$messages = []; |
93
|
|
|
$mailer = Yii::$app->mailer; |
94
|
|
|
foreach ($emails as $email) { |
95
|
|
|
$messages[] = $mailer->compose( |
96
|
|
|
$this->subscribeOptions['template'], [ |
97
|
|
|
'backLink' => $this->subscribeOptions['backLink'] |
98
|
|
|
]) |
99
|
|
|
->setFrom([$this->subscribeOptions['from'] => Yii::$app->name]) |
100
|
|
|
->setTo($email) |
101
|
|
|
->setSubject($this->subscribeOptions['subject']); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
$result = $mailer->sendMultiple($messages); |
105
|
|
|
if ($result >= 0) { |
106
|
|
|
$this->deleteFile(); |
107
|
|
|
} |
108
|
|
|
return $result; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Save email in file |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function save() |
117
|
|
|
{ |
118
|
|
|
$str = $this->prepareSaveData(); |
119
|
|
|
$file = $this->getPath(); |
120
|
|
|
file_put_contents($file, $str, FILE_APPEND); |
121
|
|
|
chmod($file, 0765); |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Delete subscribers file |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function deleteFile() |
130
|
|
|
{ |
131
|
|
|
$file = $this->state->getSubscribePath(); |
132
|
|
|
$result = false; |
133
|
|
|
if (file_exists($file)) { |
134
|
|
|
$result = unlink($file); |
135
|
|
|
} |
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* This prepare data on before save |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function prepareSaveData() |
144
|
|
|
{ |
145
|
|
|
return $this->email . ' = ' . date($this->dateFormat) . PHP_EOL; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Save follower |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function subscribe() |
153
|
|
|
{ |
154
|
|
|
if ($this->isEmail()) { |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
return $this->save(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Emails subscribe |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function getEmails() |
165
|
|
|
{ |
166
|
|
|
$subscribeData = $this->getFollowers(); |
167
|
|
|
$emails = []; |
168
|
|
|
foreach ($subscribeData as $key => $values) { |
169
|
|
|
$emails[] = $values['email']; |
170
|
|
|
} |
171
|
|
|
return $emails; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Check email is subscribe |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
|
public function isEmail() |
179
|
|
|
{ |
180
|
|
|
return ArrayHelper::isIn($this->email, $this->getEmails()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return ArrayDataProvider |
185
|
|
|
*/ |
186
|
|
|
public function getDataProvider() |
187
|
|
|
{ |
188
|
|
|
return new ArrayDataProvider([ |
189
|
|
|
'allModels' => $this->getFollowers(), |
190
|
|
|
'pagination' => [ |
191
|
|
|
'pageSize' => $this->getPageSize() |
192
|
|
|
], |
193
|
|
|
]); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return int |
198
|
|
|
*/ |
199
|
|
|
public function getPageSize() |
200
|
|
|
{ |
201
|
|
|
return $this->state->getPageSize(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function getFollowers() |
208
|
|
|
{ |
209
|
|
|
$subscribeData = $this->prepareLoadModel($this->getPath()); |
210
|
|
|
$items = []; |
211
|
|
|
foreach ($subscribeData as $email => $date) { |
212
|
|
|
$items[$email]['email'] = $email; |
213
|
|
|
$items[$email]['date'] = $date; |
214
|
|
|
} |
215
|
|
|
return $items; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* From |
220
|
|
|
* @param $from string |
221
|
|
|
* @return mixed|string |
222
|
|
|
*/ |
223
|
|
|
public function getFrom($from) |
224
|
|
|
{ |
225
|
|
|
if (isset(Yii::$app->params['senderEmail']) && !empty(Yii::$app->params['senderEmail'])) { |
226
|
|
|
$from = Yii::$app->params['senderEmail']; |
227
|
|
|
} else if (isset(Yii::$app->params['supportEmail']) && !empty(Yii::$app->params['supportEmail'])) { |
228
|
|
|
$from = Yii::$app->params['supportEmail']; |
229
|
|
|
} |
230
|
|
|
return $from; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Path to file |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function getPath() |
238
|
|
|
{ |
239
|
|
|
return $this->_path; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|