Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Move form schema logic into FormSchemaController #1850

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions code/FormSchemaController.php
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in this class is mostly directly copied in from LeftAndMain.

Note that there were no existing unit tests explicitly for the methods in this class - instead, this functionality is fairly heavily tested in behat (linkfield, elemental, history controller, WYSIWYG link modals all use it)

Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace SilverStripe\Admin;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
use SilverStripe\Forms\Schema\FormSchema;
use SilverStripe\Security\Security;

/**
* 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',
];

private static array $url_handlers = [
'GET schema/$FormName/$ItemID/$OtherItemID' => 'schema',
];

private static array $dependencies = [
'FormSchema' => '%$' . FormSchema::class,
];

/**
* Current form schema helper
*/
private ?FormSchema $schema = null;

protected function init()
{
$this->beforeExtending('onInit', function () {
$this->prepareTinyMce();
});
parent::init();
}

/**
* Get the form schema helper for this controller
*/
public function getFormSchema(): FormSchema
{
if (!$this->schema) {
$this->schema = FormSchema::singleton();
}
Comment on lines +54 to +56
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't there in LeftAndMain but it's a little protection in case the dependency doesn't get injected (e.g. not using injector to instantiate this controller for some reason)

return $this->schema;
}

/**
* Set the 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');
}

// Most usages of this method (e.g. AssetAdmin) have two methods - one method is
// named the same as the form, and does not accept a parameter - the other prefixed
// with "get" and accepts an $itemID parameter.
// Some usages exclude the getter method, so we need to check for both.
$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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->jsonError(401, 'Form not accessible');
$this->jsonError(404, 'Form not accessible');

It's an invalid route, rather than an issue with a user not being logged in

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing error codes is not in scope for this PR. This isn't new code.

}

if ($itemID) {
$form = $this->{$formMethod}($itemID);
} else {
$form = $this->{$formMethod}();
}
$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;
}

private function prepareTinyMce(): void
{
// Set the members html editor config
if (Security::getCurrentUser()) {
HTMLEditorConfig::set_active_identifier(Security::getCurrentUser()->getHtmlEditorConfigForCMS());
}

// Set default values in the config if missing. These things can't be defined in the config
// file because insufficient information exists when that is being processed
$htmlEditorConfig = HTMLEditorConfig::get_active();
$htmlEditorConfig->setOption('language', TinyMCEConfig::get_tinymce_lang());
$langUrl = TinyMCEConfig::get_tinymce_lang_url();
if ($langUrl) {
$htmlEditorConfig->setOption('language_url', $langUrl);
}
}
}
Loading