1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.name/ |
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
10
|
|
|
* @license https://vistart.name/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This trait should be used in models extended from models used BlameableTrait. |
17
|
|
|
* Notice: The models used BlameableTrait are also models used EntityTrait. |
18
|
|
|
* |
19
|
|
|
* @author vistart <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
trait MessageTrait |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
public $otherGuidAttribute = 'other_guid'; |
25
|
|
|
public $attachmentAttribute = 'attachment'; |
26
|
|
|
public $expiration = 604800; // 7 days |
27
|
|
|
public $receivedAtAttribute = 'received_at'; |
28
|
|
|
public $readAtAttribute = 'read_at'; |
29
|
|
|
public static $eventMessageReceived = 'messageReceived'; |
30
|
|
|
public static $eventMessageRead = 'messageRead'; |
31
|
|
|
public $permitChangeContent = false; |
32
|
|
|
public $permitChangeReceivedAt = false; |
33
|
|
|
|
34
|
4 |
|
public function hasBeenReceived() |
35
|
|
|
{ |
36
|
4 |
|
return is_string($this->receivedAtAttribute) ? !$this->isInitDatetime($this->receivedAt) : false; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
public function hasBeenRead() |
40
|
|
|
{ |
41
|
4 |
|
return is_string($this->readAtAttribute) ? !$this->isInitDatetime($this->readAt) : false; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
public function touchReceived() |
45
|
|
|
{ |
46
|
2 |
|
return $this->setReceivedAt(static::currentDatetime()); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
public function touchRead() |
50
|
|
|
{ |
51
|
2 |
|
return $this->setReadAt(static::currentDatetime()); |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
public function getReceivedAt() |
55
|
|
|
{ |
56
|
4 |
|
if (is_string($this->receivedAtAttribute)) { |
57
|
4 |
|
$raAttribute = $this->receivedAtAttribute; |
58
|
4 |
|
return $this->$raAttribute; |
59
|
|
|
} |
60
|
|
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
public function setReceivedAt($receivedAt) |
64
|
|
|
{ |
65
|
6 |
|
if (is_string($this->receivedAtAttribute)) { |
66
|
6 |
|
$raAttribute = $this->receivedAtAttribute; |
67
|
6 |
|
return $this->$raAttribute = $receivedAt; |
68
|
|
|
} |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
public function getReadAt() |
73
|
|
|
{ |
74
|
4 |
|
if (is_string($this->readAtAttribute)) { |
75
|
4 |
|
$raAttribute = $this->readAtAttribute; |
76
|
4 |
|
return $this->$raAttribute; |
77
|
|
|
} |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
public function setReadAt($readAt) |
82
|
|
|
{ |
83
|
6 |
|
if (is_string($this->readAtAttribute)) { |
84
|
6 |
|
$raAttribute = $this->readAtAttribute; |
85
|
6 |
|
return $this->$raAttribute = $readAt; |
86
|
|
|
} |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param \yii\base\ModelEvent $event |
92
|
|
|
*/ |
93
|
6 |
|
public function onInitReceivedAtAttribute($event) |
94
|
|
|
{ |
95
|
6 |
|
$sender = $event->sender; |
96
|
|
|
/* @var $sender static */ |
97
|
6 |
|
$sender->setReceivedAt(static::getInitDatetime($event)); |
98
|
6 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param \yii\base\ModelEvent $event |
102
|
|
|
*/ |
103
|
6 |
|
public function onInitReadAtAttribute($event) |
104
|
|
|
{ |
105
|
6 |
|
$sender = $event->sender; |
106
|
|
|
/* @var $sender static */ |
107
|
6 |
|
$sender->setReadAt(static::getInitDatetime($event)); |
108
|
6 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* We consider you have received the message if you read it. |
112
|
|
|
* @param \yii\base\ModelEvent $event |
113
|
|
|
*/ |
114
|
4 |
|
public function onReadAtChanged($event) |
115
|
|
|
{ |
116
|
4 |
|
$sender = $event->sender; |
117
|
4 |
|
if (!is_string($sender->readAtAttribute)) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
4 |
|
$raAttribute = $sender->readAtAttribute; |
121
|
4 |
|
$reaAttribute = $sender->receivedAtAttribute; |
122
|
4 |
|
if ($sender->$raAttribute != $sender->initDatetime() && $sender->$reaAttribute == $sender->initDatetime()) { |
123
|
2 |
|
$sender->$reaAttribute = $sender->currentDatetime(); |
124
|
2 |
|
} |
125
|
4 |
|
$oldRa = $sender->getOldAttribute($raAttribute); |
126
|
4 |
|
if ($oldRa != null && $oldRa != $sender->initDatetime() && $sender->$raAttribute != $oldRa) { |
127
|
|
|
$sender->$raAttribute = $oldRa; |
128
|
|
|
return; |
129
|
|
|
} |
130
|
4 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* You are not allowed to change receive time if you have received it. |
134
|
|
|
* @param \yii\base\ModelEvent $event |
135
|
|
|
*/ |
136
|
4 |
|
public function onReceivedAtChanged($event) |
137
|
|
|
{ |
138
|
4 |
|
$sender = $event->sender; |
139
|
4 |
|
if ($sender->permitChangeReceivedAt) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
4 |
|
$raAttribute = $sender->receivedAtAttribute; |
143
|
4 |
|
$oldRa = $sender->getOldAttribute($raAttribute); |
144
|
4 |
|
if ($oldRa != null && $oldRa != $sender->initDatetime() && $sender->$raAttribute != $oldRa) { |
145
|
|
|
$sender->$raAttribute = $oldRa; |
146
|
|
|
return; |
147
|
|
|
} |
148
|
4 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* You are not allowed to change the content if it is not new message. |
152
|
|
|
* @param \yii\base\ModelEvent $event |
153
|
|
|
*/ |
154
|
4 |
|
public function onContentChanged($event) |
155
|
|
|
{ |
156
|
4 |
|
$sender = $event->sender; |
157
|
4 |
|
if ($sender->permitChangeContent) { |
158
|
|
|
return; |
159
|
|
|
} |
160
|
4 |
|
$cAttribute = $sender->contentAttribute; |
161
|
4 |
|
$oldContent = $sender->getOldAttribute($cAttribute); |
162
|
4 |
|
if ($oldContent != $sender->$cAttribute) { |
163
|
2 |
|
$sender->$cAttribute = $oldContent; |
164
|
2 |
|
} |
165
|
4 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Trigger message received or read events. |
169
|
|
|
* @param \yii\db\AfterSaveEvent $event |
170
|
|
|
*/ |
171
|
4 |
|
public function onMessageUpdated($event) |
172
|
|
|
{ |
173
|
4 |
|
$sender = $event->sender; |
174
|
4 |
|
$reaAttribute = $sender->receivedAtAttribute; |
175
|
4 |
|
if (isset($event->changedAttributes[$reaAttribute]) && $event->changedAttributes[$reaAttribute] != $sender->$reaAttribute) { |
176
|
4 |
|
$sender->trigger(static::$eventMessageReceived); |
177
|
4 |
|
} |
178
|
4 |
|
$raAttribute = $sender->readAtAttribute; |
179
|
4 |
|
if (isset($event->changedAttributes[$raAttribute]) && $event->changedAttributes[$raAttribute] != $sender->$raAttribute) { |
180
|
2 |
|
$sender->trigger(static::$eventMessageRead); |
181
|
2 |
|
} |
182
|
4 |
|
} |
183
|
|
|
|
184
|
6 |
|
public function initMessageEvents() |
185
|
|
|
{ |
186
|
6 |
|
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'onInitReceivedAtAttribute']); |
|
|
|
|
187
|
6 |
|
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'onInitReadAtAttribute']); |
|
|
|
|
188
|
6 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onReceivedAtChanged']); |
|
|
|
|
189
|
6 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onReadAtChanged']); |
|
|
|
|
190
|
6 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onContentChanged']); |
|
|
|
|
191
|
6 |
|
$this->on(static::EVENT_AFTER_UPDATE, [$this, 'onMessageUpdated']); |
|
|
|
|
192
|
6 |
|
} |
193
|
|
|
|
194
|
6 |
|
public function getMessageRules() |
195
|
|
|
{ |
196
|
|
|
$rules = [ |
197
|
6 |
|
[$this->otherGuidAttribute, 'required'], |
198
|
6 |
|
[$this->otherGuidAttribute, 'string', 'max' => 36], |
199
|
6 |
|
]; |
200
|
6 |
|
if (is_string($this->attachmentAttribute)) { |
201
|
6 |
|
$rules[] = [$this->attachmentAttribute, 'safe']; |
202
|
6 |
|
} |
203
|
6 |
|
if (is_string($this->receivedAtAttribute)) { |
204
|
6 |
|
$rules[] = [$this->receivedAtAttribute, 'safe']; |
205
|
6 |
|
} |
206
|
6 |
|
if (is_string($this->readAtAttribute)) { |
207
|
6 |
|
$rules[] = [$this->readAtAttribute, 'safe']; |
208
|
6 |
|
} |
209
|
6 |
|
return $rules; |
210
|
|
|
} |
211
|
|
|
|
212
|
6 |
|
public function rules() |
213
|
|
|
{ |
214
|
6 |
|
return array_merge(parent::rules(), $this->getMessageRules()); |
215
|
|
|
} |
216
|
|
|
|
217
|
6 |
|
public function enabledFields() |
218
|
|
|
{ |
219
|
6 |
|
$fields = parent::enabledFields(); |
220
|
6 |
|
$fields[] = $this->otherGuidAttribute; |
221
|
6 |
|
if (is_string($this->attachmentAttribute)) { |
222
|
6 |
|
$fields[] = $this->attachmentAttribute; |
223
|
6 |
|
} |
224
|
6 |
|
if (is_string($this->receivedAtAttribute)) { |
225
|
6 |
|
$fields[] = $this->receivedAtAttribute; |
226
|
6 |
|
} |
227
|
6 |
|
if (is_string($this->readAtAttribute)) { |
228
|
6 |
|
$fields[] = $this->readAtAttribute; |
229
|
6 |
|
} |
230
|
6 |
|
return $fields; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.