-
Notifications
You must be signed in to change notification settings - Fork 17
/
EFaceplugsBase.php
409 lines (383 loc) · 9.01 KB
/
EFaceplugsBase.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
/**
* Wrappers for facebook plugins.
* @see http://www.yiiframework.com/extension/faceplugs/
*
* @copyright © Digitick <www.digitick.net> 2011
* @license GNU Lesser General Public License v3.0
* @version 1.3
* @author Ianaré Sévi
* @author Gustavo Salomé
*/
/**
* Base class for all facebook widgets.
*
* Initializes required properties for widgets and sets opengraph properties.
*
* @see http://developers.facebook.com/plugins
* @see http://developers.facebook.com/docs/opengraph
*
*/
abstract class EFaceplugsBase extends CWidget
{
/**
* @var string Facebook application ID.
*
* This can be set in the 'fbAppId' parameter in the Yii config file.
*/
public $app_id;
/**
* @var string Page URL, for Open Graph
*/
public $url;
/**
* @var array Open Graph properties.
*/
public $og = array();
/**
* @var boolean Check user's login status.
*/
public $status = true;
/**
* @var boolean Enable cookies to allow the server to access the session.
*/
public $cookie = true;
/**
* @var boolean Whether or not to parse XFBML tags with the JS library (will render Plugins with iframes if false).
*/
public $xfbml = true;
/**
* @var boolean Whether or not to use html5 social plugins instead of xfbml.
*/
public $html5 = false;
/**
* @var boolean Load the Facebook init script asynchronously.
*
* This speeds up page loads because loading the plugin does not block
* loading other elements of the page.
*/
public $async = true;
/**
* @var string Override default locale for the widget.
*
* Normally locale is set automatically based on the Yii language settings,
* setting it here allows a specific locale to be used.
*/
public $locale;
/**
* @var string Specify the debug mode. When active, it loads the debug
* version of the SDK (en_US only).
*
* Options :
* <ul>
* <li>'auto' - If YII_DEBUG is true, debug mode is active.
* <li>'on' - debug enabled.
* <li>'off' - debug disabled (default).
* </ul>
*/
public $debugMode = 'off';
/**
* @var boolean Force usage of https. False by default.
*/
public $https = false;
/**
* @var array Valid Facebook locales.
*/
protected $locales = array(
'az_AZ',
'be_BY',
'bg_BG',
'bn_IN',
'bs_BA',
'ca_ES',
'ck_US',
'cs_CZ',
'cy_GB',
'da_DK',
'de_DE',
'eu_ES',
'en_GB',
'en_PI',
'en_UD',
'en_US',
'es_LA',
'es_CL',
'es_CO',
'es_ES',
'es_MX',
'es_VE',
'fb_FI',
'fi_FI',
'fr_FR',
'gl_ES',
'hu_HU',
'it_IT',
'ja_JP',
'ko_KR',
'nb_NO',
'nn_NO',
'nl_NL',
'pl_PL',
'pt_BR',
'pt_PT',
'ro_RO',
'ru_RU',
'sk_SK',
'sl_SI',
'sv_SE',
'th_TH',
'tr_TR',
'ku_TR',
'zh_CN',
'zh_HK',
'zh_TW',
'fb_LT',
'af_ZA',
'sq_AL',
'hy_AM',
'hr_HR',
'nl_BE',
'eo_EO',
'et_EE',
'fo_FO',
'fr_CA',
'ka_GE',
'el_GR',
'gu_IN',
'hi_IN',
'is_IS',
'id_ID',
'ga_IE',
'jv_ID',
'kn_IN',
'kk_KZ',
'la_VA',
'lv_LV',
'li_NL',
'lt_LT',
'mk_MK',
'mg_MG',
'ms_MY',
'mt_MT',
'mr_IN',
'mn_MN',
'ne_NP',
'pa_IN',
'rm_CH',
'sa_IN',
'sr_RS',
'so_SO',
'sw_KE',
'tl_PH',
'ta_IN',
'tt_RU',
'te_IN',
'ml_IN',
'uk_UA',
'uz_UZ',
'vi_VN',
'xh_ZA',
'zu_ZA',
'km_KH',
'tg_TJ',
'ar_AR',
'he_IL',
'ur_PK',
'fa_IR',
'sy_SY',
'yi_DE',
'gn_PY',
'qu_PE',
'ay_BO',
'se_NO',
'ps_AF',
'tl_ST',
);
public function init()
{
if (property_exists($this, 'href') && $this->href === null)
$this->href = $this->url;
if (!$this->app_id && isset(Yii::app()->params->fbAppId))
$this->app_id = Yii::app()->params->fbAppId;
if ($this->url)
self::registerOpenGraph('url', $this->url);
if ($this->app_id)
self::registerOpenGraph('app_id', $this->app_id);
foreach ($this->og as $type => $value)
self::registerOpenGraph($type, $value);
}
/**
* Get the protocol used.
* @return string 'http' or 'https'
*/
protected function getProtocol()
{
if($this->https==true || Yii::app()->getRequest()->getIsSecureConnection())
return 'https';
return 'http';
}
public function run()
{
//run only once
if (!isset(Yii::app()->params->fbRootSet)) {
if ($this->debugMode === 'auto' && YII_DEBUG === true)
$this->debugMode = 'on';
if ($this->debugMode === 'on')
$script = 'static.ak.fbcdn.net/connect/en_US/core.debug.js';
else
$script = 'connect.facebook.net/'.$this->getLocale().'/all.js';
$script = $this->getProtocol() . '://' . $script;
echo CHtml::opentag('div', array('id' => 'fb-root'));
$init = $this->registerSDKScript('init', array(
'appId' => $this->app_id,
'status' => $this->status,
'cookie' => $this->cookie,
'xfbml' => $this->xfbml,
)
);
if ($this->async)
$init = "window.fbAsyncInit = function(){{$init}};
(function(){
var e=document.createElement('script');
e.async=true;
e.src='{$script}';
document.getElementById('fb-root').appendChild(e);}());";
else
Yii::app()->clientScript->registerScriptFile($script, CClientScript::POS_END);
echo CHtml::closeTag('div');
Yii::app()->getClientScript()->registerScript('fb-script', $init, CClientScript::POS_END);
Yii::app()->params->fbRootSet = true;
}
}
/**
* Register an OpenGraph property.
* @param string $property
* @param string $data
* @return void
*/
public static function registerOpenGraph($property, $data)
{
$property = 'og:' . $property;
Yii::app()->clientScript->registerMetaTag($data, null, null, array('property' => $property));
}
/**
* Determine the script locale to load.
* Looks at $locale variable declared in this file first
* Then looks at the Yii application language
* Defaults to en_US
* @return string locale code
*/
protected function getLocale()
{
if (isset($this->locale))
$locale = strtolower($this->locale);
else
$locale = Yii::app()->language;
// Adjustments, mainly because facebook doesn't have all countries
// of the same language translated.
$lang = substr($locale, 0, 2);
$adjust = array(
'de' => 'de_de',
'nl' => 'nl_nl',
'ru' => 'ru_ru',
'ar' => 'ar_ar', // non standard
'ku' => 'ku_tr',
);
// single check languages, array above ...
if (isset($adjust[$lang]))
$locale = $adjust[$lang];
// english
else if ($lang === 'en' && !in_array($locale, array('en_us','en_pi','en_ud'))) {
// closer to US english
if ($locale === 'en_ca')
$locale = 'en_us';
// closer to UK english
else
$locale = 'en_gb';
}
// french
else if ($lang === 'fr' && $locale !== 'fr_ca')
$locale = 'fr_fr';
// spanish
else if ($lang === 'es' && !in_array($locale, array('es_es','es_cl','es_co','es_mx','es_ve')))
$locale = 'es_la'; // non standard
// portuguese
else if ($lang === 'pt' && $locale !== 'pt_br')
$locale = 'pt_pt';
$c = explode('_', $locale);
if (!isset($c[1]))
throw new CException('Locale for Facebook plugins must be in the following format : ll_CC');
$locale = $c[0] . '_' . strtoupper($c[1]);
if (!in_array($locale, $this->locales))
throw new CException('Invalid Facebook locale');
return $locale;
}
/**
* Grabs public properties of the class for passing to the plugin creator.
* @return array Associative array
*/
protected function getParams()
{
$ignore = array('skin', 'actionPrefix', 'app_id', 'url', 'status',
'cookie', 'async', 'debugMode', 'xfbml', 'https', 'html5');
$ref = new ReflectionObject($this);
$props = $ref->getProperties(ReflectionProperty::IS_PUBLIC);
$params = array();
foreach ($props as $k => $v) {
$name = $v->name;
if ($this->$name !== null && !is_array($this->$name) && !in_array($name, $ignore)) {
if (is_bool($this->$name))
$value = ($this->$name === true) ? 'true' : 'false';
else
$value = $this->$name;
$params[$name] = $value;
}
}
return $params;
}
/**
* Creates a method of facebook sdk script.
* @param string $method
* @param array $args args to use in the method
* @return string the js created
*/
public function registerSDKScript($method, $args=array())
{
$args = CJavaScript::encode($args);
return "FB.{$method}({$args});";
}
/**
* @param $name the name of the Facebook Social Plugin
* @return void
*/
protected function renderTag($name)
{
$params = $this->getParams();
if ($this->html5)
$this->makeHtml5Tag('fb-'.$name,$params);
else
$this->makeXfbmlTag('fb:'.$name,$params);
}
/**
* @param $class the name of the Facebook Social Plugin
* @param $params the parameters for the Facebook Social Plugin
* @return void
*/
protected function makeHtml5Tag($class, $params)
{
$newParams = array();
foreach($params as $key=>$data)
$newParams['data-'.$key] = $data;
$newParams['class'] = $class;
echo CHtml::openTag('div', $newParams), CHtml::closeTag('div');
}
/**
* @param $tagName the name of the Facebook Social Plugin
* @param $params the parameters for the Facebook Social Plugin
* @return void
*/
protected function makeXfbmlTag($tagName, $params)
{
echo CHtml::openTag($tagName, $params), CHtml::closeTag($tagName);
}
}