You are here

function party_hat_hats_unassign in Party 7

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

Unassign hats

Parameters

$party:

$hats: An array of hat maching names to unassign.

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

1 call to party_hat_hats_unassign()
party_hat_rules_unassign_hat in modules/party_hat/party_hat.rules.inc
Implement the unassign hat rule.

File

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

Code

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

  // Get the hat items from the party object
  $hat_items = field_get_items('party', $party, 'party_hat');
  foreach ($hats as $hat) {
    $has_hat = FALSE;
    foreach ($hat_items as $delta => $item) {
      if ($item['hat_name'] == $hat) {
        $has_hat = TRUE;
        unset($hat_items[$delta]);
      }
    }

    // Don't remove the hat if its not assigned
    if (!$has_hat) {
      continue;
    }
    $unassigned_hats[] = $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($unassigned_hats)) {
    if (module_exists('rules')) {
      rules_invoke_all('party_hat_unassign_hats', $party, $unassigned_hats);
    }
    else {
      module_invoke_all('party_hat_unassign_hats', $party, $unassigned_hats);
    }
  }
}