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