You are here

function party_party_primary_fields_alter in Party 7

Implements hook_party_primary_fields_alter().

File

./party.party.inc, line 120
Party general hook include.

Code

function party_party_primary_fields_alter(Party $party, &$needs_store) {

  // Check we are not creating a duplicate primary email address.
  if ($needs_store && variable_get('party_ensure_no_dup_emails', FALSE)) {

    // See if we have an email address to check for duplicates.
    if (!empty($party->email)) {
      $query = party_query();
      $query
        ->fields('party');
      $query
        ->range(0, 1);
      $query
        ->condition('email', $party->email);

      // If already saved,
      if (!empty($party->pid)) {
        $query
          ->condition('pid', $party->pid, '!=');
      }

      // If the email is already used, clear it.
      if ($match = $query
        ->execute()
        ->fetchAssoc()) {

        // Log a message.
        $args = array(
          '@party_email' => $party->email,
          '@matched_party_label' => $match['label'],
          '@matched_party_id' => $match['pid'],
        );
        $message = 'Prevented saving party primary email address @party_email
          as the address is already used by @matched_party_label
          (@matched_party_id). The party has been saved with the email left
          empty.';
        drupal_set_message(t($message, $args), 'warning', FALSE);
        watchdog('party', $message, $args);

        // Clear the email.
        $party->email = NULL;
      }
    }
  }
}