Login   Register  
PHP Classes
elePHPant
Icontem

File: facebook.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Viet Vu  >  Facebook Plugin  >  facebook.php  >  Download  
File: facebook.php
Role: Class source
Content type: text/plain
Description: jxFacebook
Class: Facebook Plugin
Insert Facebook social plugins in Web pages
Author: By
Last change: # fix some bugs
- remove default opengraphs
^ allow adding multi opengraphs like og:image can have multi row
^ add close for meta tags
Date: 2012-08-24 22:36
Size: 14,909 bytes
 

Contents

Class file image Download
<?php

/**
 * Facebook library
 * This class provide method to work with Facebook SDK / plugins and Opengraph
 * PHP version 5.3
 *
 * @author	JOOservices Ltd Company
 * @copyright	Copyright (c) 2011 - 2012 JOOservices Ltd Company. All rights reserved.
 * @license	GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
 */
?>
<?php

/**
 * @role    Always check class exists before create it
 */
if ( !class_exists ( 'JxFacebook' ) ) {

    /**
     * Facebook base object
     */
    class JxFacebookObject {

        protected $opengraph = array ( ) ; /* used in <head><meta> */
        protected $attributes = array ( ) ; /* use for Facebook plugins */
        protected $app_id = '129458053806961' ;
        protected $admins = '100002155587121' ;
        protected $url = '' ;

        /**
         * Construct method
         * @param string $url
         */
        public function __construct ( $url = '' ) {
            $this->url = $url ;
        }

        /**
         * Method to get singleton instance of this class
         * @staticvar JxFacebook $instance
         * @param string $url
         * @return \JxFacebook
         */
        public static function getInstance ( $url = '' ) {
            static $instance ;
            if ( !isset ( $instance ) )
                $instance = new JxFacebook ( $url ) ;
            return $instance ;
        }

        /**
         * Method to set opengraph
         * @link https://developers.facebook.com/docs/opengraph/
         * @param string $name
         * @param string $value
         */
        public function setOpenGraph ( $name , $value , $prefix = 'og' ) {
            $this->opengraph[$prefix . ':' . $name][] = $value ;
        }

        /**
         * Reset datas of an opengraph
         * @param string $name
         * @param string $prefix
         */
        public function cleanOpenGraph ( $name , $prefix = 'og' ) {
            $this->opengraph[$prefix . ':' . $name] = array ( ) ;
        }

        /**
         * Method to get opengraph
         * @param string $name
         * @param string $def
         * @return array
         */
        public function getOpenGraph ( $name , $def = '' , $prefix = 'og' ) {
            if ( isset ( $this->opengraph[$prefix . ':' . $name] ) ) {
                return $this->opengraph[$prefix . ':' . $name] ;
            }else
                return $def ;
        }

        /**
         * Method to set attribute use for plugin
         * @param string $name
         * @param string $value
         */
        public function setAttribute ( $name , $value ) {
            $this->attributes[$name] = $value ;
        }

        /**
         * Method to get attribute use for plugin
         * @param string $name
         * @param string $def
         * @return string
         */
        public function getAttribute ( $name , $def = '' ) {
            if ( isset ( $this->attributes[$name] ) ) {
                return $this->attributes[$name] ;
            }else
                return $def ;
        }

        /**
         * Method to set facebook appid
         * @param string $id
         */
        public function setAppId ( $id ) {
            $this->app_id = $id ;
        }

        /**
         * Method to get facebook appid
         */
        public function getAppId ( $def = '129458053806961' ) {
            if ( isset ( $this->app_id ) )
                return $this->app_id ;
            else
                return $def ;
        }

        /**
         * Method to set facebook admins id
         * @param string $id
         */
        public function setAdmins ( $id ) {
            $this->admins = $id ;
        }

        /**
         * Method to get facebook admins id
         * @param string $def
         * @return string
         */
        public function getAdmins ( $def = '100002155587121' ) {
            if ( isset ( $this->admins ) )
                return $this->admins ;
            else
                return $def ;
        }

        /**
         * Method to set url use for attribute & opengraph
         * @param string $url
         */
        public function setUrl ( $url ) {
            $this->url = $url ;
            $this->setAttribute ( 'href' , $url ) ;
            $this->setOpenGraph ( 'og:url' , $url ) ;
        }

        /**
         * Method to get current using url
         * @return string
         */
        public function getUrl () {
            return $this->url ;
        }

    }

    /**
     * Facebook class
     * @package
     * @subpackage
     * @uses
     *
     * @tutorial
     */
    class JxFacebook extends JxFacebookObject {

        /**
         * Construct method
         * @todo Need use with pure PHP instead Zend
         */
        public function __construct ( $url = '' ) {
            parent::__construct ( $url ) ;
        }

        /**
         * Method to get instance of
         * @staticvar self $instance
         * @return \self
         */
        public static function getInstance ( $url = '' ) {
            static $instance ;
            if ( !isset ( $instance ) ) {
                $instance = new self ( $url ) ;
            }
            return $instance ;
        }

        /**
         * Get Facebook SDK class
         * @param string $appId
         * @param string $secret
         * @return \Facebook
         */
        public function getSdk ( $appId , $secret ) {
            require_once 'sdk/facebook.php' ;
            return new Facebook ( array ( 'appId' => $appId , 'secret' => $secret , ) ) ;
        }

        /**
         * Method to get facebook scripts
         * @return string
         */
        public function getScript ( $langTag = 'en_US' ) {
            $script = '<div id="fb-root"></div>
            <script>
                (function(d, s, id) {
                    var js, fjs = d.getElementsByTagName(s)[0];
                    if (d.getElementById(id)) return;
                    js = d.createElement(s); js.id = id;
                    js.src = "//connect.facebook.net/' . $langTag . '/all.js#xfbml=1&appId=' . $this->getAppId () . '";
                    fjs.parentNode.insertBefore(js, fjs);
               }(document, \'script\', \'facebook-jssdk\'));
            </script>' ;
            return $script ;
        }

        /**
         * @link    https://developers.facebook.com/docs/reference/javascript/FB.init/
         * @param string $langTag
         * @return string
         */
        public function loadScript ( $langTag = 'en_US' ) {
            static $loaded = false ;
            if ( $loaded == false ) {
                $script = '
                <div id="fb-root"></div>
                <script>
                    if (typeof window.FB === "undefined") {
                        window.fbAsyncInit = function() {
                            FB.init({

                            });

                            // Additional initialization code here
                        };

                        // Load the SDK Asynchronously
                        (function(d){
                            var js, id = \'facebook-jssdk\', ref = d . getElementsByTagName(\'script\')[0];
                            if (d . getElementById(id)) {
                                return;
                            }
                            js = d . createElement(\'script\');
                            js.id = id;
                            js.async = true;
                            js.src = "//connect.facebook.net/' . $langTag . '/all.js#xfbml=1&appId=' . $this->getAppId () . '";
                            ref . parentNode . insertBefore(js, ref);
                        }(document));
                    }
                </script>' ;
                $loaded = true ;
            } else {
                $script = '' ;
            }
            return $script ;
        }

        /**
         * Method to get facebook attributes for <html> tag
         * @return string
         */
        public function getHtml () {
            $type = $this->getOpenGraph ( 'type' , array ( 'website' ) ) ;
            $html = 'xmlns:fb = "http://ogp.me/ns/fb#"' ;
            $html .='prefix = "og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# ' . $type[0] . ': http://ogp.me/ns/' . $type[0] . '# "' ;
            return $html ;
        }

        /**
         * Method to get head <meta> tag
         * @return string
         */
        public function getHead () {
            $head = "\n" ;
            foreach ( $this->opengraph as $key => $opengraph ) {
                $prev = '' ;
                foreach ( $opengraph as $value ) {
                    if ( $value != '' && $value != $prev ) {
                        $head .= '<meta property = "' . $key . '" content = "' . $value . '" />' . "\n" ;
                        $prev = $value ;
                    }
                }
            }
            $head .= '<meta property = "fb:admins" content = "' . $this->getAdmins () . '" />' . "\n" ;
            $head .= '<meta property = "fb:app_id" content = "' . $this->getAppId () . '" />' . "\n" ;
            return $head ;
        }

        /**
         * Method to get plugin html
         * @param string $name
         * @return string
         */
        public function getPlugin ( $name ) {
            $class = 'jooxfb-' . $name . ' fb-' . $name ;
            $html = '<div class = "' . $class . '"' ;
            foreach ( $this->attributes as $key => $value ) {
                $html .= 'data-' . $key . ' = "' . $value . '"' ;
            }
            $html .= '></div>' ;
            return $html ;
        }

        /**
         * @link https://developers.facebook.com/docs/reference/dialogs/
         * @param type $dialog
         * @param type $obj
         * @param type $type
         * @return string
         */
        public function getDialog ( $dialog = 'feed' , $obj = array ( ) , $type = 'url' ) {
            $fbDialog = 'https://www.facebook.com/dialog/' . $dialog . '?' ;
            $defObject = array (
                'redirect_uri' => $this->url . '&fb_redirected' ,
                'display' => ($type == 'url') ? 'page' : 'popup' , /* https://developers.facebook.com/docs/reference/dialogs/#display */
                'from' => '' ,
                'to' => '' ,
                'link' => $this->url ,
                'picture' => '' ,
                'source' => '' ,
                'name' => '' ,
                'caption' => '' ,
                'description' => '' ,
                'properties' => '' ,
                'actions' => '' ,
                'ref' => ''
                    ) ;
            $obj = array_merge ( $obj , $defObject ) ;

            switch ( $type ) {
                case 'url' :
                    if ( $type == 'url' ) {
                        $url = 'app_id=' . $this->getAppId () ;
                        foreach ( $obj as $key => $value ) {
                            if ( $value != '' ) {
                                $url .= '&' . $key . '=' . urlencode ( $value ) ;
                            }
                        }
                        return $fbDialog . $url ;
                    }
                case 'json' :
                    return json_encode ( $obj ) ;
                case 'script' :
                    /* return javascript object use for FB.ui */
                    $script = '
                            var jxFacebookUi = {}
                        ' ;
                    break ;
            }
        }

        /**
         *
         * @param array $data
         */
        public function setProperties ( $data ) {
            if ( is_array ( $data ) ) {
                foreach ( $data as $name => $value ) {
                    $this->setAttribute ( $name , $value ) ;
                }
            }
        }

        /**
         * Method to set og:type
         * @param string $content
         */
        public function setType ( $data ) {
            $this->setOpenGraph ( 'type' , $data ) ;
        }

        /**
         * Method to set og:title
         * @param string $data
         */
        public function setTitle ( $data ) {
            $this->setOpenGraph ( 'title' , $data ) ;
        }

        /**
         * Method to set og:description
         * @param string $data
         */
        public function setDescription ( $data ) {
            $this->setOpenGraph ( 'description' , $data ) ;
        }

        /**
         * Method to quick set OpenGraph siteName
         * @param string $content
         */
        public function setSiteName ( $content ) {
            $this->setOpenGraph ( 'site_name' , $content ) ;
        }

        /**
         * Shortcut method to set Opengraphs for article type
         * @link https://developers.facebook.com/docs/opengraph/objects/builtin/
         * @param string $title
         * @param string $description
         * @param string $image
         * @param string $siteName
         * @param string $article
         * @return string
         */
        public function getArticle ( $title , $description , $image , $siteName , $article = array ( ) ) {
            $this->setType ( 'article' ) ;
            $this->setTitle ( $title ) ;
            $this->setDescription ( $description ) ;
            $this->setSiteName ( $siteName ) ;
            $this->setOpenGraph ( 'image' , $image ) ;
            foreach ( $article as $key => $value ) {
                $this->setOpenGraph ( $key , $value , 'article:' ) ;
            }
            return $this->getHead () ;
        }

        /**
         * Shortcut method to set Opengraphs for blog type
         * @link https://developers.facebook.com/docs/opengraph/objects/builtin/
         * @param string $title
         * @param string $description
         * @param mixed $image
         * @param string $url
         * @return string   complete <head>
         */
        public function getBlog ( $title , $description , $image ) {
            $this->setType ( 'blog' ) ;
            $this->setTitle ( $title ) ;
            $this->setDescription ( $description ) ;
            $this->setOpenGraph ( 'image' , $image ) ;
            return $this->getHead () ;
        }

        /**
         * Shortcut method to set Opengraphs for website type
         * @link https://developers.facebook.com/docs/opengraph/objects/builtin/
         * @param string $title
         * @param string $description
         * @param mixed $image
         * @param string $url
         * @return string   complete <head>
         */
        public function getWebsite ( $title , $description , $image ) {
            $this->setType ( 'website' ) ;
            $this->setTitle ( $title ) ;
            $this->setDescription ( $description ) ;
            $this->setOpenGraph ( 'image' , $image ) ;
            return $this->getHead () ;
        }

    }

}
?>