Index: image_import.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/contrib/image_import/image_import.pages.inc,v
retrieving revision 1.3
diff -u -r1.3 image_import.pages.inc
--- image_import.pages.inc	9 Mar 2009 03:22:43 -0000	1.3
+++ image_import.pages.inc	29 Aug 2009 21:31:55 -0000
@@ -50,6 +50,19 @@
       unset($form['type']);
       unset($form['#node']);
     }
+    if (module_exists('image_gallery')) {
+      $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
@@ -185,26 +198,48 @@
         'taxonomy' => isset($form_state['values']['taxonomy']) ? $form_state['values']['taxonomy'] : array(),
         'filepath' => $filepath,
         'origname' => $origname,
+        'subgallery' => $form_state['values']['tree'],
       );
       $batch['operations'][] = array('_image_import_batch_op', array($args));
     }
   }
+  if ($form_state['values']['delete_directories']) {
+    $batch['operations'][] = array('_image_import_recursive_delete_empty_directories_batch_op', array(array('basepath' => $form['#dirpath'])));
+  }
 
   batch_set($batch);
 }
 
 function _image_import_batch_op($args, &$context) {
+  $error = FALSE;
+  // if user choses to reproduce directories structure, save the chosen gallery,
+  // then create, if needed, the nested galleries where the image goes
+  if ($args['subgallery']) {
+    static $subgalleries = array();
+    $image_galleries_vid = _image_gallery_get_vid();
+    $subgalleries_parent_tid = $args['taxonomy'][$image_galleries_vid];
+    $args['taxonomy'][$image_galleries_vid] = _image_import_create_subgalleries(
+      $args['origname'],
+      $image_galleries_vid,
+      $subgalleries_parent_tid,
+      $subgalleries,
+      $context
+    );
+    if (!$args['taxonomy'][$image_galleries_vid]) {
+      $error = TRUE;
+    }
+  }
+
   // Create the node object.
-  if ($node = image_create_node_from($args['filepath'], $args['title'], $args['body'], $args['taxonomy'])) {
+  if (!$error && $node = image_create_node_from($args['filepath'], $args['title'], $args['body'], $args['taxonomy'])) {
     // Remove the original image now that the import has completed.
     file_delete($args['filepath']);
 
-    $context['results']['good'][] = t('Imported %origname as <a href="!node-link">@node-title</a> @status <a href="!edit-link">[edit]</a>.', array(
+    $context['results']['good'][] = t('Imported %origname as !link @status [!edit-link].', array(
       '%origname' => $args['origname'],
-      '!node-link' => url('node/'. $node->nid),
-      '@node-title' => $node->title,
+      '!link' => l($node->title, 'node/'. $node->nid),
       '@status' => $node->status ? '' : t('(Unpublished)'),
-      '!edit-link' => url('node/'. $node->nid .'/edit'),
+      '!edit-link' => l(t('edit'), 'node/'. $node->nid .'/edit'),
     ));
   }
   else {
@@ -229,3 +264,116 @@
   }
   watchdog('image_import', 'Completed image import.');
 }
+
+/**
+ * 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.
+ * @param $context
+ *   The execution log.
+ * @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, &$context) {
+  // get a term id from its parent (if any), its vocabulary, and its name
+  $tid_query = 'SELECT td.tid FROM {term_data} td, {term_hierarchy} th WHERE td.tid = th.tid AND vid = %d AND name = \'%s\' AND parent = %d';
+  $requested_subgalleries = explode('/', $origname); // explode path to image
+  array_pop($requested_subgalleries); // remove the filename at the end
+  $parent_tid = (int) $subgalleries_parent_tid; // parent gallery
+  $subgallery_tid = (int) $subgalleries_parent_tid; // current image's gallery
+  // create each gallery in the path, if necessary
+  foreach ($requested_subgalleries as $key => $subgallery_name) {
+    // the current subgallery's relative path
+    $subgallery_path = implode('/', array_slice($requested_subgalleries, 0, $key+1));
+    // was this subgallery already created by the current batch ?
+    $subgallery_tid = $subgalleries[$subgallery_path];
+    if (!$subgallery_tid) {
+      // first, look in DB
+      $subgallery_tid = db_result(db_query($tid_query, $image_galleries_vid, $subgallery_name, $parent_tid));
+      // didn't find? create it
+      if (!$subgallery_tid) {
+        $subgallery_term = array(
+          'name' => $subgallery_name,
+          'parent' => $parent_tid,
+          'vid' => $image_galleries_vid,
+        );
+        $status = taxonomy_save_term($subgallery_term);
+        if ($status) {
+          // get its tid back
+          $subgallery_tid = db_result(db_query($tid_query, $image_galleries_vid, $subgallery_name, $parent_tid));
+          $term = taxonomy_get_term($subgallery_tid);
+          $context['results']['good'][] = t('Created gallery !link [!edit-link].', array(
+            '!link' => l($subgallery_name, taxonomy_term_path($term)),
+            '!edit-link' => l(t('edit'), 'admin/content/taxonomy/edit/term/'. $subgallery_tid),
+          ));
+        }
+        else {
+          watchdog('image_import', 'There was an error that prevented gallery %gallery_path from being created.', array('%gallery_path' => $subgallery_path), WATCHDOG_ERROR);
+          $context['results']['bad'][] = t('Error creating gallery %gallery_path.', array('%gallery_path' => $subgallery_path));
+          return 0; // return an error
+        }
+      }
+      $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;
+}
+
+
+/**
+ * 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;
+}
+
+function _image_import_recursive_delete_empty_directories_batch_op($args, &$context) {
+  $deleted = _image_import_recursive_delete_empty_directories($args['basepath']);
+  if (!empty($deleted)) {
+    $context['results']['good'][] = t('Deleted directories: !dir-list.', array(
+      '!dir-list' => theme('item_list', $deleted),
+    ));
+  }
+  else {
+    $context['results']['good'][] = t('No directories were deleted.');
+  }
+  $context['finished'] = 1;
+}
Index: translations/image_import.pot
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/contrib/image_import/translations/image_import.pot,v
retrieving revision 1.4
diff -u -r1.4 image_import.pot
--- translations/image_import.pot	27 Dec 2008 11:08:36 -0000	1.4
+++ translations/image_import.pot	29 Aug 2009 21:25:44 -0000
@@ -1,19 +1,19 @@
-# $Id: image_import.pot,v 1.4 2008/12/27 11:08:36 hass Exp $
+# $Id$
 #
-# LANGUAGE translation of Drupal (contrib-image_import)
+# LANGUAGE translation of Drupal (general)
 # Copyright YEAR NAME <EMAIL@ADDRESS>
 # Generated from files:
-#  image_import.admin.inc,v 1.2 2008/12/22 21:37:28 drewish
-#  image_import.pages.inc,v 1.2 2008/12/27 06:58:41 drewish
-#  image_import.module,v 1.19 2008/12/27 06:58:41 drewish
-#  image_import.install,v 1.6 2008/12/27 06:58:41 drewish
+#  image_import.admin.inc,v 1.3 2009/03/09 02:57:25 sun
+#  image_import.pages.inc,v 1.3 2009/03/09 03:22:43 sun
+#  image_import.module,v 1.21 2009/03/09 02:57:25 sun
+#  image_import.install,v 1.7 2009/01/15 02:10:52 sun
 #  image_import.info,v 1.4 2008/01/06 22:15:25 drewish
 #
 #, fuzzy
 msgid ""
 msgstr ""
 "Project-Id-Version: PROJECT VERSION\n"
-"POT-Creation-Date: 2008-12-27 12:04+0100\n"
+"POT-Creation-Date: 2009-08-29 23:25+0200\n"
 "PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n"
 "Last-Translator: NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <EMAIL@ADDRESS>\n"
@@ -22,139 +22,175 @@
 "Content-Transfer-Encoding: 8bit\n"
 "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
 
-#: contrib/image_import/image_import.admin.inc:7
+#: image_import.admin.inc:10
 msgid "Import path"
 msgstr ""
 
-#: contrib/image_import/image_import.admin.inc:10
+#: image_import.admin.inc:12
 msgid "The directory to import image nodes from. Drupal will need to have write access to this directory so we can move the file."
 msgstr ""
 
-#: contrib/image_import/image_import.admin.inc:11
+#: image_import.admin.inc:13
 msgid "<strong>Note:</strong> a path begining with a <kbd>/</kbd> indicates the path is relative to the server's root, one starting without specifies a path relative to Drupal's root. I.e. <kbd>/tmp/image</kbd> would be the temp directory off the root while <kbd>tmp/image</kbd> would be inside Drupal's directory."
 msgstr ""
 
-#: contrib/image_import/image_import.admin.inc:29
+#: image_import.admin.inc:36
 msgid "You can't import from the image module's directory. The import deletes the original files so you would just be asking for trouble."
 msgstr ""
 
-#: contrib/image_import/image_import.admin.inc:32
+#: image_import.admin.inc:47
 msgid "Your settings are configured correctly, you can import images <a href='!image_import_page'>here</a>."
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:12
+#: image_import.pages.inc:12
 msgid "Not a JPG, GIF or PNG file."
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:23
+#: image_import.pages.inc:23
 msgid "You need to configure the import directory on the image import module's <a href='!admin-settings-import'>settings page</a>."
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:115
+#: image_import.pages.inc:56
+msgid "Reproduce directories structure with subgalleries"
+msgstr ""
+
+#: image_import.pages.inc:57
+msgid "A subgallery will be created only if the folder contains at least an image."
+msgstr ""
+
+#: image_import.pages.inc:138
 msgid "Import"
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:121
+#: image_import.pages.inc:144
 msgid "No files were found."
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:133
+#: image_import.pages.inc:156
+msgid "Name"
+msgstr ""
+
+#: image_import.pages.inc:156
 msgid "Size"
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:133
+#: image_import.pages.inc:156
 msgid "Dimensions"
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:161
+#: image_import.pages.inc:184
 msgid "Importing image"
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:192
-msgid "Imported %origname as <a href=\"!node-link\">@node-title</a> @status <a href=\"!edit-link\">[edit]</a>."
+#: image_import.pages.inc:238
+msgid "Imported %origname as !link @status [!edit-link]."
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:196
+#: image_import.pages.inc:241
 msgid "(Unpublished)"
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:202
+#: image_import.pages.inc:242;313
+msgid "edit"
+msgstr ""
+
+#: image_import.pages.inc:247
 msgid "Error importing %filename."
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:211
+#: image_import.pages.inc:256
 msgid "There was a problem importing files: !bad-list"
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:214
+#: image_import.pages.inc:259
 msgid "There was a problem importing the files."
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:218
+#: image_import.pages.inc:263
 msgid "Successfully imported: !good-list"
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:201;220 contrib/image_import/image_import.module:0
+#: image_import.pages.inc:311
+msgid "Created gallery !link [!edit-link]."
+msgstr ""
+
+#: image_import.pages.inc:318
+msgid "Error creating gallery %gallery_path."
+msgstr ""
+
+#: image_import.pages.inc:371
+msgid "Deleted directories: !dir-list."
+msgstr ""
+
+#: image_import.pages.inc:376
+msgid "No directories were deleted."
+msgstr ""
+
+#: image_import.pages.inc:246;265;317
 msgid "image_import"
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:201
+#: image_import.pages.inc:246
 msgid "There was an error that prevented %filename from being imported."
 msgstr ""
 
-#: contrib/image_import/image_import.pages.inc:220
+#: image_import.pages.inc:265
 msgid "Completed image import."
 msgstr ""
 
-#: contrib/image_import/image_import.module:10
-msgid "Import multiple image files and save them as image nodes. The files will be moved from their location into the image module's files directory. Searching for image files in %dirpath."
+#: image_import.pages.inc:317
+msgid "There was an error that prevented gallery %gallery_path from being created."
 msgstr ""
 
-#: contrib/image_import/image_import.module:12
-msgid "Configure the image import module's settings."
+#: image_import.module:10
+msgid "Import multiple image files and save them as image nodes. The files will be moved from their location into the image module's files directory. Searching for image files in %dirpath."
 msgstr ""
 
-#: contrib/image_import/image_import.module:22
+#: image_import.module:18
 msgid "import images"
 msgstr ""
 
-#: contrib/image_import/image_import.module:32;41 contrib/image_import/image_import.install:10
+#: image_import.module:26;35 image_import.install:10
 msgid "Image import"
 msgstr ""
 
-#: contrib/image_import/image_import.module:33
+#: image_import.module:27
 msgid "Import image from the filesystem."
 msgstr ""
 
-#: contrib/image_import/image_import.module:42
+#: image_import.module:36
 msgid "Change settings for the Image Import module."
 msgstr ""
 
-#: contrib/image_import/image_import.install:14
+#: image_import.install:14
 msgid "Import directory has not been configured."
 msgstr ""
 
-#: contrib/image_import/image_import.install:15
+#: image_import.install:15
 msgid "The import directory must be <a href=\"@configure\">configured</a> and exist in order for the Image import module to function."
 msgstr ""
 
-#: contrib/image_import/image_import.install:20
+#: image_import.install:20
 msgid "Import directory %dirpath does not exist or is not writable."
 msgstr ""
 
-#: contrib/image_import/image_import.install:21
+#: image_import.install:21
 msgid "The import directory %dirpath either does not exist or does not grant the web container write permission. Either <a href=\"@choose\">choose</a> a different directory or create the %dirpath directory and grant write permissions. The Image import module will not function until this is corrected."
 msgstr ""
 
-#: contrib/image_import/image_import.install:25
+#: image_import.install:25
 msgid "Import directory %dirpath exists."
 msgstr ""
 
-#: contrib/image_import/image_import.info:0
+#: image_import.info:0
 msgid "Image Import"
 msgstr ""
 
-#: contrib/image_import/image_import.info:0
+#: image_import.info:0
 msgid "Allows batches of images to be imported from a directory on the server."
 msgstr ""
 
+#: image_import.info:0
+msgid "Image"
+msgstr ""
+

