-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathShareLinks.php
73 lines (66 loc) · 1.85 KB
/
ShareLinks.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
<?php
namespace ijackua\sharelinks;
use ijackua\sharelinks\ShareLinksAssets;
use Yii;
use yii\helpers\ArrayHelper;
class ShareLinks extends \yii\base\Widget
{
/**
* Constants representing each socil net
*/
const SOCIAL_TWITTER = 1;
const SOCIAL_FACEBOOK = 2;
const SOCIAL_VKONTAKTE = 3;
const SOCIAL_GPLUS = 4;
const SOCIAL_LINKEDIN = 5;
const SOCIAL_KINDLE = 6;
const SOCIAL_XING = 7;
/**
* Path to view file that will contain markup for the whole soc net buttons block
*
* @var string
*/
public $viewName = 'main';
/**
* Url to share
*
* @var string
*/
public $url;
/**
* jQuery selector for sharing links (to bind popup on click there)
*
* @var string
*/
public $linkSelector = '.socialShareBlock a';
/**
* Sharing url template for each network
*
* @var array
*/
public $shareUrlMap = [
self::SOCIAL_TWITTER => 'https://twitter.com/intent/tweet?url={url}',
self::SOCIAL_FACEBOOK => 'https://www.facebook.com/sharer/sharer.php?u={url}',
self::SOCIAL_VKONTAKTE => 'http://vk.com/share.php?url={url}',
self::SOCIAL_GPLUS => 'https://plus.google.com/share?url={url}',
self::SOCIAL_LINKEDIN => 'http://www.linkedin.com/shareArticle?url={url}',
self::SOCIAL_KINDLE => 'http://fivefilters.org/kindle-it/send.php?url={url}',
self::SOCIAL_XING => 'https://www.xing.com/spi/shares/new?url={url}'
];
public function init()
{
$this->url = (empty($this->url)) ? Yii::$app->getRequest()->getAbsoluteUrl() : $this->url;
ShareLinksAssets::register($this->view);
}
public function run()
{
$js = '$("' . $this->linkSelector . '").yiiShareLinks();';
$this->view->registerJs($js);
echo $this->render($this->viewName);
}
public function shareUrl($networkId, $url=null)
{
if( !$url ) { $url = $this->url; }
return str_replace('{url}', urlencode($url), ArrayHelper::getValue($this->shareUrlMap, $networkId));
}
}