/
UserFollowButton.php
129 lines (103 loc) · 3.63 KB
/
UserFollowButton.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
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2016 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\user\widgets;
use Yii;
use yii\bootstrap\Html;
/**
* UserFollowButton
*
* @author luke
* @since 0.11
*/
class UserFollowButton extends \yii\base\Widget
{
/**
* @var \humhub\modules\user\models\User
*/
public $user;
/**
* @var string label for follow button (optional)
*/
public $followLabel = null;
/**
* @var string label for unfollow button (optional)
*/
public $unfollowLabel = null;
/**
* @var string options for follow button
*/
public $followOptions = ['class' => 'btn btn-primary'];
/**
* @var array options for unfollow button
*/
public $unfollowOptions = ['class' => 'btn btn-primary active'];
/**
* @inheritdoc
*/
public function init()
{
if ($this->followLabel === null) {
$this->followLabel = Yii::t('UserModule.base', 'Follow');
}
if ($this->unfollowLabel === null) {
$this->unfollowLabel = '<span class="glyphicon glyphicon-ok"></span> ' . Yii::t('UserModule.base', 'Following');
}
if (!isset($this->followOptions['class'])) {
$this->followOptions['class'] = "";
}
if (!isset($this->unfollowOptions['class'])) {
$this->unfollowOptions['class'] = "";
}
if (!isset($this->followOptions['style'])) {
$this->followOptions['style'] = "";
}
if (!isset($this->unfollowOptions['style'])) {
$this->unfollowOptions['style'] = "";
}
}
/**
* @inheritdoc
*/
public function run()
{
if ($this->user->isCurrentUser() || \Yii::$app->user->isGuest) {
return;
}
// Add class for javascript handling
$this->followOptions['class'] .= ' followButton';
$this->unfollowOptions['class'] .= ' unfollowButton';
// Hide inactive button
if ($this->user->isFollowedByUser()) {
$this->followOptions['style'] .= ' display:none;';
} else {
$this->unfollowOptions['style'] .= ' display:none;';
}
// Add UserId Buttons
$this->followOptions['data-content-container-id'] = $this->user->id;
$this->unfollowOptions['data-content-container-id'] = $this->user->id;
// Add JS Action
$this->followOptions['data-action-click'] = 'content.container.follow';
$this->unfollowOptions['data-action-click'] = 'content.container.unfollow';
// Add Action Url
$this->followOptions['data-action-url'] = $this->user->createUrl('/user/profile/follow');
$this->unfollowOptions['data-action-url'] = $this->user->createUrl('/user/profile/unfollow');
// Add Action Url
$this->followOptions['data-ui-loader'] = '';
$this->unfollowOptions['data-ui-loader'] = '';
// Confirm action "Unfollow"
$this->unfollowOptions['data-action-confirm'] = Yii::t('SpaceModule.base', 'Would you like to unfollow {userName}?', [
'{userName}' => '<strong>' . Html::encode($this->user->getDisplayName()) . '</strong>'
]);
$module = Yii::$app->getModule('user');
// still enable unfollow if following was disabled afterwards.
if ($module->disableFollow) {
return Html::a($this->unfollowLabel, '#', $this->unfollowOptions);
}
return Html::a($this->unfollowLabel, '#', $this->unfollowOptions) .
Html::a($this->followLabel, '#', $this->followOptions);
}
}