2 /***************************************************************
5 * (c) 2011 Claus Due, Wildside A/S <claus@wildside.dk>
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * Field provider for Extbase CommandController Scheduler task
29 * @subpackage Scheduler
31 class Tx_Extbase_Scheduler_FieldProvider
implements Tx_Scheduler_AdditionalFieldProvider
{
34 * @var Tx_Extbase_MVC_CLI_CommandManager
36 protected $commandManager;
39 * @var Tx_Extbase_Object_ObjectManagerInterface
41 protected $objectManager;
44 * @var Tx_Extbase_Reflection_Service
46 protected $reflectionService;
49 * @var Tx_Extbase_Scheduler_Task
56 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
57 * @param Tx_Extbase_MVC_CLI_CommandManager $commandManager
58 * @param Tx_Extbase_Reflection_Service $reflectionService
60 public function __construct(Tx_Extbase_Object_ObjectManagerInterface
$objectManager = NULL, Tx_Extbase_MVC_CLI_CommandManager
$commandManager = NULL, Tx_Extbase_Reflection_Service
$reflectionService = NULL) {
61 $this->objectManager
= $objectManager !== NULL ?
$objectManager : t3lib_div
::makeInstance('Tx_Extbase_Object_ObjectManager');
62 $this->commandManager
= $commandManager !== NULL ?
$commandManager : $this->objectManager
->get('Tx_Extbase_MVC_CLI_CommandManager');
63 $this->reflectionService
= $reflectionService !== NULL ?
$reflectionService : $this->objectManager
->get('Tx_Extbase_Reflection_Service');
67 * Render additional information fields within the scheduler backend.
69 * @param array $taskInfo Array information of task to return
70 * @param mixed $task Tx_Extbase_Scheduler_Task or tx_scheduler_Execution instance
71 * @param Tx_Scheduler_Module $schedulerModule Reference to the calling object (BE module of the Scheduler)
72 * @return array Additional fields
73 * @see interfaces/tx_scheduler_AdditionalFieldProvider#getAdditionalFields($taskInfo, $task, $schedulerModule)
75 public function getAdditionalFields(array &$taskInfo, $task, Tx_Scheduler_Module
$schedulerModule) {
77 if ($this->task
!== NULL) {
78 $this->task
->setScheduler();
81 $fields['action'] = $this->getCommandControllerActionField();
82 if ($this->task
!== NULL && $this->task
->getCommandIdentifier()) {
83 $command = $this->commandManager
->getCommandByIdentifier($this->task
->getCommandIdentifier());
84 $fields['description'] = $this->getCommandControllerActionDescriptionField();
85 $argumentFields = $this->getCommandControllerActionArgumentFields($command->getArgumentDefinitions());
86 $fields = array_merge($fields, $argumentFields);
93 * Validates additional selected fields
95 * @param array $submittedData
96 * @param Tx_Scheduler_Module $schedulerModule
99 public function validateAdditionalFields(array &$submittedData, Tx_Scheduler_Module
$schedulerModule) {
104 * Saves additional field values
106 * @param array $submittedData
107 * @param Tx_Scheduler_Task $task
110 public function saveAdditionalFields(array $submittedData, Tx_Scheduler_Task
$task) {
111 $task->setCommandIdentifier($submittedData['task_extbase']['action']);
112 $task->setArguments($submittedData['task_extbase']['arguments']);
117 * Get description of selected command
121 protected function getCommandControllerActionDescriptionField() {
122 $command = $this->commandManager
->getCommandByIdentifier($this->task
->getCommandIdentifier());
125 'label' => '<strong>' . $command->getDescription() . '</strong>'
130 * Gets a select field containing all possible CommandController actions
134 protected function getCommandControllerActionField() {
135 $commands = $this->commandManager
->getAvailableCommands();
137 foreach ($commands as $command) {
138 if ($command->isInternal() === FALSE) {
139 $classNameParts = explode('_', $command->getControllerClassName());
140 $identifier = $command->getCommandIdentifier();
141 $options[$identifier] = $classNameParts[1] . ' ' . str_replace('CommandController', '', $classNameParts[3]) . ': ' . $command->getControllerCommandName();
145 $currentlySelectedCommand = $this->task
!== NULL ?
$this->task
->getCommandIdentifier() : NULL;
147 'code' => $this->renderSelectField($name, $options, $currentlySelectedCommand),
148 'label' => $this->getActionLabel()
153 * Gets a set of fields covering arguments which must be sent to $currentControllerAction.
154 * Also registers the default values of those fields with the Task, allowing
155 * them to be read upon execution.
157 * @param array $argumentDefinitions
160 protected function getCommandControllerActionArgumentFields(array $argumentDefinitions) {
162 $argumentValues = $this->task
->getArguments();
163 foreach ($argumentDefinitions as $index=>$argument) {
164 $name = $argument->getName();
165 $defaultValue = $this->getDefaultArgumentValue($argument);
166 $this->task
->addDefaultValue($name, $defaultValue);
167 $value = isset($argumentValues[$name]) ?
$argumentValues[$name] : $defaultValue;
168 $fields[$name] = array(
169 'code' => $this->renderField($argument, $value),
170 'label' => $this->getArgumentLabel($argument)
177 * Gets a label for $key based on either provided extension or currently
178 * selected CommandController extension,ยด
180 * @param string $localLanguageKey
181 * @param string $extensionName
184 protected function getLanguageLabel($localLanguageKey, $extensionName = NULL) {
185 if (!$extensionName) {
186 list ($extensionName, $commandControllerName, $commandName) = explode(':', $this->task
->getCommandIdentifier());
188 $label = Tx_Extbase_Utility_Localization
::translate($localLanguageKey, $extensionName);
193 * Gets the data type required for the argument value
195 * @param Tx_Extbase_MVC_CLI_CommandArgumentDefinition $argument
196 * @return string the argument type
198 protected function getArgumentType(Tx_Extbase_MVC_CLI_CommandArgumentDefinition
$argument) {
199 $command = $this->commandManager
->getCommandByIdentifier($this->task
->getCommandIdentifier());
200 $controllerClassName = $command->getControllerClassName();
201 $methodName = $command->getControllerCommandName() . 'Command';
202 $tags = $this->reflectionService
->getMethodTagsValues($controllerClassName, $methodName);
203 foreach ($tags['param'] as $tag) {
204 list ($argumentType, $argumentVariableName) = explode(' ', $tag);
205 if (substr($argumentVariableName, 1) === $argument->getName()) {
206 return $argumentType;
212 * Get a human-readable label for a command argument
214 * @param Tx_Extbase_MVC_CLI_CommandArgumentDefinition $argument
217 protected function getArgumentLabel(Tx_Extbase_MVC_CLI_CommandArgumentDefinition
$argument) {
218 $argumentName = $argument->getName();
219 list ($extensionName, $commandControllerName, $commandName) = explode(':', $this->task
->getCommandIdentifier());
220 $path = array('command', $commandControllerName, $commandName, 'arguments', $argumentName);
221 $labelNameIndex = implode('.', $path);
222 $label = $this->getLanguageLabel($labelNameIndex);
224 $label = 'Argument: ' . $argumentName;
226 $descriptionIndex = $labelNameIndex . '.description';
227 $description = $this->getLanguageLabel($descriptionIndex);
228 if (strlen($description) === 0) {
229 $description = $argument->getDescription();
231 if (strlen($description) > 0) {
232 $label .= '. <em>' . htmlspecialchars($description) . '</em>';
238 * Gets the default value of argument
240 * @param Tx_Extbase_MVC_CLI_CommandArgumentDefinition $argument
243 protected function getDefaultArgumentValue(Tx_Extbase_MVC_CLI_CommandArgumentDefinition
$argument) {
244 $type = $this->getArgumentType($argument);
245 $argumentName = $argument->getName();
246 $command = $this->commandManager
->getCommandByIdentifier($this->task
->getCommandIdentifier());
247 $argumentReflection = $this->reflectionService
->getMethodParameters($command->getControllerClassName(), $command->getControllerCommandName() . 'Command');
248 $defaultValue = $argumentReflection[$argumentName]['defaultValue'];
249 if ($type === 'boolean') {
250 $defaultValue = ((bool) $defaultValue) ?
1 : 0;
252 return $defaultValue;
256 * Get a human-readable label for the action field
260 protected function getActionLabel() {
261 $index = 'task.action';
262 $label = $this->getLanguageLabel($index, 'extbase');
264 $label = 'CommandController Command. <em>Save and reopen to define command arguments</em>';
270 * Render a select field with name $name and options $options
272 * @param string $name
273 * @param array $options
274 * @param string $selectedOptionValue
277 protected function renderSelectField($name, array $options, $selectedOptionValue) {
279 '<select name="tx_scheduler[task_extbase][' . htmlspecialchars($name) . ']">'
281 foreach ($options as $optionValue=>$optionLabel) {
282 $selected = $optionValue === $selectedOptionValue ?
' selected="selected"' : '';
283 array_push($html, '<option title="test" value="' . htmlspecialchars($optionValue) . '"' . $selected . '>' . htmlspecialchars($optionLabel) . '</option>');
285 array_push($html, '</select>');
286 return implode(LF
, $html);
290 * Renders a field for defining an argument's value
292 * @param Tx_Extbase_MVC_CLI_CommandArgumentDefinition $argument
293 * @param mixed $currentValue
296 protected function renderField(Tx_Extbase_MVC_CLI_CommandArgumentDefinition
$argument, $currentValue) {
297 $type = $this->getArgumentType($argument);
298 $name = $argument->getName();
299 $fieldName = 'tx_scheduler[task_extbase][arguments][' . htmlspecialchars($name) . ']';
300 if ($type === 'boolean') {
301 // checkbox field for boolean values.
302 $html = '<input type="hidden" name="' . $fieldName . '" value="0" />';
303 $html .= '<input type="checkbox" name="' . $fieldName . '" value="1" ' . ((boolean
)$currentValue ?
' checked="checked"' : '') . '/>';
305 // regular string, also the default field type
306 $html = '<input type="text" name="' . $fieldName . '" value="' . htmlspecialchars($currentValue) . '" /> ';