You are here

function party_hat_hats_assign in Party 8.2

Same name and namespace in other branches
  1. 7 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.

5 calls to party_hat_hats_assign()
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_rules_assign_hat in modules/party_hat/party_hat.rules.inc
Implement the assign hat rule.
party_user_create_party_for_user in modules/party_user/party_user.module
party_user_create_party_for_user
party_user_user_insert in modules/party_user/party_user.module
Implements hook_user_insert().

File

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

Code

function party_hat_hats_assign($party, $hats) {

  // 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;
  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);
    }
  }
}