Passed
Pull Request — master (#372)
by Robbie
03:12
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
12
/**
13
 * Controller for "ElementalArea" - handles loading and saving of in-line edit forms in an elemental area in admin
14
 */
15
class ElementalAreaController extends LeftAndMain
16
{
17
    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...
18
19
    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...
20
21
    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...
22
        'elementForm',
23
        'schema',
24
    );
25
26
    public function getClientConfig()
27
    {
28
        $clientConfig = parent::getClientConfig();
29
        $clientConfig['form']['elementForm'] = [
30
            'schemaUrl' => $this->Link('schema/elementForm'),
31
        ];
32
        return $clientConfig;
33
    }
34
35
    /**
36
     * @param HTTPRequest|null $request
37
     * @return Form
38
     * @throws HTTPResponse_Exception
39
     */
40
    public function elementForm(HTTPRequest $request = null)
41
    {
42
        // Get ID either from posted back value, or url parameter
43
        if (!$request) {
44
            $this->jsonError(400);
45
            return null;
46
        }
47
        $id = $request->param('ID');
48
        if (!$id) {
49
            $this->jsonError(400);
50
            return null;
51
        }
52
        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

52
        return $this->getElementForm(/** @scrutinizer ignore-type */ $id) ?: $this->jsonError(404);
Loading history...
53
    }
54
55
    /**
56
     * @param int $elementID
57
     * @return Form|null Returns null if no element exists for the given ID
58
     */
59
    public function getElementForm($elementID)
60
    {
61
        $scaffolder = Injector::inst()->get(DefaultFormFactory::class);
62
        $element = BaseElement::get()->byID($elementID);
63
64
        if (!$element) {
0 ignored issues
show
introduced by
$element is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
65
            return null;
66
        }
67
68
        /** @var Form $form */
69
        $form = $scaffolder->getForm(
70
            $this,
71
            'ElementForm_'.$elementID,
72
            ['Record' => $element]
73
        );
74
75
        $form->addExtraClass('form--no-dividers');
76
77
        return $form;
78
    }
79
}
80