Blame view

frontend/controllers/ChatController.php 3.62 KB
fbdb1f1c   Yarik   test
1
2
3
  <?php
  namespace frontend\controllers;
  
6a97773c   Administrator   01.03.16
4
  use common\models\Chat;
8c448b56   Administrator   add yii jquery
5
  use common\models\Fields;
560b88a0   Administrator   01.03.16
6
  use common\models\File;
6a97773c   Administrator   01.03.16
7
  use common\models\Message;
fbdb1f1c   Yarik   test
8
9
10
11
  use Yii;
  use common\models\User;
  use yii\data\ActiveDataProvider;
  use yii\filters\AccessControl;
06ec2844   Administrator   28.03.16
12
  use yii\helpers\ArrayHelper;
fbdb1f1c   Yarik   test
13
14
  use yii\web\Controller;
  use yii\web\NotFoundHttpException;
560b88a0   Administrator   01.03.16
15
  use yii\web\UploadedFile;
fbdb1f1c   Yarik   test
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
  
  /**
   * Site controller
   */
  class ChatController extends Controller
  {
      public $defaultAction = 'list';
  
      public function behaviors()
      {
          return [
              'access' => [
                  'class' => AccessControl::className(),
                  'rules' => [
                      [
6a97773c   Administrator   01.03.16
32
                          'actions' => ['list', 'message', 'message-save'],
fbdb1f1c   Yarik   test
33
34
35
36
37
38
39
40
41
42
43
                          'allow' => true,
                          'roles' => ['@'],
                      ],
                  ],
              ],
          ];
      }
  
  
      public function actionList()
      {
808969ba   Administrator   01.03.16
44
45
46
47
48
49
50
51
52
          $user = \Yii::$app->user->identity;
          $chat = Chat::find()
              ->where([
                  'or',
                  ['from_user' => $user->id,],
                  ['to_user' =>  $user->id,],
              ])
  
              ->with('messages.user');
9fd076e8   Administrator   01.03.16
53
54
  
          $chat = new ActiveDataProvider([
808969ba   Administrator   01.03.16
55
              'query' =>  $chat,
9fd076e8   Administrator   01.03.16
56
57
58
59
60
61
62
63
              'pagination' => [
                  'pageSize' => 5,
              ],
          ]);
  
          return $this->render('list',[
              'chat' => $chat
          ]);
fbdb1f1c   Yarik   test
64
65
      }
  
6a97773c   Administrator   01.03.16
66
      public function actionMessage($user_id)
fbdb1f1c   Yarik   test
67
      {
6a97773c   Administrator   01.03.16
68
69
70
71
72
73
74
75
76
77
78
79
          $user = \Yii::$app->user->identity;
  
          $chat = Chat::find()
              ->where([
              'or',
              ['from_user' => $user_id,],
              ['to_user' => $user_id,],
              ])
              ->andWhere([
                  'or',
                  ['from_user'=> $user->id,],
                  ['to_user'=>  $user->id,],
560b88a0   Administrator   01.03.16
80
81
82
              ])
              ->with('messages.user')
              ->one();
6a97773c   Administrator   01.03.16
83
84
85
86
87
          if(!$chat instanceof Chat){
              $chat = new Chat();
              $chat->from_user =  $user->id;
              $chat->to_user =  $user_id;
              $chat->save();
6a97773c   Administrator   01.03.16
88
89
          }
  
7d705d85   Yarik   test
90
91
          $phones = Fields::getData($chat->interlocutor->id, User::className(), 'phone');
          $sites = Fields::getData($chat->interlocutor->id, User::className(), 'site');
8c448b56   Administrator   add yii jquery
92
  
6a97773c   Administrator   01.03.16
93
94
95
96
  
          $post = \Yii::$app->request->post();
          if(isset($post)){
  
6a97773c   Administrator   01.03.16
97
98
              $message = new Message();
  
6a97773c   Administrator   01.03.16
99
100
101
102
              if($message->load($post, 'Message')){
  
                  $message->chat_id = $chat->chat_id;
                  $message->user_id = $user->id;
560b88a0   Administrator   01.03.16
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  
                  $message->file = UploadedFile::getInstances($message, 'file');
  
                  if(!empty($message->file)) {
  
                      if(is_array($message->file)){
  
                          foreach($message->file as $file){
  
                              if($file instanceof UploadedFile){
  
                                  $file_model = new File();
                                  $file_id[] = $file_model->saveFile($file);
  
                              }
  
                          }
  
                      } else {
  
                          if($message->file instanceof UploadedFile){
  
                              $file_model = new File();
                              $file_id[] = $file_model->saveFile($message->file);
                          }
  
                      }
  
                      $message->files = json_encode($file_id);
                  }
  
  
6a97773c   Administrator   01.03.16
135
136
137
                  $message->save();
  
                  return $this->redirect(['chat/message', 'user_id'=>$user_id]);
560b88a0   Administrator   01.03.16
138
  
6a97773c   Administrator   01.03.16
139
140
141
142
143
              }
          }
  
          return $this->render('message',[
              'chat' => $chat,
8c448b56   Administrator   add yii jquery
144
145
146
              'user_id' => $user_id,
              'phones' => $phones,
              'sites' => $sites,
6a97773c   Administrator   01.03.16
147
          ]);
fbdb1f1c   Yarik   test
148
      }
6a97773c   Administrator   01.03.16
149
  
fbdb1f1c   Yarik   test
150
  }