diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/README.md b/README.md index 111daa7..2dfee5a 100755 --- a/README.md +++ b/README.md @@ -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) diff --git a/code/GridFieldPage.php b/code/GridFieldPage.php index b5018db..0932e66 100644 --- a/code/GridFieldPage.php +++ b/code/GridFieldPage.php @@ -1,123 +1,34 @@ 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", - ' 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", - ' 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 = "" - . _t("GridFieldPage.StatusDraftDeleted", "draft deleted") . ""; - //$status = 'Draft deleted'; - } - - // If modified on stage, add - if ($this->IsModifiedOnStage) { - // add to published status - $statusflag = "" - . _t("GridFieldPage.StatusModified", "draft modified") . ""; - //$status = 'Draft modified'; - } - - // If same on stage... - if ($this->IsSameOnStage) { - // leave as is - } - } else { - if ($this->IsAddedToStage) { - $status = _t( - "GridFieldPage.StatusDraft", - ' 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, mic@restruct.nl + * @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 diff --git a/code/GridFieldPageExtension.php b/code/GridFieldPageExtension.php new file mode 100644 index 0000000..dd2ce4d --- /dev/null +++ b/code/GridFieldPageExtension.php @@ -0,0 +1,15 @@ +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", + ' 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", + ' 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 = "" + . _t("GridFieldPage.StatusDraftDeleted", "draft deleted") . ""; + //$status = 'Draft deleted'; + } + + // If modified on stage, add + if($page->IsModifiedOnStage) { + // add to published status + $statusflag = "" + . _t("GridFieldPage.StatusModified", "draft modified") . ""; + //$status = 'Draft modified'; + } + + // If same on stage... + if($page->IsSameOnStage) { + // leave as is + } + + } else { + if($page->IsAddedToStage) { + $status = _t( + "GridFieldPage.StatusDraft", + ' 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); + } + +} diff --git a/composer.json b/composer.json index 121e852..9b93ac2 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,7 @@ "name": "micschk" }], "require": { + "php": ">=5.4.0,<7", "silverstripe/framework": "~3.0", "silverstripe/cms": "~3.0", "micschk/silverstripe-excludechildren": "*",