You are here

function taxonomy_manager_delete_terms in Taxonomy Manager 7

Same name and namespace in other branches
  1. 5 taxonomy_manager.module \taxonomy_manager_delete_terms()
  2. 6.2 taxonomy_manager.admin.inc \taxonomy_manager_delete_terms()
  3. 6 taxonomy_manager.admin.inc \taxonomy_manager_delete_terms()

deletes terms from the database optional orphans (terms where parent get deleted) can be deleted as well

(difference to taxonomy_term_delete: deletion of orphans optional)

Parameters

$tids array of term id to delete:

$options associative array with options: if $options['delete_orphans'] is true, orphans get deleted

1 call to taxonomy_manager_delete_terms()
taxonomy_manager_term_confirm_delete_submit in ./taxonomy_manager.admin.inc
Submit handler to delete a term after confirmation.

File

./taxonomy_manager.admin.inc, line 2432

Code

function taxonomy_manager_delete_terms($tids, $options = array()) {
  $deleted_term = array();
  $remaining_child_terms = array();
  if (!is_array($tids)) {
    array(
      $tids,
    );
  }
  while (count($tids) > 0) {
    $orphans = array();
    foreach ($tids as $tid) {
      if ($children = taxonomy_get_children($tid)) {
        foreach ($children as $child) {
          $parents = taxonomy_get_parents($child->tid);
          if ($options['delete_orphans']) {
            if (count($parents) == 1) {
              $orphans[$child->tid] = $child->tid;
            }
            else {
              $remaining_child_terms[$child->tid] = $child;
            }
          }
          else {
            $remaining_child_terms[$child->tid] = $child;
            db_delete('taxonomy_term_hierarchy')
              ->condition('tid', $child->tid)
              ->condition('parent', $tid)
              ->execute();
            if (count($parents) == 1) {
              $is_root = (bool) db_query_range("SELECT 1 FROM {taxonomy_term_hierarchy} h WHERE h.tid = :tid AND h.parent = 0", 0, 1, array(
                ':tid' => $child->tid,
              ))
                ->fetchField();
              if (!$is_root) {
                db_insert('taxonomy_term_hierarchy')
                  ->fields(array(
                  'tid',
                  'parent',
                ))
                  ->values(array(
                  'tid' => $child->tid,
                  'parent' => 0,
                ))
                  ->execute();
              }
            }
          }
        }
      }
      $term = taxonomy_term_load($tid);
      if ($term) {
        $deleted_terms[] = $term;
        db_delete('taxonomy_term_data')
          ->condition('tid', $tid)
          ->execute();
        db_delete('taxonomy_term_hierarchy')
          ->condition('tid', $tid)
          ->execute();
        field_attach_delete('taxonomy_term', $term);
        module_invoke_all('taxonomy_term_delete', $term);

        // Reset cache after each deletion as it might affect parents/children information that is used later on.
        taxonomy_terms_static_reset();
      }
    }
    $tids = $orphans;
  }
  return array(
    'deleted_terms' => $deleted_terms,
    'remaining_child_terms' => $remaining_child_terms,
  );
}