|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use App\Event\Notifications; |
|
5
|
|
|
use Cake\Core\Configure; |
|
6
|
|
|
use Cake\Database\Expression\QueryExpression; |
|
7
|
|
|
use Cake\Event\Event; |
|
8
|
|
|
use Cake\I18n\Time; |
|
9
|
|
|
use Cake\Network\Exception\NotFoundException; |
|
10
|
|
|
use Cake\Routing\Router; |
|
11
|
|
|
|
|
12
|
|
|
class ConversationsController extends AppController |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Initialization hook method. |
|
17
|
|
|
* |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
public function initialize() |
|
21
|
|
|
{ |
|
22
|
|
|
parent::initialize(); |
|
23
|
|
|
|
|
24
|
|
|
$this->loadComponent('RequestHandler'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* BeforeFilter handle. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Event $event The beforeFilter event that was fired. |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public function beforeFilter(Event $event) |
|
35
|
|
|
{ |
|
36
|
|
|
parent::beforeFilter($event); |
|
37
|
|
|
|
|
38
|
|
|
$this->Auth->deny(); |
|
39
|
|
|
|
|
40
|
|
|
if (Configure::read('Conversations.enabled') === false && $this->request->action != 'maintenance') { |
|
41
|
|
|
$this->redirect(['action' => 'maintenance']); |
|
42
|
|
|
} elseif (Configure::read('Conversations.enabled') === true && $this->request->action == 'maintenance') { |
|
43
|
|
|
$this->redirect(['action' => 'index']); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Display all conversations for the user. |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public function index() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->ConversationsUsers = $this->loadModel('ConversationsUsers'); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
$this->paginate = [ |
|
57
|
|
|
'maxLimit' => Configure::read('Conversations.conversations_per_page') |
|
58
|
|
|
]; |
|
59
|
|
|
$conversations = $this->ConversationsUsers |
|
|
|
|
|
|
60
|
|
|
->find() |
|
61
|
|
|
->contain([ |
|
62
|
|
|
'Users', |
|
63
|
|
|
'Conversations', |
|
64
|
|
|
'Conversations.Users' => function ($q) { |
|
65
|
|
|
return $q->find('medium'); |
|
66
|
|
|
}, |
|
67
|
|
|
'Conversations.LastMessage', |
|
68
|
|
|
'Conversations.LastMessageUser' |
|
69
|
|
|
]) |
|
70
|
|
|
->where([ |
|
71
|
|
|
'ConversationsUsers.user_id' => $this->Auth->user('id'), |
|
72
|
|
|
'Conversations.conversation_open <>' => 2 |
|
73
|
|
|
]) |
|
74
|
|
|
->order([ |
|
75
|
|
|
'ConversationsUsers.is_read' => 'ASC', |
|
76
|
|
|
'ConversationsUsers.is_star' => 'DESC', |
|
77
|
|
|
'Conversations.last_message_date' => 'DESC', |
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
|
|
$conversations = $this->paginate($conversations); |
|
81
|
|
|
|
|
82
|
|
|
$this->set(compact('conversations')); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Function to do an action when a user select a/many conversations. |
|
87
|
|
|
* |
|
88
|
|
|
* Action list : |
|
89
|
|
|
* star : Conversations Important |
|
90
|
|
|
* normal : Make normal conversation |
|
91
|
|
|
* exit : Exit conversations |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
* |
|
95
|
|
|
* @throws NotFoundException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function action() |
|
98
|
|
|
{ |
|
99
|
|
|
if (!$this->request->is('ajax')) { |
|
100
|
|
|
throw new NotFoundException(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$actionAllowed = ['star', 'normal', 'exit']; |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
if (!array_key_exists('action', $this->request->getParsedBody()) || !in_array($this->request->getData('action'), $actionAllowed)) { |
|
|
|
|
|
|
106
|
|
|
$json = []; |
|
107
|
|
|
$json['error'] = '1'; |
|
108
|
|
|
$json['message'] = __d('conversations', 'Action unknown.'); |
|
109
|
|
|
|
|
110
|
|
|
$this->set(compact('json')); |
|
111
|
|
|
$this->set('_serialize', 'json'); |
|
112
|
|
|
|
|
113
|
|
|
return; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
View Code Duplication |
if (!array_key_exists('conversations', $this->request->getParsedBody())) { |
|
|
|
|
|
|
117
|
|
|
$json = []; |
|
118
|
|
|
$json['error'] = '1'; |
|
119
|
|
|
$json['message'] = __d('conversations', 'You have not chosen any conversations.'); |
|
120
|
|
|
|
|
121
|
|
|
$this->set(compact('json')); |
|
122
|
|
|
$this->set('_serialize', 'json'); |
|
123
|
|
|
|
|
124
|
|
|
return; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->loadModel('ConversationsUsers'); |
|
128
|
|
|
|
|
129
|
|
|
$action = $this->request->getData('action'); |
|
130
|
|
|
$array = $this->request->getData('conversations'); |
|
131
|
|
|
|
|
132
|
|
|
switch ($action) { |
|
133
|
|
View Code Duplication |
case "star": |
|
|
|
|
|
|
134
|
|
|
foreach ($array as $conversationId) { |
|
|
|
|
|
|
135
|
|
|
$this->ConversationsUsers->updateAll( |
|
|
|
|
|
|
136
|
|
|
['is_star' => 1], |
|
137
|
|
|
[ |
|
138
|
|
|
'conversation_id' => $conversationId, |
|
139
|
|
|
'user_id' => $this->Auth->user('id') |
|
140
|
|
|
] |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$json['message'] = __d('conversations', 'Your conversation(s) has been Stared.'); |
|
|
|
|
|
|
145
|
|
|
$json['error'] = '0'; |
|
146
|
|
|
$json['redirect'] = Router::url(['action' => 'index']); |
|
147
|
|
|
|
|
148
|
|
|
$this->set(compact('json')); |
|
149
|
|
|
|
|
150
|
|
|
break; |
|
151
|
|
|
|
|
152
|
|
View Code Duplication |
case "normal": |
|
|
|
|
|
|
153
|
|
|
foreach ($array as $conversationId) { |
|
|
|
|
|
|
154
|
|
|
$this->ConversationsUsers->updateAll( |
|
|
|
|
|
|
155
|
|
|
['is_star' => 0], |
|
156
|
|
|
[ |
|
157
|
|
|
'conversation_id' => $conversationId, |
|
158
|
|
|
'user_id' => $this->Auth->user('id') |
|
159
|
|
|
] |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$json['message'] = __d('conversations', 'Your conversation(s) has been set normal.'); |
|
|
|
|
|
|
164
|
|
|
$json['error'] = '0'; |
|
165
|
|
|
$json['redirect'] = Router::url(['action' => 'index']); |
|
166
|
|
|
|
|
167
|
|
|
$this->set(compact('json')); |
|
168
|
|
|
break; |
|
169
|
|
|
|
|
170
|
|
|
case "exit": |
|
171
|
|
|
foreach ($array as $conversationId) { |
|
|
|
|
|
|
172
|
|
|
$user = $this->ConversationsUsers |
|
|
|
|
|
|
173
|
|
|
->find() |
|
174
|
|
|
->contain([ |
|
175
|
|
|
'Conversations' |
|
176
|
|
|
]) |
|
177
|
|
|
->where([ |
|
178
|
|
|
'ConversationsUsers.user_id' => $this->Auth->user('id'), |
|
179
|
|
|
'ConversationsUsers.conversation_id' => $conversationId |
|
180
|
|
|
]) |
|
181
|
|
|
->first(); |
|
182
|
|
|
|
|
183
|
|
|
//Check if the user is the owner of the conversation. |
|
184
|
|
|
if ($user->conversation->user_id == $this->Auth->user('id')) { |
|
185
|
|
|
$conversation = $this->Conversations->get($conversationId); |
|
|
|
|
|
|
186
|
|
|
$conversation->conversation_open = 2; |
|
187
|
|
|
$this->Conversations->save($conversation); |
|
|
|
|
|
|
188
|
|
|
} else { |
|
189
|
|
|
$this->ConversationsUsers->delete($user); |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
$conversation = $this->Conversations->get($conversationId); |
|
|
|
|
|
|
192
|
|
|
$conversation->recipient_count = $user->conversation->recipient_count - 1; |
|
193
|
|
|
$this->Conversations->save($conversation); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$json['message'] = __d('conversations', 'You have left your conversation(s) successfully.'); |
|
|
|
|
|
|
198
|
|
|
$json['error'] = '0'; |
|
199
|
|
|
$json['redirect'] = Router::url(['action' => 'index']); |
|
200
|
|
|
|
|
201
|
|
|
$this->set(compact('json')); |
|
202
|
|
|
break; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$this->set('_serialize', 'json'); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Create a new conversation. |
|
210
|
|
|
* |
|
211
|
|
|
* @return void |
|
212
|
|
|
*/ |
|
213
|
|
|
public function create() |
|
214
|
|
|
{ |
|
215
|
|
|
$this->loadModel('Conversations'); |
|
216
|
|
|
$conversation = $this->Conversations->newEntity($this->request->getParsedBody(), ['validate' => 'create']); |
|
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
if ($this->request->is('post')) { |
|
219
|
|
|
$users = str_replace(",", "", trim(strtolower($this->request->getData('users')))); |
|
220
|
|
|
$users = explode(" ", $users); |
|
221
|
|
|
|
|
222
|
|
|
//Check max users. |
|
223
|
|
|
if (!(count($users) <= Configure::read('Conversations.max_users_per_conversation'))) { |
|
224
|
|
|
$this->Flash->error(__d('conversations', 'You cannot invite more than {0} user(s) in this conversation.', Configure::read('Conversations.max_users_per_conversation'))); |
|
225
|
|
|
$this->set(compact('conversation')); |
|
226
|
|
|
|
|
227
|
|
|
return; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$userMiniCount = false; |
|
231
|
|
|
$this->loadModel('Users'); |
|
232
|
|
|
|
|
233
|
|
|
//We check if at least one user in all list exist. |
|
234
|
|
|
foreach ($users as $user) { |
|
235
|
|
|
$userCheck = $this->Users |
|
|
|
|
|
|
236
|
|
|
->find() |
|
237
|
|
|
->where([ |
|
238
|
|
|
'LOWER(Users.username)' => $user |
|
239
|
|
|
]) |
|
240
|
|
|
->first(); |
|
241
|
|
|
|
|
242
|
|
|
//If an user exist and if the user is not the own of the conversation. |
|
243
|
|
|
if ($userCheck && $userCheck->id != $this->Auth->user('id')) { |
|
244
|
|
|
$userMiniCount = true; |
|
245
|
|
|
break; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
if ($userMiniCount === false) { |
|
250
|
|
|
$this->Flash->error(__d('conversations', 'Please enter at least one valid recipient.')); |
|
251
|
|
|
$this->set(compact('conversation')); |
|
252
|
|
|
|
|
253
|
|
|
return; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
$conversation->user_id = $this->Auth->user('id'); |
|
257
|
|
|
$conversation->reply_count = 1; |
|
258
|
|
|
$conversation->recipient_count = 1; |
|
259
|
|
|
$conversation->last_message_user_id = $this->Auth->user('id'); |
|
260
|
|
|
$conversation->last_message_date = new Time(); |
|
261
|
|
|
|
|
262
|
|
|
if ($conversation = $this->Conversations->save($conversation)) { |
|
|
|
|
|
|
263
|
|
|
$this->loadModel('ConversationsMessages'); |
|
264
|
|
|
$this->loadModel('ConversationsUsers'); |
|
265
|
|
|
|
|
266
|
|
|
$data = []; |
|
267
|
|
|
$data['message'] = $this->request->getData('message'); |
|
268
|
|
|
$data['conversation_id'] = $conversation->id; |
|
269
|
|
|
$data['user_id'] = $this->Auth->user('id'); |
|
270
|
|
|
|
|
271
|
|
|
$entity = $this->ConversationsMessages->newEntity($data); |
|
|
|
|
|
|
272
|
|
|
$message = $this->ConversationsMessages->save($entity); |
|
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
$data = []; |
|
275
|
|
|
$data['conversation_id'] = $conversation->id; |
|
276
|
|
|
$data['user_id'] = $this->Auth->user('id'); |
|
277
|
|
|
|
|
278
|
|
|
$entity = $this->ConversationsUsers->newEntity($data); |
|
|
|
|
|
|
279
|
|
|
$user = $this->ConversationsUsers->save($entity); |
|
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
$this->Conversations->updateAll( |
|
|
|
|
|
|
282
|
|
|
[ |
|
283
|
|
|
'first_message_id' => $message->id, |
|
284
|
|
|
'last_message_id' => $message->id |
|
285
|
|
|
], |
|
286
|
|
|
[ |
|
287
|
|
|
'id' => $conversation->id |
|
288
|
|
|
] |
|
289
|
|
|
); |
|
290
|
|
|
|
|
291
|
|
|
//Save all invited users. |
|
292
|
|
|
foreach ($users as $user) { |
|
293
|
|
|
$userExist = $this->Users |
|
|
|
|
|
|
294
|
|
|
->find() |
|
295
|
|
|
->where([ |
|
296
|
|
|
'LOWER(Users.username)' => $user |
|
297
|
|
|
]) |
|
298
|
|
|
->first(); |
|
299
|
|
|
|
|
300
|
|
|
//If the user exist. |
|
301
|
|
|
if (is_null($userExist)) { |
|
302
|
|
|
break; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
//Check if the user is not already in the conversation. |
|
306
|
|
|
$conversUserCheck = $this->ConversationsUsers |
|
|
|
|
|
|
307
|
|
|
->find() |
|
308
|
|
|
->where([ |
|
309
|
|
|
'ConversationsUsers.conversation_id' => $conversation->id, |
|
310
|
|
|
'ConversationsUsers.user_id' => $userExist->id |
|
311
|
|
|
]) |
|
312
|
|
|
->first(); |
|
313
|
|
|
|
|
314
|
|
|
if (!is_null($conversUserCheck)) { |
|
315
|
|
|
break; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
$data = []; |
|
319
|
|
|
$data['conversation_id'] = $conversation->id; |
|
320
|
|
|
$data['user_id'] = $userExist->id; |
|
321
|
|
|
|
|
322
|
|
|
$entity = $this->ConversationsUsers->newEntity($data); |
|
|
|
|
|
|
323
|
|
|
$user = $this->ConversationsUsers->save($entity); |
|
|
|
|
|
|
324
|
|
|
|
|
325
|
|
|
$expression = new QueryExpression('recipient_count = recipient_count + 1'); |
|
326
|
|
|
$this->Conversations->updateAll( |
|
|
|
|
|
|
327
|
|
|
[$expression], |
|
328
|
|
|
[ |
|
329
|
|
|
'id' => $conversation->id |
|
330
|
|
|
] |
|
331
|
|
|
); |
|
332
|
|
|
|
|
333
|
|
|
//Notifications Event. |
|
334
|
|
|
$this->eventManager()->attach(new Notifications()); |
|
|
|
|
|
|
335
|
|
|
$event = new Event('Model.Notifications.dispatch', $this, [ |
|
336
|
|
|
'sender_id' => $this->Auth->user('id'), |
|
337
|
|
|
'conversation_id' => $conversation->id, |
|
338
|
|
|
'type' => 'conversation.reply' |
|
339
|
|
|
]); |
|
340
|
|
|
$this->eventManager()->dispatch($event); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
$this->Flash->success(__d('conversations', 'Your conversation has been created successfully !')); |
|
344
|
|
|
$this->redirect([ |
|
345
|
|
|
'_name' => 'conversations-view', |
|
346
|
|
|
'slug' => $conversation->title, |
|
347
|
|
|
'id' => $conversation->id |
|
348
|
|
|
]); |
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
$this->set(compact('conversation')); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* Display a conversation. |
|
357
|
|
|
* |
|
358
|
|
|
* @return void|\Cake\Network\Response |
|
359
|
|
|
*/ |
|
360
|
|
|
public function view() |
|
361
|
|
|
{ |
|
362
|
|
|
$this->loadModel('ConversationsUsers'); |
|
363
|
|
|
|
|
364
|
|
|
$conversation = $this->ConversationsUsers |
|
|
|
|
|
|
365
|
|
|
->find() |
|
366
|
|
|
->contain([ |
|
367
|
|
|
'Users', |
|
368
|
|
|
'Conversations', |
|
369
|
|
|
'Conversations.LastMessage', |
|
370
|
|
|
'Conversations.LastMessageUser' |
|
371
|
|
|
]) |
|
372
|
|
|
->where([ |
|
373
|
|
|
'ConversationsUsers.conversation_id' => $this->request->id, |
|
374
|
|
|
'ConversationsUsers.user_id' => $this->Auth->user('id'), |
|
375
|
|
|
'Conversations.conversation_open <>' => 2 |
|
376
|
|
|
]) |
|
377
|
|
|
->first(); |
|
378
|
|
|
|
|
379
|
|
|
if (is_null($conversation)) { |
|
380
|
|
|
$this->Flash->error(__d('conversations', "This conversation doesn't exist or has been deleted.")); |
|
381
|
|
|
|
|
382
|
|
|
return $this->redirect(['action' => 'index']); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
$this->loadModel('ConversationsMessages'); |
|
386
|
|
|
|
|
387
|
|
|
$this->paginate = [ |
|
388
|
|
|
'maxLimit' => Configure::read('Conversations.messages_per_page') |
|
389
|
|
|
]; |
|
390
|
|
|
|
|
391
|
|
|
$messages = $this->ConversationsMessages |
|
|
|
|
|
|
392
|
|
|
->find() |
|
393
|
|
|
->contain([ |
|
394
|
|
|
'Users' => function ($q) { |
|
395
|
|
|
return $q->find('full')->formatResults(function ($users) { |
|
396
|
|
|
return $users->map(function ($user) { |
|
397
|
|
|
$user->online = $this->SessionsActivity->getOnlineStatus($user); |
|
|
|
|
|
|
398
|
|
|
|
|
399
|
|
|
return $user; |
|
400
|
|
|
}); |
|
401
|
|
|
}); |
|
402
|
|
|
}, |
|
403
|
|
|
'Users.Groups', |
|
404
|
|
|
'LastEditUsers' => function ($q) { |
|
405
|
|
|
return $q->find('short'); |
|
406
|
|
|
}, |
|
407
|
|
|
]) |
|
408
|
|
|
->where([ |
|
409
|
|
|
'ConversationsMessages.conversation_id' => $this->request->id |
|
410
|
|
|
]) |
|
411
|
|
|
->order([ |
|
412
|
|
|
'ConversationsMessages.created' => 'ASC' |
|
413
|
|
|
]); |
|
414
|
|
|
|
|
415
|
|
|
$messages = $this->paginate($messages); |
|
416
|
|
|
|
|
417
|
|
|
//Update "is_read" for the current user. |
|
418
|
|
|
$user = $this->ConversationsUsers->get($conversation->id); |
|
|
|
|
|
|
419
|
|
|
$user->is_read = 1; |
|
420
|
|
|
$this->ConversationsUsers->save($user); |
|
|
|
|
|
|
421
|
|
|
|
|
422
|
|
|
//Current user. |
|
423
|
|
|
$this->loadModel('Users'); |
|
424
|
|
|
$currentUser = $this->Users |
|
|
|
|
|
|
425
|
|
|
->find() |
|
426
|
|
|
->contain([ |
|
427
|
|
|
'Groups' => function ($q) { |
|
428
|
|
|
return $q->select(['id', 'is_staff']); |
|
429
|
|
|
} |
|
430
|
|
|
]) |
|
431
|
|
|
->where([ |
|
432
|
|
|
'Users.id' => $this->Auth->user('id') |
|
433
|
|
|
]) |
|
434
|
|
|
->select(['id', 'group_id']) |
|
435
|
|
|
->first(); |
|
436
|
|
|
|
|
437
|
|
|
//Build the newEntity for the comment form. |
|
438
|
|
|
$messageForm = $this->ConversationsMessages->newEntity(); |
|
|
|
|
|
|
439
|
|
|
|
|
440
|
|
|
$this->set(compact('conversation', 'messages', 'currentUser', 'messageForm')); |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
/** |
|
444
|
|
|
* Action to search some users when adding an user in a conversation. |
|
445
|
|
|
* |
|
446
|
|
|
* @return void |
|
447
|
|
|
* |
|
448
|
|
|
* @throws \Cake\Network\Exception\NotFoundException |
|
449
|
|
|
*/ |
|
450
|
|
|
public function inviteMember() |
|
451
|
|
|
{ |
|
452
|
|
|
if (!$this->request->is('ajax')) { |
|
453
|
|
|
throw new NotFoundException(); |
|
454
|
|
|
} |
|
455
|
|
|
$keyword = strtolower($this->request->getQuery('query')); |
|
456
|
|
|
|
|
457
|
|
|
$this->loadModel('Users'); |
|
458
|
|
|
$users = $this->Users |
|
|
|
|
|
|
459
|
|
|
->find() |
|
460
|
|
|
->where(function ($q) use ($keyword) { |
|
461
|
|
|
return $q |
|
462
|
|
|
->like('LOWER(Users.username)', "%$keyword%"); |
|
463
|
|
|
}) |
|
464
|
|
|
->limit(12) |
|
465
|
|
|
->toArray(); |
|
466
|
|
|
|
|
467
|
|
|
foreach ($users as $user) { |
|
468
|
|
|
$json[] = h($user->username); |
|
|
|
|
|
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
$this->set(compact('json')); |
|
472
|
|
|
$this->set('_serialize', 'json'); |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
/** |
|
476
|
|
|
* Quote a post. |
|
477
|
|
|
* |
|
478
|
|
|
* @throws \Cake\Network\Exception\NotFoundException |
|
479
|
|
|
* |
|
480
|
|
|
* @return mixed |
|
481
|
|
|
*/ |
|
482
|
|
|
public function quote() |
|
483
|
|
|
{ |
|
484
|
|
|
if (!$this->request->is('ajax')) { |
|
485
|
|
|
throw new NotFoundException(); |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
$this->loadModel('ConversationsMessages'); |
|
489
|
|
|
|
|
490
|
|
|
$message = $this->ConversationsMessages |
|
|
|
|
|
|
491
|
|
|
->find() |
|
492
|
|
|
->where([ |
|
493
|
|
|
'ConversationsMessages.id' => $this->request->id |
|
494
|
|
|
]) |
|
495
|
|
|
->contain([ |
|
496
|
|
|
'Users' => function ($q) { |
|
497
|
|
|
return $q->find('short'); |
|
498
|
|
|
} |
|
499
|
|
|
]) |
|
500
|
|
|
->first(); |
|
501
|
|
|
|
|
502
|
|
|
$json = []; |
|
503
|
|
|
|
|
504
|
|
|
if (!is_null($message)) { |
|
505
|
|
|
$message->toArray(); |
|
506
|
|
|
|
|
507
|
|
|
$url = Router::url(['action' => 'go', $message->id]); |
|
508
|
|
|
$text = __d('conversations', 'has said :'); |
|
509
|
|
|
|
|
510
|
|
|
//Build the quote. |
|
511
|
|
|
$json['message'] = <<<EOT |
|
512
|
|
|
<div> |
|
513
|
|
|
<div> |
|
514
|
|
|
<a href="{$url}"> |
|
515
|
|
|
<strong>{$message->user->full_name} {$text}</strong> |
|
516
|
|
|
</a> |
|
517
|
|
|
</div> |
|
518
|
|
|
<blockquote> |
|
519
|
|
|
$message->message |
|
520
|
|
|
</blockquote> |
|
521
|
|
|
</div><p> </p><p> </p> |
|
522
|
|
|
EOT; |
|
523
|
|
|
|
|
524
|
|
|
$json['error'] = false; |
|
525
|
|
|
|
|
526
|
|
|
$this->set(compact('json')); |
|
527
|
|
|
} else { |
|
528
|
|
|
$json['post'] = __d('conversations', "This message doesn't exist."); |
|
529
|
|
|
$json['error'] = true; |
|
530
|
|
|
|
|
531
|
|
|
$this->set(compact('json')); |
|
532
|
|
|
} |
|
533
|
|
|
|
|
534
|
|
|
//Send response in JSON. |
|
535
|
|
|
$this->set('_serialize', 'json'); |
|
536
|
|
|
} |
|
537
|
|
|
|
|
538
|
|
|
/** |
|
539
|
|
|
* Get the form to edit a message. |
|
540
|
|
|
* |
|
541
|
|
|
* @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request. |
|
542
|
|
|
* |
|
543
|
|
|
* @return void |
|
544
|
|
|
*/ |
|
545
|
|
|
public function getEditMessage() |
|
546
|
|
|
{ |
|
547
|
|
|
if (!$this->request->is('ajax')) { |
|
548
|
|
|
throw new NotFoundException(); |
|
549
|
|
|
} |
|
550
|
|
|
|
|
551
|
|
|
$this->loadModel('ConversationsMessages'); |
|
552
|
|
|
$this->viewBuilder()->layout(false); |
|
|
|
|
|
|
553
|
|
|
|
|
554
|
|
|
$message = $this->ConversationsMessages |
|
|
|
|
|
|
555
|
|
|
->find() |
|
556
|
|
|
->where([ |
|
557
|
|
|
'ConversationsMessages.id' => $this->request->getData('id') |
|
558
|
|
|
]) |
|
559
|
|
|
->first(); |
|
560
|
|
|
|
|
561
|
|
|
$json = [ |
|
562
|
|
|
'error' => false, |
|
563
|
|
|
'errorMessage' => '' |
|
564
|
|
|
]; |
|
565
|
|
|
|
|
566
|
|
View Code Duplication |
if (is_null($message)) { |
|
|
|
|
|
|
567
|
|
|
$json['error'] = true; |
|
568
|
|
|
$json['errorMessage'] = __d('conversations', "This message doesn't exist or has been deleted !"); |
|
569
|
|
|
|
|
570
|
|
|
$this->set(compact('json')); |
|
571
|
|
|
|
|
572
|
|
|
return; |
|
573
|
|
|
} |
|
574
|
|
|
|
|
575
|
|
|
//Current user. |
|
576
|
|
|
$this->loadModel('Users'); |
|
577
|
|
|
$currentUser = $this->Users |
|
|
|
|
|
|
578
|
|
|
->find() |
|
579
|
|
|
->contain([ |
|
580
|
|
|
'Groups' => function ($q) { |
|
581
|
|
|
return $q->select(['id', 'is_staff']); |
|
582
|
|
|
} |
|
583
|
|
|
]) |
|
584
|
|
|
->where([ |
|
585
|
|
|
'Users.id' => $this->Auth->user('id') |
|
586
|
|
|
]) |
|
587
|
|
|
->select(['id', 'group_id']) |
|
588
|
|
|
->first(); |
|
589
|
|
|
|
|
590
|
|
|
if ($message->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) { |
|
591
|
|
|
$json['error'] = true; |
|
592
|
|
|
$json['errorMessage'] = __d('conversations', "You don't have the authorization to edit this message !"); |
|
593
|
|
|
|
|
594
|
|
|
$this->set(compact('json')); |
|
595
|
|
|
|
|
596
|
|
|
return; |
|
597
|
|
|
} |
|
598
|
|
|
|
|
599
|
|
|
$this->set(compact('json', 'message')); |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
|
|
/** |
|
603
|
|
|
* Edit a message. |
|
604
|
|
|
* |
|
605
|
|
|
* @param int $id Id of the message. |
|
606
|
|
|
* |
|
607
|
|
|
* @return \Cake\Network\Response |
|
608
|
|
|
*/ |
|
609
|
|
|
public function messageEdit($id = null) |
|
610
|
|
|
{ |
|
611
|
|
|
if (!$this->request->is(['post', 'put'])) { |
|
612
|
|
|
throw new NotFoundException(); |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
$this->loadModel('ConversationsMessages'); |
|
616
|
|
|
|
|
617
|
|
|
$message = $this->ConversationsMessages |
|
|
|
|
|
|
618
|
|
|
->find() |
|
619
|
|
|
->where([ |
|
620
|
|
|
'ConversationsMessages.id' => $id |
|
621
|
|
|
]) |
|
622
|
|
|
->first(); |
|
623
|
|
|
|
|
624
|
|
|
if (is_null($message)) { |
|
625
|
|
|
$this->Flash->error(__d('conversations', "This post doesn't exist or has been deleted !")); |
|
626
|
|
|
|
|
627
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
|
|
628
|
|
|
} |
|
629
|
|
|
|
|
630
|
|
|
//Current user. |
|
631
|
|
|
$this->loadModel('Users'); |
|
632
|
|
|
$currentUser = $this->Users |
|
|
|
|
|
|
633
|
|
|
->find() |
|
634
|
|
|
->contain([ |
|
635
|
|
|
'Groups' => function ($q) { |
|
636
|
|
|
return $q->select(['id', 'is_staff']); |
|
637
|
|
|
} |
|
638
|
|
|
]) |
|
639
|
|
|
->where([ |
|
640
|
|
|
'Users.id' => $this->Auth->user('id') |
|
641
|
|
|
]) |
|
642
|
|
|
->select(['id', 'group_id']) |
|
643
|
|
|
->first(); |
|
644
|
|
|
|
|
645
|
|
|
if ($message->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) { |
|
646
|
|
|
$this->Flash->error(__d('conversations', "You don't have the authorization to edit this post !")); |
|
647
|
|
|
|
|
648
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
|
|
649
|
|
|
} |
|
650
|
|
|
|
|
651
|
|
|
$this->ConversationsMessages->patchEntity($message, $this->request->getParsedBody()); |
|
|
|
|
|
|
652
|
|
|
$message->last_edit_date = new Time(); |
|
653
|
|
|
$message->last_edit_user_id = $this->Auth->user('id'); |
|
654
|
|
|
$message->edit_count++; |
|
655
|
|
|
|
|
656
|
|
|
if ($this->ConversationsMessages->save($message)) { |
|
|
|
|
|
|
657
|
|
|
$this->Flash->success(__d('conversations', 'This message has been edited successfully !')); |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
return $this->redirect(['action' => 'go', $message->id]); |
|
661
|
|
|
} |
|
662
|
|
|
|
|
663
|
|
|
/** |
|
664
|
|
|
* Redirect an user to a conversation, page and message. |
|
665
|
|
|
* |
|
666
|
|
|
* @param int $messageId Id of the message. |
|
667
|
|
|
* |
|
668
|
|
|
* @return \Cake\Network\Response |
|
669
|
|
|
*/ |
|
670
|
|
View Code Duplication |
public function go($messageId = null) |
|
|
|
|
|
|
671
|
|
|
{ |
|
672
|
|
|
$this->loadModel('ConversationsMessages'); |
|
673
|
|
|
|
|
674
|
|
|
$message = $this->ConversationsMessages |
|
|
|
|
|
|
675
|
|
|
->find() |
|
676
|
|
|
->contain([ |
|
677
|
|
|
'Conversations' |
|
678
|
|
|
]) |
|
679
|
|
|
->where([ |
|
680
|
|
|
'ConversationsMessages.id' => $messageId |
|
681
|
|
|
]) |
|
682
|
|
|
->first(); |
|
683
|
|
|
|
|
684
|
|
|
if (is_null($message)) { |
|
685
|
|
|
$this->Flash->error(__d('conversations', "This message doesn't exist or has been deleted.")); |
|
686
|
|
|
|
|
687
|
|
|
return $this->redirect(['controller' => 'conversations', 'action' => 'index']); |
|
688
|
|
|
} |
|
689
|
|
|
|
|
690
|
|
|
$message->toArray(); |
|
691
|
|
|
|
|
692
|
|
|
//Count the number of messages before this message. |
|
693
|
|
|
$messagesBefore = $this->ConversationsMessages |
|
|
|
|
|
|
694
|
|
|
->find() |
|
695
|
|
|
->where([ |
|
696
|
|
|
'ConversationsMessages.conversation_id' => $message->conversation_id, |
|
697
|
|
|
'ConversationsMessages.created <' => $message->created |
|
698
|
|
|
]) |
|
699
|
|
|
->count(); |
|
700
|
|
|
|
|
701
|
|
|
//Get the number of messages per page. |
|
702
|
|
|
$messagesPerPage = Configure::read('Conversations.messages_per_page'); |
|
703
|
|
|
|
|
704
|
|
|
//Calculate the page. |
|
705
|
|
|
$page = ceil($messagesBefore / $messagesPerPage); |
|
706
|
|
|
|
|
707
|
|
|
$page = ($page > 1) ? $page : 1; |
|
708
|
|
|
|
|
709
|
|
|
//Redirect the user. |
|
710
|
|
|
return $this->redirect([ |
|
711
|
|
|
'_name' => 'conversations-view', |
|
712
|
|
|
'slug' => $message->conversation->title, |
|
713
|
|
|
'id' => $message->conversation->id, |
|
714
|
|
|
'?' => ['page' => $page], |
|
715
|
|
|
'#' => 'message-' . $messageId |
|
716
|
|
|
]); |
|
717
|
|
|
} |
|
718
|
|
|
|
|
719
|
|
|
/** |
|
720
|
|
|
* Function to kick an user from a conversation. |
|
721
|
|
|
* |
|
722
|
|
|
* @return void |
|
723
|
|
|
*/ |
|
724
|
|
|
public function kick() |
|
725
|
|
|
{ |
|
726
|
|
|
if (!$this->request->is('ajax')) { |
|
727
|
|
|
throw new NotFoundException(); |
|
728
|
|
|
} |
|
729
|
|
|
|
|
730
|
|
|
$this->loadModel('ConversationsUsers'); |
|
731
|
|
|
|
|
732
|
|
|
$currentUser = $this->ConversationsUsers |
|
|
|
|
|
|
733
|
|
|
->find() |
|
734
|
|
|
->contain([ |
|
735
|
|
|
'Conversations', |
|
736
|
|
|
'Users' => function ($q) { |
|
737
|
|
|
return $q->find('short'); |
|
738
|
|
|
}, |
|
739
|
|
|
'Users.Groups' => function ($q) { |
|
740
|
|
|
return $q->select(['id', 'is_staff']); |
|
741
|
|
|
} |
|
742
|
|
|
]) |
|
743
|
|
|
->where([ |
|
744
|
|
|
'ConversationsUsers.user_id' => $this->Auth->user('id'), |
|
745
|
|
|
'ConversationsUsers.conversation_id' => $this->request->id |
|
746
|
|
|
]) |
|
747
|
|
|
->first(); |
|
748
|
|
|
|
|
749
|
|
|
//Check if the current user is the owner of this conversation or if he is not a staff member. |
|
750
|
|
|
if ($currentUser->user_id != $currentUser->conversation->user_id && !$currentUser->user->group->is_staff) { |
|
751
|
|
|
$json['message'] = __d('conversations', 'You cannot kick this user from this conversation.'); |
|
|
|
|
|
|
752
|
|
|
$json['error'] = true; |
|
753
|
|
|
|
|
754
|
|
|
$this->set(compact('json')); |
|
755
|
|
|
$this->set('_serialize', 'json'); |
|
756
|
|
|
|
|
757
|
|
|
return; |
|
758
|
|
|
} |
|
759
|
|
|
|
|
760
|
|
|
$user = $this->ConversationsUsers |
|
|
|
|
|
|
761
|
|
|
->find() |
|
762
|
|
|
->contain([ |
|
763
|
|
|
'Conversations', |
|
764
|
|
|
'Users' => function ($q) { |
|
765
|
|
|
return $q->find('short'); |
|
766
|
|
|
}, |
|
767
|
|
|
'Users.Groups' => function ($q) { |
|
768
|
|
|
return $q->select(['id', 'is_staff']); |
|
769
|
|
|
} |
|
770
|
|
|
]) |
|
771
|
|
|
->where([ |
|
772
|
|
|
'ConversationsUsers.user_id' => $this->request->user_id, |
|
773
|
|
|
'ConversationsUsers.conversation_id' => $this->request->id |
|
774
|
|
|
]) |
|
775
|
|
|
->first(); |
|
776
|
|
|
|
|
777
|
|
|
//Check if the user to kick is in the conversation and if he is not owner of this conversation and if he is not a staff member. |
|
778
|
|
|
if (is_null($user) || $this->request->user_id == $currentUser->conversation->user_id || $user->user->group->is_staff) { |
|
779
|
|
|
$json['message'] = __d('conversations', 'You cannot kick this user from this conversation.'); |
|
|
|
|
|
|
780
|
|
|
$json['error'] = true; |
|
781
|
|
|
|
|
782
|
|
|
$this->set(compact('json')); |
|
783
|
|
|
$this->set('_serialize', 'json'); |
|
784
|
|
|
|
|
785
|
|
|
return; |
|
786
|
|
|
} |
|
787
|
|
|
|
|
788
|
|
|
$this->ConversationsUsers->delete($user); |
|
|
|
|
|
|
789
|
|
|
|
|
790
|
|
|
$expression = new QueryExpression('recipient_count = recipient_count - 1'); |
|
791
|
|
|
$this->Conversations->updateAll( |
|
|
|
|
|
|
792
|
|
|
[$expression], |
|
793
|
|
|
[ |
|
794
|
|
|
'id' => $this->request->id |
|
795
|
|
|
] |
|
796
|
|
|
); |
|
797
|
|
|
|
|
798
|
|
|
$json['message'] = __d('conversations', 'This user has been kicked successfully.'); |
|
|
|
|
|
|
799
|
|
|
$json['error'] = false; |
|
800
|
|
|
$json['id'] = $this->request->user_id; |
|
|
|
|
|
|
801
|
|
|
$json['recipients'] = $currentUser->conversation->recipient_count - 1; |
|
802
|
|
|
|
|
803
|
|
|
$this->set(compact('json')); |
|
804
|
|
|
$this->set('_serialize', 'json'); |
|
805
|
|
|
} |
|
806
|
|
|
|
|
807
|
|
|
/** |
|
808
|
|
|
* Reply to a conversation. |
|
809
|
|
|
* |
|
810
|
|
|
* @return void|\Cake\Network\Response |
|
811
|
|
|
*/ |
|
812
|
|
|
public function reply() |
|
813
|
|
|
{ |
|
814
|
|
|
$this->loadModel('ConversationsMessages'); |
|
815
|
|
|
|
|
816
|
|
|
if ($this->request->is('post')) { |
|
817
|
|
|
$conversation = $this->Conversations |
|
|
|
|
|
|
818
|
|
|
->find() |
|
819
|
|
|
->where(['Conversations.id' => $this->request->id]) |
|
820
|
|
|
->first(); |
|
821
|
|
|
|
|
822
|
|
|
$this->loadModel('ConversationsUsers'); |
|
823
|
|
|
$user = $this->ConversationsUsers |
|
|
|
|
|
|
824
|
|
|
->find() |
|
825
|
|
|
->where([ |
|
826
|
|
|
'ConversationsUsers.conversation_id' => $this->request->id, |
|
827
|
|
|
'ConversationsUsers.user_id' => $this->Auth->user('id') |
|
828
|
|
|
]) |
|
829
|
|
|
->first(); |
|
830
|
|
|
|
|
831
|
|
|
//Check if the conversation is found. |
|
832
|
|
|
if (is_null($user) || is_null($conversation) || $conversation->conversation_open == 2) { |
|
833
|
|
|
$this->Flash->error(__d('conversations', "This conversation doesn't exist or has been deleted !")); |
|
834
|
|
|
|
|
835
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
|
|
836
|
|
|
} |
|
837
|
|
|
|
|
838
|
|
|
//Check if the conversation is open. |
|
839
|
|
View Code Duplication |
if ($conversation->conversation_open == false) { |
|
|
|
|
|
|
840
|
|
|
$this->Flash->error(__d('conversations', 'This conversation is closed, you cannot reply.')); |
|
841
|
|
|
|
|
842
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
|
|
843
|
|
|
} |
|
844
|
|
|
|
|
845
|
|
|
//Build the newEntity for the post form. |
|
846
|
|
|
$this->request = $this->request |
|
847
|
|
|
->withData('conversation.last_post_date', new Time()) |
|
848
|
|
|
->withData('conversation.last_post_user_id', $this->Auth->user('id')) |
|
849
|
|
|
->withData('user_id', $this->Auth->user('id')) |
|
850
|
|
|
->withData('conversation_id', $this->request->id); |
|
851
|
|
|
|
|
852
|
|
|
$message = $this->ConversationsMessages->newEntity($this->request->getParsedBody(), [ |
|
|
|
|
|
|
853
|
|
|
'associated' => ['Conversations'], |
|
854
|
|
|
'validate' => 'create' |
|
855
|
|
|
]); |
|
856
|
|
|
|
|
857
|
|
|
//Handle validation errors (Due to the redirect) |
|
858
|
|
|
if (!empty($message->errors())) { |
|
859
|
|
|
$this->Flash->conversationsReply('Validation errors', [ |
|
860
|
|
|
'key' => 'ConversationsReply', |
|
861
|
|
|
'params' => [ |
|
862
|
|
|
'errors' => $message->errors() |
|
863
|
|
|
] |
|
864
|
|
|
]); |
|
865
|
|
|
|
|
866
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
|
|
867
|
|
|
} |
|
868
|
|
|
|
|
869
|
|
|
if ($message->conversation->isNew() === true) { |
|
870
|
|
|
$message->conversation->isNew(false); |
|
871
|
|
|
} |
|
872
|
|
|
|
|
873
|
|
|
$message->conversation->accessible('id', true); |
|
874
|
|
|
$message->conversation->id = $this->request->id; |
|
|
|
|
|
|
875
|
|
|
|
|
876
|
|
|
if ($message = $this->ConversationsMessages->save($message)) { |
|
|
|
|
|
|
877
|
|
|
//Update the last message id for the conversation. |
|
878
|
|
|
$this->loadModel('Conversations'); |
|
879
|
|
|
$conversation = $this->Conversations->get($this->request->id); |
|
|
|
|
|
|
880
|
|
|
$conversation->last_message_id = $message->id; |
|
881
|
|
|
$conversation->last_message_date = new Time(); |
|
882
|
|
|
$conversation->last_message_user_id = $this->Auth->user('id'); |
|
883
|
|
|
$this->Conversations->save($conversation); |
|
|
|
|
|
|
884
|
|
|
|
|
885
|
|
|
//Notifications Event. |
|
886
|
|
|
$this->eventManager()->attach(new Notifications()); |
|
|
|
|
|
|
887
|
|
|
$event = new Event('Model.Notifications.dispatch', $this, [ |
|
888
|
|
|
'sender_id' => $this->Auth->user('id'), |
|
889
|
|
|
'conversation_id' => $conversation->id, |
|
890
|
|
|
'type' => 'conversation.reply' |
|
891
|
|
|
]); |
|
892
|
|
|
$this->eventManager()->dispatch($event); |
|
893
|
|
|
|
|
894
|
|
|
$conversationOpen = !is_null($this->request->getData('conversation.conversation_open')) ? $this->request->getData('conversation.conversation_open') : true; |
|
895
|
|
|
|
|
896
|
|
|
if ($conversationOpen == false) { |
|
897
|
|
|
$this->Flash->success(__d('conversations', 'Your reply has been posted successfully and the conversation has been closed !')); |
|
898
|
|
|
} else { |
|
899
|
|
|
$this->Flash->success(__d('conversations', 'Your reply has been posted successfully !')); |
|
900
|
|
|
} |
|
901
|
|
|
|
|
902
|
|
|
//Redirect the user to the last page of the article. |
|
903
|
|
|
return $this->redirect([ |
|
904
|
|
|
'controller' => 'conversations', |
|
905
|
|
|
'action' => 'go', |
|
906
|
|
|
$message->id |
|
907
|
|
|
]); |
|
908
|
|
|
} |
|
909
|
|
|
} |
|
910
|
|
|
|
|
911
|
|
|
$this->redirect($this->referer()); |
|
|
|
|
|
|
912
|
|
|
} |
|
913
|
|
|
|
|
914
|
|
|
/** |
|
915
|
|
|
* Edit a conversation. |
|
916
|
|
|
* |
|
917
|
|
|
* @return \Cake\Network\Response |
|
918
|
|
|
*/ |
|
919
|
|
|
public function edit() |
|
920
|
|
|
{ |
|
921
|
|
|
$this->loadModel('Conversations'); |
|
922
|
|
|
|
|
923
|
|
|
if ($this->request->is('put')) { |
|
924
|
|
|
$conversation = $this->Conversations |
|
|
|
|
|
|
925
|
|
|
->find() |
|
926
|
|
|
->where([ |
|
927
|
|
|
'Conversations.id' => $this->request->id |
|
928
|
|
|
]) |
|
929
|
|
|
->first(); |
|
930
|
|
|
|
|
931
|
|
|
//Check if the conversation is found. |
|
932
|
|
View Code Duplication |
if (is_null($conversation)) { |
|
|
|
|
|
|
933
|
|
|
$this->Flash->error(__d('conversations', "This conversation doesn't exist or has been deleted !")); |
|
934
|
|
|
|
|
935
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
|
|
936
|
|
|
} |
|
937
|
|
|
|
|
938
|
|
|
//Check if the user has the permission to edit it. |
|
939
|
|
View Code Duplication |
if ($this->Auth->isAuthorized() === false) { |
|
|
|
|
|
|
940
|
|
|
$this->Flash->error(__d('conversations', "You don't have the authorization to edit this conversation !")); |
|
941
|
|
|
|
|
942
|
|
|
return $this->redirect([ |
|
943
|
|
|
'controller' => 'conversations', |
|
944
|
|
|
'action' => 'go', |
|
945
|
|
|
$conversation->last_message_id |
|
946
|
|
|
]); |
|
947
|
|
|
} |
|
948
|
|
|
|
|
949
|
|
|
$this->Conversations->patchEntity($conversation, $this->request->getParsedBody(), ['validate' => 'edit']); |
|
|
|
|
|
|
950
|
|
|
|
|
951
|
|
|
if ($this->Conversations->save($conversation)) { |
|
|
|
|
|
|
952
|
|
|
if ($conversation->conversation_open == false) { |
|
953
|
|
|
$this->Flash->success(__d('conversations', 'Your conversation has been edited and closed successfully !')); |
|
954
|
|
|
} else { |
|
955
|
|
|
$this->Flash->success(__d('conversations', 'Your conversation has been edited successfully !')); |
|
956
|
|
|
} |
|
957
|
|
|
|
|
958
|
|
|
return $this->redirect([ |
|
959
|
|
|
'controller' => 'conversations', |
|
960
|
|
|
'action' => 'go', |
|
961
|
|
|
$conversation->last_message_id |
|
962
|
|
|
]); |
|
963
|
|
|
} |
|
964
|
|
|
} |
|
965
|
|
|
|
|
966
|
|
|
$this->redirect($this->referer()); |
|
|
|
|
|
|
967
|
|
|
} |
|
968
|
|
|
|
|
969
|
|
|
/** |
|
970
|
|
|
* Function to invite user(s) in a conversation. |
|
971
|
|
|
* |
|
972
|
|
|
* @return void|\Cake\Network\Response |
|
973
|
|
|
*/ |
|
974
|
|
|
public function invite() |
|
975
|
|
|
{ |
|
976
|
|
|
$this->loadModel('ConversationsUsers'); |
|
977
|
|
|
|
|
978
|
|
|
$conversation = $this->ConversationsUsers |
|
|
|
|
|
|
979
|
|
|
->find() |
|
980
|
|
|
->contain([ |
|
981
|
|
|
'Conversations', |
|
982
|
|
|
'Users', |
|
983
|
|
|
'Users.Groups' |
|
984
|
|
|
]) |
|
985
|
|
|
->where([ |
|
986
|
|
|
'ConversationsUsers.conversation_id' => $this->request->id, |
|
987
|
|
|
'ConversationsUsers.user_id' => $this->Auth->user('id') |
|
988
|
|
|
]) |
|
989
|
|
|
->first(); |
|
990
|
|
|
|
|
991
|
|
View Code Duplication |
if (is_null($conversation) || $conversation->conversation->conversation_open != 1) { |
|
|
|
|
|
|
992
|
|
|
$this->Flash->error(__d('conversations', 'This conversation is closed or has been deleted !')); |
|
993
|
|
|
|
|
994
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
|
|
995
|
|
|
} |
|
996
|
|
|
|
|
997
|
|
|
if (!$conversation->conversation->open_invite && $conversation->conversation->user_id != $this->Auth->user('id') && !$conversation->user->group->is_staff) { |
|
998
|
|
|
$this->Flash->error(__d('conversations', "You don't have the authorization to invite in this conversation !")); |
|
999
|
|
|
|
|
1000
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
|
|
1001
|
|
|
} |
|
1002
|
|
|
|
|
1003
|
|
|
if ($this->request->is(['post', 'put'])) { |
|
1004
|
|
|
$users = str_replace(",", "", trim(strtolower($this->request->getData('users')))); |
|
1005
|
|
|
$users = explode(" ", $users); |
|
1006
|
|
|
|
|
1007
|
|
|
$maxUsersCheck = $this->ConversationsUsers |
|
|
|
|
|
|
1008
|
|
|
->find() |
|
1009
|
|
|
->where([ |
|
1010
|
|
|
'ConversationsUsers.conversation_id' => $this->request->id |
|
1011
|
|
|
]) |
|
1012
|
|
|
->count(); |
|
1013
|
|
|
|
|
1014
|
|
|
//Check max users. |
|
1015
|
|
|
if (count($users) + $maxUsersCheck >= Configure::read('Conversations.max_users_per_conversation')) { |
|
1016
|
|
|
$this->Flash->error(__d('conversations', 'You cannot invite more than {0} user(s) in this conversation.', Configure::read('Conversations.max_users_per_conversation'))); |
|
1017
|
|
|
|
|
1018
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
|
|
1019
|
|
|
} |
|
1020
|
|
|
|
|
1021
|
|
|
$this->loadModel('Users'); |
|
1022
|
|
|
$this->loadModel('Conversations'); |
|
1023
|
|
|
|
|
1024
|
|
|
foreach ($users as $user) { |
|
1025
|
|
|
$user = $this->Users |
|
|
|
|
|
|
1026
|
|
|
->find() |
|
1027
|
|
|
->where([ |
|
1028
|
|
|
'LOWER(Users.username)' => $user |
|
1029
|
|
|
]) |
|
1030
|
|
|
->first(); |
|
1031
|
|
|
|
|
1032
|
|
|
if (!is_null($user)) { |
|
1033
|
|
|
$check = $this->ConversationsUsers |
|
|
|
|
|
|
1034
|
|
|
->find() |
|
1035
|
|
|
->where([ |
|
1036
|
|
|
'ConversationsUsers.conversation_id' => $this->request->id, |
|
1037
|
|
|
'ConversationsUsers.user_id' => $user->id |
|
1038
|
|
|
]) |
|
1039
|
|
|
->first(); |
|
1040
|
|
|
|
|
1041
|
|
|
if (is_null($check)) { |
|
1042
|
|
|
$data = []; |
|
1043
|
|
|
$data['conversation_id'] = $this->request->id; |
|
|
|
|
|
|
1044
|
|
|
$data['user_id'] = $user->id; |
|
1045
|
|
|
|
|
1046
|
|
|
$entity = $this->ConversationsUsers->newEntity($data); |
|
|
|
|
|
|
1047
|
|
|
$this->ConversationsUsers->save($entity); |
|
|
|
|
|
|
1048
|
|
|
|
|
1049
|
|
|
$expression = new QueryExpression('recipient_count = recipient_count + 1'); |
|
1050
|
|
|
$this->Conversations->updateAll( |
|
|
|
|
|
|
1051
|
|
|
[$expression], |
|
1052
|
|
|
[ |
|
1053
|
|
|
'id' => $this->request->id |
|
1054
|
|
|
] |
|
1055
|
|
|
); |
|
1056
|
|
|
} |
|
1057
|
|
|
} |
|
1058
|
|
|
} |
|
1059
|
|
|
|
|
1060
|
|
|
$this->Flash->success(__d('conversations', 'Your user(s) has been added successfully.')); |
|
1061
|
|
|
} |
|
1062
|
|
|
|
|
1063
|
|
|
return $this->redirect([ |
|
1064
|
|
|
'controller' => 'conversations', |
|
1065
|
|
|
'action' => 'go', |
|
1066
|
|
|
$conversation->conversation->last_message_id |
|
1067
|
|
|
]); |
|
1068
|
|
|
} |
|
1069
|
|
|
|
|
1070
|
|
|
/** |
|
1071
|
|
|
* Function to leave a conversation. |
|
1072
|
|
|
* |
|
1073
|
|
|
* @return void|\Cake\Network\Response |
|
1074
|
|
|
*/ |
|
1075
|
|
|
public function leave() |
|
1076
|
|
|
{ |
|
1077
|
|
|
$this->loadModel('ConversationsUsers'); |
|
1078
|
|
|
|
|
1079
|
|
|
$user = $this->ConversationsUsers |
|
|
|
|
|
|
1080
|
|
|
->find() |
|
1081
|
|
|
->contain([ |
|
1082
|
|
|
'Conversations' |
|
1083
|
|
|
]) |
|
1084
|
|
|
->where([ |
|
1085
|
|
|
'ConversationsUsers.conversation_id' => $this->request->id, |
|
1086
|
|
|
'ConversationsUsers.user_id' => $this->Auth->user('id') |
|
1087
|
|
|
]) |
|
1088
|
|
|
->first(); |
|
1089
|
|
|
|
|
1090
|
|
|
if (is_null($user)) { |
|
1091
|
|
|
$this->Flash->error(__d('conversations', 'You are not in this conversation.')); |
|
1092
|
|
|
|
|
1093
|
|
|
return $this->redirect($this->referer()); |
|
|
|
|
|
|
1094
|
|
|
} |
|
1095
|
|
|
|
|
1096
|
|
|
if ($user->conversation->user_id != $this->Auth->user('id')) { |
|
1097
|
|
|
$this->loadModel('Conversations'); |
|
1098
|
|
|
|
|
1099
|
|
|
$this->ConversationsUsers->delete($user); |
|
|
|
|
|
|
1100
|
|
|
|
|
1101
|
|
|
$expression = new QueryExpression('recipient_count = recipient_count - 1'); |
|
1102
|
|
|
$this->Conversations->updateAll( |
|
|
|
|
|
|
1103
|
|
|
[$expression], |
|
1104
|
|
|
[ |
|
1105
|
|
|
'id' => $this->request->id |
|
1106
|
|
|
] |
|
1107
|
|
|
); |
|
1108
|
|
|
|
|
1109
|
|
|
$this->Flash->success(__d('conversations', 'You have left the conversation successfully.')); |
|
1110
|
|
|
|
|
1111
|
|
|
return $this->redirect(['controller' => 'conversations', 'action' => 'index']); |
|
1112
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
1113
|
|
|
$this->Flash->error(__d('conversations', 'You can not leave your own conversation.')); |
|
1114
|
|
|
|
|
1115
|
|
|
return $this->redirect([ |
|
1116
|
|
|
'controller' => 'conversations', |
|
1117
|
|
|
'action' => 'go', |
|
1118
|
|
|
$user->conversation->last_message_id |
|
1119
|
|
|
]); |
|
1120
|
|
|
} |
|
1121
|
|
|
} |
|
1122
|
|
|
|
|
1123
|
|
|
/** |
|
1124
|
|
|
* Search conversations. |
|
1125
|
|
|
* |
|
1126
|
|
|
* @return void |
|
1127
|
|
|
*/ |
|
1128
|
|
|
public function search() |
|
1129
|
|
|
{ |
|
1130
|
|
|
$this->loadModel('ConversationsUsers'); |
|
1131
|
|
|
|
|
1132
|
|
|
//Check the keyword to search. (For pagination) |
|
1133
|
|
View Code Duplication |
if (!empty($this->request->getData('search'))) { |
|
|
|
|
|
|
1134
|
|
|
$keyword = $this->request->getData('search'); |
|
1135
|
|
|
$this->request->session()->write('Search.Conversations.Keyword', $keyword); |
|
1136
|
|
|
} else { |
|
1137
|
|
|
if ($this->request->session()->read('Search.Conversations.Keyword')) { |
|
|
|
|
|
|
1138
|
|
|
$keyword = $this->request->session()->read('Search.Conversations.Keyword'); |
|
1139
|
|
|
} else { |
|
1140
|
|
|
$keyword = ''; |
|
1141
|
|
|
} |
|
1142
|
|
|
} |
|
1143
|
|
|
|
|
1144
|
|
|
//Pagination |
|
1145
|
|
|
$this->paginate = [ |
|
1146
|
|
|
'maxLimit' => Configure::read('Conversations.conversations_per_page') |
|
1147
|
|
|
]; |
|
1148
|
|
|
|
|
1149
|
|
|
$conversations = $this->ConversationsUsers |
|
|
|
|
|
|
1150
|
|
|
->find() |
|
1151
|
|
|
->contain([ |
|
1152
|
|
|
'Users', |
|
1153
|
|
|
'Conversations', |
|
1154
|
|
|
'Conversations.Users' => function ($q) { |
|
1155
|
|
|
return $q->find('medium'); |
|
1156
|
|
|
}, |
|
1157
|
|
|
'Conversations.LastMessage', |
|
1158
|
|
|
'Conversations.LastMessageUser' |
|
1159
|
|
|
]) |
|
1160
|
|
|
->where([ |
|
1161
|
|
|
'ConversationsUsers.user_id' => $this->Auth->user('id'), |
|
1162
|
|
|
'Conversations.conversation_open <>' => 2 |
|
1163
|
|
|
]) |
|
1164
|
|
|
->andWhere(function ($q) use ($keyword) { |
|
1165
|
|
|
return $q |
|
1166
|
|
|
->like('Conversations.title', "%$keyword%"); |
|
1167
|
|
|
}) |
|
1168
|
|
|
->order([ |
|
1169
|
|
|
'ConversationsUsers.is_read' => 'ASC', |
|
1170
|
|
|
'ConversationsUsers.is_star' => 'DESC', |
|
1171
|
|
|
'Conversations.last_message_date' => 'DESC', |
|
1172
|
|
|
]); |
|
1173
|
|
|
|
|
1174
|
|
|
$conversations = $this->paginate($conversations); |
|
1175
|
|
|
|
|
1176
|
|
|
$this->set(compact('conversations', 'keyword')); |
|
1177
|
|
|
} |
|
1178
|
|
|
|
|
1179
|
|
|
/** |
|
1180
|
|
|
* Action to render the maintenance page. |
|
1181
|
|
|
* |
|
1182
|
|
|
* @return void |
|
1183
|
|
|
*/ |
|
1184
|
|
|
public function maintenance() |
|
1185
|
|
|
{ |
|
1186
|
|
|
} |
|
1187
|
|
|
} |
|
1188
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.