2011-07-15 16:05:58 +02:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Controllers;
|
|
|
|
|
2017-03-10 04:33:32 +01:00
|
|
|
use SilverStripe\ORM\CMSPreviewable;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\View\ViewableData;
|
2016-06-02 05:56:45 +02:00
|
|
|
|
2011-07-15 16:05:58 +02:00
|
|
|
/**
|
|
|
|
* 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).
|
2011-07-21 20:00:48 +02:00
|
|
|
* The class can be used for any {@link DataObject} subclass implementing the {@link CMSPreviewable} interface.
|
2016-03-08 21:50:55 +01:00
|
|
|
*
|
2011-07-15 16:05:58 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class SilverStripeNavigator extends ViewableData
|
|
|
|
{
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
2017-03-10 04:33:32 +01:00
|
|
|
* @var DataObject|\SilverStripe\ORM\CMSPreviewable
|
2017-01-25 21:59:25 +01:00
|
|
|
*/
|
|
|
|
protected $record;
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
2017-03-10 04:33:32 +01:00
|
|
|
* @param DataObject|\SilverStripe\ORM\CMSPreviewable $record
|
2017-01-25 21:59:25 +01:00
|
|
|
*/
|
|
|
|
public function __construct(CMSPreviewable $record)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->record = $record;
|
|
|
|
}
|
2011-07-15 16:05:58 +02:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
|
|
|
* @return SS_List of SilverStripeNavigatorItem
|
|
|
|
*/
|
|
|
|
public function getItems()
|
|
|
|
{
|
2020-04-19 06:18:01 +02:00
|
|
|
$items = [];
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-09-20 03:51:07 +02:00
|
|
|
$classes = ClassInfo::subclassesFor(SilverStripeNavigatorItem::class);
|
|
|
|
array_shift($classes);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
// Sort menu items according to priority
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
/** @var SilverStripeNavigatorItem $item */
|
|
|
|
$item = new $class($this->record);
|
|
|
|
if (!$item->canView()) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
// This funny litle formula ensures that the first item added with the same priority will be left-most.
|
|
|
|
$priority = $item->getPriority() * 100 - 1;
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
// Ensure that we can have duplicates with the same (default) priority
|
|
|
|
while (isset($items[$priority])) {
|
|
|
|
$priority++;
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$items[$priority] = $item;
|
|
|
|
}
|
|
|
|
ksort($items);
|
2011-07-21 20:00:48 +02:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
// Drop the keys and let the ArrayList handle the numbering, so $First, $Last and others work properly.
|
|
|
|
return new ArrayList(array_values($items));
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
2017-03-10 04:33:32 +01:00
|
|
|
* @return DataObject|\SilverStripe\ORM\CMSPreviewable
|
2017-01-25 21:59:25 +01:00
|
|
|
*/
|
|
|
|
public function getRecord()
|
|
|
|
{
|
|
|
|
return $this->record;
|
|
|
|
}
|
2011-07-15 16:05:58 +02:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
|
|
|
* @param DataObject|CMSPreviewable $record
|
|
|
|
* @return array template data
|
|
|
|
*/
|
|
|
|
public 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 && $item->isActive()) {
|
|
|
|
$message = $newMessage;
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2020-04-19 06:18:01 +02:00
|
|
|
return [
|
2017-01-25 21:59:25 +01:00
|
|
|
'items' => $html,
|
|
|
|
'message' => $message
|
2020-04-19 06:18:01 +02:00
|
|
|
];
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
2011-07-15 16:05:58 +02:00
|
|
|
}
|