Skip to content

Commit

Permalink
NEW (micschk#8): Ability to apply grid field capability to pages via …
Browse files Browse the repository at this point in the history
…DataExtension.
  • Loading branch information
patricknelson committed Apr 26, 2017
1 parent 69074ed commit ea9ecc8
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
119 changes: 13 additions & 106 deletions code/GridFieldPage.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php
require_once(__DIR__ . '/GridFieldPageTrait.php');

/**
* Extend this object to implement grid field capability for Page objects. Optionally, you can apply the
* GridFieldPageExtension instead.
*
* @author Michael van Schaik, [email protected]
* @since 2017-04-26
*/

class GridFieldPage extends Page {

private static $can_be_root = false;
Expand All @@ -15,112 +25,9 @@ class GridFieldPage extends Page {
private static $summary_fields = array(
"Title", 'MenuTitle'
);

/**
* add an arrow-overlay to this page's icon when open in the CMS
*/
public function getTreeTitle() {
return str_replace(
'jstree-pageicon',
'jstree-pageicon gridfieldpage-overlay',
parent::getTreeTitle());
}

/*
* Display status in the CMS grid
*/
public function getStatus($cached = true) {

$status = null;
$statusflag = null;

if($this->hasMethod("isPublished")) {

$published = $this->isPublished();

if($published) {
$status = _t(
"GridFieldPage.StatusPublished",
'<i class="btn-icon btn-icon-accept"></i> Published on {date}',
"State for when a post is published.",
array(
"date" => $this->dbObject("LastEdited")->Nice()
)
);
//$status = 'Published';

// Special case where sortorder changed
$liveRecord = Versioned::get_by_stage(get_class($this), 'Live')->byID($this->ID);
//return $this->Sort . ' - ' . $liveRecord->Sort;
if($liveRecord->Sort && $liveRecord->Sort != $this->Sort){
// override published status
$status = _t(
"GridFieldPage.StatusDraftReordered",
'<i class="btn-icon btn-icon-arrow-circle-double"></i> Draft modified (reordered)',
"State for when a page has been reordered."
);
//$status = 'Draft modified (reordered)';
}

// Special case where deleted from draft
if($this->IsDeletedFromStage) {
// override published status
$statusflag = "<span class='modified'>"
. _t("GridFieldPage.StatusDraftDeleted", "draft deleted") . "</span>";
//$status = 'Draft deleted';
}

// If modified on stage, add
if($this->IsModifiedOnStage) {
// add to published status
$statusflag = "<span class='modified'>"
. _t("GridFieldPage.StatusModified", "draft modified") . "</span>";
//$status = 'Draft modified';
}

// If same on stage...
if($this->IsSameOnStage) {
// leave as is
}

} else {
if($this->IsAddedToStage) {
$status = _t(
"GridFieldPage.StatusDraft",
'<i class="btn-icon btn-icon-pencil"></i> Saved as Draft on {date}',
"State for when a post is saved but not published.",
array(
"date" => $this->dbObject("LastEdited")->Nice()
)
);
//$status = 'Draft';
}

}

}

// allow for extensions
$this->extend('updateStatus', $status, $statusflag);

return DBField::create_field('HTMLVarchar', $status.$statusflag);

}

public function getCMSActions() {

// hide delete-draft button if page is published
// (deleting from draft while having a published page,
// removes the page from the gridfield and makes it un-reachable from the CMS
// The Gridfield gets records from draft only (AllChildrenIncludingDeleted breaks
// gridfield sorting & filtering)
$actions = parent::getCMSActions();
if ($this->isPublished()) {
$actions->removeByName('action_delete');
}
return $actions;
}


use GridFieldPageTrait;

}

class GridFieldPage_Controller extends Page_Controller {
Expand Down
15 changes: 15 additions & 0 deletions code/GridFieldPageExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
require_once(__DIR__ . '/GridFieldPageTrait.php');

/**
* Applies grid field functionality for pages as a SilverStripe Data Extension instead of requiring you to extend the
* GridFieldPage object. This is useful in case you don't wish to change existing class inheritance but still have this
* functionality.
*
* @author Patrick Nelson, [email protected]
* @since 2017-04-26
*/

class GridFieldPageExtension extends DataExtension {
use GridFieldPageTrait;
}
123 changes: 123 additions & 0 deletions code/GridFieldPageTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Contains core functionality required to implement grid field capability for Page objects. Implement this by either
* extending the GridFieldPage class or applying the GridFieldPageExtension to your page via SilverStripe API:
* https://docs.silverstripe.org/en/3/developer_guides/extending/extensions/
*
* @author Michael van Schaik, [email protected]
* @author Patrick Nelson, [email protected]
* @since 2017-04-26
*/

trait GridFieldPageTrait {

/**
* Page instance will vary depending on context (i.e. is this trait on page or page extension?)
*
* @return GridFieldPage
*/
private function getGridFieldPage() {
if ($this instanceof GridFieldPageExtension) {
return $this->getOwner();

} elseif ($this instanceof GridFieldPage) {
return $this;

} else {
// Shouldn't happen but in case someone silly incorporates this trait in the wrong context ;)
return GridFieldPage::create();
}
}

/**
* add an arrow-overlay to this page's icon when open in the CMS
*/
public function getTreeTitle() {
return str_replace(
'jstree-pageicon',
'jstree-pageicon gridfieldpage-overlay',
$this->getGridFieldPage()->getTreeTitle());
}

/*
* Display status in the CMS grid
*/
public function getStatus($cached = true) {

$status = null;
$statusflag = null;
$page = $this->getGridFieldPage();


if($page->hasMethod("isPublished")) {

$published = $page->isPublished();

if($published) {
$status = _t(
"GridFieldPage.StatusPublished",
'<i class="btn-icon btn-icon-accept"></i> Published on {date}',
"State for when a post is published.",
array(
"date" => $page->dbObject("LastEdited")->Nice()
)
);
//$status = 'Published';

// Special case where sortorder changed
$liveRecord = Versioned::get_by_stage(get_class($page), 'Live')->byID($page->ID);
//return $page->Sort . ' - ' . $liveRecord->Sort;
if($liveRecord->Sort && $liveRecord->Sort != $page->Sort){
// override published status
$status = _t(
"GridFieldPage.StatusDraftReordered",
'<i class="btn-icon btn-icon-arrow-circle-double"></i> Draft modified (reordered)',
"State for when a page has been reordered."
);
//$status = 'Draft modified (reordered)';
}

// Special case where deleted from draft
if($page->IsDeletedFromStage) {
// override published status
$statusflag = "<span class='modified'>"
. _t("GridFieldPage.StatusDraftDeleted", "draft deleted") . "</span>";
//$status = 'Draft deleted';
}

// If modified on stage, add
if($page->IsModifiedOnStage) {
// add to published status
$statusflag = "<span class='modified'>"
. _t("GridFieldPage.StatusModified", "draft modified") . "</span>";
//$status = 'Draft modified';
}

// If same on stage...
if($page->IsSameOnStage) {
// leave as is
}

} else {
if($page->IsAddedToStage) {
$status = _t(
"GridFieldPage.StatusDraft",
'<i class="btn-icon btn-icon-pencil"></i> Saved as Draft on {date}',
"State for when a post is saved but not published.",
array(
"date" => $page->dbObject("LastEdited")->Nice()
)
);
//$status = 'Draft';
}

}

}

// allow for extensions
$page->extend('updateStatus', $status, $statusflag);
return DBField::create_field('HTMLVarchar', $status.$statusflag);
}

}

0 comments on commit ea9ecc8

Please sign in to comment.