path; // Reverse-lookup to figure what this attached image is, if any. $file = db_fetch_object(db_query("SELECT * FROM {files} WHERE filepath = '%s'", $current_filepath)); // If none, no problem. $all_images = taxonomy_image_attach_get_image_nodes(); $form['taxonomy_image']['image_attach']['iid'] = array( '#type' => 'select', '#title' => t('Use existing image'), '#options' => $all_images, '#default_value' => $file->nid, '#description' => t("Choose from existing image nodes."), '#prefix' => t('or:'), ); // TODO Ajax to display the one you just chose? $sizes = image_get_sizes(); $size_options = array(); foreach ($sizes as $size => $size_def) { $size_options[$size] = $size; } $form['taxonomy_image']['image_attach']['image_size'] = array( '#type' => 'select', '#title' => t('Preset size from existing image'), '#default_value' => $file->filename ? $file->filename : variable_get('taxonomy_image_attach_default_size', 'thumbnail'), '#options' => $size_options, '#description' => t("Any resizing preferences set in the taxonomy_image admin may over-ride this size choice."), ); if (! variable_get('taxonomy_image_attach_enable_size', FALSE) ) { // Actually, scratch that selector, if you're not allowed to use it. Just set a value. $form['taxonomy_image']['image_attach']['image_size']['#type'] = 'value'; $form['taxonomy_image']['image_attach']['image_size']['#value'] = variable_get('taxonomy_image_attach_default_size', 'thumbnail'); } break; case 'taxonomy_image_admin_settings': $form['image_attach'] = array( '#type' => 'fieldset', '#title' => t('Existing Image Attachment'), ); $form['image_attach']['taxonomy_image_attach_enable'] = array( '#type' => 'checkbox', '#title' => t('Allow selection of existing image node images to use as taxonomy_images'), '#default_value' => variable_get('taxonomy_image_attach_enable', TRUE), ); $form['image_attach']['taxonomy_image_attach_enable_size'] = array( '#type' => 'checkbox', '#title' => t('Allow selection of image derivative sizes'), '#default_value' => variable_get('taxonomy_image_attach_enable_size', FALSE), ); $sizes = image_get_sizes(); $size_options = array(); foreach ($sizes as $size => $size_def) { $size_options[$size] = $size; } $form['image_attach']['taxonomy_image_attach_default_size'] = array( '#type' => 'select', '#title' => t('Default image attach size'), '#options' => $size_options, '#default_value' => variable_get('taxonomy_image_attach_default_size', 'thumbnail'), ); $form['buttons']['#weight'] = 10; break; } return $form; } /** * Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user. * * Direct copy of the way image_attach does so * @see _image_attach_get_image_nodes() */ function taxonomy_image_attach_get_image_nodes() { $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")); if (db_num_rows($result) == 0) { return array(); } $rows = array(0 => t('None')); while ($node = db_fetch_object($result)) { $rows[$node->nid] = $node->title; } return $rows; } /** * Implementation of hook_taxonomy. * Capture term updates - including submission of the term edit form */ 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; } }