‪TYPO3CMS  ‪main
CreateRecordReaction.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\ResponseFactoryInterface;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Message\StreamFactoryInterface;
32 
39 {
40  public function ‪__construct(
41  private readonly ResponseFactoryInterface $responseFactory,
42  private readonly StreamFactoryInterface $streamFactory,
43  ) {}
44 
45  public static function ‪getType(): string
46  {
47  return 'create-record';
48  }
49 
50  public static function ‪getDescription(): string
51  {
52  return 'LLL:EXT:reactions/Resources/Private/Language/locallang_db.xlf:sys_reaction.reaction_type.create_record';
53  }
54 
55  public static function ‪getIconIdentifier(): string
56  {
57  return 'content-database';
58  }
59 
60  public function ‪react(ServerRequestInterface $request, array $payload, ‪ReactionInstruction $reaction): ResponseInterface
61  {
62  // @todo: Response needs to be based on given accept headers
63 
64  $table = (string)($reaction->‪toArray()['table_name'] ?? '');
65  ‪$fields = (array)($reaction->‪toArray()['fields'] ?? []);
66 
67  if (!(new ‪CreateRecordReactionTable($table))->isAllowedForCreation()) {
68  return $this->‪jsonResponse(['success' => false, 'error' => 'Invalid argument "table_name"'], 400);
69  }
70 
71  if (‪$fields === []) {
72  return $this->‪jsonResponse(['success' => false, 'error' => 'No fields given.'], 400);
73  }
74 
75  $dataHandlerData = [];
76  foreach (‪$fields as $fieldName => $value) {
77  $dataHandlerData[$fieldName] = $this->‪replacePlaceHolders($value, $payload);
78  }
79  $dataHandlerData['pid'] = (int)($reaction->‪toArray()['storage_pid'] ?? 0);
80 
81  $data[$table][‪StringUtility::getUniqueId('NEW')] = $dataHandlerData;
82  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
83  $dataHandler->start($data, [], $this->‪getBackendUser());
84  $dataHandler->process_datamap();
85 
86  return $this->‪buildResponseFromDataHandler($dataHandler, 201);
87  }
88 
92  public function ‪replacePlaceHolders(mixed $value, array $payload): string
93  {
94  if (is_string($value)) {
95  $re = '/\$\{([^\}]*)\}/m';
96  preg_match_all($re, $value, $matches, PREG_SET_ORDER, 0);
97  foreach ($matches as $match) {
98  try {
99  $value = str_replace($match[0], (string)‪ArrayUtility::getValueByPath($payload, $match[1], '.'), $value);
100  } catch (‪MissingArrayPathException) {
101  // Ignore this exception to show the user that there was no placeholder in the payload
102  }
103  }
104  }
105  return $value;
106  }
107 
108  protected function ‪buildResponseFromDataHandler(‪DataHandler $dataHandler, int $successCode = 200): ResponseInterface
109  {
110  // Success depends on whether at least one NEW id has been substituted
111  $success = $dataHandler->substNEWwithIDs !== [] && $dataHandler->substNEWwithIDs_table !== [];
112 
113  ‪$statusCode = $successCode;
114  $data = [
115  'success' => $success,
116  ];
117 
118  if (!$success) {
119  ‪$statusCode = 400;
120  $data['error'] = 'Record could not be created';
121  }
122 
123  return $this->‪jsonResponse($data, ‪$statusCode);
124  }
125 
126  protected function ‪jsonResponse(array $data, int ‪$statusCode = 200): ResponseInterface
127  {
128  return $this->responseFactory
129  ->createResponse(‪$statusCode)
130  ->withHeader('Content-Type', 'application/json')
131  ->withBody($this->streamFactory->createStream((string)json_encode($data)));
132  }
133 
135  {
136  return ‪$GLOBALS['BE_USER'];
137  }
138 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction\getType
‪static getType()
Definition: CreateRecordReaction.php:45
‪TYPO3\CMS\Reactions\Model\ReactionInstruction
Definition: ReactionInstruction.php:27
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction\react
‪react(ServerRequestInterface $request, array $payload, ReactionInstruction $reaction)
Definition: CreateRecordReaction.php:60
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction\jsonResponse
‪jsonResponse(array $data, int $statusCode=200)
Definition: CreateRecordReaction.php:126
‪TYPO3\CMS\Reactions\Model\ReactionInstruction\toArray
‪toArray()
Definition: ReactionInstruction.php:63
‪TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException
Definition: MissingArrayPathException.php:27
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction\buildResponseFromDataHandler
‪buildResponseFromDataHandler(DataHandler $dataHandler, int $successCode=200)
Definition: CreateRecordReaction.php:108
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction\getBackendUser
‪getBackendUser()
Definition: CreateRecordReaction.php:134
‪TYPO3\CMS\Reactions\Authentication\ReactionUserAuthentication
Definition: ReactionUserAuthentication.php:31
‪$fields
‪$fields
Definition: pages.php:5
‪TYPO3\CMS\Core\Utility\ArrayUtility\getValueByPath
‪static getValueByPath(array $array, array|string $path, string $delimiter='/')
Definition: ArrayUtility.php:176
‪TYPO3\CMS\Redirects\Message\$statusCode
‪identifier readonly UriInterface readonly int $statusCode
Definition: RedirectWasHitMessage.php:34
‪TYPO3\CMS\Reactions\Reaction\ReactionInterface
Definition: ReactionInterface.php:25
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction\getIconIdentifier
‪static getIconIdentifier()
Definition: CreateRecordReaction.php:55
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction\getDescription
‪static getDescription()
Definition: CreateRecordReaction.php:50
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction
Definition: CreateRecordReaction.php:39
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction\replacePlaceHolders
‪replacePlaceHolders(mixed $value, array $payload)
Definition: CreateRecordReaction.php:92
‪TYPO3\CMS\Reactions\Validation\CreateRecordReactionTable
Definition: CreateRecordReactionTable.php:28
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction\__construct
‪__construct(private readonly ResponseFactoryInterface $responseFactory, private readonly StreamFactoryInterface $streamFactory,)
Definition: CreateRecordReaction.php:40
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Reactions\Reaction
Definition: CreateRecordReaction.php:18
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57