Passed
Pull Request — master (#12)
by Matthew
01:46
created

FoxyAdmin   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Breadcrumbs() 0 6 1
A init() 0 5 2
A save_foxy_setting() 0 14 2
B getEditForm() 0 57 7
1
<?php
2
3
namespace Dynamic\Foxy\Admin;
4
5
use Dynamic\Foxy\Model\Setting;
6
use SilverStripe\Admin\LeftAndMain;
7
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree 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...
8
use SilverStripe\Control\Director;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\FormAction;
11
use SilverStripe\Forms\HiddenField;
12
use SilverStripe\Forms\LiteralField;
13
use SilverStripe\ORM\ArrayList;
14
use SilverStripe\ORM\DataObject;
15
use SilverStripe\ORM\ValidationException;
16
use SilverStripe\ORM\ValidationResult;
17
use SilverStripe\SiteConfig\SiteConfig;
0 ignored issues
show
Bug introduced by
The type SilverStripe\SiteConfig\SiteConfig 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...
18
use SilverStripe\Versioned\RecursivePublishable;
19
use SilverStripe\View\ArrayData;
20
use SilverStripe\View\Requirements;
21
22
/**
23
 * Class FoxyAdmin
24
 */
25
class FoxyAdmin extends LeftAndMain
26
{
27
    /**
28
     * @var string
29
     */
30
    private static $url_segment = 'foxystripe';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
31
32
    /**
33
     * @var string
34
     */
35
    private static $url_rule = '/$Action/$ID/$OtherID';
0 ignored issues
show
introduced by
The private property $url_rule is not used, and could be removed.
Loading history...
36
37
    /**
38
     * @var int
39
     */
40
    private static $menu_priority = 0;
0 ignored issues
show
introduced by
The private property $menu_priority is not used, and could be removed.
Loading history...
41
42
    /**
43
     * @var string
44
     */
45
    private static $menu_title = 'FoxyStripe';
0 ignored issues
show
introduced by
The private property $menu_title is not used, and could be removed.
Loading history...
46
47
    /**
48
     * @var string
49
     */
50
    private static $menu_icon_class = 'font-icon-cog';
0 ignored issues
show
introduced by
The private property $menu_icon_class is not used, and could be removed.
Loading history...
51
52
    /**
53
     * @var string
54
     */
55
    private static $tree_class = Setting::class;
0 ignored issues
show
introduced by
The private property $tree_class is not used, and could be removed.
Loading history...
56
57
    /**
58
     * @var array
59
     */
60
    private static $required_permission_codes = ['EDIT_GLOBAL_PERMISSION'];
0 ignored issues
show
introduced by
The private property $required_permission_codes is not used, and could be removed.
Loading history...
61
62
    /**
63
     * Initialises the {@link TemplateConfigSetting} controller.
64
     */
65
    public function init()
66
    {
67
        parent::init();
68
        if (class_exists(SiteTree::class)) {
69
            Requirements::javascript('silverstripe/cms: client/dist/js/bundle.js');
70
        }
71
    }
72
73
    /**
74
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
75
     * @param null $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
76
     *
77
     * @return $this|Form
78
     */
79
    public function getEditForm($id = null, $fields = null)
80
    {
81
        $config = Setting::current_foxy_setting();
82
        $fields = $config->getCMSFields();
83
        // Tell the CMS what URL the preview should show
84
        $home = Director::absoluteBaseURL();
85
        $fields->push(new HiddenField('PreviewURL', 'Preview URL', $home));
86
        // Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load
87
        $fields->push($navField = new LiteralField(
88
            'SilverStripeNavigator',
89
            $this->getSilverStripeNavigator()
90
        ));
91
        $navField->setAllowHTML(true);
92
        // Retrieve validator, if one has been setup (e.g. via data extensions).
93
        if ($config->hasMethod('getCMSValidator')) {
94
            $validator = $config->getCMSValidator();
0 ignored issues
show
Bug introduced by
The method getCMSValidator() does not exist on Dynamic\Foxy\Model\Setting. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

94
            /** @scrutinizer ignore-call */ 
95
            $validator = $config->getCMSValidator();
Loading history...
95
        } else {
96
            $validator = null;
97
        }
98
        $actions = $config->getCMSActions();
99
        $negotiator = $this->getResponseNegotiator();
100
        $form = Form::create(
101
            $this,
102
            'EditForm',
103
            $fields,
104
            $actions,
105
            $validator
106
        )->setHTMLID('Form_EditForm');
107
        $form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) {
0 ignored issues
show
Unused Code introduced by
The parameter $errors is not used and could be removed. ( Ignorable by Annotation )

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

107
        $form->setValidationResponseCallback(function (/** @scrutinizer ignore-unused */ ValidationResult $errors) use ($negotiator, $form) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
            $request = $this->getRequest();
109
            if ($request->isAjax() && $negotiator) {
110
                $result = $form->forTemplate();
111
                return $negotiator->respond($request, array(
112
                    'CurrentForm' => function () use ($result) {
113
                        return $result;
114
                    },
115
                ));
116
            }
117
        });
118
        $form->addExtraClass('flexbox-area-grow fill-height cms-content cms-edit-form');
119
        $form->setAttribute('data-pjax-fragment', 'CurrentForm');
120
        if ($form->Fields()->hasTabSet()) {
121
            $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
122
        }
123
        $form->setHTMLID('Form_EditForm');
124
        $form->loadDataFrom($config);
125
        $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
126
        // Use <button> to allow full jQuery UI styling
127
        $actions = $actions->dataFields();
128
        if ($actions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $actions of type SilverStripe\Forms\FormField[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
129
            /** @var FormAction $action */
130
            foreach ($actions as $action) {
131
                $action->setUseButtonTag(true);
132
            }
133
        }
134
        $this->extend('updateEditForm', $form);
135
        return $form;
136
    }
137
138
    /**
139
     * Save the current sites {@link GlobalSiteSetting} into the database.
140
     *
141
     * @param array $data
142
     * @param Form  $form
143
     *
144
     * @return string
145
     * @throws ValidationException
146
     */
147
    public function save_foxy_setting($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

147
    public function save_foxy_setting(/** @scrutinizer ignore-unused */ $data, $form)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        $data = $form->getData();
150
        $siteConfig = DataObject::get_by_id(Setting::class, $data['ID']);
151
        $form->saveInto($siteConfig);
152
        $siteConfig->write();
153
        if ($siteConfig->hasExtension(RecursivePublishable::class)) {
154
            $siteConfig->publishRecursive();
155
        }
156
        $this->response->addHeader(
157
            'X-Status',
158
            rawurlencode(_t('SilverStripe\\Admin\\LeftAndMain.SAVEDUP', 'Saved.'))
159
        );
160
        return $form->forTemplate();
161
    }
162
163
    /**
164
     * @param bool $unlinked
165
     *
166
     * @return ArrayList
167
     */
168
    public function Breadcrumbs($unlinked = false)
169
    {
170
        return new ArrayList(array(
171
            new ArrayData(array(
172
                'Title' => static::menu_title(),
173
                'Link' => $this->Link(),
174
            )),
175
        ));
176
    }
177
}
178