Skip to content

Commit

Permalink
feat(page-sections): Add page section class to fix polymorphic issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Crespi committed Jun 4, 2018
1 parent 2250174 commit 53d48de
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 77 deletions.
3 changes: 3 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/GridFieldPageSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
55 changes: 35 additions & 20 deletions src/PageElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function canCreate($member = null, $context = []) { return true; }

private static $db = [
"Name" => "Varchar(255)",
"__Counter" => "Int"
];

private static $many_many = [
Expand All @@ -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 = [
Expand All @@ -69,6 +71,10 @@ function canCreate($member = null, $context = []) { return true; }
"Children",
];

private static $cascade_deletes = [
"Children",
];

private static $summary_fields = [
"SingularName",
"ID",
Expand All @@ -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();
Expand Down Expand Up @@ -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())) {
Expand Down
30 changes: 30 additions & 0 deletions src/PageElementSelfRel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FLXLabs\PageSections;

use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;

class PageElementSelfRel extends DataObject {

Expand All @@ -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();
}
}
}
88 changes: 88 additions & 0 deletions src/PageSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace FLXLabs\PageSections;

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Versioned\Versioned;

use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;

class PageSection extends DataObject {

private static $table_name = "FLXLabs_PageSections_PageSection";

private static $db = [
"__Counter" => "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();
}
}
78 changes: 49 additions & 29 deletions src/PageSectionPageElementRel.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
<?php

namespace FLXLabs\PageSections;

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\DataObject;

class PageSectionPageElementRel extends DataObject {

private static $table_name = "FLXLabs_PageSections_PageSectionPageElementRel";

private static $db = [
"SortOrder" => "Int",
];

private static $has_one = [
"PageSection" => "Page",
"Element" => PageElement::class,
];


public function onBeforeWrite() {
parent::onBeforeWrite();

if (!$this->ID && !$this->SortOrder) {
$this->SortOrder = 1337;
}
}
}
<?php

namespace FLXLabs\PageSections;

use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;

class PageSectionPageElementRel extends DataObject {

private static $table_name = "FLXLabs_PageSections_PageSectionPageElementRel";

private static $db = array(
"SortOrder" => "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();
}
}
}
Loading

0 comments on commit 53d48de

Please sign in to comment.