-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW Move form schema logic into FormSchemaController
- Loading branch information
1 parent
f7ff272
commit 26138b8
Showing
3 changed files
with
177 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Admin; | ||
|
||
use SilverStripe\Control\HTTPRequest; | ||
use SilverStripe\Control\HTTPResponse; | ||
use SilverStripe\Core\Validation\ValidationResult; | ||
use SilverStripe\Forms\Form; | ||
use SilverStripe\Forms\Schema\FormSchema; | ||
|
||
/** | ||
* The base class for controllers in the CMS that need access to form schema functionality. | ||
*/ | ||
abstract class FormSchemaController extends AdminController | ||
{ | ||
/** | ||
* Form schema header identifier | ||
*/ | ||
public const SCHEMA_HEADER = 'X-Formschema-Request'; | ||
|
||
private static array $allowed_actions = [ | ||
'schema', | ||
'methodSchema', | ||
]; | ||
|
||
private static array $url_handlers = [ | ||
'GET schema/$FormName/$ItemID/$OtherItemID' => 'schema', | ||
'GET methodSchema/$Method/$FormName/$ItemID' => 'methodSchema', | ||
]; | ||
|
||
private static array $dependencies = [ | ||
'FormSchema' => '%$' . FormSchema::class, | ||
]; | ||
|
||
/** | ||
* Current form schema helper | ||
*/ | ||
private ?FormSchema $schema = null; | ||
|
||
/** | ||
* Get form schema helper | ||
*/ | ||
public function getFormSchema(): FormSchema | ||
{ | ||
if (!$this->schema) { | ||
$this->schema = FormSchema::singleton(); | ||
} | ||
return $this->schema; | ||
} | ||
|
||
/** | ||
* Set form schema helper for this controller | ||
*/ | ||
public function setFormSchema(FormSchema $schema): static | ||
{ | ||
$this->schema = $schema; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Gets a JSON schema representing the current edit form. | ||
*/ | ||
public function schema(HTTPRequest $request): HTTPResponse | ||
{ | ||
$formName = $request->param('FormName'); | ||
$itemID = $request->param('ItemID'); | ||
|
||
if (!$formName) { | ||
$this->jsonError(400, 'Missing request params'); | ||
} | ||
|
||
// The getter can accept an ID where the main form action wouldn't | ||
$formMethod = "get{$formName}"; | ||
if (!$this->hasMethod($formMethod)) { | ||
$formMethod = $formName; | ||
if (!$this->hasMethod($formMethod)) { | ||
$this->jsonError(404, 'Form not found'); | ||
} | ||
} | ||
|
||
if (!$this->hasAction($formName)) { | ||
$this->jsonError(401, 'Form not accessible'); | ||
} | ||
|
||
if ($itemID) { | ||
$form = $this->{$formMethod}($itemID); | ||
} else { | ||
$form = $this->{$formMethod}(); | ||
} | ||
$schemaID = $request->getURL(); | ||
return $this->getSchemaResponse($schemaID, $form); | ||
} | ||
|
||
/** | ||
* Get the form schema from a given method. | ||
* The method must return a Form. | ||
*/ | ||
public function methodSchema(HTTPRequest $request): HTTPResponse | ||
{ | ||
$method = $request->param('Method'); | ||
$formName = $request->param('FormName'); | ||
$itemID = $request->param('ItemID'); | ||
|
||
if (!$formName || !$method) { | ||
$this->jsonError(400, 'Missing request params'); | ||
} | ||
|
||
if (!$this->hasMethod($method)) { | ||
$this->jsonError(404, 'Method not found'); | ||
} | ||
if (!$this->hasAction($method)) { | ||
$this->jsonError(401, 'Method not accessible'); | ||
} | ||
|
||
$methodItem = $this->{$method}(); | ||
if (!$methodItem->hasMethod($formName)) { | ||
$this->jsonError(404, 'Form not found'); | ||
} | ||
if (!$methodItem->hasAction($formName)) { | ||
$this->jsonError(401, 'Form not accessible'); | ||
} | ||
|
||
$form = $methodItem->{$formName}($itemID); | ||
$schemaID = $request->getURL(); | ||
|
||
return $this->getSchemaResponse($schemaID, $form); | ||
} | ||
|
||
/** | ||
* Check if the current request has a X-Formschema-Request header set. | ||
* Used by conditional logic that responds to validation results | ||
*/ | ||
protected function getSchemaRequested(): bool | ||
{ | ||
$parts = $this->getRequest()->getHeader(static::SCHEMA_HEADER); | ||
return !empty($parts); | ||
} | ||
|
||
/** | ||
* Generate schema for the given form based on the X-Formschema-Request header value | ||
* | ||
* @param string $schemaID ID for this schema. Required. | ||
* @param Form|null $form Required for 'state' or 'schema' response | ||
* @param ValidationResult $errors Required for 'error' response | ||
* @param array $extraData Any extra data to be merged with the schema response | ||
*/ | ||
protected function getSchemaResponse(string $schemaID, ?Form $form = null, ValidationResult $errors = null, array $extraData = []): HTTPResponse | ||
{ | ||
$parts = $this->getRequest()->getHeader(static::SCHEMA_HEADER); | ||
$data = $this | ||
->getFormSchema() | ||
->getMultipartSchema($parts, $schemaID, $form, $errors); | ||
|
||
if ($extraData) { | ||
$data = array_merge($data, $extraData); | ||
} | ||
|
||
$response = new HTTPResponse(json_encode($data)); | ||
$response->addHeader('Content-Type', 'application/json'); | ||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.