TYPO3 CMS  TYPO3_8-7
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 
20 
25 {
29  protected $commandManager;
30 
34  protected $objectManager;
35 
39  protected $reflectionService;
40 
44  protected $task;
45 
53  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)
54  {
55  $this->objectManager = $objectManager !== null ? $objectManager : \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
56  $this->commandManager = $commandManager !== null ? $commandManager : $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class);
57  $this->reflectionService = $reflectionService !== null ? $reflectionService : $this->objectManager->get(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
58  }
59 
69  public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
70  {
71  $this->task = $task;
72  if ($this->task !== null) {
73  $this->task->setScheduler();
74  }
75  $fields = [];
76  $fields['action'] = $this->getCommandControllerActionField();
77  if ($this->task !== null && $this->task->getCommandIdentifier()) {
78  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
79  $fields['description'] = $this->getCommandControllerActionDescriptionField();
80  $argumentFields = $this->getCommandControllerActionArgumentFields($command->getArgumentDefinitions());
81  $fields = array_merge($fields, $argumentFields);
82  $this->task->save();
83  }
84  return $fields;
85  }
86 
94  public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
95  {
96  return true;
97  }
98 
106  public function saveAdditionalFields(array $submittedData, AbstractTask $task)
107  {
108  $task->setCommandIdentifier($submittedData['task_extbase']['action']);
109  $task->setArguments((array)$submittedData['task_extbase']['arguments']);
110  return true;
111  }
112 
119  {
120  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
121  return [
122  'code' => '',
123  'label' => '<strong>' . $command->getDescription() . '</strong>'
124  ];
125  }
126 
132  protected function getCommandControllerActionField()
133  {
134  $commands = $this->commandManager->getAvailableCommands();
135  $options = [];
136  foreach ($commands as $command) {
137  if ($command->isInternal() === true || $command->isCliOnly() === true) {
138  continue;
139  }
140  $className = $command->getControllerClassName();
141  if (strpos($className, '\\')) {
142  $classNameParts = explode('\\', $className);
143  // Skip vendor and product name for core classes
144  if (strpos($className, 'TYPO3\\CMS\\') === 0) {
145  $classPartsToSkip = 2;
146  } else {
147  $classPartsToSkip = 1;
148  }
149  $classNameParts = array_slice($classNameParts, $classPartsToSkip);
150  $extensionName = $classNameParts[0];
151  $controllerName = $classNameParts[2];
152  } else {
153  $classNameParts = explode('_', $className);
154  $extensionName = $classNameParts[1];
155  $controllerName = $classNameParts[3];
156  }
157  $identifier = $command->getCommandIdentifier();
158  $options[$identifier] = $extensionName . ' ' . str_replace('CommandController', '', $controllerName) . ': ' . $command->getControllerCommandName();
159  }
160  $name = 'action';
161  $currentlySelectedCommand = $this->task !== null ? $this->task->getCommandIdentifier() : null;
162  return [
163  'code' => $this->renderSelectField($name, $options, $currentlySelectedCommand),
164  'label' => $this->getActionLabel()
165  ];
166  }
167 
176  protected function getCommandControllerActionArgumentFields(array $argumentDefinitions)
177  {
178  $fields = [];
179  $argumentValues = $this->task->getArguments();
180  foreach ($argumentDefinitions as $argument) {
181  $name = $argument->getName();
182  $defaultValue = $this->getDefaultArgumentValue($argument);
183  $this->task->addDefaultValue($name, $defaultValue);
184  $value = isset($argumentValues[$name]) ? $argumentValues[$name] : $defaultValue;
185  $fields[$name] = [
186  'code' => $this->renderField($argument, $value),
187  'label' => $this->getArgumentLabel($argument)
188  ];
189  }
190  return $fields;
191  }
192 
201  protected function getLanguageLabel($localLanguageKey, $extensionName = null)
202  {
203  if (!$extensionName) {
204  list($extensionName, $commandControllerName, $commandName) = explode(':', $this->task->getCommandIdentifier());
205  }
206  $label = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($localLanguageKey, $extensionName);
207  return $label;
208  }
209 
216  protected function getArgumentType(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
217  {
218  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
219  $controllerClassName = $command->getControllerClassName();
220  $methodName = $command->getControllerCommandName() . 'Command';
221  $tags = $this->reflectionService->getMethodTagsValues($controllerClassName, $methodName);
222  foreach ($tags['param'] as $tag) {
223  list($argumentType, $argumentVariableName) = explode(' ', $tag);
224  if (substr($argumentVariableName, 1) === $argument->getName()) {
225  return $argumentType;
226  }
227  }
228  return '';
229  }
230 
237  protected function getArgumentLabel(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
238  {
239  $argumentName = $argument->getName();
240  list($extensionName, $commandControllerName, $commandName) = explode(':', $this->task->getCommandIdentifier());
241  $path = ['command', $commandControllerName, $commandName, 'arguments', $argumentName];
242  $labelNameIndex = implode('.', $path);
243  $label = $this->getLanguageLabel($labelNameIndex);
244  if (!$label) {
245  $label = 'Argument: ' . $argumentName;
246  }
247  $descriptionIndex = $labelNameIndex . '.description';
248  $description = $this->getLanguageLabel($descriptionIndex);
249  if ((string)$description === '') {
250  $description = $argument->getDescription();
251  }
252  if ((string)$description !== '') {
253  $label .= '. <em>' . htmlspecialchars($description) . '</em>';
254  }
255  return $label;
256  }
257 
264  protected function getDefaultArgumentValue(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
265  {
266  $type = $this->getArgumentType($argument);
267  $argumentName = $argument->getName();
268  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
269  $argumentReflection = $this->reflectionService->getMethodParameters($command->getControllerClassName(), $command->getControllerCommandName() . 'Command');
270  $defaultValue = $argumentReflection[$argumentName]['defaultValue'];
271  if (TypeHandlingUtility::normalizeType($type) === 'boolean') {
272  $defaultValue = (bool)$defaultValue ? 1 : 0;
273  }
274  return $defaultValue;
275  }
276 
282  protected function getActionLabel()
283  {
284  $index = 'task.action';
285  $label = $this->getLanguageLabel($index, 'extbase');
286  if (!$label) {
287  $label = 'CommandController Command. <em>Save and reopen to define command arguments</em>';
288  }
289  return $label;
290  }
291 
300  protected function renderSelectField($name, array $options, $selectedOptionValue)
301  {
302  $html = [
303  '<select class="form-control" name="tx_scheduler[task_extbase][' . htmlspecialchars($name) . ']">'
304  ];
305  foreach ($options as $optionValue => $optionLabel) {
306  $selected = $optionValue === $selectedOptionValue ? ' selected="selected"' : '';
307  $html[] = '<option title="test" value="' . htmlspecialchars($optionValue) . '"' . $selected . '>' . htmlspecialchars($optionLabel) . '</option>';
308  }
309  $html[] = '</select>';
310  return implode(LF, $html);
311  }
312 
320  protected function renderField(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument, $currentValue)
321  {
322  $type = $this->getArgumentType($argument);
323  $name = $argument->getName();
324  $fieldName = 'tx_scheduler[task_extbase][arguments][' . htmlspecialchars($name) . ']';
325  if (TypeHandlingUtility::normalizeType($type) === 'boolean') {
326  // checkbox field for boolean values.
327  $html = '<input type="hidden" name="' . $fieldName . '" value="0">';
328  $html .= '<div class="checkbox"><label><input type="checkbox" name="' . $fieldName . '" value="1" ' . ((bool)$currentValue ? ' checked="checked"' : '') . '></label></div>';
329  } else {
330  // regular string, also the default field type
331  $html = '<input class="form-control" type="text" name="' . $fieldName . '" value="' . htmlspecialchars($currentValue) . '"> ';
332  }
333  return $html;
334  }
335 }
getArgumentType(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
getDefaultArgumentValue(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
getArgumentLabel(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
saveAdditionalFields(array $submittedData, AbstractTask $task)
static translate($key, $extensionName=null, $arguments=null)
renderField(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument, $currentValue)
static makeInstance($className,... $constructorArguments)
$fields
Definition: pages.php:4
__construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager=null, \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager=null, \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService=null)
getCommandControllerActionArgumentFields(array $argumentDefinitions)
getLanguageLabel($localLanguageKey, $extensionName=null)
renderSelectField($name, array $options, $selectedOptionValue)
getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)