2014-08-13 02:41:18 +02:00
|
|
|
<?php
|
|
|
|
|
2017-04-24 19:17:09 +02:00
|
|
|
namespace JonoM\BetterNavigator\Extension;
|
|
|
|
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
use SilverStripe\CMS\Controllers\SilverStripeNavigator;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
|
2016-07-22 23:33:28 +02:00
|
|
|
class BetterNavigatorExtension extends DataExtension {
|
2014-08-13 02:41:18 +02:00
|
|
|
|
2015-12-18 00:58:50 +01:00
|
|
|
/**
|
|
|
|
* Provides a front-end utility menu with administrative functions and developer tools
|
|
|
|
* Relies on SilverStripeNavigator
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function BetterNavigator() {
|
2017-04-24 19:17:09 +02:00
|
|
|
|
2015-12-18 04:34:13 +01:00
|
|
|
// Make sure this is a page
|
2017-04-24 19:17:09 +02:00
|
|
|
if (!$this->isAPage()) return false;
|
2014-08-13 02:41:18 +02:00
|
|
|
|
2015-12-18 04:34:13 +01:00
|
|
|
// Only show navigator to appropriate users
|
|
|
|
$isDev = Director::isDev();
|
|
|
|
$canViewDraft = (Permission::check('VIEW_DRAFT_CONTENT') || Permission::check('CMS_ACCESS_CMSMain'));
|
2017-04-24 19:17:09 +02:00
|
|
|
if ($isDev || $canViewDraft) {
|
2015-12-18 04:34:13 +01:00
|
|
|
// Get SilverStripeNavigator links & stage info (CMS/Stage/Live/Archive)
|
2017-04-24 19:17:09 +02:00
|
|
|
$nav = [];
|
2015-12-18 04:34:13 +01:00
|
|
|
$viewing = '';
|
2017-04-24 19:17:09 +02:00
|
|
|
$navigator = SilverStripeNavigator::create($this->owner->dataRecord);
|
2015-12-18 04:34:13 +01:00
|
|
|
$items = $navigator->getItems();
|
2017-04-24 19:17:09 +02:00
|
|
|
foreach ($items as $item) {
|
2015-12-18 04:34:13 +01:00
|
|
|
$name = $item->getName();
|
|
|
|
$active = $item->isActive();
|
2017-04-24 19:17:09 +02:00
|
|
|
$nav[$name] = [
|
2015-12-18 04:34:13 +01:00
|
|
|
'Link' => $item->getLink(),
|
|
|
|
'Active' => $active
|
2017-04-24 19:17:09 +02:00
|
|
|
];
|
2015-12-18 04:34:13 +01:00
|
|
|
if ($active) {
|
|
|
|
if ($name == 'LiveLink') $viewing = 'Live';
|
|
|
|
if ($name == 'StageLink') $viewing = 'Draft';
|
|
|
|
if ($name == 'ArchiveLink') $viewing = 'Archived';
|
2015-12-18 00:58:50 +01:00
|
|
|
}
|
2015-12-18 04:34:13 +01:00
|
|
|
}
|
|
|
|
// Only show edit link if user has permission to edit this page
|
2017-04-24 19:17:09 +02:00
|
|
|
$editLink = false;
|
|
|
|
if ($this->owner->dataRecord->canEdit() && Permission::check('CMS_ACCESS_CMSMain') || $isDev) {
|
|
|
|
$editLink = $nav['CMSLink']['Link'];
|
|
|
|
}
|
2015-12-18 00:58:50 +01:00
|
|
|
|
2015-12-18 04:34:13 +01:00
|
|
|
// Is the logged in member nominated as a developer?
|
|
|
|
$member = Member::currentUser();
|
|
|
|
$devs = Config::inst()->get('BetterNavigator', 'developers');
|
2016-02-10 00:04:12 +01:00
|
|
|
$identifierField = Member::config()->unique_identifier_field;
|
|
|
|
$isDeveloper = $member && is_array($devs) ? in_array($member->{$identifierField}, $devs) : false;
|
2015-12-18 00:58:50 +01:00
|
|
|
|
2015-12-18 04:34:13 +01:00
|
|
|
// Add other data for template
|
|
|
|
$backURL = '?BackURL=' . urlencode($this->owner->Link());
|
2017-04-24 19:17:09 +02:00
|
|
|
$bNData = array_merge($nav, [
|
2015-12-18 04:34:13 +01:00
|
|
|
'Member' => $member,
|
2017-04-24 19:17:09 +02:00
|
|
|
'Stage' => Versioned::get_stage(),
|
2015-12-18 04:34:13 +01:00
|
|
|
'Viewing' => $viewing, // What we're viewing doesn't necessarily align with the active Stage
|
2017-03-23 00:34:49 +01:00
|
|
|
'LoginLink' => Controller::join_links(Director::absoluteBaseURL(), Config::inst()->get('Security', 'login_url'), $backURL),
|
|
|
|
'LogoutLink' => Controller::join_links(Director::absoluteBaseURL() . 'Security/logout', $backURL),
|
2015-12-18 04:34:13 +01:00
|
|
|
'EditLink' => $editLink,
|
|
|
|
'Mode' => Director::get_environment_type(),
|
|
|
|
'IsDeveloper' => $isDeveloper
|
2017-04-24 19:17:09 +02:00
|
|
|
]);
|
2015-12-18 00:58:50 +01:00
|
|
|
|
2015-12-18 04:34:13 +01:00
|
|
|
// Merge with page data, send to template and render
|
|
|
|
$bNData = new ArrayData($bNData);
|
2017-04-24 19:17:09 +02:00
|
|
|
$page = $this->owner->customise(['BetterNavigator' => $bNData]);
|
|
|
|
return $page->renderWith('BetterNavigator\\BetterNavigator');
|
2015-12-18 00:58:50 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-24 19:17:09 +02:00
|
|
|
|
|
|
|
protected function isAPage() {
|
|
|
|
return $this->owner
|
|
|
|
&& $this->owner->dataRecord
|
|
|
|
&& $this->owner->dataRecord instanceof SiteTree
|
|
|
|
&& $this->owner->dataRecord->ID > 0;
|
|
|
|
}
|
2014-10-06 04:19:56 +02:00
|
|
|
}
|