Commit 560b88a0c7f3d8ee898e891ec5c88c28861ff837

Authored by Administrator
1 parent 6a97773c

01.03.16

common/models/Chat.php
... ... @@ -75,6 +75,6 @@ class Chat extends \yii\db\ActiveRecord
75 75 */
76 76 public function getMessages()
77 77 {
78   - return $this->hasMany(Message::className(), ['chat_id' => 'chat_id'])->orderBy('message_id')->all() ;
  78 + return $this->hasMany(Message::className(), ['chat_id' => 'chat_id'])->orderBy('message_id') ;
79 79 }
80 80 }
... ...
common/models/File.php
... ... @@ -3,6 +3,7 @@
3 3 namespace common\models;
4 4  
5 5 use Yii;
  6 +use yii\web\UploadedFile;
6 7  
7 8 /**
8 9 * This is the model class for table "file".
... ... @@ -46,4 +47,24 @@ class File extends \yii\db\ActiveRecord
46 47 'dir' => 'Dir',
47 48 ];
48 49 }
  50 +
  51 + /**
  52 + * @param UploadedFile $file
  53 + * @return file id in model File
  54 + */
  55 +
  56 + public function saveFile(UploadedFile $file){
  57 + $imgDir = Yii::getAlias('@storage/'.'user_'.\Yii::$app->user->id.'/files/');
  58 + $uploadName = $file->baseName.'_'. time().'.'.$file->extension;
  59 + if(!is_dir($imgDir)) {
  60 + mkdir($imgDir, 0755, true);
  61 + }
  62 +
  63 + if($file->saveAs($imgDir.$uploadName)){
  64 + $this->dir = '/storage/user_'.\Yii::$app->user->id.'/files/'.$uploadName;
  65 + $this->name = $file->baseName.'.'.$file->extension;
  66 + $this->save();
  67 + return $this->file_id;
  68 + }
  69 + }
49 70 }
... ...
common/models/Message.php
... ... @@ -4,6 +4,7 @@ namespace common\models;
4 4  
5 5 use Yii;
6 6  
  7 +
7 8 /**
8 9 * This is the model class for table "message".
9 10 *
... ... @@ -19,6 +20,9 @@ use Yii;
19 20 */
20 21 class Message extends \yii\db\ActiveRecord
21 22 {
  23 +
  24 + public $file;
  25 +
22 26 /**
23 27 * @inheritdoc
24 28 */
... ... @@ -35,7 +39,7 @@ class Message extends \yii\db\ActiveRecord
35 39 return [
36 40 [['chat_id', 'user_id', 'status'], 'integer'],
37 41 [['text'], 'string'],
38   - [['date'], 'safe'],
  42 + [['date','file'], 'safe'],
39 43 [['files'], 'string', 'max' => 255],
40 44 [['chat_id'], 'exist', 'skipOnError' => true, 'targetClass' => Chat::className(), 'targetAttribute' => ['chat_id' => 'chat_id']],
41 45 ];
... ... @@ -65,6 +69,19 @@ class Message extends \yii\db\ActiveRecord
65 69 return $this->hasOne(Chat::className(), ['chat_id' => 'chat_id']);
66 70 }
67 71  
  72 + public function getUser()
  73 + {
  74 + return $this->hasOne(UserInfo::className(), ['user_id' => 'user_id']);
  75 + }
  76 +
  77 + public function getFilesList(){
  78 + $files = json_decode($this->files);
  79 + return File::findAll($files);
  80 +
  81 + }
  82 +
  83 +
  84 +
