/**
  * @param WorkflowMessageInQueue $workflowMessageInQueue
  * @param RedBeanModel $model
  */
 protected function processWorkflowMessageInQueue(WorkflowMessageInQueue $workflowMessageInQueue, RedBeanModel $model)
 {
     $workflow = SavedWorkflowToWorkflowAdapter::makeWorkflowBySavedWorkflow($workflowMessageInQueue->savedWorkflow);
     if (!$workflow->getIsActive()) {
         return;
     }
     WorkflowEmailMessagesUtil::processOnWorkflowMessageInQueueJob($workflow, $model, self::resolveTriggeredByUser($workflowMessageInQueue));
 }
 /**
  * @param ByTimeWorkflowInQueue $byTimeWorkflowInQueue
  * @param RedBeanModel $model
  * @throws FailedToSaveModelException
  */
 protected function processByTimeWorkflowInQueue(ByTimeWorkflowInQueue $byTimeWorkflowInQueue, RedBeanModel $model)
 {
     $workflow = SavedWorkflowToWorkflowAdapter::makeWorkflowBySavedWorkflow($byTimeWorkflowInQueue->savedWorkflow);
     if (!$workflow->getIsActive()) {
         return;
     }
     $workflow->setTimeTriggerRequireChangeToProcessToFalse();
     if (WorkflowTriggersUtil::areTriggersTrueOnByTimeWorkflowQueueJob($workflow, $model)) {
         WorkflowActionsUtil::processOnByTimeWorkflowInQueueJob($workflow, $model, Yii::app()->user->userModel);
         WorkflowEmailMessagesUtil::processAfterSave($workflow, $model, Yii::app()->user->userModel);
         if ($model->isModified()) {
             $saved = $model->save();
             if (!$saved) {
                 throw new FailedToSaveModelException();
             }
         }
     }
 }
 /**
  * Given a RedBeanModel, process afterSave actions such as update related, create, and create related.
  * Also process any email messages.  If the workflow is by-time, then we should process the ByTimeWorkflowInQueue
  * model.
  * @param Item $model
  * @param User $triggeredByUser
  * @throws NotSupportedException
  */
 public static function resolveAfterSaveByModel(Item $model, User $triggeredByUser)
 {
     foreach ($model->getWorkflowsToProcessAfterSave() as $workflow) {
         if ($workflow->getType() == Workflow::TYPE_BY_TIME) {
             static::processToByTimeWorkflowInQueue($workflow, $model);
         } elseif ($workflow->getType() == Workflow::TYPE_ON_SAVE) {
             WorkflowActionsUtil::processAfterSave($workflow, $model, $triggeredByUser);
             WorkflowEmailMessagesUtil::processAfterSave($workflow, $model, $triggeredByUser);
         } else {
             throw new NotSupportedException();
         }
     }
 }
 public function testProcessAfterSaveWhenSendIsImmediateAndToAContactThatIsRelatedToTheTriggeredModel()
 {
     foreach (EmailMessage::getAll() as $emailMessage) {
         $emailMessage->delete();
     }
     $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
     $emailTemplate = new EmailTemplate();
     $emailTemplate->name = 'the name';
     $emailTemplate->modelClassName = 'Account';
     $emailTemplate->textContent = 'some content';
     $emailTemplate->type = 2;
     $emailTemplate->subject = 'subject';
     $saved = $emailTemplate->save();
     $this->assertTrue($saved);
     $workflow = new Workflow();
     $workflow->setId(self::$savedWorkflow->id);
     $workflow->type = Workflow::TYPE_ON_SAVE;
     $emailMessageForm = new EmailMessageForWorkflowForm('Account', Workflow::TYPE_ON_SAVE);
     $emailMessageForm->sendAfterDurationSeconds = 0;
     $emailMessageForm->emailTemplateId = $emailTemplate->id;
     $emailMessageForm->sendFromType = EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT;
     $recipients = array(array('type' => WorkflowEmailMessageRecipientForm::TYPE_DYNAMIC_TRIGGERED_MODEL_RELATION, 'audienceType' => EmailMessageRecipient::TYPE_TO, 'relation' => 'contacts'));
     $emailMessageForm->setAttributes(array(EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS => $recipients));
     $workflow->addEmailMessage($emailMessageForm);
     $model = new Account();
     $model->name = 'the account';
     $contact = new Contact();
     $contact->firstName = 'Jason';
     $contact->lastName = 'Blue';
     $contact->state = ContactsUtil::getStartingState();
     $contact->primaryEmail->emailAddress = '*****@*****.**';
     $this->assertTrue($contact->save());
     $contact2 = new Contact();
     $contact2->firstName = 'Laura';
     $contact2->lastName = 'Blue';
     $contact2->state = ContactsUtil::getStartingState();
     $contact2->primaryEmail->emailAddress = '*****@*****.**';
     $this->assertTrue($contact2->save());
     $model->contacts->add($contact);
     $model->contacts->add($contact2);
     $this->assertTrue($model->save());
     WorkflowEmailMessagesUtil::processAfterSave($workflow, $model, Yii::app()->user->userModel);
     $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
     $queuedEmailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
     $this->assertEquals(1, count($queuedEmailMessages));
     $this->assertEquals(2, count($queuedEmailMessages[0]->recipients));
     $this->assertEquals('Jason Blue', $queuedEmailMessages[0]->recipients[0]->toName);
     $this->assertEquals('*****@*****.**', $queuedEmailMessages[0]->recipients[0]->toAddress);
     $this->assertEquals($contact->id, $queuedEmailMessages[0]->recipients[0]->personOrAccount->id);
     $this->assertEquals('Laura Blue', $queuedEmailMessages[0]->recipients[1]->toName);
     $this->assertEquals('*****@*****.**', $queuedEmailMessages[0]->recipients[1]->toAddress);
     $this->assertEquals($contact2->id, $queuedEmailMessages[0]->recipients[1]->personOrAccount->id);
 }
 public function testMakeEmailMessageForWorkflowFormByQueueModelAndWorkflow()
 {
     $model = ContactTestHelper::createContactByNameForOwner('Jason', Yii::app()->user->userModel);
     $savedWorkflow = new SavedWorkflow();
     $savedWorkflow->name = 'some workflow';
     $savedWorkflow->description = 'description';
     $savedWorkflow->moduleClassName = 'WorkflowsTestModule';
     $savedWorkflow->triggerOn = Workflow::TRIGGER_ON_NEW;
     $savedWorkflow->type = 'some type';
     $savedWorkflow->order = 1;
     $savedWorkflow->serializedData = serialize(array('something'));
     $savedWorkflow->isActive = true;
     $saved = $savedWorkflow->save();
     $this->assertTrue($saved);
     $emailMessage = null;
     $emailMessage['emailTemplateId'] = 5;
     $emailMessage['sendFromType'] = EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT;
     $emailMessage['sendAfterDurationType'] = TimeDurationUtil::DURATION_TYPE_DAY;
     $emailMessage['sendAfterDurationInterval'] = '44';
     $emailMessage[EmailMessageForWorkflowForm::EMAIL_MESSAGE_RECIPIENTS] = array(array('type' => WorkflowEmailMessageRecipientForm::TYPE_DYNAMIC_TRIGGERED_MODEL, 'audienceType' => EmailMessageRecipient::TYPE_TO));
     $workflowMessageInQueue = WorkflowTestHelper::createExpiredWorkflowMessageInQueue($model, $savedWorkflow, serialize(array($emailMessage)));
     $workflow = new Workflow();
     $workflow->setModuleClassName('WorkflowsTestModule');
     $workflow->setType(Workflow::TYPE_ON_SAVE);
     $emailMessageForWorkflowForm = WorkflowEmailMessagesUtil::makeEmailMessageForWorkflowFormByQueueModelAndWorkflow($workflowMessageInQueue, $workflow);
     $this->assertEquals(5, $emailMessageForWorkflowForm->emailTemplateId);
     $this->assertEquals(EmailMessageForWorkflowForm::SEND_FROM_TYPE_DEFAULT, $emailMessageForWorkflowForm->sendFromType);
     $this->assertEquals(TimeDurationUtil::DURATION_TYPE_DAY, $emailMessageForWorkflowForm->sendAfterDurationType);
     $this->assertEquals(44, $emailMessageForWorkflowForm->sendAfterDurationInterval);
     $this->assertEquals(1, count($emailMessageForWorkflowForm->getEmailMessageRecipients()));
 }