/**
  * @param array $data
  * @return waContact
  */
 protected function afterAuth($data)
 {
     $contact_id = 0;
     // find contact by auth adapter id, i.e. facebook_id
     $contact_data_model = new waContactDataModel();
     $row = $contact_data_model->getByField(array('field' => $data['source'] . '_id', 'value' => $data['source_id'], 'sort' => 0));
     if ($row) {
         $contact_id = $row['contact_id'];
     }
     if (wa()->getUser()->isAuth()) {
         $contact = wa()->getUser();
         if ($contact_id && $contact_id != $contact->getId()) {
             // delete old link
             $contact_data_model->deleteByField(array('contact_id' => $contact_id, 'field' => $data['source'] . '_id'));
             // save new link
             $contact->save(array($data['source'] . '_id' => $data['source_id']));
         }
         $contact_id = $contact->getId();
     }
     // try find user by email
     if (!$contact_id && isset($data['email'])) {
         $contact_model = new waContactModel();
         $sql = "SELECT c.id FROM wa_contact_emails e\n            JOIN wa_contact c ON e.contact_id = c.id\n            WHERE e.email LIKE '" . $contact_model->escape($data['email'], 'like') . "' AND e.sort = 0 AND c.password != ''";
         $contact_id = $contact_model->query($sql)->fetchField('id');
         // save source_id
         if ($contact_id) {
             $tmp = array('contact_id' => $contact_id, 'field' => $data['source'] . '_id', 'sort' => 0);
             // contact already has this source
             $row = $contact_data_model->getByField($tmp);
             if ($row) {
                 $contact_data_model->updateByField($tmp, array('value' => $data['source_id']));
             } else {
                 $tmp['value'] = $data['source_id'];
                 $contact_data_model->insert($tmp);
             }
         }
     }
     // create new contact
     if (!$contact_id) {
         $contact = $this->createContact($data);
         if ($contact) {
             $contact_id = $contact->getId();
         }
     } elseif (empty($contact)) {
         $contact = new waContact($contact_id);
     }
     // auth user
     if ($contact_id) {
         if (!wa()->getUser()->isAuth()) {
             wa()->getAuth()->auth(array('id' => $contact_id));
         }
         return $contact;
     }
     return false;
 }
 /**
  * Delete one or more contacts and fire event сontacts.delete
  *
  * @event contacts.delete
  *
  * @param int|array $id - contact id or array of contact ids
  * @return bool
  */
 public function delete($id, $send_event = true)
 {
     if ($send_event) {
         // Fire @event contacts.delete allowing other applications to clean up their data
         if (!is_array($id)) {
             $id = array($id);
         }
         wa()->event(array('contacts', 'delete'), $id);
     }
     if (is_array($id)) {
         $nid = array();
         foreach ($id as $i) {
             $nid[] = -(int) $i;
         }
     } else {
         $nid = -(int) $id;
     }
     // Delete rights
     $right_model = new waContactRightsModel();
     $right_model->deleteByField('group_id', $nid);
     // Delete settings
     $setting_model = new waContactSettingsModel();
     $setting_model->deleteByField('contact_id', $id);
     // Delete emails
     $contact_email_model = new waContactEmailsModel();
     $contact_email_model->deleteByField('contact_id', $id);
     // Delete from groups
     $user_groups_model = new waUserGroupsModel();
     $user_groups_model->deleteByField('contact_id', $id);
     // Delete from contact lists
     if (class_exists('contactsContactListsModel')) {
         // @todo: Use plugin for contacts
         $contact_lists_model = new contactsContactListsModel();
         $contact_lists_model->deleteByField('contact_id', $id);
     }
     // Delete from contact rights
     $contact_rights_model = new contactsRightsModel();
     $contact_rights_model->deleteByField('group_id', $nid);
     // Delete data
     $contact_data_model = new waContactDataModel();
     $contact_data_model->deleteByField('contact_id', $id);
     $contact_data_text_model = new waContactDataTextModel();
     $contact_data_text_model->deleteByField('contact_id', $id);
     //        // Delete contact from logs
     //        $login_log_model = new waLoginLogModel();
     //        $login_log_model->deleteByField('contact_id', $id);
     // Clear references
     $this->updateByField(array('company_contact_id' => $id), array('company_contact_id' => 0));
     // Delete contact
     return $this->deleteById($id);
 }