|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/yiimaker/yii2-social-share |
|
4
|
|
|
* @copyright Copyright (c) 2017 Yii Maker |
|
5
|
|
|
* @license BSD 3-Clause License |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace ymaker\social\share\widgets; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\Exception; |
|
12
|
|
|
use yii\base\InvalidConfigException; |
|
13
|
|
|
use yii\base\Widget; |
|
14
|
|
|
use yii\di\Instance; |
|
15
|
|
|
use yii\helpers\ArrayHelper; |
|
16
|
|
|
use yii\helpers\Html; |
|
17
|
|
|
use yii\helpers\Url; |
|
18
|
|
|
use ymaker\social\share\assets\SocialIconsAsset; |
|
19
|
|
|
use ymaker\social\share\configurators\Configurator; |
|
20
|
|
|
use ymaker\social\share\configurators\ConfiguratorInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Widget for rendering the share links. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Vladimir Kuprienko <[email protected]> |
|
26
|
|
|
* @since 1.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
class SocialShare extends Widget |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string ID of configurator component. |
|
32
|
|
|
*/ |
|
33
|
|
|
public $configuratorId; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var string Absolute URL to the page. |
|
36
|
|
|
*/ |
|
37
|
|
|
public $url = ''; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var string Title for share. |
|
40
|
|
|
*/ |
|
41
|
|
|
public $title = ''; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var string Description for share. |
|
44
|
|
|
*/ |
|
45
|
|
|
public $description = ''; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var string Absolute URL to the image for share. |
|
48
|
|
|
*/ |
|
49
|
|
|
public $imageUrl = ''; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var string Name of the wrapper tag. |
|
52
|
|
|
*/ |
|
53
|
|
|
public $wrapperTag = 'ul'; |
|
54
|
|
|
/** |
|
55
|
|
|
* @var array HTML options for wrapper tag. |
|
56
|
|
|
*/ |
|
57
|
|
|
public $wrapperOptions = ['class' => 'social-share']; |
|
58
|
|
|
/** |
|
59
|
|
|
* @var bool|string Name of the wrapper tag for link. |
|
60
|
|
|
* Set `false` value if you don't want using wrapper for link. |
|
61
|
|
|
*/ |
|
62
|
|
|
public $linkWrapperTag = 'li'; |
|
63
|
|
|
/** |
|
64
|
|
|
* @var array HTML options for link wrapper tag. |
|
65
|
|
|
*/ |
|
66
|
|
|
public $linkWrapperOptions = []; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var \ymaker\social\share\configurators\ConfiguratorInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $_configurator; |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritdoc |
|
76
|
|
|
*/ |
|
77
|
|
|
public function init() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->_configurator = Instance::ensure($this->configuratorId, ConfiguratorInterface::class); |
|
80
|
|
|
|
|
81
|
|
|
if (empty($this->url)) { |
|
82
|
|
|
$this->url = Url::to('', true); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
private function enableDefaultIcons() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->_configurator instanceof Configurator && |
|
92
|
|
|
$this->_configurator->enableDefaultIcons; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Creates driver object. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $driverConfig |
|
99
|
|
|
* @return object |
|
100
|
|
|
*/ |
|
101
|
|
|
private function createDriver($driverConfig) |
|
102
|
|
|
{ |
|
103
|
|
|
return Yii::createObject(ArrayHelper::merge([ |
|
104
|
|
|
'class' => $driverConfig['class'], |
|
105
|
|
|
'url' => $this->url, |
|
106
|
|
|
'title' => $this->title, |
|
107
|
|
|
'description' => $this->description, |
|
108
|
|
|
'imageUrl' => $this->imageUrl |
|
109
|
|
|
], isset($driverConfig['config']) ? $driverConfig['config'] : [])); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Build label for driver. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $driverConfig |
|
116
|
|
|
* @param string $defaultLabel |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function buildLabel($driverConfig, $defaultLabel) |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->enableDefaultIcons() |
|
122
|
|
|
? Html::tag('i', '', [ |
|
123
|
|
|
'class' => $this->_configurator->getIconSelector($driverConfig['class']) |
|
124
|
|
|
]) |
|
125
|
|
|
: isset($driverConfig['label']) ? $driverConfig['label'] : $defaultLabel; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Combine global and custom HTML options. |
|
130
|
|
|
* |
|
131
|
|
|
* @param array $driverConfig |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
|
|
private function combineOptions($driverConfig) |
|
135
|
|
|
{ |
|
136
|
|
|
$options = $driverConfig['options']; |
|
137
|
|
|
|
|
138
|
|
|
$globalOptions = $this->_configurator->getOptions(); |
|
139
|
|
|
if (empty($globalOptions)) { |
|
140
|
|
|
return $options; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (isset($options['class'])) { |
|
144
|
|
|
Html::addCssClass($globalOptions, $options['class']); |
|
145
|
|
|
unset($options['class']); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return ArrayHelper::merge($globalOptions, $options); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Returns array with share links in <a> HTML tag. |
|
153
|
|
|
* |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function processSocialNetworks() |
|
157
|
|
|
{ |
|
158
|
|
|
$socialNetworks = $this->_configurator->getSocialNetworks(); |
|
159
|
|
|
$shareLinks = []; |
|
160
|
|
|
|
|
161
|
|
|
foreach ($socialNetworks as $key => $socialNetwork) { |
|
162
|
|
|
if (isset($socialNetwork['class'])) { |
|
163
|
|
|
/* @var \ymaker\social\share\base\Driver $driver */ |
|
164
|
|
|
$driver = $this->createDriver($socialNetwork); |
|
165
|
|
|
|
|
166
|
|
|
$shareLinks[] = Html::a( |
|
167
|
|
|
$this->buildLabel($socialNetwork, $key), |
|
168
|
|
|
$driver->getLink(), |
|
169
|
|
|
$this->combineOptions($socialNetwork) |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $shareLinks; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @inheritdoc |
|
179
|
|
|
*/ |
|
180
|
|
|
public function run() |
|
181
|
|
|
{ |
|
182
|
|
|
$links = $this->processSocialNetworks(); |
|
183
|
|
|
|
|
184
|
|
|
if ($this->enableDefaultIcons()) { |
|
185
|
|
|
$this->getView()->registerAssetBundle(SocialIconsAsset::class); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
echo Html::beginTag($this->wrapperTag, $this->wrapperOptions); |
|
189
|
|
|
foreach ($links as $link) { |
|
190
|
|
|
echo ($this->linkWrapperTag !== false) |
|
191
|
|
|
? Html::tag($this->linkWrapperTag, $link, $this->linkWrapperOptions) |
|
192
|
|
|
: $link; |
|
193
|
|
|
} |
|
194
|
|
|
echo Html::endTag($this->wrapperTag); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|