|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
7
|
|
|
|
|
8
|
|
|
class MessageModel extends Model |
|
9
|
|
|
{ |
|
10
|
|
|
use SoftDeletes; |
|
11
|
|
|
protected $table='message'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @access public |
|
15
|
|
|
* @param array $config the config of the message. must including sender,receiver,title,content. //TODO:Can contain reply. |
|
16
|
|
|
* @return array the result. has 'result' ['ret' => 'success/faild', 'message' => null/'faild reason']. |
|
17
|
|
|
*/ |
|
18
|
|
|
public static function send($config) |
|
19
|
|
|
{ |
|
20
|
|
|
$message = new MessageModel; |
|
21
|
|
|
$message->sender = $config['sender']; |
|
22
|
|
|
$message->receiver = $config['receiver']; |
|
23
|
|
|
$message->title = $config['title']; |
|
24
|
|
|
$message->content = $config['content']; |
|
25
|
|
|
/* |
|
26
|
|
|
if(isset($config['reply'])){ |
|
27
|
|
|
$message->reply = $config['reply']; |
|
28
|
|
|
} |
|
29
|
|
|
if(isset($config['allow_reply'])){ |
|
30
|
|
|
$message->reply = $config['allow_reply']; |
|
31
|
|
|
} |
|
32
|
|
|
*/ |
|
33
|
|
|
$message->official = 1; |
|
34
|
|
|
$message->save(); |
|
35
|
|
|
return true; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* to get a unread message liist of a user |
|
40
|
|
|
* |
|
41
|
|
|
* @access public |
|
42
|
|
|
* @param integer message receiver id |
|
43
|
|
|
* @return array the result. |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function unread($uid) |
|
46
|
|
|
{ |
|
47
|
|
|
return static::where([ |
|
48
|
|
|
'receiver' => $uid, |
|
49
|
|
|
'unread' => 1, |
|
50
|
|
|
]) |
|
51
|
|
|
->orderByDesc('created_at') |
|
52
|
|
|
->get()->all(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* to check if a message allows replies |
|
57
|
|
|
* |
|
58
|
|
|
* @access public |
|
59
|
|
|
* @param integer id |
|
|
|
|
|
|
60
|
|
|
* @return array result. |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function allowReply($id) |
|
63
|
|
|
{ |
|
64
|
|
|
$message = static::where('id',$id)->first(); |
|
65
|
|
|
if(empty($message)){ |
|
66
|
|
|
return false; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
return $message['allow_reply'] ? true : false; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* to get a user's all messages' pagnition |
|
73
|
|
|
* |
|
74
|
|
|
* @access public |
|
75
|
|
|
* @param integer id |
|
76
|
|
|
* @return array result. |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function list($uid) |
|
79
|
|
|
{ |
|
80
|
|
|
return static::join('users','message.sender','=','users.id') |
|
|
|
|
|
|
81
|
|
|
->where('receiver',$uid) |
|
82
|
|
|
->select( |
|
83
|
|
|
'message.id as id', |
|
84
|
|
|
'users.name as sender_name', |
|
85
|
|
|
'users.avatar as sender_avatar', |
|
86
|
|
|
'message.sender as sender', |
|
87
|
|
|
'message.title as title', |
|
88
|
|
|
'message.created_at as time', |
|
89
|
|
|
'message.official as official', |
|
90
|
|
|
'message.unread as unread' |
|
91
|
|
|
) |
|
92
|
|
|
->orderByDesc('message.created_at') |
|
93
|
|
|
->orderByDesc('message.unread') |
|
94
|
|
|
->paginate(30); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* to get a message's detail. |
|
99
|
|
|
* |
|
100
|
|
|
* @access public |
|
101
|
|
|
* @param integer id |
|
102
|
|
|
* @return array result. |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function read($mid) |
|
105
|
|
|
{ |
|
106
|
|
|
$message = static::find($mid); |
|
107
|
|
|
if(!empty($message)){ |
|
108
|
|
|
$message->unread = 0; |
|
109
|
|
|
$message->save(); |
|
110
|
|
|
} |
|
111
|
|
|
return static::join('users','message.sender','=','users.id') |
|
|
|
|
|
|
112
|
|
|
->where('message.id',$mid) |
|
113
|
|
|
->select( |
|
114
|
|
|
'message.id as id', |
|
115
|
|
|
'users.name as sender_name', |
|
116
|
|
|
'users.avatar as sender_avatar', |
|
117
|
|
|
'message.sender as sender', |
|
118
|
|
|
'message.title as title', |
|
119
|
|
|
'message.content as content', |
|
120
|
|
|
'message.created_at as time', |
|
121
|
|
|
'message.official as official', |
|
122
|
|
|
'message.unread as unread', |
|
123
|
|
|
'message.allow_reply as allow_reply' |
|
124
|
|
|
)->first(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* to get a message's detail. |
|
129
|
|
|
* |
|
130
|
|
|
* @access public |
|
131
|
|
|
* @param integer uid |
|
|
|
|
|
|
132
|
|
|
* @return integer result. |
|
133
|
|
|
*/ |
|
134
|
|
|
public static function allRead($uid) |
|
135
|
|
|
{ |
|
136
|
|
|
return static::where('receiver',$uid) |
|
137
|
|
|
->update(['unread' => 0]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* to remove a user's all read-ed message. |
|
142
|
|
|
* |
|
143
|
|
|
* @access public |
|
144
|
|
|
* @param integer uid |
|
145
|
|
|
* @return integer result. |
|
146
|
|
|
*/ |
|
147
|
|
|
public static function removeAllRead($uid) |
|
148
|
|
|
{ |
|
149
|
|
|
return static::where([ |
|
150
|
|
|
'receiver' => $uid, |
|
151
|
|
|
'unread' => 0 |
|
152
|
|
|
])->delete(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* to soft delete the message |
|
157
|
|
|
* |
|
158
|
|
|
* @access public |
|
159
|
|
|
* @param integer|array the id of the message or the array with ids of the messages. |
|
|
|
|
|
|
160
|
|
|
* @return bool result. |
|
161
|
|
|
*/ |
|
162
|
|
|
public static function remove($messages) |
|
163
|
|
|
{ |
|
164
|
|
|
$del_count = 0; |
|
165
|
|
|
if(is_array($messages)){ |
|
166
|
|
|
foreach ($messages as $mid) { |
|
167
|
|
|
$message = static::find($mid); |
|
168
|
|
|
if(!empty($message)){ |
|
169
|
|
|
$message->delete(); |
|
170
|
|
|
$del_count ++; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
}else{ |
|
174
|
|
|
$message = static::find($messages); |
|
175
|
|
|
if(!empty($message)){ |
|
176
|
|
|
$message->delete(); |
|
177
|
|
|
$del_count ++; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
return $del_count; |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|