--- image_import.module.old	Fri Jan 18 10:59:13 2008
+++ image_import.module	Fri Feb 22 01:12:36 2008
@@ -71,6 +71,12 @@ 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 folders structure with subgalleries');
+        '#description' => 'A subgallery will be created only if the folder contains at least an image.',
+      );
     }
 
     // Put the image files into an array for the checkboxes and gather
@@ -183,6 +189,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 +199,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 folders 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] = _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) {
@@ -223,6 +237,60 @@ function image_import_form_submit($form,
     }
   }
 }
+
+/**
+ * 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 _create_subgalleries($origname,$image_galleries_vid,$subgalleries_parent_tid,&$subgalleries) {
+  // get a term id from its parent, its vocabulary, and its name
+  $tid_query = 'SELECT {term_data}.tid FROM {term_data}, {term_hierarchy} WHERE {term_data}.tid = {term_hierarchy}.tid AND parent = %d AND vid = %d AND name = \'%s\'';
+  // 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)
+      $subgallery_tid = db_result(db_query($tid_query, $parent_tid, $image_galleries_vid, $subgallery_name));
+      // if not, create it
+      if(!is_int($subgallery_tid)) {
+        $subgallery_term = array(
+          'name' => $subgallery_name,
+          'parent' => $parent_tid,
+          'vid' => $image_galleries_vid,
+        );
+        taxonomy_save_term($subgallery_term);
+        // get its tid back
+        $subgallery_tid = (int) db_result(db_query($tid_query, $parent_tid, $image_galleries_vid, $subgallery_name));
+      }
+      $subgalleries[$subgallery_path] = $subgallery_tid;
+    }
+    // for the next subgallery to be created, last created will be its parent
+    $parent_tid = $subgallery_tid;
+  }
+  return $subgallery_tid;
+} 
 
 function image_import_admin_settings() {
   $form['image_import_path'] = array(
