# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: contributions/modules/node_gallery/node_gallery.admin.inc
--- contributions/modules/node_gallery/node_gallery.admin.inc Base (1.11.2.22)
+++ contributions/modules/node_gallery/node_gallery.admin.inc Locally Modified (Based On 1.11.2.22)
@@ -20,9 +20,11 @@
   $ng_rels = node_gallery_get_all_relationships();
   
   if (!empty($ng_rels)) {
-    $headers = array(t('Relationship name'), t('Gallery type'), t('Image type'), t('Operations'));
+    $headers = array(t('Relationship name'), t('Gallery type'), t('Image type'), t('Image Field'), t('Operations'));
     foreach ($ng_rels as $gallery_type => $config) {
-      $rows[] = array($config['settings']['name'], $gallery_type, $config['image_type'], 
+      $image_type = content_types($config['image_type']);
+      $field_link = 'admin/content/node-type/'.$type['url_str'].'/fields/'.$config['imagefield_name'];
+      $rows[] = array($config['settings']['name'], $gallery_type, $config['image_type'], l($config['imagefield_name'], $field_link),
       theme('links', node_gallery_relationship_operations($gallery_type)));
     }
     return theme('table', $headers, $rows, array('class' => 'node-gallery-config-list'));
@@ -188,7 +190,33 @@
     if (empty($imagecaches)) {
       form_set_error('Node gallery config', t('There are no imagecache presets. <a href="@imagecache">You must create at least one preset</a>.', array('@imagecache' => url('admin/build/imagecache/add'))));
     }
+    module_load_include('inc', 'content', 'includes/content.crud');
+    $imagefield_instances = content_field_instance_read(array('type_name' => $relationship['image_type']));
+    $options_imagefields = array();
+    if (!empty($imagefield_instances)) {
+      foreach ($imagefield_instances as $imagefield_instance) {
+        $options_imagefields[$imagefield_instance['field_name']] = $imagefield_instance['field_name'];
+      }
+    }
+    if (!array_key_exists('field_node_gallery_image', $options_imagefields)) {
+      $options_imagefields['field_node_gallery_image'] = t('Create new imagefield');
+    }
     
+    $form['node_gallery_relationship_settings'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Relationship settings'),
+      '#description' => t('This area defines the settings used to define the relationship between the content types.'),
+      '#collapsible' => TRUE,
+      '#collapsed' => FALSE,
+    );
+    
+    $form['node_gallery_relationship_settings']['imagefield'] = array(
+      '#type' => 'select',
+      '#title' => t('Imagefield for Galleries'),
+      '#description' => t('Select the imagefield that will be used for the gallery images.'),
+      '#options' => $options_imagefields,
+      //'#default_value' => (isset($relationship['imagefield_name']) && !empty($relationship['imagefield_name'])) ? $relationship['imagefield_name'] : '',
+    );
     $viewdisplays = node_gallery_build_views_select();
   
     $form['node_gallery_gallery_view'] = array(
@@ -321,8 +349,36 @@
   $r = new stdClass;
   $r->gallery_type = $form_state['storage']['gallery_type'];
   $r->image_type = $form_state['storage']['image_type'];
-  // @todo: Build a list of all imagefields on the image content type, and allow the user to choose one or create our default field_node_gallery_image imagefield content type.
-  $r->imagefield_name = 'field_node_gallery_image';
+
+  $r->imagefield_name = $form_state['values']['imagefield'];
+  if ($r->imagefield_name == 'field_node_gallery_image') {
+    // if the field is named 'field_node_gallery_image' check if it exists
+    // or if we need to create it. We could hack around that check with a pre- or
+    // suffix, but I rate this way cleaner (though more costly).
+    module_load_include('inc', 'content', 'includes/content.crud');
+    $param = array(
+      'type_name' => $r->image_type,
+      'field_name' => 'field_node_gallery_image',
+    );
+    $field_instance = content_field_instance_read($param);
+    if (empty($field_instance)) {
+      // create the default imagefield read out of the default type
+      $path = drupal_get_path('module', 'node_gallery') . "/cck_types";
+      $cck_file = $path . '/node_gallery_image.cck';
+      if (module_exists('image_fupload_imagefield') && variable_get('node_gallery_image_fupload_integration', FALSE)) {
+        $cck_file = $path . '/node_gallery_image_fupload.cck';
+      }
+      $param = array('type_name' => $r->image_type);
+      {
+        $content = array();
+        eval(file_get_contents($cck_file));
+        $param = array_merge($param, array_pop($content['fields']));
+        unset($content);
+      }
+      content_field_instance_create($param);
+      drupal_set_message(t('Node Gallery imagefield created.'));
+    }
+  }
   $existing = node_gallery_get_relationship($r->gallery_type);
   $settings = isset($existing['settings']) ? $existing['settings'] : array();
   $valid_settings=array('view_gallery_full_image_display', 'view_gallery_teaser_view_image_display', 'display_navigator', 'node_images_page_fragment', 'teaser_display_type', 'gallery_display_type');
Index: contributions/modules/node_gallery/node_gallery.inc
--- contributions/modules/node_gallery/node_gallery.inc Base (1.2.2.42)
+++ contributions/modules/node_gallery/node_gallery.inc Locally Modified (Based On 1.2.2.42)
@@ -613,8 +613,8 @@
   db_query("UPDATE {node_gallery_galleries} SET cover_image = NULL WHERE cover_image = %d", $node->nid);
   // We don't delete the file from the filesystem anymore, because imagefield handles this for us
   // Clean our imagecache - note, this API call leaves empty dirs
-  // @todo: eliminate hardcoded reference to field_node_gallery_image below
-  imagecache_image_flush($node->field_node_gallery_image[0]['filepath']);
\ No newline at end of file
+  $relationship = node_gallery_get_relationship(NULL, $node->type);
+  imagecache_image_flush($node->{$relationship['imagefield_name']}[0]['filepath']);
\ No newline at end of file
   // Clean up empty dirs, if they exist
   node_gallery_clean_empty_dirs(dirname($node->field_node_gallery_image[0]['filepath']));
 }
Index: contributions/modules/node_gallery/node_gallery.install
--- contributions/modules/node_gallery/node_gallery.install Base (1.19.2.35)
+++ contributions/modules/node_gallery/node_gallery.install Locally Modified (Based On 1.19.2.35)
@@ -129,18 +129,27 @@
 
   // Get the files content
   if (file_exists($filename)) {
-    $content = implode('', file($filename));
+    $macro = implode('', file($filename));
   }
   else {
     drupal_set_message(t('Unable to read cck file !file for import. Exiting.', array('!file' => $filename)), 'error');
     return;
   }
-
+  if ($type != '<create>') {
+    // add the imagefield for the image type.
+    if ($type == NODE_GALLERY_DEFAULT_IMAGE_TYPE) {
+        $content = array();
+        eval($macro);
+        $param = array('type_name' => $type);
+        $param = array_merge($param, array_pop($content['fields']));
+        content_field_instance_create($param);
+    }
+  } else {
   // Build form state
   $form_state = array(
   'values' => array(
     'type_name' => $type,
-    'macro' => $content,
+      'macro' => $macro,
     ),
   );
 
@@ -148,6 +157,7 @@
   drupal_execute('content_copy_import_form', $form_state);
   content_clear_type_cache();
 }
+}
 
 function _migrate_files_to_imagefields() {
   $total = db_result(db_query('SELECT COUNT(nid) FROM {node_galleries}'));
Index: contributions/modules/node_gallery/node_gallery.module
--- contributions/modules/node_gallery/node_gallery.module Base (1.18.2.75)
+++ contributions/modules/node_gallery/node_gallery.module Locally Modified (Based On 1.18.2.75)
@@ -310,11 +310,13 @@
           
           // If the user has image_fupload enabled, they have to allow multiple imagefields per node.
           // We really don't want that, so we zap the buttons on this form and set the right theme.
-          // @todo: eliminate hardcoded reference to field_node_gallery_image below
-          $form['field_node_gallery_image']['#theme'] = 'content_single_value';
-          unset($form['field_node_gallery_image']['0']['_weight']);
-          unset($form['field_node_gallery_image']['field_node_gallery_image_add_more']);
-          unset($form['field_node_gallery_image']['1']);
\ No newline at end of file
+          $relationship = node_gallery_get_relationship(NULL, $form['type']['#value']);
+          $field_name = $relationship['imagefield_name'];
+          
+          $form[$field_name]['#theme'] = 'content_single_value';
+          unset($form[$field_name]['0']['_weight']);
+          unset($form[$field_name][$field_name.'_add_more']);
+          unset($form[$field_name]['1']);
\ No newline at end of file
           if ($form['fupload']) {
             unset($form['body_field']);
             // @todo Need to display cover radios and tabledrag for weight on fupload form.
Index: contributions/modules/node_gallery/theme/theme.inc
--- contributions/modules/node_gallery/theme/theme.inc Base (1.2.2.11)
+++ contributions/modules/node_gallery/theme/theme.inc Locally Modified (Based On 1.2.2.11)
@@ -83,17 +83,16 @@
   $config = $vars['config'];
   
   $image_view = theme('image_view', $config['image_size']['preview'], $node);
+  $relationship = node_gallery_get_relationship(NULL, $node->type);
   switch ($config['original']['view_original']) {
     case 'default':
-      // @todo: eliminate hardcoded reference to field_node_gallery_image below
-      $output = l($image_view, file_create_url($node->field_node_gallery_image[0]['filepath']),
+      $output = l($image_view, file_create_url($node->{$relationship['imagefield_name']}[0]['filepath']),
          array('attributes' => array('target' => '_blank'), 'html' => TRUE));
       break;
     case 'text':
       $download_text = empty($config['original']['view_original_text']) ? t('Download the Original Image') : $config['original']['view_original_text'];
       $output = $image_view;
-      // @todo: eliminate hardcoded reference to field_node_gallery_image below
-      $extra = '<div class="download-full-link">'. l($download_text, file_create_url($node->field_node_gallery_image[0]['filepath']), array('attributes' => array('target' => '_blank'), 'html' => FALSE)) .'</div>';
+      $extra = '<div class="download-full-link">'. l($download_text, file_create_url($node->{$relationship['imagefield_name']}[0]['filepath']), array('attributes' => array('target' => '_blank'), 'html' => FALSE)) .'</div>';
       break;
     default:
       $output = $image_view;
@@ -122,8 +121,8 @@
 }
 
 function theme_image_view($imagecache, $image) {
-  // @todo: eliminate hardcoded reference to field_node_gallery_image below
-  return theme('imagecache', $imagecache, $image->field_node_gallery_image[0]['filepath'], $image->title, $image->title);
+  $relationship = node_gallery_get_relationship(NULL, $node->type);
+  return theme('imagecache', $imagecache, $image->{$relationship['imagefield_name']}[0]['filepath'], $image->title, $image->title);
 }
 
 function theme_gallery_edit_images_form(&$form) {
@@ -151,7 +150,9 @@
   return $output;
 }
 
-function theme_gallery_sort_images_form(&$form) {
\ No newline at end of file
+// @todo Have a look at the form array passed to this function, it's crazy, we
+// should really eliminate this function, so I left the hardcoded field name.
+function theme_gallery_sort_images_form($form) {
\ No newline at end of file
   drupal_add_tabledrag('upload-attachments', 'order', 'sibling', 'upload-weight');
   
   $thumb_imagecache = $form['#thumb_imagecache'];
