示例#1
0
 /**
  * Adding a new message.
  * @param integer $user message receiver's ID
  * @return string|\yii\web\Response
  */
 public function actionNew($user = null)
 {
     $podiumUser = User::findMe();
     if (Message::tooMany($podiumUser->id)) {
         $this->warning(Yii::t('podium/flash', 'You have reached maximum {max_messages, plural, =1{ message} other{ messages}} per {max_minutes, plural, =1{ minute} other{ minutes}} limit. Wait few minutes before sending a new message.', ['max_messages' => Message::SPAM_MESSAGES, 'max_minutes' => Message::SPAM_WAIT]));
         return $this->redirect(['messages/inbox']);
     }
     $model = new Message();
     $to = null;
     if (!empty($user) && (int) $user > 0 && (int) $user != $podiumUser->id) {
         $member = User::find()->where(['id' => (int) $user, 'status' => User::STATUS_ACTIVE])->limit(1)->one();
         if ($member) {
             $model->receiversId = [$member->id];
             $to = $member;
         }
     }
     if ($model->load(Yii::$app->request->post())) {
         if ($model->validate()) {
             $validated = [];
             $errors = false;
             if (!empty($model->friendsId)) {
                 $model->receiversId = array_merge(is_array($model->receiversId) ? $model->receiversId : [], is_array($model->friendsId) ? $model->friendsId : []);
             }
             if (empty($model->receiversId)) {
                 $this->addError('receiver_id', Yii::t('podium/view', 'You have to select at least one message receiver.'));
                 $errors = true;
             } else {
                 foreach ($model->receiversId as $r) {
                     if ($r == $podiumUser->id) {
                         $this->addError('receiver_id', Yii::t('podium/view', 'You can not send message to yourself.'));
                         $errors = true;
                     } elseif ($podiumUser->isIgnoredBy($r)) {
                         $this->addError('receiver_id', Yii::t('podium/view', 'One of the selected members ignores you and has been removed from message receivers.'));
                         $errors = true;
                     } else {
                         $member = User::find()->where(['id' => (int) $r, 'status' => User::STATUS_ACTIVE])->limit(1)->one();
                         if ($member) {
                             $validated[] = $member->id;
                             if (count($validated) > Message::MAX_RECEIVERS) {
                                 $this->addError('receiver_id', Yii::t('podium/view', 'You can send message up to a maximum of 10 receivers at once.'));
                                 $errors = true;
                                 break;
                             }
                         }
                     }
                 }
                 $model->receiversId = $validated;
             }
             if (!$errors) {
                 if ($model->send()) {
                     $this->success(Yii::t('podium/flash', 'Message has been sent.'));
                     return $this->redirect(['messages/inbox']);
                 } else {
                     $this->error(Yii::t('podium/flash', 'Sorry! There was some error while sending your message.'));
                 }
             }
         }
     }
     return $this->render('new', ['model' => $model, 'to' => $to, 'friends' => User::friendsList()]);
 }