You are here

public function PartyAcquisition::acquire in Party 7

Create or acquire a party based off the given parameters.

Parameters

array $values: An array of party fields to match on. Keys are the field and values are the expected values. If empty, we will use the given behavior.

array $context: An array of contextual information. See PartyAcquisition::$default_context for the standard contexts. Callers can use this to pass additional information through to hooks.

string $method: Optionally pass a variable to be filled with the acquisition method. On a successful acquisition this will contain either 'create' or 'acquire'.

Return value

Party|FALSE The acquired or newly created party or FALSE on a failure.

Overrides PartyAcquisitionInterface::acquire

File

includes/party.acquisition.inc, line 149
Base classes for acquisition processes.

Class

PartyAcquisition
Base class for acquiring parties.

Code

public function acquire(array $values, array $context = NULL, &$method = '') {

  // Check all the keys in $values are valid.
  $schema = drupal_get_schema('party');
  $unknown = array_diff(array_keys($values), array_keys($schema['fields']));
  if (!empty($unknown)) {
    watchdog('party', 'Attempt to acquire on unknown values: %values', array(
      '%values' => implode(', ', $unknown),
    ), WATCHDOG_WARNING);
    return FALSE;
  }

  // Set up our context.
  $this
    ->setContext($context);

  // Set up our values.
  $args = array(
    &$values,
    &$this->context,
  );
  $this
    ->invoke('values_alter', $args);

  // Look for a match.
  if (!empty($values)) {
    $party = $this
      ->findMatch($values);
  }
  else {
    $this->context['fail'] = self::FAIL_NO_VALUES;
    $party = FALSE;
  }

  // If we haven't found a match, see if we should create.
  if ($party) {
    $method = 'acquire';
  }
  elseif ($this->context['behavior'] & self::BEHAVIOR_CREATE) {
    $method = 'create';
    $party = party_create();
  }

  // Fire off post acquisition hooks.
  $args = array(
    &$party,
    &$method,
    &$values,
    &$this->context,
  );
  $this
    ->invoke('post_acquisition', $args);

  // Return our result.
  return $party;
}