-
Notifications
You must be signed in to change notification settings - Fork 53
/
Camp.php
671 lines (589 loc) · 21.8 KB
/
Camp.php
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
<?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\Patch;
use ApiPlatform\Metadata\Post;
use App\InputFilter;
use App\Repository\CampRepository;
use App\Serializer\Normalizer\RelatedCollectionLink;
use App\State\CampCreateProcessor;
use App\State\CampRemoveProcessor;
use App\Util\EntityMap;
use App\Validator\AssertContainsAtLeastOneManager;
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;
/**
* The main entity that eCamp is designed to manage. Contains programme which may be
* distributed across multiple time periods.
*/
#[ApiResource(
operations: [
new Get(
security: 'is_granted("CAMP_COLLABORATOR", object) or is_granted("CAMP_IS_PROTOTYPE", object)',
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
),
new Patch(
security: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
denormalizationContext: ['groups' => ['write', 'update']],
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
),
new Delete(
processor: CampRemoveProcessor::class,
security: 'object.owner == user'
),
new GetCollection(
security: 'is_authenticated()'
),
new Post(
processor: CampCreateProcessor::class,
security: 'is_authenticated()',
validationContext: ['groups' => ['Default', 'create', 'Camp:create']],
denormalizationContext: ['groups' => ['write', 'create']],
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
),
],
denormalizationContext: ['groups' => ['write']],
forceEager: false,
normalizationContext: ['groups' => ['read']]
)]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['isPrototype'])]
#[ORM\Entity(repositoryClass: CampRepository::class)]
#[ORM\Index(columns: ['isPrototype'])]
class Camp extends BaseEntity implements BelongsToCampInterface, CopyFromPrototypeInterface {
public const ITEM_NORMALIZATION_CONTEXT = [
'groups' => ['read', 'Camp:Periods', 'Period:Days', 'Camp:CampCollaborations', 'CampCollaboration:User'],
'swagger_definition_name' => 'read',
];
#[AssertContainsAtLeastOneManager(groups: ['update'])]
#[SerializedName('campCollaborations')]
#[Groups(['read'])]
#[ORM\OneToMany(targetEntity: CampCollaboration::class, mappedBy: 'camp', orphanRemoval: true)]
public Collection $collaborations;
/**
* UserCamp Collections
* Based von view_user_camps; lists all user who can see this camp.
*/
#[ORM\OneToMany(targetEntity: UserCamp::class, mappedBy: 'camp')]
public Collection $userCamps;
/**
* The time periods of the camp, there must be at least one. Periods in a camp may not overlap.
* When creating a camp, the initial periods may be specified as nested payload, but updating,
* adding or removing periods later should be done through the period endpoints.
*/
#[Assert\Valid]
#[Assert\Count(min: 1, groups: ['create'])]
#[Assert\Count(min: 2, minMessage: 'A camp must have at least one period.', groups: ['Period:delete'])]
#[ApiProperty(
writableLink: true,
example: [['description' => 'Hauptlager', 'start' => '2022-01-01', 'end' => '2022-01-08']]
)]
#[Groups(['read', 'create'])]
#[ORM\OneToMany(targetEntity: Period::class, mappedBy: 'camp', orphanRemoval: true, cascade: ['persist'])]
#[ORM\OrderBy(['start' => 'ASC'])]
public Collection $periods;
/**
* Types of programme, such as sports activities or meal times.
*/
#[ApiProperty(
writable: false,
uriTemplate: Category::CAMP_SUBRESOURCE_URI_TEMPLATE,
example: '"/camp/1a2b3c4d/categories"'
)]
#[Groups(['read'])]
#[ORM\OneToMany(targetEntity: Category::class, mappedBy: 'camp', orphanRemoval: true, cascade: ['persist'])]
public Collection $categories;
/**
* All the progress labels within this camp.
*/
#[ApiProperty(writable: false, example: '["/progress_labels/1a2b3c4d"]')]
#[Groups(['read'])]
#[ORM\OneToMany(targetEntity: ActivityProgressLabel::class, mappedBy: 'camp', orphanRemoval: true, cascade: ['persist'])]
public Collection $progressLabels;
/**
* All the programme that will be carried out during the camp. An activity may be carried out
* multiple times in the same camp.
*/
#[ApiProperty(writable: false, example: '/activities?camp=%2Fcamps%2F1a2b3c4d')]
#[Groups(['read'])]
#[ORM\OneToMany(targetEntity: Activity::class, mappedBy: 'camp', orphanRemoval: true)]
public Collection $activities;
/**
* Lists for collecting the required materials needed for carrying out the programme. Each collaborator
* has a material list, and there may be more, such as shopping lists.
*/
#[ApiProperty(writable: false, example: '["/material_lists/1a2b3c4d"]')]
#[Groups(['read'])]
#[ORM\OneToMany(targetEntity: MaterialList::class, mappedBy: 'camp', orphanRemoval: true, cascade: ['persist'])]
public Collection $materialLists;
/**
* List of all Checklists of this Camp.
* Each Checklist is a List of ChecklistItems.
*/
#[ApiProperty(writable: false, uriTemplate: Checklist::CAMP_SUBRESOURCE_URI_TEMPLATE)]
#[Groups(['read'])]
#[ORM\OneToMany(targetEntity: Checklist::class, mappedBy: 'camp', orphanRemoval: true, cascade: ['persist'])]
public Collection $checklists;
/**
* List all CampRootContentNodes of this Camp;
* Calculated by the View view_camp_root_content_node.
*/
#[ORM\OneToMany(targetEntity: CampRootContentNode::class, mappedBy: 'camp')]
public Collection $campRootContentNodes;
/**
* The id of the camp that was used as a template for creating this camp. Internal for now, is
* not published through the API.
*/
#[ORM\Column(type: 'string', length: 16, nullable: true)]
public ?string $campPrototypeId = null;
/**
* The prototype camp that will be used as a template to create this camp.
* Only the ID will be persisted.
*/
#[ApiProperty(readable: false, example: '/camps/1a2b3c4d')]
#[Groups(['create'])]
public ?Camp $campPrototype = null;
/**
* Whether this camp may serve as a template for creating other camps.
*/
#[Assert\Type('bool')]
#[Assert\DisableAutoMapping]
#[ApiProperty(example: true, writable: false)]
#[Groups(['read'])]
#[ORM\Column(type: 'boolean')]
public bool $isPrototype = false;
/**
* A short title for the camp.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 32)]
#[ApiProperty(example: 'SoLa 2022')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $shortTitle;
/**
* The full title of the camp.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\NotBlank]
#[Assert\Length(max: 32)]
#[ApiProperty(example: 'Abteilungs-Sommerlager 2022')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text')]
public string $title;
/**
* The thematic topic (if any) of the camp's programme and storyline.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 128)]
#[ApiProperty(example: 'Piraten')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $motto = null;
/**
* A textual description of the location of the camp.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 128)]
#[ApiProperty(example: 'Wiese hinter der alten Mühle')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $addressName = null;
/**
* The street name and number (if any) of the location of the camp.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 128)]
#[ApiProperty(example: 'Schönriedweg 23')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $addressStreet = null;
/**
* The zipcode of the location of the camp.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 128)]
#[ApiProperty(example: '1234')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $addressZipcode = null;
/**
* The name of the town where the camp will take place.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 128)]
#[ApiProperty(example: 'Hintertüpfingen')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $addressCity = null;
/**
* The name of the organization which plans and carries out the camp.
* Organisator (Name der Jugendorganisation)
* Organisateur (nom de l’organisation de jeunesse)
* Organizzatore (nome dell’organizzazione giovanile)
* This field is required on picassos of Y+S camps.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 64)]
#[ApiProperty(example: 'Pfadi Luftig')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $organizer = null;
/**
* Rough categorization of the camp (house, tent, traveling, summer, autumn).
* Lagerart (Haus-, Zelt-, Unterwegslager, Sommer-, Herbstlager)
* Forme de camp (camp sous toit, camp sous tente, camp itinérant, camp d’été, camp d’automne)
* Tipo di campo (campo sotto tetto, in tenda, itinerante, estivo, autunnale)
* This field is required on picassos of Y+S camps.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 64)]
#[ApiProperty(example: 'Zeltlager')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $kind = null;
/**
* The name of the Y+S coach who is in charge of the camp.
* Name des J+S-Coachs
* Nom du coach J+S
* Nome del coach G+S
* This field is required on picassos of Y+S camps.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 64)]
#[ApiProperty(example: 'Albert Anderegg')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $coachName = null;
/**
* The official course number, identifying this course.
* Kursnummer
* Le numéro du cours
* Numero di corso
* This field is required on picassos of youth organization courses.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 64)]
#[ApiProperty(example: 'PBS AG 123-23')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $courseNumber = null;
/**
* The official name for the type of this course.
* Kursbezeichnung (bei J+S Kurs: J+S Bezeichnung)
* Description (cours de base, d’animation, etc.)
* Nome del corso (in caso di corso G+S: tipo di corso G+S)
* This field is required on picassos of youth organization courses.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 64)]
#[ApiProperty(example: 'PBS AG 123-23')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $courseKind = null;
/**
* The name of the training advisor who is in charge of the course.
* Bürgerlicher Name von LKB
* Le nom d’origine du CàF
* Cognome e nome (non totem) del CaF
* This field is required on picassos of youth organization courses.
*/
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\Length(max: 64)]
#[ApiProperty(example: 'Albert Anderegg')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text', nullable: true)]
public ?string $trainingAdvisorName = null;
/**
* Whether the Y+S logo should be printed on the picasso of this camp.
* J+S-Logo (bei J+S-Kurs)
* Le logo J+S (pour des cours J+S)
* Logo G+S (solamente per corsi G+S)
* The logo is required for Y+S courses.
*/
#[ApiProperty(default: null, example: true)]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'boolean', options: ['default' => false])]
public bool $printYSLogoOnPicasso = false;
/**
* The person that created the camp. This value never changes, even when the person
* leaves the camp.
*/
#[Assert\DisableAutoMapping]
#[ApiProperty(writable: false)]
#[Groups(['read'])]
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: false)]
public ?User $creator = null;
/**
* The single person currently in charge of managing the camp. If this person leaves
* the camp, another collaborator must be appointed as owner.
*/
#[Assert\DisableAutoMapping]
#[ApiProperty(readable: false, writable: false)]
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'ownedCamps')]
#[ORM\JoinColumn(nullable: false)]
public ?User $owner = null;
public function __construct() {
parent::__construct();
$this->collaborations = new ArrayCollection();
$this->userCamps = new ArrayCollection();
$this->periods = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->progressLabels = new ArrayCollection();
$this->activities = new ArrayCollection();
$this->materialLists = new ArrayCollection();
$this->checklists = new ArrayCollection();
$this->campRootContentNodes = new ArrayCollection();
}
/**
* @return Period[]
*/
#[ApiProperty(readableLink: true)]
#[SerializedName('periods')]
#[Groups(['Camp:Periods'])]
public function getEmbeddedPeriods(): array {
return $this->periods->getValues();
}
#[ApiProperty(readable: false)]
public function getCamp(): ?Camp {
return $this;
}
/**
* The people working on planning and carrying out the camp. Only collaborators have access
* to the camp's contents.
*
* @return CampCollaboration[]
*/
#[ApiProperty(writable: false, example: '["/camp_collaborations/1a2b3c4d"]')]
public function getCampCollaborations(): array {
return $this->collaborations->getValues();
}
/**
* The people working on planning and carrying out the camp. Only collaborators have access
* to the camp's contents.
*
* @return CampCollaboration[]
*/
#[ApiProperty(writable: false, readableLink: true)]
#[SerializedName('campCollaborations')]
#[Groups('Camp:CampCollaborations')]
public function getEmbeddedCampCollaborations(): array {
return $this->collaborations->getValues();
}
public function addCampCollaboration(CampCollaboration $collaboration): self {
if (!$this->collaborations->contains($collaboration)) {
$this->collaborations[] = $collaboration;
$collaboration->camp = $this;
}
return $this;
}
public function removeCampCollaboration(CampCollaboration $collaboration): self {
if ($this->collaborations->removeElement($collaboration)) {
if ($collaboration->camp === $this) {
$collaboration->camp = null;
}
}
return $this;
}
/**
* All profiles of the users collaborating in this camp.
*
* @return Profile[]
*/
#[ApiProperty(example: '/profiles?user.collaborations.camp=%2Fcamps%2F1a2b3c4d')]
#[RelatedCollectionLink(Profile::class, ['user.collaborations.camp' => '$this'])]
#[Groups(['read'])]
public function getProfiles(): array {
$accessibleCampCollaborations = array_filter(
$this->getCampCollaborations(),
function (CampCollaboration $campCollaboration) {
return CampCollaboration::STATUS_ESTABLISHED === $campCollaboration->status
&& $campCollaboration->getEmbeddedUser();
}
);
return array_map(function (CampCollaboration $campCollaboration) {
return $campCollaboration->getEmbeddedUser()->getProfile();
}, $accessibleCampCollaborations);
}
/**
* @return Period[]
*/
public function getPeriods(): array {
return $this->periods->getValues();
}
public function addPeriod(Period $period): self {
if (!$this->periods->contains($period)) {
$this->periods[] = $period;
$period->camp = $this;
}
return $this;
}
public function removePeriod(Period $period): self {
if ($this->periods->removeElement($period)) {
if ($period->camp === $this) {
$period->camp = null;
}
}
return $this;
}
/**
* @return Category[]
*/
public function getCategories(): array {
return $this->categories->getValues();
}
public function addCategory(Category $category): self {
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
$category->camp = $this;
}
return $this;
}
public function removeCategory(Category $category): self {
if ($this->categories->removeElement($category)) {
if ($category->camp === $this) {
$category->camp = null;
}
}
return $this;
}
/**
* @return ActivityProgressLabel[]
*/
public function getProgressLabels(): array {
return $this->progressLabels->getValues();
}
public function addProgressLabel(ActivityProgressLabel $progressLabel) {
if (!$this->progressLabels->contains($progressLabel)) {
$this->progressLabels[] = $progressLabel;
$progressLabel->camp = $this;
}
return $this;
}
public function removeProgressLabel(ActivityProgressLabel $progressLabel) {
if ($this->progressLabels->removeElement($progressLabel)) {
if ($progressLabel->camp === $this) {
$progressLabel->camp = null;
}
}
return $this;
}
/**
* @return Activity[]
*/
public function getActivities(): array {
return $this->activities->getValues();
}
public function addActivity(Activity $activity): self {
if (!$this->activities->contains($activity)) {
$this->activities[] = $activity;
$activity->camp = $this;
}
return $this;
}
public function removeActivity(Activity $activity): self {
if ($this->activities->removeElement($activity)) {
if ($activity->camp === $this) {
$activity->camp = null;
}
}
return $this;
}
/**
* @return MaterialList[]
*/
public function getMaterialLists(): array {
return $this->materialLists->getValues();
}
public function addMaterialList(MaterialList $materialList): self {
if (!$this->materialLists->contains($materialList)) {
$this->materialLists[] = $materialList;
$materialList->camp = $this;
}
return $this;
}
public function removeMaterialList(MaterialList $materialList): self {
if ($this->materialLists->removeElement($materialList)) {
if ($materialList->camp === $this) {
$materialList->camp = null;
}
}
return $this;
}
/**
* @return Checklist[]
*/
public function getChecklists(): array {
return $this->checklists->getValues();
}
public function addChecklist(Checklist $checklist): self {
if (!$this->checklists->contains($checklist)) {
$this->checklists[] = $checklist;
$checklist->camp = $this;
}
return $this;
}
public function removeChecklist(Checklist $checklist): self {
if ($this->checklists->removeElement($checklist)) {
if ($checklist->camp === $this) {
$checklist->camp = null;
}
}
return $this;
}
/**
* @param Camp $prototype
* @param EntityMap $entityMap
*/
public function copyFromPrototype($prototype, $entityMap): void {
$entityMap->add($prototype, $this);
$this->campPrototypeId = $prototype->getId();
// copy MaterialLists
foreach ($prototype->getMaterialLists() as $materialListPrototype) {
$materialList = new MaterialList();
$this->addMaterialList($materialList);
$materialList->copyFromPrototype($materialListPrototype, $entityMap);
}
// copy Checklists
foreach ($prototype->getChecklists() as $checklistPrototype) {
$checklist = new Checklist();
$this->addChecklist($checklist);
$checklist->copyFromPrototype($checklistPrototype, $entityMap);
}
// copy Categories
foreach ($prototype->getCategories() as $categoryPrototype) {
$category = new Category();
$this->addCategory($category);
$category->copyFromPrototype($categoryPrototype, $entityMap);
}
// copy ActivityProgressLabels
foreach ($prototype->getProgressLabels() as $progressLabelPrototype) {
$progressLabel = new ActivityProgressLabel();
$this->addProgressLabel($progressLabel);
$progressLabel->copyFromPrototype($progressLabelPrototype, $entityMap);
}
}
}