Completed
Pull Request — master (#305)
by Robbie
02:01
created

ItemEditForm()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 0
1
<?php
2
3
namespace SilverStripe\Subsites\Forms;
4
5
use SilverStripe\Forms\DropdownField;
6
use SilverStripe\Forms\Form;
7
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
8
use SilverStripe\Subsites\Model\Subsite;
9
10
class GridFieldSubsiteDetailFormItemRequest extends GridFieldDetailForm_ItemRequest
11
{
12
13
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
        'ItemEditForm',
15
    ];
16
17
    /**
18
     * Builds an item edit form.  The arguments to getCMSFields() are the popupController and
19
     * popupFormName, however this is an experimental API and may change.
20
     *
21
     * @todo In the future, we will probably need to come up with a tigher object representing a partially
22
     * complete controller with gaps for extra functionality.  This, for example, would be a better way
23
     * of letting Security/login put its log-in form inside a UI specified elsewhere.
24
     *
25
     * @return Form
26
     * @see GridFieldDetailForm_ItemRequest::ItemEditForm()
27
     */
28
    public function ItemEditForm()
29
    {
30
        $form = parent::ItemEditForm();
31
32
        if ($this->record->ID == 0) {
33
            $templates = Subsite::get()->sort('Title');
34
            $templateArray = [];
35
            if ($templates) {
36
                $templateArray = $templates->map('ID', 'Title');
37
            }
38
39
            $templateDropdown = new DropdownField(
40
                'TemplateID',
41
                _t('SilverStripe\\Subsites\\Model\\Subsite.COPYSTRUCTURE', 'Copy structure from:'),
42
                $templateArray
43
            );
44
            $templateDropdown->setEmptyString('(' . _t('SilverStripe\\Subsites\\Model\\Subsite.NOTEMPLATE', 'No template') . ')');
45
            $form->Fields()->addFieldToTab('Root.Configuration', $templateDropdown);
46
        }
47
48
        return $form;
49
    }
50
51
    public function doSave($data, $form)
52
    {
53
        $new_record = $this->record->ID == 0;
54
        if ($new_record && isset($data['TemplateID']) && !empty($data['TemplateID'])) {
55
            $template = Subsite::get()->byID(intval($data['TemplateID']));
56
            if ($template) {
57
                $this->record = $template->duplicate();
58
            }
59
        }
60
61
        return parent::doSave($data, $form);
62
    }
63
}
64