You are here

function taxonomy_action in Views Bulk Operations (VBO) 5

File

./taxonomy.action.inc, line 18

Code

function taxonomy_action(&$node, $context) {
  $terms = array();
  foreach ($context['terms'] as $tid) {
    $terms[$tid] = taxonomy_get_term($tid);
  }

  // check for add or delete
  if ($context['do'] == TAXONOMY_ACTION_ADD || $context['do'] == TAXONOMY_ACTION_DELETE) {
    $existing_terms = taxonomy_node_get_terms($node->nid);
    if ($context['do'] == TAXONOMY_ACTION_DELETE) {

      // delete terms
      while (list($delete_tid) = each($terms)) {
        if (array_key_exists($delete_tid, $existing_terms)) {
          unset($existing_terms[$delete_tid]);
        }
      }
      $terms = $existing_terms;
    }
    else {

      // add terms
      foreach ($terms as $add_tid => $term) {
        if (array_key_exists($add_tid, $existing_terms)) {
          unset($terms[$add_tid]);
        }
      }
      $terms = array_merge($terms, $existing_terms);
    }
  }
  else {
    if ($context['do'] == TAXONOMY_ACTION_REPLACE_VOCABULARY) {
      $existing_terms = taxonomy_node_get_terms($node->nid);
      foreach ($existing_terms as $existing_term) {
        foreach ($terms as $term) {
          if ($term->vid == $existing_term->vid) {
            unset($existing_terms[$existing_term->tid]);
          }
        }
      }
      $terms = array_merge($terms, $existing_terms);
    }
  }
  $node->taxonomy = $terms;
}