You are here

function views_bulk_operations_taxonomy_action in Views Bulk Operations (VBO) 6.3

Same name and namespace in other branches
  1. 6 actions/taxonomy.action.inc \views_bulk_operations_taxonomy_action()
1 string reference to 'views_bulk_operations_taxonomy_action'
views_bulk_operations_views_default_views in ./views_bulk_operations.views_default.inc
Implementation of hook_views_default_views().

File

./taxonomy.action.inc, line 18

Code

function views_bulk_operations_taxonomy_action(&$node, $context) {
  if (empty($context['terms']) && $context['do'] == TAXONOMY_ACTION_REPLACE) {

    // Just delete terms
    taxonomy_node_delete($node);
    $node->taxonomy = array();
    return;
  }

  // Operate on a terms array.
  if (isset($context['terms']['tags'])) {
    $tags = $context['terms']['tags'];
    unset($context['terms']['tags']);
  }
  $terms = array();
  foreach ($context['terms'] as $tid) {
    $terms[$tid] = taxonomy_get_term($tid);
  }
  switch ($context['do']) {
    case TAXONOMY_ACTION_DELETE:
      $existing_terms = taxonomy_node_get_terms($node);
      while (list($delete_tid) = each($terms)) {
        if (array_key_exists($delete_tid, $existing_terms)) {
          unset($existing_terms[$delete_tid]);
        }
      }
      $terms = $existing_terms;
      if (empty($terms)) {
        taxonomy_node_delete($node);
      }
      break;
    case TAXONOMY_ACTION_ADD:
      $existing_terms = taxonomy_node_get_terms($node);
      foreach ($terms as $add_tid => $term) {
        if (array_key_exists($add_tid, $existing_terms)) {
          unset($terms[$add_tid]);
        }
      }
      $terms = array_merge($terms, $existing_terms);
      break;
    case TAXONOMY_ACTION_REPLACE_VOCABULARY:
      $existing_terms = taxonomy_node_get_terms($node);
      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);
      break;
    case TAXONOMY_ACTION_REPLACE:
      break;
  }

  // Reassemble a taxonomy array that can be saved.
  $taxonomy = array();
  if (!empty($terms)) {
    foreach ($terms as $term) {
      if (!isset($taxonomy[$term->vid])) {
        $taxonomy[$term->vid] = $term->tid;
      }
      else {
        if (is_array($taxonomy[$term->vid])) {
          $taxonomy[$term->vid][$term->tid] = $term->tid;
        }
        else {
          $previous_tid = $taxonomy[$term->vid];
          $taxonomy[$term->vid] = array(
            $previous_tid => $previous_tid,
            $term->tid => $term->tid,
          );
        }
      }
    }
  }
  if (isset($tags) && $context['do'] != TAXONOMY_ACTION_DELETE) {
    $taxonomy['tags'] = $tags;
  }
  $node->taxonomy = $taxonomy;
}