‪TYPO3CMS  9.5
FieldProvider.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
27 {
31  protected ‪$commandManager;
32 
36  protected ‪$objectManager;
37 
41  protected ‪$reflectionService;
42 
46  protected ‪$task;
47 
55  public function ‪__construct(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface ‪$objectManager = null, \‪TYPO3\CMS\‪Extbase\Mvc\Cli\‪CommandManager ‪$commandManager = null, \‪TYPO3\CMS\‪Extbase\Reflection\ReflectionService ‪$reflectionService = null)
56  {
57  $this->objectManager = ‪$objectManager ?? \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\‪TYPO3\CMS\‪Extbase\Object\ObjectManager::class);
58  $this->commandManager = ‪$commandManager ?? $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\Cli\CommandManager::class);
59  $this->reflectionService = ‪$reflectionService ?? $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Reflection\ReflectionService::class);
60  }
61 
71  public function ‪getAdditionalFields(array &$taskInfo, ‪$task, ‪SchedulerModuleController $schedulerModule)
72  {
73  $this->task = ‪$task;
74  if ($this->task !== null) {
75  $this->task->‪setScheduler();
76  }
77  ‪$fields = [];
79  if ($this->task !== null && $this->task->getCommandIdentifier()) {
80  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
82  $argumentFields = $this->‪getCommandControllerActionArgumentFields($command->getArgumentDefinitions());
83  ‪$fields = array_merge(‪$fields, $argumentFields);
84  $this->task->save();
85  }
86  return ‪$fields;
87  }
88 
96  public function ‪validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
97  {
98  return true;
99  }
100 
108  public function ‪saveAdditionalFields(array $submittedData, AbstractTask ‪$task)
109  {
110  ‪$task->‪setCommandIdentifier($submittedData['task_extbase']['action']);
111  ‪$task->‪setArguments((array)$submittedData['task_extbase']['arguments']);
112  return true;
113  }
114 
121  {
122  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
123  return [
124  'code' => '',
125  'label' => '<strong>' . $command->getDescription() . '</strong>'
126  ];
127  }
128 
134  protected function ‪getCommandControllerActionField()
135  {
136  $commands = $this->commandManager->getAvailableCommands();
137  $options = [];
138  foreach ($commands as $command) {
139  if ($command->isInternal() === true || $command->isCliOnly() === true) {
140  continue;
141  }
142  $className = $command->getControllerClassName();
143  $classNameParts = explode('\\', $className);
144  // Skip vendor and product name for core classes
145  if (strpos($className, 'TYPO3\\CMS\\') === 0) {
146  $classPartsToSkip = 2;
147  } else {
148  $classPartsToSkip = 1;
149  }
150  $classNameParts = array_slice($classNameParts, $classPartsToSkip);
151  $extensionName = $classNameParts[0];
152  $controllerName = $classNameParts[2];
153  $identifier = $command->getCommandIdentifier();
154  $options[$identifier] = $extensionName . ' ' . str_replace('CommandController', '', $controllerName) . ': ' . $command->getControllerCommandName();
155  }
156  $name = 'action';
157  $currentlySelectedCommand = $this->task !== null ? $this->task->getCommandIdentifier() : null;
158  return [
159  'code' => $this->‪renderSelectField($name, $options, $currentlySelectedCommand),
160  'label' => $this->‪getActionLabel()
161  ];
162  }
163 
172  protected function ‪getCommandControllerActionArgumentFields(array $argumentDefinitions)
173  {
174  ‪$fields = [];
175  $argumentValues = $this->task->getArguments();
176  foreach ($argumentDefinitions as $argument) {
177  $name = $argument->getName();
178  $defaultValue = $this->‪getDefaultArgumentValue($argument);
179  $this->task->addDefaultValue($name, $defaultValue);
180  $value = $argumentValues[$name] ?? $defaultValue;
181  ‪$fields[$name] = [
182  'code' => $this->‪renderField($argument, $value),
183  'label' => $this->‪getArgumentLabel($argument)
184  ];
185  }
186  return ‪$fields;
187  }
188 
197  protected function ‪getLanguageLabel($localLanguageKey, $extensionName = null)
198  {
199  if (!$extensionName) {
200  list($extensionName, $commandControllerName, $commandName) = explode(':', $this->task->getCommandIdentifier());
201  }
202  $label = ‪\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($localLanguageKey, $extensionName);
203  return $label;
204  }
205 
212  protected function ‪getArgumentType(\‪TYPO3\CMS\‪Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
213  {
214  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
215  $controllerClassName = $command->getControllerClassName();
216  $methodName = $command->getControllerCommandName() . 'Command';
217 
218  $tags = $this->reflectionService
219  ->getClassSchema($controllerClassName)
220  ->getMethod($methodName)['tags']['param'] ?? [];
221  foreach ($tags as $tag) {
222  list($argumentType, $argumentVariableName) = explode(' ', $tag);
223  if (substr($argumentVariableName, 1) === $argument->getName()) {
224  return $argumentType;
225  }
226  }
227  return '';
228  }
229 
236  protected function ‪getArgumentLabel(\‪TYPO3\CMS\‪Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
237  {
238  $argumentName = $argument->getName();
239  list($extensionName, $commandControllerName, $commandName) = explode(':', $this->task->getCommandIdentifier());
240  $path = ['command', $commandControllerName, $commandName, 'arguments', $argumentName];
241  $labelNameIndex = implode('.', $path);
242  $label = $this->‪getLanguageLabel($labelNameIndex);
243  if (!$label) {
244  $label = 'Argument: ' . $argumentName;
245  }
246  $descriptionIndex = $labelNameIndex . '.description';
247  $description = $this->‪getLanguageLabel($descriptionIndex);
248  if ((string)$description === '') {
249  $description = $argument->getDescription();
250  }
251  if ((string)$description !== '') {
252  $label .= '. <em>' . htmlspecialchars($description) . '</em>';
253  }
254  return $label;
255  }
256 
263  protected function ‪getDefaultArgumentValue(\‪TYPO3\CMS\‪Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
264  {
265  $type = $this->‪getArgumentType($argument);
266  $argumentName = $argument->getName();
267  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
268 
269  $argumentReflection = $this->reflectionService
270  ->getClassSchema($command->getControllerClassName())
271  ->getMethod($command->getControllerCommandName() . 'Command')['params'] ?? [];
272 
273  $defaultValue = $argumentReflection[$argumentName]['defaultValue'];
274  if (‪TypeHandlingUtility::normalizeType($type) === 'boolean') {
275  $defaultValue = (bool)$defaultValue ? 1 : 0;
276  }
277  return $defaultValue;
278  }
279 
285  protected function ‪getActionLabel()
286  {
287  $index = 'task.action';
288  $label = $this->‪getLanguageLabel($index, 'extbase');
289  if (!$label) {
290  $label = 'CommandController Command. <em>Save and reopen to define command arguments</em>';
291  }
292  return $label;
293  }
294 
303  protected function ‪renderSelectField($name, array $options, $selectedOptionValue)
304  {
305  $html = [
306  '<select class="form-control" name="tx_scheduler[task_extbase][' . htmlspecialchars($name) . ']">'
307  ];
308  foreach ($options as $optionValue => $optionLabel) {
309  $selected = $optionValue === $selectedOptionValue ? ' selected="selected"' : '';
310  $html[] = '<option title="test" value="' . htmlspecialchars($optionValue) . '"' . $selected . '>' . htmlspecialchars($optionLabel) . '</option>';
311  }
312  $html[] = '</select>';
313  return implode(LF, $html);
314  }
315 
323  protected function ‪renderField(\‪TYPO3\CMS\‪Extbase\Mvc\Cli\CommandArgumentDefinition $argument, $currentValue)
324  {
325  $type = $this->‪getArgumentType($argument);
326  $name = $argument->getName();
327  $fieldName = 'tx_scheduler[task_extbase][arguments][' . htmlspecialchars($name) . ']';
328  if (‪TypeHandlingUtility::normalizeType($type) === 'boolean') {
329  // checkbox field for boolean values.
330  $html = '<input type="hidden" name="' . $fieldName . '" value="0">';
331  $html .= '<div class="checkbox"><label><input type="checkbox" name="' . $fieldName . '" value="1" ' . ((bool)$currentValue ? ' checked="checked"' : '') . '></label></div>';
332  } else {
333  // regular string, also the default field type
334  $html = '<input class="form-control" type="text" name="' . $fieldName . '" value="' . htmlspecialchars($currentValue) . '"> ';
335  }
336  return $html;
337  }
338 }
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\renderSelectField
‪string renderSelectField($name, array $options, $selectedOptionValue)
Definition: FieldProvider.php:299
‪TYPO3\CMS\Extbase\Scheduler
Definition: FieldProvider.php:2
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\getAdditionalFields
‪array getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
Definition: FieldProvider.php:67
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Scheduler\Task\setCommandIdentifier
‪setCommandIdentifier($commandIdentifier)
Definition: Task.php:113
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\getLanguageLabel
‪string getLanguageLabel($localLanguageKey, $extensionName=null)
Definition: FieldProvider.php:193
‪TYPO3\CMS\Extbase\Scheduler\Task\setArguments
‪setArguments($arguments)
Definition: Task.php:129
‪TYPO3
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\$commandManager
‪TYPO3 CMS Extbase Mvc Cli CommandManager $commandManager
Definition: FieldProvider.php:30
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: FieldProvider.php:34
‪$fields
‪$fields
Definition: pages.php:4
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\renderField
‪string renderField(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument, $currentValue)
Definition: FieldProvider.php:319
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\getDefaultArgumentValue
‪mixed getDefaultArgumentValue(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
Definition: FieldProvider.php:259
‪TYPO3\CMS\Scheduler\Task\AbstractTask\setScheduler
‪setScheduler()
Definition: AbstractTask.php:278
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:32
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility
Definition: TypeHandlingUtility.php:19
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\getCommandControllerActionField
‪array getCommandControllerActionField()
Definition: FieldProvider.php:130
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\$reflectionService
‪TYPO3 CMS Extbase Reflection ReflectionService $reflectionService
Definition: FieldProvider.php:38
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate($key, $extensionName=null, $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:63
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\getActionLabel
‪string getActionLabel()
Definition: FieldProvider.php:281
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\$task
‪TYPO3 CMS Extbase Scheduler Task $task
Definition: FieldProvider.php:42
‪TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
Definition: SchedulerModuleController.php:53
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
Definition: CommandManager.php:23
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\getCommandControllerActionDescriptionField
‪array getCommandControllerActionDescriptionField()
Definition: FieldProvider.php:116
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\normalizeType
‪static string normalizeType($type)
Definition: TypeHandlingUtility.php:70
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\__construct
‪__construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager=null, \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager=null, \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService=null)
Definition: FieldProvider.php:51
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\getArgumentLabel
‪string getArgumentLabel(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
Definition: FieldProvider.php:232
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\saveAdditionalFields
‪bool saveAdditionalFields(array $submittedData, AbstractTask $task)
Definition: FieldProvider.php:104
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider
Definition: FieldProvider.php:27
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\getCommandControllerActionArgumentFields
‪array getCommandControllerActionArgumentFields(array $argumentDefinitions)
Definition: FieldProvider.php:168
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\getArgumentType
‪string getArgumentType(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
Definition: FieldProvider.php:208
‪TYPO3\CMS\Extbase\Scheduler\FieldProvider\validateAdditionalFields
‪bool validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
Definition: FieldProvider.php:92
‪TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface
Definition: AdditionalFieldProviderInterface.php:21