-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathChecklist.php
More file actions
199 lines (178 loc) · 6.96 KB
/
Checklist.php
File metadata and controls
199 lines (178 loc) · 6.96 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
<?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\ChecklistRepository;
use App\State\ChecklistCreateProcessor;
use App\Util\EntityMap;
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\Validator\Constraints as Assert;
/**
* A Checklist
* Tree-Structure with ChecklistItems.
*/
#[ApiResource(
operations: [
new Get(
security: 'is_granted("CHECKLIST_IS_PROTOTYPE", object) or
is_granted("CAMP_IS_PUBLIC", object) or
is_granted("CAMP_COLLABORATOR", object)
'
),
new Patch(
security: '(is_granted("CHECKLIST_IS_PROTOTYPE", object) and is_granted("ROLE_ADMIN")) or
(is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object))
'
),
new Delete(
security: '(is_granted("CHECKLIST_IS_PROTOTYPE", object) and is_granted("ROLE_ADMIN")) or
(is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object))
',
validate: true,
validationContext: ['groups' => ['delete']],
),
new GetCollection(
security: 'is_authenticated()'
),
new Post(
processor: ChecklistCreateProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: '(is_granted("CHECKLIST_IS_PROTOTYPE", object) and is_granted("ROLE_ADMIN")) or
(!is_granted("CHECKLIST_IS_PROTOTYPE", object) and (is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.camp === null))
'
),
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)'
),
],
extraProperties: [
'filter_by_current_user' => false,
]
),
],
denormalizationContext: ['groups' => ['write']],
normalizationContext: ['groups' => ['read']],
order: ['camp.id', 'name'],
)]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['camp', 'isPrototype'])]
#[ORM\Entity(repositoryClass: ChecklistRepository::class)]
class Checklist extends BaseEntity implements BelongsToCampInterface, CopyFromPrototypeInterface {
public const CAMP_SUBRESOURCE_URI_TEMPLATE = '/camps/{campId}/checklists{._format}';
/**
* The camp this checklist belongs to.
*/
#[ApiProperty(example: '/camps/1a2b3c4d')]
#[Groups(['read', 'create'])]
#[Assert\Expression('!(this.isPrototype == true and this.camp != null)', 'This value should be null.')]
#[Assert\Expression('!(this.isPrototype == false and this.camp == null)', 'This value should not be null.')]
#[ORM\ManyToOne(targetEntity: Camp::class, inversedBy: 'checklists')]
#[ORM\JoinColumn(onDelete: 'cascade')]
public ?Camp $camp = null;
/**
* Copy contents from this source checklist.
*/
#[ApiProperty(example: '/checklists/1a2b3c4d')]
#[Groups(['create'])]
public ?Checklist $copyChecklistSource;
/**
* All ChecklistItems that belong to this Checklist.
*/
#[Assert\Valid(groups: ['delete'])]
#[ApiProperty(writable: false, uriTemplate: ChecklistItem::CHECKLIST_SUBRESOURCE_URI_TEMPLATE)]
#[Groups(['read'])]
#[ORM\OneToMany(targetEntity: ChecklistItem::class, mappedBy: 'checklist', cascade: ['persist'])]
public Collection $checklistItems;
/**
* The human readable name of the checklist.
*/
#[ApiProperty(example: 'PBS Ausbildungsziele')]
#[Groups(['read', 'write'])]
#[InputFilter\Trim]
#[InputFilter\CleanText]
#[Assert\NotBlank]
#[Assert\Length(max: 32)]
#[ORM\Column(type: 'text')]
public string $name;
/**
* The id of the checklist that was used as a template for creating this checklist. Internal for now, is
* not published through the API.
*/
#[ORM\Column(type: 'string', length: 16, nullable: true)]
public ?string $checklistPrototypeId = null;
/**
* Whether this checklist is a template.
*/
#[Assert\Type('bool')]
#[Assert\DisableAutoMapping]
#[ApiProperty(example: true, writable: true)]
#[Groups(['read', 'create'])]
#[ORM\Column(type: 'boolean', options: ['default' => false])]
public bool $isPrototype = false;
public function __construct() {
parent::__construct();
$this->checklistItems = new ArrayCollection();
}
public function getCamp(): ?Camp {
return $this->camp;
}
/**
* @return ChecklistItem[]
*/
public function getChecklistItems(): array {
return $this->checklistItems->getValues();
}
public function addChecklistItem(ChecklistItem $checklistItem): self {
if (!$this->checklistItems->contains($checklistItem)) {
$this->checklistItems[] = $checklistItem;
$checklistItem->checklist = $this;
}
return $this;
}
public function removeChecklistItem(ChecklistItem $checklistItem): self {
if ($this->checklistItems->removeElement($checklistItem)) {
// set the owning side to null (unless already changed)
if ($checklistItem->checklist === $this) {
$checklistItem->checklist = null;
}
}
return $this;
}
/**
* @param Checklist $prototype
* @param EntityMap $entityMap
*/
public function copyFromPrototype($prototype, $entityMap): void {
$entityMap->add($prototype, $this);
$this->checklistPrototypeId = $prototype->getId();
// copy Checklist base properties
$this->name ??= $prototype->name;
// deep copy ChecklistItems
foreach ($prototype->getChecklistItems() as $checklistItemPrototype) {
// deep copy root ChecklistItems
// skip non-root ChecklistItems as these are copyed by there parent
if (null == $checklistItemPrototype->parent) {
$checklistItem = new ChecklistItem();
$this->addChecklistItem($checklistItem);
$checklistItem->copyFromPrototype($checklistItemPrototype, $entityMap);
}
}
}
}