public function RedhenContactEntityController::setUser in RedHen CRM 7
Set the active user for a contact.
Parameters
RedhenContact $contact: Contact object to associate.
Return value
bool TRUE if contact is successfully linked to user.
1 call to RedhenContactEntityController::setUser()
- RedhenContactEntityController::save in modules/
redhen_contact/ lib/ redhen_contact.controller.inc - Saves a contact.
File
- modules/
redhen_contact/ lib/ redhen_contact.controller.inc, line 149 - The controller for the contact entity containing the CRUD operations.
Class
- RedhenContactEntityController
- The controller class for contacts.
Code
public function setUser(RedhenContact $contact, DatabaseTransaction $transaction = NULL) {
$user_result = db_select('redhen_contact_user', 'cu')
->fields('cu')
->condition('uid', $contact->uid, '=')
->execute();
if ($user_result
->rowCount() > 0) {
$rows = $user_result
->fetchAllAssoc('uid');
foreach ($rows as $row) {
// This user is already actively linked to this contact.
if ($row->contact_id == $contact->contact_id && $row->status == 1) {
return TRUE;
}
else {
if ($row->contact_id != $contact->contact_id && $row->status == 1) {
return FALSE;
}
else {
if ($row->contact_id == $contact->contact_id && $row->status == NULL) {
return FALSE;
}
}
}
}
}
// Now grab any record for this contact.
$contact_result = db_select('redhen_contact_user', 'cu')
->fields('cu')
->condition('contact_id', $contact->contact_id, '=')
->condition('status', 1, '=')
->execute();
try {
// Contact does not have a user link, insert.
if ($contact_result
->rowCount() == 0) {
db_insert('redhen_contact_user')
->fields(array(
'contact_id' => $contact->contact_id,
'uid' => $contact->uid,
'status' => 1,
'created' => REQUEST_TIME,
'updated' => REQUEST_TIME,
))
->execute();
module_invoke_all('redhen_contact_user_update', 'insert', $contact);
}
else {
// Update with new user.
db_update('redhen_contact_user')
->fields(array(
'uid' => $contact->uid,
'updated' => REQUEST_TIME,
))
->condition('contact_id', $contact->contact_id)
->condition('status', 1)
->execute();
// Clone the contact to populate with the old user info.
$old_contact = clone $contact;
$old_user = $contact_result
->fetchAssoc();
$old_contact->uid = $old_user['uid'];
module_invoke_all('redhen_contact_user_update', 'update', $contact, $old_contact);
}
return TRUE;
} catch (Exception $e) {
if (isset($transaction)) {
$transaction
->rollback();
}
watchdog_exception($this->entityType, $e);
throw $e;
}
}