From 6f255f44c42faab7343590a07cbc9f0a474ede9e Mon Sep 17 00:00:00 2001 From: Severin Hauser Date: Tue, 21 Mar 2023 11:31:26 +0100 Subject: [PATCH] feat: reduce number of created versions --- .gitignore | 3 +++ src/PageElement.php | 24 ++++++++++++++++++++---- src/PageElementSelfRel.php | 12 ++++++++++-- src/PageSection.php | 7 ++++++- src/PageSectionPageElementRel.php | 12 ++++++++++-- 5 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3698562 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +vendor/ +composer.lock +resources/ \ No newline at end of file diff --git a/src/PageElement.php b/src/PageElement.php index 37e0818..19bb5dd 100755 --- a/src/PageElement.php +++ b/src/PageElement.php @@ -131,12 +131,20 @@ public function onAfterWrite() if (Versioned::get_stage() == Versioned::DRAFT && $this->isChanged("__Counter", DataObject::CHANGE_VALUE)) { foreach ($this->PageSections() as $section) { $section->__Counter++; - $section->write(); + if ($section->isLiveVersion()) { + $section->write(); + } else { + $section->writeWithoutVersion(); + } } foreach ($this->Parents() as $parent) { $parent->__Counter++; - $parent->write(); + if ($parent->isLiveVersion()) { + $parent->write(); + } else { + $parent->writeWithoutVersion(); + } } } } @@ -148,7 +156,11 @@ public function onAfterDelete() if (Versioned::get_stage() == Versioned::DRAFT) { foreach ($this->PageSections() as $section) { $section->__Counter++; - $section->write(); + if ($section->isLiveVersion()) { + $section->write(); + } else { + $section->writeWithoutVersion(); + } } } } @@ -158,7 +170,11 @@ public function onAfterArchive() if (Versioned::get_stage() == Versioned::DRAFT) { foreach ($this->PageSections() as $section) { $section->__Counter++; - $section->write(); + if ($section->isLiveVersion()) { + $section->write(); + } else { + $section->writeWithoutVersion(); + } } } } diff --git a/src/PageElementSelfRel.php b/src/PageElementSelfRel.php index 0006c3c..4fa9787 100644 --- a/src/PageElementSelfRel.php +++ b/src/PageElementSelfRel.php @@ -36,7 +36,11 @@ public function onAfterWrite() if (!$this->__NewOrder && Versioned::get_stage() == Versioned::DRAFT) { $this->Parent()->__Counter++; - $this->Parent()->write(); + if ($this->Parent()->isLiveVersion()) { + $this->Parent()->write(); + } else { + $this->Parent()->writeWithoutVersion(); + } } } @@ -46,7 +50,11 @@ public function onAfterDelete() if (Versioned::get_stage() == Versioned::DRAFT) { $this->Parent()->__Counter++; - $this->Parent()->write(); + if ($this->Parent()->isLiveVersion()) { + $this->Parent()->write(); + } else { + $this->Parent()->writeWithoutVersion(); + } } } } diff --git a/src/PageSection.php b/src/PageSection.php index 3b48ea0..5f8e379 100644 --- a/src/PageSection.php +++ b/src/PageSection.php @@ -68,7 +68,12 @@ public function onAfterWrite() if (!$this->__isNew && Versioned::get_stage() == Versioned::DRAFT && $this->isChanged("__Counter", DataObject::CHANGE_VALUE)) { $this->Parent()->__PageSectionCounter++; - $this->Parent()->write(); + // Only create a new version when the previous one is e published one. + if ($this->Parent()->isLiveVersion()) { + $this->Parent()->write(); + } else { + $this->Parent()->writeWithoutVersion(); + } } } diff --git a/src/PageSectionPageElementRel.php b/src/PageSectionPageElementRel.php index 8649880..c4f1e3a 100644 --- a/src/PageSectionPageElementRel.php +++ b/src/PageSectionPageElementRel.php @@ -36,7 +36,11 @@ public function onAfterWrite() if (!$this->__NewOrder && Versioned::get_stage() == Versioned::DRAFT) { $this->PageSection()->__Counter++; - $this->PageSection()->write(); + if ($this->PageSection()->isLiveVersion()) { + $this->PageSection()->write(); + } else { + $this->PageSection()->writeWithoutVersion(); + } } } @@ -46,7 +50,11 @@ public function onAfterDelete() if (Versioned::get_stage() == Versioned::DRAFT) { $this->PageSection()->__Counter++; - $this->PageSection()->write(); + if ($this->PageSection()->isLiveVersion()) { + $this->PageSection()->write(); + } else { + $this->PageSection()->writeWithoutVersion(); + } } } }