/**
  * connect() method check if token is valid or not, and either redirect on FB interface or log the customer by creating his account if necessary
  *
  * @param array $aParams
  * @return string
  */
 public function connect(array $aParams = null)
 {
     // use case - not FB code returned - redirect
     if (empty($aParams['code']) || empty($aParams['access_token'])) {
         // redirect on FB interface
         $this->redirect();
     } else {
         // test token
         // $oFBUser = BT_FPCModuleTools::jsonDecode(BT_FPCModuleTools::fileGetContent('https://graph.facebook.com/me?access_token=' . $aParams['access_token']));
         $oFBUser = BT_FPCModuleTools::jsonDecode(BT_FPCModuleTools::fileGetContent('https://graph.facebook.com/me?fields=id,name,last_name,first_name,email&access_token=' . $aParams['access_token']));
         // only if social user id exist
         if (!empty($oFBUser->id)) {
             // set create status
             $bCreateStatus = true;
             $bCreatePs = false;
             $bCreateSocial = false;
             // set FB data
             $this->oUser->id = $oFBUser->id;
             $this->oUser->customerId = 0;
             $this->oUser->first_name = $oFBUser->first_name;
             $this->oUser->last_name = $oFBUser->last_name;
             $this->oUser->email = $oFBUser->email;
             // set birthday
             if (!empty($oFBUser->birthday)) {
                 $aBirthday = explode('/', $oFBUser->birthday);
                 // format date for PS customer table
                 $this->oUser->birthday = $aBirthday[2] . '-' . $aBirthday[0] . '-' . $aBirthday[1];
             }
             // set gender
             if (!empty($oFBUser->gender)) {
                 // get gender ID from PS
                 $this->oUser->gender = parent::getGender($oFBUser->gender);
             }
             // use case - customer is already logged and ask him account association
             if (($iCustomerId = self::$oSession->get('iCustomerId')) !== null) {
                 // get customer id
                 $this->oUser->customerId = $iCustomerId;
                 // delete customer ID session
                 self::$oSession->delete('iCustomerId');
                 // use case - social account not exists
                 if (!parent::existSocialAccount($this->oUser->id)) {
                     $bCreateSocial = true;
                 } else {
                     $aSocialData = parent::getSocialData($oFBUser->id);
                     // get customer data of old account
                     $aCustomerData = parent::getCustomerData($aSocialData[0]['CNT_CUST_ID']);
                     throw new BT_FacebookException(FacebookPsConnect::$oModule->l('This Facebook account has already been linked to a customer account on our shop. The e-mail address of this account on our shop is', 'facebook-connector_class') . ' : "' . $aCustomerData[0]['email'] . '". ' . FacebookPsConnect::$oModule->l('Please contact the merchant to warn him', 'facebook-connector_class') . '.', 520);
                 }
             } else {
                 // test if user already exist in social table
                 $bCreateSocial = !parent::existSocialAccount($this->oUser->id);
                 // test if user already exist in PS table
                 $bCreatePs = !parent::existPsAccount($this->oUser->email);
                 // use case - social account exist
                 if (empty($bCreateSocial)) {
                     // use case - create new PS account and have to delete old social account
                     if (!empty($bCreatePs)) {
                         $iCustomerId = parent::getCustomerId($this->oUser->id);
                         // use case - PS customer account exists too
                         if (parent::existPsAccount($iCustomerId, 'id')) {
                             $bCreateSocial = false;
                             $bCreatePs = false;
                         } else {
                             parent::deleteSocialAccount($this->oUser->id);
                             $bCreateSocial = true;
                         }
                     }
                 } elseif (!$bCreatePs) {
                     $this->oUser->customerId = parent::getCustomerIdByMail($this->oUser->email);
                 }
             }
             // use case - if one of 2 accounts has to be created at least
             if (!empty($bCreatePs) || !empty($bCreateSocial)) {
                 // create customer in 1 or 2 tables
                 $bCreateStatus = parent::createCustomer($bCreatePs, $bCreateSocial);
             }
             // use case  - create status valid
             if ($bCreateStatus) {
                 // load customer
                 parent::loadCustomer($this->oUser->id);
                 // get data session if exists
                 $sData = self::$oSession->get('data');
                 // delete session
                 self::$oSession->delete('data');
                 return $this->login($sData);
             } else {
                 throw new BT_FacebookException(FacebookPsConnect::$oModule->l('Internal server error. Account creation processing is unavailable', 'facebook-connector_class'), 521);
             }
         } else {
             throw new BT_FacebookException(FacebookPsConnect::$oModule->l('The token is not valid. You may be a victim of cross-site request forgery or the connect method to the Facebook URL with HTTPS is not allowed. Please contact the merchant to warn him', 'facebook-connector_class'), 522);
         }
     }
 }