Version: current

      1 <?php
      2
      3 /**
      4  * Admin interface to manage and create {@link Subsite} instances.
      5  *
      6  * @package subsites
      7  */
      8 class SubsiteAdmin extends ModelAdmin {
      9
     10     static $managed_models = array('Subsite');
     11     static $url_segment = 'subsites';
     12     static $menu_title = "Subsites";
     13     static $collection_controller_class = "SubsiteAdmin_CollectionController";
     14
     15     function canView($member=null) {
     16         if(!$member && $member !== FALSE) $member = Member::currentUser();
     17         if(!parent::canView($member)) return false;
     18         return (Director::isDev()) ? true : false;
     19     }
     20
     21 }
     22
     23 class SubsiteAdmin_CollectionController extends ModelAdmin_CollectionController {
     24
     25     function AddForm() {
     26         $form = parent::AddForm();
     27
     28         $templates = DataObject::get('Subsite', '', 'Title');
     29         $templateArray = array('' => "(No template)");
     30         if ($templates) {
     31             $templateArray = $templateArray + $templates->map('ID', 'Title');
     32         }
     33
     34         $form->Fields()->addFieldsToTab('Root.Configuration', array(
     35             new DropdownField('Type', 'Type', array(
     36                 'subsite' => 'New site',
     37                 'template' => 'New template',
     38             )),
     39             new DropdownField('TemplateID', 'Copy structure from:', $templateArray)
     40         ));
     41
     42         return $form;
     43     }
     44
     45     function doCreate($data, $form, $request) {
     46         if (isset($data['TemplateID']) && $data['TemplateID']) {
     47             $template = DataObject::get_by_id('Subsite', $data['TemplateID']);
     48         } else {
     49             $template = null;
     50         }
     51
     52         // Create subsite from existing template
     53         switch ($data['Type']) {
     54             case 'template':
     55                 if ($template)
     56                     $subsite = $template->duplicate();
     57                 else {
     58                     $subsite = new Subsite_Template();
     59                     $subsite->write();
     60                 }
     61                 break;
     62
     63             case 'subsite':
     64             default:
     65                 if ($template) {
     66                     // если копируем из подсайта, то предваращем его в Subsite_Template
     67                     if ($template->class == 'Subsite') {
     68                         $template = new Subsite_Template($template->toMap());
     69                     }
     70                     $subsite = $template->createInstance($data['Title']);
     71                 } else {
     72                     $subsite = new Subsite();
     73                     $subsite->Title = $data['Title'];
     74                     $subsite->write();
     75                 }
     76                 break;
     77         }
     78
     79         $form->dataFieldByName('Domains')->setExtraData(array(
     80             "SubsiteID" => $subsite->ID,
     81         ));
     82         $form->saveInto($subsite);
     83         $subsite->write();
     84
     85         if (Director::is_ajax()) {
     86             $recordController = new ModelAdmin_RecordController($this, $request, $subsite->ID);
     87             return new SS_HTTPResponse(
     88                     $recordController->EditForm()->forAjaxTemplate(),
     89                     200,
     90                     sprintf(
     91                             _t('ModelAdmin.LOADEDFOREDITING', "Loaded '%s' for editing."),
     92                             $subsite->Title
     93                     )
     94             );
     95         } else {
     96             Director::redirect(Controller::join_links($this->Link(), $subsitess->ID, 'edit'));
     97         }
     98     }
     99
    100 }
    101
    102 ?>
    103

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