1
|
|
|
<?php |
2
|
|
|
namespace Azine\SocialBarBundle\Templating; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
5
|
|
|
|
6
|
|
|
class SocialBarTwigExtension extends \Twig_Extension |
7
|
|
|
{ |
8
|
|
|
protected $container; |
9
|
|
|
/** |
10
|
|
|
* @var \Twig_Environment |
11
|
|
|
*/ |
12
|
|
|
protected $twig; |
13
|
|
|
|
14
|
|
|
public function initRuntime(\Twig_Environment $environment){ |
15
|
|
|
$this->twig = $environment; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor. |
20
|
|
|
* |
21
|
|
|
* @param ContainerInterface $container |
22
|
|
|
*/ |
23
|
20 |
|
public function __construct(ContainerInterface $container) |
24
|
|
|
{ |
25
|
20 |
|
$this->container = $container; |
26
|
20 |
|
} |
27
|
|
|
|
28
|
1 |
|
public function getName() |
29
|
|
|
{ |
30
|
1 |
|
return 'azine_social_bar'; |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public function getFunctions() |
34
|
|
|
{ |
35
|
1 |
|
$functions['socialButtons' ] = new \Twig_SimpleFunction('socialButtons' , array($this,'getSocialButtons'), array('is_safe' => array('html'))); |
|
|
|
|
36
|
1 |
|
$functions['facebookButton' ] = new \Twig_SimpleFunction('facebookButton' , array($this,'getFacebookButton'), array('is_safe' => array('html'))); |
|
|
|
|
37
|
1 |
|
$functions['twitterButton' ] = new \Twig_SimpleFunction('twitterButton' , array($this,'getTwitterButton'), array('is_safe' => array('html'))); |
|
|
|
|
38
|
1 |
|
$functions['googlePlusButton'] = new \Twig_SimpleFunction('googlePlusButton', array($this,'getGooglePlusButton'), array('is_safe' => array('html'))); |
|
|
|
|
39
|
1 |
|
$functions['xingButton' ] = new \Twig_SimpleFunction('xingButton' , array($this,'getXingButton'), array('is_safe' => array('html'))); |
|
|
|
|
40
|
1 |
|
$functions['linkedInButton' ] = new \Twig_SimpleFunction('linkedInButton' , array($this,'getLinkedInButton'), array('is_safe' => array('html'))); |
|
|
|
|
41
|
1 |
|
return $functions; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get all the buttons in one row |
46
|
|
|
* @param array $parameters |
47
|
|
|
* @param string $action |
48
|
|
|
*/ |
49
|
3 |
|
public function getSocialButtons(array $parameters = array(), $action = "share") |
50
|
|
|
{ |
51
|
3 |
|
$commonParams = $parameters; |
52
|
3 |
|
$render_parameters = array(); |
53
|
3 |
|
if(array_key_exists('facebook', $commonParams)) unset($commonParams['facebook']); |
54
|
3 |
|
if(array_key_exists('twitter', $commonParams)) unset($commonParams['twitter']); |
55
|
3 |
|
if(array_key_exists('googleplus', $commonParams)) unset($commonParams['googleplus']); |
56
|
3 |
|
if(array_key_exists('xing', $commonParams)) unset($commonParams['xing']); |
57
|
3 |
|
if(array_key_exists('linkedin', $commonParams)) unset($commonParams['linkedin']); |
58
|
|
|
|
59
|
|
|
// no parameters were defined, keeps default values |
60
|
3 |
View Code Duplication |
if (!array_key_exists('facebook', $parameters)) { |
|
|
|
|
61
|
1 |
|
$render_parameters['facebook'] = $parameters; |
62
|
|
|
|
63
|
|
|
// parameters are defined, overrides default values |
64
|
3 |
|
} elseif (is_array($parameters['facebook'])) { |
65
|
1 |
|
$render_parameters['facebook'] = array_merge($commonParams, $parameters['facebook']); |
66
|
|
|
|
67
|
|
|
// the button is not displayed |
68
|
1 |
|
} else { |
69
|
1 |
|
$render_parameters['facebook'] = false; |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
View Code Duplication |
if (!array_key_exists('twitter', $parameters)) { |
|
|
|
|
73
|
1 |
|
$render_parameters['twitter'] = $parameters; |
74
|
|
|
|
75
|
3 |
|
} elseif (is_array($parameters['twitter'])) { |
76
|
1 |
|
$render_parameters['twitter'] = array_merge($commonParams, $parameters['twitter']); |
77
|
|
|
|
78
|
1 |
|
} else { |
79
|
1 |
|
$render_parameters['twitter'] = false; |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
View Code Duplication |
if (!array_key_exists('googleplus', $parameters)) { |
|
|
|
|
83
|
1 |
|
$render_parameters['googleplus'] = $parameters; |
84
|
|
|
|
85
|
3 |
|
} elseif (is_array($parameters['googleplus'])) { |
86
|
1 |
|
$render_parameters['googleplus'] = array_merge($commonParams, $parameters['googleplus']); |
87
|
|
|
|
88
|
1 |
|
} else { |
89
|
1 |
|
$render_parameters['googleplus'] = false; |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
View Code Duplication |
if (!array_key_exists('xing', $parameters)) { |
|
|
|
|
93
|
1 |
|
$render_parameters['xing'] = $parameters; |
94
|
|
|
|
95
|
3 |
|
} elseif (is_array($parameters['xing'])) { |
96
|
1 |
|
$render_parameters['xing'] = array_merge($commonParams, $parameters['xing']); |
97
|
|
|
|
98
|
1 |
|
} else { |
99
|
1 |
|
$render_parameters['xing'] = false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
3 |
View Code Duplication |
if (!array_key_exists('linkedin', $parameters)) { |
|
|
|
|
104
|
1 |
|
$render_parameters['linkedin'] = $parameters; |
105
|
|
|
|
106
|
3 |
|
} elseif (is_array($parameters['linkedin'])) { |
107
|
1 |
|
$render_parameters['linkedin'] = array_merge($commonParams, $parameters['linkedin']); |
108
|
|
|
|
109
|
1 |
|
} else { |
110
|
1 |
|
$render_parameters['linkedin'] = false; |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
$render_parameters['action'] = $action; |
114
|
3 |
|
$render_parameters['width'] = 130; |
115
|
3 |
|
$render_parameters['height'] = 20; |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
// get the helper service and display the template |
119
|
3 |
|
return $this->container->get('azine.socialBarHelper')->socialButtons($render_parameters); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Render the html for the facebook like button |
124
|
|
|
* => https://developers.facebook.com/docs/reference/plugins/like/ |
125
|
|
|
* @param array $parameters |
126
|
|
|
*/ |
127
|
3 |
|
public function getFacebookButton($parameters = array(), $action = "share") |
128
|
|
|
{ |
129
|
|
|
// default values, you can override the values by setting them |
130
|
|
|
$parameters = $parameters + array( |
131
|
3 |
|
'locale' => 'en_US', |
132
|
3 |
|
'send' => false, |
133
|
3 |
|
'width' => 130, |
134
|
3 |
|
'showFaces' => false, |
135
|
3 |
|
'layout' => 'button_count', |
136
|
3 |
|
); |
137
|
|
|
|
138
|
3 |
View Code Duplication |
if ($action == "share") { |
|
|
|
|
139
|
1 |
|
$parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null; |
140
|
1 |
|
$parameters['action'] = 'fb-like'; |
141
|
|
|
|
142
|
3 |
|
} elseif ($action == "follow") { |
143
|
1 |
|
$parameters['url'] = $this->container->getParameter("azine_social_bar_fb_profile_url"); |
144
|
1 |
|
$parameters['action'] = "fb-follow"; |
145
|
|
|
|
146
|
1 |
|
} else { |
147
|
1 |
|
throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
return $this->container->get('azine.socialBarHelper')->facebookButton($parameters); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Render the html for the twitter button |
155
|
|
|
* => |
156
|
|
|
* @param array $parameters |
157
|
|
|
*/ |
158
|
3 |
|
public function getTwitterButton($parameters = array(), $action = "share") |
159
|
|
|
{ |
160
|
|
|
$parameters = $parameters + array( |
161
|
3 |
|
'url' => array_key_exists('url', $parameters) ? $parameters['url'] : null, |
162
|
3 |
|
'locale' => 'en', |
163
|
3 |
|
'message' => 'I want to share that page with you', |
164
|
3 |
|
'text' => 'Tweet', |
165
|
3 |
|
'via' => $this->container->getParameter("azine_social_bar_twitter_username"), |
166
|
3 |
|
'tag' => array_key_exists('tag', $parameters) ? $parameters['tag'] : $this->container->getParameter("azine_social_bar_twitter_username"), |
167
|
3 |
|
); |
168
|
3 |
|
if ($action == "share") { |
169
|
1 |
|
$parameters['actionClass'] = "twitter-share-button"; |
170
|
1 |
|
$parameters['action'] = "share"; |
171
|
|
|
|
172
|
3 |
|
} elseif ($action == "follow") { |
173
|
1 |
|
$parameters['actionClass'] = "twitter-follow-button"; |
174
|
1 |
|
$parameters['action'] = $this->container->getParameter("azine_social_bar_twitter_username"); |
175
|
|
|
|
176
|
1 |
|
} else { |
177
|
1 |
|
throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
178
|
|
|
} |
179
|
|
|
|
180
|
2 |
|
return $this->container->get('azine.socialBarHelper')->twitterButton($parameters); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Render the html for the Google+ button |
185
|
|
|
* => |
186
|
|
|
* @param array $parameters |
187
|
|
|
*/ |
188
|
3 |
|
public function getGooglePlusButton($parameters = array(), $action = "share") |
189
|
|
|
{ |
190
|
|
|
$parameters = $parameters + array( |
191
|
3 |
|
'locale' => 'en', |
192
|
3 |
|
'size' => 'medium', |
193
|
3 |
|
'annotation' => 'bubble', |
194
|
3 |
|
'width' => 130, |
195
|
3 |
|
'height' => 20, |
196
|
3 |
|
); |
197
|
|
|
|
198
|
3 |
|
if ($action == "share") { |
199
|
1 |
|
$parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null; |
200
|
1 |
|
$parameters['rel'] = 'author'; |
201
|
1 |
|
$parameters['action'] = 'g-plusone'; |
202
|
|
|
|
203
|
3 |
|
} elseif ($action == "follow") { |
204
|
1 |
|
$parameters['url'] = $this->container->getParameter("azine_social_bar_google_plus_profile_url"); |
205
|
1 |
|
$parameters['rel'] = "publisher"; |
206
|
1 |
|
$parameters['action'] = "g-follow"; |
207
|
|
|
|
208
|
1 |
|
} else { |
209
|
1 |
|
throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
210
|
|
|
} |
211
|
|
|
|
212
|
2 |
|
return $this->container->get('azine.socialBarHelper')->googlePlusButton($parameters); |
213
|
|
|
} |
214
|
|
|
|
215
|
3 |
|
public function getLinkedInButton($parameters = array(), $action = "share") |
216
|
|
|
{ |
217
|
|
|
$parameters = $parameters + array( |
218
|
3 |
|
'locale' => 'en', |
219
|
3 |
|
'counterLocation' => 'right', |
220
|
3 |
|
); |
221
|
|
|
|
222
|
3 |
View Code Duplication |
if ($action == "share") { |
|
|
|
|
223
|
1 |
|
$parameters['action'] = 'IN/Share'; |
224
|
1 |
|
$parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null; |
225
|
|
|
|
226
|
3 |
|
} elseif ($action == "follow") { |
227
|
1 |
|
$parameters['action'] = 'IN/FollowCompany'; |
228
|
1 |
|
$parameters['companyId'] = $this->container->getParameter("azine_social_bar_linked_in_company_id"); |
229
|
|
|
|
230
|
1 |
|
} else { |
231
|
1 |
|
throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
232
|
|
|
} |
233
|
|
|
|
234
|
2 |
|
return $this->container->get('azine.socialBarHelper')->linkedInButton($parameters); |
235
|
|
|
} |
236
|
|
|
|
237
|
3 |
|
public function getXingButton($parameters = array(), $action = "share") |
238
|
|
|
{ |
239
|
|
|
$parameters = $parameters + array( |
240
|
3 |
|
'locale' => 'en', |
241
|
3 |
|
'action' => 'XING/Share', |
242
|
3 |
|
'counterLocation' => 'right', |
243
|
3 |
|
); |
244
|
|
|
|
245
|
3 |
|
if ($action == "share") { |
246
|
1 |
|
$parameters['url'] = array_key_exists('url', $parameters) ? $parameters['url'] : null; |
247
|
3 |
|
} elseif ($action == "follow") { |
248
|
1 |
|
$parameters['url'] = $this->container->getParameter("azine_social_bar_xing_profile_url"); |
249
|
1 |
|
} else { |
250
|
1 |
|
throw new \Exception("Unknown social action. Only 'share' and 'follow' are known at the moment."); |
251
|
|
|
} |
252
|
|
|
|
253
|
2 |
|
return $this->container->get('azine.socialBarHelper')->xingButton($parameters); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.