Skip to content

Commit

Permalink
feat(relations): Connect elements with a many_many "through" relation…
Browse files Browse the repository at this point in the history
… [WIP]
  • Loading branch information
Marco Crespi committed Feb 22, 2018
1 parent 067e409 commit 76dfc5c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 43 deletions.
6 changes: 6 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions code/GridFieldPageSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
50 changes: 28 additions & 22 deletions code/PageElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -46,20 +47,24 @@ 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(
"Children" => array(
"SortOrder" => 'Int',
),
);

private static $owns = [
"Children",
];
Expand All @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions code/PageElementSelfRel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace FlxLabs\PageSections;

use SilverStripe\ORM\DataObject;

class PageElementSelfRel extends DataObject {

private static $table_name = "FLXLabs_PageElementSelfRel";

private static $db = array(
"SortOrder" => "Int",
);

private static $has_one = array(
"Parent" => PageElement::class,
"Child" => PageElement::class,
);
}
29 changes: 29 additions & 0 deletions code/PageSectionPageElementRel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace FlxLabs\PageSections;

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

class PageSectionPageElementRel extends DataObject {

private static $table_name = "FLXLabs_PageSectionPageElementRel";

private static $db = array(
"SortOrder" => "Int",
);

private static $has_one = array(
"PageSection" => "Page",
"Element" => PageElement::class,
);


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

if (!$this->ID && !$this->SortOrder) {
$this->SortOrder = 1337;
}
}
}
38 changes: 21 additions & 17 deletions code/PageSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ 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);
if (!$sections) $sections = array("Main");

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,
);
}
Expand All @@ -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");
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 76dfc5c

Please sign in to comment.