forked from bubasuma/yii2-simplechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversationWidget.php
106 lines (91 loc) · 2.58 KB
/
ConversationWidget.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
<?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 ConversationWidget
* @package bubasuma\simplechat
*
* @author Buba Suma <bubasuma@gmail.com>
* @since 1.0
*/
class ConversationWidget extends ListView
{
/**
* The current user
* @var array
*/
public $user;
/**
* The current conversation
* @since 2.0
* @var array
*/
public $current;
public $clientOptions = [];
public $liveOptions = [];
private $tag;
public function registerJs()
{
$id = $this->options['id'];
$options = Json::htmlEncode($this->clientOptions);
$user = Json::htmlEncode($this->user);
$current = Json::htmlEncode($this->current);
$view = $this->getView();
ConversationAsset::register($view);
$view->registerJs("jQuery('#$id').yiiSimpleChatConversations($user, $current, $options);");
}
/**
* @inheritdoc
*/
public function init()
{
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
if (!isset($this->clientOptions['itemCssClass'])) {
$this->clientOptions['itemCssClass'] = 'conversation';
}
$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,
'isCurrent' => $model['contact']['id'] == $this->current['contact']['id'],
'settings' => $this->clientOptions,
], $this->viewParams));
} else {
$content = call_user_func($this->itemView, $model, $key, $index, $this);
}
return $content;
}
public function renderSection($name)
{
switch ($name) {
case '{items}':
return $this->renderItems();
default:
return false;
}
}
}