diff --git a/README.md b/README.md index 37417a2..edaacc2 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # Kirby 3 Many To Many Field -> ⚠️ Please update to v1.0.2 if you used the plugin before, as the old version contained a bug that could lead to data loss. See the issue [Sorting Structure](https://github.com/jonasholfeld/kirby3-many-to-many-field/issues/7) for more info on the bug. ⚠️ +> ⚠️ Version 2.0 of this plugin uses the [Unique IDs](https://getkirby.com/docs/guide/uuids) (aka UUIDs) that are part of the Kirby core since Kirby 3.8.0. Make sure to only use it with Kirby 3.8.0 or higher (doesn't work with Kirby 4 though). Upgrading from 1.0 to 2.0 with existing content is not possible and will lead to corrupted data. ⚠️ -This plugin allows you to create many-to-many relationships between pages in Kirby. The relationship is bidirectional, meaning it can be edited from either side and is automatically updated on the other side. The relationship can have attributes that can be updated from both sides as well. You can define multiple many-to-many relations on one page. If a page with a relation to one or many other pages gets deleted, all relations to this page get deleted as well. - -You need to install the [AutoId plugin](https://github.com/bnomei/kirby3-autoid) by Bnomei to your project as well for this plugin to work. +This plugin allows you to create many-to-many relationships between pages in Kirby. It is designed based on the many-to-many relationship commonly found in traditional database systems (hence the name). The relationship is bidirectional, meaning it can be edited from either side and is automatically updated on the other side. The relationship can have attributes that can be updated from both sides as well. You can define multiple many-to-many relations on one page. If a page with a relation to one or many other pages gets deleted, all relations to this page get deleted as well. This plugin uses two hooks: the [page.update:after](https://getkirby.com/docs/reference/plugins/hooks/page-update-after) and the [page.delete:before](https://getkirby.com/docs/reference/plugins/hooks/page-delete-before) hook. If you use these hooks in your project as well, make sure to rename the hooks and trigger them seperately as described [here](https://getkirby.com/docs/reference/plugins/extensions/hooks#creating-your-own-hooks). +The README and the example blueprints are based on an Employee-Project relationship commonly used in database systems. + ![many-to-many-kirby3](https://user-images.githubusercontent.com/282518/122782365-f3b90c80-d2b0-11eb-8428-9b4efecbc713.jpg) ## Installation @@ -28,43 +28,19 @@ git submodule add https://github.com/jonasholfeld/kirby3-many-to-many-field.git composer require jonasholfeld/kirby3-many-to-many-field ``` -## Setup - -1. [Install AutoID](#1-Install-AutoID) -2. [Use AutoID to identify your pages](#2-Use-AutoID-to-identify-your-pages) -3. [Setup your blueprints](#3-Setup-your-blueprints) -- 3.0.1 [Quickstart](#301-Quickstart ) -- 3.1 [Naming and Type](#31-Naming-and-Type) -- 3.2 [The foreignkey field](#32-The-foreignkey-field) -- 3.3 [The unique validator](#33-The-unique-validator) -- 3.4 [The relation fields](#34-The-relation-fields) -- 3.5 [Corresponding blueprint](#35-Corresponding-blueprint) -- 3.6 [Additional structure fields](#36-Additional-structure-fields) -- 3.7 [How to use in templates](#37-How-to-use-in-templates) - -### 1. Install AutoID - -Add the [AutoID](https://github.com/bnomei/kirby3-autoid/) plugin by Bnomei to your kirby-project. - -### 2. Use AutoID to identify your pages - -Both blueprints of the pages you want to have a many-2-many relation need to have this field: - -```yaml -autoid: - type: hidden - translate: false -``` - -The AutoID plugin automatically generates a unique ID for every page that will be created after it’s install. If some pages already exist without an ID, you can force a [re-index](https://github.com/bnomei/kirby3-autoid/wiki/Force-Re-index). - -### 3. Setup your blueprints +## Setup your blueprints The many-to-many plugin gets all its information about the related pages from your blueprints, so it’s essential to set them up right. You can check out the [example blueprints](exampleBlueprints) to get a better idea about how to setup yours. Both blueprints need the manytomany field in order to connect the pages correctly. As it’s important to set them up correctly, the following text explains every step bit by bit. -#### 3.0.1 Quickstart +1. [Quickstart](#1-Quickstart ) +2. [Setup in Detail](#-2-Setup-in-Detail) +- 2.1 [Necessery structure fields](#31-Necessary-Structure-Fields) +- 2.2 [Additional structure fields](#36-Additional-structure-fields) +- 2.3 [How to use in templates](#37-How-to-use-in-templates) + +#### 1 Quickstart You can use and adjust these two blueprints to setup a relation between two pages with the plugin. It implements the classic Employee <--> Project relation you might know from database examples (see ER-diagram above). Make sure to rename all fields according to your situation. To fully understand all the fields and adjust them to your situation you should read on. @@ -77,31 +53,25 @@ fields: description: type: text label: Description - employees: type: manytomany label: Employees + translate: false fields: foreignkey: label: Employee - type: multiselect - min: 1 - max: 1 + type: select options: query query: fetch: site.find('employees').childrenAndDrafts text: "{{ page.title }}" - value: "{{ page.autoid }}" + value: "{{ page.uuid }}" hours: type: number label: Number of hours validate: unique: employees - relatedPage: employees relatationField: projects - - autoid: - translate: false ``` #### **`employee.yml`** @@ -113,245 +83,136 @@ fields: age: type: number label: Age - projects: type: manytomany label: Projects + translate: false fields: foreignkey: label: Project - type: multiselect - min: 1 - max: 1 + type: select options: query query: fetch: site.find('projects').childrenAndDrafts text: "{{ page.title }}" - value: "{{ page.autoid }}" + value: "{{ page.uuid }}" hours: type: number label: Number of hours validate: unique: projects - relatedPage: projects relatationField: employees - - autoid: - translate: false ``` -#### 3.1 Naming and Type +## 2 Setup in Detail + +#### 2.1 Necessary Structure Fields -You can name the field how you like. A name hinting to the nature of the relation or the templates of the related pages might be helpful. +Let's go through above's example step by step and look at the neccesary fields. + +You can name the relation field how you like. A name hinting to the nature of the relation or the templates of the related pages might be helpful. You need to specify the type as *manytomany*: ```yaml -myRelatedPages: #<-- name how you like +employees: #<-- name how you like type: manytomany ... ``` The manytomany-field inherits from the [structure field](https://getkirby.com/docs/reference/panel/fields/structure), so it is setup like a normal structure-field with a couple of additional fields that need to be filled. -#### 3.2 The foreignkey field - -The foreignkey field is the field inside our manytomany-field that saves the "foreign keys". In our case they are the IDs created by the autoID plugin. You create a field inside the manytomany-field called "foreignkey" that is a multiselect that queries its options from the pages you would like to link to. To be more specific, it queries the children of a given page, so you need to specify the name of the parent-page to whose subpages you would like to link to. -It is important to use *page.autoid* as the value, you can chose what to use as the text, but I recommend to use *page.title* to identify the pages. - ```yaml -myRelatedPages: - type: manytomany - lable: My Related Pages - fields: - foreignkey: #<-- name needs to be *foreignkey* - label: Related Page - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('myRelatedParentPage').childrenAndDrafts # <-- use name of parent-page of related pages here - text: "{{ page.title }}" - value: "{{ page.autoid }}" - validate: - unique: myRelatedPages +fields: + foreignkey: #<-- must be called like this + label: Employee + type: select #<-- must be a select field + options: query + query: + fetch: site.find('employees').childrenAndDrafts #<-- adjust to your needs... + text: "{{ page.title }}" + value: "{{ page.uuid }}" ... ``` -#### 3.3 The unique validator - -Duplicate entries inside the manytomany field cause problems, so make sure to use the unique validator of the plugin: +The first necessary field is called "foreignkey" and saves the ID of the related page. It is a select field that fetches its options from a query. Adjust this to your needs, but dont change the name of the field. ```yaml -myRelatedPages: - type: manytomany - lable: My Related Pages - fields: - foreignkey: - label: Related Page - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('myRelatedParentPage').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - validate: - unique: myRelatedPages #<-- use name of your field +validate: + unique: projects +relatationField: employees ... ``` -#### 3.4 The relation fields - -There are three equally important fields you need to add to the manytomany field. They specify the template of the related pages, their parent-page and the name of the corresponding manytomany field in their blueprint. Make sure to fill them out correctly. - -```yaml -myRelatedPages: - type: manytomany - lable: My Related Pages - fields: - foreignkey: - label: Related Page - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('myRelatedParentPage').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - validate: - unique: myRelatedPages - relatedPage: myRelatedFolder #<-- name of the parent-page of the linked pages - relatationField: myOtherRelatedPages #<-- name of the corresponding manytomany-field in the blueprint of linked pages -... -``` +The other two necessary fields are a validator that makes sure you link a page only once to another, and a static field that saves the name of the corresponding relation field, that is the field in the linked page the relation should be written to. This is needed because there could be multiple relation fields in the same blueprint and the plugin needs to know which relation should be written to which field. -#### 3.5 Corresponding blueprint +#### 2.2 Corresponding blueprint -To be able to edit the relation from both sides, both blueprints of the related pages need to have a field of the type manytomany. They need to have corresponding values in the specific fields. Here is an example of two blueprints, in this case with a relation between students and schools. +To be able to edit the relation from both sides, both blueprints of the related pages need to have a field of the type manytomany. They need to have corresponding values in the specific fields. Lets visit aboves example again and look how the fields are corresponding... -#### **`school.yml`** +#### **`project.yml`** ```yaml -title: School -fields: - students: +title: Project + +fields: + description: + type: text + label: Description + employees: #<-- name of the related entities... type: manytomany - label: Students + translate: false fields: foreignkey: - label: Student - type: multiselect - min: 1 - max: 1 + label: Employee + type: select options: query query: - fetch: site.find('students').childrenAndDrafts + fetch: site.find('employees').childrenAndDrafts #<-- query to the related entities... text: "{{ page.title }}" - value: "{{ page.autoid }}" + value: "{{ page.uuid }}" + hours: + type: number + label: Number of hours validate: - unique: students - relatedPage: students - relatationField: schools - autoid: - type: hidden - translate: false + unique: employees #<-- name of the manytomany field to be validated + relatationField: projects #<-- name of the corresponding relation field ``` -#### **`student.yml`** +#### **`employee.yml`** + ```yaml -title: School -fields: - students: +title: Employee + +fields: + age: + type: number + label: Age + projects: #<-- name of the related entities... type: manytomany - label: Students + label: Projects + translate: false fields: foreignkey: - label: Student - type: multiselect - min: 1 - max: 1 + label: Project + type: select options: query query: - fetch: site.find('students').childrenAndDrafts + fetch: site.find('projects').childrenAndDrafts #<-- query to the related entities... text: "{{ page.title }}" - value: "{{ page.autoid }}" - year: + value: "{{ page.uuid }}" + hours: type: number + label: Number of hours validate: - unique: students - relatedPage: students - relatationField: schools - autoid: - type: hidden - translate: false + unique: projects #<-- name of the manytomany field to be validated + relatationField: employees #<-- name of the corresponding relation field ``` Once your blueprints are setup like this, the manytomany field changes on both sides, when there is an update from one of them. #### 3.6 Additional structure fields -As mentioned above, the manytomany field is just a structure field with some special fields. That means you can add any number of fields to the structure, if you need to save some extra information about the relation, e.g. a year. Just make sure the two linked blueprints both have the extra fields in the manytomany field: - -#### **`school.yml`** -```yaml -title: School -fields: - students: - type: manytomany - label: Students - fields: - foreignkey: - label: Student - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('students').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - year: #<-- some extra field - type: number - validate: - unique: students - relatedPage: students - relatationField: schools - autoid: - type: hidden - translate: false -``` - -#### **`student.yml`** -```yaml -title: Student -fields: - schools: - type: manytomany - label: Schools - fields: - foreignkey: - label: School - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('schools').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - year: #<-- the same extra field - type: number - validate: - unique: schools - relatedPage: schools - relatationField: students - autoid: - type: hidden - translate: false -``` +As mentioned above, the manytomany field is just a structure field with some special fields. That means you can add any number of fields to the structure, if you need to save some extra information about the relation, e.g. the number of hours an employee worked on a project (like in the example above). Just make sure the two linked blueprints both have the extra fields in the manytomany field like seen above with the additional "hours" field. ### 3.7 How to use in templates @@ -363,8 +224,8 @@ fields: $projects = $page->projects()->toStructure(); // we can then loop through the entries and render the individual fields foreach($projects as $project): - // Fetching the project page by using the fromAutoID-fieldMethod from the AUTOID plugin - $projectPage = $project->foreignkey()->fromAutoID(); ?> + // Fetching the project page by using the page method + $projectPage = kirby()->page($project->foreignkey()); ?>

