Index: contrib/image_attach/image_attach.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/contrib/image_attach/image_attach.module,v
retrieving revision 1.47
diff -u -p -r1.47 image_attach.module
--- contrib/image_attach/image_attach.module	22 Mar 2009 18:36:37 -0000	1.47
+++ contrib/image_attach/image_attach.module	19 Jun 2009 11:14:58 -0000
@@ -170,6 +170,30 @@ function image_attach_form_alter(&$form,
       '#default_value' => variable_get('image_attach_weight_body_'. $form['#node_type']->type, 0),
       '#description' => t('This value determines the order of the image when displayed in the body.'),
     );
+    if(module_exists('image_gallery')) {
+      $form['image_attach']['image_gallery'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Image gallery'),
+        '#collapsible' => TRUE,
+        '#collapsed' => TRUE,
+      );
+      $image_attach_gallery_defaults_options =  array(
+        0 => t('Use nodetype gallery'),
+        1 => t('Use nodetype gallery and only permit existing images from this gallery'),        
+        2 => t('Use per node gallery'),
+        3 => t('Use per node gallery, if empty use nodetype gallery'),
+      );
+      $form['image_attach']['image_gallery']['image_attach_gallery_defaults'] = array(
+        '#type' => 'radios',
+        '#title' => t('Add images to gallery'),
+        '#default_value' => variable_get('image_attach_gallery_defaults_'. $form['#node_type']->type, 0),
+        '#options' => $image_attach_gallery_defaults_options,
+      );
+      $form['image_attach']['image_gallery']['image_attach_gallery'] = taxonomy_form(_image_gallery_get_vid());
+      $form['image_attach']['image_gallery']['image_attach_gallery']['#default_value'] = variable_get('image_attach_gallery_'. $form['#node_type']->type, 0);
+      $form['image_attach']['image_gallery']['image_attach_gallery']['#weight'] = 0;
+      $form['image_attach']['image_gallery']['image_attach_gallery']['#description'] = t('Choose an image gallery to place the new image in.');
+    }
   }
   // Node edit form.
   else if (isset($form['type']['#value']) && $form['type']['#value'] != 'image') {
@@ -202,7 +226,7 @@ function image_attach_form_alter(&$form,
         $form['image_attach']['iid'] = array(
           '#type' => 'select',
           '#title' => t('Existing image'),
-          '#options' => _image_attach_get_image_nodes(),
+          '#options' => _image_attach_get_image_nodes($type),
           $value => empty($node->iid) ? NULL : $node->iid,
           '#description' => t('Choose an image already existing on the server if you do not upload a new one.')
         );
@@ -228,6 +252,15 @@ function image_attach_form_alter(&$form,
         $value => '',
         '#description' => t('The title the image will be shown with.')
       );
+      if(module_exists('image_gallery') && variable_get('image_attach_gallery_defaults_'. $type, 0)) {
+        $form['image_attach']['image_gallery'] = taxonomy_form(_image_gallery_get_vid());
+        if (is_array($image->taxonomy)) {
+          $image_gallery_value = array_keys($image->taxonomy);
+          $form['image_attach']['image_gallery']['#value'] = $image_gallery_value[0];
+        }
+        $form['image_attach']['image_gallery']['#weight'] = 10;        
+        $form['image_attach']['image_gallery']['#description'] = t('Choose an image gallery to place the new image in.');
+      }
     }
   }
 }
@@ -247,6 +280,25 @@ function image_attach_validate($form, &$
     // This func now does all the things to initialize an image properly
     $image = image_create_node_from($file->filepath, $image_title, '');
     if ($image && !form_get_errors()) {
+      // If image node is saved add taxonomy if needed
+      $image_attach_gallery_defaults_type = variable_get('image_attach_gallery_defaults_'. $form['type']['#value'], 0);
+      switch($image_attach_gallery_defaults_type) {
+        default:        
+          $gallery_tid = variable_get('image_attach_gallery_'. $form['type']['#value'], 0);
+          break;
+        case 2:
+          $gallery_tid = $_POST['image_gallery'];
+          break;            
+        case 3:
+          $gallery_tid = ($_POST['image_gallery']) ? $_POST['image_gallery'] : variable_get('image_attach_gallery_'. $form['type']['#value'], 0);
+          break;          
+      }
+      if ($gallery_tid) {
+        $gallery = taxonomy_get_term($gallery_tid);
+        if ($gallery) {
+          taxonomy_node_save($image, array($gallery->tid));
+        }
+      }
       drupal_set_message(t("Created new image to attach to this node. !image_link", array('!image_link' => l($image_title, 'node/'. $image->nid) )));
       form_set_value($form['image_attach']['iid'], $image->nid, $form_state);
     }
@@ -318,9 +370,18 @@ function image_attach_nodeapi(&$node, $o
 /**
  * Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user.
  */
-function _image_attach_get_image_nodes() {
-  $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, n.sticky FROM {node} n WHERE n.status = 1 AND n.type = 'image' ORDER BY n.sticky DESC, n.title ASC"));
-  $result_count = db_result(db_query("SELECT COUNT(*) FROM {node} n WHERE n.status = 1 AND n.type = 'image'"));
+function _image_attach_get_image_nodes($nodetype = NULL) {
+  if(module_exists('image_gallery') && (variable_get('image_attach_gallery_defaults_'. $nodetype, 0) == 1)) {
+    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, n.sticky FROM {node} n 
+      INNER JOIN {term_node} tn ON n.nid = tn.nid 
+      WHERE n.status = 1 AND n.type = 'image' AND tn.tid = %d
+      ORDER BY n.sticky DESC, n.title ASC"), variable_get('image_attach_gallery_'. $nodetype, 0));  
+  } else {
+    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, n.sticky FROM {node} n 
+      WHERE n.status = 1 AND n.type = 'image'
+      ORDER BY n.sticky DESC, n.title ASC"));
+  }
+  
   $rows = array(0 => t('None'));
   while ($node = db_fetch_object($result)) {
     $rows[$node->nid] = $node->title;
