--- image_import.module.old	Fri Jan 18 10:59:13 2008
+++ image_import.module	Sun Feb 24 20:52:11 2008
@@ -71,7 +71,19 @@ function image_import_form() {
       taxonomy_form_alter($form, array(), 'image_node_form');
       unset($form['type']);
       unset($form['#node']);
+      
+      $form['tree'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Reproduce directories structure with subgalleries'),
+        '#description' => t('A subgallery will be created only if the folder contains at least an image.'),
+      );
     }
+    
+    $form['delete_directories'] = array(
+      '#type' => 'checkbox',
+      '#title' => 'Delete empty directories after import',
+      '#description' => '',
+    );
 
     // Put the image files into an array for the checkboxes and gather
     // additional information like dimensions and filesizes. Make sure that
@@ -183,6 +195,8 @@ function image_import_form_submit($form,
     if (file_check_directory($dirpath)) {
       $nodes = array();
       $files = array();
+      $subgalleries = array();
+      $image_galleries_vid = _image_gallery_get_vid();
       foreach (array_filter($form_state['values']['import_file']) as $index) {
         // try to avoid php's script timeout with a bunch of large files or
         // a slow machine
@@ -191,12 +205,18 @@ function image_import_form_submit($form,
         }
         $origname = $form_state['values']['file_list'][$index];
         $filename = file_check_location($dirpath .'/'. $origname, $dirpath);
+        $node_taxonomy = $form_state['values']['taxonomy'];
+        // if user chose to reproduce directories structure, save the chosen gallery, then create, if needed, the nested galleries where the image goes
+        if( $form_state['values']['tree'] ) {
+          $subgalleries_parent_tid = $node_taxonomy[$image_galleries_vid];
+          $node_taxonomy[$image_galleries_vid] = _image_import_create_subgalleries($origname,$image_galleries_vid,$subgalleries_parent_tid,$subgalleries);
+        }
         if ($filename) {
           $node = image_create_node_from(
             $filename,
             $form_state['values']['title'][$index],
             $form_state['values']['body'][$index],
-            $form_state['values']['taxonomy']
+            $node_taxonomy
           );
 
           if ($node) {
@@ -220,9 +240,116 @@ function image_import_form_submit($form,
       else {
         drupal_set_message(t('No image files were imported.'));
       }
+      
+      // delete empty directories if needed, then report progress
+      if( $form_state['values']['delete_directories'] ) {
+        $deleted = _image_import_recursive_delete_empty_directories($dirpath);
+        if(!empty($deleted)) {
+          drupal_set_message(t('The following directories were deleted:') . theme('item_list',$deleted));
+        }
+        else {
+          drupal_set_message(t('No directories were deleted.'));
+        }
+      }
+
     }
   }
 }
+
+/**
+ * Delete recursively all directories inside $basepath.
+ *  
+ * @param $basepath
+ *   The base directory where to find empty directories.
+ * @param $dir
+ *   A directory inside $basepath, used for recursive purpose.
+ *   Should not be used when calling this method.
+ * @return
+ *   An array of the deleted directories paths, relative to $basepath.
+ */
+function _image_import_recursive_delete_empty_directories($basepath, $dir='') {
+  $deleted = array();
+  // append $basepath to $dir only if $dir is not empty (to avoid trailing slash) 
+  if( $dir != '' ) {
+    $crnt_dir = $basepath.'/'.$dir;
+  } else {
+    $crnt_dir = $basepath;
+  }
+  // get each file in $crnt_dir, except . , .. and CVS
+  $ls = file_scan_directory($crnt_dir, '.*', array('.', '..', 'CVS'), 0, FALSE);
+
+  // for each directory inside
+  foreach($ls as $file) {
+    if( is_dir($file->filename) ) {
+      $next_dir = $dir ? $dir.'/'.$file->basename : $file->basename; 
+      // delete recursively empty directories inside
+      $deleted = array_merge($deleted, _image_import_recursive_delete_empty_directories($basepath, $next_dir));
+      // if the directory is empty, delete it
+      if( sizeof(file_scan_directory($file->filename, '.*', array('.', '..'), 0, FALSE)) == 0 ) {
+        rmdir($file->filename);
+        $deleted[] = '<em>' . $next_dir . '</em>';
+      }
+    }
+  }
+  return $deleted;
+} 
+
+/**
+ * Creates subgalleries.
+ * 
+ * @param $origname
+ *   The original name (with its path) of the image which subcategory has to be created.
+ * @param $image_galleries_vid
+ *   The vocabulary id of corresponding to image galleries.
+ * @param $subgalleries_parent_tid
+ *   The term id of the parent gallery, which will contain the subgallery.
+ * @param $subgalleries
+ *   An array of the tids of each subgallery already created, indexed by subgallery path.
+ * @return
+ *   The tid of the subgallery in which the image goes to.  
+ */
+function _image_import_create_subgalleries($origname,$image_galleries_vid,$subgalleries_parent_tid,&$subgalleries) {
+  // get a term id from its parent (if any), its vocabulary, and its name
+  $tid_query = 'SELECT {term_data}.tid FROM {term_data}, {term_hierarchy} WHERE {term_data}.tid = {term_hierarchy}.tid AND vid = %d AND name = \'%s\' AND parent = %d';
+  // get the exploded path to the image
+  $requested_subgalleries = explode('/', $origname);
+  // remove the filename at the end
+  array_pop($requested_subgalleries);
+  // the parent gallery of the first to be created was passed to this function
+  $parent_tid = (int) $subgalleries_parent_tid;
+  // in case there is no sub gallery to create, then the image goes to the parent passed to this function
+  $subgallery_tid = (int) $subgalleries_parent_tid;
+  // create each gallery in the path, if necessary
+  for($i=0; $i<sizeof($requested_subgalleries); $i++) {
+    // the current subgallery's name and relative path
+    $subgallery_name = $requested_subgalleries[$i];
+    $subgallery_path = implode('/',array_slice($requested_subgalleries,0,$i+1));
+    // verify whether this subgallery was already created by the current import or not
+    $subgallery_tid = $subgalleries[$subgallery_path];
+    if( !is_int($subgallery_tid) ) {
+      // try to get tid from database before creating (maybe the subgallery existed before)
+      $db_result = db_fetch_object(db_query($tid_query, $image_galleries_vid, $subgallery_name, $parent_tid));
+      $subgallery_tid = $db_result->tid;
+      // if not, create it
+      if($subgallery_tid == NULL) {
+        $subgallery_term = array(
+          'name' => $subgallery_name,
+          'parent' => $parent_tid,
+          'vid' => $image_galleries_vid,
+        );
+        taxonomy_save_term($subgallery_term);
+        drupal_set_message(t('Created new gallery %term.', array('%term' => $subgallery_name)));
+        // get its tid back
+        $db_result = db_fetch_object(db_query($tid_query, $image_galleries_vid, $subgallery_name, $parent_tid));
+        $subgallery_tid = (int)$db_result->tid;
+      }
+      $subgalleries[$subgallery_path] = $subgallery_tid;
+    }
+    // for the next subgallery to be created, last created will be its parent
+    $parent_tid = (int)$subgallery_tid;
+  }
+  return $subgallery_tid;
+} 
 
 function image_import_admin_settings() {
   $form['image_import_path'] = array(
