/**
  * Performs a FB login
  */
 public function fblogin()
 {
     $fb_user = $this->Connect->FB->getUser();
     if (!$fb_user) {
         if (isset($this->request->query['error_reason'])) {
             // an error has occurred
             //fblogin?error=access_denied&error_code=200&error_description=Permissions+error&error_reason=user_denied&state=dca62b8eb289375be97d4783b4caedc4
             $error = $this->request->query['error'];
             $error_code = $this->request->query['error_code'];
             $error_description = $this->request->query['error_description'];
             $error_reason = $this->request->query['error_reason'];
             if ($error_reason == 'user_denied') {
                 $this->Session->setFlash(__d('paszport', 'LC_PASZPORT_FACEBOOK_LOGIN_USER_DENIED', true), null, array('class' => 'alert-error'));
                 $this->redirect(array('action' => 'login'));
             } else {
                 $this->Session->setFlash(__d('paszport', 'LC_PASZPORT_FACEBOOK_LOGIN_FAILED', true), null, array('class' => 'alert-error'));
                 $this->redirect(array('action' => 'login'));
             }
         } else {
             $this->redirect($this->Connect->FB->getLoginUrl(array('scope' => 'email,user_birthday')));
         }
     } else {
         # we do have access to user details
         $user_data = $this->Connect->FB->api('/me/?fields=id,first_name,last_name,email,gender,picture.type(square).width(200),birthday,locale');
         if ($user_data == null) {
             $this->redirect($this->Connect->FB->getLoginUrl(array('scope' => 'email,user_birthday')));
         }
         /* $conds = array(
                'conditions' => array(
                    'OR' => array(
                        array('User.facebook_id' => $user_data['id']),
                        array('User.email' => $user_data['email'])
                    ),
                ),
                'contain' => array('Language', 'Group', 'UserExpand'),
            ); */
         // Fatal Error (1): Call to a member function find() on a non-object in [/home/www/portal-v2/_mojePanstwo-Portal/app/Plugin/Paszport/Controller/UsersController.php, line 145]
         // Trzeba wszystkie wywołania $this->PaszportApi zmienić na odwołania do nowego API przez model User
         $_user = new User();
         $user = $_user->findFacebookUser($user_data['id'], $user_data['email']);
         if (!isset($user['user']) || empty($user['user'])) {
             $user = false;
         }
         if ($user && $user['user']['facebook_id']) {
             # if user is already FB connected to us, just log him in
             $this->Auth->login($user['user']);
             // true
             $this->_log(array('msg' => 'LC_PASZPORT_LOG_LOGIN_FB', 'ip' => $this->request->clientIp(), 'user_agent' => env('HTTP_USER_AGENT')));
             if ($this->Session->read('API.gate')) {
                 $service = $this->Session->read('API.service');
                 $session = $this->Session->read('API.session');
                 $this->externalgate($service, $session);
             }
             $this->redirect($this->Auth->redirectUrl());
         } else {
             # if not we will attempt to create new user based on his facebook data or we will merge his existing account
             if ($user && $user['User']['email']) {
                 # merge attempt
                 $this->User->id = $user['User']['id'];
                 $this->PassportApi->field('users', $user['User']['id'], array('User' => array('facebook_id' => $user_data['id'])));
                 $this->Session->setFlash(__d('paszport', 'LC_PASZPORT_FB_ACCOUNT_MERGED', true), 'alert', array('class' => 'alert-success'));
                 $this->Auth->login($user['User']);
                 if ($this->Session->read('API.gate')) {
                     $service = $this->Session->read('API.service');
                     $session = $this->Session->read('API.session');
                     $this->externalgate($service, $session);
                 }
                 $this->redirect(array('action' => 'index'));
                 // show profile for user to verify it
             } else {
                 # new user
                 $to_save = array('User' => array('personal_name' => $user_data['first_name'], 'personal_lastname' => $user_data['last_name'], 'username' => $user_data['first_name'] . '' . $user_data['last_name'] . rand(0, 999), 'email' => $user_data['email'], 'password' => $this->Auth->password(md5($user_data['id'] . $user_data['email'])), 'repassword' => $this->Auth->password(md5($user_data['id'] . $user_data['email'])), 'facebook_id' => (int) $user_data['id'], 'personal_bday' => date('Y-m-d', strtotime($user_data['birthday'])), 'personal_gender' => $this->__fbGender($user_data['gender']), 'language_id' => $this->__fbLanguage($user_data['locale']), 'photo' => preg_replace('/https/', 'http', $user_data['picture']['data']['url']), 'source' => 'facebook', 'group_id' => 1));
                 $resp_user = $_user->register($to_save);
                 if (array_key_exists('user', $resp_user) && !empty($resp_user['user'])) {
                     $this->Session->setFlash(__d('paszport', 'LC_PASZPORT_FACEBOOK_REGISTER', true), null, array('class' => 'alert-success'));
                     $this->Auth->login($resp_user['user']);
                     $this->Session->write('FB_JUST_REGISTERED', true);
                     if ($this->Session->read('API.gate')) {
                         $service = $this->Session->read('API.service');
                         $session = $this->Session->read('API.session');
                         $this->externalgate($service, $session);
                     }
                     $this->redirect(array('action' => 'login'));
                 } else {
                     $this->Session->setFlash(__d('paszport', 'LC_PASZPORT_FACEBOOK_REGISTER_FAILED', true), null, array('class' => 'alert-error'));
                     $this->redirect(array('action' => 'login'));
                 }
             }
         }
     }
 }