‪TYPO3CMS  ‪main
EditController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
32 
38 #[AsController]
40 {
41  protected const ‪JAVASCRIPT_HELPER = 'EXT:backend/Resources/Public/JavaScript/helper.js';
42 
58  protected ‪$P;
59 
65  protected ‪$doClose;
66 
70  protected string ‪$closeWindow;
71 
72  public function ‪__construct(
73  private readonly ‪FlexFormTools $flexFormTools,
74  ) {
75  $this->closeWindow = sprintf(
76  '<script %s></script>',
77  GeneralUtility::implodeAttributes([
79  GeneralUtility::getFileAbsFileName(self::JAVASCRIPT_HELPER)
80  ),
81  'data-action' => 'window.close',
82  ], true)
83  );
84  }
85 
90  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
91  {
92  $parsedBody = $request->getParsedBody();
93  $queryParams = $request->getQueryParams();
94 
95  $this->P = $parsedBody['P'] ?? $queryParams['P'] ?? [];
96  // Used for the return URL to FormEngine so that we can close the window.
97  $this->doClose = $parsedBody['doClose'] ?? $queryParams['doClose'] ?? 0;
98 
99  return $this->‪processRequest();
100  }
101 
107  protected function ‪processRequest(): ResponseInterface
108  {
109  if ($this->doClose) {
110  return new ‪HtmlResponse($this->closeWindow);
111  }
112  // Initialize:
113  $table = $this->P['table'];
114  $field = $this->P['field'];
115 
116  if (empty($this->P['flexFormDataStructureIdentifier'])) {
117  // If there is not flex data structure identifier, field config is found in globals
118  $config = ‪$GLOBALS['TCA'][$table]['columns'][$field]['config'];
119  } else {
120  // If there is a flex data structure identifier, parse that data structure and
121  // fetch config defined by given flex path
122  $dataStructure = $this->flexFormTools->parseDataStructureByIdentifier($this->P['flexFormDataStructureIdentifier']);
123  $config = ‪ArrayUtility::getValueByPath($dataStructure, $this->P['flexFormDataStructurePath']);
124  if (!is_array($config)) {
125  throw new \RuntimeException(
126  'Something went wrong finding flex path ' . $this->P['flexFormDataStructurePath']
127  . ' in data structure identified by ' . $this->P['flexFormDataStructureIdentifier'],
128  1537356346
129  );
130  }
131  }
132 
133  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
134  $urlParameters = [
135  'returnUrl' => (string)$uriBuilder->buildUriFromRoute('wizard_edit', ['doClose' => 1]),
136  ];
137 
138  // Detecting the various allowed field type setups and acting accordingly.
139  if (is_array($config)
140  && $config['type'] === 'select'
141  && !($config['MM'] ?? false)
142  && (int)($config['maxitems'] ?? 0) <= 1
143  && ‪MathUtility::canBeInterpretedAsInteger($this->P['currentValue'])
144  && $this->P['currentValue']
145  && $config['foreign_table']
146  ) {
147  // SINGLE value
148  $urlParameters['edit[' . $config['foreign_table'] . '][' . $this->P['currentValue'] . ']'] = 'edit';
149  // Redirect to FormEngine
150  ‪$url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
151  return new RedirectResponse(‪$url);
152  }
153 
154  if (!empty($config['type'])
155  && !empty($this->P['currentSelectedValues'])
156  && (
157  $config['type'] === 'select' && !empty($config['foreign_table'])
158  || $config['type'] === 'group' && !empty($config['allowed'])
159  )
160  ) {
161  // MULTIPLE VALUES:
162  // Init settings:
163  $allowedTables = $config['type'] === 'group' ? $config['allowed'] : $config['foreign_table'];
164  // Selecting selected values into an array:
165  $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
166  $relationHandler->start($this->P['currentSelectedValues'], $allowedTables);
167  $value = $relationHandler->getValueArray(true);
168  // Traverse that array and make parameters for FormEngine
169  foreach ($value as $rec) {
170  $recTableUidParts = ‪GeneralUtility::revExplode('_', $rec, 2);
171  $urlParameters['edit[' . $recTableUidParts[0] . '][' . $recTableUidParts[1] . ']'] = 'edit';
172  }
173  // Redirect to FormEngine
174  ‪$url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
175 
176  return new RedirectResponse(‪$url);
177  }
178  return new HtmlResponse($this->closeWindow);
179  }
180 }
‪TYPO3\CMS\Backend\Controller\Wizard\EditController\processRequest
‪processRequest()
Definition: EditController.php:105
‪TYPO3\CMS\Backend\Controller\Wizard\EditController\$doClose
‪int $doClose
Definition: EditController.php:63
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Database\RelationHandler
Definition: RelationHandler.php:36
‪TYPO3\CMS\Backend\Controller\Wizard\EditController\JAVASCRIPT_HELPER
‪const JAVASCRIPT_HELPER
Definition: EditController.php:41
‪TYPO3\CMS\Backend\Controller\Wizard\EditController\$closeWindow
‪string $closeWindow
Definition: EditController.php:68
‪TYPO3\CMS\Backend\Controller\Wizard\EditController\mainAction
‪mainAction(ServerRequestInterface $request)
Definition: EditController.php:88
‪TYPO3\CMS\Core\Utility\ArrayUtility\getValueByPath
‪static getValueByPath(array $array, array|string $path, string $delimiter='/')
Definition: ArrayUtility.php:176
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath(string $targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:52
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Backend\Controller\Wizard
Definition: AddController.php:18
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
Definition: FlexFormTools.php:40
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Backend\Controller\Wizard\EditController\__construct
‪__construct(private readonly FlexFormTools $flexFormTools,)
Definition: EditController.php:70
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility\revExplode
‪static list< string > revExplode(string $delimiter, string $string, int $limit=0)
Definition: GeneralUtility.php:787
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Backend\Controller\Wizard\EditController
Definition: EditController.php:40
‪TYPO3\CMS\Backend\Controller\Wizard\EditController\$P
‪array $P
Definition: EditController.php:57
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:28