-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathChatRoom.php
101 lines (85 loc) · 2.84 KB
/
ChatRoom.php
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
<?php
/**
* @link https://github.com/sintret/yii2-chat-adminlte
* @copyright Copyright (c) 2015 Andy fitria <[email protected]>
* @license MIT
*/
namespace sintret\chat;
use Yii;
use yii\base\Widget;
use sintret\chat\models\Chat;
/**
* @author Andy Fitria <[email protected]>
*/
class ChatRoom extends Widget {
public $sourcePath = '@vendor/sintret/yii2-chat-adminlte/assets';
public $css = [
];
public $js = [ // Configured conditionally (source/minified) during init()
'js/chat.js',
];
public $depends = [
'yii\web\JqueryAsset',
];
public $models;
public $url;
public $userModel;
public $userField;
public $model;
public $loadingImage;
public function init() {
$this->model = new Chat();
if ($this->userModel === NULL) {
$this->userModel = Yii::$app->getUser()->identityClass;
}
$this->model->userModel = $this->userModel;
if ($this->userField === NULL) {
$this->userField = 'avatarImage';
}
$this->model->userField = $this->userField;
Yii::$app->assetManager->publish("@vendor/sintret/yii2-chat-adminlte/assets/img/loadingAnimation.gif");
$this->loadingImage = Yii::$app->assetManager->getPublishedUrl("@vendor/sintret/yii2-chat-adminlte/assets/img/loadingAnimation.gif");
parent::init();
}
public function run() {
parent::init();
ChatJs::register($this->view);
$model = new Chat();
$model->userModel = $this->userModel;
$model->userField = $this->userField;
$data = $model->data();
return $this->render('index', [
'data' => $data,
'url' => $this->url,
'userModel' => $this->userModel,
'userField' => $this->userField,
'loading' => $this->loadingImage
]);
}
public static function sendChat($post) {
if (isset($post['message']))
$message = $post['message'];
if (isset($post['userfield']))
$userField = $post['userfield'];
if (isset($post['model']))
$userModel = $post['model'];
else
$userModel = Yii::$app->getUser()->identityClass;
$model = new \sintret\chat\models\Chat;
$model->userModel = $userModel;
if ($userField)
$model->userField = $userField;
if ($message) {
$model->message = $message;
$model->userId = Yii::$app->user->id;
if ($model->save()) {
echo $model->data();
} else {
print_r($model->getErrors());
exit(0);
}
} else {
echo $model->data();
}
}
}