Completed
Push — master ( 8ffc0f...db4e23 )
by Robbie
12s
created

ElementalAreaController::elementForm()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 1
dl 0
loc 13
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\Core\Injector\Injector;
8
use SilverStripe\Forms\DefaultFormFactory;
9
use SilverStripe\Forms\Form;
10
11
/**
12
 * Controller for "ElementalArea" - handles loading and saving of in-line edit forms in an elemental area in admin
13
 */
14
class ElementalAreaController extends LeftAndMain
15
{
16
    private static $url_segment = 'element-area';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
17
18
    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...
19
20
    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...
21
        'elementForm',
22
        'schema',
23
    );
24
25
    /**
26
     * @param HTTPRequest|null $request
27
     * @return Form
28
     * @throws \SilverStripe\Control\HTTPResponse_Exception
29
     */
30
    public function elementForm(HTTPRequest $request = null)
31
    {
32
        // Get ID either from posted back value, or url parameter
33
        if (!$request) {
34
            $this->jsonError(400);
35
            return null;
36
        }
37
        $id = $request->param('ID');
38
        if (!$id) {
39
            $this->jsonError(400);
40
            return null;
41
        }
42
        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

42
        return $this->getElementForm(/** @scrutinizer ignore-type */ $id) ?: $this->jsonError(404);
Loading history...
43
    }
44
45
    /**
46
     * @param int $elementID
47
     * @return Form|null Returns null if no element exists for the given ID
48
     */
49
    public function getElementForm($elementID)
50
    {
51
        $scaffolder = Injector::inst()->get(DefaultFormFactory::class);
52
        $element = BaseElement::get()->byID($elementID);
53
54
        if (!$element) {
0 ignored issues
show
introduced by
$element is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
55
            return null;
56
        }
57
58
        return $scaffolder->getForm(
59
            $this,
60
            'ElementForm_'.$elementID,
61
            ['Record' => $element]
62
        );
63
    }
64
}
65