Blame view

common/models/Chat.php 2.93 KB
14a09168   Alex Savenko   init commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  <?php

  

  namespace common\models;

  

  use Yii;

  

  /**

   * This is the model class for table "chat".

   *

   * @property integer $chat_id

   * @property integer $status

   * @property string $comment

   * @property integer $from_user

   * @property integer $to_user

   *

   * @property User $fromUser

   * @property User $toUser

   * @property Message[] $messages

   */

  class Chat extends \yii\db\ActiveRecord

  {

      public $newMessage;

  

      /**

       * @inheritdoc

       */

      public static function tableName()

      {

          return 'chat';

      }

  

      /**

       * @inheritdoc

       */

      public function rules()

      {

          return [

              [['status', 'from_user', 'to_user'], 'integer'],

              [['comment'], 'string'],

              [['from_user'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['from_user' => 'id']],

              [['to_user'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['to_user' => 'id']],

          ];

      }

  

      /**

       * @inheritdoc

       */

      public function attributeLabels()

      {

          return [

              'chat_id' => Yii::t('app', 'chat_id'),

              'status' => Yii::t('app', 'status'),

              'comment' => Yii::t('app', 'comment'),

              'from_user' => Yii::t('app', 'from_user'),

              'to_user' => Yii::t('app', 'to_user'),

          ];

      }

  

      /**

       * @return \yii\db\ActiveQuery

       */

      public function getFromUser()

      {

          return $this->hasOne(User::className(), ['id' => 'from_user']);

      }

  

      /**

       * @return \yii\db\ActiveQuery

       */

      public function getToUser()

      {

          return $this->hasOne(User::className(), ['id' => 'to_user']);

      }

  

      /**

       * @return \yii\db\ActiveQuery

       */

      public function getMessages()

      {

          return $this->hasMany(Message::className(), ['chat_id' => 'chat_id'])->orderBy('message_id') ;

  

      }

  

  

      public function getAllMessages(){

          $messages = $this->hasMany(Message::className(), ['chat_id' => 'chat_id'])->orderBy('message_id');

          $messages_clone = clone($messages);

          Message::updateAll(['status' => 0], ['message_id' => $messages->select(['message_id'])->where(['status' => 1])->column()]);

  

          return $messages_clone;

      }

  

  

      public function getInterlocutor(){

  

          if($this->from_user == \Yii::$app->user->id){

              return $this->hasOne(User::className(), ['id' => 'to_user']);

          } else {

              return $this->hasOne(User::className(), ['id' => 'from_user']);

          }

      }

  

      public function hasNewMessage(){

          $newMessages = $this->getMessages()->where(['status'=>'1', ])

              ->andWhere(['<>','user_id', \Yii::$app->user->id])->all();

          if($newMessages){

  

              $this->newMessage = $newMessages;

  

              return true;

  

          } else {

              return false;

          }

      }

  

  

  }