You are here

function views_bulk_operations_taxonomy_action in Views Bulk Operations (VBO) 6

Same name and namespace in other branches
  1. 6.3 taxonomy.action.inc \views_bulk_operations_taxonomy_action()

File

actions/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;

  // Match content taxonomy fields with their respective vocabulary.
  if (module_exists('content_taxonomy')) {
    $type_name = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type);
    $type = content_types($type_name);
    foreach ($type['fields'] as $field) {
      if ($field['type'] == 'content_taxonomy') {
        $vid = $field['vid'];
        $term_values = $node->taxonomy[$field['vid']];
        $new_values = array();
        if (is_array($term_values)) {
          foreach ($term_values as $value) {
            $new_values[]['value'] = $value;
          }
        }
        else {
          $new_values[]['value'] = $term_values;
        }
        $node->{$field}['field_name'] = $new_values;
      }
    }
  }
}