PageRenderTime 35ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/views/helpers/facebook.php

https://github.com/copongcopong/CakePHP-Facebook-Plugin
PHP | 425 lines | 203 code | 24 blank | 198 comment | 22 complexity | 9829547a36a0db31c208a0365cd99f12 MD5 | raw file
  1. <?php
  2. /**
  3. * Facebook.Facebook helper generates fbxml and loads javascripts
  4. *
  5. * @author Nick Baker <nick [at] webtechnick [dot] com>
  6. * @version since 2.3.0
  7. * @license MIT
  8. * @link http://www.webtechnick.com
  9. */
  10. App::import('Lib', 'Facebook.FacebookInfo');
  11. class FacebookHelper extends AppHelper {
  12. /**
  13. * Helpers to load with this helper.
  14. */
  15. var $helpers = array('Html', 'Session');
  16. /**
  17. * Default Facebook.Share javascript URL
  18. * @access private
  19. */
  20. var $__fbShareScript = 'http://static.ak.fbcdn.net/connect.php/js/FB.Share';
  21. /**
  22. * Get the info on this plugin
  23. * @param string name to retrieve (default 'version')
  24. * - 'name' => Plugin Name
  25. * - 'author' => Author Name
  26. * - 'email' => Support Email
  27. * - 'link' => Support Link
  28. * - 'license' => License Info
  29. * @return string plugin version
  30. */
  31. function info($name = 'version'){
  32. if(FacebookInfo::_isAvailable($name)){
  33. return FacebookInfo::$name();
  34. }
  35. else {
  36. return "$name is not an available option";
  37. }
  38. }
  39. /**
  40. * Loaoder is no longer needed and is now deprecated
  41. * @return null
  42. */
  43. function loader(){
  44. return null;
  45. }
  46. /**
  47. * HTML XMLNS tag (required)
  48. * @return string of html header
  49. * @access public
  50. */
  51. function html(){
  52. return '<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">';
  53. }
  54. /**
  55. * Login Button
  56. * $this->Facebook->init() is required for this
  57. * @param array of options
  58. * - show-faces bool Show pictures of the user's friends who have joined your application
  59. * - width int The width of the plugin in pixels
  60. * - max-rows int The maximum number of rows of profile pictures to show
  61. * - perms list of permissions to ask for when logging in separated by commas (eg: 'email,read_stream,publish_stream'). (http://developers.facebook.com/docs/authentication/permissions)
  62. * @return string XFBML tag
  63. * @access public
  64. */
  65. function login($options = array()){
  66. return $this->__fbTag('fb:login-button', '', $options);
  67. }
  68. /**
  69. * Logout Button
  70. * $this->Facebook->init() is required for this
  71. * @param array of options
  72. * - redirect string to your app's logout url (default null)
  73. * - label string of text to use in link (default logout)
  74. * - confirm string Alert dialog which will be visible if user clicks on the button/link
  75. * - custom used to create custom link instead of standart fbml. if redirect option is set this one is not required.
  76. * @return string XFBML tag for logout button
  77. * @access public
  78. */
  79. function logout($options = array()){
  80. $options = array_merge(
  81. array(
  82. 'autologoutlink' => 'true',
  83. 'label' => 'logout',
  84. 'custom' => false
  85. ),
  86. $options
  87. );
  88. if(isset($options['redirect']) || $options['custom']){
  89. if(isset($options['redirect']) && $options['redirect']){
  90. $options['redirect'] = Router::url($options['redirect']);
  91. $response = "window.location = '{$options['redirect']}'";
  92. } else {
  93. $response = "window.location.reload();";
  94. }
  95. $onclick = "FB.logout(function(response){".$response."});";
  96. if(isset($options['confirm'])){
  97. $onclick = 'if(confirm("'.$options['confirm'].'")){'.$onclick.'}';
  98. }
  99. return $this->Html->link($options['label'], '#', array('onclick' => $onclick));
  100. } else {
  101. unset($options['label'], $options['escape'], $options['custom']);
  102. return $this->__fbTag('fb:login-button', '', $options);
  103. }
  104. }
  105. /**
  106. * Unsubscribe Button - Function which creates link for disconnecting user from the specific application
  107. * $this->Facebook->init() is required for this
  108. * @param array of options
  109. * - redirect string to your app's logout url (default null)
  110. * - label string of text to use in link (default logout)
  111. * - confirm string Alert dialog which will be visible if user clicks on the button/link
  112. * @return string Link for disconnect button
  113. * @access public
  114. */
  115. function disconnect($options = array()){
  116. $options = array_merge(
  117. array(
  118. 'label' => 'logout'
  119. ),
  120. $options
  121. );
  122. if(isset($options['redirect']) && $options['redirect']){
  123. $options['redirect'] = Router::url($options['redirect']);
  124. $response = "window.location = '{$options['redirect']}'";
  125. } else {
  126. $response = "window.location.reload();";
  127. }
  128. $onclick = "FB.api({ method: 'Auth.revokeAuthorization' }, function(response) {".$response."});";
  129. if(isset($options['confirm'])){
  130. $onclick = 'if(confirm("'.$options['confirm'].'")){'.$onclick.'}';
  131. }
  132. return $this->Html->link($options['label'], '#', array('onclick' => $onclick));
  133. }
  134. /**
  135. * Share this page
  136. * @param string url: url to share with facebook (default current page)
  137. * @param array options to pass into share
  138. * - style: 'button' or 'link' (default'button')
  139. * - label: title of text to link(default 'share')
  140. * - anchor: a href anchor name (default 'fb_share')
  141. * - fbxml: true or false. If true, use fb:share-button xml style instead of javascript share (default false)
  142. * @return string XFBML tag along with shareJs script
  143. * @access public
  144. */
  145. function share($url = null, $options = array()){
  146. // @todo this can be improved using the router
  147. if(!$url) $url = env('SERVER_NAME') . $this->here;
  148. $defaults = array(
  149. 'style' => 'button',
  150. 'label' => 'share',
  151. 'anchor' => 'fb_share',
  152. 'fbxml' => false
  153. );
  154. $options = array_merge($defaults, $options);
  155. if(!$options['fbxml']){
  156. switch($options['style']){
  157. case 'link': $options['type'] = 'icon_link'; break;
  158. default: $options['type'] = 'button'; break;
  159. }
  160. }
  161. if($options['fbxml']){
  162. unset($options['fbxml']);
  163. $retval = $this->__fbTag('fb:share-button','',$options);
  164. }
  165. else {
  166. $retval = $this->Html->link($options['label'], 'http://www.facebook.com/sharer.php', array('share_url' => $url, 'type' => $options['type'], 'name' => $options['anchor']));
  167. $retval .= $this->Html->script($this->__fbShareScript);
  168. }
  169. return $retval;
  170. }
  171. /**
  172. * Profile Picture of Facebook User
  173. * $facebook->init() is required for this
  174. * @param int facebook user id.
  175. * @param array options to pass into pic
  176. * - uid : user_id to view profile picture
  177. * - size : size of the picture represented as a string. 'thumb','small','normal','square' (default thumb)
  178. * - facebook-logo: (default true)
  179. * - width: width of the picture in pixels
  180. * - height: height of the picture in pixels
  181. * @return string fb tag for profile picture or empty string if uid is not present
  182. * @access public
  183. */
  184. function picture($uid = null, $options = array()){
  185. $options = array_merge(
  186. array(
  187. 'uid' => $uid,
  188. 'facebook-logo' => 1,
  189. ),
  190. $options
  191. );
  192. if($options['uid']){
  193. return $this->__fbTag('fb:profile-pic', '', $options);
  194. }
  195. else {
  196. return "";
  197. }
  198. }
  199. /**
  200. * Build a like box
  201. * $facebook->init() is required for this
  202. * @link http://developers.facebook.com/docs/reference/plugins/like-box
  203. * @param array of options to pass into likebox
  204. * - stream : 1 turns stream on, 0 turns stream off (default false)
  205. * - header : 1 turns header on, 0 turns logobar off (default false)
  206. * - width : width of the box (default 300)
  207. * - connections : number of connections to show (default 10)
  208. * - colorscheme : dark | light (default light)
  209. */
  210. function likebox($url = null, $options = array()){
  211. $options = array_merge(
  212. array(
  213. 'href' => $url,
  214. 'stream' => 'false',
  215. 'header' => 'false',
  216. 'width' => '300',
  217. 'connections' => '10'
  218. ),
  219. $options
  220. );
  221. return $this->__fbTag('fb:like-box', '', $options);
  222. }
  223. /**
  224. * Build a become a fan, fanbox
  225. * $facebook->init() is required for this
  226. * @param array options to pass into fanbox
  227. * - stream : 1 turns stream on, 0 turns stream off (default 0)
  228. * - connections : 1 turns connections on, 0 turns connections off (default 0)
  229. * - logobar : 1 turns logobar on, 0 turns logobar off (default 0)
  230. * - profile_id : Your Application Id (default Configure::read('Facebook.app_id')
  231. * @return string xfbhtml tag
  232. * @access public
  233. */
  234. function fanbox($options = array()){
  235. $options = array_merge(
  236. array(
  237. 'profile_id' => Configure::read('Facebook.appId'),
  238. 'stream' => 0,
  239. 'logobar' => 0,
  240. 'connections' => 0,
  241. ),
  242. $options
  243. );
  244. return $this->__fbTag('fb:fan', '', $options);
  245. }
  246. /**
  247. * Build a livestream window to your live stream app on facebook
  248. * $facebook->init() is required for this
  249. * @param array options to pass into livestream
  250. * - event_app_id : Your Application Id (default Configure::read('Facebook.appId')
  251. * - xid : Your event XID
  252. * - width : width of window in pixels
  253. * - height: height of window in pixels
  254. * @return string xfbhtml tag
  255. * @access public
  256. */
  257. function livestream($options = array()){
  258. $options = array_merge(
  259. array(
  260. 'event_app_id' => Configure::read('Facebook.appId'),
  261. 'xid' => 'YOUR_EVENT_XID',
  262. 'width' => '300',
  263. 'height' => '500',
  264. ),
  265. $options
  266. );
  267. return $this->__fbTag('fb:live-stream','',$options);
  268. }
  269. /**
  270. * Build a facebook comments area.
  271. * $facebook->init() is required for this
  272. * @param array of options for comments
  273. * - numposts : number of posts to show (default 10)
  274. * - width : int width of comments blog (default 550)
  275. * @return string xfbhtml tag
  276. * @access public
  277. */
  278. function comments($options = array()){
  279. return $this->__fbTag('fb:comments', '', $options);
  280. }
  281. /**
  282. * Build a facebook recommendations area.
  283. * $facebook->init() is required for this
  284. * @param array of options for recommendations
  285. * - width : int width of object (default 300)
  286. * - height : int height of object (default 300)
  287. * - header : boolean (default true)
  288. * - colorscheme : light, dark (default light)
  289. * - font : default arial
  290. * - bordercolor : color of border (black, white, grey)
  291. * @return string xfbhtml tag
  292. * @access public
  293. */
  294. function recommendations($options = array()){
  295. return $this->__fbTag('fb:recommendations', '', $options);
  296. }
  297. /**
  298. * Build a facebook friendpile area.
  299. * $facebook->init() is required for this
  300. * @param array of options for recommendations
  301. * - numrows : int of rows object (default 1)
  302. * - width : int width of object (default 300)
  303. * @return string xfbhtml tag
  304. * @access public
  305. */
  306. function friendpile($options = array()){
  307. return $this->__fbTag('fb:friendpile', '', $options);
  308. }
  309. /**
  310. * Build a facebook activity feed area.
  311. * $facebook->init() is required for this
  312. * @param array of options for recommendations
  313. * - width : int width of object (default 300)
  314. * - height : int height of object (default 300)
  315. * - header : boolean (default true)
  316. * - colorscheme : light, dark (default light)
  317. * - font : default arial
  318. * - bordercolor : color of border (black, white, grey)
  319. * - recommendations : show recommendations default "false"
  320. * @return string xfbhtml tag
  321. * @access public
  322. */
  323. function activity($options = array()){
  324. return $this->__fbTag('fb:activity', '', $options);
  325. }
  326. /**
  327. * Build a facebook like box
  328. * $facebook->init() is required for this
  329. * @param array of options for like box
  330. * - href : URL to like (default same page)
  331. * - show_faces : boolean (default true)
  332. * - font : font type (arial, lucida grande, segoe ui, tahoma, trebuchet ms, verdana)
  333. * - layout : the layout type if the button (button_count, standard, default: standard)
  334. * - action : the title of the action (like or recommend, default: like)
  335. * - colorscheme : the look of the button (dark or light, default: light)
  336. * @return string xfbhtml tag
  337. * @access public
  338. */
  339. function like($options = array()){
  340. return $this->__fbTag('fb:like', '', $options);
  341. }
  342. /**
  343. * HTML XMLNS tag (required)
  344. * @param array of options
  345. * @example $this->Facebook->init();
  346. * @return string of scriptBlock for FB.init() or error
  347. * @access public
  348. */
  349. function init($options = array()){
  350. if(FacebookInfo::getConfig('appId')){
  351. $appId = FacebookInfo::getConfig('appId');
  352. $session = json_encode($this->Session->read('FB.Session'));
  353. $init = '<div id="fb-root"></div>';
  354. $init .= $this->Html->scriptBlock(
  355. "
  356. window.fbAsyncInit = function() {
  357. FB.init({
  358. appId : '{$appId}',
  359. session : {$session}, // don't refetch the session when PHP already has it
  360. status : true, // check login status
  361. cookie : true, // enable cookies to allow the server to access the session
  362. xfbml : true // parse XFBML
  363. });
  364. // whenever the user logs in, we refresh the page
  365. FB.Event.subscribe('auth.login', function() {
  366. window.location.reload();
  367. });
  368. };
  369. (function() {
  370. var e = document.createElement('script');
  371. e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
  372. e.async = true;
  373. document.getElementById('fb-root').appendChild(e);
  374. }());
  375. ",
  376. $options
  377. );
  378. return $init;
  379. }
  380. else {
  381. return "<span class='error'>No Facebook configuration detected. Please add the facebook configuration file to your config folder.</span>";
  382. }
  383. }
  384. /**
  385. * Generate a facebook tag
  386. * @param string fb:tag
  387. * @param string label to pass inbetween the tag
  388. * @param array of options as name=>value pairs to add to facebook tag attribute
  389. * @access private
  390. */
  391. private function __fbTag($tag, $label, $options){
  392. //TODO make this a little nicer, pron to errors if a value has a ' in it.
  393. $retval = "<$tag";
  394. foreach($options as $name => $value){
  395. if($value === false) $value = 0;
  396. $retval .= " " . $name . "='" . $value . "'";
  397. }
  398. $retval .= ">$label</$tag>";
  399. return $retval;
  400. }
  401. }