-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathActivity.php
More file actions
328 lines (293 loc) · 11.2 KB
/
Activity.php
File metadata and controls
328 lines (293 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
namespace App\Entity;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\InputFilter;
use App\Repository\ActivityRepository;
use App\State\ActivityCreateProcessor;
use App\State\ActivityRemoveProcessor;
use App\Validator\AssertBelongsToSameCamp;
use App\Validator\AssertLastCollectionItemIsNotDeleted;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;
/**
* A piece of programme that will be carried out once or multiple times in a camp.
*/
#[ApiResource(
operations: [
new Get(
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
security: 'is_granted("CAMP_COLLABORATOR", object) or
is_granted("CAMP_IS_PUBLIC", object)'
),
new Patch(
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
security: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
validationContext: ['groups' => ['Default', 'update']]
),
new Delete(
processor: ActivityRemoveProcessor::class,
security: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
),
new GetCollection(
normalizationContext: self::COLLECTION_NORMALIZATION_CONTEXT,
security: 'is_authenticated()'
),
new GetCollection(
uriTemplate: self::CAMP_SUBRESOURCE_URI_TEMPLATE,
uriVariables: [
'campId' => new Link(
toProperty: 'camp',
fromClass: Camp::class,
security: 'is_granted("CAMP_COLLABORATOR", camp) or
is_granted("CAMP_IS_PUBLIC", camp)'
),
],
normalizationContext: self::COLLECTION_NORMALIZATION_CONTEXT,
security: 'is_fully_authenticated()',
extraProperties: [
'filter_by_current_user' => false,
]
),
new Post(
processor: ActivityCreateProcessor::class,
validationContext: ['groups' => ['Default', 'create']],
denormalizationContext: ['groups' => ['write', 'create']],
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.category === null'
),
],
denormalizationContext: ['groups' => ['write']],
normalizationContext: ['groups' => ['read']]
)]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['camp'])]
#[ORM\Entity(repositoryClass: ActivityRepository::class)]
class Activity extends BaseEntity implements BelongsToCampInterface {
use HasRootContentNodeTrait;
public const CAMP_SUBRESOURCE_URI_TEMPLATE = '/camps/{campId}/activities{._format}';
public const COLLECTION_NORMALIZATION_CONTEXT = [
'groups' => [
'read',
'Activity:ActivityProgressLabel',
'Activity:ActivityResponsibles',
'Activity:ScheduleEntries',
],
];
public const ITEM_NORMALIZATION_CONTEXT = [
'groups' => [
'read',
'Activity:Category',
'Activity:ActivityProgressLabel',
'Activity:ActivityResponsibles',
'Activity:ScheduleEntries',
'Activity:ContentNodes',
],
'swagger_definition_name' => 'read',
];
/**
* The list of points in time when this activity's programme will be carried out.
*/
#[Assert\Valid]
#[AssertLastCollectionItemIsNotDeleted(groups: ['ScheduleEntry:delete'], message: 'Cannot delete the last schedule entry.')]
#[Assert\Count(min: 1, groups: ['create'])]
#[ApiProperty(
writableLink: true,
example: [['period' => '/periods/1a2b3c4a', 'start' => '2023-05-01T15:00:00+00:00', 'end' => '2023-05-01T16:00:00+00:00']],
)]
#[Groups(['read', 'create'])]
#[ORM\OneToMany(targetEntity: ScheduleEntry::class, mappedBy: 'activity', orphanRemoval: true, cascade: ['persist'])]
#[ORM\OrderBy(['startOffset' => 'ASC', 'left' => 'ASC', 'endOffset' => 'DESC', 'id' => 'ASC'])]
public Collection $scheduleEntries;
/**
* The camp to which this activity belongs.
*/
#[Assert\DisableAutoMapping] // camp is set in the data processor
#[ApiProperty(writable: false, example: '/camps/1a2b3c4d')]
#[Groups(['read'])]
#[ORM\ManyToOne(targetEntity: Camp::class, inversedBy: 'activities')]
#[ORM\JoinColumn(nullable: false, onDelete: 'cascade')]
public ?Camp $camp = null;
/**
* The category to which this activity belongs. The category determines color and numbering scheme
* of the activity, and is used for marking similar activities. Must be in the same camp as the activity.
*/
#[ApiProperty(example: '/categories/1a2b3c4d')]
#[AssertBelongsToSameCamp(groups: ['update'])]
#[Groups(['read', 'write'])]
#[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'activities')]
#[ORM\JoinColumn(nullable: false)]
public ?Category $category = null;
/**
* Copy contents from this source activity.
*/
#[ApiProperty(example: '/activities/1a2b3c4d')]
#[Groups(['create'])]
public ?Activity $copyActivitySource;
/**
* The current assigned ProgressLabel.
*/
#[ApiProperty(example: '/progress_labels/1a2b3c4d')]
#[AssertBelongsToSameCamp(groups: ['update'])]
#[Groups(['read', 'write'])]
#[ORM\ManyToOne(targetEntity: ActivityProgressLabel::class, inversedBy: 'activities')]
#[ORM\JoinColumn(nullable: true)]
public ?ActivityProgressLabel $progressLabel = null;
/**
* The title of this activity that is shown in the picasso.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\NotBlank]
#[Assert\Length(max: 32)]
#[ApiProperty(example: 'Sportolympiade')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text')]
public ?string $title = null;
/**
* The physical location where this activity's programme will be carried out.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 64)]
#[ApiProperty(example: 'Spielwiese')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text')]
public string $location = '';
/**
* All comments of the activity.
*/
#[ApiProperty(
writable: false,
uriTemplate: Comment::ACTIVITY_SUBRESOURCE_URI_TEMPLATE,
example: '/activities/1a2b3c4d/comments'
)]
#[Groups(['read'])]
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'activity')]
public Collection $comments;
/**
* The list of people that are responsible for planning or carrying out this activity.
*/
#[ApiProperty(writable: false)]
#[Groups(['read'])]
#[ORM\OneToMany(targetEntity: ActivityResponsible::class, mappedBy: 'activity', orphanRemoval: true)]
private Collection $activityResponsibles;
public function __construct() {
parent::__construct();
$this->scheduleEntries = new ArrayCollection();
$this->activityResponsibles = new ArrayCollection();
$this->comments = new ArrayCollection();
}
public function getCamp(): ?Camp {
return $this->camp ?? $this->category?->camp;
}
#[ApiProperty(readableLink: true)]
#[SerializedName('category')]
#[Groups('Activity:Category')]
public function getEmbeddedCategory(): ?Category {
return $this->category;
}
#[ApiProperty(readableLink: true)]
#[SerializedName('progressLabel')]
#[Groups('Activity:ActivityProgressLabel')]
public function getEmbeddedProgressLabel(): ?ActivityProgressLabel {
return $this->progressLabel;
}
/**
* @return ContentNode[]
*/
#[ApiProperty(readableLink: true)]
#[SerializedName('contentNodes')]
#[Groups(['Activity:ContentNodes'])]
public function getEmbeddedContentNodes(): array {
return $this->getContentNodes();
}
/**
* @return ScheduleEntry[]
*/
#[ApiProperty(readableLink: true)]
#[SerializedName('scheduleEntries')]
#[Groups(['Activity:ScheduleEntries'])]
public function getEmbeddedScheduleEntries(): array {
return $this->scheduleEntries->getValues();
}
/**
* @return ScheduleEntry[]
*/
public function getScheduleEntries(): array {
return $this->scheduleEntries->getValues();
}
public function addScheduleEntry(ScheduleEntry $scheduleEntry): self {
if (!$this->scheduleEntries->contains($scheduleEntry)) {
$this->scheduleEntries[] = $scheduleEntry;
$scheduleEntry->activity = $this;
}
return $this;
}
public function removeScheduleEntry(ScheduleEntry $scheduleEntry): self {
if ($this->scheduleEntries->removeElement($scheduleEntry)) {
if ($scheduleEntry->activity === $this) {
$scheduleEntry->activity = null;
}
}
return $this;
}
/**
* @return ActivityResponsible[]
*/
#[ApiProperty(readableLink: true, security: '!is_granted("CAMP_COLLABORATOR", object)')]
#[SerializedName('activityResponsibles')]
#[Groups(['Activity:ActivityResponsibles'])]
public function getRedactedEmbeddedActivityResponsibles(): array {
return [];
}
/**
* @return ActivityResponsible[]
*/
#[ApiProperty(readableLink: true, security: 'is_granted("CAMP_COLLABORATOR", object)')]
#[SerializedName('activityResponsibles')]
#[Groups(['Activity:ActivityResponsibles'])]
public function getEmbeddedActivityResponsibles(): array {
return $this->getActivityResponsibles();
}
/**
* @return ActivityResponsible[]
*/
public function getActivityResponsibles(): array {
return $this->activityResponsibles->getValues();
}
public function addActivityResponsible(ActivityResponsible $activityResponsible): self {
if (!$this->activityResponsibles->contains($activityResponsible)) {
$this->activityResponsibles[] = $activityResponsible;
$activityResponsible->activity = $this;
}
return $this;
}
public function removeActivityResponsible(ActivityResponsible $activityResponsible): self {
if ($this->activityResponsibles->removeElement($activityResponsible)) {
if ($activityResponsible->activity === $this) {
$activityResponsible->activity = null;
}
}
return $this;
}
public function removeComment(Comment $comment): self {
if ($this->comments->removeElement($comment)) {
if ($comment->activity === $this) {
$comment->activity = null;
}
}
return $this;
}
}