Completed
Push — master ( cfffae...fa5fd4 )
by Nic
05:44
created

src/Admin/FoxyStripeAdmin.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Dynamic\FoxyStripe\Admin;
4
5
use Dynamic\FoxyStripe\Model\FoxyStripeSetting;
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\View\ArrayData;
17
use SilverStripe\View\Requirements;
18
19
class FoxyStripeAdmin extends LeftAndMain
20
{
21
    /**
22
     * @var string
23
     */
24
    private static $url_segment = 'foxystripe';
25
26
    /**
27
     * @var string
28
     */
29
    private static $url_rule = '/$Action/$ID/$OtherID';
30
31
    /**
32
     * @var int
33
     */
34
    private static $menu_priority = 4;
35
36
    /**
37
     * @var string
38
     */
39
    private static $menu_title = 'FoxyStripe';
40
41
    /**
42
     * @var string
43
     */
44
    private static $menu_icon_class = 'font-icon-cog';
45
46
    /**
47
     * @var string
48
     */
49
    private static $tree_class = FoxyStripeSetting::class;
50
51
    /**
52
     * @var array
53
     */
54
    private static $required_permission_codes = ['EDIT_GLOBAL_PERMISSION'];
55
56
    /**
57
     * Initialises the {@link FoxyStripeSetting} controller.
58
     */
59
    public function init()
60
    {
61
        parent::init();
62
63
        Requirements::javascript('silverstripe/cms: client/dist/js/bundle.js');
64
    }
65
66
    /**
67
     * @param null $id
68
     * @param null $fields
69
     *
70
     * @return $this|Form
71
     */
72
    public function getEditForm($id = null, $fields = null)
73
    {
74
        $config = FoxyStripeSetting::current_foxystripe_setting();
75
        $fields = $config->getCMSFields();
76
77
        // Tell the CMS what URL the preview should show
78
        $home = Director::absoluteBaseURL();
79
        $fields->push(new HiddenField('PreviewURL', 'Preview URL', $home));
80
81
        // Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load
82
        $fields->push($navField = new LiteralField(
83
            'SilverStripeNavigator',
84
            $this->getSilverStripeNavigator()
85
        ));
86
        $navField->setAllowHTML(true);
87
88
        // Retrieve validator, if one has been setup (e.g. via data extensions).
89
        if ($config->hasMethod('getCMSValidator')) {
90
            $validator = $config->getCMSValidator();
91
        } else {
92
            $validator = null;
93
        }
94
95
        $actions = $config->getCMSActions();
96
        $negotiator = $this->getResponseNegotiator();
97
        $form = Form::create(
98
            $this,
99
            'EditForm',
100
            $fields,
101
            $actions,
102
            $validator
103
        )->setHTMLID('Form_EditForm');
104
        $form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) {
105
            $request = $this->getRequest();
106
            if ($request->isAjax() && $negotiator) {
107
                $result = $form->forTemplate();
108
109
                return $negotiator->respond($request, array(
110
                    'CurrentForm' => function () use ($result) {
111
                        return $result;
112
                    },
113
                ));
114
            }
115
            return null;
116
        });
117
        $form->addExtraClass('flexbox-area-grow fill-height cms-content cms-edit-form');
118
        $form->setAttribute('data-pjax-fragment', 'CurrentForm');
119
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
127
        // Use <button> to allow full jQuery UI styling
128
        $actions = $actions->dataFields();
129
        if ($actions) {
130
            /** @var FormAction $action */
131
            foreach ($actions as $action) {
132
                $action->setUseButtonTag(true);
133
            }
134
        }
135
136
        $this->extend('updateEditForm', $form);
137
138
        return $form;
139
    }
140
141
    /**
142
     * Save the current sites {@link FoxyStripeSetting} into the database.
143
     *
144
     * @param $data
145
     * @param $form
146
     *
147
     * @return \SilverStripe\Control\HTTPResponse
148
     * @throws \SilverStripe\Control\HTTPResponse_Exception
149
     */
150
    public function save_foxystripe_setting($data, $form)
151
    {
152
        $config = FoxyStripeSetting::current_foxystripe_setting();
153
        $form->saveInto($config);
154
        try {
155
            $config->write();
156
        } catch (ValidationException $ex) {
157
            $form->sessionMessage($ex->getResult()->message(), 'bad');
0 ignored issues
show
The method message() does not exist on SilverStripe\ORM\ValidationResult. ( Ignorable by Annotation )

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

157
            $form->sessionMessage($ex->getResult()->/** @scrutinizer ignore-call */ message(), 'bad');

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...
158
159
            return $this->getResponseNegotiator()->respond($this->request);
160
        }
161
        $this->response->addHeader('X-Status', rawurlencode(_t('SilverStripe\\Admin\\LeftAndMain.SAVEDUP', 'Saved.')));
162
163
        return $form->forTemplate();
164
    }
165
166
    /**
167
     * @param bool $unlinked
168
     *
169
     * @return ArrayList
170
     */
171
    public function Breadcrumbs($unlinked = false)
172
    {
173
        return new ArrayList(array(
174
            new ArrayData(array(
175
                'Title' => static::menu_title(),
176
                'Link' => $this->Link(),
177
            )),
178
        ));
179
    }
180
}
181