Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW (#8): Ability to apply grid field capability to pages via DataExtension #9

Merged
merged 2 commits into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This module is meant as base classes, it can be used on its own but usually you
*These will get auto-installed when using composer:*

* SilverStripe 3.0 or newer
* PHP 5.4 or newer
* [silverstripe-excludechildren module to hide pages from the sitetree](https://github.com/micschk/silverstripe-excludechildren)
* [silverstripe-gridfieldsitetreebuttons to manage SiteTree items in a gridfield](https://github.com/micschk/silverstripe-gridfieldsitetreebuttons)

Expand Down
147 changes: 29 additions & 118 deletions code/GridFieldPage.php
Original file line number Diff line number Diff line change
@@ -1,123 +1,34 @@
<?php
class GridFieldPage extends Page
{

private static $can_be_root = false;
private static $allowed_children = "none";

private static $defaults = array(
'ShowInMenus' => false,
);

private static $searchable_fields = array(
'Title', 'MenuTitle'
);

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;
}
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;
private static $allowed_children = "none";

private static $defaults = array (
'ShowInMenus' => false,
);

private static $searchable_fields = array(
'Title', 'MenuTitle'
);

private static $summary_fields = array(
"Title", 'MenuTitle'
);

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);
}

}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"name": "micschk"
}],
"require": {
"php": ">=5.4.0,<7",
"silverstripe/framework": "~3.0",
"silverstripe/cms": "~3.0",
"micschk/silverstripe-excludechildren": "*",
Expand Down