From 38c082e10cb114f0feb06adb1590adf3071d8dad Mon Sep 17 00:00:00 2001 From: Marco Crespi Date: Thu, 28 Jun 2018 12:49:35 +0200 Subject: [PATCH] feat(page-element): Add "publish on all pages" button Add tab to show which pages an element is on. Fixed "useCount" of elements --- code/GridFieldPageSectionsExtension.php | 7 ++- code/PageElement.php | 83 ++++++++++++++++++++++--- code/PageSectionsExtension.php | 9 ++- composer.json | 3 +- templates/PageElement.ss | 2 +- 5 files changed, 91 insertions(+), 13 deletions(-) diff --git a/code/GridFieldPageSectionsExtension.php b/code/GridFieldPageSectionsExtension.php index 90603fc..d9f426e 100644 --- a/code/GridFieldPageSectionsExtension.php +++ b/code/GridFieldPageSectionsExtension.php @@ -192,7 +192,7 @@ public function getColumnContent($gridField, $record, $columnName) { return ViewableData::create()->customise(array( "ButtonField" => $field, "ID" => $record->ID, - "UsedCount" => $record->Parents()->Count() + $record->Pages()->Count(), + "UsedCount" => $record->Parents()->Count() + $record->getAllPages()->Count(), "ClassName" => $record->i18n_singular_name(), "Title" => $record->Title, ))->renderWith("GridFieldPageElement"); @@ -244,7 +244,10 @@ public function getColumnContent($gridField, $record, $columnName) { null, null ); - $deleteButton->setAttribute("data-used-count", $record->Parents()->Count() + $record->Pages()->Count()); + $deleteButton->setAttribute( + "data-used-count", + $record->Parents()->Count() + $record->getAllPages()->Count() + ); $deleteButton->addExtraClass("col-actions__button delete-button"); $deleteButton->setButtonContent(' diff --git a/code/PageElement.php b/code/PageElement.php index 4d12587..3b6de8e 100644 --- a/code/PageElement.php +++ b/code/PageElement.php @@ -25,7 +25,6 @@ function canCreate($member = null) { return true; } private static $versioned_belongs_many_many = array( 'Parents' => 'PageElement', - 'Pages' => 'Page', ); private static $many_many_extraFields = array( @@ -44,6 +43,10 @@ function canCreate($member = null) { return true; } 'ID' ); + private static $better_buttons_actions = array ( + 'publishOnAllPages', + ); + public static function getAllowedPageElements() { $classes = array_values(ClassInfo::subclassesFor('PageElement')); // remove @@ -90,24 +93,81 @@ public function getGridFieldPreview() { return $this->Title; } + // Get all the versioned_belongs_many_many that might have been added by + // additional page sections from various pages. (Also contains "Parent" relation) + public function getVersionedBelongsManyMany() { + return Config::inst()->get($this->getClassName(), "versioned_belongs_many_many"); + } + + // Gets all the pages that this page element is on, plus + // adds an __PageSectionName attribute to the page object so we + // know which section this element is in. + public function getAllPages() { + $pages = ArrayList::create(); + foreach ($this->getVersionedBelongsManyMany() as $name => $relation) { + // Skip any relations that probably aren't from page sections + $splits = explode("_", $name); + if (count($splits) < 2 || mb_substr($splits[1], 0, 11) !== "PageSection") { + continue; + } + + // Add all pages (and the page section that this element is in) + foreach ($this->$name() as $page) { + $stage = Versioned::current_stage(); + Versioned::reading_stage(Versioned::get_live_stage()); + + $oldPage = DataObject::get_by_id($page->ClassName, $page->ID); + + $page->__PageSectionName = mb_substr($splits[1], 11); + $page->__PageElementVersion = $page->$splits[1]()->filter("ID", $this->ID)->First()->Version; + $page->__PageElementPublishedVersion = $oldPage->$splits[1]()->filter("ID", $this->ID)->First()->Version; + $pages->add($page); + + Versioned::reading_stage($stage); + } + } + return $pages; + } + public function getCMSFields() { $fields = parent::getCMSFields(); - $fields->removeByName('Pages'); $fields->removeByName('Parents'); $fields->removeByName("Children"); if ($this->ID && count(static::getAllowedPageElements())) { - $fields->addFieldToTab('Root.PageSections', $this->getChildrenGridField()); + $fields->addFieldToTab('Root.Children', $this->getChildrenGridField()); + } + + // Add our newest version as a readonly field + $fields->addFieldsToTab( + "Root.Main", + ReadonlyField::create("Version", "Version", $this->Version), + "Title" + ); + + // Create an array of all the pages this element is on + $pages = $this->getAllPages(); + + // Remove default fields + foreach ($this->getVersionedBelongsManyMany() as $name => $rel) { + $fields->removeByName($name); } - $fields->addFieldsToTab("Root.Pages", ReadonlyField::create("Version", "Version", $this->Version)); $config = GridFieldConfig_Base::create() ->removeComponentsByType(GridFieldDataColumns::class) ->addComponent($dataColumns = new GridFieldDataColumns()); - $dataColumns->setDisplayFields(["Title" => "Title", "getIsPublished" => "Is Published"]); - $gridField = GridField::create("Pages", "Pages", $this->Pages(), $config); + $dataColumns->setDisplayFields([ + "ID" => "ID", + "ClassName" => "Type", + "Title" => "Title", + "__PageSectionName" => "PageSection", + "__PageElementVersion" => "Element version", + "__PageElementPublishedVersion" => "Published element version", + "getPublishState" => "Page state", + ]); + $gridField = GridField::create("Pages", "Pages", $pages, $config); $fields->addFieldToTab("Root.Pages", $gridField); - + return $fields; } @@ -152,7 +212,16 @@ public function getBetterButtonsActions() { $fieldList = FieldList::create(array( BetterButton_SaveAndClose::create(), BetterButton_Save::create(), + BetterButtonCustomAction::create('publishOnAllPages', 'Publish on all pages') + ->setRedirectType(BetterButtonCustomAction::REFRESH) )); return $fieldList; } + + public function publishOnAllPages() { + foreach ($this->getAllPages() as $page) { + $page->publish(Versioned::current_stage(), Versioned::get_live_stage()); + } + return 'Published on all pages'; + } } diff --git a/code/PageSectionsExtension.php b/code/PageSectionsExtension.php index 76acb7d..36607db 100644 --- a/code/PageSectionsExtension.php +++ b/code/PageSectionsExtension.php @@ -2,7 +2,7 @@ class PageSectionsExtension extends DataExtension { // Generate the needed relations on the class - public function extraStatics($class = null, $extensionClass = null) { + public static function get_extra_config($class = null, $extensionClass = null, $args) { $versioned_many_many = array(); $many_many_extraFields = array(); @@ -20,6 +20,11 @@ public function extraStatics($class = null, $extensionClass = null) { $name = "PageSection".$section; $versioned_many_many[$name] = "PageElement"; $many_many_extraFields[$name] = array("SortOrder" => "Int"); + + // Add the inverse relation to the PageElement class + Config::inst()->update(PageElement::class, "versioned_belongs_many_many", array( + $class . "_" . $name => $class . "." . $name + )); } // Create the relations for our sections @@ -94,7 +99,7 @@ public function PageSection($name = "Main") { ); } - public function getIsPublished() { + public function getPublishState() { return DBField::create_field("HTMLText", $this->owner->latestPublished() ? "Published" : "Draft"); } } diff --git a/composer.json b/composer.json index d5b20ca..75ee6f1 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "require": { "silverstripe/framework": "~3.7", "flxlabs/silverstripe-versionedrelations": "^0.1.14", - "symbiote/silverstripe-gridfieldextensions": "^2.0.2" + "symbiote/silverstripe-gridfieldextensions": "^2.0.2", + "unclecheese/betterbuttons": "^1.3.14" }, "extra": { "installer-name": "pagesections" diff --git a/templates/PageElement.ss b/templates/PageElement.ss index 04c2f2c..d7a3c0d 100644 --- a/templates/PageElement.ss +++ b/templates/PageElement.ss @@ -1,5 +1,5 @@
-

$Title

+

$Title (v$Version)

$Layout
$RenderChildren($ParentList)