68 85 public function isMy(){
69 86 if($this->user_id == \Yii::$app->user->id){
70 87 return true;
... ...
frontend/controllers/ChatController.php
... ... @@ -2,6 +2,7 @@
2 2 namespace frontend\controllers;
3 3  
4 4 use common\models\Chat;
  5 +use common\models\File;
5 6 use common\models\Message;
6 7 use Yii;
7 8 use common\models\User;
... ... @@ -9,6 +10,7 @@ use yii\data\ActiveDataProvider;
9 10 use yii\filters\AccessControl;
10 11 use yii\web\Controller;
11 12 use yii\web\NotFoundHttpException;
  13 +use yii\web\UploadedFile;
12 14  
13 15  
14 16 /**
... ... @@ -54,7 +56,9 @@ class ChatController extends Controller
54 56 'or',
55 57 ['from_user'=> $user->id,],
56 58 ['to_user'=> $user->id,],
57   - ])->one();
  59 + ])
  60 + ->with('messages.user')
  61 + ->one();
58 62 if(!$chat instanceof Chat){
59 63 $chat = new Chat();
60 64 $chat->from_user = $user->id;
... ... @@ -67,18 +71,48 @@ class ChatController extends Controller
67 71 $post = \Yii::$app->request->post();
68 72 if(isset($post)){
69 73  
70   -
71   -
72 74 $message = new Message();
73 75  
74   -
75 76 if($message->load($post, 'Message')){
76 77  
77 78 $message->chat_id = $chat->chat_id;
78 79 $message->user_id = $user->id;
  80 +
  81 + $message->file = UploadedFile::getInstances($message, 'file');
  82 +
  83 + if(!empty($message->file)) {
  84 +
  85 + if(is_array($message->file)){
  86 +
  87 + foreach($message->file as $file){
  88 +
  89 + if($file instanceof UploadedFile){
  90 +
  91 + $file_model = new File();
  92 + $file_id[] = $file_model->saveFile($file);
  93 +
  94 + }
  95 +
  96 + }
  97 +
  98 + } else {
  99 +
  100 + if($message->file instanceof UploadedFile){
  101 +
  102 + $file_model = new File();
  103 + $file_id[] = $file_model->saveFile($message->file);
  104 + }
  105 +
  106 + }
  107 +
  108 + $message->files = json_encode($file_id);
  109 + }
  110 +
  111 +
79 112 $message->save();
80 113  
81 114 return $this->redirect(['chat/message', 'user_id'=>$user_id]);
  115 +
82 116 }
83 117 }
84 118  
... ...
frontend/views/chat/message.php
1 1 <?php
2 2 use common\models\Message;
3 3 use common\models\Option;
  4 +use yii\helpers\Html;
4 5 use yii\widgets\ActiveForm;
5 6  
6 7 $this->registerJsFile("/js/forms.js");
... ... @@ -93,27 +94,35 @@ $this-&gt;registerJsFile(&quot;/js/forms.js&quot;);
93 94 <?php foreach($chat->messages as $message):?>
94 95 <?php if($message->isMy()):?>
95 96 <div class="comment right">
96   - <div class="author_pic"><img src="/images/ded-ico.png"></div>
  97 + <div class="author_pic"><?= Html::img($message->user->minImg($message->user->image,48,48))?></div>
97 98 <div class="comment_text">
98 99 <?= $message->text ?>
99 100 <div class="comment_time">
100   - 26.11.15<br>
101   - 18:00
  101 + <?= $message->date ?>
102 102 </div>
103 103 </div>
104   - <div class="offer_link"><a href="#">Коммерческое предложение</a></div>
  104 + <?php if($message->filesList):?>
  105 + <?php foreach($message->filesList as $file): ?>
  106 + <div class="offer_link">
  107 + <?= Html::a($file->name, $file->dir, ['target' => '_blank']);?></div>
  108 + <?php endforeach; ?>
  109 + <?php endif; ?>
105 110 <div style="clear:both;"></div>
106 111 </div>
107 112 <?php else: ?>
108 113 <div class="comment left">
109   - <div class="author_pic"><img src="/images/ded-ico.png"></div>
  114 + <div class="author_pic"><?= Html::img($message->user->minImg($message->user->image,48,48))?></div>
110 115 <div class="comment_text">
111 116 <?= $message->text ?>
112 117 <div class="comment_time">
113   - 25.11.15<br>
114   - 15:00
  118 + <?= $message->date ?>
115 119 </div>
116 120 </div>
  121 + <?php if($message->filesList):?>
  122 + <?php foreach($message->filesList as $file): ?>
  123 + <div class="offer_link"> <?= Html::a($file->name, $file->dir, ['target' => '_blank']);?></div>
  124 + <?php endforeach; ?>
  125 + <?php endif; ?>
117 126 <div style="clear:both;"></div>
118 127 </div>
119 128 <?php endif;?>
... ... @@ -122,15 +131,19 @@ $this-&gt;registerJsFile(&quot;/js/forms.js&quot;);
122 131 </div>
123 132 </div>
124 133 <div class="comment_type">
125   - <?php $form = ActiveForm::begin(['method'=> 'post']); ?>
  134 + <?php $form = ActiveForm::begin(['method'=> 'post','options' =>['enctype'=>'multipart/form-data']]); ?>
126 135  
127 136 <?= $form->field(new Message(), 'text')
128 137 ->textarea(['class'=> 'message_text']); ?>
129   - <input type="submit" class="send_mess_but" value="Отправить">
  138 + <input type="submit" class="send_mess_but" value="Отправить">
130 139 <div class="inputfile">
131   - <div class="file_input_title">Прикрепить файл</div>
132   - <input type="file" class="input_file">
133   - <div class="input_file_text">Максимальный размер файла 5 МБ</div>
  140 + <div class="tender-file-wr">
  141 + <?= $form->field(new Message(), 'file[]')
  142 + ->fileInput(['class'=>'multi'])
  143 + ->label(false)?>
  144 + <a href="#" class="addfilemulti">Прикрепить файл</a>
  145 + <div class="max-size">Максимальный размер файла 5 МБ</div>
  146 + </div>
134 147 </div>
135 148 <?php $form::end(); ?>
136 149 </div>
... ...
frontend/web/css/style.css
... ... @@ -4112,7 +4112,7 @@ ul.min_markers_two
4112 4112 /***js_scroll***/
4113 4113 .jspContainer{overflow:hidden;position:relative}.jspPane{position:absolute}.jspVerticalBar{position:absolute;top:0;right:0;width:16px;height:100%;background:red}.jspHorizontalBar{position:absolute;bottom:0;left:0;width:100%;height:16px;background:red}.jspCap{display:none}.jspHorizontalBar .jspCap{float:left}.jspTrack{background:#dde;position:relative}.jspDrag{background:#bbd;position:relative;top:0;left:0;cursor:pointer}.jspHorizontalBar .jspTrack,.jspHorizontalBar .jspDrag{float:left;height:100%}.jspArrow{background:#50506d;text-indent:-20000px;display:block;cursor:pointer;padding:0;margin:0}.jspArrow.jspDisabled{cursor:default;background:#80808d}.jspVerticalBar .jspArrow{height:16px}.jspHorizontalBar .jspArrow{width:16px;float:left;height:100%}.jspVerticalBar .jspArrow:focus{outline:none}.jspCorner{background:#eeeef4;float:left;height:100%}* html .jspCorner{margin:0 -3px 0 0}
4114 4114 /*** message-read***/
4115   -.comments_block{width:938px;height:498px;background-color:#fcfcfc;border:1px solid #b7b7b7;border-right:none;margin-bottom:57px;color:#333;font-family:Roboto;font-size:13px;position:relative;overflow:hidden;outline:none}.comment_type{width:940px}.comment_type .pole{width:660px;margin:0 auto;position:relative}.comment_type .mes_title{font-size:18px;color:#333;margin:18px 0}.message_text{width:638px;height:78px;resize:none;border:1px solid #c2c2c2;margin:0 auto;margin-bottom:26px;margin-top:18px;padding:10px;font-family:Roboto}.send_mess_but{outline:none;border:none;cursor:pointer;font-size:12px!important;padding-top:2px;width:170px;height:43px;text-align:center;text-transform:uppercase;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr = '#148ad6',endColorstr = '#0072bc');-ms-filter:"progid:DXImageTransform.Microsoft.gradient(startColorstr = '#148ad6',endColorstr = '#0072bc')";background-image:-moz-linear-gradient(top,#148ad6,#0072bc);background-image:-ms-linear-gradient(top,#148ad6,#0072bc);background-image:-o-linear-gradient(top,#148ad6,#0072bc);background-image:-webkit-gradient(linear,center top,center bottom,from(#148ad6),to(#0072bc));background-image:-webkit-linear-gradient(top,#148ad6,#0072bc);background-image:linear-gradient(top,#148ad6,#0072bc);color:#fff;border-bottom:2px solid #0054a6;border-radius:4px;transition:.1s;top:170px;line-height:3.5}.send_mess_but:hover{opacity:.9}.send_mess_but:active{line-height:3.7}.pick_file{position:absolute;top:170px;right:0;width:130px}.pick_file .link:after{content:'';background:url(/images/skrpk.png) left center no-repeat;position:absolute;width:19px;height:21px;left:-28px;top:0}.pick_file .link{text-transform:uppercase;color:#0276c0;font-size:13px;cursor:pointer;text-decoration:none;border-bottom:1px solid #70b2db;position:relative}.pick_file .capt{color:#c2c2c2;font-size:11px;line-height:16px;margin-top:3px}.comment_text{position:relative;padding:12px 13px;width:602px;border:1px solid #c2c2c2;min-height:74px;left:10px;line-height:1.3;background-color:#fff}.comment.left .comment_text{float:left}.comment.right .comment_text{float:right}.comment.left .comment_text:before{content:'';background:url(/images/mes_bord_left.png);position:absolute;width:11px;height:11px;top:-1px;left:-11px}.comment.right .comment_text:before{content:'';background:url(/images/mes_bord_right.png);position:absolute;width:11px;height:11px;top:-1px;right:-11px}.comment.left .author_pic{float:left;margin:0 10px}.comment.right .author_pic{float:right;margin:0 25px 0 30px}.comment_time{color:#c6c6c6;font-family:Roboto;font-size:13px;font-weight:600;line-height:32px;position:absolute}.comment.left .comment_time{top:17px;right:-64px}.comment.right .comment_time{top:18px;left:-64px}.comment{position:relative;margin:30px 21px 30px 1px}.comment .offer_link a{color:#0173bd;position:relative}.comment .offer_link a:before{content:'';position:absolute;background:url(/images/skrpk_sm.png) left center no-repeat;width:19px;height:21px;left:-26px;top:-3px}.comment.right .offer_link{float:right;margin-right:94px;margin-top:12px;margin-bottom:-17px}.comment.left .offer_link{left:104px;bottom:-27px}.comment_block .content{overflow:auto;width:938px;height:498px}.fileform{background-color:#FFF;border:1px solid #CCC;border-radius:2px;cursor:pointer;height:26px;overflow:hidden;padding:2px;position:relative;text-align:left;vertical-align:middle;width:230px}.comment_type form{width:660px;margin:0 auto}.comment_type form label{color:#333;font-family:Roboto;font-size:18px}.input_file_text{color:#c2c2c2;font-size:11px;font-family:Roboto;width:125px;position:absolute;right:0;line-height:16px;top:18px;overflow:hidden;text-overflow:ellipsis;white-space:pre-line}.inputfile{float:right;position:relative;width:153px;padding-bottom:10px;background:url(/images/skrpk.png);background-repeat:no-repeat}.input_file{opacity:0;cursor:pointer;width:160px}.inputfile .file_input_title{position:absolute;color:#0276c0;border-bottom:1px solid #0276c0;text-transform:uppercase;font-family:Roboto;font-size:13px;right:0}.jspArrow{background-color:#0173bd;width:20px;height:20px}.jspVerticalBar{width:20px}.jspVerticalBar .jspArrow{height:20px}.jspTrack{background:#ebebeb;position:relative}.jspDrag{background:#c2c2c2;position:relative;top:0;left:0;cursor:pointer;border-left:2px solid #ebebeb;border-right:2px solid #ebebeb}.jspVerticalBar{background:#ebebeb}.jspArrowUp{background:#0173bd url(/images/scroll_up.png) center center no-repeat}.jspArrowUp:hover{opacity:.8}.jspArrowUp:active{background-position-y:1px!important}.jspDisabled.jspArrowUp{background:#80808d url(/images/scroll_up_grey.png) center center no-repeat}.jspArrowDown{background:#0173bd url(/images/scroll_down.png) center center no-repeat}.jspArrowDown:hover{opacity:.8}.jspArrowDown:active{background-position-y:1px!important}.jspArrowDown{background:#0173bd url(/images/scroll_down.png) center center no-repeat}.jspDisabled.jspArrowDown{background:#80808d url(/images/scroll_down_grey.png) center center no-repeat}.jspContainer{outline:none;border:none}
  4115 +.comments_block{width:938px;height:498px;background-color:#fcfcfc;border:1px solid #b7b7b7;border-right:none;margin-bottom:57px;color:#333;font-family:Roboto;font-size:13px;position:relative;overflow:hidden;outline:none}.comment_type{width:940px}.comment_type .pole{width:660px;margin:0 auto;position:relative}.comment_type .mes_title{font-size:18px;color:#333;margin:18px 0}.message_text{width:638px;height:78px;resize:none;border:1px solid #c2c2c2;margin:0 auto;margin-bottom:26px;margin-top:18px;padding:10px;font-family:Roboto}.send_mess_but{outline:none;border:none;cursor:pointer;font-size:12px!important;padding-top:2px;width:170px;height:43px;text-align:center;text-transform:uppercase;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr = '#148ad6',endColorstr = '#0072bc');-ms-filter:"progid:DXImageTransform.Microsoft.gradient(startColorstr = '#148ad6',endColorstr = '#0072bc')";background-image:-moz-linear-gradient(top,#148ad6,#0072bc);background-image:-ms-linear-gradient(top,#148ad6,#0072bc);background-image:-o-linear-gradient(top,#148ad6,#0072bc);background-image:-webkit-gradient(linear,center top,center bottom,from(#148ad6),to(#0072bc));background-image:-webkit-linear-gradient(top,#148ad6,#0072bc);background-image:linear-gradient(top,#148ad6,#0072bc);color:#fff;border-bottom:2px solid #0054a6;border-radius:4px;transition:.1s;top:170px;line-height:3.5}.send_mess_but:hover{opacity:.9}.send_mess_but:active{line-height:3.7}.pick_file{position:absolute;top:170px;right:0;width:130px}.pick_file .link:after{content:'';background:url(/images/skrpk.png) left center no-repeat;position:absolute;width:19px;height:21px;left:-28px;top:0}.pick_file .link{text-transform:uppercase;color:#0276c0;font-size:13px;cursor:pointer;text-decoration:none;border-bottom:1px solid #70b2db;position:relative}.pick_file .capt{color:#c2c2c2;font-size:11px;line-height:16px;margin-top:3px}.comment_text{position:relative;padding:12px 13px;width:602px;border:1px solid #c2c2c2;min-height:74px;left:10px;line-height:1.3;background-color:#fff}.comment.left .comment_text{float:left}.comment.right .comment_text{float:right}.comment.left .comment_text:before{content:'';background:url(/images/mes_bord_left.png);position:absolute;width:11px;height:11px;top:-1px;left:-11px}.comment.right .comment_text:before{content:'';background:url(/images/mes_bord_right.png);position:absolute;width:11px;height:11px;top:-1px;right:-11px}.comment.left .author_pic{float:left;margin:0 10px}.comment.right .author_pic{float:right;margin:0 25px 0 30px}.comment_time{color:#c6c6c6;font-family:Roboto;font-size:13px;font-weight:600;line-height:32px;position:absolute}.comment.left .comment_time{top: 17px; right: -120px; width: 100px;}.comment.right .comment_time{ top: 18px; left: -88px; width: 100px;}.comment{position:relative;margin:30px 21px 30px 1px}.comment .offer_link a{color:#0173bd;position:relative}.comment .offer_link a:before{content:'';position:absolute;background:url(/images/skrpk_sm.png) left center no-repeat;width:19px;height:21px;left:-26px;top:-3px}.comment.right .offer_link{float:right;margin-right:94px;margin-top:12px;margin-bottom:-17px}.comment.left .offer_link{left:104px;bottom:-27px}.comment_block .content{overflow:auto;width:938px;height:498px}.fileform{background-color:#FFF;border:1px solid #CCC;border-radius:2px;cursor:pointer;height:26px;overflow:hidden;padding:2px;position:relative;text-align:left;vertical-align:middle;width:230px}.comment_type form{width:660px;margin:0 auto}.comment_type form label{color:#333;font-family:Roboto;font-size:18px}.input_file_text{color:#c2c2c2;font-size:11px;font-family:Roboto;width:125px;position:absolute;right:0;line-height:16px;top:18px;overflow:hidden;text-overflow:ellipsis;white-space:pre-line}.inputfile{float:right;position:relative;width:153px;padding-bottom:10px;background-repeat:no-repeat}.input_file{opacity:0;cursor:pointer;width:160px}.inputfile .file_input_title{position:absolute;color:#0276c0;border-bottom:1px solid #0276c0;text-transform:uppercase;font-family:Roboto;font-size:13px;right:0}.jspArrow{background-color:#0173bd;width:20px;height:20px}.jspVerticalBar{width:20px}.jspVerticalBar .jspArrow{height:20px}.jspTrack{background:#ebebeb;position:relative}.jspDrag{background:#c2c2c2;position:relative;top:0;left:0;cursor:pointer;border-left:2px solid #ebebeb;border-right:2px solid #ebebeb}.jspVerticalBar{background:#ebebeb}.jspArrowUp{background:#0173bd url(/images/scroll_up.png) center center no-repeat}.jspArrowUp:hover{opacity:.8}.jspArrowUp:active{background-position-y:1px!important}.jspDisabled.jspArrowUp{background:#80808d url(/images/scroll_up_grey.png) center center no-repeat}.jspArrowDown{background:#0173bd url(/images/scroll_down.png) center center no-repeat}.jspArrowDown:hover{opacity:.8}.jspArrowDown:active{background-position-y:1px!important}.jspArrowDown{background:#0173bd url(/images/scroll_down.png) center center no-repeat}.jspDisabled.jspArrowDown{background:#80808d url(/images/scroll_down_grey.png) center center no-repeat}.jspContainer{outline:none;border:none}
4116 4116 /***form-order***/
4117 4117 #modal_form_offer {height: 505px;}
4118 4118 #modal_form_offer,#modal_form_question, #modal_form_question{margin-top: 70px;}
... ...