You are here

function search_api_saved_searches_user_update in Search API Saved Searches 7

Same name and namespace in other branches
  1. 8 search_api_saved_searches.module \search_api_saved_searches_user_update()

Implements hook_user_update().

If a user gets activated, associate saved searches with the same mail address with them.

If a user gets deactivated, disable all related saved searches.

Also, change mail address of saved searches when the user mail address changes.

File

./search_api_saved_searches.module, line 317
Offers the ability to save searches and be notified of new results.

Code

function search_api_saved_searches_user_update(&$edit, $account, $category) {

  // Sometimes this update hook is invoked without setting $account->original.
  // In this case, we need to load the original ourselves.
  if (empty($account->original)) {
    if (!empty($account->uid)) {
      $account->original = entity_load_unchanged('user', $account->uid);
    }

    // If the original couldn't be loaded, we cannot do anything here.
    if (empty($account->original)) {
      return;
    }
  }

  // For newly activated users, transfer all saved searches with their mail
  // address to them.
  if (!empty($account->status) && empty($account->original->status)) {
    foreach (search_api_saved_search_load_multiple(FALSE, array(
      'mail' => $account->mail,
      'uid' => 0,
    )) as $search) {
      $search->uid = $account->uid;
      if (empty($search
        ->settings()->options['registered_user_delete_key'])) {
        unset($search->options['key']);
      }
      $search
        ->save();
    }
  }

  // If an account gets deactivated/banned, disable all associated searches.
  if (empty($account->status) && !empty($account->original->status)) {
    foreach (search_api_saved_search_load_multiple(FALSE, array(
      'uid' => $account->uid,
    )) as $search) {
      $search->enabled = FALSE;
      $search
        ->save();
    }
  }

  // If the user's mail address changed, also change the mail address of the
  // user's saved searches from previous (original) to current address.
  if ($account->mail != $account->original->mail) {
    foreach (search_api_saved_search_load_multiple(FALSE, array(
      'mail' => $account->original->mail,
      'uid' => $account->uid,
    )) as $search) {
      $search->mail = $account->mail;
      $search
        ->save();
    }
  }
}