public function beforeSave()
 {
     // pass timing
     $timing = $this->getProperty('timing', 0);
     if (empty($timing)) {
         $this->addFieldError('timing', $this->modx->lexicon('scheduler.error.no-timing'));
     }
     $this->object->setTiming($timing, false);
     return parent::beforeSave();
 }
 /**
  * @param sTaskRun $run
  * @return mixed
  */
 public function _run(&$run)
 {
     $content = $this->get('content');
     // Get some common file alternatives; prepending the base and core paths to specified directory.
     $fileBasePath = $this->xpdo->getOption('base_path') . $content;
     $fileCorePath = $this->xpdo->getOption('core_path') . $content;
     // Get path from the namespace
     $fileNamespacePath = false;
     $namespace = $this->_getNamespace();
     if ($namespace) {
         $fileNamespacePath = $namespace->getCorePath() . $content;
     }
     // The $file placeholder will hold the eventual file path as determined by priority
     $file = false;
     // Run through different ways the file could be specified in priority:
     // 1. Absolute specification
     // 2. Relative to the namespace core path
     // 3. Relative to the MODX root path
     // 4. Relative to the MODX core path
     if (file_exists($content)) {
         $file = $content;
     } elseif ($namespace && file_exists($fileNamespacePath)) {
         $file = $fileNamespacePath;
     } elseif (file_exists($fileBasePath)) {
         $file = $fileBasePath;
     } elseif (file_exists($fileCorePath)) {
         $file = $fileCorePath;
     }
     // $result will hold the eventual response from the task. Null for now.
     $result = null;
     // If we have determined a valid file path, we run it
     if (!empty($file)) {
         // Wrapping in try/catch block in an attempt to catch exceptions before they break stuff entirely
         try {
             // Make some stuff available for convenience
             $task =& $this;
             $modx =& $this->xpdo;
             $scriptProperties = $run->get('data');
             if (!is_array($scriptProperties)) {
                 $scriptProperties = array();
             }
             // Include the file. Sorta ugly but open to better approaches.
             $result = (include $file);
         } catch (Exception $e) {
             $run->addError('exception', array('error' => $e->getMessage(), 'file' => $file, 'line' => $e->getLine(), 'trace' => $e->getTraceAsString()));
         }
     } else {
         $run->addError('file_not_found', array('file' => $content, 'file_namespace_path' => $fileNamespacePath, 'file_base_path' => $fileBasePath, 'file_core_path' => $fileCorePath));
     }
     if ($result !== null) {
         return $result;
     }
     return false;
 }
 public function beforeSave()
 {
     // get timing or create one
     $timing = $this->getProperty('timing', 0);
     if (empty($timing)) {
         $timingNr = $this->getProperty('timing_number', 1);
         $timingInterval = $this->getProperty('timing_interval', 'minute') . ($timingNr != 1 ? 's' : '');
         // to make it: minutes, hours, months.. etc.
         $timing = strtotime('+' . $timingNr . ' ' . $timingInterval);
     }
     if (empty($timing)) {
         $this->addFieldError('timing', $this->modx->lexicon('scheduler.error.no-timing'));
     }
     $this->object->setTiming($timing, false);
     return parent::beforeSave();
 }
 /**
  * @param sTaskRun $run
  * @return mixed
  */
 public function _run(&$run)
 {
     $snippet = $this->get('content');
     $scriptProperties = (array) $run->get('data');
     $scriptProperties['task'] =& $this;
     $scriptProperties['run'] =& $run;
     // Check if the snippet exists before running it.
     // This may fail with OnElementNotFound snippets in 2.3
     $key = !empty($snippet) && is_numeric($snippet) ? 'id' : 'name';
     if ($this->xpdo->getCount('modSnippet', array($key => $snippet)) < 1) {
         $run->addError('snippet_not_found', array('snippet' => $snippet));
         return false;
     }
     /** @var modSnippet $snippet */
     $snippet = $this->xpdo->getObject('modSnippet', array($key => $snippet));
     if (empty($snippet) || !is_object($snippet)) {
         $run->addError('snippet_not_found', array('snippet' => $snippet));
         return false;
     }
     $snippet->setCacheable(false);
     $out = $snippet->process($scriptProperties);
     unset($scriptProperties, $snippet);
     return $out;
 }
 /**
  * @param sTaskRun $run
  * @return mixed
  */
 public function _run(&$run)
 {
     $action = $this->get('content');
     $path = $this->getOption('core_path') . 'model/modx/processors/';
     $data = array('task' => &$this, 'run' => &$run);
     $runData = $run->get('data');
     if (is_array($runData)) {
         $data = array_merge($runData, $data);
     }
     $namespace = $this->_getNamespace();
     if ($namespace && $namespace->name != 'core') {
         $path = $namespace->getCorePath() . 'processors/';
     }
     /** @var modProcessorResponse $response */
     $response = $this->xpdo->runProcessor($action, $data, array('processors_path' => $path));
     if ($response->isError()) {
         $errors = $response->getFieldErrors();
         /** @var modProcessorResponseError $error */
         foreach ($errors as $error) {
             $run->addError($error->field, array('message' => $error->error));
         }
     }
     return $response->getMessage();
 }
 public function beforeSave()
 {
     $this->newObject->set('status', sTaskRun::STATUS_SCHEDULED);
     $this->newObject->set('executedon', null);
     $this->newObject->set('errors', null);
     $this->newObject->set('message', null);
     // get timing or create one
     $timing = $this->getProperty('timing_new', 0);
     if (empty($timing)) {
         $timingNr = $this->getProperty('timing_number', 1);
         $timingInterval = $this->getProperty('timing_interval', 'minute') . ($timingNr != 1 ? 's' : '');
         // to make it: minutes, hours, months.. etc.
         $timing = strtotime('+' . $timingNr . ' ' . $timingInterval);
     }
     if (empty($timing)) {
         $this->addFieldError('timing', $this->modx->lexicon('scheduler.error.no-timing'));
     }
     $this->newObject->setTiming($timing, false);
     return parent::beforeSave();
 }
 /**
  * @param sTaskRun $run
  * @return mixed
  */
 protected function _run(&$run)
 {
     /* This method should be abstract, but because of using xPDO::loadClass() in the main model, it cannot handle it */
     $run->addError('stask_direct_run_failure', array('message' => 'Not allowed to run the sTask::_run() method directly!'));
     return false;
 }