API CHANGE Refactored SilverStripeNavigator and SilverStripeNavigatorItem to instance-based utility classes, which take a DataObject record in the constructor rather than all of their methods (increasing template flexibility and code isolation).

ENHANCEMENT Added SilverStripeNavigatorItem->canView() for flexible permission control
MINOR Fixed duplicate priority overrides in SilverStripeNavigator
MINOR Removed unnecessary inline styling from SilverStripeNavigator HTML generation
This commit is contained in:
Ingo Schommer 2011-07-15 16:05:58 +02:00
parent 9c40e495e2
commit e13537e0a7
3 changed files with 331 additions and 189 deletions

View File

@ -0,0 +1,277 @@
<?php
/**
* Utility class representing links to different views of a record
* for CMS authors, usually for {@link SiteTree} objects with "stage" and "live" links.
* Useful both in the CMS and alongside the page template (for logged in authors).
*
* New item types can be defined by extending the {@link SilverStripeNavigatorItem} class,
* for example the "cmsworkflow" module defines a new "future state" item with a date selector
* to view embargoed data at a future point in time. So the item doesn't always have to be a simple link.
*
* @package cms
* @subpackage content
*/
class SilverStripeNavigator {
/**
* @var DataObject
*/
protected $record;
/**
* @param DataObject
*/
function __construct($record) {
$this->record = $record;
}
/**
* @return DataObjectSet of SilverStripeNavigatorItem
*/
function getItems() {
$items = '';
$classes = ClassInfo::subclassesFor('SilverStripeNavigatorItem');
array_shift($classes);
// Sort menu items according to priority
$i = 0;
foreach($classes as $class) {
// Skip base class
if($class == 'SilverStripeNavigatorItem') continue;
$i++;
$item = new $class($this->record);
if(!$item->canView()) continue;
// This funny litle formula ensures that the first item added with the same priority will be left-most.
$priority = $item->getPriority() * 100 - 1;
// Ensure that we can have duplicates with the same (default) priority
while(isset($items[$priority])) $priority++;
$items[$priority] = $item;
}
ksort($items);
return new DataObjectSet($items);
}
/**
* @return DataObject
*/
function getRecord() {
return $this->record;
}
/**
* @param SiteTree $record
* @return Array template data
*/
static function get_for_record($record) {
$html = '';
$message = '';
$navigator = new SilverStripeNavigator($record);
$items = $navigator->getItems();
foreach($items as $item) {
$text = $item->getHTML();
if($text) $html .= $text;
$newMessage = $item->getMessage();
if($newMessage) $message = $newMessage;
}
return array(
'items' => $html,
'message' => $message
);
}
}
/**
* Navigator items are links that appear in the $SilverStripeNavigator bar.
* To add an item, extends this class.
*
* @package cms
* @subpackage content
*/
class SilverStripeNavigatorItem extends ViewableData {
/**
* @param DataObject
*/
protected $record;
/**
* @param DataObject
*/
function __construct($record) {
$this->record = $record;
}
/**
* @return String HTML, mostly a link - but can be more complex as well.
* For example, a "future state" item might show a date selector.
* Hence there's no getLink() method.
*/
function getHTML() {}
/**
* @return String
*/
function getMessage() {}
/**
* @return DataObject
*/
function getRecord() {
return $this->record;
}
/**
* @return Int
*/
function getPriority() {
return $this->stat('priority');
}
/**
* Filters items based on member permissions or other criteria.
*
* @param Member
* @return Boolean
*/
function canView($member = null) {
return true;
}
}
/**
* @package cms
* @subpackage content
*/
class SilverStripeNavigatorItem_CMSLink extends SilverStripeNavigatorItem {
static $priority = 10;
function getHTML() {
if(is_a(Controller::curr(), 'CMSMain')) {
return '<a class="current">CMS</a>';
} else {
$cmsLink = 'admin/show/' . $this->record->ID;
$cmsLink = "<a href=\"$cmsLink\">". _t('ContentController.CMS', 'CMS') ."</a>";
return $cmsLink;
}
}
function getLink() {
if(is_a(Controller::curr(), 'CMSMain')) {
return Controller::curr()->AbsoluteLink('show') . $this->record->ID;
}
}
}
/**
* @package cms
* @subpackage content
*/
class SilverStripeNavigatorItem_StageLink extends SilverStripeNavigatorItem {
static $priority = 20;
function getHTML() {
if(Versioned::current_stage() == 'Stage' && !(ClassInfo::exists('SiteTreeFutureState') && SiteTreeFutureState::get_future_datetime())) {
return "<a class=\"current\">". _t('ContentController.DRAFTSITE', 'Draft Site') ."</a>";
} else {
$draftPage = Versioned::get_one_by_stage('SiteTree', 'Stage', '"SiteTree"."ID" = ' . $this->record->ID);
if($draftPage) {
$this->recordLink = Controller::join_links($draftPage->AbsoluteLink(), "?stage=Stage");
return "<a href=\"$this->recordLink\">". _t('ContentController.DRAFTSITE', 'Draft Site') ."</a>";
}
}
}
function getMessage() {
if(Versioned::current_stage() == 'Stage') {
return "<div id=\"SilverStripeNavigatorMessage\" title=\"". _t('ContentControl.NOTEWONTBESHOWN', 'Note: this message will not be shown to your visitors') ."\">". _t('ContentController.DRAFTSITE', 'Draft Site') ."</div>";
}
}
function getLink() {
if(Versioned::current_stage() == 'Stage') {
return Controller::join_links($this->record->AbsoluteLink(), '?stage=Stage');
}
}
}
/**
* @package cms
* @subpackage content
*/
class SilverStripeNavigatorItem_LiveLink extends SilverStripeNavigatorItem {
static $priority = 30;
function getHTML() {
if(Versioned::current_stage() == 'Live') {
return "<a class=\"current\">". _t('ContentController.PUBLISHEDSITE', 'Published Site') ."</a>";
} else {
$livePage = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $this->record->ID);
if($livePage) {
$this->recordLink = Controller::join_links($livePage->AbsoluteLink(), "?stage=Live");
return "<a href=\"$this->recordLink\">". _t('ContentController.PUBLISHEDSITE', 'Published Site') ."</a>";
}
}
}
function getMessage() {
if(Versioned::current_stage() == 'Live') {
return "<div id=\"SilverStripeNavigatorMessage\" title=\"". _t('ContentControl.NOTEWONTBESHOWN', 'Note: this message will not be shown to your visitors') ."\">". _t('ContentController.PUBLISHEDSITE', 'Published Site') ."</div>";
}
}
function getLink() {
if(Versioned::current_stage() == 'Live') {
return Controller::join_links($this->record->AbsoluteLink(), '?stage=Live');
}
}
}
/**
* @package cms
* @subpackage content
*/
class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem {
static $priority = 40;
function getHTML() {
if(Versioned::current_archived_date()) {
return "<a class=\"current\">". _t('ContentController.ARCHIVEDSITE', 'Archived Site') ."</a>";
} else {
// Display the archive link if the page currently displayed in the CMS is other version than live and draft
$currentDraft = Versioned::get_one_by_stage('SiteTree', 'Draft', '"SiteTree"."ID" = ' . $this->record->ID);
$currentLive = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $this->record->ID);
if(
(!$currentDraft || ($currentDraft && $this->record->Version != $currentDraft->Version))
&& (!$currentLive || ($currentLive && $this->record->Version != $currentLive->Version))
) {
$this->recordLink = $this->record->AbsoluteLink();
return "<a href=\"$this->recordLink?archiveDate={$this->record->LastEdited}\" target=\"_blank\">". _t('ContentController.ARCHIVEDSITE', 'Archived Site') ."</a>";
}
}
}
function getMessage() {
if($date = Versioned::current_archived_date()) {
$dateObj = Object::create('Datetime');
$dateObj->setValue($date);
return "<div id=\"SilverStripeNavigatorMessage\" title=\"". _t('ContentControl.NOTEWONTBESHOWN', 'Note: this message will not be shown to your visitors') ."\">". _t('ContentController.ARCHIVEDSITEFROM', 'Archived site from') ."<br>" . $dateObj->Nice() . "</div>";
}
}
function getLink() {
if($date = Versioned::current_archived_date()) {
return $this->record->AbsoluteLink() . '?archiveDate=' . $date;
}
}
}
?>

