-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathChecklistNode.php
More file actions
110 lines (99 loc) · 3.95 KB
/
ChecklistNode.php
File metadata and controls
110 lines (99 loc) · 3.95 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
<?php
namespace App\Entity\ContentNode;
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\Entity\ChecklistItem;
use App\Entity\ContentNode;
use App\Repository\ChecklistNodeRepository;
use App\State\ContentNode\ChecklistNodePersistProcessor;
use App\Util\EntityMap;
use App\Validator\ChecklistItem\AssertBelongsToSameCamp;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ApiResource(
operations: [
new Get(
security: 'is_granted("CAMP_COLLABORATOR", object) or
is_granted("CAMP_IS_PUBLIC", object)'
),
new Patch(
processor: ChecklistNodePersistProcessor::class,
denormalizationContext: ['groups' => ['write', 'update']],
security: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
validationContext: ['groups' => ['Default', 'update']]
),
new Delete(
security: '(is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)) and object.parent !== null'
),
new GetCollection(
security: 'is_authenticated()'
),
new Post(
processor: ChecklistNodePersistProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.parent === null',
validationContext: ['groups' => ['Default', 'create']],
),
],
denormalizationContext: ['groups' => ['write']],
normalizationContext: ['groups' => ['read']],
routePrefix: '/content_node'
)]
#[ORM\Entity(repositoryClass: ChecklistNodeRepository::class)]
class ChecklistNode extends ContentNode {
/**
* List of selected ChecklistItems.
*/
#[ApiProperty(example: '["/checklist_items/1a2b3c4d"]')]
#[Groups(['read'])]
#[ORM\ManyToMany(targetEntity: ChecklistItem::class, inversedBy: 'checklistNodes')]
#[ORM\JoinTable(name: 'checklistnode_checklistitem')]
#[ORM\JoinColumn(name: 'checklistnode_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
#[ORM\InverseJoinColumn(name: 'checklistitem_id', referencedColumnName: 'id', onDelete: 'RESTRICT')]
#[ORM\OrderBy(['position' => 'ASC'])]
public Collection $checklistItems;
#[AssertBelongsToSameCamp(groups: ['update'])]
#[ApiProperty(example: '["1a2b3c4d"]')]
#[Groups(['write'])]
public ?array $addChecklistItemIds = [];
#[ApiProperty(example: '["1a2b3c4d"]')]
#[Groups(['write'])]
public ?array $removeChecklistItemIds = [];
public function __construct() {
parent::__construct();
$this->checklistItems = new ArrayCollection();
}
/**
* @return ChecklistItem[]
*/
public function getChecklistItems(): array {
return $this->checklistItems->getValues();
}
public function addChecklistItem(ChecklistItem $checklistItem) {
$this->checklistItems->add($checklistItem);
}
public function removeChecklistItem(ChecklistItem $checklistItem) {
$this->checklistItems->removeElement($checklistItem);
}
/**
* @param ChecklistNode $prototype
* @param EntityMap $entityMap
*/
public function copyFromPrototype($prototype, $entityMap): void {
parent::copyFromPrototype($prototype, $entityMap);
// copy all checklist-items
foreach ($prototype->checklistItems as $itemPrototype) {
/** @var ChecklistItem $itemPrototype */
/** @var ChecklistItem $checklilstItem */
$checklilstItem = $entityMap->get($itemPrototype);
$this->addChecklistItem($checklilstItem);
}
}
}