1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CourseBundle\Entity; |
8
|
|
|
|
9
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter; |
10
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
11
|
|
|
use ApiPlatform\Metadata\ApiFilter; |
12
|
|
|
use ApiPlatform\Metadata\ApiResource; |
13
|
|
|
use ApiPlatform\Metadata\Delete; |
14
|
|
|
use ApiPlatform\Metadata\Get; |
15
|
|
|
use ApiPlatform\Metadata\GetCollection; |
16
|
|
|
use ApiPlatform\Metadata\Patch; |
17
|
|
|
use ApiPlatform\Metadata\Post; |
18
|
|
|
use Chamilo\CoreBundle\Entity\User; |
19
|
|
|
use Chamilo\CoreBundle\State\CBlogAssignAuthorProcessor; |
20
|
|
|
use DateTime; |
21
|
|
|
use Doctrine\ORM\Mapping as ORM; |
22
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
23
|
|
|
|
24
|
|
|
#[ApiResource( |
25
|
|
|
operations: [ |
26
|
|
|
new Get(), |
27
|
|
|
new GetCollection(), |
28
|
|
|
new Post( |
29
|
|
|
security: "is_granted('ROLE_USER')", |
30
|
|
|
processor: CBlogAssignAuthorProcessor::class |
31
|
|
|
), |
32
|
|
|
new Patch(security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_TEACHER') or (object.getAuthor() != null and object.getAuthor() === user)"), |
33
|
|
|
new Delete(security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_TEACHER') or (object.getAuthor() != null and object.getAuthor() === user)"), |
34
|
|
|
], |
35
|
|
|
normalizationContext: ['groups' => ['blog_comment:read']], |
36
|
|
|
denormalizationContext: ['groups' => ['blog_comment:write']], |
37
|
|
|
paginationEnabled: true |
38
|
|
|
)] |
39
|
|
|
#[ApiFilter(SearchFilter::class, properties: [ |
40
|
|
|
'post' => 'exact', |
41
|
|
|
])] |
42
|
|
|
#[ApiFilter(OrderFilter::class, properties: [ |
43
|
|
|
'dateCreation' => 'DESC', |
44
|
|
|
])] |
45
|
|
|
#[ORM\Table(name: 'c_blog_comment')] |
46
|
|
|
#[ORM\Entity] |
47
|
|
|
#[ORM\HasLifecycleCallbacks] |
48
|
|
|
class CBlogComment |
49
|
|
|
{ |
50
|
|
|
#[Groups(['blog_comment:read'])] |
51
|
|
|
#[ORM\Column(name: 'iid', type: 'integer')] |
52
|
|
|
#[ORM\Id] |
53
|
|
|
#[ORM\GeneratedValue] |
54
|
|
|
protected ?int $iid = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* LEGACY: required non-null column kept for backwards compatibility. |
58
|
|
|
* We set 0 by default so inserts don't fail. |
59
|
|
|
*/ |
60
|
|
|
#[Groups(['blog_comment:read'])] |
61
|
|
|
#[ORM\Column(name: 'comment_id', type: 'integer', nullable: false, options: ['default' => 0])] |
62
|
|
|
protected int $commentId = 0; |
63
|
|
|
|
64
|
|
|
#[Groups(['blog_comment:read','blog_comment:write'])] |
65
|
|
|
#[ORM\Column(name: 'title', type: 'string', length: 250, nullable: false)] |
66
|
|
|
protected string $title = ''; |
67
|
|
|
|
68
|
|
|
#[Groups(['blog_comment:read','blog_comment:write'])] |
69
|
|
|
#[ORM\Column(name: 'comment', type: 'text', nullable: false)] |
70
|
|
|
protected string $comment; |
71
|
|
|
|
72
|
|
|
#[Groups(['blog_comment:read'])] |
73
|
|
|
#[ORM\ManyToOne(targetEntity: User::class)] |
74
|
|
|
#[ORM\JoinColumn(name: 'author_id', referencedColumnName: 'id', onDelete: 'CASCADE')] |
75
|
|
|
protected ?User $author = null; |
76
|
|
|
|
77
|
|
|
#[Groups(['blog_comment:read'])] |
78
|
|
|
#[ORM\Column(name: 'date_creation', type: 'datetime', nullable: false)] |
79
|
|
|
protected DateTime $dateCreation; |
80
|
|
|
|
81
|
|
|
#[Groups(['blog_comment:read','blog_comment:write'])] |
82
|
|
|
#[ORM\ManyToOne(targetEntity: CBlog::class)] |
83
|
|
|
#[ORM\JoinColumn(name: 'blog_id', referencedColumnName: 'iid', onDelete: 'CASCADE')] |
84
|
|
|
protected ?CBlog $blog = null; |
85
|
|
|
|
86
|
|
|
/** Real relation to the blog post (keeps DB column name post_id). */ |
87
|
|
|
#[Groups(['blog_comment:read','blog_comment:write'])] |
88
|
|
|
#[ORM\ManyToOne(targetEntity: CBlogPost::class)] |
89
|
|
|
#[ORM\JoinColumn(name: 'post_id', referencedColumnName: 'iid', nullable: false, onDelete: 'CASCADE')] |
90
|
|
|
protected ?CBlogPost $post = null; |
91
|
|
|
|
92
|
|
|
/** Optional: parent comment (threading). Kept nullable, matches parent_comment_id column. */ |
93
|
|
|
#[Groups(['blog_comment:read','blog_comment:write'])] |
94
|
|
|
#[ORM\ManyToOne(targetEntity: self::class)] |
95
|
|
|
#[ORM\JoinColumn(name: 'parent_comment_id', referencedColumnName: 'iid', nullable: true, onDelete: 'CASCADE')] |
96
|
|
|
protected ?CBlogComment $parentComment = null; |
97
|
|
|
|
98
|
|
|
public function __construct() |
99
|
|
|
{ |
100
|
|
|
$this->dateCreation = new DateTime(); |
101
|
|
|
// $this->title already defaults to '' |
102
|
|
|
// $this->commentId already defaults to 0 (legacy) |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
#[ORM\PrePersist] |
106
|
|
|
public function ensureRequiredFields(): void |
107
|
|
|
{ |
108
|
|
|
if (!isset($this->commentId)) { |
109
|
|
|
$this->commentId = 0; // legacy default |
110
|
|
|
} |
111
|
|
|
if (!isset($this->dateCreation)) { |
112
|
|
|
$this->dateCreation = new DateTime(); |
113
|
|
|
} |
114
|
|
|
if (!isset($this->title)) { |
115
|
|
|
$this->title = ''; |
116
|
|
|
} |
117
|
|
|
if ($this->blog === null && $this->post instanceof CBlogPost) { |
118
|
|
|
$this->blog = $this->post->getBlog(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getIid(): ?int |
123
|
|
|
{ |
124
|
|
|
return $this->iid; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getCommentId(): int |
128
|
|
|
{ |
129
|
|
|
return $this->commentId; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function setCommentId(int $commentId): self |
133
|
|
|
{ |
134
|
|
|
$this->commentId = $commentId; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getTitle(): string |
140
|
|
|
{ |
141
|
|
|
return $this->title; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function setTitle(string $title): self |
145
|
|
|
{ |
146
|
|
|
$this->title = $title; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getComment(): string |
152
|
|
|
{ |
153
|
|
|
return $this->comment; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function setComment(string $comment): self |
157
|
|
|
{ |
158
|
|
|
$this->comment = $comment; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getAuthor(): ?User |
164
|
|
|
{ |
165
|
|
|
return $this->author; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function setAuthor(?User $author): self |
169
|
|
|
{ |
170
|
|
|
$this->author = $author; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getDateCreation(): DateTime |
176
|
|
|
{ |
177
|
|
|
return $this->dateCreation; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function setDateCreation(DateTime $dateCreation): self |
181
|
|
|
{ |
182
|
|
|
$this->dateCreation = $dateCreation; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function getBlog(): ?CBlog |
188
|
|
|
{ |
189
|
|
|
return $this->blog; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function setBlog(?CBlog $blog): self |
193
|
|
|
{ |
194
|
|
|
$this->blog = $blog; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function getPost(): ?CBlogPost |
200
|
|
|
{ |
201
|
|
|
return $this->post; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function setPost(?CBlogPost $post): self |
205
|
|
|
{ |
206
|
|
|
$this->post = $post; |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getParentComment(): ?CBlogComment |
212
|
|
|
{ |
213
|
|
|
return $this->parentComment; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function setParentComment(?CBlogComment $parentComment): self |
217
|
|
|
{ |
218
|
|
|
$this->parentComment = $parentComment; |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
#[Groups(['blog_comment:read'])] |
224
|
|
|
public function getAuthorInfo(): array |
225
|
|
|
{ |
226
|
|
|
$u = $this->getAuthor(); |
227
|
|
|
if (!$u) { |
228
|
|
|
return ['id' => null, 'name' => '—']; |
229
|
|
|
} |
230
|
|
|
$name = method_exists($u, 'getFullName') ? $u->getFullName() |
231
|
|
|
: (method_exists($u, 'getUsername') ? $u->getUsername() : 'User'); |
232
|
|
|
|
233
|
|
|
return [ |
234
|
|
|
'id' => method_exists($u, 'getId') ? $u->getId() : null, |
235
|
|
|
'name' => $name, |
236
|
|
|
]; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|