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'; |
|
|
|
|
18
|
|
|
|
19
|
|
|
private static $ignore_menuitem = true; |
|
|
|
|
20
|
|
|
|
21
|
|
|
private static $allowed_actions = array( |
|
|
|
|
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); |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|