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
|
2 |
|
public function hasBeenReceived() |
35
|
|
|
{ |
36
|
2 |
|
return is_string($this->receivedAtAttribute) ? !$this->isInitDatetime($this->receivedAtAttribute) : false; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
public function hasBeenRead() |
40
|
|
|
{ |
41
|
2 |
|
return is_string($this->readAtAttribute) ? !$this->isInitDatetime($this->readAtAttribute) : false; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function touchReceived() |
45
|
|
|
{ |
46
|
1 |
|
return $this->setReceivedAt(static::currentDatetime()); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function touchRead() |
50
|
|
|
{ |
51
|
1 |
|
return $this->setReadAt(static::currentDatetime()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getReceivedAt() |
55
|
|
|
{ |
56
|
|
|
if (is_string($this->receivedAtAttribute)) { |
57
|
|
|
$raAttribute = $this->receivedAtAttribute; |
58
|
|
|
return $this->$raAttribute; |
59
|
|
|
} |
60
|
|
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
public function setReceivedAt($receivedAt) |
64
|
|
|
{ |
65
|
3 |
|
if (is_string($this->receivedAtAttribute)) { |
66
|
3 |
|
$raAttribute = $this->receivedAtAttribute; |
67
|
3 |
|
return $this->$raAttribute = $receivedAt; |
68
|
|
|
} |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getReadAt() |
73
|
|
|
{ |
74
|
|
|
if (is_string($this->readAtAttribute)) { |
75
|
|
|
$raAttribute = $this->readAtAttribute; |
76
|
|
|
return $this->$raAttribute; |
77
|
|
|
} |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
public function setReadAt($readAt) |
82
|
|
|
{ |
83
|
3 |
|
if (is_string($this->readAtAttribute)) { |
84
|
3 |
|
$raAttribute = $this->readAtAttribute; |
85
|
3 |
|
return $this->$raAttribute = $readAt; |
86
|
|
|
} |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param \yii\base\ModelEvent $event |
92
|
|
|
*/ |
93
|
3 |
|
public function onInitReceivedAtAttribute($event) |
94
|
|
|
{ |
95
|
3 |
|
$sender = $event->sender; |
96
|
|
|
/* @var $sender static */ |
97
|
3 |
|
$sender->setReceivedAt(static::getInitDatetime($event)); |
98
|
3 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param \yii\base\ModelEvent $event |
102
|
|
|
*/ |
103
|
3 |
|
public function onInitReadAtAttribute($event) |
104
|
|
|
{ |
105
|
3 |
|
$sender = $event->sender; |
106
|
|
|
/* @var $sender static */ |
107
|
3 |
|
$sender->setReadAt(static::getInitDatetime($event)); |
108
|
3 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* We consider you have received the message if you read it. |
112
|
|
|
* @param \yii\base\ModelEvent $event |
113
|
|
|
*/ |
114
|
2 |
|
public function onReadAtChanged($event) |
115
|
|
|
{ |
116
|
2 |
|
$sender = $event->sender; |
117
|
2 |
|
if (!is_string($sender->readAtAttribute)) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
2 |
|
$raAttribute = $sender->readAtAttribute; |
121
|
2 |
|
$reaAttribute = $sender->receivedAtAttribute; |
122
|
2 |
|
if ($sender->$raAttribute != $sender->initDatetime() && $sender->$reaAttribute == $sender->initDatetime()) { |
123
|
1 |
|
$sender->$reaAttribute = $sender->currentDatetime(); |
124
|
1 |
|
} |
125
|
2 |
|
$oldRa = $sender->getOldAttribute($raAttribute); |
126
|
2 |
|
if ($oldRa != $sender->initDatetime() && $sender->$raAttribute != $oldRa) { |
127
|
|
|
$sender->$raAttribute = $oldRa; |
128
|
|
|
} |
129
|
2 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* You are not allowed to change receive time if you have received it. |
133
|
|
|
* @param \yii\base\ModelEvent $event |
134
|
|
|
*/ |
135
|
2 |
|
public function onReceivedAtChanged($event) |
136
|
|
|
{ |
137
|
2 |
|
$sender = $event->sender; |
138
|
2 |
|
if ($sender->permitChangeReceivedAt) { |
139
|
|
|
return; |
140
|
|
|
} |
141
|
2 |
|
$raAttribute = $sender->receivedAtAttribute; |
142
|
2 |
|
$oldRa = $sender->getOldAttribute($raAttribute); |
143
|
2 |
|
if ($oldRa != $sender->initDatetime() && $sender->$raAttribute != $oldRa) { |
144
|
|
|
$sender->$raAttribute = $oldRa; |
145
|
|
|
} |
146
|
2 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* You are not allowed to change the content if it is not new message. |
150
|
|
|
* @param \yii\base\ModelEvent $event |
151
|
|
|
*/ |
152
|
2 |
|
public function onContentChanged($event) |
153
|
|
|
{ |
154
|
2 |
|
$sender = $event->sender; |
155
|
2 |
|
if ($sender->permitChangeContent) { |
156
|
|
|
return; |
157
|
|
|
} |
158
|
2 |
|
$cAttribute = $sender->contentAttribute; |
159
|
2 |
|
$oldContent = $sender->getOldAttribute($cAttribute); |
160
|
2 |
|
if ($oldContent != $sender->$cAttribute) { |
161
|
1 |
|
$sender->$cAttribute = $oldContent; |
162
|
1 |
|
} |
163
|
2 |
|
} |
164
|
|
|
|
165
|
3 |
|
public function initMessageEvents() |
166
|
|
|
{ |
167
|
3 |
|
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'onInitReceivedAtAttribute']); |
|
|
|
|
168
|
3 |
|
$this->on(static::EVENT_BEFORE_INSERT, [$this, 'onInitReadAtAttribute']); |
|
|
|
|
169
|
3 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onReadAtChanged']); |
|
|
|
|
170
|
3 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onReceivedAtChanged']); |
|
|
|
|
171
|
3 |
|
$this->on(static::EVENT_BEFORE_UPDATE, [$this, 'onContentChanged']); |
|
|
|
|
172
|
3 |
|
} |
173
|
|
|
|
174
|
3 |
|
public function getMessageRules() |
175
|
|
|
{ |
176
|
|
|
$rules = [ |
177
|
3 |
|
[$this->otherGuidAttribute, 'required'], |
178
|
3 |
|
[$this->otherGuidAttribute, 'string', 'max' => 36], |
179
|
3 |
|
]; |
180
|
3 |
|
if (is_string($this->attachmentAttribute)) { |
181
|
3 |
|
$rules[] = [$this->attachmentAttribute, 'safe']; |
182
|
3 |
|
} |
183
|
3 |
|
if (is_string($this->receivedAtAttribute)) { |
184
|
3 |
|
$rules[] = [$this->receivedAtAttribute, 'safe']; |
185
|
3 |
|
} |
186
|
3 |
|
if (is_string($this->readAtAttribute)) { |
187
|
3 |
|
$rules[] = [$this->readAtAttribute, 'safe']; |
188
|
3 |
|
} |
189
|
3 |
|
return $rules; |
190
|
|
|
} |
191
|
|
|
|
192
|
3 |
|
public function rules() |
193
|
|
|
{ |
194
|
3 |
|
return array_merge(parent::rules(), $this->getMessageRules()); |
195
|
|
|
} |
196
|
|
|
|
197
|
3 |
|
public function enabledFields() |
198
|
|
|
{ |
199
|
3 |
|
$fields = parent::enabledFields(); |
200
|
3 |
|
$fields[] = $this->otherGuidAttribute; |
201
|
3 |
|
if (is_string($this->attachmentAttribute)) { |
202
|
3 |
|
$fields[] = $this->attachmentAttribute; |
203
|
3 |
|
} |
204
|
3 |
|
if (is_string($this->receivedAtAttribute)) { |
205
|
3 |
|
$fields[] = $this->receivedAtAttribute; |
206
|
3 |
|
} |
207
|
3 |
|
if (is_string($this->readAtAttribute)) { |
208
|
3 |
|
$fields[] = $this->readAtAttribute; |
209
|
3 |
|
} |
210
|
3 |
|
return $fields; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
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.