Commit af5c9885551c091ed34d9a7648d7192fe70e1850
1 parent
9b93120e
Fixes
Showing
3 changed files
with
183 additions
and
145 deletions
Show diff stats
frontend/controllers/ChatController.php
1 | 1 | <?php |
2 | -namespace frontend\controllers; | |
3 | - | |
4 | -use common\models\Chat; | |
5 | -use common\models\Fields; | |
6 | -use common\models\File; | |
7 | -use common\models\Message; | |
8 | -use Yii; | |
9 | -use common\models\User; | |
10 | -use yii\data\ActiveDataProvider; | |
11 | -use yii\filters\AccessControl; | |
12 | -use yii\helpers\ArrayHelper; | |
13 | -use yii\web\Controller; | |
14 | -use yii\web\NotFoundHttpException; | |
15 | -use yii\web\UploadedFile; | |
16 | - | |
17 | - | |
18 | -/** | |
19 | - * Site controller | |
20 | - */ | |
21 | -class ChatController extends Controller | |
22 | -{ | |
23 | - public $defaultAction = 'list'; | |
24 | - | |
25 | - public function behaviors() | |
2 | + namespace frontend\controllers; | |
3 | + | |
4 | + use common\models\Chat; | |
5 | + use common\models\Fields; | |
6 | + use common\models\File; | |
7 | + use common\models\Message; | |
8 | + use Yii; | |
9 | + use common\models\User; | |
10 | + use yii\data\ActiveDataProvider; | |
11 | + use yii\db\ActiveQuery; | |
12 | + use yii\filters\AccessControl; | |
13 | + use yii\helpers\ArrayHelper; | |
14 | + use yii\web\Controller; | |
15 | + use yii\web\NotFoundHttpException; | |
16 | + use yii\web\UploadedFile; | |
17 | + | |
18 | + /** | |
19 | + * Site controller | |
20 | + */ | |
21 | + class ChatController extends Controller | |
26 | 22 | { |
27 | - return [ | |
28 | - 'access' => [ | |
29 | - 'class' => AccessControl::className(), | |
30 | - 'rules' => [ | |
31 | - [ | |
32 | - 'actions' => ['list', 'message', 'message-save'], | |
33 | - 'allow' => true, | |
34 | - 'roles' => ['@'], | |
23 | + public $defaultAction = 'list'; | |
24 | + | |
25 | + public function behaviors() | |
26 | + { | |
27 | + return [ | |
28 | + 'access' => [ | |
29 | + 'class' => AccessControl::className(), | |
30 | + 'rules' => [ | |
31 | + [ | |
32 | + 'actions' => [ | |
33 | + 'list', | |
34 | + 'message', | |
35 | + 'message-save', | |
36 | + ], | |
37 | + 'allow' => true, | |
38 | + 'roles' => [ '@' ], | |
39 | + ], | |
35 | 40 | ], |
36 | 41 | ], |
37 | - ], | |
38 | - ]; | |
39 | - } | |
40 | - | |
41 | - | |
42 | - public function actionList() | |
43 | - { | |
44 | - $user = \Yii::$app->user->identity; | |
45 | - $chat = Chat::find() | |
46 | - ->where([ | |
47 | - 'or', | |
48 | - ['from_user' => $user->id,], | |
49 | - ['to_user' => $user->id,], | |
50 | - ]) | |
51 | - | |
52 | - ->with('messages.user'); | |
53 | - | |
54 | - $chat = new ActiveDataProvider([ | |
55 | - 'query' => $chat, | |
56 | - 'pagination' => [ | |
57 | - 'pageSize' => 5, | |
58 | - ], | |
59 | - ]); | |
60 | - | |
61 | - return $this->render('list',[ | |
62 | - 'chat' => $chat | |
63 | - ]); | |
64 | - } | |
65 | - | |
66 | - public function actionMessage($user_id) | |
67 | - { | |
68 | - $user = \Yii::$app->user->identity; | |
69 | - | |
70 | - $chat = Chat::find() | |
71 | - ->where([ | |
72 | - 'or', | |
73 | - ['from_user' => $user_id,], | |
74 | - ['to_user' => $user_id,], | |
75 | - ]) | |
76 | - ->andWhere([ | |
77 | - 'or', | |
78 | - ['from_user'=> $user->id,], | |
79 | - ['to_user'=> $user->id,], | |
80 | - ]) | |
81 | - ->with('messages.user') | |
82 | - ->one(); | |
83 | - if(!$chat instanceof Chat){ | |
84 | - $chat = new Chat(); | |
85 | - $chat->from_user = $user->id; | |
86 | - $chat->to_user = $user_id; | |
87 | - $chat->save(); | |
42 | + ]; | |
43 | + } | |
44 | + | |
45 | + public function actionList() | |
46 | + { | |
47 | + $user = \Yii::$app->user->identity; | |
48 | + $chat = Chat::find() | |
49 | + ->where( | |
50 | + [ | |
51 | + 'or', | |
52 | + [ 'from_user' => $user->id, ], | |
53 | + [ 'to_user' => $user->id, ], | |
54 | + ] | |
55 | + ) | |
56 | + ->with('messages.user'); | |
57 | + | |
58 | + $chat = new ActiveDataProvider( | |
59 | + [ | |
60 | + 'query' => $chat, | |
61 | + 'pagination' => [ | |
62 | + 'pageSize' => 5, | |
63 | + ], | |
64 | + ] | |
65 | + ); | |
66 | + | |
67 | + return $this->render( | |
68 | + 'list', | |
69 | + [ | |
70 | + 'chat' => $chat, | |
71 | + ] | |
72 | + ); | |
88 | 73 | } |
89 | - | |
90 | - $phones = Fields::getData($chat->interlocutor->id, User::className(), 'phone'); | |
91 | - $sites = Fields::getData($chat->interlocutor->id, User::className(), 'site'); | |
92 | - | |
93 | - | |
94 | - $post = \Yii::$app->request->post(); | |
95 | - if(isset($post)){ | |
96 | - | |
97 | - $message = new Message(); | |
98 | - | |
99 | - if($message->load($post, 'Message')){ | |
100 | - | |
101 | - $message->chat_id = $chat->chat_id; | |
102 | - $message->user_id = $user->id; | |
103 | - | |
104 | - $message->file = UploadedFile::getInstances($message, 'file'); | |
105 | - | |
106 | - if(!empty($message->file)) { | |
107 | - | |
108 | - if(is_array($message->file)){ | |
109 | - | |
110 | - foreach($message->file as $file){ | |
111 | - | |
112 | - if($file instanceof UploadedFile){ | |
113 | - | |
74 | + | |
75 | + public function actionMessage($user_id) | |
76 | + { | |
77 | + $user = \Yii::$app->user->identity; | |
78 | + if ($user->getId() == $user_id) { | |
79 | + return $this->redirect([ 'chat/list' ]); | |
80 | + } | |
81 | + | |
82 | + $chat = Chat::find() | |
83 | + ->where( | |
84 | + [ | |
85 | + 'or', | |
86 | + [ 'from_user' => $user_id, ], | |
87 | + [ 'to_user' => $user_id, ], | |
88 | + ] | |
89 | + ) | |
90 | + ->andWhere( | |
91 | + [ | |
92 | + 'or', | |
93 | + [ 'from_user' => $user->id, ], | |
94 | + [ 'to_user' => $user->id, ], | |
95 | + ] | |
96 | + ) | |
97 | + ->with( | |
98 | + [ | |
99 | + 'messages' => function ($query) { | |
100 | + /** | |
101 | + * @var ActiveQuery $query | |
102 | + */ | |
103 | + $query->orderBy([ 'date' => SORT_DESC ]) | |
104 | + ->with('user'); | |
105 | + }, | |
106 | + ] | |
107 | + ) | |
108 | + ->one(); | |
109 | + if (!$chat instanceof Chat) { | |
110 | + $chat = new Chat(); | |
111 | + $chat->from_user = $user->id; | |
112 | + $chat->to_user = $user_id; | |
113 | + $chat->save(); | |
114 | + } | |
115 | + | |
116 | + $phones = Fields::getData($chat->interlocutor->id, User::className(), 'phone'); | |
117 | + $sites = Fields::getData($chat->interlocutor->id, User::className(), 'site'); | |
118 | + | |
119 | + $post = \Yii::$app->request->post(); | |
120 | + if (isset( $post )) { | |
121 | + | |
122 | + $message = new Message(); | |
123 | + | |
124 | + if ($message->load($post, 'Message')) { | |
125 | + | |
126 | + $message->chat_id = $chat->chat_id; | |
127 | + $message->user_id = $user->id; | |
128 | + | |
129 | + $message->file = UploadedFile::getInstances($message, 'file'); | |
130 | + | |
131 | + if (!empty( $message->file )) { | |
132 | + | |
133 | + if (is_array($message->file)) { | |
134 | + | |
135 | + foreach ($message->file as $file) { | |
136 | + | |
137 | + if ($file instanceof UploadedFile) { | |
138 | + | |
139 | + $file_model = new File(); | |
140 | + $file_id[] = $file_model->saveFile($file); | |
141 | + | |
142 | + } | |
143 | + | |
144 | + } | |
145 | + | |
146 | + } else { | |
147 | + | |
148 | + if ($message->file instanceof UploadedFile) { | |
149 | + | |
114 | 150 | $file_model = new File(); |
115 | - $file_id[] = $file_model->saveFile($file); | |
116 | - | |
151 | + $file_id[] = $file_model->saveFile($message->file); | |
117 | 152 | } |
118 | - | |
119 | - } | |
120 | - | |
121 | - } else { | |
122 | - | |
123 | - if($message->file instanceof UploadedFile){ | |
124 | - | |
125 | - $file_model = new File(); | |
126 | - $file_id[] = $file_model->saveFile($message->file); | |
153 | + | |
127 | 154 | } |
128 | - | |
155 | + | |
156 | + $message->files = json_encode($file_id); | |
129 | 157 | } |
130 | - | |
131 | - $message->files = json_encode($file_id); | |
158 | + | |
159 | + $message->save(); | |
160 | + | |
161 | + return $this->redirect( | |
162 | + [ | |
163 | + 'chat/message', | |
164 | + 'user_id' => $user_id, | |
165 | + ] | |
166 | + ); | |
167 | + | |
132 | 168 | } |
133 | - | |
134 | - | |
135 | - $message->save(); | |
136 | - | |
137 | - return $this->redirect(['chat/message', 'user_id'=>$user_id]); | |
138 | - | |
139 | 169 | } |
170 | + | |
171 | + return $this->render( | |
172 | + 'message', | |
173 | + [ | |
174 | + 'chat' => $chat, | |
175 | + 'user_id' => $user_id, | |
176 | + 'phones' => $phones, | |
177 | + 'sites' => $sites, | |
178 | + ] | |
179 | + ); | |
140 | 180 | } |
141 | - | |
142 | - return $this->render('message',[ | |
143 | - 'chat' => $chat, | |
144 | - 'user_id' => $user_id, | |
145 | - 'phones' => $phones, | |
146 | - 'sites' => $sites, | |
147 | - ]); | |
181 | + | |
148 | 182 | } |
149 | - | |
150 | -} | ... | ... |
frontend/views/chat/message.php
... | ... | @@ -41,14 +41,20 @@ |
41 | 41 | </div> |
42 | 42 | <div class="cab-mes-read-cont-com"><?= count($chat->interlocutor->comments) ?> мнений</div> |
43 | 43 | <div class="cab-mes-read-cont-soc"> |
44 | - <?= Html::a(Html::img('/images/ico-fb.png'), "{$chat->interlocutor->userInfo->social_fb}", [ 'target' => '_blank' ]) ?> | |
45 | - | |
46 | - <?= Html::a(Html::img('/images/ico-tw.png'), "{$chat->interlocutor->userInfo->social_t}", [ 'target' => '_blank' ]) ?> | |
47 | - | |
48 | - <?= Html::a(Html::img('/images/ico-in.png'), "{$chat->interlocutor->userInfo->social_in}", [ 'target' => '_blank' ]) ?> | |
49 | - | |
50 | - <?= Html::a(Html::img('/images/ico-vk.png'), "{$chat->interlocutor->userInfo->social_vk}", [ 'target' => '_blank' ]) ?> | |
51 | - | |
44 | + <?php | |
45 | + if(!empty($chat->interlocutor->userInfo->social_fb)) { | |
46 | + echo Html::a(Html::img('/images/ico-fb.png'), "{$chat->interlocutor->userInfo->social_fb}", [ 'target' => '_blank' ]); | |
47 | + } | |
48 | + if(!empty($chat->interlocutor->userInfo->social_t)) { | |
49 | + echo Html::a(Html::img('/images/ico-tw.png'), "{$chat->interlocutor->userInfo->social_t}", [ 'target' => '_blank' ]); | |
50 | + } | |
51 | + if(!empty($chat->interlocutor->userInfo->social_in)) { | |
52 | + echo Html::a(Html::img('/images/ico-in.png'), "{$chat->interlocutor->userInfo->social_in}", [ 'target' => '_blank' ]); | |
53 | + } | |
54 | + if(!empty($chat->interlocutor->userInfo->social_vk)) { | |
55 | + echo Html::a(Html::img('/images/ico-vk.png'), "{$chat->interlocutor->userInfo->social_vk}", [ 'target' => '_blank' ]); | |
56 | + } | |
57 | + ?> | |
52 | 58 | </div> |
53 | 59 | </div> |
54 | 60 | <div class="performance-vacancy-add-favorite"> | ... | ... |