-
Notifications
You must be signed in to change notification settings - Fork 3
/
EShareBox.php
175 lines (168 loc) · 5 KB
/
EShareBox.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
167
168
169
170
171
172
173
174
175
<?php
/**
* ShareBox
* Create a list of social networks that a user may share the page with.
*
* CSS base and 48px icons from Beautiful Social Bookmarking Widget by Harish.
* http://www.way2blogging.org/2011/03/add-beautiful-social-bookmarking-widget.html
*
* 16, 24 and 32 px icons from IconDock
* http://icondock.com/free/vector-social-media-icons
*
* @copyright © Digitick <www.digitick.net> 2011-2012
* @license Public Domain
* @author Ianaré Sévi
* @author Vincent Castelain
*
* Note: the company logos in the icons are copyright of their respective owners.
*/
/**
* Main widget class.
*/
class EShareBox extends CWidget
{
/**
* @var string URL to share.
*/
public $url;
/**
* @var string Title for the page to share.
*/
public $title;
/**
* @var boolean whether to animate the links.
*/
public $animate = true;
/**
* @var integer Size for share icons.
*/
public $iconSize = 24;
/**
* @var string custom icon path.
*/
public $iconPath;
/**
* @var array Services to include.
* Note that the exclude filter is still applied.
*/
public $include = array();
/**
* @var array Services to exclude.
*/
public $exclude = array();
/**
* @var array Html options for UL element.
*/
public $ulHtmlOptions = array();
/**
* @var array Html options for LI elements.
*/
public $liHtmlOptions = array();
/**
* @var array Definitions for sharing services.
*/
protected $shareDefinitions = array(
'facebook' => array(
'url' => 'https://www.facebook.com/sharer.php?u={url}&t={title}',
'title' => 'Share this on Facebook',
'name' => 'Facebook'
),
'twitter' => array(
'url' => 'http://twitter.com/intent/tweet?url={url}&text={title}',
'title' => 'Tweet This!',
'name' => 'Twitter',
),
'google-plus' => array(
'url' => 'https://plus.google.com/share?url={url}',
'title' => 'Share this on Google+',
'name' => 'Google+'
),
'stumbleupon' => array(
'url' => 'http://www.stumbleupon.com/submit?url={url}&title={title}',
'title' => 'Stumble upon something good? Share it on StumbleUpon',
'name' => 'StumbleUpon'
),
'digg' => array(
'url' => 'http://digg.com/submit?phase=2&url={url}&title={title}',
'title' => 'Digg this!',
'name' => 'Digg',
),
'delicious' => array(
'url' => 'http://delicious.com/post?url={url}&title={title}',
'title' => 'Share this on del.icio.us',
'name' => 'Delicious',
),
'linkedin' => array(
'url' => 'http://www.linkedin.com/shareArticle?mini=true&url={url}&title={title}',
'title' => 'Share this on LinkedIn',
'name' => 'LinkedIn',
),
'reddit' => array(
'url' => 'http://reddit.com/submit?url={url}&title={title}',
'title' => 'Share this on Reddit',
'name' => 'Reddit',
),
'technorati' => array(
'url' => 'http://technorati.com/faves?add={url}',
'title' => 'Share this on Technorati',
'name' => 'Technorati',
),
'newsvine' => array(
'url' => 'http://www.newsvine.com/_tools/seed&save?u={url}',
'title' => 'Bookmark on Newsvine',
'name' => 'Newsvine',
),
/*
'email' => array(
'url' => 'http://mysite.com/sendEmail?url={url}&title={title}',
'title' => 'Email this',
'name' => 'E-Mail',
),
*/
);
/**
* @var array Default html options. Will be merged with $htmlOptions provided by user.
*/
protected $defaultUlHtmlOptions = array(
'class' => 'way2blogging-social',
);
public function init()
{
if (!$this->url || !$this->title) {
throw new CException('Could not initialize ShareBox : "title" and "url" parameters are required.');
}
$assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . '/assets');
Yii::app()->clientScript->registerCssFile($assets . '/style.css');
if (!$this->iconPath) {
$this->iconPath = $assets . '/images';
}
if (!empty($this->include)) {
foreach ($this->shareDefinitions as $share => $info) {
if (!in_array($share, $this->include)) {
unset($this->shareDefinitions[$share]);
}
}
}
foreach ((array) $this->exclude as $share) {
unset($this->shareDefinitions[$share]);
}
if($this->animate) {
$this->defaultUlHtmlOptions['class'] = $this->defaultUlHtmlOptions['class'] . ' way2blogging-cssanime';
}
$this->defaultUlHtmlOptions['class'] = $this->defaultUlHtmlOptions['class'] . " way2blogging-size{$this->iconSize}";
$this->ulHtmlOptions = array_merge($this->defaultUlHtmlOptions, $this->ulHtmlOptions);
}
public function run()
{
echo CHtml::openTag('ul', $this->ulHtmlOptions);
foreach ($this->shareDefinitions as $name => $def) {
$linkText = CHtml::tag('strong', array(), $def['name']);
$url = strtr($def['url'], array('{url}' => urlencode($this->url), '{title}' => urlencode($this->title)));
$link = CHtml::link($linkText, $url, array('rel' => 'nofollow', 'target' => '_blank', 'title' => $def['title']));
$bgImage = "{$this->iconPath}/{$this->iconSize}px/{$name}.png";
$this->liHtmlOptions['style'] = "background-image:url({$bgImage});";
echo "\n" . CHtml::tag('li', $this->liHtmlOptions, $link);
}
echo CHtml::closeTag('ul');
}
}