You are here

function media_browser_plus_taxonomy_term_delete in Media Browser Plus 7.2

Same name and namespace in other branches
  1. 7.3 media_browser_plus.module \media_browser_plus_taxonomy_term_delete()

Implements hook_taxonomy_term_delete().

File

./media_browser_plus.module, line 2159
Adds fields to the media browser forms for better UX

Code

function media_browser_plus_taxonomy_term_delete($term) {

  // Figure out if this is a folder term and if so handle the related files.
  $vocabulary = taxonomy_vocabulary_machine_name_load('media_folders');
  if (!empty($vocabulary) && $term->vid == $vocabulary->vid) {

    // Create an array of all the folders to handle.
    $folders = array(
      '0:' . $term->tid => $term,
    );

    // Fetch all sub-folders.
    $tree = taxonomy_get_tree($term->vid, $term->tid);
    foreach ($tree as $subterm) {
      $folders[$subterm->depth + 1 . ':' . $subterm->tid] = $subterm;
    }

    // Ensure the order for processing is right.
    krsort($folders);
    foreach ($folders as $folder) {

      // Fetch all files from the folder.
      $conditions = array();
      $conditions[] = array(
        'field' => array(
          'field_folder',
          'tid',
          $folder->tid,
          '=',
        ),
      );
      $options = array(
        'apply_filter' => FALSE,
        'count_only' => FALSE,
        'paging' => FALSE,
        'conditions' => $conditions,
      );
      $folder_path = media_browser_plus_construct_dir_path($folder);
      if ($files = media_browser_plus_load_multiple($options)) {
        $all_files_deleted = TRUE;
        if (!empty($files->results)) {
          foreach ($files->results as $file) {
            if (!file_delete($file)) {
              $all_files_deleted = FALSE;
            }
          }
        }

        // Also delete the folder when it's empty.
        if ($all_files_deleted) {
          if (!@drupal_rmdir($folder_path)) {
            drupal_set_message(t('Unable to delete the folder (!path) on the disk', array(
              '!path' => $folder_path,
            )), 'error');
          }
        }
      }
    }
  }
}