You are here

public function PartyController::setPrimaryFields in Party 8.2

Same name and namespace in other branches
  1. 7 includes/party.entity.inc \PartyController::setPrimaryFields()

Set the primary fields for the party.

Parameters

$party: A party object.

1 call to PartyController::setPrimaryFields()
PartyController::invoke in includes/party.entity.inc
Implements EntityAPIControllerInterface.

File

includes/party.entity.inc, line 103
Contains the controller classes for Party entities.

Class

PartyController
The API controller class for the Party entity.

Code

public function setPrimaryFields($party, $store = TRUE) {
  $primary_fields = variable_get('party_primary_fields', array());

  // Update the email.
  if (!empty($primary_fields['email'])) {

    // Extract our info from the setting.
    list($data_set_name, $field_name, $column) = explode(':', $primary_fields['email']);
    $controller = party_get_crm_controller($party, $data_set_name);
    if ($entity = $controller
      ->getEntity()) {
      if ($field_name) {
        $items = field_get_items($controller
          ->getDataInfo('entity type'), $entity, $field_name);
        if ($items) {
          $item = reset($items);
          $party->email = $item[$column];
        }
        else {
          $party->email = NULL;
        }
      }
      else {
        $party->email = $entity->{$column};
      }
    }
  }
  if (empty($party->email) && !empty($primary_fields['email2'])) {

    // Extract our info from the setting.
    list($data_set_name, $field_name, $column) = explode(':', $primary_fields['email2']);
    $controller = party_get_crm_controller($party, $data_set_name);
    if ($entity = $controller
      ->getEntity()) {
      if ($field_name) {
        $items = field_get_items($controller
          ->getDataInfo('entity type'), $entity, $field_name);
        if ($items) {
          $item = reset($items);
          $party->email = $item[$column];
        }
        else {
          $party->email = NULL;
        }
      }
      else {
        $party->email = $entity->{$column};
      }
    }
  }
  if ($store && isset($party->pid)) {

    // Save the primary feilds to the database.
    db_query('UPDATE {party} SET email = :email WHERE pid = :pid', array(
      ':email' => $party->email,
      ':pid' => $party->pid,
    ));
  }
}