[TASK] Re-work/simplify copyright header in PHP files - Part 8
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Scheduler / FieldProvider.php
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 /**
17 * Field provider for Extbase CommandController Scheduler task
18 */
19 class FieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {
20
21 /**
22 * @var \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
23 */
24 protected $commandManager;
25
26 /**
27 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
28 */
29 protected $objectManager;
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
33 */
34 protected $reflectionService;
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Scheduler\Task
38 */
39 protected $task;
40
41 /**
42 * Constructor
43 *
44 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
45 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager
46 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
47 */
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
54 /**
55 * Render additional information fields within the scheduler backend.
56 *
57 * @param array &$taskInfo Array information of task to return
58 * @param mixed $task \TYPO3\CMS\Scheduler\Task\AbstractTask or \TYPO3\CMS\Scheduler\Execution instance
59 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the calling object (BE module of the Scheduler)
60 * @return array Additional fields
61 * @see \TYPO3\CMS\Scheduler\AdditionalFieldProvider#getAdditionalFields($taskInfo, $task, $schedulerModule)
62 */
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
80 /**
81 * Validates additional selected fields
82 *
83 * @param array &$submittedData
84 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule
85 * @return boolean
86 */
87 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule) {
88 return TRUE;
89 }
90
91 /**
92 * Saves additional field values
93 *
94 * @param array $submittedData
95 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task
96 * @return boolean
97 */
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
104 /**
105 * Get description of selected command
106 *
107 * @return string
108 */
109 protected function getCommandControllerActionDescriptionField() {
110 $command = $this->commandManager->getCommandByIdentifier($this->task->getCommandIdentifier());
111 return array(
112 'code' => '',
113 'label' => '<strong>' . $command->getDescription() . '</strong>'
114 );
115 }
116
117 /**
118 * Gets a select field containing all possible CommandController actions
119 *
120 * @return array
121 */
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
156 /**
157 * Gets a set of fields covering arguments which must be sent to $currentControllerAction.
158 * Also registers the default values of those fields with the Task, allowing
159 * them to be read upon execution.
160 *
161 * @param array $argumentDefinitions
162 * @return array
163 */
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
180 /**
181 * Gets a label for $key based on either provided extension or currently
182 * selected CommandController extension,ยด
183 *
184 * @param string $localLanguageKey
185 * @param string $extensionName
186 * @return string
187 */
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
196 /**
197 * Gets the data type required for the argument value
198 *
199 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument
200 * @return string the argument type
201 */
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
216 /**
217 * Get a human-readable label for a command argument
218 *
219 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument
220 * @return string
221 */
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
242 /**
243 * Gets the default value of argument
244 *
245 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument
246 * @return mixed
247 */
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
260 /**
261 * Get a human-readable label for the action field
262 *
263 * @return string
264 */
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
274 /**
275 * Render a select field with name $name and options $options
276 *
277 * @param string $name
278 * @param array $options
279 * @param string $selectedOptionValue
280 * @return string
281 */
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
294 /**
295 * Renders a field for defining an argument's value
296 *
297 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition $argument
298 * @param mixed $currentValue
299 * @return string
300 */
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 }