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
|
|
|
* @version 2.0 |
20
|
|
|
* @author vistart <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
trait MessageTrait |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
public $otherGuidAttribute = 'other_guid'; |
26
|
|
|
public $attachmentAttribute = 'attachment'; |
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
|
|
|
public $permitChangeReadAt = false; |
34
|
|
|
|
35
|
4 |
|
public function hasBeenReceived() |
36
|
|
|
{ |
37
|
4 |
|
return is_string($this->receivedAtAttribute) ? !$this->isInitDatetime($this->getReceivedAt()) : false; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
public function hasBeenRead() |
41
|
|
|
{ |
42
|
4 |
|
return is_string($this->readAtAttribute) ? !$this->isInitDatetime($this->getReadAt()) : false; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function touchReceived() |
46
|
|
|
{ |
47
|
2 |
|
return $this->setReceivedAt(static::currentDatetime()); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function touchRead() |
51
|
|
|
{ |
52
|
2 |
|
return $this->setReadAt(static::currentDatetime()); |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
public function getReceivedAt() |
56
|
|
|
{ |
57
|
4 |
|
if (is_string($this->receivedAtAttribute)) { |
58
|
4 |
|
$raAttribute = $this->receivedAtAttribute; |
59
|
4 |
|
return $this->$raAttribute; |
60
|
|
|
} |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
public function setReceivedAt($receivedAt) |
65
|
|
|
{ |
66
|
8 |
|
if (is_string($this->receivedAtAttribute)) { |
67
|
8 |
|
$raAttribute = $this->receivedAtAttribute; |
68
|
8 |
|
return $this->$raAttribute = $receivedAt; |
69
|
|
|
} |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
public function getReadAt() |
74
|
|
|
{ |
75
|
4 |
|
if (is_string($this->readAtAttribute)) { |
76
|
4 |
|
$raAttribute = $this->readAtAttribute; |
77
|
4 |
|
return $this->$raAttribute; |
78
|
|
|
} |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
8 |
|
public function setReadAt($readAt) |
83
|
|
|
{ |
84
|
8 |
|
if (is_string($this->readAtAttribute)) { |
85
|
8 |
|
$raAttribute = $this->readAtAttribute; |
86
|
8 |
|
return $this->$raAttribute = $readAt; |
87
|
|
|
} |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \yii\base\ModelEvent $event |
93
|
|
|
*/ |
94
|
8 |
|
public function onInitReceivedAtAttribute($event) |
95
|
|
|
{ |
96
|
8 |
|
$sender = $event->sender; |
97
|
|
|
/* @var $sender static */ |
98
|
8 |
|
$sender->setReceivedAt(static::getInitDatetime($event)); |
99
|
8 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param \yii\base\ModelEvent $event |
103
|
|
|
*/ |
104
|
8 |
|
public function onInitReadAtAttribute($event) |
105
|
|
|
{ |
106
|
8 |
|
$sender = $event->sender; |
107
|
|
|
/* @var $sender static */ |
108
|
8 |
|
$sender->setReadAt(static::getInitDatetime($event)); |
109
|
8 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* We consider you have received the message if you read it. |
113
|
|
|
* @param \yii\base\ModelEvent $event |
114
|
|
|
*/ |
115
|
4 |
|
public function onReadAtChanged($event) |
116
|
|
|
{ |
117
|
4 |
|
$sender = $event->sender; |
118
|
4 |
|
$raAttribute = $sender->readAtAttribute; |
119
|
4 |
|
if (!is_string($raAttribute)) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
4 |
|
$reaAttribute = $sender->receivedAtAttribute; |
123
|
4 |
|
if (is_string($reaAttribute) && !$sender->isInitDatetime($sender->$raAttribute) && $sender->isInitDatetime($sender->$reaAttribute)) { |
124
|
2 |
|
$sender->$reaAttribute = $sender->currentDatetime(); |
125
|
2 |
|
} |
126
|
4 |
|
if ($sender->permitChangeReadAt) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
4 |
|
$oldRa = $sender->getOldAttribute($raAttribute); |
130
|
4 |
|
if ($oldRa != null && !$sender->isInitDatetime($oldRa) && $sender->$raAttribute != $oldRa) { |
131
|
|
|
$sender->$raAttribute = $oldRa; |
132
|
|
|
return; |
133
|
|
|
} |
134
|
4 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* You are not allowed to change receive time if you have received it. |
138
|
|
|
* @param \yii\base\ModelEvent $event |
139
|
|
|
*/ |
140
|
4 |
|
public function onReceivedAtChanged($event) |
141
|
|
|
{ |
142
|
4 |
|
$sender = $event->sender; |
143
|
4 |
|
$raAttribute = $sender->receivedAtAttribute; |
144
|
4 |
|
if (!is_string($raAttribute)) { |
145
|
|
|
return; |
146
|
|
|
} |
147
|
4 |
|
if ($sender->permitChangeReceivedAt) { |
148
|
|
|
return; |
149
|
|
|
} |
150
|
4 |
|
$oldRa = $sender->getOldAttribute($raAttribute); |
151
|
4 |
|
if ($oldRa != null && !$sender->isInitDatetime($oldRa) && $sender->$raAttribute != $oldRa) { |
152
|
|
|
$sender->$raAttribute = $oldRa; |
153
|
|
|
return; |
154
|
|
|
} |
155
|
4 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* You are not allowed to change the content if it is not new message. |
159
|
|
|
* @param \yii\base\ModelEvent $event |
160
|
|
|
*/ |
161
|
4 |
|
public function onContentChanged($event) |
162
|
|
|
{ |
163
|
4 |
|
$sender = $event->sender; |
164
|
4 |
|
if ($sender->permitChangeContent) { |
165
|
|
|
return; |
166
|
|
|
} |
167
|
4 |
|
$cAttribute = $sender->contentAttribute; |
168
|
4 |
|
$oldContent = $sender->getOldAttribute($cAttribute); |
169
|
4 |
|
if ($oldContent != $sender->$cAttribute) { |
170
|
2 |
|
$sender->$cAttribute = $oldContent; |
171
|
2 |
|
} |
172
|
4 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Trigger message received or read events. |
176
|
|
|
* @param \yii\db\AfterSaveEvent $event |
177
|
|
|
*/ |
178
|
4 |
|
public function onMessageUpdated($event) |
179
|
|
|
{ |
180
|
4 |
|
$sender = $event->sender; |
181
|
4 |
|
$reaAttribute = $sender->receivedAtAttribute; |
182
|
4 |
|
if (isset($event->changedAttributes[$reaAttribute]) && $event->changedAttributes[$reaAttribute] != $sender->$reaAttribute) { |
183
|
4 |
|
$sender->trigger(static::$eventMessageReceived); |
184
|
4 |
|
} |
185
|
4 |
|
$raAttribute = $sender->readAtAttribute; |
186
|
4 |
|
if (isset($event->changedAttributes[$raAttribute]) && $event->changedAttributes[$raAttribute] != $sender->$raAttribute) { |
187
|
2 |
|
$sender->trigger(static::$eventMessageRead); |
188
|
2 |
|
} |
189
|
4 |
|
} |
190
|
|
|
|
191
|
8 |
|
public function initMessageEvents() |
192
|
|
|
{ |
193
|
8 |
|
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'onInitReceivedAtAttribute']); |
|
|
|
|
194
|
8 |
|
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'onInitReadAtAttribute']); |
|
|
|
|
195
|
8 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onReceivedAtChanged']); |
|
|
|
|
196
|
8 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onReadAtChanged']); |
|
|
|
|
197
|
8 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onContentChanged']); |
|
|
|
|
198
|
8 |
|
$this->on(static::EVENT_AFTER_UPDATE, [$this, 'onMessageUpdated']); |
|
|
|
|
199
|
8 |
|
} |
200
|
|
|
|
201
|
8 |
|
public function getMessageRules() |
202
|
|
|
{ |
203
|
8 |
|
$rules = []; |
204
|
8 |
|
if (is_string($this->otherGuidAttribute)) { |
205
|
|
|
$rules = [ |
206
|
8 |
|
[$this->otherGuidAttribute, 'required'], |
207
|
8 |
|
[$this->otherGuidAttribute, 'string', 'max' => 36], |
208
|
8 |
|
]; |
209
|
8 |
|
} |
210
|
8 |
|
if (is_string($this->attachmentAttribute)) { |
211
|
8 |
|
$rules[] = [$this->attachmentAttribute, 'safe']; |
212
|
8 |
|
} |
213
|
8 |
|
if (is_string($this->receivedAtAttribute)) { |
214
|
8 |
|
$rules[] = [$this->receivedAtAttribute, 'safe']; |
215
|
8 |
|
} |
216
|
8 |
|
if (is_string($this->readAtAttribute)) { |
217
|
8 |
|
$rules[] = [$this->readAtAttribute, 'safe']; |
218
|
8 |
|
} |
219
|
8 |
|
return $rules; |
220
|
|
|
} |
221
|
|
|
|
222
|
8 |
|
public function rules() |
223
|
|
|
{ |
224
|
8 |
|
return array_merge(parent::rules(), $this->getMessageRules()); |
225
|
|
|
} |
226
|
|
|
|
227
|
8 |
|
public function enabledFields() |
228
|
|
|
{ |
229
|
8 |
|
$fields = parent::enabledFields(); |
230
|
8 |
|
if (is_string($this->otherGuidAttribute)) { |
231
|
8 |
|
$fields[] = $this->otherGuidAttribute; |
232
|
8 |
|
} |
233
|
8 |
|
if (is_string($this->attachmentAttribute)) { |
234
|
8 |
|
$fields[] = $this->attachmentAttribute; |
235
|
8 |
|
} |
236
|
8 |
|
if (is_string($this->receivedAtAttribute)) { |
237
|
8 |
|
$fields[] = $this->receivedAtAttribute; |
238
|
8 |
|
} |
239
|
8 |
|
if (is_string($this->readAtAttribute)) { |
240
|
8 |
|
$fields[] = $this->readAtAttribute; |
241
|
8 |
|
} |
242
|
8 |
|
return $fields; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.