You are here

function party_hat_hats_assign in Party 7

Same name and namespace in other branches
  1. 8.2 modules/party_hat/party_hat.module \party_hat_hats_assign()

Assign a number of hats to a party

Parameters

$party:

$hats: An array of hat machine names to assign.

boolean $save: (default: True) If true, save the party and trigger rules after assigning hats.

6 calls to party_hat_hats_assign()
hook_party_acquisition_post_acquisition in ./party.api.php
Allow modules to act post acquisition.
PartyB2BContactPartyMigration::complete in starterkits/party_starterkit_b2b/party_starterkit_b2b_migrate/party_starterkit_b2b_migrate_contacts.inc
Attach profile2 from PartyOrganizationProfile2Migration
PartyB2BOrganizationPartyMigration::complete in starterkits/party_starterkit_b2b/party_starterkit_b2b_migrate/party_starterkit_b2b_migrate_organizations.inc
Attach profile2 from PartyOrganizationProfile2Migration
party_hat_party_acquisition_post_acquisition in modules/party_hat/party_hat.party_acquisition.inc
Implements hook_party_acquisition_post_acquisition().
party_hat_rules_assign_hat in modules/party_hat/party_hat.rules.inc
Implement the assign hat rule.

... See full list

File

modules/party_hat/party_hat.module, line 455
party_hat.module Provides an extensible access system for parties.

Code

function party_hat_hats_assign($party, $hats, $save = TRUE) {

  // Get the hat items from the party object
  $hat_items = field_get_items('party', $party, 'party_hat');

  // If there are no hats, set an empty array.
  if (empty($hat_items)) {
    $hat_items = array();
  }
  foreach ($hats as $hat) {
    $has_hat = FALSE;
    foreach ($hat_items as $item) {
      if ($item['hat_name'] == $hat) {
        $has_hat = TRUE;
      }
    }

    // Don't add the hat if the Party already has it
    if ($has_hat) {
      continue;
    }
    $assigned_hats[] = $hat;
    $hat_items[]['hat_name'] = $hat;
  }
  $party->party_hat[LANGUAGE_NONE] = $hat_items;

  // Stop here if we're not saving the party.
  if (!$save) {
    return;
  }
  party_save($party);
  if (isset($assigned_hats)) {
    if (module_exists('rules')) {
      rules_invoke_all('party_hat_assign_hats', $party, $assigned_hats);
    }
    else {
      module_invoke_all('party_hat_assign_hats', $party, $assigned_hats);
    }
  }
}