-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathActivityProgressLabel.php
More file actions
138 lines (127 loc) · 4.74 KB
/
ActivityProgressLabel.php
File metadata and controls
138 lines (127 loc) · 4.74 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
<?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\ActivityProgressLabelRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Progress labels in a camp.
* To each activity one label can be assigned.
*/
#[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_MANAGER", object)',
validationContext: ['groups' => ['Default', 'update']]
),
new Delete(
validate: true,
validationContext: ['groups' => ['delete']],
security: 'is_granted("CAMP_MANAGER", object)'
),
new GetCollection(
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)'
),
],
security: 'is_fully_authenticated()',
extraProperties: [
'filter_by_current_user' => false,
]
),
new Post(
validationContext: ['groups' => ['Default', 'create']],
denormalizationContext: ['groups' => ['write', 'create']],
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
securityPostDenormalize: 'is_granted("CAMP_MANAGER", object) or object.camp === null'
),
],
denormalizationContext: ['groups' => ['write']],
normalizationContext: ['groups' => ['read']],
order: ['camp.id', 'position']
)]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['camp'])]
#[ORM\Entity(repositoryClass: ActivityProgressLabelRepository::class)]
#[ORM\UniqueConstraint(name: 'activity_progress_label_unique', columns: ['campid', 'position'])]
class ActivityProgressLabel extends BaseEntity implements BelongsToCampInterface, CopyFromPrototypeInterface {
public const CAMP_SUBRESOURCE_URI_TEMPLATE = '/camps/{campId}/activity_progress_labels{._format}';
public const ITEM_NORMALIZATION_CONTEXT = [
'groups' => [
'read',
],
'swagger_definition_name' => 'read',
];
/**
* The camp to which this label belongs.
*/
#[ApiProperty(example: '/camps/1a2b3c4d')]
#[Gedmo\SortableGroup]
#[Groups(['read', 'create'])]
#[ORM\ManyToOne(targetEntity: Camp::class, inversedBy: 'progressLabels')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
public ?Camp $camp = null;
/**
* All the programme this progress label is asigned.
*/
#[Assert\Count(
exactly: 0,
exactMessage: 'It\'s not possible to delete a progress label as long as it has an activity linked to it.',
groups: ['delete']
)]
#[ApiProperty(readable: false, writable: false)]
#[ORM\OneToMany(targetEntity: Activity::class, mappedBy: 'progressLabel', orphanRemoval: true)]
public Collection $activities;
#[ApiProperty(example: 0)]
#[Gedmo\SortablePosition]
#[Groups(['read', 'write'])]
#[ORM\Column(name: 'position', type: 'integer', nullable: false)]
public int $position = -1;
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\NotBlank]
#[Assert\Length(max: 32)]
#[ApiProperty(example: 'Planned')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: 'text')]
public ?string $title = null;
public function __construct() {
parent::__construct();
$this->activities = new ArrayCollection();
}
public function getCamp(): ?Camp {
return $this->camp;
}
public function copyFromPrototype($prototype, $entityMap): void {
$entityMap->add($prototype, $this);
$this->position = $prototype->position;
$this->title = $prototype->title;
}
}