TYPO3 CMS  TYPO3_6-2
FieldProvider.php
Go to the documentation of this file.
1 <?php
3 
20 
24  protected $commandManager;
25 
29  protected $objectManager;
30 
34  protected $reflectionService;
35 
39  protected $task;
40 
48  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) {
49  $this->objectManager = $objectManager !== NULL ? $objectManager : \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
50  $this->commandManager = $commandManager !== NULL ? $commandManager : $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandManager');
51  $this->reflectionService = $reflectionService !== NULL ? $reflectionService : $this->objectManager->get('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService');
52  }
53 
63  public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule) {
64  $this->task = $task;
65  if ($this->task !== NULL) {
66  $this->task->setScheduler();
67  }
68  $fields = array();
69  $fields['action'] = $this->getCommandControllerActionField();
70  if ($this->task !== NULL && $this->task->getCommandIdentifier()) {
71  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
72  $fields['description'] = $this->getCommandControllerActionDescriptionField();
73  $argumentFields = $this->getCommandControllerActionArgumentFields($command->getArgumentDefinitions());
74  $fields = array_merge($fields, $argumentFields);
75  $this->task->save();
76  }
77  return $fields;
78  }
79 
87  public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule) {
88  return TRUE;
89  }
90 
98  public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
99  $task->setCommandIdentifier($submittedData['task_extbase']['action']);
100  $task->setArguments((array) $submittedData['task_extbase']['arguments']);
101  return TRUE;
102  }
103 
110  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
111  return array(
112  'code' => '',
113  'label' => '<strong>' . $command->getDescription() . '</strong>'
114  );
115  }
116 
122  protected function getCommandControllerActionField() {
123  $commands = $this->commandManager->getAvailableCommands();
124  $options = array();
125  foreach ($commands as $command) {
126  if ($command->isInternal() === FALSE) {
127  $className = $command->getControllerClassName();
128  if (strpos($className, '\\')) {
129  $classNameParts = explode('\\', $className);
130  // Skip vendor and product name for core classes
131  if (strpos($className, 'TYPO3\\CMS\\') === 0) {
132  $classPartsToSkip = 2;
133  } else {
134  $classPartsToSkip = 1;
135  }
136  $classNameParts = array_slice($classNameParts, $classPartsToSkip);
137  $extensionName = $classNameParts[0];
138  $controllerName = $classNameParts[2];
139  } else {
140  $classNameParts = explode('_', $className);
141  $extensionName = $classNameParts[1];
142  $controllerName = $classNameParts[3];
143  }
144  $identifier = $command->getCommandIdentifier();
145  $options[$identifier] = $extensionName . ' ' . str_replace('CommandController', '', $controllerName) . ': ' . $command->getControllerCommandName();
146  }
147  }
148  $name = 'action';
149  $currentlySelectedCommand = $this->task !== NULL ? $this->task->getCommandIdentifier() : NULL;
150  return array(
151  'code' => $this->renderSelectField($name, $options, $currentlySelectedCommand),
152  'label' => $this->getActionLabel()
153  );
154  }
155 
164  protected function getCommandControllerActionArgumentFields(array $argumentDefinitions) {
165  $fields = array();
166  $argumentValues = $this->task->getArguments();
167  foreach ($argumentDefinitions as $argument) {
168  $name = $argument->getName();
169  $defaultValue = $this->getDefaultArgumentValue($argument);
170  $this->task->addDefaultValue($name, $defaultValue);
171  $value = isset($argumentValues[$name]) ? $argumentValues[$name] : $defaultValue;
172  $fields[$name] = array(
173  'code' => $this->renderField($argument, $value),
174  'label' => $this->getArgumentLabel($argument)
175  );
176  }
177  return $fields;
178  }
179 
188  protected function getLanguageLabel($localLanguageKey, $extensionName = NULL) {
189  if (!$extensionName) {
190  list($extensionName, $commandControllerName, $commandName) = explode(':', $this->task->getCommandIdentifier());
191  }
192  $label = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($localLanguageKey, $extensionName);
193  return $label;
194  }
195 
202  protected function getArgumentType(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument) {
203  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
204  $controllerClassName = $command->getControllerClassName();
205  $methodName = $command->getControllerCommandName() . 'Command';
206  $tags = $this->reflectionService->getMethodTagsValues($controllerClassName, $methodName);
207  foreach ($tags['param'] as $tag) {
208  list($argumentType, $argumentVariableName) = explode(' ', $tag);
209  if (substr($argumentVariableName, 1) === $argument->getName()) {
210  return $argumentType;
211  }
212  }
213  return '';
214  }
215 
222  protected function getArgumentLabel(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument) {
223  $argumentName = $argument->getName();
224  list($extensionName, $commandControllerName, $commandName) = explode(':', $this->task->getCommandIdentifier());
225  $path = array('command', $commandControllerName, $commandName, 'arguments', $argumentName);
226  $labelNameIndex = implode('.', $path);
227  $label = $this->getLanguageLabel($labelNameIndex);
228  if (!$label) {
229  $label = 'Argument: ' . $argumentName;
230  }
231  $descriptionIndex = $labelNameIndex . '.description';
232  $description = $this->getLanguageLabel($descriptionIndex);
233  if (strlen($description) === 0) {
234  $description = $argument->getDescription();
235  }
236  if (strlen($description) > 0) {
237  $label .= '. <em>' . htmlspecialchars($description) . '</em>';
238  }
239  return $label;
240  }
241 
248  protected function getDefaultArgumentValue(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument) {
249  $type = $this->getArgumentType($argument);
250  $argumentName = $argument->getName();
251  $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
252  $argumentReflection = $this->reflectionService->getMethodParameters($command->getControllerClassName(), $command->getControllerCommandName() . 'Command');
253  $defaultValue = $argumentReflection[$argumentName]['defaultValue'];
254  if ($type === 'boolean') {
255  $defaultValue = (boolean) $defaultValue ? 1 : 0;
256  }
257  return $defaultValue;
258  }
259 
265  protected function getActionLabel() {
266  $index = 'task.action';
267  $label = $this->getLanguageLabel($index, 'extbase');
268  if (!$label) {
269  $label = 'CommandController Command. <em>Save and reopen to define command arguments</em>';
270  }
271  return $label;
272  }
273 
282  protected function renderSelectField($name, array $options, $selectedOptionValue) {
283  $html = array(
284  '<select name="tx_scheduler[task_extbase][' . htmlspecialchars($name) . ']">'
285  );
286  foreach ($options as $optionValue => $optionLabel) {
287  $selected = $optionValue === $selectedOptionValue ? ' selected="selected"' : '';
288  array_push($html, '<option title="test" value="' . htmlspecialchars($optionValue) . '"' . $selected . '>' . htmlspecialchars($optionLabel) . '</option>');
289  }
290  array_push($html, '</select>');
291  return implode(LF, $html);
292  }
293 
301  protected function renderField(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument, $currentValue) {
302  $type = $this->getArgumentType($argument);
303  $name = $argument->getName();
304  $fieldName = 'tx_scheduler[task_extbase][arguments][' . htmlspecialchars($name) . ']';
305  if ($type === 'boolean') {
306  // checkbox field for boolean values.
307  $html = '<input type="hidden" name="' . $fieldName . '" value="0" />';
308  $html .= '<input type="checkbox" name="' . $fieldName . '" value="1" ' . ((boolean) $currentValue ? ' checked="checked"' : '') . '/>';
309  } else {
310  // regular string, also the default field type
311  $html = '<input type="text" name="' . $fieldName . '" value="' . htmlspecialchars($currentValue) . '" /> ';
312  }
313  return $html;
314  }
315 }
$controllerClassName
Definition: Install.php:108
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, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule)
__construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager=NULL, \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager=NULL, \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService=NULL)
renderField(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument, $currentValue)
getCommandControllerActionArgumentFields(array $argumentDefinitions)
renderSelectField($name, array $options, $selectedOptionValue)
getLanguageLabel($localLanguageKey, $extensionName=NULL)
static translate($key, $extensionName, $arguments=NULL)
getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule)