/**
  * Creates a new Message
  * and redirects to it.
  */
 public function actionCreate()
 {
     $userGuid = Yii::$app->request->get('userGuid');
     $model = new CreateMessage();
     // Preselect user if userGuid is given
     if ($userGuid != "") {
         $user = User::findOne(array('guid' => $userGuid));
         if (isset($user)) {
             $model->recipient = $user->guid;
         }
     }
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         // Create new Message
         $message = new Message();
         $message->title = $model->title;
         $message->save();
         // Attach Message Entry
         $messageEntry = new MessageEntry();
         $messageEntry->message_id = $message->id;
         $messageEntry->user_id = Yii::$app->user->id;
         $messageEntry->content = $model->message;
         $messageEntry->save();
         File::attachPrecreated($messageEntry, Yii::$app->request->post('fileUploaderHiddenGuidField'));
         // Attach also Recipients
         foreach ($model->getRecipients() as $recipient) {
             $userMessage = new UserMessage();
             $userMessage->message_id = $message->id;
             $userMessage->user_id = $recipient->id;
             $userMessage->save();
         }
         // Inform recipients (We need to add all before)
         foreach ($model->getRecipients() as $recipient) {
             $message->notify($recipient);
         }
         // Attach User Message
         $userMessage = new UserMessage();
         $userMessage->message_id = $message->id;
         $userMessage->user_id = Yii::$app->user->id;
         $userMessage->is_originator = 1;
         $userMessage->last_viewed = new \yii\db\Expression('NOW()');
         $userMessage->save();
         return $this->htmlRedirect(['index', 'id' => $message->id]);
     }
     return $this->renderAjax('create', array('model' => $model));
 }
 /**
  * Creates a new Message
  * and redirects to it.
  */
 public function actionCreate()
 {
     $userGuid = Yii::$app->request->get('userGuid');
     $model = new CreateMessage();
     // Preselect user if userGuid is given
     if ($userGuid != "") {
         $user = User::findOne(array('guid' => $userGuid));
         if (isset($user) && (version_compare(Yii::$app->version, '1.1', 'lt') || $user->getPermissionManager()->can(new SendMail()))) {
             $model->recipient = $user->guid;
         }
     }
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         // Create new Message
         $message = new Message();
         $message->title = $model->title;
         $message->save();
         // Attach Message Entry
         $messageEntry = new MessageEntry();
         $messageEntry->message_id = $message->id;
         $messageEntry->user_id = Yii::$app->user->id;
         $messageEntry->content = $model->message;
         $messageEntry->save();
         File::attachPrecreated($messageEntry, Yii::$app->request->post('fileUploaderHiddenGuidField'));
         // Attach also Recipients
         foreach ($model->getRecipients() as $recipient) {
             $userMessage = new UserMessage();
             $userMessage->message_id = $message->id;
             $userMessage->user_id = $recipient->id;
             $userMessage->save();
         }
         // Inform recipients (We need to add all before)
         foreach ($model->getRecipients() as $recipient) {
             try {
                 $message->notify($recipient);
             } catch (\Exception $e) {
                 Yii::error('Could not send notification e-mail to: ' . $recipient->username . ". Error:" . $e->getMessage());
             }
         }
         // Attach User Message
         $userMessage = new UserMessage();
         $userMessage->message_id = $message->id;
         $userMessage->user_id = Yii::$app->user->id;
         $userMessage->is_originator = 1;
         $userMessage->last_viewed = new \yii\db\Expression('NOW()');
         $userMessage->save();
         return $this->htmlRedirect(['index', 'id' => $message->id]);
     }
     return $this->renderPartial('create', array('model' => $model));
 }