Skip to content

Commit

Permalink
fix(page-sections): Move classes to FlxLabs\PageSections namespace. A…
Browse files Browse the repository at this point in the history
…pply extension to sitetree, because class Page might not exist. Fix some basic styling issues in gridview. Rename "Title" field to "Name".
  • Loading branch information
Marco Crespi committed Feb 1, 2018
1 parent ead0e10 commit 067e409
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 46 deletions.
2 changes: 1 addition & 1 deletion _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Name: pagesections
---
PageSections\PageElement:
FlxLabs\PageSections\PageElement:
extensions:
- SilverStripe\Versioned\Versioned
4 changes: 2 additions & 2 deletions code/GridFieldPageSectionsExtension.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace PageSections;
namespace FlxLabs\PageSections;

use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
Expand Down Expand Up @@ -394,7 +394,7 @@ public function handleAdd(GridField $gridField, HTTPRequest $request) {
}

$child = $type::create();
$child->Title = "New " . $type;
$child->Name = "New " . $type;
$child->write();

$obj->Children()->Add($child);
Expand Down
68 changes: 35 additions & 33 deletions code/PageElement.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace PageSections;
namespace FlxLabs\PageSections;

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\ClassInfo;
Expand All @@ -11,16 +11,21 @@
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member;
use SilverStripe\Versioned\Versioned;

use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;
use UncleCheese\BetterButtons\Actions\BetterButtonPrevNextAction;
use UncleCheese\BetterButtons\Buttons\BetterButton_SaveAndClose;
use UncleCheese\BetterButtons\Buttons\BetterButton_Save;

class PageElement extends DataObject {

protected static $singularName = 'Element';
protected static $pluralName = 'Elements';
protected static $singularName = "Element";
protected static $pluralName = "Elements";

public static function getSingularName() {
return static::$singularName;
Expand All @@ -37,21 +42,21 @@ function canCreate($member = null, $context = array()) { return true; }
private static $can_be_root = true;

private static $db = array(
'Title' => 'Varchar(255)',
"Name" => "Varchar(255)",
);

private static $many_many = array(
'Children' => PageElement::class,
"Children" => PageElement::class,
);

private static $belongs_many_many = array(
'Parents' => PageElement::class,
'Pages' => 'Page',
"Parents" => PageElement::class,
"Pages" => SiteTree::class,
);

private static $many_many_extraFields = array(
'Children' => array(
'SortOrder' => 'Int',
"Children" => array(
"SortOrder" => 'Int',
),
);

Expand All @@ -60,15 +65,15 @@ function canCreate($member = null, $context = array()) { return true; }
];

private static $summary_fields = array(
'SingularName',
'ID',
'GridFieldPreview',
"SingularName",
"ID",
"GridFieldPreview",
);

private static $searchable_fields = array(
'ClassName',
'Title',
'ID'
"ClassName",
"Name",
"ID",
);

public static function getAllowedPageElements() {
Expand All @@ -89,12 +94,24 @@ public function onBeforeWrite() {
}
}

public function onAfterWrite() {
$stage = Versioned::get_stage();

foreach ($this->Parents() as $parent) {
$parent->copyVersionToStage($stage, $stage, true);
}

foreach ($this->Pages() as $page) {
$page->copyVersionToStage($stage, $stage, true);
}
}

public function getChildrenGridField() {
$addNewButton = new GridFieldAddNewMultiClass();
$addNewButton->setClasses($this->getAllowedPageElements());

$autoCompl = new GridFieldAddExistingAutocompleter('buttons-before-right');
$autoCompl->setResultsFormat('$Title ($ID)');
$autoCompl->setResultsFormat('$Name ($ID)');
$autoCompl->setSearchList(PageElement::get()->exclude("ID", $this->getParentIDs()));

$config = GridFieldConfig::create()
Expand All @@ -111,7 +128,7 @@ public function getChildrenGridField() {
}

public function getGridFieldPreview() {
return $this->Title;
return $this->Name;
}

public function getCMSFields() {
Expand All @@ -135,7 +152,7 @@ public function getParentIDs() {
return $IDArr;
}

public function renderChildren($parents) {
public function renderChildren($parents = null) {
return $this->renderWith(
"RenderChildren",
array("Elements" => $this->Children(), "ParentList" => strval($this->ID) . "," . $parents)
Expand All @@ -156,19 +173,4 @@ public function forTemplate($parentList = "") {
array("ParentList" => $parentList, "Parents" => $parents, "Page" => $page)
);
}

public function getBetterButtonsUtils() {
$fieldList = FieldList::create(array(
BetterButtonPrevNextAction::create(),
));
return $fieldList;
}

public function getBetterButtonsActions() {
$fieldList = FieldList::create(array(
BetterButton_SaveAndClose::create(),
BetterButton_Save::create(),
));
return $fieldList;
}
}
15 changes: 8 additions & 7 deletions code/PageSectionsExtension.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace PageSections;
namespace FlxLabs\PageSections;

use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
Expand All @@ -13,6 +13,7 @@
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Versioned\Versioned;

use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;

Expand All @@ -35,12 +36,10 @@ public static function get_extra_config($class = null, $extensionClass = null) {
}

// Create the relations for our sections
//Config::inst()->update($class, "many_many", $many_many);
//Config::inst()->update($class, "many_many_extraFields", $many_many_extraFields);
Config::modify()->merge($class, "owns", $owns);
return array(
"many_many" => $many_many,
"many_many_extraFields" => $many_many_extraFields,
"owns" => $owns,
);
}

Expand All @@ -51,6 +50,8 @@ public static function getAllowedPageElements() {
}

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

$sections = $this->owner->config()->get("page_sections");
if (!$sections) $sections = array("Main");

Expand Down Expand Up @@ -80,7 +81,7 @@ public function updateCMSFields(FieldList $fields) {
$addNewButton->setClasses($this->owner->getAllowedPageElements());

$autoCompl = new GridFieldAddExistingAutocompleter('buttons-before-right');
$autoCompl->setResultsFormat('$Title ($ID)');
$autoCompl->setResultsFormat('$Name ($ID)');

$config = GridFieldConfig::create()
->addComponent(new GridFieldButtonRow("before"))
Expand All @@ -98,8 +99,8 @@ public function updateCMSFields(FieldList $fields) {
}
}

public function PageSection($name = "Main") {
$elements = $this->owner->{"PageSection" . $name}();
public function RenderPageSection($name = "Main") {
$elements = $this->owner->{"PageSection" . $name}()->Sort("SortOrder");
return $this->owner->renderWith(
"RenderChildren",
array("Elements" => $elements, "ParentList" => strval($this->owner->ID))
Expand Down
8 changes: 6 additions & 2 deletions css/GridFieldPageSectionsExtension.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@

.ss-gridfield-pagesections .col-treenav {
height: 100%;
padding: 0 !important;
padding-left: 0;
/*padding: 0 !important;*/
}

.ss-gridfield-pagesections .col-treenav > button {
//width: 100% !important;
/*width: 100% !important;*/
height: 100% !important;
font-weight: bold !important;
/*font-size: 150% !important;*/
color: black !important;
border: none;
background: transparent;
padding-right: 0;
}

.ss-gridfield-pagesections .col-treenav > button, .ss-gridfield-pagesections .col-treenav > button > span {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div className="$ClassName $Page.ClassName" style="margin-left: {$Parents.Count}em">
<h1>$Title</h1>
<h2>$Name</h2>
$Layout
<div>
$RenderChildren($ParentList)
Expand Down

0 comments on commit 067e409

Please sign in to comment.