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'; |
|
|
|
|
19
|
|
|
|
20
|
|
|
private static $ignore_menuitem = true; |
|
|
|
|
21
|
|
|
|
22
|
|
|
private static $allowed_actions = array( |
|
|
|
|
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); |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
81
|
|
|
$contentField->setRows(5); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $form; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|