Skip to content

Commit 85bc9bd

Browse files
[FEATURE] Optional validators (kitodo#1760)
Co-authored-by: Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
1 parent 26e50f6 commit 85bc9bd

File tree

4 files changed

+108
-29
lines changed

4 files changed

+108
-29
lines changed

Classes/Controller/ValidationFormController.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
namespace Kitodo\Dlf\Controller;
1313

1414
use Psr\Http\Message\ResponseInterface;
15+
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
16+
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
17+
use TYPO3\CMS\Core\Utility\GeneralUtility;
18+
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
1519

1620
/**
1721
* Plugin 'ValidationForm' for the 'dlf' extension
@@ -31,8 +35,33 @@ class ValidationFormController extends AbstractController
3135
*/
3236
public function mainAction(): ResponseInterface
3337
{
34-
$language = $this->request->getAttribute('language');
35-
$this->view->assign("url", $language->getBase()->getPath() . '?middleware=dlf/domDocumentValidation');
38+
/** @var SiteLanguage $siteLanguage */
39+
$siteLanguage = $this->request->getAttribute('language') ?? $this->request->getAttribute('site')->getDefaultLanguage();
40+
41+
$typeParam = 'dfgviewer';
42+
43+
// retrieve validation configuration from plugin.tx_dlf typoscript
44+
/** @var ConfigurationManagerInterface $configurationManager */
45+
$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
46+
$typoScript = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
47+
48+
/** @var TypoScriptService $typoScriptService */
49+
$typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
50+
$settings = $typoScriptService->convertTypoScriptArrayToPlainArray($typoScript['plugin.']['tx_dlf.']['settings.']);
51+
52+
$disabledValidators = [];
53+
if (array_key_exists("domDocumentValidation", $settings) && array_key_exists($typeParam, $settings["domDocumentValidation"])) {
54+
$validationConfiguration = $settings['domDocumentValidation'][$typeParam];
55+
foreach ($validationConfiguration as $key => $value) {
56+
if (isset($value['disabled']) && $value['disabled'] === "true") {
57+
$disabledValidators[$key]['title'] = $value['title'];
58+
$disabledValidators[$key]['shortdescription'] = $value['shortdescription'];
59+
}
60+
}
61+
}
62+
63+
$this->view->assign("url", $siteLanguage->getBase()->getPath() . '?middleware=dlf/domDocumentValidation');
64+
$this->view->assign("disabledValidators", $disabledValidators);
3665
return $this->htmlResponse();
3766
}
3867
}

Classes/Middleware/DOMDocumentValidation.php

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<?php
2-
3-
namespace Kitodo\Dlf\Middleware;
4-
52
/**
63
* (c) Kitodo. Key to digital objects e.V. <contact@kitodo.org>
74
*
@@ -12,6 +9,8 @@
129
* LICENSE.txt file that was distributed with this source code.
1310
*/
1411

12+
namespace Kitodo\Dlf\Middleware;
13+
1514
use DOMDocument;
1615
use InvalidArgumentException;
1716
use Kitodo\Dlf\Validation\DOMDocumentValidationStack;
@@ -24,6 +23,7 @@
2423
use TYPO3\CMS\Core\Localization\LanguageService;
2524
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
2625
use TYPO3\CMS\Core\Log\LogManager;
26+
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
2727
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
2828
use TYPO3\CMS\Core\Utility\GeneralUtility;
2929
use TYPO3\CMS\Core\Utility\PathUtility;
@@ -116,7 +116,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
116116
throw new InvalidArgumentException('Validation configuration type does not exist.', 1744373532);
117117
}
118118

119-
$validationConfiguration = $settings['domDocumentValidation'][$typeParam];
119+
$validationConfiguration = $this->removeDisabledValidators($parameters, $settings['domDocumentValidation'][$typeParam]);
120+
120121
$validation = GeneralUtility::makeInstance(DOMDocumentValidationStack::class, $validationConfiguration);
121122
// validate and return json response
122123
return $this->getJsonResponse($validationConfiguration, $validation->validate($document));
@@ -159,11 +160,45 @@ protected function getJsonResponse(array $configurations, ?Result $result): Resp
159160
return $response;
160161
}
161162

