You are here

function drush_redhen_contact_link_users in RedHen CRM 8

Implements drush_hook_COMMAND().

Search for users without Redhen Contacts and attempt to connect them to existing Contacts based on email address.

File

modules/redhen_contact/redhen_contact.drush.inc, line 35
Drush tools for the Redhen Contacts module.

Code

function drush_redhen_contact_link_users($check = 50) {
  $query = \Drupal::database()
    ->select('users_field_data', 'u');
  $query
    ->leftJoin('redhen_contact', 'rc', 'rc.uid = u.uid');
  $query
    ->fields('u', [
    'uid',
    'mail',
  ]);
  $query
    ->isNull('rc.uid');
  $query
    ->range(0, $check);
  $results = $query
    ->execute()
    ->fetchAllAssoc('uid');
  $log = [];
  foreach ($results as $orphan) {
    $contacts = Contact::loadByMail($orphan->mail);
    if ($contacts) {
      foreach ($contacts as $contact) {
        if (!$contact
          ->getUserId()) {

          // We have a match!
          $contact
            ->setUserId($orphan->uid);
          $contact
            ->save();
          $log[$orphan->uid] = $contact
            ->id();
          continue 2;
        }
      }
    }
  }
  if (empty($log) && count($results) == $check) {
    drush_print("No User/Contact connections were created from " . count($results) . " Users found without Contacts. You may want to re-run this function with a higher limit.");
  }
  else {
    drush_print(count($log) . " User/Contact connections created from " . count($results) . " Users found without Contacts.");
  }
}