2 namespace TYPO3\CMS\Extbase\Scheduler
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility
;
18 use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface
;
19 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController
;
20 use TYPO3\CMS\Scheduler\Task\AbstractTask
;
23 * Field provider for Extbase CommandController Scheduler task
25 class FieldProvider
implements AdditionalFieldProviderInterface
28 * @var \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
30 protected $commandManager;
33 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
35 protected $objectManager;
38 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
40 protected $reflectionService;
43 * @var \TYPO3\CMS\Extbase\Scheduler\Task
50 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
51 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager
52 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
54 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 $this->objectManager
= $objectManager ?? \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance(\TYPO3\CMS\Extbase\
Object\ObjectManager
::class);
57 $this->commandManager
= $commandManager ??
$this->objectManager
->get(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
::class);
58 $this->reflectionService
= $reflectionService ??
$this->objectManager
->get(\TYPO3\CMS\Extbase\Reflection\ReflectionService
::class);
62 * Render additional information fields within the scheduler backend.
64 * @param array &$taskInfo Array information of task to return
65 * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
66 * @param SchedulerModuleController $schedulerModule Reference to the calling object (BE module of the Scheduler)
67 * @return array Additional fields
68 * @see \TYPO3\CMS\Scheduler\AdditionalFieldProvider#getAdditionalFields($taskInfo, $task, $schedulerModule)
70 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController
$schedulerModule)
73 if ($this->task
!== null
) {
74 $this->task
->setScheduler();
77 $fields['action'] = $this->getCommandControllerActionField();
78 if ($this->task
!== null
&& $this->task
->getCommandIdentifier()) {
79 $command = $this->commandManager
->getCommandByIdentifier($this->task
->getCommandIdentifier());
80 $fields['description'] = $this->getCommandControllerActionDescriptionField();
81 $argumentFields = $this->getCommandControllerActionArgumentFields($command->getArgumentDefinitions());
82 $fields = array_merge($fields, $argumentFields);
89 * Validates additional selected fields
91 * @param array &$submittedData
92 * @param SchedulerModuleController $schedulerModule
95 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController
$schedulerModule)
101 * Saves additional field values
103 * @param array $submittedData
104 * @param AbstractTask $task
107 public function saveAdditionalFields(array $submittedData, AbstractTask
$task)
109 $task->setCommandIdentifier($submittedData['task_extbase']['action']);
110 $task->setArguments((array)$submittedData['task_extbase']['arguments']);
115 * Get description of selected command
119 protected function getCommandControllerActionDescriptionField()
121 $command = $this->commandManager
->getCommandByIdentifier($this->task
->getCommandIdentifier());
124 'label' => '<strong>' . $command->getDescription() . '</strong>'
129 * Gets a select field containing all possible CommandController actions
133 protected function getCommandControllerActionField()
135 $commands = $this->commandManager
->getAvailableCommands();
137 foreach ($commands as $command) {
138 if ($command->isInternal() === true ||
$command->isCliOnly() === true
) {
141 $className = $command->getControllerClassName();
142 $classNameParts = explode('\\', $className);
143 // Skip vendor and product name for core classes
144 if (strpos($className, 'TYPO3\\CMS\\') === 0) {
145 $classPartsToSkip = 2;
147 $classPartsToSkip = 1;
149 $classNameParts = array_slice($classNameParts, $classPartsToSkip);
150 $extensionName = $classNameParts[0];
151 $controllerName = $classNameParts[2];
152 $identifier = $command->getCommandIdentifier();
153 $options[$identifier] = $extensionName . ' ' . str_replace('CommandController', '', $controllerName) . ': ' . $command->getControllerCommandName();
156 $currentlySelectedCommand = $this->task
!== null ?
$this->task
->getCommandIdentifier() : null
;
158 'code' => $this->renderSelectField($name, $options, $currentlySelectedCommand),
159 'label' => $this->getActionLabel()
164 * Gets a set of fields covering arguments which must be sent to $currentControllerAction.
165 * Also registers the default values of those fields with the Task, allowing
166 * them to be read upon execution.
168 * @param array $argumentDefinitions
171 protected function getCommandControllerActionArgumentFields(array $argumentDefinitions)
174 $argumentValues = $this->task
->getArguments();
175 foreach ($argumentDefinitions as $argument) {
176 $name = $argument->getName();
177 $defaultValue = $this->getDefaultArgumentValue($argument);
178 $this->task
->addDefaultValue($name, $defaultValue);
179 $value = $argumentValues[$name] ??
$defaultValue;
181 'code' => $this->renderField($argument, $value),
182 'label' => $this->getArgumentLabel($argument)
189 * Gets a label for $key based on either provided extension or currently
190 * selected CommandController extension,ยด
192 * @param string $localLanguageKey
193 * @param string $extensionName
196 protected function getLanguageLabel($localLanguageKey, $extensionName = null
)
198 if (!$extensionName) {
199 list($extensionName, $commandControllerName, $commandName) = explode(':', $this->task
->getCommandIdentifier());
201 $label = \TYPO3\CMS\Extbase\Utility\LocalizationUtility
::translate($localLanguageKey, $extensionName);
206 * Gets the data type required for the argument value
208 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument
209 * @return string the argument type
211 protected function getArgumentType(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition
$argument)
213 $command = $this->commandManager
->getCommandByIdentifier($this->task
->getCommandIdentifier());
214 $controllerClassName = $command->getControllerClassName();
215 $methodName = $command->getControllerCommandName() . 'Command';
217 $tags = $this->reflectionService
218 ->getClassSchema($controllerClassName)
219 ->getMethod($methodName)['tags']['param'] ??
[];
220 foreach ($tags as $tag) {
221 list($argumentType, $argumentVariableName) = explode(' ', $tag);
222 if (substr($argumentVariableName, 1) === $argument->getName()) {
223 return $argumentType;
230 * Get a human-readable label for a command argument
232 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument
235 protected function getArgumentLabel(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition
$argument)
237 $argumentName = $argument->getName();
238 list($extensionName, $commandControllerName, $commandName) = explode(':', $this->task
->getCommandIdentifier());
239 $path = ['command', $commandControllerName, $commandName, 'arguments', $argumentName];
240 $labelNameIndex = implode('.', $path);
241 $label = $this->getLanguageLabel($labelNameIndex);
243 $label = 'Argument: ' . $argumentName;
245 $descriptionIndex = $labelNameIndex . '.description';
246 $description = $this->getLanguageLabel($descriptionIndex);
247 if ((string)$description === '') {
248 $description = $argument->getDescription();
250 if ((string)$description !== '') {
251 $label .= '. <em>' . htmlspecialchars($description) . '</em>';
257 * Gets the default value of argument
259 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument
262 protected function getDefaultArgumentValue(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition
$argument)
264 $type = $this->getArgumentType($argument);
265 $argumentName = $argument->getName();
266 $command = $this->commandManager
->getCommandByIdentifier($this->task
->getCommandIdentifier());
268 $argumentReflection = $this->reflectionService
269 ->getClassSchema($command->getControllerClassName())
270 ->getMethod($command->getControllerCommandName() . 'Command')['params'] ??
[];
272 $defaultValue = $argumentReflection[$argumentName]['defaultValue'];
273 if (TypeHandlingUtility
::normalizeType($type) === 'boolean') {
274 $defaultValue = (bool
)$defaultValue ?
1 : 0;
276 return $defaultValue;
280 * Get a human-readable label for the action field
284 protected function getActionLabel()
286 $index = 'task.action';
287 $label = $this->getLanguageLabel($index, 'extbase');
289 $label = 'CommandController Command. <em>Save and reopen to define command arguments</em>';
295 * Render a select field with name $name and options $options
297 * @param string $name
298 * @param array $options
299 * @param string $selectedOptionValue
302 protected function renderSelectField($name, array $options, $selectedOptionValue)
305 '<select class="form-control" name="tx_scheduler[task_extbase][' . htmlspecialchars($name) . ']">'
307 foreach ($options as $optionValue => $optionLabel) {
308 $selected = $optionValue === $selectedOptionValue ?
' selected="selected"' : '';
309 $html[] = '<option title="test" value="' . htmlspecialchars($optionValue) . '"' . $selected . '>' . htmlspecialchars($optionLabel) . '</option>';
311 $html[] = '</select>';
312 return implode(LF
, $html);
316 * Renders a field for defining an argument's value
318 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument
319 * @param mixed $currentValue
322 protected function renderField(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition
$argument, $currentValue)
324 $type = $this->getArgumentType($argument);
325 $name = $argument->getName();
326 $fieldName = 'tx_scheduler[task_extbase][arguments][' . htmlspecialchars($name) . ']';
327 if (TypeHandlingUtility
::normalizeType($type) === 'boolean') {
328 // checkbox field for boolean values.
329 $html = '<input type="hidden" name="' . $fieldName . '" value="0">';
330 $html .= '<div class="checkbox"><label><input type="checkbox" name="' . $fieldName . '" value="1" ' . ((bool
)$currentValue ?
' checked="checked"' : '') . '></label></div>';
332 // regular string, also the default field type
333 $html = '<input class="form-control" type="text" name="' . $fieldName . '" value="' . htmlspecialchars($currentValue) . '"> ';