Beispiel #1
0
 public function searchDeleted($params)
 {
     $dataProvider = $this->search();
     $dataProvider->query->where(['or', ['and', ['sender_id' => Yii::$app->user->id], ['sender_status' => Message::getDeletedStatuses()]], ['and', ['receiver_id' => Yii::$app->user->id], ['receiver_status' => Message::getDeletedStatuses()]]]);
     if (!($this->load($params) && $this->validate())) {
         $dataProvider->query->joinWith(['receiverUser' => function ($q) {
             $q->from(User::tableName() . ' pdu_receiver');
         }, 'senderUser' => function ($q) {
             $q->from(User::tableName() . ' pdu_sender');
         }]);
         return $dataProvider;
     }
     $dataProvider->query->andFilterWhere(['like', 'topic', $this->topic]);
     $dataProvider->query->joinWith(['receiverUser' => function ($q) {
         $q->from(User::tableName() . ' pdu_receiver')->where(['like', 'pdu_receiver.username', $this->receiverName]);
     }, 'senderUser' => function ($q) {
         $q->from(User::tableName() . ' pdu_sender')->where(['like', 'pdu_sender.username', $this->senderName]);
     }]);
     return $dataProvider;
 }
 /**
  * Searches for sent messages.
  * @param array $params
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     // not very proud of this query - slow for sure
     // let me know if it can be done better.
     $subquery = (new Query())->select(['m2.replyto'])->from(['m1' => Message::tableName()])->leftJoin(['m2' => Message::tableName()], '`m1`.`replyto` = `m2`.`id`')->where(['is not', 'm2.replyto', null]);
     $query = self::find()->where(['and', ['sender_id' => User::loggedId(), 'sender_status' => Message::getSentStatuses()], ['not in', Message::tableName() . '.id', $subquery]]);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['attributes' => ['id', 'topic', 'created_at']]]);
     $dataProvider->sort->defaultOrder = ['id' => SORT_DESC];
     $dataProvider->pagination->pageSize = Yii::$app->session->get('per-page', 20);
     if (!($this->load($params) && $this->validate())) {
         $dataProvider->query->joinWith(['messageReceivers' => function ($q) {
             $q->joinWith(['receiver']);
         }]);
         return $dataProvider;
     }
     $dataProvider->query->andFilterWhere(['like', 'topic', $this->topic]);
     if (preg_match('/^(forum|orum|rum|um|m)?#([0-9]+)$/', strtolower($this->receiverName), $matches)) {
         $dataProvider->query->joinWith(['messageReceivers' => function ($q) use($matches) {
             $q->joinWith(['receiver' => function ($q) use($matches) {
                 $q->andFilterWhere(['username' => ['', null], User::tableName() . '.id' => $matches[2]]);
             }]);
         }]);
     } elseif (preg_match('/^([0-9]+)$/', $this->receiverName, $matches)) {
         $dataProvider->query->joinWith(['messageReceivers' => function ($q) use($matches) {
             $q->joinWith(['receiver' => function ($q) use($matches) {
                 $q->andFilterWhere(['or', ['like', 'username', $this->receiverName], ['username' => ['', null], 'id' => $matches[1]]]);
             }]);
         }]);
     } else {
         $dataProvider->query->joinWith(['messageReceivers' => function ($q) {
             $q->joinWith(['receiver' => function ($q) {
                 $q->andFilterWhere(['like', User::tableName() . '.username', $this->receiverName]);
             }]);
         }]);
     }
     return $dataProvider;
 }
Beispiel #3
0
 /**
  * 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;
 }
Beispiel #4
0
 /**
  * Sends message.
  * @return boolean
  */
 public function send()
 {
     if (!(new Query())->select('id')->from(User::tableName())->where(['id' => $this->receiver_id, 'status' => User::STATUS_ACTIVE])->exists()) {
         return false;
     }
     $this->sender_id = User::loggedId();
     $this->sender_status = self::STATUS_READ;
     $this->receiver_status = self::STATUS_NEW;
     if ($this->save()) {
         Cache::getInstance()->deleteElement('user.newmessages', $this->receiver_id);
         return true;
     }
     return false;
 }