1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Extensions; |
4
|
|
|
|
5
|
|
|
use Embed\Exceptions\InvalidUrlException; |
6
|
|
|
use SilverStripe\Admin\LeftAndMain; |
7
|
|
|
use SilverStripe\Admin\ModalController; |
8
|
|
|
use SilverStripe\AssetAdmin\Forms\RemoteFileFormFactory; |
9
|
|
|
use SilverStripe\Control\HTTPRequest; |
10
|
|
|
use SilverStripe\Control\HTTPResponse; |
11
|
|
|
use SilverStripe\Core\Convert; |
12
|
|
|
use SilverStripe\Core\Extension; |
13
|
|
|
use SilverStripe\Core\Injector\Injector; |
14
|
|
|
use SilverStripe\Forms\FieldList; |
15
|
|
|
use SilverStripe\Forms\Form; |
16
|
|
|
use SilverStripe\Forms\Schema\FormSchema; |
17
|
|
|
use SilverStripe\ORM\ValidationResult; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Decorates ModalController with an insert-oembed modal |
21
|
|
|
* @see ModalController |
22
|
|
|
*/ |
23
|
|
|
class RemoteFileModalExtension extends Extension |
24
|
|
|
{ |
25
|
|
|
private static $allowed_actions = array( |
|
|
|
|
26
|
|
|
'remoteCreateForm', |
27
|
|
|
'remoteEditForm', |
28
|
|
|
'remoteEditFormSchema', |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return ModalController |
33
|
|
|
*/ |
34
|
|
|
public function getOwner() |
35
|
|
|
{ |
36
|
|
|
/** @var ModalController $owner */ |
37
|
|
|
$owner = $this->owner; |
38
|
|
|
return $owner; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return HTTPRequest |
43
|
|
|
*/ |
44
|
|
|
protected function getRequest() |
45
|
|
|
{ |
46
|
|
|
return $this->getOwner()->getController()->getRequest(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return FormSchema |
51
|
|
|
*/ |
52
|
|
|
protected function getFormSchema() |
53
|
|
|
{ |
54
|
|
|
return FormSchema::singleton(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Form for creating a new OEmbed object in the WYSIWYG, used by the InsertEmbedModal component |
59
|
|
|
* |
60
|
|
|
* @return Form |
61
|
|
|
*/ |
62
|
|
|
public function remoteCreateForm() |
63
|
|
|
{ |
64
|
|
|
return Injector::inst()->get(RemoteFileFormFactory::class) |
65
|
|
|
->getForm( |
66
|
|
|
$this->getOwner(), |
67
|
|
|
'remoteCreateForm', |
68
|
|
|
['type' => 'create'] |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Form for editing a OEmbed object in the WYSIWYG, used by the InsertEmbedModal component |
74
|
|
|
* |
75
|
|
|
* @return Form |
76
|
|
|
*/ |
77
|
|
|
public function remoteEditForm() |
78
|
|
|
{ |
79
|
|
|
$url = $this->getRequest()->requestVar('embedurl'); |
80
|
|
|
$form = null; |
|
|
|
|
81
|
|
|
$form = Injector::inst()->get(RemoteFileFormFactory::class) |
82
|
|
|
->getForm( |
83
|
|
|
$this->getOwner(), |
84
|
|
|
'remoteEditForm', |
85
|
|
|
['type' => 'edit', 'url' => $url] |
86
|
|
|
); |
87
|
|
|
return $form; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Capture the schema handling process, as there is validation done to the URL provided before form is generated |
92
|
|
|
* |
93
|
|
|
* @param HTTPRequest $request |
94
|
|
|
* @return HTTPResponse |
95
|
|
|
*/ |
96
|
|
|
public function remoteEditFormSchema(HTTPRequest $request) |
97
|
|
|
{ |
98
|
|
|
$schemaID = $request->getURL(); |
99
|
|
|
try { |
100
|
|
|
$form = $this->remoteEditForm(); |
101
|
|
|
return $this->getSchemaResponse($schemaID, $form); |
102
|
|
|
} catch (InvalidUrlException $exception) { |
103
|
|
|
$errors = ValidationResult::create() |
104
|
|
|
->addError($exception->getMessage()); |
105
|
|
|
// @todo - Don't create dummy form (pass $form = null) |
106
|
|
|
$form = Form::create(null, 'Form', FieldList::create(), FieldList::create()); |
107
|
|
|
$code = $exception->getCode(); |
108
|
|
|
|
109
|
|
|
if ($code < 300) { |
110
|
|
|
$code = 500; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this |
114
|
|
|
->getSchemaResponse($schemaID, $form, $errors) |
115
|
|
|
->setStatusCode($code); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Generate schema for the given form based on the X-Formschema-Request header value |
121
|
|
|
* |
122
|
|
|
* @todo de-dupe this logic with LeftAndMain::getSchemaResponse() |
123
|
|
|
* |
124
|
|
|
* @param string $schemaID ID for this schema. Required. |
125
|
|
|
* @param Form $form Required for 'state' or 'schema' response |
126
|
|
|
* @param ValidationResult $errors Required for 'error' response |
127
|
|
|
* @param array $extraData Any extra data to be merged with the schema response |
128
|
|
|
* @return HTTPResponse |
129
|
|
|
*/ |
130
|
|
|
protected function getSchemaResponse($schemaID, $form = null, ValidationResult $errors = null, $extraData = []) |
131
|
|
|
{ |
132
|
|
|
$parts = $this->getRequest()->getHeader(LeftAndMain::SCHEMA_HEADER); |
133
|
|
|
$data = $this |
134
|
|
|
->getFormSchema() |
135
|
|
|
->getMultipartSchema($parts, $schemaID, $form, $errors); |
136
|
|
|
|
137
|
|
|
if ($extraData) { |
|
|
|
|
138
|
|
|
$data = array_merge($data, $extraData); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$response = new HTTPResponse(Convert::raw2json($data)); |
142
|
|
|
$response->addHeader('Content-Type', 'application/json'); |
143
|
|
|
return $response; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|