You are here

delete_orphaned_terms.module in Delete Orphaned Terms 6

Same filename and directory in other branches
  1. 6.2 delete_orphaned_terms.module
  2. 7.2 delete_orphaned_terms.module

File

delete_orphaned_terms.module
View source
<?php

/**
 * @file 
 */

/**
 * Implementation of hook_perm()
 */
function delete_orphaned_terms_perm() {
  return array(
    'delete orphaned terms',
  );
}

/**
 * Implementation of hook_menu()
 */
function delete_orphaned_terms_menu() {
  $items = array();
  $items['admin/settings/delete-orphaned-items'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'delete_orphaned_terms_form',
    ),
    'title' => 'Delete Orphaned Terms',
    'description' => "Delete terms with no content associated with them.",
    'access arguments' => array(
      'delete orphaned terms',
    ),
  );
  return $items;
}
function delete_orphaned_terms_form(&$form_state) {
  $vocabularies = taxonomy_get_vocabularies();
  foreach ($vocabularies as $vocabulary) {
    $vocab_names[$vocabulary->vid] = $vocabulary->name . ' (currently ' . count(taxonomy_get_tree($vocabulary->vid)) . ' terms)';
  }
  $form['dot']['orphaned_terms_vocabs'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Vocabularies'),
    '#description' => t('Select one or more vocabularies to delete orphaned terms from.'),
    '#options' => $vocab_names,
    '#default_value' => variable_get('orphaned_terms_vocabs', array()),
    '#multiple' => TRUE,
  );
  $form['dot']['orphan_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Orphan value'),
    '#description' => t('Number of nodes associated with a term in order for it to be considered orphaned.'),
    '#default_value' => variable_get('orphan_value', 0),
    '#max_length' => 1,
    '#size' => 1,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}
function delete_orphaned_terms_form_submit($form, &$form_state) {
  variable_set('orphaned_terms_vocabs', $form_state['values']['orphaned_terms_vocabs']);
  variable_set('orphan_value', $form_state['values']['orphan_value']);
  foreach ($form_state['values']['orphaned_terms_vocabs'] as $vid) {
    if ($vid > 0) {
      $vocab = taxonomy_vocabulary_load($vid);
      $count = delete_orphaned_terms_delete($vid, $form_state['values']['orphan_value']);
      drupal_set_message('Previous ' . $vocab->name . ' term count: ' . $count);
    }
  }
}

/**
 * Implementation of hook_cron()
 */

/*
function delete_orphaned_terms_cron() {
 $vocabularies = taxonomy_get_vocabularies();
 foreach ($vocabularies as $vocabulary) {
   $count = delete_orphaned_terms_delete($vocabulary->vid);
   drupal_set_message('Term count: ' . $count);
 }
}
*/
function delete_orphaned_terms_delete($vid, $orphan_value) {
  $terms = taxonomy_get_tree($vid);
  foreach ($terms as $term) {
    if (taxonomy_term_count_nodes($term->tid) <= $orphan_value) {
      taxonomy_del_term($term->tid);
    }
  }
  return count($terms);
}

/**
 * Implementation of hook_help() 
 */
function delete_orphaned_terms_help($path, $arg) {
}