Version: current

      1 <?php
      2 /**
      3  * Decorator designed to add subsites support to LeftAndMain
      4  *
      5  * @package subsites
      6  */
      7 class LeftAndMainSubsites extends Extension {
      8     static $allowed_actions = array(
      9         'changesubsite',
     10     );
     11
     12     function augmentInit() {
     13         Requirements::css('subsites/css/LeftAndMain_Subsites.css');
     14         Requirements::javascript('subsites/javascript/LeftAndMain_Subsites.js');
     15         Requirements::javascript('subsites/javascript/VirtualPage_Subsites.js');
     16     }
     17
     18     /**
     19      * Set the title of the CMS tree
     20      */
     21     function getCMSTreeTitle() {
     22         $subsite = Subsite::currentSubSite();
     23         return $subsite ? htmlspecialchars($subsite->Title, ENT_QUOTES) : 'Site Content';
     24     }
     25
     26     function updatePageOptions(&$fields) {
     27         $fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID()));
     28     }
     29
     30
     31     public function changesubsite() {
     32         $id = $_REQUEST['SubsiteID'];
     33         Subsite::changeSubsite($id==-1 ? 0 : $id);
     34
     35         if ($id == '-1') Cookie::set('noSubsiteFilter', 'true', 1);
     36         else Cookie::set('noSubsiteFilter', 'false', 1);
     37
     38         if(Director::is_ajax()) {
     39             $tree = '';
     40             if ($this->owner->hasMethod('SiteTreeAsUL')) {
     41                 $tree = $this->owner->SiteTreeAsUL();
     42                 $tree = ereg_replace('^[ \t\r\n]*<ul[^>]*>','', $tree);
     43                 $tree = ereg_replace('</ul[^>]*>[ \t\r\n]*$','', $tree);
     44             }
     45             return $tree;
     46         } else
     47             return array();
     48     }
     49
     50     public function Subsites() {
     51         $accessPerm = 'CMS_ACCESS_'. $this->owner->class;
     52         $subsites = false;
     53         switch($this->owner->class) {
     54             case "AssetAdmin":
     55                 $subsites = Subsite::accessible_sites($accessPerm, true, "Shared files & images");
     56                 break;
     57
     58             case "SecurityAdmin":
     59                 $subsites = Subsite::accessible_sites($accessPerm, true, "Groups accessing all sites");
     60                 if($subsites->find('ID',0)) {
     61                     $subsites->push(new ArrayData(array('Title' => 'All groups', 'ID' => -1)));
     62                 }
     63                 break;
     64
     65             case "CMSMain":
     66             case "CustomMenuAdmin":
     67                 if (singleton($this->owner->class)->hasMethod('SiteTreeAsUL')) {
     68                     // If there's a default site then main site has no meaning
     69                     $showMainSite = !DataObject::get_one('Subsite',"\"DefaultSite\"=1 AND \"IsPublic\"=1");
     70                     $subsites = Subsite::accessible_sites($accessPerm, $showMainSite);
     71                 }
     72                 break;
     73
     74             case "ReportAdmin":
     75                 // не используем
     76                 break;
     77
     78             default:
     79                 $showSubsitesMenu = true;
     80                 $admin = singleton($this->owner->class);
     81                 if ($admin->is_a('ModelAdmin') && ($managedModels = $admin::$managed_models)) {
     82                     $showSubsitesMenu = false;
     83                     if (is_array($managedModels)) {
     84                         foreach($managedModels as $managedModel) {
     85                             if (Object::has_extension($managedModel, 'DataObjectSubsites') || Object::has_extension($managedModel, 'SiteTreeSubsites')) {
     86                                 $showSubsitesMenu = true;
     87                             }
     88                         }
     89                     } else {
     90                         if (Object::has_extension($managedModels, 'DataObjectSubsites') || Object::has_extension($managedModels, 'SiteTreeSubsites')) {
     91                             $showSubsitesMenu = true;
     92                         }
     93                     }
     94                 }
     95                 if ($showSubsitesMenu) {
     96                     $showMainSite = !DataObject::get_one('Subsite',"\"DefaultSite\"=1 AND \"IsPublic\"=1");
     97                     $subsites = Subsite::accessible_sites($accessPerm, $showMainSite);
     98                 }
     99                 break;
    100         }
    101
    102         return $subsites;
    103     }
    104
    105     public function SubsiteList() {
    106         if ($this->owner->class == 'AssetAdmin') {
    107             // See if the right decorator is there....
    108             $file = new File();
    109             if (!$file->hasExtension('FileSubsites')) {
    110                 return false;
    111             }
    112         }
    113
    114         $list = $this->Subsites();
    115         if (!$list) {
    116             return false;
    117         }
    118
    119         $currentSubsiteID = Subsite::currentSubsiteID();
    120
    121         //$currentSubsiteID = ($requestSubsiteID) ? $requestSubsiteID : Session::get('SubsiteID');
    122
    123         if($list->Count() > 1) {
    124             $output = '<select id="SubsitesSelect">';
    125
    126             foreach($list as $subsite) {
    127                 $selected = $subsite->ID == $currentSubsiteID ? ' selected="selected"' : '';
    128
    129                 $output .= "\n<option value=\"{$subsite->ID}\"$selected>".htmlspecialchars($subsite->Title, ENT_QUOTES)."</option>";
    130             }
    131
    132             $output .= '</select>';
    133
    134             Requirements::css('subsites/css/LeftAndMain_Subsites.css');
    135             Requirements::javascript('subsites/javascript/LeftAndMain_Subsites.js');
    136             return $output;
    137         } else if($list->Count() == 1) {
    138 //          Requirements::css('subsites/css/LeftAndMain_Subsites.css');
    139 //          return $list->First()->Title;
    140         }
    141     }
    142
    143     public function CanAddSubsites() {
    144         return Permission::check("ADMIN", "any", null, "all");
    145     }
    146
    147     /**
    148      * Alternative security checker for LeftAndMain.
    149      * If security isn't found, then it will switch to a subsite where we do have access.
    150      */
    151     public function alternateAccessCheck() {
    152         $className = $this->owner->class;
    153
    154         // Switch to the subsite of the current page
    155         if ($this->owner->class == 'CMSMain' && $currentPage = $this->owner->currentPage()) {
    156             if (Subsite::currentSubsiteID() != $currentPage->SubsiteID) {
    157                 Subsite::changeSubsite($currentPage->SubsiteID);
    158             }
    159         }
    160
    161         // Switch to a subsite that this user can actually access.
    162         $member = Member::currentUser();
    163         if ($member && $member->isAdmin()) return true; //admin can access all subsites
    164
    165         $mainSitePermission = Subsite::hasMainSitePermission($member, array("CMS_ACCESS_{$this->owner->class}"));
    166         $sites = Subsite::accessible_sites("CMS_ACCESS_{$this->owner->class}", $mainSitePermission)->toDropdownMap();
    167         if($sites && !isset($sites[Subsite::currentSubsiteID()])) {
    168             $siteIDs = array_keys($sites);
    169             Subsite::changeSubsite($siteIDs[0]);
    170             return true;
    171         }
    172
    173         // Switch to a different top-level menu item
    174         $menu = CMSMenu::get_menu_items();
    175         foreach($menu as $candidate) {
    176             if($candidate->controller != $this->owner->class) {
    177
    178                 $sites = Subsite::accessible_sites("CMS_ACCESS_{$candidate->controller}", $mainSitePermission)->toDropdownMap();
    179                 if($sites && !isset($sites[Subsite::currentSubsiteID()])) {
    180                     $siteIDs = array_keys($sites);
    181                     Subsite::changeSubsite($siteIDs[0]);
    182                     $cClass = $candidate->controller;
    183                     $cObj = new $cClass();
    184                     Director::redirect($cObj->Link());
    185                     return null;
    186                 }
    187             }
    188         }
    189
    190         // If all of those fail, you really don't have access to the CMS
    191         return null;
    192     }
    193
    194     function augmentNewSiteTreeItem(&$item) {
    195         $item->SubsiteID = isset($_POST['SubsiteID']) ? $_POST['SubsiteID'] : Subsite::currentSubsiteID();
    196     }
    197
    198     function onAfterSave($record) {
    199         if($record->hasMethod('NormalRelated') && ($record->NormalRelated() || $record->ReverseRelated())) {
    200             FormResponse::status_message('Saved, please update related pages.', 'good');
    201         }
    202     }
    203 }
    204
    205
    206
    207 ?>
    208

[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. -