/**
  * @param WorkflowMessageInQueue $model
  * @return string
  */
 public static function renderSummaryContent(WorkflowMessageInQueue $model)
 {
     $params = array('label' => strval($model->savedWorkflow), 'wrapLabel' => false);
     $moduleClassName = $model->getModuleClassName();
     $moduleId = $moduleClassName::getDirectoryName();
     $element = new DetailsLinkActionElement('default', $moduleId, $model->savedWorkflow->id, $params);
     return $element->render() . static::resolveModelAndContent($model);
 }
 /**
  * @param WorkflowMessageInQueue $model
  * @return string
  */
 public static function renderSummaryContent(WorkflowMessageInQueue $model)
 {
     $params = array('label' => strval($model->savedWorkflow), 'wrapLabel' => false);
     $moduleClassName = $model->getModuleClassName();
     $moduleId = $moduleClassName::getDirectoryName();
     $element = new DetailsLinkActionElement('default', $moduleId, $model->savedWorkflow->id, $params);
     $relatedModel = self::resolveModel($model);
     return $element->render() . ' &mdash; <span class="less-pronounced-text">' . self::resolveModelContent($relatedModel) . '</span>';
 }
 /**
  * @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 (WorkflowMessageInQueue::getModelsToProcess($resolvedBatchSize) as $workflowMessageInQueue) {
             if ($processedModelsCount < $batchSize || $batchSize == null) {
                 try {
                     $model = $this->resolveModel($workflowMessageInQueue);
                     $this->resolveSavedWorkflowIsValid($workflowMessageInQueue);
                     $this->processWorkflowMessageInQueue($workflowMessageInQueue, $model);
                 } catch (NotFoundException $e) {
                 }
                 $workflowMessageInQueue->delete();
                 $processedModelsCount++;
             } else {
                 Yii::app()->jobQueue->add('WorkflowMessageInQueue', 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;
     }
 }
 public function testProcessAfterSaveWhenSendIsInFuture()
 {
     $this->assertEquals(0, WorkflowMessageInQueue::getCount());
     $workflow = new Workflow();
     $workflow->setId(self::$savedWorkflow->id);
     $workflow->type = Workflow::TYPE_ON_SAVE;
     $emailMessageForm = new EmailMessageForWorkflowForm('WorkflowModelTestItem', Workflow::TYPE_ON_SAVE);
     $emailMessageForm->sendAfterDurationInterval = 1;
     $emailMessageForm->sendAfterDurationType = TimeDurationUtil::DURATION_TYPE_DAY;
     $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::TYPE_DYNAMIC_TRIGGERED_MODEL_USER, 'audienceType' => EmailMessageRecipient::TYPE_TO, 'dynamicUserType' => DynamicTriggeredModelUserWorkflowEmailMessageRecipientForm::DYNAMIC_USER_TYPE_CREATED_BY_USER));
     $emailMessageForm->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
     $workflow->addEmailMessage($emailMessageForm);
     $model = new WorkflowModelTestItem();
     $model->lastName = 'lastName';
     $model->string = 'string';
     $this->assertTrue($model->save());
     $compareDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + 86400);
     WorkflowEmailMessagesUtil::processAfterSave($workflow, $model, Yii::app()->user->userModel);
     $workflowMessageInQueues = WorkflowMessageInQueue::getAll();
     $this->assertEquals(1, count($workflowMessageInQueues));
     $this->assertTrue($workflowMessageInQueues[0]->savedWorkflow->isSame(self::$savedWorkflow));
     $this->assertTrue($workflowMessageInQueues[0]->triggeredByUser->isSame(Yii::app()->user->userModel));
     $this->assertEquals($model->getClassId('Item'), $workflowMessageInQueues[0]->modelItem->getClassId('Item'));
     $this->assertEquals('WorkflowModelTestItem', $workflowMessageInQueues[0]->modelClassName);
     $this->assertEquals($compareDateTime, $workflowMessageInQueues[0]->processDateTime);
     $emailMessageData = SavedWorkflowToWorkflowAdapter::makeArrayFromEmailMessageForWorkflowFormAttributesData(array($emailMessageForm));
     $this->assertEquals(serialize($emailMessageData), $workflowMessageInQueues[0]->serializedData);
     $this->assertTrue($workflowMessageInQueues[0]->delete());
 }
 public static function createExpiredWorkflowMessageInQueue(RedBeanModel $model, SavedWorkflow $savedWorkflow)
 {
     $workflowMessageInQueue = new WorkflowMessageInQueue();
     $workflowMessageInQueue->modelClassName = get_class($model);
     $workflowMessageInQueue->modelItem = $model;
     $workflowMessageInQueue->processDateTime = '2007-02-02 00:00:00';
     $workflowMessageInQueue->savedWorkflow = $savedWorkflow;
     $workflowMessageInQueue->triggeredByUser = Yii::app()->user->userModel;
     $workflowMessageInQueue->serializedData = serialize(array('something'));
     $saved = $workflowMessageInQueue->save();
     assert($saved);
     // Not Coding Standard
     return $workflowMessageInQueue;
 }
 /**
  * @depends testRun
  */
 public function testRunAgainstWorkflowThatWasDeleted()
 {
     $model = WorkflowTestHelper::createWorkflowModelTestItem('Green', '514');
     $timeTrigger = array('attributeIndexOrDerivedType' => 'string', 'operator' => OperatorRules::TYPE_EQUALS, 'value' => '514', 'durationSeconds' => '333');
     $actions = array(array('type' => ActionForWorkflowForm::TYPE_UPDATE_SELF, ActionForWorkflowForm::ACTION_ATTRIBUTES => array('string' => array('shouldSetValue' => '1', 'type' => WorkflowActionAttributeForm::TYPE_STATIC, 'value' => 'jason'))));
     $savedWorkflow = WorkflowTestHelper::createByTimeSavedWorkflow($timeTrigger, array(), $actions);
     WorkflowTestHelper::createExpiredWorkflowMessageInQueue($model, $savedWorkflow);
     //Now delete the old workflow
     $deleted = $savedWorkflow->delete();
     $this->assertTrue($deleted);
     $this->assertEquals(1, count(WorkflowMessageInQueue::getAll()));
     $job = new WorkflowMessageInQueueJob();
     $this->assertTrue($job->run());
     $this->assertEquals(0, count(WorkflowMessageInQueue::getAll()));
 }
 /**
  * @see BaseJob::run()
  */
 public function run()
 {
     try {
         $originalUser = Yii::app()->user->userModel;
         Yii::app()->user->userModel = BaseJobControlUserConfigUtil::getUserToRunAs();
         foreach (WorkflowMessageInQueue::getModelsToProcess(self::$pageSize) as $workflowMessageInQueue) {
             try {
                 $model = $this->resolveModel($workflowMessageInQueue);
                 $this->resolveSavedWorkflowIsValid($workflowMessageInQueue);
                 $this->processWorkflowMessageInQueue($workflowMessageInQueue, $model);
             } catch (NotFoundException $e) {
             }
             $workflowMessageInQueue->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;
     }
 }
 /**
  * @param Workflow $workflow
  * @param EmailMessageForWorkflowForm $emailMessage
  * @param RedBeanModel $model
  * @param User $triggeredByUser
  * @throws FailedToSaveModelException
  */
 protected static function processEmailMessageAfterSave(Workflow $workflow, EmailMessageForWorkflowForm $emailMessage, RedBeanModel $model, User $triggeredByUser)
 {
     if ($emailMessage->sendAfterDurationInterval == 0) {
         $helper = new WorkflowEmailMessageProcessingHelper($emailMessage, $model, $triggeredByUser);
         $helper->process();
     } else {
         $emailMessageData = SavedWorkflowToWorkflowAdapter::makeArrayFromEmailMessageForWorkflowFormAttributesData(array($emailMessage));
         $workflowMessageInQueue = new WorkflowMessageInQueue();
         $workflowMessageInQueue->processDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($emailMessage->resolveNewTimeStampForDuration(time()));
         $workflowMessageInQueue->savedWorkflow = SavedWorkflow::getById((int) $workflow->getId());
         $workflowMessageInQueue->modelClassName = get_class($model);
         $workflowMessageInQueue->modelItem = $model;
         $workflowMessageInQueue->serializedData = serialize($emailMessageData);
         $workflowMessageInQueue->triggeredByUser = $triggeredByUser;
         $saved = $workflowMessageInQueue->save();
         if (!$saved) {
             throw new FailedToSaveModelException();
         }
     }
 }
 /**
  * Special method to load ByTimeWorkflowInQueue models
  */
 public function actionLoadWorkflowMessageInQueue()
 {
     if (!Group::isUserASuperAdministrator(Yii::app()->user->userModel)) {
         throw new NotSupportedException();
     }
     $model = new Account();
     $model->name = 'test account';
     $saved = $model->save();
     if (!$saved) {
         throw new NotSupportedException();
     }
     $savedWorkflow = new SavedWorkflow();
     $savedWorkflow->name = 'Test for demo Message Queue model';
     $savedWorkflow->description = 'description';
     $savedWorkflow->moduleClassName = 'AccountsModule';
     $savedWorkflow->triggerOn = Workflow::TRIGGER_ON_NEW;
     $savedWorkflow->type = Workflow::TYPE_BY_TIME;
     $savedWorkflow->isActive = false;
     $savedWorkflow->order = 1;
     $savedWorkflow->serializedData = serialize(array(ComponentForWorkflowForm::TYPE_TRIGGERS => array(), ComponentForWorkflowForm::TYPE_ACTIONS => array(), ComponentForWorkflowForm::TYPE_EMAIL_MESSAGES => array()));
     $saved = $savedWorkflow->save();
     if (!$saved) {
         throw new NotSupportedException();
     }
     for ($i = 1; $i < 6; $i++) {
         $workflowMessageInQueue = new WorkflowMessageInQueue();
         $workflowMessageInQueue->processDateTime = '2014-03-0' . $i . ' 00:00:00';
         $workflowMessageInQueue->savedWorkflow = $savedWorkflow;
         $workflowMessageInQueue->modelClassName = get_class($model);
         $workflowMessageInQueue->modelItem = $model;
         $workflowMessageInQueue->serializedData = serialize(array());
         $workflowMessageInQueue->triggeredByUser = Yii::app()->user->userModel;
         $saved = $workflowMessageInQueue->save();
         if (!$saved) {
             throw new NotSupportedException();
         }
     }
 }
 /**
  * @depends testSetAndGet
  */
 public function testGetModelsToProcess($pageSize)
 {
     $this->assertEquals(1, WorkflowMessageInQueue::getCount());
     $models = WorkflowMessageInQueue::getModelsToProcess(10);
     $this->assertEquals(1, count($models));
     //Now have one that is not ready for processing. It should still only get 1
     $model = new WorkflowModelTestItem();
     $model->lastName = 'Green2';
     $model->string = 'string2';
     $saved = $model->save();
     $this->assertTrue($saved);
     $savedWorkflows = SavedWorkflow::getByName('some workflow');
     $workflowMessageInQueue = new WorkflowMessageInQueue();
     $workflowMessageInQueue->modelClassName = get_class($model);
     $workflowMessageInQueue->modelItem = $model;
     $workflowMessageInQueue->processDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + 86400);
     $workflowMessageInQueue->savedWorkflow = $savedWorkflows[0];
     $workflowMessageInQueue->serializedData = serialize(array('something'));
     $saved = $workflowMessageInQueue->save();
     $this->assertTrue($saved);
     $models = WorkflowMessageInQueue::getModelsToProcess(10);
     $this->assertEquals(1, count($models));
 }
 public function testMassDeletePagesProperlyAndRemovesAllSelectedForWorkflowMessageInQueue()
 {
     //MassDelete for selected Record Count
     $workflowMessageInQueue01 = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($this->model, $this->savedWorkflow);
     $workflowMessageInQueue02 = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($this->model, $this->savedWorkflow);
     $workflowMessageInQueue03 = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($this->model, $this->savedWorkflow);
     $workflowMessageInQueue04 = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($this->model, $this->savedWorkflow);
     $workflowMessageInQueue05 = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($this->model, $this->savedWorkflow);
     $workflowMessageInQueue06 = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($this->model, $this->savedWorkflow);
     $workflowMessageInQueue07 = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($this->model, $this->savedWorkflow);
     $workflowMessageInQueue08 = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($this->model, $this->savedWorkflow);
     $workflowMessageInQueue09 = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($this->model, $this->savedWorkflow);
     $workflowMessageInQueue10 = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($this->model, $this->savedWorkflow);
     //save Model MassDelete for entire search result
     $this->setGetArray(array('selectAll' => '1', 'WorkflowMessageInQueue_page' => 1));
     $this->setPostArray(array('selectedRecordCount' => 10));
     //Run Mass Delete using progress save for page1.
     $pageSize = Yii::app()->pagination->getForCurrentUserByType('massDeleteProgressPageSize');
     $this->assertEquals(5, $pageSize);
     $this->runControllerWithExitExceptionAndGetContent('workflows/defaultMessageQueue/massDelete');
     //check for previous mass delete progress
     $models = WorkflowMessageInQueue::getAll();
     $this->assertEquals(5, count($models));
     $this->setGetArray(array('selectAll' => '1', 'WorkflowMessageInQueue_page' => 2));
     $this->setPostArray(array('selectedRecordCount' => 10));
     //Run Mass Delete using progress save for page2.
     $pageSize = Yii::app()->pagination->getForCurrentUserByType('massDeleteProgressPageSize');
     $this->assertEquals(5, $pageSize);
     $this->runControllerWithNoExceptionsAndGetContent('workflows/defaultMessageQueue/massDeleteProgress');
     //calculating count
     $models = WorkflowMessageInQueue::getAll();
     $this->assertEquals(0, count($models));
 }