162-
private function getTranslation(string $key, ?array $arguments = null): string
163+
/**
164+
* Get the message.
165+
*
166+
* @return \Closure
167+
*/
168+
protected function getMessageText(): \Closure
169+
{
170+
return function (Message $message): string {
171+
return $message->getMessage();
172+
};
173+
}
174+
175+
/**
176+
* Removes validators marked as disabled from the configuration.
177+
*
178+
* If a validator in the configuration has the disabled flag set to true and its key does not exist in the enabledValidators parameter, it will be removed.
179+
*
180+
* @param array $parameters The parameters of the middleware.
181+
* @param array $validationConfiguration The validation configuration to remove from
182+
* @return array The validator configuration without the disabled validators
183+
*/
184+
protected function removeDisabledValidators(array $parameters, array $validationConfiguration): array
185+
{
186+
$enableValidators = [];
187+
if (array_key_exists("enableValidators", $parameters)) {
188+
$enableValidators = explode(",", $parameters['enableValidators']);
189+
}
190+
foreach ($validationConfiguration as $key => $value) {
191+
if (isset($value['disabled']) && $value['disabled'] === "true" && !in_array($key, $enableValidators)) {
192+
unset($validationConfiguration[$key]);
193+
}
194+
}
195+
return $validationConfiguration;
196+
}
197+
198+
protected function getTranslation(string $key, ?array $arguments = null): string
163199
{
164-
$language =
165-
$this->request->getAttribute('language')
166-
?? $this->request->getAttribute('site')->getDefaultLanguage();
200+
/** @var SiteLanguage $siteLanguage */
201+
$siteLanguage = $this->request->getAttribute('language') ?? $this->request->getAttribute('site')->getDefaultLanguage();
167202

168203
/** @var LanguageServiceFactory $languageServiceFactory */
169204
$languageServiceFactory = GeneralUtility::makeInstance(
@@ -172,7 +207,7 @@ private function getTranslation(string $key, ?array $arguments = null): string
172207

173208
/** @var LanguageService $languageService */
174209
$languageService = $languageServiceFactory
175-
->createFromSiteLanguage($language);
210+
->createFromSiteLanguage($siteLanguage);
176211

177212
if (isset($arguments) && count($arguments) > 0) {
178213
return vsprintf(
@@ -182,16 +217,4 @@ private function getTranslation(string $key, ?array $arguments = null): string
182217
}
183218
return $languageService->sL($key);
184219
}
185-
186-
/**
187-
* Get the message.
188-
*
189-
* @return \Closure
190-
*/
191-
private function getMessageText(): \Closure
192-
{
193-
return function (Message $message): string {
194-
return $message->getMessage();
195-
};
196-
}
197220
}

Resources/Private/Templates/ValidationForm/Main.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
<f:format.raw><f:translate key='LLL:EXT:dlf/Resources/Private/Language/locallang.xlf:plugins.validationform.label' /></f:format.raw>
1313
<input type="text" name="url" class="url" value="" required>
1414
</label>
15+
<div class="disabled-validators">
16+
<f:for each="{disabledValidators}" as="value" key="key" >
17+
<label title="{f:translate(key: value.shortdescription)}">
18+
<input type="checkbox" name="enableValidator[]" class="checkbox" value="{key}"/>
19+
{f:translate(key: value.title)}
20+
</label>
21+
</f:for>
22+
</div>
1523
<input type="submit" class="submit" value="<f:translate key='LLL:EXT:dlf/Resources/Private/Language/locallang.xlf:plugins.validationform.validate'/>">
1624
</form>
17-
18-
<script>
19-
20-
</script>
2125
</div>
2226
</html>

Resources/Public/JavaScript/ValidationForm/ValidationForm.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,26 @@ dlfValidationForms.forEach((validationForm) => {
5050
validationForm.addEventListener("submit", function (event) {
5151
event.preventDefault();
5252

53+
const formData = new FormData(event.target);
54+
const data = {};
55+
5356
// Convert submitted values to data
54-
const data = Object.fromEntries(new FormData(event.target).entries());
57+
for (const [key, value] of formData.entries()) {
58+
// If key ends with [], treat as array
59+
if (key.endsWith('[]')) {
60+
const cleanKey = key.slice(0, -2);
61+
// eslint-disable-next-line
62+
if (!data[cleanKey]) {
63+
// eslint-disable-next-line
64+
data[cleanKey] = [];
65+
}
66+
// eslint-disable-next-line
67+
data[cleanKey].push(value);
68+
} else {
69+
// eslint-disable-next-line
70+
data[key] = value;
71+
}
72+
}
5573

5674
/**
5775
* Create a list of messages.
@@ -140,7 +158,12 @@ dlfValidationForms.forEach((validationForm) => {
140158
const loader = buildLoader(event.target);
141159
const form = event.target.parentElement;
142160

143-
getData(this.action + '&type=' + encodeURI(data.type) + '&url=' + encodeURI(data.url)).then(data => {
161+
let dataUrl = this.action + '&type=' + encodeURIComponent(data.type) + '&url=' + encodeURIComponent(data.url);
162+
if (data.enableValidator && Array.isArray(data.enableValidator)) {
163+
dataUrl += '&enableValidators=' + encodeURIComponent(data.enableValidator.join(','));
164+
}
165+
166+
getData(dataUrl).then(data => {
144167
let validation = form.querySelector('.validation');
145168
if (validation) {
146169
form.removeChild(validation);

0 commit comments

Comments
 (0)