Version: current

     1 <?php
     2 /**
     3  *
     4  * Задача добавления подсайта на сайт
     5  * Парамеры:
     6  * Title=Название_подсайта - строка
     7  * Domain=Домен_подсайта - строка
     8  * Language=яз_ЯЗ - языковая константа (en_US, de_DE и т.д.)
     9  * [Theme=Тема] (необязат.) - тема для подсайта
    10  * [populate=1] (необязат.) - создавать на подсайте страницы по умолчанию (HomePage, ContactsPage и т.д.)
    11  *
    12  */
    13 class AddSubsiteTask extends BuildTask {
    14
    15     protected $title;
    16     protected $domain;
    17     protected $language;
    18     protected $theme;
    19     protected $populate;
    20
    21     function run($request) {
    22         if (!Director::is_cli() && !Permission::check("ADMIN") ) {
    23             return print "Permission denied";
    24         }
    25
    26         if ($request->getVar('Title')) {
    27             $this->title = $request->getVar('Title');
    28         } else {
    29             return print "No required parameter 'Title' \n";
    30         }
    31         if ($request->getVar('Domain')) {
    32             $this->domain = $request->getVar('Domain');
    33         } else {
    34             return print "No required parameter 'Domain' \n";
    35
    36         }
    37         if ($request->getVar('Language')) {
    38             $this->language = $request->getVar('Language');
    39         } else {
    40             return print "No required parameter 'Language' \n";
    41         }
    42         if ($request->getVar('Theme')) {
    43             $this->theme = $request->getVar('Theme');
    44         }
    45         if ($request->getVar('populate')) {
    46             $this->populate = $request->getVar('populate');
    47         }
    48
    49         // ищем подсайт по домену, если не нашли - создаем новый
    50         if ($domain = DataObject::get_one('SubsiteDomain', "Domain = '{$this->domain}'")) {
    51             $subsite = $domain->Subsite();
    52             if (!$subsite->ID) { // могут оставаться домены от несуществующих подсайтов
    53                 $subsite = new Subsite();
    54             }
    55         } else {
    56             $subsite = new Subsite();
    57         }
    58         $subsite->Title = $this->title;
    59         $subsite->Language = $this->language;
    60         if ($this->theme) {
    61             $subsite->Theme = $this->theme;
    62         }
    63         $subsite->write();
    64
    65         if (!$domain) {
    66             $domain = new SubsiteDomain();
    67             $domain->Domain = $this->domain;
    68         }
    69         $domain->SubsiteID = $subsite->ID;
    70         $domain->write();
    71
    72         if($this->populate) {
    73             Subsite::changeSubsite($subsite->ID);
    74             i18n::set_locale($subsite->Language);
    75
    76             $dataClasses = ClassInfo::subclassesFor('DataObject');
    77             array_shift($dataClasses);
    78
    79             foreach($dataClasses as $dataClass) {
    80                 // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness
    81                 // Test_ indicates that it's the data class is part of testing system
    82                 if(strpos($dataClass,'Test_') === false && class_exists($dataClass)) {
    83                     singleton($dataClass)->requireDefaultRecords();
    84                 }
    85             }
    86         }
    87     }
    88 }

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