View File

@ -1,189 +0,0 @@
<?php
/**
* @package cms
* @subpackage content
*/
class SilverStripeNavigator {
/**
* @param SiteTree $record
* @return Array template data
*/
static function get_for_record($record) {
$items = '';
$message = '';
$navItemClasses = ClassInfo::subclassesFor('SilverStripeNavigatorItem');
array_shift($navItemClasses);
// Sort menu items according to priority
$menuPriority = array();
$i = 0;
foreach($navItemClasses as $navItemClass) {
if($navItemClass == 'SilverStripeNavigatorItem') continue;
$i++;
$obj = new $navItemClass();
// This funny litle formula ensures that the first item added with the same priority will be left-most.
$priority = Object::get_static($navItemClass, 'priority');
$menuPriority[$priority * 100 - 1] = $obj;
}
ksort($menuPriority);
foreach($menuPriority as $obj) {
$text = $obj->getHTML($record);
if($text) $items .= $text;
$newMessage = $obj->getMessage($record);
if($newMessage) $message = $newMessage;
}
return array(
'items' => $items,
'message' => $message
);
}
}
/**
* Navigator items are links that appear in the $SilverStripeNavigator bar.
* To add an item, extends this class.
*
* @package cms
* @subpackage content
*/
class SilverStripeNavigatorItem extends Object {
function getHTML($page) {}
function getMessage($page) {}
}
/**
* @package cms
* @subpackage content
*/
class SilverStripeNavigatorItem_CMSLink extends SilverStripeNavigatorItem {
static $priority = 10;
function getHTML($page) {
if(is_a(Controller::curr(), 'CMSMain')) {
return '<a class="current">CMS</a>';
} else {
$cmsLink = 'admin/show/' . $page->ID;
$cmsLink = "<a href=\"$cmsLink\" class=\"newWindow\" target=\"cms\">". _t('ContentController.CMS', 'CMS') ."</a>";
return $cmsLink;
}
}
function getLink($page) {
if(is_a(Controller::curr(), 'CMSMain')) {
return Controller::curr()->AbsoluteLink('show') . $page->ID;
}
}
}
/**
* @package cms
* @subpackage content
*/
class SilverStripeNavigatorItem_StageLink extends SilverStripeNavigatorItem {
static $priority = 20;
function getHTML($page) {
if(Versioned::current_stage() == 'Stage' && !(ClassInfo::exists('SiteTreeFutureState') && SiteTreeFutureState::get_future_datetime())) {
return "<a class=\"current\">". _t('ContentController.DRAFTSITE', 'Draft Site') ."</a>";
} else {
$draftPage = Versioned::get_one_by_stage('SiteTree', 'Stage', '"SiteTree"."ID" = ' . $page->ID);
if($draftPage) {
$pageLink = Controller::join_links($draftPage->AbsoluteLink(), "?stage=Stage");
return "<a href=\"$pageLink\" class=\"newWindow\" target=\"site\" style=\"left : -1px;\">". _t('ContentController.DRAFTSITE', 'Draft Site') ."</a>";
}
}
}
function getMessage($page) {
if(Versioned::current_stage() == 'Stage') {
return "<div id=\"SilverStripeNavigatorMessage\" title=\"". _t('ContentControl.NOTEWONTBESHOWN', 'Note: this message will not be shown to your visitors') ."\">". _t('ContentController.DRAFTSITE', 'Draft Site') ."</div>";
}
}
function getLink($page) {
if(Versioned::current_stage() == 'Stage') {
return Controller::join_links($page->AbsoluteLink(), '?stage=Stage');
}
}
}
/**
* @package cms
* @subpackage content
*/
class SilverStripeNavigatorItem_LiveLink extends SilverStripeNavigatorItem {
static $priority = 30;
function getHTML($page) {
if(Versioned::current_stage() == 'Live') {
return "<a class=\"current\">". _t('ContentController.PUBLISHEDSITE', 'Published Site') ."</a>";
} else {
$livePage = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $page->ID);
if($livePage) {
$pageLink = Controller::join_links($livePage->AbsoluteLink(), "?stage=Live");
return "<a href=\"$pageLink\" class=\"newWindow\" target=\"site\" style=\"left : -3px;\">". _t('ContentController.PUBLISHEDSITE', 'Published Site') ."</a>";
}
}
}
function getMessage($page) {
if(Versioned::current_stage() == 'Live') {
return "<div id=\"SilverStripeNavigatorMessage\" title=\"". _t('ContentControl.NOTEWONTBESHOWN', 'Note: this message will not be shown to your visitors') ."\">". _t('ContentController.PUBLISHEDSITE', 'Published Site') ."</div>";
}
}
function getLink($page) {
if(Versioned::current_stage() == 'Live') {
return Controller::join_links($page->AbsoluteLink(), '?stage=Live');
}
}
}
/**
* @package cms
* @subpackage content
*/
class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem {
static $priority = 40;
function getHTML($page) {
if(Versioned::current_archived_date()) {
return "<a class=\"current\">". _t('ContentController.ARCHIVEDSITE', 'Archived Site') ."</a>";
} else {
// Display the archive link if the page currently displayed in the CMS is other version than live and draft
$currentDraft = Versioned::get_one_by_stage('SiteTree', 'Draft', '"SiteTree"."ID" = ' . $page->ID);
$currentLive = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $page->ID);
if(
(!$currentDraft || ($currentDraft && $page->Version != $currentDraft->Version))
&& (!$currentLive || ($currentLive && $page->Version != $currentLive->Version))
) {
$pageLink = $page->AbsoluteLink();
return "<a href=\"$pageLink?archiveDate={$page->LastEdited}\" class=\"newWindow\" target=\"site\" style=\"left : -3px;\">". _t('ContentController.ARCHIVEDSITE', 'Archived Site') ."</a>";
}
}
}
function getMessage($page) {
if($date = Versioned::current_archived_date()) {
$dateObj = Object::create('Datetime');
$dateObj->setValue($date);
return "<div id=\"SilverStripeNavigatorMessage\" title=\"". _t('ContentControl.NOTEWONTBESHOWN', 'Note: this message will not be shown to your visitors') ."\">". _t('ContentController.ARCHIVEDSITEFROM', 'Archived site from') ."<br>" . $dateObj->Nice() . "</div>";
}
}
function getLink($page) {
if($date = Versioned::current_archived_date()) {
return $page->AbsoluteLink() . '?archiveDate=' . $date;
}
}
}
?>

