Skip to content

Commit

Permalink
feat(page-element): Add "publish on all pages" button
Browse files Browse the repository at this point in the history
Add tab to show which pages an element is on.
Fixed "useCount" of elements
  • Loading branch information
Marco Crespi committed Jun 28, 2018
1 parent 0793c1d commit 38c082e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 13 deletions.
7 changes: 5 additions & 2 deletions code/GridFieldPageSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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('<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg">
Expand Down
83 changes: 76 additions & 7 deletions code/PageElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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';
}
}
9 changes: 7 additions & 2 deletions code/PageSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand Down Expand Up @@ -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");
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion templates/PageElement.ss
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>
<h1>$Title (v$Version)</h1>
$Layout
<div>
$RenderChildren($ParentList)
Expand Down

0 comments on commit 38c082e

Please sign in to comment.