taxonomy_image_attach.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/taxonomy_image/contributed/taxonomy_image_attach/taxonomy_image_attach.module
    2. 6 contributions/taxonomy_image/contributed/taxonomy_image_attach/taxonomy_image_attach.module

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.

If you are editing your term definitions a lot, I recommend http://drupal.org/project/edit_term for features that make handling terms even more like handling nodes.

TODO: The selection could be made either wider (all files/filesystem scan) or narrower (images tagged in a certain gallery)

@author dman http://coders.co.nz/

Functions & methods

NameDescription
taxonomy_image_attach_form_alterAdd an image selector to the taxonomy_image fieldset alongside the upload field.
taxonomy_image_attach_get_image_nodesFetch an array of all candidate referenced nodes, for use in presenting the selection form to the user.
taxonomy_image_attach_menuImplementation of hook_menu.
taxonomy_image_attach_taxonomyImplementation of hook_taxonomy. Capture term updates - including submission of the term edit form
taxonomy_image_image_attach_form

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Add functionality to terms similar to the image_attach.module.
  5. * Allow a term editor to choose from EXISTING images rather than upload new ones all the time.
  6. * Currently this uses image.module handling of image nodes to index available images.
  7. *
  8. * If you are editing your term definitions a lot, I recommend
  9. * http://drupal.org/project/edit_term
  10. * for features that make handling terms even more like handling nodes.
  11. *
  12. * TODO:
  13. * The selection could be made either wider (all files/filesystem scan)
  14. * or narrower (images tagged in a certain gallery)
  15. *
  16. * @author dman http://coders.co.nz/
  17. */
  18. /**
  19. * Implementation of hook_menu.
  20. */
  21. function taxonomy_image_attach_menu($may_cache) {
  22. $items = array();
  23. if ($may_cache) {
  24. // Add a tab to the Admin Settings
  25. $items[] = array(
  26. 'path' => 'admin/settings/taxonomy_image/image_attach',
  27. 'title' => 'Image Attach',
  28. 'callback' => 'drupal_get_form',
  29. 'callback arguments' => array('taxonomy_image_image_attach_form'),
  30. 'access' => user_access('administer site configuration'),
  31. 'description' => 'Configure Taxonomy_image Image_attach functionality.',
  32. 'type' => MENU_LOCAL_TASK,
  33. 'weight' => 1,
  34. );
  35. }
  36. return $items;
  37. }
  38. function taxonomy_image_image_attach_form() {
  39. drupal_add_css(drupal_get_path('module', 'taxonomy_image') .'/taxonomy_image.css');
  40. $form = array();
  41. $form['taxonomy_image_attach_enable'] = array(
  42. '#type' => 'checkbox',
  43. '#title' => t('Allow selection of existing image node images to use as taxonomy_images'),
  44. '#default_value' => variable_get('taxonomy_image_attach_enable', TRUE),
  45. );
  46. $form['taxonomy_image_attach_enable_size'] = array(
  47. '#type' => 'checkbox',
  48. '#title' => t('Allow selection of image derivative sizes'),
  49. '#default_value' => variable_get('taxonomy_image_attach_enable_size', FALSE),
  50. );
  51. $form['taxonomy_image_attach_default_size'] = array(
  52. '#type' => 'radios',
  53. '#title' => t('Default image attach size'),
  54. '#options' => drupal_map_assoc(_taxonomy_image_presets()),
  55. '#default_value' => variable_get('taxonomy_image_attach_default_size', 'thumbnail'),
  56. '#prefix' => '<div class="taxonomy_image_radios">',
  57. '#suffix' => '</div><div class="clear-block"></div>',
  58. );
  59. return system_settings_form($form);
  60. }
  61. /**
  62. * Add an image selector to the taxonomy_image fieldset alongside the upload field.
  63. */
  64. function taxonomy_image_attach_form_alter($form_id, &$form) {
  65. switch ($form_id) {
  66. case 'taxonomy_form_term':
  67. $current_image = taxonomy_image_get_object($form['tid']['#value']);
  68. $current_filepath = $current_image->path;
  69. // Reverse-lookup to figure what this attached image is, if any.
  70. $file = db_fetch_object(db_query("SELECT * FROM {files} WHERE filepath = '%s'", $current_filepath));
  71. // If none, no problem.
  72. $all_images = taxonomy_image_attach_get_image_nodes();
  73. $form['taxonomy_image']['image_attach']['iid'] = array(
  74. '#type' => 'select',
  75. '#title' => t('Use existing image'),
  76. '#options' => $all_images,
  77. '#default_value' => $file->nid,
  78. '#description' => t("Choose from existing image nodes."),
  79. '#prefix' => t('<strong>or:</strong>'),
  80. );
  81. // TODO: Ajax to display the one you just chose?
  82. $sizes = image_get_sizes();
  83. $size_options = array();
  84. foreach ($sizes as $size => $size_def) {
  85. $size_options[$size] = $size;
  86. }
  87. $form['taxonomy_image']['image_attach']['image_size'] = array(
  88. '#type' => 'select',
  89. '#title' => t('Preset size from existing image'),
  90. '#default_value' => $file->filename ? $file->filename : variable_get('taxonomy_image_attach_default_size', 'thumbnail'),
  91. '#options' => $size_options,
  92. '#description' => t("Any resizing preferences set in the taxonomy_image admin may over-ride this size choice."),
  93. );
  94. if (!variable_get('taxonomy_image_attach_enable_size', FALSE)) {
  95. // Actually, scratch that selector, if you're not allowed to use it. Just set a value.
  96. $form['taxonomy_image']['image_attach']['image_size']['#type'] = 'value';
  97. $form['taxonomy_image']['image_attach']['image_size']['#value'] = variable_get('taxonomy_image_attach_default_size', 'thumbnail');
  98. }
  99. break;
  100. }
  101. return $form;
  102. }
  103. /**
  104. * Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user.
  105. *
  106. * Direct copy of the way image_attach does so
  107. * @see _image_attach_get_image_nodes()
  108. */
  109. function taxonomy_image_attach_get_image_nodes() {
  110. $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.status=1 AND type='image' ORDER BY n.sticky DESC, n.title ASC"));
  111. if (db_num_rows($result) == 0) {
  112. return array();
  113. }
  114. $rows = array(0 => t('None'));
  115. while ($node = db_fetch_object($result)) {
  116. $rows[$node->nid] = $node->title;
  117. }
  118. return $rows;
  119. }
  120. /**
  121. * Implementation of hook_taxonomy.
  122. * Capture term updates - including submission of the term edit form
  123. */
  124. function taxonomy_image_attach_taxonomy($op, $type, $form_values = NULL) {
  125. $directory = file_create_path(variable_get('taxonomy_image_path', 'category_pictures'));
  126. file_check_directory($directory, FILE_CREATE_DIRECTORY);
  127. // We're only interested in term changes.
  128. if ($type != 'term') {
  129. return;
  130. }
  131. $tid = $form_values['tid'];
  132. switch ($op) {
  133. case 'insert':
  134. case 'update':
  135. if (file_check_upload('path')) {
  136. // An upload will have just happened. Not our problem, don't even try to interfere.
  137. break;
  138. }
  139. if ($form_values['current_image_delete'] == 1) {
  140. // Then don't immediately re-attach it!
  141. break;
  142. }
  143. $is_current_image = db_result(db_query('SELECT COUNT(tid) FROM {term_image} WHERE tid=%d', $tid));
  144. if ($iid = $form_values['iid']) {
  145. $oldpath = db_result(db_query('SELECT path FROM {term_image} WHERE tid=%d', $tid));
  146. if ($is_current_image) {
  147. // Delete old image before saving the new one,
  148. // Image files outside of the dedicated directory should be safe
  149. taxonomy_image_delete($tid);
  150. }
  151. $image_node = node_load($iid);
  152. // choose size
  153. $filepath = $image_node->images[$form_values['image_size']];
  154. if ($filepath && file_exists($filepath)) {
  155. $insert = db_query("INSERT INTO {term_image} (tid, path) VALUES ('%d', '%s')", $tid, $filepath);
  156. if ($insert == FALSE) {
  157. $message = theme('error', t('Database insert failed. [tid = !tid, path = @path.', array('!tid' => $tid, '@path' => $file->filepath)));
  158. }
  159. }
  160. else {
  161. $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)));
  162. }
  163. }
  164. return drupal_set_message($message);
  165. case 'delete':
  166. taxonomy_image_delete($tid);
  167. return;
  168. }
  169. }