/**
  * @param EmailMessageForWorkflowForm $emailMessageForWorkflowForm
  * @param RedBeanModel $model
  * @param User $triggeredByUser
  */
 public static function processOnWorkflowMessageInQueueJob(EmailMessageForWorkflowForm $emailMessageForWorkflowForm, RedBeanModel $model, User $triggeredByUser)
 {
     try {
         if ($emailMessageForWorkflowForm->getEmailMessageRecipientFormsCount() > 0) {
             $helper = new WorkflowEmailMessageProcessingHelper($emailMessageForWorkflowForm, $model, $triggeredByUser);
             $helper->process();
         }
     } catch (Exception $e) {
         WorkflowUtil::handleProcessingException($e, 'application.modules.workflows.utils.WorkflowEmailMessagesUtil.processOnWorkflowMessageInQueueJob');
     }
 }
 /**
  * @see BaseJob::run()
  */
 public function run()
 {
     try {
         $originalUser = Yii::app()->user->userModel;
         Yii::app()->user->userModel = BaseControlUserConfigUtil::getUserToRunAs();
         foreach (ByTimeWorkflowInQueue::getModelsToProcess(self::$pageSize) as $byTimeWorkflowInQueue) {
             try {
                 $model = $this->resolveModel($byTimeWorkflowInQueue);
                 $this->resolveSavedWorkflowIsValid($byTimeWorkflowInQueue);
                 $this->processByTimeWorkflowInQueue($byTimeWorkflowInQueue, $model);
             } catch (NotFoundException $e) {
                 WorkflowUtil::handleProcessingException($e, 'application.modules.workflows.jobs.ByTimeWorkflowInQueueJob.run');
             }
             $byTimeWorkflowInQueue->delete();
         }
         Yii::app()->user->userModel = $originalUser;
         return true;
     } catch (MissingASuperAdministratorException $e) {
         //skip running workflow, since no super administrators are available.
         $this->errorMessage = Zurmo::t('WorkflowsModule', 'Could not process since no super administrators were found');
         return false;
     }
 }
 /**
  * @see BaseJob::run()
  */
 public function run()
 {
     try {
         $originalUser = Yii::app()->user->userModel;
         Yii::app()->user->userModel = BaseControlUserConfigUtil::getUserToRunAs();
         $processedModelsCount = 0;
         $batchSize = $this->resolveBatchSize();
         if ($batchSize != null) {
             $resolvedBatchSize = $batchSize + 1;
         } else {
             $resolvedBatchSize = null;
         }
         foreach (ByTimeWorkflowInQueue::getModelsToProcess($resolvedBatchSize) as $byTimeWorkflowInQueue) {
             if ($processedModelsCount < $batchSize || $batchSize == null) {
                 try {
                     $model = $this->resolveModel($byTimeWorkflowInQueue);
                     $this->resolveSavedWorkflowIsValid($byTimeWorkflowInQueue);
                     $this->processByTimeWorkflowInQueue($byTimeWorkflowInQueue, $model);
                 } catch (NotFoundException $e) {
                     WorkflowUtil::handleProcessingException($e, 'application.modules.workflows.jobs.ByTimeWorkflowInQueueJob.run');
                 }
                 $byTimeWorkflowInQueue->delete();
                 $processedModelsCount++;
             } else {
                 Yii::app()->jobQueue->add('ByTimeWorkflowInQueue', 5);
                 break;
             }
         }
         Yii::app()->user->userModel = $originalUser;
         return true;
     } catch (MissingASuperAdministratorException $e) {
         //skip running workflow, since no super administrators are available.
         $this->errorMessage = Zurmo::t('WorkflowsModule', 'Could not process since no super administrators were found');
         return false;
     }
 }
 /**
  * @param Workflow $workflow
  * @param RedBeanModel $model
  * @throws FailedToSaveModelException
  */
 protected static function processToByTimeWorkflowInQueue(Workflow $workflow, RedBeanModel $model)
 {
     assert('$workflow->getId() > 0');
     try {
         $byTimeWorkflowInQueue = ByTimeWorkflowInQueue::resolveByWorkflowIdAndModel(SavedWorkflow::getById((int) $workflow->getId()), $model);
         $byTimeWorkflowInQueue->processDateTime = static::resolveProcessDateTimeByWorkflowAndModel($workflow, $model);
         $saved = $byTimeWorkflowInQueue->save();
         if (!$saved) {
             throw new FailedToSaveModelException();
         }
     } catch (ValueForProcessDateTimeIsNullException $e) {
         //For now just log this exception. If this exception is thrown it means a date or dateTime
         //somehow was set to empty, so we can't properly process this.
         WorkflowUtil::handleProcessingException($e, 'application.modules.workflows.utils.SavedWorkflowsUtil.processToByTimeWorkflowInQueue');
     }
 }
 /**
  * Process any workflow actions that are updates to the passed in model during the processing of any
  * ByTimeWorkflowQueue models. @see ByTimeWorkflowInQueueJob.
  * @param Workflow $workflow
  * @param RedBeanModel $model
  * @param User $triggeredByUser
  * @throws FailedToSaveModelException
  */
 public static function processOnByTimeWorkflowInQueueJob(Workflow $workflow, RedBeanModel $model, User $triggeredByUser)
 {
     foreach ($workflow->getActions() as $action) {
         try {
             $helper = new WorkflowActionProcessingHelper((int) $workflow->getId(), $workflow->getName(), $action, $model, $triggeredByUser, false);
             $helper->processUpdateSelfAction();
             $helper->processNonUpdateSelfAction();
         } catch (Exception $e) {
             WorkflowUtil::handleProcessingException($e, 'application.modules.workflows.utils.WorkflowActionsUtil.processOnByTimeWorkflowInQueueJob');
         }
     }
 }