-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
133 lines (126 loc) · 5.35 KB
/
index.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
<?php
Kirby::plugin('jonasholfeld/many-to-many-field', [
'fields' => [
'manytomany' => [
'extends' => 'structure',
],
],
'validators' => [
'unique' => function ($value, $field) {
return count($value) == count(array_unique($value, SORT_REGULAR));
},
],
'hooks' => [
'page.update:after' => function ($newPage, $oldPage) {
$primaryKey = $newPage->uuid()->id();
$relationFields = getRelationFields($newPage);
foreach ($relationFields as $relation) {
$relationField = $newPage->blueprint()->field($relation)['relatationField'];
$oldRelationsArray = YAML::decode($oldPage->$relation()->value());
$newRelationsArray = YAML::decode($newPage->$relation()->value());
foreach($oldRelationsArray as $oldRelation) {
if(!in_array($oldRelation, $newRelationsArray)) {
try {
$foreign_subPage = kirby()->page($oldRelation['foreignkey']);
} catch (Throwable $e) {
throw new Exception('Many to Many Field Plugin: "relatedPage" field in blueprint is missing. '.$e->getMessage());
}
$singleRelationAtForeign = $oldRelation;
$singleRelationAtForeign['foreignkey'] = "page://".$primaryKey;
unset($singleRelationAtForeign['id']);
deleteRelation($foreign_subPage, $singleRelationAtForeign, $relationField);
}
}
foreach($newRelationsArray as $newRelation) {
if(!in_array($newRelation, $oldRelationsArray)) {
try {
$foreign_subPage = kirby()->page($newRelation['foreignkey']);
} catch (Throwable $e) {
throw new Exception('Many to Many Field Plugin: "relatedPage" field in blueprint is missing. '.$e->getMessage());
}
$singleRelationAtForeign = $newRelation;
$singleRelationAtForeign['foreignkey'] = "page://".$primaryKey;
unset($singleRelationAtForeign['id']);
addRelation($foreign_subPage, $singleRelationAtForeign, $relationField);
}
}
}
},
'page.delete:before' => function ($status, $page) {
$relationFields = getRelationFields($page);
// Checks if the relation field is present in the updated page
foreach ($relationFields as $relation) {
// Getting bosst-ids of related pages
$foreignKeys = YAML::decode($page->$relation()->value());
// Getting the boost-id value of the deleted page
$primaryKey = $page->uuid();
// Getting related page and relation field from the blueprint of the deleted page
$relatedPage = kirby()->page($page->blueprint()->field($relation)['relatedPage']);
$relationField = $page->blueprint()->field($relation)['relatationField'];
foreach ($foreignKeys as $foreignKey) {
// Finding the related subpage
$foreign_subPage = kirby()->page($foreignKey['foreignkey']);
// Changing the relation-entry so it matches the entry at subpage
$singleRelationAtForeign = $foreignKey;
$singleRelationAtForeign['foreignkey'] = $primaryKey;
// Deleting the relation entry from the related page
deleteRelation($foreign_subPage, $singleRelationAtForeign, $relationField);
}
}
},
],
]);
function getRelationFields($page)
{
$relationFields = [];
foreach ($page->blueprint()->fields() as $field) {
if ($field['type'] == 'manytomany') {
array_push($relationFields, $field['name']);
}
}
return $relationFields;
}
function deleteRelation($page, $value, $relationField)
{
// Getting relations field from page to delete from
$fieldData = YAML::decode($page->$relationField()->value());
// Creating empty field
$newFieldData = [];
// Pushing all entries that dont match the deleted relation
foreach ($fieldData as $relation) {
$singleRelation = $relation;
unset($singleRelation['id']);
if ($singleRelation != $value) {
array_push($newFieldData, $singleRelation);
}
}
// Encoding
try {
// Updating page
$page->update([$relationField => $newFieldData]);
} catch (Exception $e) {
return $e->getMessage();
}
}
function unSetID($value)
{
$newValue = $value;
$newValue['id'] = 0;
return $newValue;
}
function addRelation($page, $value, $relationField)
{
try {
$fieldData = YAML::decode($page->$relationField()->value());
} catch (Throwable $e) {
throw new Exception('Many to Many Field Plugin: related page or relatation field is faulty or missing. ' .$e->getMessage());
}
// Writing to relations field
array_push($fieldData, $value);
if($page->isLocked()) {
throw new Exception('Related page is current locked. Save or delete all unsaved changes on the linked page.');
} else {
$page->update([$relationField => YAML::encode($fieldData)]);
return true;
}
}