[TASK] Move some functions to traits for reuse
This commit is contained in:
parent
f206c8fb0e
commit
f427ab7cd6
@ -12,6 +12,8 @@ namespace Cjel\TemplatesAide\Controller;
|
||||
*
|
||||
***/
|
||||
|
||||
use Cjel\TemplatesAide\Traits\ValidationTrait;
|
||||
use Cjel\TemplatesAide\Traits\FormatResultTrait;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Core\Cache\CacheManager;
|
||||
@ -21,10 +23,10 @@ use TYPO3\CMS\Core\Log\LogManager;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
|
||||
use TYPO3\CMS\Extbase\Object\ObjectManager;
|
||||
use TYPO3\CMS\Frontend\Plugin\AbstractPlugin;
|
||||
use TYPO3\CMS\Frontend\Utility\EidUtility;
|
||||
use TYPO3\CMS\Extbase\Reflection\ClassSchema;
|
||||
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
|
||||
use TYPO3\CMS\Frontend\Plugin\AbstractPlugin;
|
||||
use TYPO3\CMS\Frontend\Utility\EidUtility;
|
||||
|
||||
/**
|
||||
* AbstractEIDController
|
||||
@ -32,6 +34,18 @@ use TYPO3\CMS\Extbase\Reflection\ReflectionService;
|
||||
class AbstractEIDController
|
||||
{
|
||||
|
||||
/**
|
||||
* ValidationTrait
|
||||
*/
|
||||
use ValidationTrait {
|
||||
validateAgainstSchema as traitValidateAgainstSchema;
|
||||
}
|
||||
|
||||
/**
|
||||
* FormatResultTrait
|
||||
*/
|
||||
use FormatResultTrait;
|
||||
|
||||
/**
|
||||
* @var BackendConfigurationManager
|
||||
*/
|
||||
@ -99,11 +113,13 @@ class AbstractEIDController
|
||||
$this->apiUtility = $this->objectManager->get(
|
||||
\Cjel\TemplatesAide\Utility\ApiUtility::class
|
||||
);
|
||||
$this->configurationManager->setConfiguration(array());
|
||||
$frameworkConfiguration = $this->configurationManager->getConfiguration(
|
||||
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
|
||||
$this->getExtensionKey()
|
||||
);
|
||||
$this->configurationManager->setConfiguration(
|
||||
$frameworkConfiguration
|
||||
);
|
||||
$this->settings = $frameworkConfiguration;
|
||||
$this->storagePids = explode(
|
||||
',',
|
||||
@ -241,4 +257,20 @@ class AbstractEIDController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* return function
|
||||
*
|
||||
* @param array $result
|
||||
* @return void
|
||||
*/
|
||||
protected function returnFunction(
|
||||
$result = []
|
||||
) {
|
||||
$result = $this->formatResult($result);
|
||||
unset($result['cid']);
|
||||
unset($result['componentMode']);
|
||||
unset($result['isValid']);
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ namespace Cjel\TemplatesAide\Controller;
|
||||
|
||||
/***
|
||||
*
|
||||
* This file is part of the "Templates Aide" Extension for TYPO3 CMS.
|
||||
/ This file is part of the "Templates Aide" Extension for TYPO3 CMS.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
@ -415,31 +415,6 @@ class ActionController extends BaseController
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* shortcut to get translation
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function getTranslation($key, $arguments = null)
|
||||
{
|
||||
$translation = LocalizationUtility::translate(
|
||||
$key,
|
||||
$this->getExtensionKey(),
|
||||
$arguments
|
||||
);
|
||||
if ($translation) {
|
||||
return $translation;
|
||||
}
|
||||
$translation = LocalizationUtility::translate(
|
||||
$key,
|
||||
'site_templates',
|
||||
$arguments
|
||||
);
|
||||
if ($translation) {
|
||||
return $translation;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
@ -454,41 +429,6 @@ class ActionController extends BaseController
|
||||
strtolower($reflection->getShortName());
|
||||
}
|
||||
|
||||
/**
|
||||
* gets error label based on field and keyword, uses predefined extensionkey
|
||||
*/
|
||||
protected function getErrorLabel($field, $keyword) {
|
||||
$path = 'error.' . $field . '.' . $keyword;
|
||||
$errorLabel = $this->getTranslation($path);
|
||||
if ($errorLabel == null) {
|
||||
return $path;
|
||||
}
|
||||
return $errorLabel;
|
||||
}
|
||||
|
||||
/**
|
||||
* function to add validation error manually in the controller
|
||||
*/
|
||||
protected function addValidationError(
|
||||
$field, $keyword, $overwrite = false
|
||||
) {
|
||||
$this->isValid = false;
|
||||
$this->responseStatus = [400 => 'validationError'];
|
||||
if (!array_key_exists($field, $this->errors)
|
||||
|| $overwrite == true
|
||||
) {
|
||||
$this->errors[$field] = [
|
||||
'keyword' => $keyword,
|
||||
];
|
||||
$this->errorLabels[$field] = $this->getErrorLabel(
|
||||
$field,
|
||||
$keyword
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* legacy function to prevent beaking old code
|
||||
*
|
||||
|
85
Classes/Traits/FormatResultTrait.php
Normal file
85
Classes/Traits/FormatResultTrait.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
namespace Cjel\TemplatesAide\Traits;
|
||||
|
||||
/***
|
||||
*
|
||||
* This file is part of the "Templates Aide" Extension for TYPO3 CMS.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* (c) 2021 Philipp Dieter <philippdieter@attic-media.net>
|
||||
*
|
||||
***/
|
||||
|
||||
/**
|
||||
* ValidationTrait
|
||||
*/
|
||||
trait FormatResultTrait
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function formatResult()
|
||||
{
|
||||
if ($result == null) {
|
||||
$result = [];
|
||||
}
|
||||
if (!empty($this->errors)) {
|
||||
$result = array_merge(
|
||||
$result,
|
||||
['errors' => $this->errors]
|
||||
);
|
||||
}
|
||||
if (!empty($this->errorLabels)) {
|
||||
$result = array_merge(
|
||||
$result,
|
||||
['errorLabels' => $this->errorLabels]
|
||||
);
|
||||
}
|
||||
if (is_array($this->responseStatus)) {
|
||||
$result = array_merge(
|
||||
$result,
|
||||
['errorType' => reset($this->responseStatus)]
|
||||
);
|
||||
}
|
||||
if ($this->pageType) {
|
||||
if (is_array($this->responseStatus)) {
|
||||
$this->response->setStatus(
|
||||
array_key_first($this->responseStatus)
|
||||
);
|
||||
} else {
|
||||
$this->response->setStatus($this->responseStatus);
|
||||
}
|
||||
if ($this->pageType == $this->ajaxPageType) {
|
||||
if ($this->environmentService->isEnvironmentInBackendMode()) {
|
||||
header('Content-Type: application/json');
|
||||
} else {
|
||||
$GLOBALS['TSFE']->setContentType('application/json');
|
||||
}
|
||||
}
|
||||
unset($result['data']);
|
||||
if ($this->redirect) {
|
||||
$result['redirect'] = $this->redirect;
|
||||
}
|
||||
if ($this->reload) {
|
||||
$result['reload'] = true;
|
||||
}
|
||||
return json_encode($result);
|
||||
}
|
||||
$result = array_merge(
|
||||
$result,
|
||||
['cid' => $this->contentObjectUid],
|
||||
['isValid' => $this->isValid],
|
||||
['componentMode' => $this->componentMode]
|
||||
);
|
||||
if (!empty($this->ajaxEnv)) {
|
||||
$result = array_merge(
|
||||
$result,
|
||||
['ajaxEnv' => $this->ajaxEnv]
|
||||
);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
@ -16,6 +16,7 @@ use \Opis\JsonSchema\{
|
||||
Validator, ValidationResult, ValidationError, Schema
|
||||
};
|
||||
use Cjel\TemplatesAide\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
|
||||
|
||||
/**
|
||||
* ValidationTrait
|
||||
@ -96,4 +97,64 @@ trait ValidationTrait
|
||||
return $validationResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* function to add validation error manually in the controller
|
||||
*/
|
||||
protected function addValidationError(
|
||||
$field, $keyword, $overwrite = false
|
||||
) {
|
||||
$this->isValid = false;
|
||||
$this->responseStatus = [400 => 'validationError'];
|
||||
if (!array_key_exists($field, $this->errors)
|
||||
|| $overwrite == true
|
||||
) {
|
||||
$this->errors[$field] = [
|
||||
'keyword' => $keyword,
|
||||
];
|
||||
$this->errorLabels[$field] = $this->getErrorLabel(
|
||||
$field,
|
||||
$keyword
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* gets error label based on field and keyword, uses predefined extensionkey
|
||||
*/
|
||||
protected function getErrorLabel($field, $keyword) {
|
||||
$path = 'error.' . $field . '.' . $keyword;
|
||||
$errorLabel = $this->getTranslation($path);
|
||||
if ($errorLabel == null) {
|
||||
return $path;
|
||||
}
|
||||
return $errorLabel;
|
||||
}
|
||||
|
||||
/**
|
||||
* shortcut to get translation
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function getTranslation($key, $arguments = null)
|
||||
{
|
||||
$translation = LocalizationUtility::translate(
|
||||
$key,
|
||||
$this->getExtensionKey(),
|
||||
$arguments
|
||||
);
|
||||
if ($translation) {
|
||||
return $translation;
|
||||
}
|
||||
$translation = LocalizationUtility::translate(
|
||||
$key,
|
||||
'site_templates',
|
||||
$arguments
|
||||
);
|
||||
if ($translation) {
|
||||
return $translation;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user