-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocialLikes.php
166 lines (147 loc) · 4.03 KB
/
SocialLikes.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace dominus77\sociallikesnext;
use yii\web\View;
use yii\web\JsExpression;
use yii\helpers\Json;
use yii\helpers\Html;
use dominus77\sociallikesnext\assets\SocialLikesAsset;
/**
* Class SocialLikes
* @package dominus77\sociallikesnext
*/
class SocialLikes extends \yii\base\Widget
{
const THEME_FLAT = 'flat';
const THEME_LIGHT = 'light';
const THEME_BIRMAN = 'birman';
/**
* Theme buttons
* @var string flat, light, birman
*/
public $theme = 'flat';
/**
* Display name of social networks, default true
* @var bool
*/
public $title = true;
/**
* @var array
*/
public $items = [];
/**
* @var array
*/
public $containerOptions = [];
/**
* @var array
*/
public $clientOptions = [];
/**
* @var array
*/
public $clientButtons = [];
/**
* @var string
*/
public $clientCss = '';
/**
* @var string
*/
public $id;
/**
* Initializes the widget
*/
public function init()
{
parent::init();
$this->id = $this->id ?: $this->getId();
$containerOptions = [
'id' => $this->id,
];
$this->containerOptions = array_merge($this->containerOptions, $containerOptions);
$this->containerOptions['class'] = (isset($this->containerOptions['class']) && !empty($this->containerOptions['class'])) ?
$this->theme . ' social-likes ' . $this->containerOptions['class'] :
$this->theme . ' social-likes';
}
/**
* Run widget
* @return string|void
*/
public function run()
{
if (!empty($this->items)) {
$this->registerAssets();
echo Html::beginTag('div', $this->containerOptions) . PHP_EOL;
$this->renderItems($this->items);
echo Html::endTag('div') . PHP_EOL;
}
}
/**
* Render items
* @param array $items
*/
public function renderItems($items = [])
{
foreach ($items as $key => $options) {
if (isset($options['serviceOptions']))
echo Html::tag('div', $this->getContentTitle($options) ? $key : '', $options['serviceOptions']) . PHP_EOL;
}
}
/**
* @param array $options
* @return bool|mixed
*/
protected function getContentTitle($options = [])
{
return (isset($options['title'])) ? $options['title'] : $this->title;
}
/**
* Register resources
*/
protected function registerAssets()
{
$view = $this->getView();
SocialLikesAsset::register($view);
$this->registerClientCss($view);
$this->registerClientButton($view);
$this->registerClientOptions($view);
}
/**
* @param \yii\web\View $view
*/
protected function registerClientCss($view)
{
if (!empty($this->clientCss)) {
$view->registerCss($this->clientCss);
}
}
/**
* @param \yii\web\View $view
*/
protected function registerClientButton($view)
{
if (!empty($this->clientButtons)) {
$clientButtons = Json::encode($this->clientButtons);
$script = new JsExpression("
var socialLikesButtons = {$clientButtons}
");
$view->registerJs($script, View::POS_BEGIN);
}
}
/**
* @param \yii\web\View $view
*/
public function registerClientOptions($view)
{
if (!empty($this->clientOptions)) {
$options = Json::encode($this->clientOptions);
$id = $this->containerOptions['id'];
$script = new JsExpression("
var container = document.getElementById('{$id}');
var socialLikes = SocialLikesNext.default;
socialLikes(container, {$options});
");
$view->registerJs($script);
}
}
}