-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathMessageWidget.php
141 lines (119 loc) · 3.5 KB
/
MessageWidget.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
102
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
135
136
137
138
139
140
141
<?php
/**
* @link https://github.com/bubasuma/yii2-simplechat
* @copyright Copyright (c) 2015 bubasuma
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace bubasuma\simplechat;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\ListView;
/**
* Class MessageWidget
* @package bubasuma\simplechat
*
* @author Buba Suma <[email protected]>
* @since 1.0
*/
class MessageWidget extends ListView
{
/**
* @var array
*/
public $user;
/**
* @var array
*/
public $contact;
public $formView;
public $formParams = [];
public $clientOptions = [];
public $liveOptions = [];
private $tag;
public function renderForm()
{
if (is_string($this->formView)) {
$content = $this->getView()->renderFile($this->formView, array_merge([
'widget' => $this,
], $this->formParams));
} else {
$content = call_user_func($this->formView, $this);
}
return $content;
}
public function registerJs()
{
$id = $this->options['id'];
$options = Json::htmlEncode($this->clientOptions);
$user = Json::htmlEncode($this->user);
$contact = Json::htmlEncode($this->contact);
$view = $this->getView();
MessageAsset::register($view);
$view->registerJs("jQuery('#$id').yiiSimpleChatMessages($user,$contact,$options);");
}
/**
* @inheritdoc
*/
public function init()
{
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
if (!isset($this->clientOptions['itemCssClass'])) {
$this->clientOptions['itemCssClass'] = 'msg';
}
$this->tag = ArrayHelper::remove($this->options, 'tag', 'div');
echo Html::beginTag($this->tag, $this->options);
}
public function run()
{
$this->registerJs();
echo Html::endTag($this->tag);
}
public function renderItem($model, $key, $index)
{
if ($this->itemView === null) {
$content = $key;
} elseif (is_string($this->itemView)) {
$content = $this->getView()->renderFile($this->itemView, array_merge([
'model' => $model,
'key' => $key,
'index' => $index,
'user' => $this->user,
'sender' => $model['senderId'] == $this->user['id'] ? $this->user : $this->contact,
'settings' => $this->clientOptions,
], $this->viewParams));
} else {
$content = call_user_func($this->itemView, $model, $key, $index, $this);
}
return $content;
}
public function renderItems()
{
$models = $this->dataProvider->getModels();
$keys = $this->dataProvider->getKeys();
$rows = [];
foreach (array_reverse($models, true) as $index => $model) {
$rows[] = $this->renderItem($model, $keys[$index], $index);
}
return implode($this->separator, $rows);
}
public function renderSection($name)
{
switch ($name) {
case '{items}':
return $this->renderItems();
case '{form}':
return $this->renderForm();
default:
return false;
}
}
public function getId($autoGenerate = true)
{
$users = [$this->user['id'], $this->contact['id']];
sort($users);
return md5(implode('&', $users));
}
}