FoxyAdmin::save_foxy_setting()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 13
rs 9.9666
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;
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\ValidationException;
15
use SilverStripe\ORM\ValidationResult;
16
use SilverStripe\Versioned\RecursivePublishable;
17
use SilverStripe\View\ArrayData;
18
use SilverStripe\View\Requirements;
19
20
/**
21
 * Class FoxyAdmin
22
 */
23
class FoxyAdmin extends LeftAndMain
24
{
25
    /**
26
     * @var string
27
     */
28
    private static $url_segment = 'foxy';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
29
30
    /**
31
     * @var string
32
     */
33
    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...
34
35
    /**
36
     * @var int
37
     */
38
    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...
39
40
    /**
41
     * @var string
42
     */
43
    private static $menu_title = 'Foxy';
0 ignored issues
show
introduced by
The private property $menu_title is not used, and could be removed.
Loading history...
44
45
    /**
46
     * @var string
47
     */
48
    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...
49
50
    /**
51
     * @var string
52
     */
53
    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...
54
55
    /**
56
     * @var array
57
     */
58
    private static $required_permission_codes = ['EDIT_FOXY_SETTING'];
0 ignored issues
show
introduced by
The private property $required_permission_codes is not used, and could be removed.
Loading history...
59
60
    /**
61
     * Initialises the {@link TemplateConfigSetting} controller.
62
     */
63
    public function init()
64
    {
65
        parent::init();
66
        if (class_exists(SiteTree::class)) {
67
            Requirements::javascript('silverstripe/cms: client/dist/js/bundle.js');
68
        }
69
    }
70
71
    /**
72
     * @param int|null $id
73
     * @param \SilverStripe\Forms\FieldList|null $fields
74
     *
75
     * @return $this|Form
76
     */
77
    public function getEditForm($id = null, $fields = null)
78
    {
79
        $config = Setting::current_foxy_setting();
80
        $fields = $config->getCMSFields();
81
        // Tell the CMS what URL the preview should show
82
        $home = Director::absoluteBaseURL();
83
        $fields->push(new HiddenField('PreviewURL', 'Preview URL', $home));
84
        // Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load
85
        $fields->push($navField = new LiteralField(
86
            'SilverStripeNavigator',
87
            $this->getSilverStripeNavigator()
88
        ));
89
        $navField->setAllowHTML(true);
90
        // Retrieve validator, if one has been setup (e.g. via data extensions).
91
        if ($config->hasMethod('getCMSValidator')) {
92
            $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

92
            /** @scrutinizer ignore-call */ 
93
            $validator = $config->getCMSValidator();
Loading history...
93
        } else {
94
            $validator = null;
95
        }
96
        $actions = $config->getCMSActions();
97
        $negotiator = $this->getResponseNegotiator();
98
        $form = Form::create(
99
            $this,
100
            'EditForm',
101
            $fields,
102
            $actions,
103
            $validator
104
        )->setHTMLID('Form_EditForm');
105
        $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

105
        $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...
106
            $request = $this->getRequest();
107
            if ($request->isAjax() && $negotiator) {
108
                $result = $form->forTemplate();
109
                return $negotiator->respond($request, array(
110
                    'CurrentForm' => function () use ($result) {
111
                        return $result;
112
                    },
113
                ));
114
            }
115
        });
116
        $form->addExtraClass('flexbox-area-grow fill-height cms-content cms-edit-form');
117
        $form->setAttribute('data-pjax-fragment', 'CurrentForm');
118
        if ($form->Fields()->hasTabSet()) {
119
            $form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
120
        }
121
        $form->setHTMLID('Form_EditForm');
122
        $form->loadDataFrom($config);
123
        $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
124
        // Use <button> to allow full jQuery UI styling
125
        $actions = $actions->dataFields();
126
        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...
127
            /** @var FormAction $action */
128
            foreach ($actions as $action) {
129
                $action->setUseButtonTag(true);
130
            }
131
        }
132
        $this->extend('updateEditForm', $form);
133
        return $form;
134
    }
135
136
    /**
137
     * Save the current sites {@link GlobalSiteSetting} into the database.
138
     *
139
     * @param array $data
140
     * @param Form  $form
141
     *
142
     * @return string
143
     * @throws ValidationException
144
     */
145
    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

145
    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...
146
    {
147
        $settings = Setting::current_foxy_setting();
148
        $form->saveInto($settings);
149
        $settings->write();
150
        if ($settings->hasExtension(RecursivePublishable::class)) {
151
            $settings->publishRecursive();
0 ignored issues
show
Bug introduced by
The method publishRecursive() 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

151
            $settings->/** @scrutinizer ignore-call */ 
152
                       publishRecursive();
Loading history...
152
        }
153
        $this->response->addHeader(
154
            'X-Status',
155
            rawurlencode(_t(LeftAndMain::class . '.SAVEDUP', 'Saved.'))
156
        );
157
        return $form->forTemplate();
158
    }
159
160
    /**
161
     * @param bool $unlinked
162
     *
163
     * @return ArrayList
164
     */
165
    public function Breadcrumbs($unlinked = false)
166
    {
167
        return new ArrayList(array(
168
            new ArrayData(array(
169
                'Title' => static::menu_title(),
170
                'Link' => $this->Link(),
171
            )),
172
        ));
173
    }
174
}
175