You are here

function taxonomy_image_taxonomy in Taxonomy Image 6

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

Implementation of hook_taxonomy().

File

./taxonomy_image.module, line 622
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) {

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

      // Check / create category picture directory.
      $directory = file_create_path(variable_get('taxonomy_image_path', 'category_pictures'));
      if (!file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
        drupal_set_message(t('Error creating the category picture directory: "%dir" does not exist, or is not writable.', array(
          '%dir' => $directory,
        )), 'error');
        return;
      }

      // Did they mark it to delete?
      if ($form_values['taxonomy_image_current_image_delete'] == TRUE) {
        taxonomy_image_delete($tid);
      }

      // External file used?
      if (!empty($form_values['taxonomy_image_external'])) {
        $source = $form_values['taxonomy_image_external'];
        $filename = basename($source);
        $filename = file_munge_filename($filename, TAXONOMY_IMAGE_IMAGE_EXTENSIONS);
        $where = file_destination($directory . '/' . $filename, FILE_EXISTS_REPLACE);
        if (copy($source, $where)) {
          drupal_set_message(t('Copy from @source to @where successful.', array(
            '@source' => $source,
            '@where' => $where,
          )));
          taxonomy_image_add($tid, $filename);
        }
        else {
          drupal_set_message(t('Copy from @source to @where failed.', array(
            '@source' => $source,
            '@where' => $where,
          )), 'error');
        }
      }
      else {
        $validators = array();

        // FILE_EXISTS_REPLACE allows multiple terms to use the same image without having multiple files.
        $file = file_save_upload('taxonomy_image_upload', $validators, $directory, FILE_EXISTS_REPLACE);
        if (is_object($file)) {

          // If no errors while uploading, save term-image relation into DB
          $filepath = variable_get('taxonomy_image_path', 'category_pictures') . '/' . $file->filename;
          if (taxonomy_image_add($tid, $file->filename)) {

            // Successfully added to DB, make sure Cron doesn't delete this file.
            file_set_status($file, FILE_STATUS_PERMANENT);
            drupal_set_message(t('Image uploaded as @name.', array(
              '@name' => $filepath,
            )));
          }
          else {
            drupal_set_message(t('Error while adding image [tid = !tid, path = @path].', array(
              '!tid' => $tid,
              '@path' => $filepath,
            )), 'error');
          }
        }
      }
      break;
    case 'delete':
      if (_taxonomy_image_exists($tid)) {
        taxonomy_image_delete($tid);
      }
      break;
  }
}