View File

@ -0,0 +1,54 @@
<?php
/**
* @package cms
* @subpackage tests
*/
class SilverStripeNavigatorTest extends SapphireTest {
static $fixture_file = 'cms/tests/controller/CMSMainTest.yml';
function testGetItems() {
$page = $this->objFromFixture('Page', 'page1');
$navigator = new SilverStripeNavigator($page);
$items = $navigator->getItems();
$classes = array_map('get_class', $items->toArray());
$this->assertContains('SilverStripeNavigatorItem_LiveLink', $classes,
'Adds default classes'
);
$this->assertContains('SilverStripeNavigatorTest_TestItem', $classes,
'Autodiscovers new classes'
);
}
function testCanView() {
$page = $this->objFromFixture('Page', 'page1');
$admin = $this->objFromFixture('Member', 'admin');
$author = $this->objFromFixture('Member', 'assetsonlyuser');
$navigator = new SilverStripeNavigator($page);
// TODO Shouldn't be necessary but SapphireTest logs in as ADMIN by default
$this->logInWithPermission('CMS_ACCESS_AssetAdmin');
$items = $navigator->getItems();
$classes = array_map('get_class', $items->toArray());
$this->assertNotContains('SilverStripeNavigatorTest_ProtectedTestItem', $classes);
$this->logInWithPermission('ADMIN');
$items = $navigator->getItems();
$classes = array_map('get_class', $items->toArray());
$this->assertContains('SilverStripeNavigatorTest_ProtectedTestItem', $classes);
}
}
class SilverStripeNavigatorTest_TestItem extends SilverStripeNavigatorItem implements TestOnly {
}
class SilverStripeNavigatorTest_ProtectedTestItem extends SilverStripeNavigatorItem implements TestOnly {
function canView($member = null) {
if(!$member) $member = Member::currentUser();
return Permission::checkMember($member, 'ADMIN');
}
}