You are here

function taxonomy_image_taxonomy in Taxonomy Image 5

Same name and namespace in other branches
  1. 6 taxonomy_image.module \taxonomy_image_taxonomy()

Implementation of hook_taxonomy().

File

./taxonomy_image.module, line 983
taxonomy_image.module Simple module for providing an association between taxonomy terms and images. Written by Jeremy Andrews <jeremy@kerneltrap.org>, May 2004.

Code

function taxonomy_image_taxonomy($op, $type, $form_values = NULL) {
  $directory = file_create_path(variable_get('taxonomy_image_path', 'category_pictures'));
  file_check_directory($directory, FILE_CREATE_DIRECTORY);

  // We're only interested in term changes.
  if ($type != 'term') {
    return;
  }
  $tid = $form_values['tid'];
  switch ($op) {
    case 'insert':
    case 'update':

      // Delete the cached version.
      cache_clear_all("taxonomy_image:{$tid}", 'cache_tax_image');

      // Did they mark it to delete?
      if (!empty($form_values['current_image_delete'])) {
        taxonomy_image_delete($tid);
      }
      $is_current_image = db_result(db_query('SELECT COUNT(tid) FROM {term_image} WHERE tid=%d', $tid));
      if ($file = file_save_upload('path', $directory)) {
        if ($is_current_image) {

          // Delete old image before saving the new one.
          taxonomy_image_delete($tid);
        }
        $insert = db_query("INSERT INTO {term_image} (tid, path) VALUES ('%d', '%s')", $tid, $file->filepath);
        if ($insert == FALSE) {
          $message = theme('error', t('Database insert failed. [tid = !tid, path = @path.', array(
            '!tid' => $tid,
            '@path' => $file->filepath,
          )));
        }
        else {
          $message = t('Image uploaded as @name.', array(
            '@name' => $file->filepath,
          ));
        }
      }
      else {
        if (!file_check_directory($directory)) {

          // we know what's wrong, so generate a more useful error message
          $message = theme('error', t('The category picture directory "%dir" does not exist, or is not writable.', array(
            '%dir' => variable_get('file_directory_path', 'files') . '/' . variable_get('taxonomy_image_path', 'category_pictures'),
          )));
        }
        else {
          $message = theme('error', t('Image upload failed.'));
        }
      }
      return drupal_set_message($message);
    case 'delete':
      taxonomy_image_delete($tid);
      return;
  }
}