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\BooleanFilter; |
10
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter; |
11
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
12
|
|
|
use ApiPlatform\Metadata\Get; |
13
|
|
|
use ApiPlatform\Metadata\GetCollection; |
14
|
|
|
use ApiPlatform\Metadata\Link; |
15
|
|
|
use ApiPlatform\Metadata\Post; |
16
|
|
|
use ApiPlatform\Metadata\Put; |
17
|
|
|
use ApiPlatform\Serializer\Filter\GroupFilter; |
18
|
|
|
use Doctrine\Common\Collections\Collection; |
19
|
|
|
use Doctrine\ORM\Mapping as ORM; |
20
|
|
|
use Gedmo\Timestampable\Traits\TimestampableEntity; |
21
|
|
|
use Gedmo\Mapping\Annotation as Gedmo; |
22
|
|
|
use ProjetNormandie\ForumBundle\Repository\TopicRepository; |
23
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
24
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
25
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
26
|
|
|
|
27
|
|
|
#[ORM\Table(name:'pnf_topic')] |
28
|
|
|
#[ORM\Entity(repositoryClass: TopicRepository::class)] |
29
|
|
|
#[ORM\EntityListeners(["ProjetNormandie\ForumBundle\EventListener\Entity\TopicListener"])] |
30
|
|
|
#[ORM\Index(name: "idx_lib_topic", columns: ["lib_topic"])] |
31
|
|
|
#[ApiResource( |
32
|
|
|
shortName: 'ForumTopic', |
33
|
|
|
order: ['type.position' => 'ASC', 'lastMessage.id' => 'DESC'], |
34
|
|
|
operations: [ |
35
|
|
|
new GetCollection( |
36
|
|
|
normalizationContext: ['groups' => |
37
|
|
|
['topic:read', 'topic:last-message', 'message:read', 'topic:forum', 'forum:read', 'topic:type', 'topic-type:read'] |
38
|
|
|
] |
39
|
|
|
), |
40
|
|
|
new Get(), |
41
|
|
|
new Post( |
42
|
|
|
denormalizationContext: ['groups' => ['topic:insert', 'message:insert']], |
43
|
|
|
normalizationContext: ['groups' => |
44
|
|
|
['topic:read', 'topic:forum', 'forum:read'] |
45
|
|
|
], |
46
|
|
|
security: 'is_granted("ROLE_USER")', |
47
|
|
|
), |
48
|
|
|
new Put( |
49
|
|
|
security: 'is_granted("ROLE_USER") and object.getUser() == user', |
50
|
|
|
) |
51
|
|
|
], |
52
|
|
|
normalizationContext: ['groups' => ['topic:read', 'topic:type', 'topic-type:read']], |
53
|
|
|
)] |
54
|
|
|
#[ApiResource( |
55
|
|
|
shortName: 'ForumTopic', |
56
|
|
|
uriTemplate: '/forum_forums/{id}/topics', |
57
|
|
|
uriVariables: [ |
58
|
|
|
'id' => new Link(fromClass: Forum::class, toProperty: 'forum'), |
59
|
|
|
], |
60
|
|
|
operations: [ new GetCollection() ], |
61
|
|
|
normalizationContext: ['groups' => ['forum:read']], |
62
|
|
|
)] |
63
|
|
|
#[ApiFilter( |
64
|
|
|
SearchFilter::class, |
65
|
|
|
properties: [ |
66
|
|
|
'libTopic' => 'partial', |
67
|
|
|
'forum' => 'exact', |
68
|
|
|
'forum.status' => 'exact', |
69
|
|
|
'topicUser.user' => 'exact', |
70
|
|
|
'topicUser.boolNotif' => 'exact' |
71
|
|
|
] |
72
|
|
|
)] |
73
|
|
|
#[ApiFilter( |
74
|
|
|
OrderFilter::class, |
75
|
|
|
properties: [ |
76
|
|
|
'lastMessage.id' => 'DESC', |
77
|
|
|
] |
78
|
|
|
)] |
79
|
|
|
#[ApiFilter( |
80
|
|
|
GroupFilter::class, |
81
|
|
|
arguments: [ |
82
|
|
|
'parameterName' => 'groups', |
83
|
|
|
'overrideDefaultGroups' => true, |
84
|
|
|
'whitelist' => [ |
85
|
|
|
'forum:read', |
86
|
|
|
'topic:read', |
87
|
|
|
'topic:type', |
88
|
|
|
'topic-type:read', |
89
|
|
|
'topic:forum', |
90
|
|
|
'topic:user', |
91
|
|
|
'topic:last-message', |
92
|
|
|
'topic-user:read', |
93
|
|
|
'topic:topic-user-1', |
94
|
|
|
'message:read', |
95
|
|
|
'message:user', |
96
|
|
|
'user:read' |
97
|
|
|
] |
98
|
|
|
] |
99
|
|
|
)] |
100
|
|
|
#[ApiFilter(BooleanFilter::class, properties: ['boolArchive'])] |
101
|
|
|
class Topic |
102
|
|
|
{ |
103
|
|
|
use TimestampableEntity; |
104
|
|
|
|
105
|
|
|
#[Groups(['topic:read'])] |
106
|
|
|
#[ORM\Id, ORM\Column, ORM\GeneratedValue] |
107
|
|
|
private ?int $id = null; |
108
|
|
|
|
109
|
|
|
#[Groups(['topic:read', 'topic:insert'])] |
110
|
|
|
#[Assert\NotNull] |
111
|
|
|
#[Assert\NotBlank] |
112
|
|
|
#[Assert\Length(min:3, max: 255)] |
113
|
|
|
#[ORM\Column(length: 255, nullable: false)] |
114
|
|
|
private string $libTopic; |
115
|
|
|
|
116
|
|
|
#[Groups(['topic:read'])] |
117
|
|
|
#[ORM\Column(nullable: false, options: ['default' => 0])] |
118
|
|
|
private int $nbMessage = 0; |
119
|
|
|
|
120
|
|
|
#[Groups(['topic:forum', 'topic:insert'])] |
121
|
|
|
#[ORM\ManyToOne(targetEntity: Forum::class, inversedBy: 'topics')] |
122
|
|
|
#[ORM\JoinColumn(name:'forum_id', referencedColumnName:'id', nullable:false)] |
123
|
|
|
private Forum $forum; |
124
|
|
|
|
125
|
|
|
#[Groups(['topic:user'])] |
126
|
|
|
#[ORM\ManyToOne(targetEntity: UserInterface::class)] |
127
|
|
|
#[ORM\JoinColumn(name:'user_id', referencedColumnName:'id', nullable:false)] |
128
|
|
|
private $user; |
129
|
|
|
|
130
|
|
|
#[ORM\Column(length: 255)] |
131
|
|
|
#[Gedmo\Slug(fields: ['libTopic'])] |
132
|
|
|
protected string $slug; |
133
|
|
|
|
134
|
|
|
#[Groups(['topic:type', 'topic:insert'])] |
135
|
|
|
#[ORM\ManyToOne(targetEntity: TopicType::class)] |
136
|
|
|
#[ORM\JoinColumn(name:'type_id', referencedColumnName:'id', nullable:false)] |
137
|
|
|
private TopicType $type; |
138
|
|
|
|
139
|
|
|
#[Groups(['topic:insert'])] |
140
|
|
|
#[ORM\OneToMany(targetEntity: Message::class, mappedBy: 'topic', cascade: ['persist'])] |
141
|
|
|
private Collection $messages; |
142
|
|
|
|
143
|
|
|
#[Groups(['topic:last-message'])] |
144
|
|
|
#[ORM\ManyToOne(targetEntity: Message::class, cascade: ['persist'])] |
145
|
|
|
#[ORM\JoinColumn(name:'max_message_id', referencedColumnName:'id', nullable:true, onDelete: 'SET NULL')] |
146
|
|
|
private ?Message $lastMessage; |
147
|
|
|
|
148
|
|
|
#[ORM\Column(nullable: false, options: ['default' => false])] |
149
|
|
|
private bool $boolArchive = false; |
150
|
|
|
|
151
|
|
|
#[ORM\OneToMany(targetEntity: TopicUser::class, mappedBy: 'topic')] |
152
|
|
|
private Collection $topicUser; |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
public function __toString() |
156
|
|
|
{ |
157
|
|
|
return sprintf('%s [%s]', $this->getLibTopic(), $this->getId()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
public function __construct() |
162
|
|
|
{ |
163
|
|
|
$this->messages = new ArrayCollection(); |
164
|
|
|
$this->topicUser = new ArrayCollection(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function setId(int $id): void |
168
|
|
|
{ |
169
|
|
|
$this->id = $id; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getId(): ?int |
173
|
|
|
{ |
174
|
|
|
return $this->id; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function setLibTopic(string $libTopic): void |
178
|
|
|
{ |
179
|
|
|
$this->libTopic = $libTopic; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function getLibTopic(): string |
183
|
|
|
{ |
184
|
|
|
return $this->libTopic; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function setNbMessage(int $nbMessage): void |
188
|
|
|
{ |
189
|
|
|
$this->nbMessage = $nbMessage; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getNbMessage(): int |
193
|
|
|
{ |
194
|
|
|
return $this->nbMessage; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function setForum(Forum $forum): void |
198
|
|
|
{ |
199
|
|
|
$this->forum = $forum; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function getForum(): Forum |
203
|
|
|
{ |
204
|
|
|
return $this->forum; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function setUser($user): void |
208
|
|
|
{ |
209
|
|
|
$this->user = $user; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function getUser() |
213
|
|
|
{ |
214
|
|
|
return $this->user; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function getSlug(): string |
218
|
|
|
{ |
219
|
|
|
return $this->slug; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function setType(TopicType $type): void |
223
|
|
|
{ |
224
|
|
|
$this->type = $type; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function getType(): TopicType |
228
|
|
|
{ |
229
|
|
|
return $this->type; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function setMessages(array $messages): void |
233
|
|
|
{ |
234
|
|
|
foreach ($messages as $message) { |
235
|
|
|
$this->addMessage($message); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function addMessage(Message $message): void |
240
|
|
|
{ |
241
|
|
|
$message->setTopic($this); |
242
|
|
|
$this->messages[] = $message; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function getMessages(): Collection |
246
|
|
|
{ |
247
|
|
|
return $this->messages; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function setLastMessage(?Message $message = null): void |
251
|
|
|
{ |
252
|
|
|
$this->lastMessage = $message; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function getLastMessage(): ?Message |
256
|
|
|
{ |
257
|
|
|
return $this->lastMessage; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function setBoolArchive(bool $boolArchive): void |
261
|
|
|
{ |
262
|
|
|
$this->boolArchive = $boolArchive; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function getBoolArchive(): bool |
266
|
|
|
{ |
267
|
|
|
return $this->boolArchive; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function getTopicUser(): Collection |
271
|
|
|
{ |
272
|
|
|
return $this->topicUser; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
#[Groups(['topic:topic-user-1'])] |
276
|
|
|
public function getTopicUser1(): TopicUser |
277
|
|
|
{ |
278
|
|
|
return $this->topicUser[0]; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function getUrl(): string |
282
|
|
|
{ |
283
|
|
|
return sprintf( |
284
|
|
|
'%s-forum-f%d/%s-topic-t%d/index', |
285
|
|
|
$this->getForum()->getSlug(), |
286
|
|
|
$this->getForum()->getId(), |
287
|
|
|
$this->getSlug(), |
288
|
|
|
$this->getId() |
289
|
|
|
); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|