/** * Returns number of new user messages. * @return integer */ public function getNewMessagesCount() { $cache = Cache::getInstance()->getElement('user.newmessages', $this->id); if ($cache === false) { $cache = (new Query())->from(MessageReceiver::tableName())->where(['receiver_id' => $this->id, 'receiver_status' => Message::STATUS_NEW])->count(); Cache::getInstance()->setElement('user.newmessages', $this->id, $cache); } return $cache; }
/** * Viewing the received message of given ID. * @param integer $id * @return string|\yii\web\Response */ public function actionViewReceived($id = null) { $model = MessageReceiver::find()->where(['and', ['id' => $id, 'receiver_id' => User::loggedId()], ['!=', 'receiver_status', MessageReceiver::STATUS_DELETED]])->limit(1)->one(); if (empty($model)) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the message with the given ID.')); return $this->redirect(['messages/inbox']); } $model->markRead(); return $this->render('view', ['model' => $model->message, 'type' => 'received', 'id' => $model->id]); }
/** * Sends message. * @return boolean */ public function send() { $transaction = static::getDb()->beginTransaction(); try { $this->sender_id = User::loggedId(); $this->sender_status = self::STATUS_READ; if ($this->save()) { $count = count($this->receiversId); foreach ($this->receiversId as $receiver) { if (!(new Query())->select('id')->from(User::tableName())->where(['id' => $receiver, 'status' => User::STATUS_ACTIVE])->exists()) { if ($count == 1) { throw new Exception('No active receivers to send message to!'); } else { continue; } } $message = new MessageReceiver(); $message->message_id = $this->id; $message->receiver_id = $receiver; $message->receiver_status = self::STATUS_NEW; if ($message->save()) { Cache::getInstance()->deleteElement('user.newmessages', $receiver); } else { throw new Exception('MessageReceiver saving error!'); } } $transaction->commit(); $sessionKey = 'messages.' . $this->sender_id; if (Yii::$app->session->has($sessionKey)) { $sentAlready = explode('|', Yii::$app->session->get($sessionKey)); $sentAlready[] = time(); Yii::$app->session->set($sessionKey, implode('|', $sentAlready)); } else { Yii::$app->session->set($sessionKey, time()); } return true; } else { throw new Exception('Message saving error!'); } } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), $this->id, __METHOD__); } return false; }
/** * Viewing the received message of given ID. * @param integer $id * @return string|\yii\web\Response */ public function actionViewReceived($id = null) { $model = MessageReceiver::find()->where(['and', ['id' => $id, 'receiver_id' => User::loggedId()], ['!=', 'receiver_status', MessageReceiver::STATUS_DELETED]])->limit(1)->one(); if (empty($model)) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the message with the given ID.')); return $this->redirect(['messages/inbox']); } if ($model->receiver_status == Message::STATUS_NEW) { $model->receiver_status = Message::STATUS_READ; if ($model->save()) { Cache::getInstance()->deleteElement('user.newmessages', $model->receiver_id); } } return $this->render('view', ['model' => $model->message, 'type' => 'received', 'id' => $model->id]); }