[BUGFIX] Make SingleTasks toggleable from scheduler list view 90/47190/2
authorAndreas Fernandez <a.fernandez@scripting-base.de>
Fri, 19 Feb 2016 11:18:33 +0000 (12:18 +0100)
committerFrank Naegler <frank.naegler@typo3.org>
Fri, 11 Mar 2016 09:10:01 +0000 (10:10 +0100)
The toggle button for tasks does not work for single tasks. This patch
enables the functionality and registers a single task for execution on
next scheduler run when enabled.

Additionally class constants are introduced for the two available types.

Change-Id: I91c7b24bf141ef6735b3fd38e2859d63634d507e
Resolves: #64708
Releases: master, 7.6
Reviewed-on: https://review.typo3.org/47190
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/scheduler/Classes/Controller/SchedulerModuleController.php
typo3/sysext/scheduler/Classes/Task/AbstractTask.php

index f48ec36..dccdb0f 100644 (file)
@@ -586,6 +586,11 @@ class SchedulerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClas
     {
         $task = $this->scheduler->fetchTask($this->submittedData['uid']);
         $task->setDisabled(!$task->isDisabled());
+        // If a disabled single task is enabled again, we register it for a
+        // single execution at next scheduler run.
+        if ($task->getType() === AbstractTask::TYPE_SINGLE) {
+            $task->registerSingleExecution(time());
+        }
         $task->save();
     }
 
@@ -637,13 +642,12 @@ class SchedulerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClas
                     if (!empty($taskInfo['interval']) || !empty($taskInfo['croncmd'])) {
                         // Guess task type from the existing information
                         // If an interval or a cron command is defined, it's a recurring task
-                        // @todo remove magic numbers for the type, use class constants instead
-                        $taskInfo['type'] = 2;
+                        $taskInfo['type'] = AbstractTask::TYPE_RECURRING;
                         $taskInfo['frequency'] = $taskInfo['interval'] ?: $taskInfo['croncmd'];
                     } else {
                         // It's not a recurring task
                         // Make sure interval and cron command are both empty
-                        $taskInfo['type'] = 1;
+                        $taskInfo['type'] = AbstractTask::TYPE_SINGLE;
                         $taskInfo['frequency'] = '';
                         $taskInfo['end'] = 0;
                     }
@@ -656,7 +660,7 @@ class SchedulerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClas
                     $taskInfo['end'] = 0;
                     $taskInfo['frequency'] = '';
                     $taskInfo['multiple'] = false;
-                    $taskInfo['type'] = 1;
+                    $taskInfo['type'] = AbstractTask::TYPE_SINGLE;
                 }
             } catch (\OutOfBoundsException $e) {
                 // Add a message and continue throwing the exception
@@ -666,7 +670,7 @@ class SchedulerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClas
         } else {
             // If adding a new object, set some default values
             $taskInfo['class'] = key($registeredClasses);
-            $taskInfo['type'] = 2;
+            $taskInfo['type'] = AbstractTask::TYPE_RECURRING;
             $taskInfo['start'] = $GLOBALS['EXEC_TIME'];
             $taskInfo['end'] = '';
             $taskInfo['frequency'] = '';
@@ -765,8 +769,8 @@ class SchedulerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClas
                 . BackendUtility::wrapInHelp($this->cshKey, 'task_type', $label)
                 . '<div class="form-control-wrap">'
                     . '<select name="tx_scheduler[type]" id="task_type" class="form-control">'
-                        . '<option value="1" ' . ((int)$taskInfo['type'] === 1 ? ' selected="selected"' : '') . '>' . $this->getLanguageService()->getLL('label.type.single') . '</option>'
-                        . '<option value="2" ' . ((int)$taskInfo['type'] === 2 ? ' selected="selected"' : '') . '>' . $this->getLanguageService()->getLL('label.type.recurring') . '</option>'
+                        . '<option value="1" ' . ((int)$taskInfo['type'] === AbstractTask::TYPE_SINGLE ? ' selected="selected"' : '') . '>' . $this->getLanguageService()->getLL('label.type.single') . '</option>'
+                        . '<option value="2" ' . ((int)$taskInfo['type'] === AbstractTask::TYPE_RECURRING ? ' selected="selected"' : '') . '>' . $this->getLanguageService()->getLL('label.type.recurring') . '</option>'
                     . '</select>'
                 . '</div>'
             . '</div></div>';
@@ -1113,7 +1117,7 @@ class SchedulerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClas
                             }
                         }
                         // Get execution type
-                        if ($task->getExecution()->getInterval() == 0 && $task->getExecution()->getCronCmd() == '') {
+                        if ($task->getType() === AbstractTask::TYPE_SINGLE) {
                             $execType = $this->getLanguageService()->getLL('label.type.single');
                             $frequency = '-';
                         } else {
@@ -1264,7 +1268,7 @@ class SchedulerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClas
                 return;
             }
             // Register single execution
-            if ((int)$this->submittedData['type'] === 1) {
+            if ((int)$this->submittedData['type'] === AbstractTask::TYPE_SINGLE) {
                 $task->registerSingleExecution($this->submittedData['start']);
             } else {
                 if (!empty($this->submittedData['croncmd'])) {
@@ -1306,7 +1310,7 @@ class SchedulerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClas
             // Create an instance of chosen class
             /** @var $task AbstractTask */
             $task = GeneralUtility::makeInstance($this->submittedData['class']);
-            if ((int)$this->submittedData['type'] === 1) {
+            if ((int)$this->submittedData['type'] === AbstractTask::TYPE_SINGLE) {
                 // Set up single execution
                 $task->registerSingleExecution($this->submittedData['start']);
             } else {
@@ -1374,7 +1378,7 @@ class SchedulerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClas
             }
         }
         // Check end date, if recurring task
-        if ($this->submittedData['type'] == 2 && !empty($this->submittedData['end'])) {
+        if ((int)$this->submittedData['type'] === AbstractTask::TYPE_RECURRING && !empty($this->submittedData['end'])) {
             try {
                 $this->submittedData['end'] = (int)$this->submittedData['end'];
                 if ($this->submittedData['end'] < $this->submittedData['start']) {
@@ -1390,7 +1394,7 @@ class SchedulerModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClas
         $this->submittedData['interval'] = 0;
         $this->submittedData['croncmd'] = '';
         // Check type and validity of frequency, if recurring
-        if ($this->submittedData['type'] == 2) {
+        if ((int)$this->submittedData['type'] === AbstractTask::TYPE_RECURRING) {
             $frequency = trim($this->submittedData['frequency']);
             if (empty($frequency)) {
                 // Empty frequency, not valid
index 8550a61..fc966c4 100644 (file)
@@ -21,6 +21,10 @@ namespace TYPO3\CMS\Scheduler\Task;
  */
 abstract class AbstractTask
 {
+
+    const TYPE_SINGLE = 1;
+    const TYPE_RECURRING = 2;
+
     /**
      * Reference to a scheduler object
      *
@@ -515,4 +519,18 @@ abstract class AbstractTask
     {
         $this->scheduler->removeTask($this);
     }
+
+    /**
+     * Guess task type from the existing information
+     * If an interval or a cron command is defined, it's a recurring task
+     *
+     * @return int
+     */
+    public function getType()
+    {
+        if (!empty($this->getExecution()->getInterval()) || !empty($this->getExecution()->getCronCmd())) {
+            return self::TYPE_RECURRING;
+        }
+        return self::TYPE_SINGLE;
+    }
 }