-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSidebarUserPanel.php
108 lines (93 loc) · 2.8 KB
/
SidebarUserPanel.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
<?php
namespace memclutter\AdminLTE;
use yii\base\InvalidValueException;
use yii\base\Model;
use yii\base\Widget;
use yii\db\ActiveRecord;
use yii\helpers\Html;
use yii\web\IdentityInterface;
class SidebarUserPanel extends Widget
{
/**
* @var IdentityInterface|ActiveRecord|Model User model class.
*/
public $model;
/**
* @var string|callable avatar builder. It can be a string or function.
* Examples:
*
* <?= SidebarUserPanel::widget([
* 'avatar' => function($model) {
* return Yii::getAlias('@uploads/avatars/' . $model->avatarSrc);
* },
* 'userNameAttribute' => 'username',
* ]) ?>
*
* or src string
*
* <?= SidebarUserPanel::widget([
* 'user' => $model,
* 'avatar' => Yii::getAlias('@uploads/avatars/' . $model->avatarSrc),
* 'userNameAttribute' => 'username',
* ]) ?>
*/
public $avatar;
/**
* @var string this value use in panel info
*/
public $usernameAttribute;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (empty($this->model) || ($this->model instanceof IdentityInterface)) {
if (\Yii::$app->user->isGuest) {
throw new InvalidValueException('SidebarUserPanel::user property expected not guest authorized user');
}
$this->model = \Yii::$app->user->identity;
}
if (empty($this->usernameAttribute)) {
throw new InvalidValueException('SidebarUserPanel::userNameAttribute property is required');
}
if (!$this->model->hasAttribute($this->usernameAttribute)) {
throw new InvalidValueException('SidebarUserPanel::userNameAttribute property not found');
}
}
public function run()
{
$userName = $this->getUserName();
$img = $this->buildImgTag();
return (
'<div class="user-panel">
<div class="pull-left image">
' . $img . '
</div>
<div class="pull-left info">
<p>' . $userName . '</p>
<!-- Status -->
<a href="#"><i class="fa fa-circle text-success"></i> Online</a>
</div>
</div>'
);
}
protected function buildImgTag()
{
return Html::img($this->getAvatarSrc(), [
'class' => [
'alt' => $this->getUserName(),
'title' => $this->getUserName(),
],
]);
}
protected function getUserName()
{
return $this->model->getAttribute($this->usernameAttribute);
}
protected function getAvatarSrc()
{
$avatar = $this->avatar;
return $avatar instanceof \Closure ? $avatar() : $avatar;
}
}