コード例 #1
0
ファイル: Chat.php プロジェクト: poprigun/chat
 public function registerAssets()
 {
     $view = $this->getView();
     if ($this->node) {
         $this->options['rooms'] = self::generateRoomIds(Yii::$app->user->id, PoprigunChatDialog::getUserDialogs(Yii::$app->user->id));
         $this->options['socketUrl'] = isset($this->socketUrl) ? $this->count : 'http://' . $_SERVER['SERVER_ADDR'] . ':8080';
         ChatAssets::register($view);
     } else {
         ChatWithoutNodeAssets::register($view);
     }
     $script = '
          poprigunChat = new PoprigunChat(' . Json::encode($this->options) . ');
     ';
     $view->registerJs($script, View::POS_END);
     echo $this->renderFile($this->template, ['model' => new PoprigunChatMessage(), 'options' => $this->options, 'rooms' => self::generateRoomIds(Yii::$app->user->id, PoprigunChatDialog::getUserDialogs(Yii::$app->user->id))]);
 }
コード例 #2
0
ファイル: PoprigunChatDialog.php プロジェクト: poprigun/chat
 /**
  * Get new message count
  * @param null|integer $dialogId
  * @return int|string
  */
 public function getNewCount($dialogId = null)
 {
     $query = $this->hasMany(PoprigunChatMessage::className(), ['dialog_id' => 'id'])->innerJoinWith('chatUserRel')->andWhere([PoprigunChatUserRel::tableName() . '.is_view' => PoprigunChatUserRel::NEW_MESSAGE]);
     if (null !== $dialogId) {
         $query->andWhere([PoprigunChatDialog::tableName() . '.id' => $dialogId]);
     }
     return $query->count();
 }
コード例 #3
0
ファイル: PoprigunChatUser.php プロジェクト: poprigun/chat
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getDialog()
 {
     return $this->hasOne(PoprigunChatDialog::className(), ['id' => 'dialog_id']);
 }
コード例 #4
0
ファイル: PoprigunChatMessage.php プロジェクト: poprigun/chat
 /**
  * Send message
  * @param string|null $title
  * @return bool
  */
 public function sendMessage($title = null)
 {
     try {
         switch ($this->messageType) {
             case self::MESSAGE_TO_USER:
                 $dialog = PoprigunChatDialog::getMessageDialog($this->author_id, $this->receiverId, $title);
                 break;
             case self::MESSAGE_TO_DIALOG:
                 $dialog = PoprigunChatDialog::getDialog($this->author_id, $this->receiverId);
                 if (null === $dialog) {
                     throw new \BadMethodCallException();
                 }
                 break;
             default:
                 throw new \BadMethodCallException();
         }
         $result = $dialog->addMessageToDialog($this->author_id, $this->message);
     } catch (\Exception $e) {
         $result = $e->getMessage();
     }
     return $result;
 }
コード例 #5
0
ファイル: ChatController.php プロジェクト: poprigun/chat
 /**
  * Delete dialog (change status)
  *
  * @param $dialogId
  * @return array
  */
 public function actionDeleteDialog($dialogId)
 {
     return ['status' => PoprigunChatDialog::deleteDialog(Chat::decodeDialogId($dialogId)) ? 'success' : 'error'];
 }