1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Entity; |
8
|
|
|
|
9
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter; |
10
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter; |
11
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
12
|
|
|
use ApiPlatform\Metadata\ApiFilter; |
13
|
|
|
use ApiPlatform\Metadata\ApiResource; |
14
|
|
|
use ApiPlatform\Metadata\Delete; |
15
|
|
|
use ApiPlatform\Metadata\Get; |
16
|
|
|
use ApiPlatform\Metadata\GetCollection; |
17
|
|
|
use ApiPlatform\Metadata\Post; |
18
|
|
|
use ApiPlatform\Metadata\Put; |
19
|
|
|
use Chamilo\CoreBundle\Entity\Listener\MessageListener; |
20
|
|
|
use Chamilo\CoreBundle\Filter\SearchOrFilter; |
21
|
|
|
use Chamilo\CoreBundle\Repository\MessageRepository; |
22
|
|
|
use Chamilo\CoreBundle\State\MessageByGroupStateProvider; |
23
|
|
|
use Chamilo\CoreBundle\State\MessageProcessor; |
24
|
|
|
use DateTime; |
25
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
26
|
|
|
use Doctrine\Common\Collections\Collection; |
27
|
|
|
use Doctrine\Common\Collections\Criteria; |
28
|
|
|
use Doctrine\ORM\Mapping as ORM; |
29
|
|
|
use Gedmo\Mapping\Annotation as Gedmo; |
30
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
31
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
32
|
|
|
|
33
|
|
|
#[ORM\Table(name: 'message')] |
34
|
|
|
#[ORM\Index(columns: ['user_sender_id'], name: 'idx_message_user_sender')] |
35
|
|
|
#[ORM\Index(columns: ['group_id'], name: 'idx_message_group')] |
36
|
|
|
#[ORM\Index(columns: ['msg_type'], name: 'idx_message_type')] |
37
|
|
|
#[ORM\Entity(repositoryClass: MessageRepository::class)] |
38
|
|
|
#[ORM\EntityListeners([MessageListener::class])] |
39
|
|
|
#[ApiResource( |
40
|
|
|
operations: [ |
41
|
|
|
new Get(security: "is_granted('VIEW', object)"), |
42
|
|
|
new Put(security: "is_granted('EDIT', object)"), |
43
|
|
|
new Delete(security: "is_granted('DELETE', object)"), |
44
|
|
|
new GetCollection( |
45
|
|
|
uriTemplate: '/messages', |
46
|
|
|
security: "is_granted('ROLE_USER')", |
47
|
|
|
name: 'get_all_messages' |
48
|
|
|
), |
49
|
|
|
new GetCollection( |
50
|
|
|
uriTemplate: '/messages/by-group/list', |
51
|
|
|
security: "is_granted('ROLE_USER')", |
52
|
|
|
name: 'get_messages_by_social_group', |
53
|
|
|
provider: MessageByGroupStateProvider::class |
54
|
|
|
), |
55
|
|
|
new Post(securityPostDenormalize: "is_granted('CREATE', object)"), |
56
|
|
|
], |
57
|
|
|
normalizationContext: [ |
58
|
|
|
'groups' => ['message:read'], |
59
|
|
|
], |
60
|
|
|
denormalizationContext: [ |
61
|
|
|
'groups' => ['message:write'], |
62
|
|
|
], |
63
|
|
|
security: "is_granted('ROLE_USER')", |
64
|
|
|
processor: MessageProcessor::class, |
65
|
|
|
)] |
66
|
|
|
#[ApiFilter(filterClass: OrderFilter::class, properties: ['title', 'sendDate'])] |
67
|
|
|
#[ApiFilter(SearchFilter::class, properties: [ |
68
|
|
|
'msgType' => 'exact', |
69
|
|
|
'status' => 'exact', |
70
|
|
|
'sender' => 'exact', |
71
|
|
|
'receivers.receiver' => 'exact', |
72
|
|
|
'receivers.receiverType' => 'exact', |
73
|
|
|
'receivers.tags.tag' => 'exact', |
74
|
|
|
'parent' => 'exact', |
75
|
|
|
])] |
76
|
|
|
#[ApiFilter( |
77
|
|
|
BooleanFilter::class, |
78
|
|
|
properties: ['receivers.read'] |
79
|
|
|
)] |
80
|
|
|
#[ApiFilter(SearchOrFilter::class, properties: ['title', 'content'])] |
81
|
|
|
class Message |
82
|
|
|
{ |
83
|
|
|
public const MESSAGE_TYPE_INBOX = 1; |
84
|
|
|
public const MESSAGE_TYPE_GROUP = 5; |
85
|
|
|
public const MESSAGE_TYPE_INVITATION = 6; |
86
|
|
|
public const MESSAGE_TYPE_CONVERSATION = 7; |
87
|
|
|
// status |
88
|
|
|
public const MESSAGE_STATUS_DELETED = 3; |
89
|
|
|
public const MESSAGE_STATUS_DRAFT = 4; |
90
|
|
|
public const MESSAGE_STATUS_INVITATION_PENDING = 5; |
91
|
|
|
public const MESSAGE_STATUS_INVITATION_ACCEPTED = 6; |
92
|
|
|
public const MESSAGE_STATUS_INVITATION_DENIED = 7; |
93
|
|
|
|
94
|
|
|
#[ORM\Column(name: 'id', type: 'integer')] |
95
|
|
|
#[ORM\Id] |
96
|
|
|
#[ORM\GeneratedValue] |
97
|
|
|
#[Groups(['message:read'])] |
98
|
|
|
protected ?int $id = null; |
99
|
|
|
|
100
|
|
|
#[Assert\NotBlank] |
101
|
|
|
#[Groups(['message:read', 'message:write'])] |
102
|
|
|
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'sentMessages')] |
103
|
|
|
#[ORM\JoinColumn(name: 'user_sender_id', referencedColumnName: 'id', onDelete: 'SET NULL')] |
104
|
|
|
protected ?User $sender = null; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var Collection<int, MessageRelUser> |
108
|
|
|
*/ |
109
|
|
|
#[ORM\OneToMany(mappedBy: 'message', targetEntity: MessageRelUser::class, cascade: ['persist', 'remove'])] |
110
|
|
|
#[Groups(['message:write'])] |
111
|
|
|
protected Collection $receivers; |
112
|
|
|
|
113
|
|
|
#[Assert\NotBlank] |
114
|
|
|
#[Groups(['message:read', 'message:write'])] |
115
|
|
|
#[ORM\Column(name: 'msg_type', type: 'smallint', nullable: false)] |
116
|
|
|
protected int $msgType; |
117
|
|
|
|
118
|
|
|
#[Assert\NotBlank] |
119
|
|
|
#[Groups(['message:read', 'message:write'])] |
120
|
|
|
#[ORM\Column(name: 'status', type: 'smallint', nullable: false)] |
121
|
|
|
protected int $status; |
122
|
|
|
|
123
|
|
|
#[Groups(['message:read'])] |
124
|
|
|
#[ORM\Column(name: 'send_date', type: 'datetime', nullable: false)] |
125
|
|
|
protected DateTime $sendDate; |
126
|
|
|
|
127
|
|
|
#[Assert\NotBlank] |
128
|
|
|
#[Groups(['message:read', 'message:write'])] |
129
|
|
|
#[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)] |
130
|
|
|
protected string $title; |
131
|
|
|
|
132
|
|
|
#[Assert\NotBlank] |
133
|
|
|
#[Groups(['message:read', 'message:write'])] |
134
|
|
|
#[ORM\Column(name: 'content', type: 'text', nullable: false)] |
135
|
|
|
protected string $content; |
136
|
|
|
|
137
|
|
|
#[Groups(['message:read', 'message:write'])] |
138
|
|
|
#[ORM\ManyToOne(targetEntity: Usergroup::class)] |
139
|
|
|
#[ORM\JoinColumn(name: 'group_id', referencedColumnName: 'id', onDelete: 'CASCADE')] |
140
|
|
|
protected ?Usergroup $group = null; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @var Collection<int, Message> |
144
|
|
|
*/ |
145
|
|
|
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)] |
146
|
|
|
protected Collection $children; |
147
|
|
|
|
148
|
|
|
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] |
149
|
|
|
#[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id')] |
150
|
|
|
protected ?Message $parent = null; |
151
|
|
|
|
152
|
|
|
#[Gedmo\Timestampable(on: 'update')] |
153
|
|
|
#[ORM\Column(name: 'update_date', type: 'datetime', nullable: true)] |
154
|
|
|
protected ?DateTime $updateDate; |
155
|
|
|
|
156
|
|
|
#[ORM\Column(name: 'votes', type: 'integer', nullable: true)] |
157
|
|
|
protected ?int $votes; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @var Collection<int, MessageAttachment> |
161
|
|
|
*/ |
162
|
|
|
#[Assert\Valid] |
163
|
|
|
#[Groups(['message:read', 'message:write'])] |
164
|
|
|
#[ORM\OneToMany( |
165
|
|
|
mappedBy: 'message', |
166
|
|
|
targetEntity: MessageAttachment::class, |
167
|
|
|
cascade: ['persist'], |
168
|
|
|
orphanRemoval: true, |
169
|
|
|
)] |
170
|
|
|
protected Collection $attachments; |
171
|
|
|
|
172
|
|
|
#[ORM\OneToMany(mappedBy: 'message', targetEntity: MessageFeedback::class, orphanRemoval: true)] |
173
|
|
|
protected Collection $likes; |
174
|
|
|
|
175
|
|
|
public function __construct() |
176
|
|
|
{ |
177
|
|
|
$this->sendDate = new DateTime('now'); |
178
|
|
|
$this->updateDate = $this->sendDate; |
179
|
|
|
$this->msgType = self::MESSAGE_TYPE_INBOX; |
180
|
|
|
$this->content = ''; |
181
|
|
|
$this->attachments = new ArrayCollection(); |
182
|
|
|
$this->children = new ArrayCollection(); |
183
|
|
|
$this->receivers = new ArrayCollection(); |
184
|
|
|
$this->likes = new ArrayCollection(); |
185
|
|
|
$this->votes = 0; |
186
|
|
|
$this->status = 0; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return Collection<int, MessageRelUser> |
191
|
|
|
*/ |
192
|
|
|
public function getReceivers(): Collection |
193
|
|
|
{ |
194
|
|
|
return $this->receivers; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function setReceiversFromArray(array $receivers): self |
198
|
|
|
{ |
199
|
|
|
$this->receivers = new ArrayCollection($receivers); |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
#[Groups(['message:read'])] |
205
|
|
|
public function getReceiversTo(): array |
206
|
|
|
{ |
207
|
|
|
return $this->receivers |
208
|
|
|
->filter( |
209
|
|
|
fn (MessageRelUser $messageRelUser) => MessageRelUser::TYPE_TO === $messageRelUser->getReceiverType() |
210
|
|
|
)->getValues() |
211
|
|
|
; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
#[Groups(['message:read'])] |
215
|
|
|
public function getReceiversCc(): array |
216
|
|
|
{ |
217
|
|
|
return $this->receivers |
218
|
|
|
->filter( |
219
|
|
|
fn (MessageRelUser $messageRelUser) => MessageRelUser::TYPE_CC === $messageRelUser->getReceiverType() |
220
|
|
|
) |
221
|
|
|
->getValues() |
222
|
|
|
; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
#[Groups(['message:read'])] |
226
|
|
|
public function getReceiversSender(): array |
227
|
|
|
{ |
228
|
|
|
return $this->receivers |
229
|
|
|
->filter( |
230
|
|
|
fn (MessageRelUser $messageRelUser) => MessageRelUser::TYPE_SENDER === $messageRelUser->getReceiverType() |
231
|
|
|
) |
232
|
|
|
->getValues() |
233
|
|
|
; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
#[Groups(['message:read'])] |
237
|
|
|
public function getFirstReceiver(): ?MessageRelUser |
238
|
|
|
{ |
239
|
|
|
if ($this->receivers->count() > 0) { |
240
|
|
|
return $this->receivers->first(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return null; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function hasUserReceiver(User $receiver): bool |
247
|
|
|
{ |
248
|
|
|
if ($this->receivers->count()) { |
249
|
|
|
$criteria = Criteria::create() |
250
|
|
|
->where( |
251
|
|
|
Criteria::expr()->eq('receiver', $receiver) |
252
|
|
|
) |
253
|
|
|
->andWhere( |
254
|
|
|
Criteria::expr()->eq('message', $this) |
255
|
|
|
) |
256
|
|
|
; |
257
|
|
|
|
258
|
|
|
return $this->receivers->matching($criteria)->count() > 0; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return false; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function addReceiverTo(User $receiver): self |
265
|
|
|
{ |
266
|
|
|
$messageRelUser = (new MessageRelUser()) |
267
|
|
|
->setReceiver($receiver) |
268
|
|
|
->setReceiverType(MessageRelUser::TYPE_TO) |
269
|
|
|
; |
270
|
|
|
|
271
|
|
|
$this->addReceiver($messageRelUser); |
272
|
|
|
|
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function addReceiver(MessageRelUser $messageRelUser): self |
277
|
|
|
{ |
278
|
|
|
if (!$this->receivers->contains($messageRelUser)) { |
279
|
|
|
$this->receivers->add($messageRelUser); |
280
|
|
|
|
281
|
|
|
$messageRelUser->setMessage($this); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function addReceiverCc(User $receiver): self |
288
|
|
|
{ |
289
|
|
|
$messageRelUser = (new MessageRelUser()) |
290
|
|
|
->setReceiver($receiver) |
291
|
|
|
->setReceiverType(MessageRelUser::TYPE_CC) |
292
|
|
|
; |
293
|
|
|
|
294
|
|
|
$this->addReceiver($messageRelUser); |
295
|
|
|
|
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function removeReceiver(MessageRelUser $messageRelUser): self |
300
|
|
|
{ |
301
|
|
|
$this->receivers->removeElement($messageRelUser); |
302
|
|
|
|
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function getSender(): ?User |
307
|
|
|
{ |
308
|
|
|
return $this->sender; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function setSender(?User $sender): self |
312
|
|
|
{ |
313
|
|
|
$this->sender = $sender; |
314
|
|
|
|
315
|
|
|
return $this; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
public function getMsgType(): int |
319
|
|
|
{ |
320
|
|
|
return $this->msgType; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function setMsgType(int $msgType): self |
324
|
|
|
{ |
325
|
|
|
$this->msgType = $msgType; |
326
|
|
|
|
327
|
|
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function getSendDate(): DateTime |
331
|
|
|
{ |
332
|
|
|
return $this->sendDate; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function setSendDate(DateTime $sendDate): self |
336
|
|
|
{ |
337
|
|
|
$this->sendDate = $sendDate; |
338
|
|
|
|
339
|
|
|
return $this; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
public function getTitle(): string |
343
|
|
|
{ |
344
|
|
|
return $this->title; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
public function setTitle(string $title): self |
348
|
|
|
{ |
349
|
|
|
$this->title = $title; |
350
|
|
|
|
351
|
|
|
return $this; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
public function getContent(): string |
355
|
|
|
{ |
356
|
|
|
return $this->content; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
public function setContent(string $content): self |
360
|
|
|
{ |
361
|
|
|
$this->content = $content; |
362
|
|
|
|
363
|
|
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
public function getUpdateDate(): ?DateTime |
367
|
|
|
{ |
368
|
|
|
return $this->updateDate; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function setUpdateDate(DateTime $updateDate): self |
372
|
|
|
{ |
373
|
|
|
$this->updateDate = $updateDate; |
374
|
|
|
|
375
|
|
|
return $this; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
public function getId(): ?int |
379
|
|
|
{ |
380
|
|
|
return $this->id; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function getVotes(): int |
384
|
|
|
{ |
385
|
|
|
return $this->votes; |
|
|
|
|
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function setVotes(int $votes): self |
389
|
|
|
{ |
390
|
|
|
$this->votes = $votes; |
391
|
|
|
|
392
|
|
|
return $this; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @return Collection<int, MessageAttachment> |
397
|
|
|
*/ |
398
|
|
|
public function getAttachments(): Collection |
399
|
|
|
{ |
400
|
|
|
return $this->attachments; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
public function addAttachment(MessageAttachment $attachment): static |
404
|
|
|
{ |
405
|
|
|
if (!$this->attachments->contains($attachment)) { |
406
|
|
|
$this->attachments->add($attachment); |
407
|
|
|
$attachment |
408
|
|
|
->setMessage($this) |
409
|
|
|
->setParent($this->sender) |
410
|
|
|
->setCreator($this->sender) |
411
|
|
|
; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
return $this; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
public function removeAttachment(MessageAttachment $attachment): static |
418
|
|
|
{ |
419
|
|
|
if ($this->attachments->removeElement($attachment)) { |
420
|
|
|
if ($attachment->getMessage() === $this) { |
421
|
|
|
$attachment->setMessage(null); |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
return $this; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
public function getParent(): ?self |
429
|
|
|
{ |
430
|
|
|
return $this->parent; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
public function setParent(?self $parent): self |
434
|
|
|
{ |
435
|
|
|
$this->parent = $parent; |
436
|
|
|
|
437
|
|
|
return $this; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* @return Collection<int, Message> |
442
|
|
|
*/ |
443
|
|
|
public function getChildren(): Collection |
444
|
|
|
{ |
445
|
|
|
return $this->children; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
public function addChild(self $child): self |
449
|
|
|
{ |
450
|
|
|
$this->children[] = $child; |
451
|
|
|
$child->setParent($this); |
452
|
|
|
|
453
|
|
|
return $this; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
public function getGroup(): ?Usergroup |
457
|
|
|
{ |
458
|
|
|
return $this->group; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
public function setGroup(?Usergroup $group): self |
462
|
|
|
{ |
463
|
|
|
// $this->msgType = self::MESSAGE_TYPE_GROUP; |
464
|
|
|
$this->group = $group; |
465
|
|
|
|
466
|
|
|
return $this; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
public function getStatus(): int |
470
|
|
|
{ |
471
|
|
|
return $this->status; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
public function setStatus(int $status): self |
475
|
|
|
{ |
476
|
|
|
$this->status = $status; |
477
|
|
|
|
478
|
|
|
return $this; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
public function getLikes(): Collection |
482
|
|
|
{ |
483
|
|
|
return $this->likes; |
484
|
|
|
} |
485
|
|
|
} |
486
|
|
|
|