Blame view

common/models/Chat.php 2.82 KB
6a97773c   Administrator   01.03.16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  <?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
  {
9fd076e8   Administrator   01.03.16
22
23
      public $newMessage;
  
6a97773c   Administrator   01.03.16
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
      /**
       * @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 [
06ec2844   Administrator   28.03.16
51
52
53
54
55
              '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'),
6a97773c   Administrator   01.03.16
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
          ];
      }
  
      /**
       * @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()
      {
b5a5d629   Alex Savenko   message update fix
80
          return $this->hasMany(Message::className(), ['chat_id' => 'chat_id'])->orderBy('message_id') ;
9fd076e8   Administrator   01.03.16
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  
      }
  
  
      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(){
32fd32d6   Administrator   16.03.16
95
  
9fd076e8   Administrator   01.03.16
96
          if($this->from_user == \Yii::$app->user->id){
7d705d85   Yarik   test
97
              return $this->hasOne(User::className(), ['id' => 'to_user']);
9fd076e8   Administrator   01.03.16
98
          } else {
7d705d85   Yarik   test
99
              return $this->hasOne(User::className(), ['id' => 'from_user']);
9fd076e8   Administrator   01.03.16
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
          }
      }
  
      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;
          }
6a97773c   Administrator   01.03.16
115
      }
9fd076e8   Administrator   01.03.16
116
117
  
  
6a97773c   Administrator   01.03.16
118
  }