diff --git a/_config/config.yml b/_config/config.yml index 3fcddeb..43046fb 100755 --- a/_config/config.yml +++ b/_config/config.yml @@ -4,6 +4,9 @@ Name: pagesections FLXLabs\PageSections\PageElement: extensions: - SilverStripe\Versioned\Versioned +FLXLabs\PageSections\PageSection: + extensions: + - SilverStripe\Versioned\Versioned FLXLabs\PageSections\PageElementSelfRel: extensions: - SilverStripe\Versioned\Versioned diff --git a/src/GridFieldPageSectionsExtension.php b/src/GridFieldPageSectionsExtension.php index 731e66a..0a849c2 100755 --- a/src/GridFieldPageSectionsExtension.php +++ b/src/GridFieldPageSectionsExtension.php @@ -357,16 +357,16 @@ public function handleReorder(GridField $gridField, HTTPRequest $request) { if ($type == "child") { if ($newParent) { $newParent->Children()->Add($item, $sortArr); + $newParent->write(); } else { $gridField->getList()->Add($item, $sortArr); } } else { if ($newParent) { $newParent->Children()->Add($item, $sortArr); - $newParent->writeWithoutVersion(); + $newParent->write(); } else { $gridField->getList()->Add($item, $sortArr); - $this->getPage()->writeWithoutVersion(); } } diff --git a/src/PageElement.php b/src/PageElement.php index e511efc..a46adc7 100755 --- a/src/PageElement.php +++ b/src/PageElement.php @@ -45,6 +45,7 @@ function canCreate($member = null, $context = []) { return true; } private static $db = [ "Name" => "Varchar(255)", + "__Counter" => "Int" ]; private static $many_many = [ @@ -56,7 +57,8 @@ function canCreate($member = null, $context = []) { return true; } ]; private static $belongs_many_many = [ - "Parents" => PageElement::class . ".Children" + "Parents" => PageElement::class . ".Children", + "PageSections" => PageSection::class . ".Elements", ]; private static $many_many_extraFields = [ @@ -69,6 +71,10 @@ function canCreate($member = null, $context = []) { return true; } "Children", ]; + private static $cascade_deletes = [ + "Children", + ]; + private static $summary_fields = [ "SingularName", "ID", @@ -88,29 +94,37 @@ public static function getAllowedPageElements() { return $classes; } - // public function onBeforeWrite() { - // parent::onBeforeWrite(); + public function onBeforeWrite() { + parent::onBeforeWrite(); - // $list = $this->Children()->Sort("SortOrder")->toArray(); - // $this->Children()->RemoveAll(); - // $count = count($list); - - // for ($i = 1; $i <= $count; $i++) { - // $this->Children()->Add($list[$i - 1], ["SortOrder" => $i * 2]); - // } - // } + $elems = $this->Children()->Sort("SortOrder")->Column("ID"); + $count = count($elems); + for ($i = 0; $i < $count; $i++) { + $this->Children()->Add($elems[$i], [ "SortOrder" => ($i + 1) * 2, "__NewOrder" => true ]); + } + } - // public function onAfterWrite() { - // $stage = Versioned::get_stage(); + public function onAfterWrite() { + parent::onAfterWrite(); - // foreach ($this->Parents() as $parent) { - // $parent->copyVersionToStage($stage, $stage, true); - // } + if (Versioned::get_stage() == Versioned::DRAFT) { + foreach ($this->PageSections() as $section) { + $section->__Counter++; + $section->write(); + } + } + } - // foreach ($this->Pages() as $page) { - // $page->copyVersionToStage($stage, $stage, true); - // } - // } + public function onAfterDelete() { + parent::onAfterDelete(); + + if (Versioned::get_stage() == Versioned::DRAFT) { + foreach ($this->PageSections() as $section) { + $section->__Counter++; + $section->write(); + } + } + } public function getChildrenGridField() { $addNewButton = new GridFieldAddNewMultiClass(); @@ -141,6 +155,7 @@ public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName('Pages'); $fields->removeByName('Parents'); + $fields->removeByName('__Counter'); $fields->removeByName("Children"); if ($this->ID && count(static::getAllowedPageElements())) { diff --git a/src/PageElementSelfRel.php b/src/PageElementSelfRel.php index f9300e9..dd67661 100644 --- a/src/PageElementSelfRel.php +++ b/src/PageElementSelfRel.php @@ -3,6 +3,7 @@ namespace FLXLabs\PageSections; use SilverStripe\ORM\DataObject; +use SilverStripe\Versioned\Versioned; class PageElementSelfRel extends DataObject { @@ -16,4 +17,33 @@ class PageElementSelfRel extends DataObject { "Parent" => PageElement::class, "Child" => PageElement::class, ); + + public function onBeforeWrite() { + parent::onBeforeWrite(); + + if (!$this->ID) { + if (!$this->SortOrder) { + // Add new elements at the end (highest SortOrder) + $this->SortOrder = ($this->Parent()->Children()->Count() + 1) * 2; + } + } + } + + public function onAfterWrite() { + parent::onAfterWrite(); + + if (!$this->__NewOrder && Versioned::get_stage() == Versioned::DRAFT) { + $this->Parent()->__Counter++; + $this->Parent()->write(); + } + } + + public function onAfterDelete() { + parent::onAfterDelete(); + + if (Versioned::get_stage() == Versioned::DRAFT) { + $this->Parent()->__Counter++; + $this->Parent()->write(); + } + } } diff --git a/src/PageSection.php b/src/PageSection.php new file mode 100644 index 0000000..dd3e489 --- /dev/null +++ b/src/PageSection.php @@ -0,0 +1,88 @@ + "Int", + ]; + + private static $has_one = [ + "Page" => SiteTree::class, + ]; + + private static $owns = [ + "Elements", + ]; + + private static $cascade_deletes = [ + "Elements", + ]; + + private static $many_many = [ + "Elements" => [ + "through" => PageSectionPageElementRel::class, + "from" => "PageSection", + "to" => "Element" + ] + ]; + + public function onBeforeWrite() { + parent::onBeforeWrite(); + + $elems = $this->Elements()->Sort("SortOrder")->Column("ID"); + $count = count($elems); + for ($i = 0; $i < $count; $i++) { + $this->Elements()->Add($elems[$i], [ "SortOrder" => ($i + 1) * 2, "__NewOrder" => true ]); + } + } + + public function onAfterWrite() { + parent::onAfterWrite(); + + if (Versioned::get_stage() == Versioned::DRAFT) { + $this->Page()->__PageSectionCounter++; + $this->Page()->write(); + } + } + + public function forTemplate() { + return $this->Elements()->Count(); + + $actions = FieldList::create(); + $fields = FieldList::create(); + $form = Form::create(null, "Form", $fields, $actions); + + $config = GridFieldConfig::create() + ->addComponent(new GridFieldToolbarHeader()) + ->addComponent($dataColumns = new GridFieldDataColumns()) + ->addComponent(new GridFieldPageSectionsExtension($this->owner)); + $dataColumns->setFieldCasting(["GridFieldPreview" => "HTMLText->RAW"]); + + $grid = GridField::create("Elements", "Elements", $this->Elements(), $config); + $grid->setForm($form); + $fields->add($grid); + + $form->setFields($fields); + + return $form->forTemplate(); + } +} diff --git a/src/PageSectionPageElementRel.php b/src/PageSectionPageElementRel.php index 7f1d590..bd4a867 100644 --- a/src/PageSectionPageElementRel.php +++ b/src/PageSectionPageElementRel.php @@ -1,29 +1,49 @@ - "Int", - ]; - - private static $has_one = [ - "PageSection" => "Page", - "Element" => PageElement::class, - ]; - - - public function onBeforeWrite() { - parent::onBeforeWrite(); - - if (!$this->ID && !$this->SortOrder) { - $this->SortOrder = 1337; - } - } -} + "Int", + ); + + private static $has_one = array( + "PageSection" => PageSection::class, + "Element" => PageElement::class, + ); + + public function onBeforeWrite() { + parent::onBeforeWrite(); + + if (!$this->ID) { + if (!$this->SortOrder) { + // Add new elements at the end (highest SortOrder) + $this->SortOrder = ($this->PageSection()->Elements()->Count() + 1) * 2; + } + } + } + + public function onAfterWrite() { + parent::onAfterWrite(); + + if (!$this->__NewOrder && Versioned::get_stage() == Versioned::DRAFT) { + $this->PageSection()->__Counter++; + $this->PageSection()->write(); + } + } + + public function onAfterDelete() { + parent::onAfterDelete(); + + if (Versioned::get_stage() == Versioned::DRAFT) { + $this->PageSection()->__Counter++; + $this->PageSection()->write(); + } + } +} diff --git a/src/PageSectionsExtension.php b/src/PageSectionsExtension.php index 64bb297..ecf3c46 100755 --- a/src/PageSectionsExtension.php +++ b/src/PageSectionsExtension.php @@ -22,21 +22,19 @@ class PageSectionsExtension extends DataExtension { // Generate the needed relations on the class public static function get_extra_config($class = null, $extensionClass = null) { - $many_many = []; + $has_one = []; $owns = []; $cascade_deletes = []; // Get all the sections that should be added $sections = Config::inst()->get($class, "page_sections", Config::EXCLUDE_EXTRA_SOURCES); - if (!$sections) $sections = ["Main"]; + if (!$sections) { + $sections = ["Main"]; + } foreach ($sections as $section) { $name = "PageSection".$section; - $many_many[$name] = [ - "through" => PageSectionPageElementRel::class, - "from" => "PageSection", - "to" => "Element", - ]; + $has_one[$name] = PageSection::class; $owns[] = $name; $cascade_deletes[] = $name; @@ -44,7 +42,8 @@ public static function get_extra_config($class = null, $extensionClass = null) { // Create the relations for our sections return [ - "many_many" => $many_many, + "db" => ["__PageSectionCounter" => "Int"], + "has_one" => $has_one, "owns" => $owns, "cascade_deletes" => $cascade_deletes, ]; @@ -56,28 +55,34 @@ public static function getAllowedPageElements() { return $classes; } - // public function onBeforeWrite() { - // parent::onBeforeWrite(); - - // $sections = $this->owner->config()->get("page_sections"); - // if (!$sections) $sections = ["Main"]; + public function onBeforeWrite() { + parent::onBeforeWrite(); - // foreach ($sections as $section) { - // $name = "PageSection".$section; + if ($this->owner->ID) { + $sections = $this->owner->config()->get("page_sections"); + if (!$sections) { + $sections = ["Main"]; + } - // $list = $this->owner->$name()->Sort("SortOrder")->toArray(); - // $this->owner->$name()->RemoveAll(); - // $count = count($list); + foreach ($sections as $section) { + $name = "PageSection".$section; - // for ($i = 1; $i <= $count; $i++) { - // $this->owner->$name()->Add($list[$i - 1], ["SortOrder" => $i * 2]); - // } - // } - // } + // Create a page section if we don't have one yet + if (!$this->owner->$name()->ID) { + $section = PageSection::create(); + $section->PageID = $this->owner->ID; + $section->write(); + $this->owner->$name = $section; + } + } + } + } public function updateCMSFields(FieldList $fields) { $sections = $this->owner->config()->get("page_sections"); - if (!$sections) $sections = ["Main"]; + if (!$sections) { + $sections = ["Main"]; + } foreach ($sections as $section) { $name = "PageSection".$section; @@ -101,14 +106,14 @@ public function updateCMSFields(FieldList $fields) { ->addComponent(new GridFieldDetailForm()); $dataColumns->setFieldCasting(["GridFieldPreview" => "HTMLText->RAW"]); - $f = new GridField($name, $section, $this->owner->$name(), $config); + $f = new GridField($name, $section, $this->owner->$name()->Elements(), $config); $fields->addFieldToTab("Root.PageSections", $f); } } } public function RenderPageSection($name = "Main") { - $elements = $this->owner->{"PageSection" . $name}()->Sort("SortOrder"); + $elements = $this->owner->{"PageSection" . $name}()->Elements()->Sort("SortOrder"); return $this->owner->renderWith( "RenderChildren", ["Elements" => $elements, "ParentList" => strval($this->owner->ID)]