Version: current

     1 <?php
     2 class SubsitesSelectorPage extends Page {
     3
     4     static $icon = 'webylon/img/home';
     5
     6     static $db = array(
     7         'GotoSubsite' => 'Int',
     8     );
     9
    10     static $defaults = array(
    11         'AutoChild' => 0,
    12         'DevEditType' => 'Fixed',
    13         'GotoSubsite' => -1,
    14     );
    15
    16     function canCreate() {
    17         return Director::isDev();
    18     }
    19
    20     function canDelete() {
    21         return Director::isDev();
    22     }
    23
    24     function getCMSFields() {
    25         $fields = parent::getCMSFields();
    26
    27         $subsites = DataObject::get('Subsite');
    28         if(!$subsites) $subsites = new DataObjectSet();
    29         $subsites->unshift(new ArrayData(array('Title' => _t('SubsitesSelectorPage.AUTO_REDIRECT', 'Auto'), 'ID' => -1)));
    30         $subsites->unshift(new ArrayData(array('Title' => _t('SubsitesSelectorPage.NO_REDIRECT', 'No redirect'), 'ID' => 0)));
    31
    32         $subsiteSelectionField = new DropdownField('GotoSubsite', _t('SubsitesSelectorPage.CHOOSE_REDIRECT','Redirect to'), $subsites->toDropdownMap('ID', 'Title'));
    33
    34         $fields->addFieldToTab('Root.Content.Main', $subsiteSelectionField, 'Title');
    35
    36         // TODO убрать лишние поля из админки
    37         // TODO может сделать экстеншен к RedirectorPage для выбора страниц всех подсайтов?
    38         return $fields;
    39     }
    40
    41 }
    42
    43 class SubsitesSelectorPage_Controller extends Page_Controller {
    44
    45     function init(){
    46         if ($this->GotoSubsite) {
    47             $subsite = false;
    48             if ($this->GotoSubsite < 0) {
    49                 // вырежем первый язык
    50                 $userLang = preg_replace('/;.*$/', '', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
    51                 // $userLang = q=0.8,ru-RU ?
    52                 $userLang = preg_replace('/q=[\d.]+,/', '', $userLang);
    53
    54                 // $userLang = ru-RU,ru ?
    55                 $userLang = preg_replace('/,.*$/', '', $userLang);
    56
    57                 if (strlen($userLang) > 2) {
    58                     // $userLang = ru-RU
    59                     $userLang = preg_replace('/-/', '_', $userLang);
    60                     $subsite = DataObject::get_one('Subsite', "IsPublic=1 and Language='".convert::raw2sql($userLang)."'");
    61                 }
    62
    63                 if (!$subsite) {
    64                     $userLang = i18n::get_locale_from_lang(substr($userLang, 0, 2));
    65                     // $userLang = ru
    66                     $subsite = DataObject::get_one('Subsite', "IsPublic=1 and Language='".convert::raw2sql($userLang)."'");
    67                 }
    68
    69                 if (!$subsite) {
    70                     $subsite = DataObject::get_one('Subsite', "IsPublic=1");
    71                 }
    72             }
    73             else {
    74                 $subsite = DataObject::get_one('Subsite', "IsPublic=1 and ID=".$this->GotoSubsite);
    75             }
    76
    77             if ($subsite) {
    78                 Director::redirect($subsite->PrimaryDomain . Director::baseURL());
    79             }
    80         }
    81         parent::init();
    82     }
    83 }
    84

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