TYPO3  7.6
FieldProvider.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Scheduler;
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 {
25  protected $commandManager;
26 
30  protected $objectManager;
31 
35  protected $reflectionService;
36 
40  protected $task;
41 
49  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)
50  {
51  $this->objectManager = $objectManager !== null ? $objectManager : \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
52  $this->commandManager = $commandManager !== null ? $commandManager : $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager::class);
53  $this->reflectionService = $reflectionService !== null ? $reflectionService : $this->objectManager->get(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
54  }
55 
65  public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule)
66  {
67  $this->task = $task;
68  if ($this->task !== null) {
69  $this->task->setScheduler();
70  }
71  $fields = array();
72  $fields['action'] = $this->getCommandControllerActionField();
73  if ($this->task !== null && $this->task->getCommandIdentifier()) {
74  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
75  $fields['description'] = $this->getCommandControllerActionDescriptionField();
76  $argumentFields = $this->getCommandControllerActionArgumentFields($command->getArgumentDefinitions());
77  $fields = array_merge($fields, $argumentFields);
78  $this->task->save();
79  }
80  return $fields;
81  }
82 
90  public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule)
91  {
92  return true;
93  }
94 
102  public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
103  {
104  $task->setCommandIdentifier($submittedData['task_extbase']['action']);
105  $task->setArguments((array)$submittedData['task_extbase']['arguments']);
106  return true;
107  }
108 
115  {
116  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
117  return array(
118  'code' => '',
119  'label' => '<strong>' . $command->getDescription() . '</strong>'
120  );
121  }
122 
128  protected function getCommandControllerActionField()
129  {
130  $commands = $this->commandManager->getAvailableCommands();
131  $options = array();
132  foreach ($commands as $command) {
133  if ($command->isInternal() === true || $command->isCliOnly() === true) {
134  continue;
135  }
136  $className = $command->getControllerClassName();
137  if (strpos($className, '\\')) {
138  $classNameParts = explode('\\', $className);
139  // Skip vendor and product name for core classes
140  if (strpos($className, 'TYPO3\\CMS\\') === 0) {
141  $classPartsToSkip = 2;
142  } else {
143  $classPartsToSkip = 1;
144  }
145  $classNameParts = array_slice($classNameParts, $classPartsToSkip);
146  $extensionName = $classNameParts[0];
147  $controllerName = $classNameParts[2];
148  } else {
149  $classNameParts = explode('_', $className);
150  $extensionName = $classNameParts[1];
151  $controllerName = $classNameParts[3];
152  }
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 array(
159  'code' => $this->renderSelectField($name, $options, $currentlySelectedCommand),
160  'label' => $this->getActionLabel()
161  );
162  }
163 
172  protected function getCommandControllerActionArgumentFields(array $argumentDefinitions)
173  {
174  $fields = array();
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 = isset($argumentValues[$name]) ? $argumentValues[$name] : $defaultValue;
181  $fields[$name] = array(
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  $tags = $this->reflectionService->getMethodTagsValues($controllerClassName, $methodName);
218  foreach ($tags['param'] as $tag) {
219  list($argumentType, $argumentVariableName) = explode(' ', $tag);
220  if (substr($argumentVariableName, 1) === $argument->getName()) {
221  return $argumentType;
222  }
223  }
224  return '';
225  }
226 
233  protected function getArgumentLabel(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
234  {
235  $argumentName = $argument->getName();
236  list($extensionName, $commandControllerName, $commandName) = explode(':', $this->task->getCommandIdentifier());
237  $path = array('command', $commandControllerName, $commandName, 'arguments', $argumentName);
238  $labelNameIndex = implode('.', $path);
239  $label = $this->getLanguageLabel($labelNameIndex);
240  if (!$label) {
241  $label = 'Argument: ' . $argumentName;
242  }
243  $descriptionIndex = $labelNameIndex . '.description';
244  $description = $this->getLanguageLabel($descriptionIndex);
245  if ((string)$description === '') {
246  $description = $argument->getDescription();
247  }
248  if ((string)$description !== '') {
249  $label .= '. <em>' . htmlspecialchars($description) . '</em>';
250  }
251  return $label;
252  }
253 
260  protected function getDefaultArgumentValue(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument)
261  {
262  $type = $this->getArgumentType($argument);
263  $argumentName = $argument->getName();
264  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
265  $argumentReflection = $this->reflectionService->getMethodParameters($command->getControllerClassName(), $command->getControllerCommandName() . 'Command');
266  $defaultValue = $argumentReflection[$argumentName]['defaultValue'];
267  if ($type === 'boolean') {
268  $defaultValue = (bool)$defaultValue ? 1 : 0;
269  }
270  return $defaultValue;
271  }
272 
278  protected function getActionLabel()
279  {
280  $index = 'task.action';
281  $label = $this->getLanguageLabel($index, 'extbase');
282  if (!$label) {
283  $label = 'CommandController Command. <em>Save and reopen to define command arguments</em>';
284  }
285  return $label;
286  }
287 
296  protected function renderSelectField($name, array $options, $selectedOptionValue)
297  {
298  $html = array(
299  '<select class="form-control" name="tx_scheduler[task_extbase][' . htmlspecialchars($name) . ']">'
300  );
301  foreach ($options as $optionValue => $optionLabel) {
302  $selected = $optionValue === $selectedOptionValue ? ' selected="selected"' : '';
303  array_push($html, '<option title="test" value="' . htmlspecialchars($optionValue) . '"' . $selected . '>' . htmlspecialchars($optionLabel) . '</option>');
304  }
305  array_push($html, '</select>');
306  return implode(LF, $html);
307  }
308 
316  protected function renderField(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument, $currentValue)
317  {
318  $type = $this->getArgumentType($argument);
319  $name = $argument->getName();
320  $fieldName = 'tx_scheduler[task_extbase][arguments][' . htmlspecialchars($name) . ']';
321  if ($type === 'boolean') {
322  // checkbox field for boolean values.
323  $html = '<input type="hidden" name="' . $fieldName . '" value="0">';
324  $html .= '<div class="checkbox"><label><input type="checkbox" name="' . $fieldName . '" value="1" ' . ((bool)$currentValue ? ' checked="checked"' : '') . '></label></div>';
325  } else {
326  // regular string, also the default field type
327  $html = '<input class="form-control" type="text" name="' . $fieldName . '" value="' . htmlspecialchars($currentValue) . '"> ';
328  }
329  return $html;
330  }
331 }