/**
  * Setup a default Facebook app
  **/
 public function requireDefaultRecords()
 {
     $facebook = FacebookApp::get()->count();
     if (!$facebook) {
         $facebook = FacebookApp::create();
         $facebook->write();
     }
 }
 /**
  * Log the user in via an existing Facebook account connection.
  *
  * @return SS_HTTPResponse
  **/
 public function login()
 {
     $form = $this->Form();
     if ($this->request->getVar("error")) {
         $form->sessionMessage("Unable to obtain access to Facebook.", "bad");
         return $this->renderWith(array("FacebookController", "Page", "Controller"));
     }
     $facebookApp = FacebookApp::get()->first();
     if (!$facebookApp || !$facebookApp->EnableFacebookLogin) {
         $form->sessionMessage("Facebook Login is disabled.", "bad");
     } else {
         if ($member = Member::currentUser()) {
             $member->logOut();
         }
         $facebook = $facebookApp->getFacebook();
         $user = $facebook->getUser();
         if ($user) {
             $member = Member::get()->filter("FacebookUserID", $user)->first();
             if ($member) {
                 $member->logIn();
                 $form->sessionMessage("You have logged in with your Facebook account.", "good");
                 $member->extend("onAfterMemberLogin");
             } else {
                 if ($facebookApp->EnableFacebookSignup) {
                     // Attempt to sign the user up.
                     $member = new Member();
                     // Load the user from Faceook
                     $user_profile = $facebook->api("/me");
                     if ($user_profile) {
                         // Fill in the required fields.
                         $access_token = Session::get("fb_" . $facebookApp->FacebookConsumerKey . "_access_token");
                         $signup = $member->connectFacebookAccount($user_profile, $access_token, $facebookApp->config()->get("required_user_fields"));
                         if ($signup->valid()) {
                             $member->logIn();
                             $form->sessionMessage("You have signed up with your Facbeook account.", "good");
                             // Facebook Hooks
                             $this->extend("onAfterFacebookSignup", $member);
                         } else {
                             $form->sessionMessage($signup->message(), "bad");
                         }
                     } else {
                         $form->sessionMessage("Unable to load your Facbeook account.", "bad");
                     }
                 } else {
                     $form->sessionMessage("Unable to log in with Facebook.", "bad");
                 }
             }
         } else {
             $params = $facebookApp->getLoginUrlParams();
             $url = $facebook->getLoginUrl($params);
             if ($url) {
                 return $this->redirect($url, 302);
             } else {
                 $form->sessionMessage("Unable to login to Facebook at this time.", "bad");
             }
         }
     }
     // Extend Failed facebook login
     if (!Member::currentUser()) {
         $this->extend("onAfterFailedFacebookLogin");
     }
     return $this->renderWith(array("FacebookController", "Page", "Controller"));
 }
 /**
  * Fetches the sites Facebook Applications.
  *
  * @return FacebookApp
  **/
 public function getFacebookApp()
 {
     return FacebookApp::get()->first();
 }
 /**
  * Returns the Facebook App for the current site.
  *
  * @return FacebookApp
  **/
 public function getFacebookApp()
 {
     if ($this->facebook) {
         return $this->facebook;
     }
     return $this->facebook = FacebookApp::get()->first();
 }
 /**
  * Clears out the facebook session keys.
  **/
 public function memberLoggedOut()
 {
     $facebookApp = FacebookApp::get()->first();
     if ($facebookApp) {
         foreach (array('state', 'code', 'access_token', 'user_id') as $key) {
             Session::clear("fb_" . $facebookApp->FacebookConsumerKey . "_" . $key);
         }
     }
 }