public function testCreateEmailMessageForm()
 {
     $emailMessage = new EmailMessage();
     $emailMessageForm = new CreateEmailMessageForm($emailMessage);
     $emailMessageForm->setScenario('createNonDraft');
     $this->assertFalse($emailMessageForm->validate());
     $this->assertEquals('To address cannot be blank', $emailMessageForm->getError('recipientsData_to'));
     $this->assertEquals('Subject cannot be blank.', $emailMessageForm->getModel()->getError('subject'));
     $this->assertEquals('Folder cannot be blank.', $emailMessageForm->getModel()->getError('folder'));
     $this->assertEquals('Sender cannot be blank.', $emailMessageForm->getModel()->getError('sender'));
     $messageRecipient = new EmailMessageRecipient();
     $messageRecipient->toAddress = '*****@*****.**';
     $messageRecipient->type = EmailMessageRecipient::TYPE_TO;
     $emailMessage->recipients->add($messageRecipient);
     $emailMessageForm = new CreateEmailMessageForm($emailMessage);
     $emailMessageForm->setScenario('createNonDraft');
     $this->assertNull($emailMessageForm->getError('recipientsData_to'));
 }
 public function actionCreateEmailMessage($toAddress = null, $relatedId = null, $relatedModelClassName = null)
 {
     $postData = PostUtil::getData();
     $getData = GetUtil::getData();
     $personOrAccount = self::resolvePersonOrAccountFromGet($relatedId, $relatedModelClassName);
     $emailMessage = new EmailMessage();
     $emailMessageForm = new CreateEmailMessageForm($emailMessage);
     $emailMessageForm->setScenario('createNonDraft');
     $postVariableName = get_class($emailMessageForm);
     if ($toAddress == null && $personOrAccount != null && $personOrAccount->primaryEmail->emailAddress != null) {
         $toAddress = $personOrAccount->primaryEmail->emailAddress;
     }
     if (isset($postData[$postVariableName])) {
         EmailMessageUtil::resolveEmailMessageFromPostData($postData, $emailMessageForm, Yii::app()->user->userModel);
         $this->actionValidateCreateEmailMessage($postData, $emailMessageForm);
         $this->attemptToSaveModelFromPost($emailMessageForm, null, false);
         ZurmoControllerUtil::updatePermissionsWithDefaultForModelByCurrentUser($emailMessageForm->getModel());
         Yii::app()->jobQueue->add('ProcessOutboundEmail');
     } else {
         EmailMessageUtil::resolveSignatureToEmailMessage($emailMessage, Yii::app()->user->userModel);
         EmailMessageUtil::resolvePersonOrAccountToEmailMessage($emailMessage, Yii::app()->user->userModel, $toAddress, $relatedId, $relatedModelClassName);
         $createEmailMessageModalEditView = new CreateEmailMessageModalEditView($this->getId(), $this->getModule()->getId(), $emailMessageForm);
         $view = new ModalView($this, $createEmailMessageModalEditView);
         Yii::app()->getClientScript()->setToAjaxMode();
         echo $view->render();
     }
 }