You are here

function taxonomy_image_attach_taxonomy in Taxonomy Image 5

Same name and namespace in other branches
  1. 6 contributed/taxonomy_image_attach/taxonomy_image_attach.module \taxonomy_image_attach_taxonomy()

Implementation of hook_taxonomy. Capture term updates - including submission of the term edit form

File

contributed/taxonomy_image_attach/taxonomy_image_attach.module, line 139
Add functionality to terms similar to the image_attach.module. Allow a term editor to choose from EXISTING images rather than upload new ones all the time. Currently this uses image.module handling of image nodes to index available images.

Code

function taxonomy_image_attach_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':
      if (file_check_upload('path')) {

        // An upload will have just happened. Not our problem, don't even try to interfere.
        break;
      }
      if ($form_values['current_image_delete'] == 1) {

        // Then don't immediately re-attach it!
        break;
      }
      $is_current_image = db_result(db_query('SELECT COUNT(tid) FROM {term_image} WHERE tid=%d', $tid));
      if ($iid = $form_values['iid']) {
        $oldpath = db_result(db_query('SELECT path FROM {term_image} WHERE tid=%d', $tid));
        if ($is_current_image) {

          // Delete old image before saving the new one,
          // Image files outside of the dedicated directory should be safe
          taxonomy_image_delete($tid);
        }
        $image_node = node_load($iid);

        // choose size
        $filepath = $image_node->images[$form_values['image_size']];
        if ($filepath && file_exists($filepath)) {
          $insert = db_query("INSERT INTO {term_image} (tid, path) VALUES ('%d', '%s')", $tid, $filepath);
          if ($insert == FALSE) {
            $message = theme('error', t('Database insert failed. [tid = !tid, path = @path.', array(
              '!tid' => $tid,
              '@path' => $file->filepath,
            )));
          }
        }
        else {
          $message = t("Failed to locate the %image_size version of image node !iid", array(
            '%image_size' => $form_values['image_size'],
            '!iid' => l($iid, 'node/' . $iid),
          ));
        }
      }
      return drupal_set_message($message);
    case 'delete':
      taxonomy_image_delete($tid);
      return;
  }
}