From 76dfc5c3f150571bcb41393fb611210d8e08e587 Mon Sep 17 00:00:00 2001 From: Marco Crespi Date: Thu, 22 Feb 2018 12:25:31 +0100 Subject: [PATCH] feat(relations): Connect elements with a many_many "through" relation [WIP] --- _config/config.yml | 6 +++ code/GridFieldPageSectionsExtension.php | 4 +- code/PageElement.php | 50 ++++++++++++++----------- code/PageElementSelfRel.php | 19 ++++++++++ code/PageSectionPageElementRel.php | 29 ++++++++++++++ code/PageSectionsExtension.php | 38 ++++++++++--------- composer.json | 3 +- 7 files changed, 106 insertions(+), 43 deletions(-) create mode 100644 code/PageElementSelfRel.php create mode 100644 code/PageSectionPageElementRel.php diff --git a/_config/config.yml b/_config/config.yml index be14e1b..b43d6c9 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -4,3 +4,9 @@ Name: pagesections FlxLabs\PageSections\PageElement: extensions: - SilverStripe\Versioned\Versioned +FlxLabs\PageSections\PageElementSelfRel: + extensions: + - SilverStripe\Versioned\Versioned +FlxLabs\PageSections\PageSectionPageElementRel: + extensions: + - SilverStripe\Versioned\Versioned diff --git a/code/GridFieldPageSectionsExtension.php b/code/GridFieldPageSectionsExtension.php index 8c8e797..1143f52 100644 --- a/code/GridFieldPageSectionsExtension.php +++ b/code/GridFieldPageSectionsExtension.php @@ -363,10 +363,10 @@ public function handleReorder(GridField $gridField, HTTPRequest $request) { } else { if ($newParent) { $newParent->Children()->Add($item, $sortArr); - $newParent->write(); + $newParent->writeWithoutVersion(); } else { $gridField->getList()->Add($item, $sortArr); - $this->getPage()->write(); + $this->getPage()->writeWithoutVersion(); } } diff --git a/code/PageElement.php b/code/PageElement.php index 72098b0..50bb9ce 100644 --- a/code/PageElement.php +++ b/code/PageElement.php @@ -23,7 +23,8 @@ use UncleCheese\BetterButtons\Buttons\BetterButton_Save; class PageElement extends DataObject { - + + private static $table_name = "FLXLabs_PageElement"; protected static $singularName = "Element"; protected static $pluralName = "Elements"; @@ -46,12 +47,16 @@ function canCreate($member = null, $context = array()) { return true; } ); private static $many_many = array( - "Children" => PageElement::class, + "Children" => array( + "through" => PageElementSelfRel::class, + "from" => "Parent", + "to" => "Child", + ) ); private static $belongs_many_many = array( - "Parents" => PageElement::class, - "Pages" => SiteTree::class, + "Parents" => PageElement::class . ".Children", + "Pages" => "Page.PageSectionMain", ); private static $many_many_extraFields = array( @@ -59,7 +64,7 @@ function canCreate($member = null, $context = array()) { return true; } "SortOrder" => 'Int', ), ); - + private static $owns = [ "Children", ]; @@ -83,28 +88,29 @@ public static function getAllowedPageElements() { return $classes; } - public function onBeforeWrite() { - parent::onBeforeWrite(); + // public function onBeforeWrite() { + // parent::onBeforeWrite(); - $list = $this->Children()->Sort("SortOrder")->toArray(); - $count = count($list); + // $list = $this->Children()->Sort("SortOrder")->toArray(); + // $this->Children()->RemoveAll(); + // $count = count($list); - for ($i = 1; $i <= $count; $i++) { - $this->Children()->Add($list[$i - 1], array("SortOrder" => $i * 2)); - } - } + // for ($i = 1; $i <= $count; $i++) { + // $this->Children()->Add($list[$i - 1], array("SortOrder" => $i * 2)); + // } + // } - public function onAfterWrite() { - $stage = Versioned::get_stage(); + // public function onAfterWrite() { + // $stage = Versioned::get_stage(); - foreach ($this->Parents() as $parent) { - $parent->copyVersionToStage($stage, $stage, true); - } + // foreach ($this->Parents() as $parent) { + // $parent->copyVersionToStage($stage, $stage, true); + // } - foreach ($this->Pages() as $page) { - $page->copyVersionToStage($stage, $stage, true); - } - } + // foreach ($this->Pages() as $page) { + // $page->copyVersionToStage($stage, $stage, true); + // } + // } public function getChildrenGridField() { $addNewButton = new GridFieldAddNewMultiClass(); diff --git a/code/PageElementSelfRel.php b/code/PageElementSelfRel.php new file mode 100644 index 0000000..b2c3755 --- /dev/null +++ b/code/PageElementSelfRel.php @@ -0,0 +1,19 @@ + "Int", + ); + + private static $has_one = array( + "Parent" => PageElement::class, + "Child" => PageElement::class, + ); +} diff --git a/code/PageSectionPageElementRel.php b/code/PageSectionPageElementRel.php new file mode 100644 index 0000000..d31e95f --- /dev/null +++ b/code/PageSectionPageElementRel.php @@ -0,0 +1,29 @@ + "Int", + ); + + private static $has_one = array( + "PageSection" => "Page", + "Element" => PageElement::class, + ); + + + public function onBeforeWrite() { + parent::onBeforeWrite(); + + if (!$this->ID && !$this->SortOrder) { + $this->SortOrder = 1337; + } + } +} diff --git a/code/PageSectionsExtension.php b/code/PageSectionsExtension.php index aa72559..81a5114 100644 --- a/code/PageSectionsExtension.php +++ b/code/PageSectionsExtension.php @@ -22,7 +22,6 @@ class PageSectionsExtension extends DataExtension { // Generate the needed relations on the class public static function get_extra_config($class = null, $extensionClass = null) { $many_many = array(); - $many_many_extraFields = array(); // Get all the sections that should be added $sections = Config::inst()->get($class, "page_sections", Config::EXCLUDE_EXTRA_SOURCES); @@ -30,15 +29,19 @@ public static function get_extra_config($class = null, $extensionClass = null) { foreach ($sections as $section) { $name = "PageSection".$section; - $many_many[$name] = PageElement::class; - $many_many_extraFields[$name] = array("SortOrder" => "Int"); + // $many_many[$name] = PageElement::class; + $many_many[$name] = array( + "through" => PageSectionPageElementRel::class, + "from" => "PageSection", + "to" => "Element", + ); + $owns[] = $name; } // Create the relations for our sections return array( "many_many" => $many_many, - "many_many_extraFields" => $many_many_extraFields, "owns" => $owns, ); } @@ -49,23 +52,24 @@ public static function getAllowedPageElements() { return $classes; } - public function onBeforeWrite() { - parent::onBeforeWrite(); + // public function onBeforeWrite() { + // parent::onBeforeWrite(); - $sections = $this->owner->config()->get("page_sections"); - if (!$sections) $sections = array("Main"); + // $sections = $this->owner->config()->get("page_sections"); + // if (!$sections) $sections = array("Main"); - foreach ($sections as $section) { - $name = "PageSection".$section; + // foreach ($sections as $section) { + // $name = "PageSection".$section; - $list = $this->owner->$name()->Sort("SortOrder")->toArray(); - $count = count($list); + // $list = $this->owner->$name()->Sort("SortOrder")->toArray(); + // $this->owner->$name()->RemoveAll(); + // $count = count($list); - for ($i = 1; $i <= $count; $i++) { - $this->owner->$name()->Add($list[$i - 1], array("SortOrder" => $i * 2)); - } - } - } + // for ($i = 1; $i <= $count; $i++) { + // $this->owner->$name()->Add($list[$i - 1], array("SortOrder" => $i * 2)); + // } + // } + // } public function updateCMSFields(FieldList $fields) { $sections = $this->owner->config()->get("page_sections"); diff --git a/composer.json b/composer.json index 107ef7e..e0579ee 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,7 @@ "issues": "http://github.com/flxlabs/silverstripe-pagesections/issues" }, "require": { - "silverstripe/framework": "~3.6", - "flxlabs/silverstripe-versionedrelations": "^0.1.7" + "silverstripe/framework": "^4.0.1" }, "extra": { "installer-name": "pagesections"