Passed
Push — master ( 72198b...d1ce69 )
by Thomas
02:46
created

TabulatorSubsiteDetailFormItemRequest::doSave()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 9.6111
1
<?php
2
3
namespace LeKoala\Admini\Subsites;
4
5
use LeKoala\Tabulator\TabulatorGrid_ItemRequest;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Forms\Form;
8
use SilverStripe\Subsites\Model\Subsite;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Subsites\Model\Subsite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class TabulatorSubsiteDetailFormItemRequest extends TabulatorGrid_ItemRequest
11
{
12
13
    private static $allowed_actions = [
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
     * @return Form
22
     * @see TabulatorGrid_ItemRequest::ItemEditForm()
23
     */
24
    public function ItemEditForm()
25
    {
26
        $form = parent::ItemEditForm();
27
28
        if ($this->record->ID == 0) {
29
            $templates = Subsite::get()->sort('Title');
30
            $templateArray = [];
31
            if ($templates) {
32
                $templateArray = $templates->map('ID', 'Title');
33
            }
34
35
            $templateDropdown = new DropdownField(
36
                'TemplateID',
37
                _t('Subsite.COPYSTRUCTURE', 'Copy structure from:'),
38
                $templateArray
39
            );
40
            $templateDropdown->setEmptyString('(' . _t('Subsite.NOTEMPLATE', 'No template') . ')');
41
            $form->Fields()->addFieldToTab('Root.Main', $templateDropdown);
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on SilverStripe\Control\HTTPResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            $form->/** @scrutinizer ignore-call */ 
42
                   Fields()->addFieldToTab('Root.Main', $templateDropdown);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
        }
43
44
        return $form;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $form returns the type SilverStripe\Control\HTTPResponse which is incompatible with the documented return type SilverStripe\Forms\Form.
Loading history...
45
    }
46
47
    public function doSave($data, $form)
48
    {
49
        $new_record = $this->record->ID == 0;
50
        if ($new_record && isset($data['TemplateID']) && !empty($data['TemplateID'])) {
51
            $template = Subsite::get()->byID(intval($data['TemplateID']));
52
            if ($template) {
53
                $this->record = $template->duplicate();
54
            }
55
        }
56
57
        return parent::doSave($data, $form);
58
    }
59
}
60