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.67
diff -u -p -r1.67 image_attach.module
--- contrib/image_attach/image_attach.module	17 Sep 2009 19:01:39 -0000	1.67
+++ contrib/image_attach/image_attach.module	6 Nov 2009 09:13:38 -0000
@@ -163,6 +163,32 @@ function image_attach_form_alter(&$form,
         '#description' => t('This value determines the order of the image when displayed in the teaser.'),
       );
     }
+    
+    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.');
+    }
+    
     $form['image_attach']['image_attach_size_body'] = array(
       '#type' => 'select',
       '#title' => t('Full node image size'),
@@ -227,7 +253,7 @@ function image_attach_form_alter(&$form,
         // User may attach already existing images: show a selection box containing all images.
         if (variable_get('image_attach_existing', 1)) {
           $form['image_attach']['iids']['#title'] = t('Existing images');
-          $form['image_attach']['iids']['#options'] = _image_attach_get_image_nodes();
+          $form['image_attach']['iids']['#options'] = _image_attach_get_image_nodes(array(), $type);
           $form['image_attach']['iids']['#description'] = t('Choose an image already existing on the server if you do not upload a new one.');
         }
         // User may only upload new images: show a selection box containing only attached images.
@@ -249,6 +275,17 @@ 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) > 1)) {
+            $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.');
+          }
+          
           // Provide an additional submit button, which adds an image and redirects
           // the user to the node edit form.
           $form['image_attach']['image_attach_multiple'] = array(
@@ -308,6 +345,27 @@ function image_attach_validate(&$form, &
     // 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) )));
       // Append image nid to array of images.
       $form_state['values']['iids'][$image->nid] = $image->nid;
@@ -417,8 +475,19 @@ function image_attach_nodeapi(&$node, $o
  *
  * @param $nids
  *  A list of nids to filter on. If not passed, all image nids are returned. 
- */
-function _image_attach_get_image_nodes($nids = array()) {
+ */ 
+function _image_attach_get_image_nodes($nids = array(), $type = 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"));
+  }
+  
   $placeholder = '';
   // If $nids was passed, build placeholders to put in the query
   if (count($nids)) {
