/**
  * 11a. Logs in / signs up twitter user
  */
 public function login_twitter_user($tw_user_id)
 {
     if ($tw_user_id) {
         $query = $this->db->where('twitter_id', $tw_user_id)->get('users');
         if ($query->num_rows() === 1) {
             // user found
             $user = new dbUser();
             $user->copy($query->row());
             $user->password = $user->password ? '***' : '';
             return $user;
         } else {
             // no such user, signing up
             $this->load->library('twconnect');
             $user = new dbFullUser();
             $user->twitter_id = $tw_user_id;
             $user->twitter_token = $this->twconnect->tw_access_token['oauth_token'];
             $user->twitter_token_secret = $this->twconnect->tw_access_token['oauth_token_secret'];
             $user->twitter_username = $this->twconnect->tw_user_name;
             $this->twconnect->twaccount_verify_credentials();
             // this will get us user info
             if ($this->twconnect->tw_user_info) {
                 // we have extended user info
                 $user->fullname = $this->twconnect->tw_user_info->name;
                 $user->bio = $this->twconnect->tw_user_info->description;
                 $user->location = $this->twconnect->tw_user_info->location;
                 $user->web = resolve_url($this->twconnect->tw_user_info->url);
                 $user->twitter_name = $this->twconnect->tw_user_info->name;
                 $user->twitter_img_url = $this->twconnect->tw_user_info->profile_image_url;
                 $user->twitter_verified = $this->twconnect->tw_user_info->verified;
                 $user->picture_url = $user->twitter_img_url;
                 $user->big_picture_url = resolve_url('https://api.twitter.com/1/users/profile_image?screen_name=' . $user->twitter_username . '&size=bigger');
             } else {
                 // we failed to get extended user info, but we will try to get it differently via public api request
                 $ok = set_all_info_from_social($user, 'twitter', $user->twitter_username, true);
                 if (!$ok) {
                     // we do not have twitter data
                     $user->fullname = $user->twitter_username;
                 }
             }
             // Inserting user
             $ok = $this->db->insert('users', $user);
             // we don't have id yet...
             $user->id = $this->db->insert_id();
             $this->meet_WhoYouMeet_team($user);
             return $user;
             if (!$ok) {
                 // Cannot insert user
                 return false;
             }
         }
         // end of else - no such user, signing user up
     } else {
         // no $tw_user_id passed
         return false;
     }
 }
 /**
  * 3. Actions with the list of people user wants to meet (via model_imeet.php)
  *    Security of this function relies only on user id stored in the cookie: $this->session->userdata('id').
  *    The seesion cookie should be encrypted via /application/config/config.php (it is not at the moment)
  *    
  *    Parameters $action and $param are passed via request URL: /i/iMeet/$action/$param
  *    $action values:
  *      '' - shows the list of people (not users) the current user wants to meet
  *      'new' - full form to add new person
  *      'add' - validate and add new person to table 'peopletomeet' when added from popup via model_imeet.php
  *		'add_form' - validate and add new person when addded from full form
  *      'add_user' - copy a user to a person record when adding from any user profile page
  *      'edit' - form to edit person details. $param - person id
  *      'update' - validate after editing and update person. $param - person id
  *      'delete' - delete person from the table. $param - person id
  *
  *    All actions can be performed only if the person is in the table of current user.
  *    It is checked by matching user id stored in the session cookie and userid field in 'peopletomeet' table.
  *    If the session cookie is not encrypted (or session stored in the database), the security is compromised.
  */
 public function iMeet($action = '', $param = '')
 {
     // loads view_iMeet via template
     if ($this->session->userdata('logged_in')) {
         $this->load->model('model_imeet');
         $user_id = $this->session->userdata('id');
         switch ($action) {
             case '':
                 // list of people I (user) want to meet
                 $list = $this->model_imeet->get_iMeet_list($user_id);
                 $this->load->view('includes/view_template', array('list' => $list, 'content' => 'iMeet', 'title' => my_page_title('page_iMeet_title')));
                 break;
             case 'person':
                 // shows one person users wants to meet
                 if ($this->session->userdata('logged_in')) {
                     $person = $this->model_imeet->get_personToMeet($user_id, $param);
                     if ($person) {
                         $this->load->model('model_users');
                         if ($person->usertomeetid) {
                             // checking if this person connected to a user who wants to meet me (current user)
                             $user_to_meet = $this->model_users->get_meetMe_user($user_id, $person->usertomeetid);
                             $meet_me = $user_to_meet && $user_to_meet->id == $person->usertomeetid;
                         } else {
                             $meet_me = false;
                         }
                         // checking if it is an ajax request from iMeet page
                         $cont = $this->input->post('cont');
                         if ($cont == 'popup') {
                             // ajax request from page to show person
                             $this->load->view('includes/view_template', array('person' => $person, 'meet_me' => $meet_me, 'content' => 'person', 'title' => my_page_title('page_iMeet_person_title', $person->fullname)));
                         } else {
                             // not popup ajax - showing person on top of "I want to meet" list now
                             $list = $this->model_imeet->get_iMeet_list($user_id);
                             $this->load->view('includes/view_template', array('list' => $list, 'person' => $person, 'meet_me' => $meet_me, 'content' => 'iMeet', 'title' => my_page_title('page_iMeet_person_title', $person->fullname), 'popup_content' => 'person'));
                         }
                     } else {
                         redirect('/i/iMeet');
                     }
                 } else {
                     redirect('/');
                 }
                 break;
             case 'new':
                 // form to add a new person to meet
                 $this->iMeet_form();
                 break;
             case 'add':
                 // adds user from popup, fields validated via Ajax, but we validate them just in case
                 $this->form_validation->set_rules('linkedin_username', 'LinkedIn Profile', 'trim|xss_clean');
                 $this->form_validation->set_rules('twitter_username', 'Twitter Profile', 'trim|xss_clean');
                 $this->form_validation->set_rules('reason', 'Reason to meet', 'trim|xss_clean');
                 if ($this->form_validation->run()) {
                     $new_person = new dbPersonToMeet();
                     set_all_info_from_social($new_person, 'twitter', $this->input->post('twitter_username'), true);
                     set_all_info_from_social($new_person, 'linkedin', $this->input->post('linkedin_username'), true);
                     set_all_info_from_social($new_person, 'facebook', $this->input->post('facebook_username'), true);
                     $this->choose_best_person_picture($new_person, true);
                     $new_person->reason = $this->input->post('reason');
                     if ($new_person->fullname) {
                         // adding person to meet
                         $person_id = $this->model_imeet->add_personToMeet($user_id, $new_person);
                         if ($person_id) {
                             redirect('/i/iMeet/person/' . $person_id);
                         } else {
                             // could not add person
                             $this->session->set_flashdata('error', 'Oops. We could not add person. Please try again.');
                             redirect('/i/iMeet');
                         }
                     } else {
                         // could not retirive information from twitter and linkedin
                         $this->session->set_flashdata('alert', 'We could not retrieve information about the person. Please try again.');
                         redirect('/i/iMeet');
                     }
                 } else {
                     // did not validate form, validation errors will be shown
                     $this->iMeet();
                 }
                 break;
             case 'add_form':
                 // validate and add a new person to meet
                 $new_person = new dbPersonToMeet();
                 $this->iMeet_person_from_form($user_id, $new_person);
                 if ($this->form_validation->run()) {
                     set_all_info_from_social($new_person, 'twitter', $this->input->post('twitter_username'), true);
                     set_all_info_from_social($new_person, 'linkedin', $this->input->post('linkedin_username'), true);
                     set_all_info_from_social($new_person, 'facebook', $this->input->post('facebook_username'), true);
                     $this->choose_best_person_picture($new_person, true);
                     // adding person to meet
                     $ok = $this->model_imeet->add_personToMeet($user_id, $new_person);
                     if ($ok) {
                         redirect('/i/iMeet');
                     } else {
                         // could not add person
                         $this->session->set_flashdata('error', 'Oops. We could not add person. Please try again.');
                         $this->iMeet_form($new_person);
                     }
                 } else {
                     // did not validate form, validation errors will be shown
                     $this->iMeet_form($new_person);
                 }
                 break;
             case 'add_user':
                 // validate and add a person to meet by copying user
                 $this->session->set_userdata('test', 'we are here 4');
                 $this->form_validation->set_rules('reason', lang('form_iMeet_reason_field'), 'trim|xss_clean');
                 $previous_page = $this->input->server('HTTP_REFERER');
                 if ($this->form_validation->run()) {
                     $user_to_meet_id = $this->input->post('user_to_meet_id');
                     $this->load->model('model_users');
                     $user_to_meet = $this->model_users->get_any_user($user_id, $user_to_meet_id);
                     if ($user_to_meet) {
                         $new_person = new dbPersonToMeet();
                         $new_person->copy_from_user($user_to_meet);
                         $new_person->reason = $this->input->post('reason');
                         $new_person->usertomeetid = $user_to_meet_id;
                         $ok = $this->model_imeet->add_personToMeet($user_id, $new_person);
                         if ($ok) {
                             redirect('/i/iMeet');
                         } else {
                             // could not add person
                             $this->session->set_flashdata('error', 'Oops. We could not add person. Please try again.');
                             redirect($previous_page);
                         }
                     } else {
                         // could not retrieve user
                         $this->session->set_flashdata('error', 'Oops. We could not add person. Please try again.');
                         redirect($previous_page);
                     }
                 } else {
                     // did not validate form, validation errors will be shown
                     redirect($previous_page);
                 }
                 break;
             case 'edit':
                 // edit person I want to meet
                 $person = $this->model_imeet->get_personToMeet($user_id, $param);
                 if ($person) {
                     $this->iMeet_form($person, false);
                 } else {
                     // No such person, wrong id was passed
                     redirect('/i/iMeet');
                 }
                 break;
             case 'update':
                 // validate and update person after editing
                 $old_person = $this->model_imeet->get_personToMeet($user_id, $param);
                 if ($old_person) {
                     $person = new dbPersonToMeet();
                     $person->copy($old_person);
                     $this->iMeet_person_from_form($user_id, $person, $param);
                     if ($this->form_validation->run()) {
                         set_all_info_from_social($person, 'twitter', $this->input->post('twitter_username'));
                         set_all_info_from_social($person, 'linkedin', $this->input->post('linkedin_username'));
                         set_all_info_from_social($person, 'facebook', $this->input->post('facebook_username'));
                         $this->choose_best_person_picture($person);
                         // updating person to meet
                         $ok = $this->model_imeet->update_personToMeet($user_id, $param, $person);
                         if ($ok) {
                             redirect('/i/iMeet/person/' . $param);
                         } else {
                             // can't update
                             $this->session->set_flasdata('error', 'Oops. We could not update person. Please try again.');
                             $this->iMeet_form($person, false);
                         }
                     } else {
                         // can't validate - validation error will be shown;
                         $this->iMeet_form($person, false);
                     }
                 } else {
                     // No such person, wrong id was passed
                     redirect('/i/iMeet');
                 }
                 break;
             case 'delete':
                 // delete person I want to meet
                 $this->model_imeet->delete_personToMeet($user_id, $param);
                 redirect('/i/iMeet');
                 break;
             default:
                 redirect('/i/iMeet');
         }
     } else {
         redirect('/');
     }
 }