Title: title() ?>

@@ -372,6 +233,9 @@ foreach($projects as $project): ``` +## Multilanguage / Translation + +I advice to set the *translate: false* option in your blueprints for the manytomany field, as relations should usually be the same for multilanguage pages. For other cases, adjust the code in index.php for your needs. ## License diff --git a/exampleBlueprints/artistsExample/artist.yml b/exampleBlueprints/artistsExample/artist.yml deleted file mode 100644 index 4e49333..0000000 --- a/exampleBlueprints/artistsExample/artist.yml +++ /dev/null @@ -1,24 +0,0 @@ -title: Artist - -fields: - works: - label: Works - type: manytomany - fields: - foreignKey: - label: Work - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('works').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - validate: - unique: works - relatedPage: works - relatationField: author - - autoid: - translate: false diff --git a/exampleBlueprints/artistsExample/artists.yml b/exampleBlueprints/artistsExample/artists.yml deleted file mode 100644 index 563e7f0..0000000 --- a/exampleBlueprints/artistsExample/artists.yml +++ /dev/null @@ -1,8 +0,0 @@ -title: Artists - -sections: - - children: - headline: Artists - type: pages - templates: artist diff --git a/exampleBlueprints/artistsExample/work.yml b/exampleBlueprints/artistsExample/work.yml deleted file mode 100644 index 9a55762..0000000 --- a/exampleBlueprints/artistsExample/work.yml +++ /dev/null @@ -1,24 +0,0 @@ -title: Work - -fields: - author: - label: Author - type: manytomany - fields: - foreignKey: - label: Work - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('artists').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - validate: - unique: author - relatedPage: artists - relatationField: works - - autoid: - translate: false diff --git a/exampleBlueprints/artistsExample/works.yml b/exampleBlueprints/artistsExample/works.yml deleted file mode 100644 index 5194fb8..0000000 --- a/exampleBlueprints/artistsExample/works.yml +++ /dev/null @@ -1,8 +0,0 @@ -title: Works - -sections: - - children: - headline: Works - type: pages - templates: work diff --git a/exampleBlueprints/employee.yml b/exampleBlueprints/employee.yml new file mode 100644 index 0000000..8b2bb5a --- /dev/null +++ b/exampleBlueprints/employee.yml @@ -0,0 +1,25 @@ +title: Employee + +fields: + age: + type: number + label: Age + projects: + type: manytomany + label: Projects + translate: false + fields: + foreignkey: + label: Project + type: select + options: query + query: + fetch: site.find('projects').childrenAndDrafts + text: "{{ page.title }}" + value: "{{ page.uuid }}" + hours: + type: number + label: Number of hours + validate: + unique: projects + relatationField: employees \ No newline at end of file diff --git a/exampleBlueprints/employees.yml b/exampleBlueprints/employees.yml new file mode 100644 index 0000000..6976f1e --- /dev/null +++ b/exampleBlueprints/employees.yml @@ -0,0 +1,8 @@ +title: Employees + +sections: + + children: + headline: Employees + type: pages + templates: employee \ No newline at end of file diff --git a/exampleBlueprints/project.yml b/exampleBlueprints/project.yml new file mode 100644 index 0000000..5c6dace --- /dev/null +++ b/exampleBlueprints/project.yml @@ -0,0 +1,25 @@ +title: Project + +fields: + description: + type: text + label: Description + employees: + type: manytomany + label: Employees + translate: false + fields: + foreignkey: + label: Employee + type: select + options: query + query: + fetch: site.find('employees').childrenAndDrafts + text: "{{ page.title }}" + value: "{{ page.uuid }}" + hours: + type: number + label: Number of hours + validate: + unique: employees + relatationField: projects \ No newline at end of file diff --git a/exampleBlueprints/projects.yml b/exampleBlueprints/projects.yml new file mode 100644 index 0000000..94d20b4 --- /dev/null +++ b/exampleBlueprints/projects.yml @@ -0,0 +1,8 @@ +title: Projects + +sections: + + children: + headline: Projects + type: pages + templates: project \ No newline at end of file diff --git a/exampleBlueprints/projectsExample/employee.yml b/exampleBlueprints/projectsExample/employee.yml deleted file mode 100644 index 69bf6eb..0000000 --- a/exampleBlueprints/projectsExample/employee.yml +++ /dev/null @@ -1,31 +0,0 @@ -title: Employee - -fields: - age: - type: number - label: Age - - projects: - type: manytomany - label: Projects - fields: - foreignkey: - label: Project - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('projects').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - hours: - type: number - label: Number of hours - validate: - unique: projects - relatedPage: projects - relatationField: employees - - autoid: - translate: false \ No newline at end of file diff --git a/exampleBlueprints/projectsExample/project.yml b/exampleBlueprints/projectsExample/project.yml deleted file mode 100644 index 7301b8f..0000000 --- a/exampleBlueprints/projectsExample/project.yml +++ /dev/null @@ -1,31 +0,0 @@ -title: Project - -fields: - description: - type: text - label: Descriptoion - - employees: - type: manytomany - label: Employees - fields: - foreignkey: - label: Employee - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('employees').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - hours: - type: number - label: Number of hours - validate: - unique: employees - relatedPage: employees - relatationField: projects - - autoid: - translate: false \ No newline at end of file diff --git a/exampleBlueprints/schoolsExample/school.yml b/exampleBlueprints/schoolsExample/school.yml deleted file mode 100644 index 2bc8564..0000000 --- a/exampleBlueprints/schoolsExample/school.yml +++ /dev/null @@ -1,63 +0,0 @@ -title: School - -fields: - graduats: - type: relation - label: Graduats - fields: - foreignKey: - label: School - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('students').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - year: - label: Year - type: number - degree: - label: Degree - type: text - validate: - unique: graduats - relatedPage: students - relatationField: degrees - - employees: - type: relation - label: Employees - fields: - foreignKey: - label: Employee - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('teachers').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - from: - label: from - type: number - to: - label: to - type: number - subjects: - type: multiselect - options: - design: Design - architecture: Architecture - photography: Photography - 3d: 3D - web: Web - validate: - unique: employees - relatedPage: teachers - relatationField: employers - - autoid: - translate: false diff --git a/exampleBlueprints/schoolsExample/schools.yml b/exampleBlueprints/schoolsExample/schools.yml deleted file mode 100644 index 96cbfe3..0000000 --- a/exampleBlueprints/schoolsExample/schools.yml +++ /dev/null @@ -1,8 +0,0 @@ -title: Schools - -sections: - - children: - headline: Schools - type: pages - templates: school diff --git a/exampleBlueprints/schoolsExample/student.yml b/exampleBlueprints/schoolsExample/student.yml deleted file mode 100644 index 05a0bed..0000000 --- a/exampleBlueprints/schoolsExample/student.yml +++ /dev/null @@ -1,33 +0,0 @@ -title: Student - -fields: - name: - type: text - - degrees: - label: Degrees - type: relation - fields: - foreignKey: - label: School - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('schools').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - year: - label: Year - type: number - degree: - label: Degree - type: text - validate: - unique: degrees - relatedPage: schools - relatationField: graduats - - autoid: - translate: false diff --git a/exampleBlueprints/schoolsExample/students.yml b/exampleBlueprints/schoolsExample/students.yml deleted file mode 100644 index 6aec596..0000000 --- a/exampleBlueprints/schoolsExample/students.yml +++ /dev/null @@ -1,8 +0,0 @@ -title: Students - -sections: - - children: - headline: Students - type: pages - templates: student diff --git a/exampleBlueprints/schoolsExample/teacher.yml b/exampleBlueprints/schoolsExample/teacher.yml deleted file mode 100644 index 02432db..0000000 --- a/exampleBlueprints/schoolsExample/teacher.yml +++ /dev/null @@ -1,41 +0,0 @@ -title: Teacher - -fields: - name: - type: text - - employers: - type: relation - label: Employers - fields: - foreignKey: - label: School - type: multiselect - min: 1 - max: 1 - options: query - query: - fetch: site.find('schools').childrenAndDrafts - text: "{{ page.title }}" - value: "{{ page.autoid }}" - from: - label: from - type: number - to: - label: to - type: number - subjects: - type: multiselect - options: - design: Design - architecture: Architecture - photography: Photography - 3d: 3D - web: Web - validate: - unique: employers - relatedPage: schools - relatationField: employees - - autoid: - translate: false diff --git a/exampleBlueprints/schoolsExample/teachers.yml b/exampleBlueprints/schoolsExample/teachers.yml deleted file mode 100644 index 3ff0f2d..0000000 --- a/exampleBlueprints/schoolsExample/teachers.yml +++ /dev/null @@ -1,8 +0,0 @@ -title: Teachers - -sections: - - children: - headline: Teachers - type: pages - templates: teacher diff --git a/index.php b/index.php index 0b95ac9..467b74a 100644 --- a/index.php +++ b/index.php @@ -1,119 +1,73 @@ [ '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); - - // Checks if the relation field is present in the updated page foreach ($relationFields as $relation) { - - // Checks if the relation field was edited - if (relationIsChanged($newPage, $oldPage, $relation)) { - - // Getting Content type and page from the blueprint of the updated page - $relatedPage = kirby()->page($newPage->blueprint()->field($relation)['relatedPage']); - $relationField = $newPage->blueprint()->field($relation)['relatationField']; - - // Getting the autoid value of the updated page + $relationField = $newPage->blueprint()->field($relation)['relatationField']; + $oldRelationsArray = $oldPage->$relation()->toStructure()->toArray(); + $oldRelationsArray = array_map('unSetID', $oldRelationsArray); + $newRelationsArray = $newPage->$relation()->toStructure()->toArray(); + $newRelationsArray = array_map('unSetID', $newRelationsArray); + foreach($oldRelationsArray as $oldRelation) { + if(!in_array($oldRelation, $newRelationsArray)) { try { - $primaryKey = $newPage->AUTOID(); + $foreign_subPage = kirby()->page($oldRelation['foreignkey']); } catch (Throwable $e) { - throw new Exception('Many to Many Field Plugin: Updated page needs autoid field to create a relation. '.$e->getMessage()); - } - - foreach ($newPage->$relation()->toStructure() as $singleRelation) { - // Fetching the related subpage - try { - $foreign_subPage = $relatedPage->childrenAndDrafts()->findBy('autoid', $singleRelation->toArray()['foreignkey']); - } catch (Throwable $e) { - throw new Exception('Many to Many Field Plugin: "relatedPage" field in blueprint is missing. '.$e->getMessage()); - } - // Changing the relation entry so it links to autoid of updated page - $singleRelationAtForeign = $singleRelation->toArray(); - $singleRelationAtForeign['foreignkey'] = $primaryKey; - // Deleting the id field set by the toArray() Method - unset($singleRelationAtForeign['id']); - // Adding relation to foreign subpage - addRelation($foreign_subPage, $singleRelationAtForeign, $relationField); - } - - // Filtering deleted keys - $oldForeignKeys = $oldPage->$relation()->toStructure()->toArray(); - $newForeignKeys = $newPage->$relation()->toStructure()->toArray(); - $oldForeignKeys = array_map('unSetID', $oldForeignKeys); - $newForeignKeys = array_map('unSetID', $newForeignKeys); - $deletedForeignKeys = []; - foreach($oldForeignKeys as $oldForeignKey) { - if (!in_array($oldForeignKey, $newForeignKeys)) { - array_push($deletedForeignKeys, $oldForeignKey); - } - } - foreach ($deletedForeignKeys as $foreignKey) { - - //Finding the related subpage - try { - $foreign_subPage = $relatedPage->childrenAndDrafts()->findBy('autoid', $foreignKey['foreignkey']); - } catch (Throwable $e) { - throw new Exception('Many to Many Field Plugin: "relatedPage" field in blueprint is missing. '.$e->getMessage()); - } - - //Chaning the relation-entry so it matches the entry at subpage - $singleRelationAtForeign = $foreignKey; - $singleRelationAtForeign['foreignkey'] = $primaryKey; - - // Deleting the id field set by the toArray() Method - unset($singleRelationAtForeign['id']); - deleteRelation($foreign_subPage, $singleRelationAtForeign, $relationField); + 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 autoids of related pages + // Getting bosst-ids of related pages $foreignKeys = $page->$relation()->toStructure()->toArray(); - - // Getting the autoid value of the deleted page - $primaryKey = $page->AUTOID(); - + // 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 = $relatedPage->childrenAndDrafts()->findBy('autoid', $foreignKey['foreignkey']); - + $foreign_subPage = kirby()->page($foreignKey['foreignkey']); // Changing the relation-entry so it matches the entry at subpage $singleRelationAtForeign = $foreignKey; $singleRelationAtForeign['foreignkey'] = $primaryKey; - // Deleting the id field set by the toArray() Method unset($singleRelationAtForeign['id']); - // Deleting the relation entry from the related page deleteRelation($foreign_subPage, $singleRelationAtForeign, $relationField); } @@ -125,13 +79,11 @@ function getRelationFields($page) { $relationFields = []; - foreach ($page->blueprint()->fields() as $field) { if ($field['type'] == 'manytomany') { array_push($relationFields, $field['name']); } } - return $relationFields; } @@ -140,10 +92,8 @@ function deleteRelation($page, $value, $relationField) // Getting relations field from page to delete from $fieldData = $page->$relationField()->toStructure()->toArray(); - // Creating empty field $newFieldData = []; - // Pushing all entries that dont match the deleted relation foreach ($fieldData as $relation) { $singleRelation = $relation; @@ -152,7 +102,6 @@ function deleteRelation($page, $value, $relationField) array_push($newFieldData, $singleRelation); } } - // Encoding try { // Updating page @@ -169,57 +118,20 @@ function unSetID($value) return $newValue; } -function relationIsChanged($newPage, $oldPage, $relation) -{ - - $change = false; - $oldRelationsArray = $oldPage->$relation()->toStructure()->toArray(); - $oldRelationsArray = array_map('unSetID', $oldRelationsArray); - $newRelationsArray = $newPage->$relation()->toStructure()->toArray(); - $newRelationsArray = array_map('unSetID', $newRelationsArray); - - foreach($oldRelationsArray as $oldRelation) { - if(!in_array($oldRelation, $newRelationsArray)) { - $change = true; - } - } - foreach($newRelationsArray as $newRelation) { - if(!in_array($newRelation, $oldRelationsArray)) { - $change = true; - } - } - return $change; -} function addRelation($page, $value, $relationField) { - // Getting relations field from page to add to try { - $fieldData = YAML::decode(kirby()->page($page)->$relationField()->value()); + $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()); } - - // Getting Length of relations field before insert - $fieldLengthBefore = count($fieldData); - // Writing to relations field array_push($fieldData, $value); - - // Making array unique to filter out duplicates - $fieldData = array_unique($fieldData, SORT_REGULAR); - - // Getting Length of relations field after insert - $fieldLengthAfter = count($fieldData); - - // If fieldLengthAfter is same as fieldLengthBefore, nothing was added so we skip updating to avoid cascading - if ($fieldLengthBefore !== $fieldLengthAfter) { - try { - kirby()->page($page)->update([$relationField => YAML::encode($fieldData)]); - - return true; - } catch (Exception $e) { - return $e->getMessage(); - } + 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; } }