/** * Constructor * * @api * @param string $cronCommand: The cron command can hold any combination documented as valid * expression in usual unix like crons like vixiecron. Special commands like @weekly, * ranges, steps and three letter month and weekday abbreviations are allowed. * @param integer $timestamp: optional start time, used in unit tests * @return void */ public function __construct($cronCommand, $timestamp = FALSE) { $cronCommand = tx_scheduler_CronCmd_Normalize::normalize($cronCommand); // Explode cron command to sections $this->cronCommandSections = t3lib_div::trimExplode(' ', $cronCommand); // Initialize the values with the starting time // This takes care that the calculated time is always in the future if ($timestamp === FALSE) { $timestamp = strtotime('+1 minute'); } else { $timestamp += 60; } $this->timestamp = $this->roundTimestamp($timestamp); }
/** * Processes the automate synchronization form * * @formHandler * @param array $formData Submitted form data * @return array Response */ public function saveSchedulerTask($formData) { // Exit early if the scheduler extension is not installed, as it is impossible to save tasks in such a case // Normally this should not happen, as the BE module should not offer the possibility to trigger such an action // without the Scheduler being available if ($this->scheduler === NULL) { $response = array('succes' => FALSE, 'errors' => array('scheduler' => $GLOBALS['LANG']->sL('LLL:EXT:external_import/Resources/Private/Language/locallang.xml:autosync_noscheduler'))); return $response; } // Initialize the response array $response = array('errors' => array()); $success = TRUE; // Handle the frequency // Frequency cannot be empty $interval = 0; $cronCommand = ''; if ($formData['frequency'] == '') { $success = FALSE; $response['errors']['scheduler'] = $GLOBALS['LANG']->sL('LLL:EXT:external_import/Resources/Private/Language/locallang.xml:error_invalid_data'); $response['errors']['frequency'] = $GLOBALS['LANG']->sL('LLL:EXT:external_import/Resources/Private/Language/locallang.xml:error_empty_frequency'); // Check validity of frequency. It must be either a number or a cron command } else { // Try interpreting the frequency as a cron command try { tx_scheduler_CronCmd_Normalize::normalize($formData['frequency']); $cronCommand = $formData['frequency']; } catch (Exception $e) { // Check if the frequency is a valid number // If yes, assume it is a frequency in seconds, and unset cron error code if (is_numeric($formData['frequency'])) { $interval = intval($formData['frequency']); } else { $success = FALSE; $response['errors']['scheduler'] = $GLOBALS['LANG']->sL('LLL:EXT:external_import/Resources/Private/Language/locallang.xml:error_invalid_data'); $response['errors']['frequency'] = sprintf($GLOBALS['LANG']->sL('LLL:EXT:external_import/Resources/Private/Language/locallang.xml:error_wrong_frequency'), $e->getMessage()); } } } // Handle the start date // If date is empty, use time() - 1 as default, in order to trigger calculation of // soonest possible next execution date if (empty($formData['start_date'])) { $startDate = time() - 1; // Otherwise interpret date using strotime() } else { $startDateString = $formData['start_date']; // If the time is not empty, add it to the string to interpret if (!empty($formData['start_time'])) { $startDateString .= ' ' . $formData['start_time']; } $startDate = strtotime($startDateString); // If the date is invalid, log an error if ($startDate === FALSE) { $success = FALSE; $response['errors']['scheduler'] = $GLOBALS['LANG']->sL('LLL:EXT:external_import/Resources/Private/Language/locallang.xml:error_invalid_data'); $response['errors']['start_date'] = $GLOBALS['LANG']->sL('LLL:EXT:external_import/Resources/Private/Language/locallang.xml:error_invalid_start_date'); } } // If successful so far, save the Scheduler task (create or update) if ($success) { $data = array('table' => $formData['table'], 'index' => $formData['index'], 'interval' => $interval, 'croncmd' => $cronCommand, 'start' => $startDate); if (empty($formData['uid'])) { try { $this->createTask($data); } catch (Exception $e) { $success = FALSE; $response['errors']['scheduler'] = $GLOBALS['LANG']->sL('LLL:EXT:external_import/Resources/Private/Language/locallang.xml:autosync_save_failed'); } } else { $data['uid'] = $formData['uid']; try { $this->updateTask($data); } catch (Exception $e) { $success = FALSE; $response['errors']['scheduler'] = $GLOBALS['LANG']->sL('LLL:EXT:external_import/Resources/Private/Language/locallang.xml:autosync_save_failed'); } } } $response['success'] = $success; return $response; }
/** * @test * @dataProvider invalidWeekdayDataProvider * @expectedException InvalidArgumentException */ public function normalizeWeekdayThrowsExceptionForInvalidWeekdayRepresentation($weekday) { tx_scheduler_CronCmd_Normalize::normalizeWeekday($weekday); }
/** * Checks the submitted data and performs some preprocessing on it * * @return boolean True if everything was ok, false otherwise */ protected function preprocessData() { $result = true; // Validate id $this->submittedData['uid'] = empty($this->submittedData['uid']) ? 0 : intval($this->submittedData['uid']); // Validate selected task class if (!class_exists($this->submittedData['class'])) { $this->addMessage($GLOBALS['LANG']->getLL('msg.noTaskClassFound'), t3lib_FlashMessage::ERROR); } // Check start date if (empty($this->submittedData['start'])) { $this->addMessage($GLOBALS['LANG']->getLL('msg.noStartDate'), t3lib_FlashMessage::ERROR); $result = false; } else { try { $timestamp = $this->checkDate($this->submittedData['start']); $this->submittedData['start'] = $timestamp; } catch (Exception $e) { $this->addMessage($GLOBALS['LANG']->getLL('msg.invalidStartDate'), t3lib_FlashMessage::ERROR); $result = false; } } // Check end date, if recurring task if ($this->submittedData['type'] == 2 && !empty($this->submittedData['end'])) { try { $timestamp = $this->checkDate($this->submittedData['end']); $this->submittedData['end'] = $timestamp; if ($this->submittedData['end'] < $this->submittedData['start']) { $this->addMessage($GLOBALS['LANG']->getLL('msg.endDateSmallerThanStartDate'), t3lib_FlashMessage::ERROR); $result = false; } } catch (Exception $e) { $this->addMessage($GLOBALS['LANG']->getLL('msg.invalidEndDate'), t3lib_FlashMessage::ERROR); $result = false; } } // Set default values for interval and cron command $this->submittedData['interval'] = 0; $this->submittedData['croncmd'] = ''; // Check type and validity of frequency, if recurring if ($this->submittedData['type'] == 2) { $frequency = trim($this->submittedData['frequency']); if (empty($frequency)) { // Empty frequency, not valid $this->addMessage($GLOBALS['LANG']->getLL('msg.noFrequency'), t3lib_FlashMessage::ERROR); $result = FALSE; } else { $cronErrorCode = 0; $cronErrorMessage = ''; // Try interpreting the cron command try { tx_scheduler_CronCmd_Normalize::normalize($frequency); $this->submittedData['croncmd'] = $frequency; } catch (Exception $e) { // Store the exception's result $cronErrorMessage = $e->getMessage(); $cronErrorCode = $e->getCode(); // Check if the frequency is a valid number // If yes, assume it is a frequency in seconds, and unset cron error code if (is_numeric($frequency)) { $this->submittedData['interval'] = intval($frequency); unset($cronErrorCode); } } // If there's a cron error code, issue validation error message if (!empty($cronErrorCode)) { $this->addMessage(sprintf($GLOBALS['LANG']->getLL('msg.frequencyError'), $cronErrorMessage, $cronErrorCode), t3lib_FlashMessage::ERROR); $result = FALSE; } } } // Validate additional input fields if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][$this->submittedData['class']]['additionalFields'])) { $providerObject = t3lib_div::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][$this->submittedData['class']]['additionalFields']); if ($providerObject instanceof tx_scheduler_AdditionalFieldProvider) { // The validate method will return true if all went well, but that must not // override previous false values => AND the returned value with the existing one $result &= $providerObject->validateAdditionalFields($this->submittedData, $this); } } return $result; }