|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProjetNormandie\ForumBundle\Entity; |
|
6
|
|
|
|
|
7
|
|
|
use ApiPlatform\Metadata\ApiResource; |
|
8
|
|
|
use ApiPlatform\Metadata\ApiFilter; |
|
9
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter; |
|
10
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
|
11
|
|
|
use ApiPlatform\Metadata\Get; |
|
12
|
|
|
use ApiPlatform\Metadata\GetCollection; |
|
13
|
|
|
use ApiPlatform\Metadata\Link; |
|
14
|
|
|
use ApiPlatform\Metadata\Post; |
|
15
|
|
|
use ApiPlatform\Metadata\Put; |
|
16
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
17
|
|
|
use Gedmo\Timestampable\Traits\TimestampableEntity; |
|
18
|
|
|
use ProjetNormandie\ForumBundle\Repository\MessageRepository; |
|
19
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
|
20
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
21
|
|
|
|
|
22
|
|
|
#[ORM\Table(name:'pnf_message')] |
|
23
|
|
|
#[ORM\Entity(repositoryClass: MessageRepository::class)] |
|
24
|
|
|
#[ORM\EntityListeners(["ProjetNormandie\ForumBundle\EventListener\Entity\MessageListener"])] |
|
25
|
|
|
#[ApiResource( |
|
26
|
|
|
shortName: 'ForumMessage', |
|
27
|
|
|
order: ['id' => 'ASC'], |
|
28
|
|
|
operations: [ |
|
29
|
|
|
new GetCollection( |
|
30
|
|
|
normalizationContext: ['groups' => ['message:read', 'message:message', 'message:user', 'user:read']] |
|
31
|
|
|
), |
|
32
|
|
|
new Get(), |
|
33
|
|
|
new Post( |
|
34
|
|
|
denormalizationContext: ['groups' => ['message:insert']], |
|
35
|
|
|
security: 'is_granted("ROLE_USER")', |
|
36
|
|
|
), |
|
37
|
|
|
new Put( |
|
38
|
|
|
denormalizationContext: ['groups' => ['message:update']], |
|
39
|
|
|
security: 'is_granted("ROLE_USER") and object.getUser() == user', |
|
40
|
|
|
), |
|
41
|
|
|
], |
|
42
|
|
|
normalizationContext: ['groups' => ['message:read', 'message:message']] |
|
43
|
|
|
)] |
|
44
|
|
|
#[ApiResource( |
|
45
|
|
|
shortName: 'ForumMessage', |
|
46
|
|
|
uriTemplate: '/forum_topics/{id}/messages', |
|
47
|
|
|
uriVariables: [ |
|
48
|
|
|
'id' => new Link(fromClass: Topic::class, toProperty: 'topic'), |
|
49
|
|
|
], |
|
50
|
|
|
operations: [ new GetCollection() ], |
|
51
|
|
|
normalizationContext: ['groups' => ['message:read', 'message:message', 'message:user', 'user:read:minimal']], |
|
52
|
|
|
)] |
|
53
|
|
|
#[ApiFilter( |
|
54
|
|
|
SearchFilter::class, |
|
55
|
|
|
properties: [ |
|
56
|
|
|
'topic' => 'exact', |
|
57
|
|
|
'user' => 'exact', |
|
58
|
|
|
'topic.forum.status' => 'exact', |
|
59
|
|
|
] |
|
60
|
|
|
)] |
|
61
|
|
|
#[ApiFilter( |
|
62
|
|
|
OrderFilter::class, |
|
63
|
|
|
properties: [ |
|
64
|
|
|
'id' => 'ASC', |
|
65
|
|
|
'createdAt' => 'ASC', |
|
66
|
|
|
] |
|
67
|
|
|
)] |
|
68
|
|
|
class Message |
|
69
|
|
|
{ |
|
70
|
|
|
use TimestampableEntity; |
|
71
|
|
|
|
|
72
|
|
|
#[Groups(['message:read', 'message:update'])] |
|
73
|
|
|
#[ORM\Id, ORM\Column, ORM\GeneratedValue] |
|
74
|
|
|
private ?int $id = null; |
|
75
|
|
|
|
|
76
|
|
|
#[Groups(['message:message', 'message:insert', 'message:update'])] |
|
77
|
|
|
#[Assert\NotNull] |
|
78
|
|
|
#[Assert\NotBlank] |
|
79
|
|
|
#[ORM\Column(type: 'text', nullable: false)] |
|
80
|
|
|
private string $message; |
|
81
|
|
|
|
|
82
|
|
|
#[Groups(['message:read'])] |
|
83
|
|
|
#[ORM\Column(nullable: false, options: ['default' => 1])] |
|
84
|
|
|
private int $position = 1; |
|
85
|
|
|
|
|
86
|
|
|
#[Groups(['message:topic', 'message:insert'])] |
|
87
|
|
|
#[ORM\ManyToOne(targetEntity: Topic::class, inversedBy: 'messages')] |
|
88
|
|
|
#[ORM\JoinColumn(name:'topic_id', referencedColumnName:'id', nullable:false)] |
|
89
|
|
|
private Topic $topic; |
|
90
|
|
|
|
|
91
|
|
|
#[Groups(['message:user'])] |
|
92
|
|
|
#[ORM\ManyToOne(targetEntity: UserInterface::class)] |
|
93
|
|
|
#[ORM\JoinColumn(name:'user_id', referencedColumnName:'id', nullable:false)] |
|
94
|
|
|
private $user; |
|
95
|
|
|
|
|
96
|
|
|
public function __toString() |
|
97
|
|
|
{ |
|
98
|
|
|
return sprintf('[%s]', $this->getId()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function setId(int $id): void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->id = $id; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function getId(): ?int |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->id; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function setMessage(string $message): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->message = $message; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getMessage(): string |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->message; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function setTopic(Topic $topic): void |
|
122
|
|
|
{ |
|
123
|
|
|
$this->topic = $topic; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getTopic(): Topic |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->topic; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function setUser($user): void |
|
132
|
|
|
{ |
|
133
|
|
|
$this->user = $user; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function getUser() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->user; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function setPosition(int $position): void |
|
142
|
|
|
{ |
|
143
|
|
|
$this->position = $position; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function getPosition(): int |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->position; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getPage(): int |
|
152
|
|
|
{ |
|
153
|
|
|
return (int) floor(($this->getPosition() - 1) / 20) + 1; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function getUrl(): string |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->getTopic()->getUrl() . '?page=' . $this->getPage() . '#' . $this->getId(); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|