Version: current

      1 <?php
      2 /**
      3  * @package cms
      4  * @subpackage content
      5  */
      6 class SilverStripeNavigator {
      7
      8     /**
      9      * @param SiteTree $record
     10      * @return Array template data
     11      */
     12     static function get_for_record($record) {
     13         $items = '';
     14         $message = '';
     15
     16         $navItemClasses = ClassInfo::subclassesFor('SilverStripeNavigatorItem');
     17         array_shift($navItemClasses);
     18
     19         // Sort menu items according to priority
     20         $menuPriority = array();
     21         $i = 0;
     22         foreach($navItemClasses as $navItemClass) {
     23             if($navItemClass == 'SilverStripeNavigatorItem') continue;
     24
     25             $i++;
     26             $obj = new $navItemClass();
     27             // This funny litle formula ensures that the first item added with the same priority will be left-most.
     28             $priority = Object::get_static($navItemClass, 'priority');
     29             $menuPriority[$priority * 100 - 1] = $obj;
     30         }
     31         ksort($menuPriority);
     32
     33         foreach($menuPriority as $obj) {
     34
     35             $text = $obj->getHTML($record);
     36             if($text) $items .= $text;
     37             $newMessage = $obj->getMessage($record);
     38             if($newMessage) $message = $newMessage;
     39         }
     40
     41         return array(
     42             'items' => $items,
     43             'message' => $message
     44         );
     45     }
     46 }
     47
     48 /**
     49  * Navigator items are links that appear in the $SilverStripeNavigator bar.
     50  * To add an item, extends this class.
     51  *
     52  * @package cms
     53  * @subpackage content
     54  */
     55 class SilverStripeNavigatorItem extends Object {
     56     function getHTML($page) {}
     57     function getMessage($page) {}
     58 }
     59
     60 /**
     61  * @package cms
     62  * @subpackage content
     63  */
     64 class SilverStripeNavigatorItem_CMSLink extends SilverStripeNavigatorItem {
     65     static $priority = 10;
     66
     67     function getHTML($page) {
     68         if(is_a(Controller::curr(), 'CMSMain')) {
     69             return '<a class="current">CMS</a>';
     70         } else {
     71             $cmsLink = 'admin/show/' . $page->ID;
     72             $cmsLink = "<a href=\"$cmsLink\" class=\"newWindow\" target=\"cms\">". _t('ContentController.CMS', 'CMS') ."</a>";
     73
     74             return $cmsLink;
     75         }
     76     }
     77
     78     function getLink($page) {
     79         if(is_a(Controller::curr(), 'CMSMain')) {
     80             return Controller::curr()->AbsoluteLink('show') . $page->ID;
     81         }
     82     }
     83
     84 }
     85
     86 /**
     87  * @package cms
     88  * @subpackage content
     89  */
     90 class SilverStripeNavigatorItem_StageLink extends SilverStripeNavigatorItem {
     91     static $priority = 20;
     92
     93     function getHTML($page) {
     94         // TODO cmsworkflow module coupling
     95         if(Versioned::current_stage() == 'Stage' && !(ClassInfo::exists('SiteTreeFutureState') && SiteTreeFutureState::get_future_datetime())) {
     96             return "<a class=\"current\">". _t('ContentController.DRAFTSITE', 'Draft Site') ."</a>";
     97         } else {
     98             $draftPage = Versioned::get_one_by_stage('SiteTree', 'Stage', '"SiteTree"."ID" = ' . $page->ID);
     99             if($draftPage) {
    100                 $pageLink = $draftPage->AbsoluteLink();
    101                 return "<a href=\"$pageLink?stage=Stage\" class=\"newWindow\" target=\"site\" style=\"left : -1px;\">". _t('ContentController.DRAFTSITE', 'Draft Site') ."</a>";
    102             }
    103         }
    104     }
    105
    106     function getMessage($page) {
    107         if(Versioned::current_stage() == 'Stage') {
    108             return "<div id=\"SilverStripeNavigatorMessage\" title=\"". _t('ContentControl.NOTEWONTBESHOWN', 'Note: this message will not be shown to your visitors') ."\">".  _t('ContentController.DRAFTSITE', 'Draft Site') ."</div>";
    109         }
    110     }
    111
    112     function getLink($page) {
    113         if(Versioned::current_stage() == 'Stage') {
    114             return $page->AbsoluteLink() . '?stage=Stage';
    115         }
    116     }
    117 }
    118
    119 /**
    120  * @package cms
    121  * @subpackage content
    122  */
    123 class SilverStripeNavigatorItem_LiveLink extends SilverStripeNavigatorItem {
    124     static $priority = 30;
    125
    126     function getHTML($page) {
    127         if(Versioned::current_stage() == 'Live') {
    128             return "<a class=\"current\">". _t('ContentController.PUBLISHEDSITE', 'Published Site') ."</a>";
    129         } else {
    130             $livePage = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $page->ID);
    131             if($livePage) {
    132                 $pageLink = $livePage->AbsoluteLink();
    133                 return "<a href=\"$pageLink?stage=Live\" class=\"newWindow\" target=\"site\" style=\"left : -3px;\">". _t('ContentController.PUBLISHEDSITE', 'Published Site') ."</a>";
    134             }
    135         }
    136     }
    137
    138     function getMessage($page) {
    139         if(Versioned::current_stage() == 'Live') {
    140             return "<div id=\"SilverStripeNavigatorMessage\" title=\"". _t('ContentControl.NOTEWONTBESHOWN', 'Note: this message will not be shown to your visitors') ."\">".  _t('ContentController.PUBLISHEDSITE', 'Published Site') ."</div>";
    141         }
    142     }
    143
    144     function getLink($page) {
    145         if(Versioned::current_stage() == 'Live') {
    146             return $page->AbsoluteLink() . '?stage=Live';
    147         }
    148     }
    149 }
    150
    151 /**
    152  * @package cms
    153  * @subpackage content
    154  */
    155 class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem {
    156     static $priority = 40;
    157
    158     function getHTML($page) {
    159         if(Versioned::current_archived_date()) {
    160             return "<a class=\"current\">". _t('ContentController.ARCHIVEDSITE', 'Archived Site') ."</a>";
    161         } else {
    162             // Display the archive link if the page currently displayed in the CMS is other version than live and draft
    163             $currentDraft = Versioned::get_one_by_stage('SiteTree', 'Draft', '"SiteTree"."ID" = ' . $page->ID);
    164             $currentLive = Versioned::get_one_by_stage('SiteTree', 'Live', '"SiteTree"."ID" = ' . $page->ID);
    165             if ($currentDraft && $currentLive && $page->Version!=$currentDraft->Version && $page->Version!=$currentLive->Version) {
    166                 $pageLink = $page->AbsoluteLink();
    167                 return "<a href=\"$pageLink?archiveDate={$page->LastEdited}\" class=\"newWindow\" target=\"site\" style=\"left : -3px;\">". _t('ContentController.ARCHIVEDSITE', 'Archived Site') ."</a>";
    168             }
    169         }
    170     }
    171
    172     function getMessage($page) {
    173         if($date = Versioned::current_archived_date()) {
    174             $dateObj = Object::create('Datetime');
    175             $dateObj->setValue($date);
    176             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>";
    177         }
    178     }
    179
    180     function getLink($page) {
    181         if($date = Versioned::current_archived_date()) {
    182             return $page->AbsoluteLink() . '?archiveDate=' . $date;
    183         }
    184     }
    185 }
    186
    187 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -