diff --git a/code/SubsiteAdmin.php b/code/SubsiteAdmin.php index 9174977..69d8bf9 100644 --- a/code/SubsiteAdmin.php +++ b/code/SubsiteAdmin.php @@ -19,8 +19,8 @@ class SubsiteAdmin extends ModelAdmin { if($this->modelClass=='Subsite') { $grid=$form->Fields()->dataFieldByName('Subsite'); if($grid) { - $grid->getConfig()->addComponent(new GridFieldAddFromTemplateButton('toolbar-header-right')); - $grid->getConfig()->addComponent(new GridFieldAddFromTemplate()); + $grid->getConfig()->removeComponentsByType('GridFieldDetailForm'); + $grid->getConfig()->addComponent(new GridFieldSubsiteDetailForm()); } } diff --git a/code/extensions/SiteTreeSubsites.php b/code/extensions/SiteTreeSubsites.php index edcf974..65f3299 100644 --- a/code/extensions/SiteTreeSubsites.php +++ b/code/extensions/SiteTreeSubsites.php @@ -90,7 +90,7 @@ class SiteTreeSubsites extends DataExtension { } function updateCMSFields(FieldList $fields) { - if($this->owner->MasterPageID) $fields->insertFirst(new HeaderField('This page\'s content is copied from a master page: ' . $this->owner->MasterPage()->Title, 2)); + if($this->owner->MasterPageID) $fields->addFieldToTab('Root.Main', new HeaderField('This page\'s content is copied from a master page: ' . $this->owner->MasterPage()->Title, 2), 'Title'); // replace readonly link prefix $subsite = $this->owner->Subsite(); diff --git a/code/forms/GridFieldAddFromTemplateButton.php b/code/forms/GridFieldAddFromTemplateButton.php deleted file mode 100644 index 7b8d747..0000000 --- a/code/forms/GridFieldAddFromTemplateButton.php +++ /dev/null @@ -1,148 +0,0 @@ -targetFragment = $targetFragment; - } - - public function getHTMLFragments($gridField) { - $data = new ArrayData(array( - 'NewFromTemplateLink' => $gridField->Link('newFromTemplate'), - )); - return array( - $this->targetFragment => $data->renderWith('GridFieldAddFromTemplateButton'), - ); - } - -} - -class GridFieldAddFromTemplate extends GridFieldDetailForm { - public function getURLHandlers($gridField) { - return array( - 'newFromTemplate'=>'newFromTemplate', - ); - } - - public function newFromTemplate($gridField, $request) { - $controller = $gridField->getForm()->Controller(); - - if(is_numeric($request->param('ID'))) { - $record = $gridField->getList()->byId($request->param("ID")); - } else { - $record = Object::create($gridField->getModelClass()); - } - - - $handler = Object::create('GridFieldAddFromTemplate_ItemRequest', $gridField, $this, $record, $controller, $this->name); - $handler->setTemplate($this->template); - - return $handler->handleRequest($request, DataModel::inst()); - } -} - -class GridFieldAddFromTemplate_ItemRequest extends GridFieldDetailForm_ItemRequest { - public function Link($action = null) { - return $this->gridField->Link('newFromTemplate'); - } - - function edit($request) { - $controller = $this->getToplevelController(); - $form = $this->NewFromTemplateForm($this->gridField, $request); - - $return = $this->customise(array( - 'Backlink' => $controller->Link(), - 'ItemEditForm' => $form, - ))->renderWith($this->template); - - if($controller->isAjax()) { - return $return; - } else { - // If not requested by ajax, we need to render it within the controller context+template - return $controller->customise(array( - // TODO CMS coupling - 'Content' => $return, - )); - } - } - - public function NewFromTemplateForm() { - $templates=DataObject::get('Subsite_Template'); - - $fields=new FieldList( - new DropdownField('TemplateID', _t('GridFieldAddFromTemplate.TEMPLATE', '_Template'), $templates->map('ID', 'Name')) - ); - - $actions=new FieldList( - FormAction::create('doCreateFromTemplate', _t('GridFieldDetailsForm.Create', 'Create'))->setUseButtonTag(true)->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'add') - ); - - // Add a Cancel link which is a button-like link and link back to one level up. - $curmbs = $this->Breadcrumbs(); - if($curmbs && $curmbs->count()>=2){ - $one_level_up = $curmbs->offsetGet($curmbs->count()-2); - $text = " - Link."\"> - Cancel - "; - $actions->push(new LiteralField('cancelbutton', $text)); - } - - $validator=new RequiredFields('TemplateID'); - - $form=new Form($this, 'NewFromTemplateForm', $fields, $actions, $validator); - - // TODO Coupling with CMS - $toplevelController = $this->getToplevelController(); - if($toplevelController && $toplevelController instanceof LeftAndMain) { - // Always show with base template (full width, no other panels), - // regardless of overloaded CMS controller templates. - // TODO Allow customization, e.g. to display an edit form alongside a search form from the CMS controller - $form->setTemplate('LeftAndMain_EditForm'); - $form->addExtraClass('cms-content cms-edit-form center ss-tabset'); - if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet'); - // TODO Link back to controller action (and edited root record) rather than index, - // which requires more URL knowledge than the current link to this field gives us. - // The current root record is held in session only, - // e.g. page/edit/show/6/ vs. page/edit/EditForm/field/MyGridField/.... - $form->Backlink = $toplevelController->Link(); - } - - return $form; - } - - public function doCreateFromTemplate($data, Form $form) { - $template=DataObject::get_by_id('Subsite_Template', intval($data['TemplateID'])); - - if($template) { - $subsite=$template->createInstance($data['Title']); - $subsite->write(); - - $this->record($subsite); - return $this->redirect(parent::Link()); - }else { - $form->sessionMessage(_t('GridFieldAddFromTemplate.TEMPLATE_NOT_FOUND', '_The selected template could not be found'), 'bad'); - return $this->redirectBack(); - } - } - - /** - * CMS-specific functionality: Passes through navigation breadcrumbs - * to the template, and includes the currently edited record (if any). - * see {@link LeftAndMain->Breadcrumbs()} for details. - * - * @param boolean $unlinked - * @return ArrayData - */ - function Breadcrumbs($unlinked = false) { - if(!$this->popupController->hasMethod('Breadcrumbs')) return; - - $items = $this->popupController->Breadcrumbs($unlinked); - $items->push(new ArrayData(array( - 'Title' => sprintf(_t('GridFieldAddFromTemplate.NewFromTemplate', 'New %s from template'), $this->record->singular_name()), - 'Link' => false - ))); - - return $items; - } -} \ No newline at end of file diff --git a/code/forms/GridFieldSubsiteDetailForm.php b/code/forms/GridFieldSubsiteDetailForm.php new file mode 100644 index 0000000..df61917 --- /dev/null +++ b/code/forms/GridFieldSubsiteDetailForm.php @@ -0,0 +1,65 @@ +record->ID == 0) { + $templates = Subsite_Template::get()->sort('Title'); + $templateArray = array(); + if($templates) { + $templateArray = $templates->map('ID', 'Title'); + } + + $form->Fields()->addFieldToTab('Root.Configuration', new DropdownField('TemplateID', 'Copy structure from:', $templateArray, null, null, "(No template)")); + } + + return $form; + } + + function doSave($data, $form) { + $new_record = $this->record->ID == 0; + + if($new_record && isset($data['TemplateID']) && !empty($data['TemplateID'])) { + $template = Subsite_Template::get()->byID(intval($data['TemplateID'])); + if($template) { + $this->record=$template->createInstance($data['Title']); + } + } + + try { + $form->saveInto($this->record); + $this->record->write(); + $this->gridField->getList()->add($this->record); + } catch(ValidationException $e) { + $form->sessionMessage($e->getResult()->message(), 'bad'); + return Controller::curr()->redirectBack(); + } + + // TODO Save this item into the given relationship + + $message = sprintf( + _t('GridFieldDetailForm.Saved', 'Saved %s %s'), + $this->record->singular_name(), + '"' . htmlspecialchars($this->record->Title, ENT_QUOTES) . '"' + ); + + $form->sessionMessage($message, 'good'); + + return Controller::curr()->redirect($this->Link()); + } +} \ No newline at end of file diff --git a/code/model/Subsite.php b/code/model/Subsite.php index abe41a0..ecdce77 100644 --- a/code/model/Subsite.php +++ b/code/model/Subsite.php @@ -660,12 +660,9 @@ class Subsite_Template extends Subsite { if($children) { foreach($children as $child) { - $childClone = $child->duplicateToSubsite($intranet); - //Change to destination subsite self::changeSubsite($intranet->ID); - $newTemplate = parent::duplicate($doWrite); - $childClone = $child->duplicateToSubsite($newTemplate, false); + $childClone = $child->duplicateToSubsite($intranet); $childClone->ParentID = $destParentID; $childClone->writeToStage('Stage');