Passed
Pull Request — master (#372)
by Robbie
03:13
created

ElementalAreaController::getClientConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace DNADesign\Elemental\Controllers;
3
4
use DNADesign\Elemental\Models\BaseElement;
5
use SilverStripe\Admin\LeftAndMain;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Control\HTTPResponse_Exception;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Forms\DefaultFormFactory;
10
use SilverStripe\Forms\Form;
11
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
12
13
/**
14
 * Controller for "ElementalArea" - handles loading and saving of in-line edit forms in an elemental area in admin
15
 */
16
class ElementalAreaController extends LeftAndMain
17
{
18
    private static $url_segment = 'elemental-area';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
19
20
    private static $ignore_menuitem = true;
0 ignored issues
show
introduced by
The private property $ignore_menuitem is not used, and could be removed.
Loading history...
21
22
    private static $allowed_actions = array(
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
23
        'elementForm',
24
        'schema',
25
    );
26
27
    public function getClientConfig()
28
    {
29
        $clientConfig = parent::getClientConfig();
30
        $clientConfig['form']['elementForm'] = [
31
            'schemaUrl' => $this->Link('schema/elementForm'),
32
        ];
33
        return $clientConfig;
34
    }
35
36
    /**
37
     * @param HTTPRequest|null $request
38
     * @return Form
39
     * @throws HTTPResponse_Exception
40
     */
41
    public function elementForm(HTTPRequest $request = null)
42
    {
43
        // Get ID either from posted back value, or url parameter
44
        if (!$request) {
45
            $this->jsonError(400);
46
            return null;
47
        }
48
        $id = $request->param('ID');
49
        if (!$id) {
50
            $this->jsonError(400);
51
            return null;
52
        }
53
        return $this->getElementForm($id) ?: $this->jsonError(404);
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type integer expected by parameter $elementID of DNADesign\Elemental\Cont...oller::getElementForm(). ( Ignorable by Annotation )

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

53
        return $this->getElementForm(/** @scrutinizer ignore-type */ $id) ?: $this->jsonError(404);
Loading history...
54
    }
55
56
    /**
57
     * @param int $elementID
58
     * @return Form|null Returns null if no element exists for the given ID
59
     */
60
    public function getElementForm($elementID)
61
    {
62
        $scaffolder = Injector::inst()->get(DefaultFormFactory::class);
63
        $element = BaseElement::get()->byID($elementID);
64
65
        if (!$element) {
0 ignored issues
show
introduced by
$element is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
66
            return null;
67
        }
68
69
        /** @var Form $form */
70
        $form = $scaffolder->getForm(
71
            $this,
72
            'ElementForm_'.$elementID,
73
            ['Record' => $element]
74
        );
75
76
        $form->addExtraClass('form--no-dividers');
77
78
        /** @var HTMLEditorField $contentField */
79
        $contentField = $form->Fields()->fieldByName('Root.Main.HTML');
80
        if ($contentField) {
0 ignored issues
show
introduced by
$contentField is of type SilverStripe\Forms\HTMLEditor\HTMLEditorField, thus it always evaluated to true. If $contentField can have other possible types, add them to src/Controllers/ElementalAreaController.php:78
Loading history...
81
            $contentField->setRows(5);
82
        }
83
84
        return $form;
85
    }
86
}
87