diff options
author | Silvio <s1lv10@uol.com.br> | 2010-03-23 10:35:12 -0300 |
---|---|---|
committer | Silvio <s1lv10@uol.com.br> | 2010-03-23 10:35:12 -0300 |
commit | 52fd889584ab1c535a0b84505aeb0f840f18d857 (patch) | |
tree | 810289b4c6ce67a291a41c8403ab0b25fc1b0780 | |
download | mass_image_import-52fd889584ab1c535a0b84505aeb0f840f18d857.tar.gz mass_image_import-52fd889584ab1c535a0b84505aeb0f840f18d857.tar.bz2 |
Initial import
-rw-r--r-- | .mass_image_import.module.swp | bin | 0 -> 16384 bytes | |||
-rw-r--r-- | mass_image_import.info | 6 | ||||
-rw-r--r-- | mass_image_import.module | 180 |
3 files changed, 186 insertions, 0 deletions
diff --git a/.mass_image_import.module.swp b/.mass_image_import.module.swp Binary files differnew file mode 100644 index 0000000..097c3e1 --- /dev/null +++ b/.mass_image_import.module.swp diff --git a/mass_image_import.info b/mass_image_import.info new file mode 100644 index 0000000..40b9f10 --- /dev/null +++ b/mass_image_import.info @@ -0,0 +1,6 @@ +; $Id$ +name = Mass image import +description = Import tons of images in a single batch +core = 6.x +version = "6.x-0.1" +dependencies[] = image_import diff --git a/mass_image_import.module b/mass_image_import.module new file mode 100644 index 0000000..87d7cbf --- /dev/null +++ b/mass_image_import.module @@ -0,0 +1,180 @@ +<?php +// $Id$ + +/** + * @file + * Mass image import module. + * + * This module implements a bulk image import operation and uses + * code from image_import module. + */ + +/** + * Implementation of hook_menu(). + */ +function mass_image_import_menu() { + $items['admin/content/mass_image_import'] = array( + 'title' => t('Mass image import'), + 'description' => t('Import tons of images at once.'), + 'page callback' => 'mass_image_import', + 'access arguments' => array('administer content'), + 'type' => MENU_NORMAL_ITEM, + ); + + return $items; +} + +/** + * Include image_import helper functions. + */ +require_once drupal_get_path('module', 'image_import') .'image_import.pages.inc'; + +/** + * Menu callback. + */ +function mass_image_import() { + $output = t('Mass image import.'); + $output .= drupal_get_form('mass_image_import_form'); + return $output; +} + +/** + * Mass import form. + */ +function mass_image_import_form(&$form_state) { + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Import images'), + ); + return $form; +} + +/** + * Handle post-validation form-submission. + */ +function mass_image_import_form_submit($form, &$form_state) { + // Setup batch configuration + $batch = array( + 'title' => t('Importing images'), + 'operations' => array(array('mass_image_import_batch', array())), + 'finished' => 'mass_image_import_batch_finished', + 'file' => drupal_get_path('module', 'bcc') . '/bcc.import.inc', + ); + + // Run batch for operations + batch_set($batch); + batch_process(); +} + +/** + * Batch operation callback for image import. + */ +function mass_image_import_batch(&$context) { + if (empty($context['sandbox'])) { + $files = mass_image_import_get_files(); + $context['sandbox']['images'] = $files; + $context['sandbox']['progress'] = 0; + $context['sandbox']['max'] = count($files); + } + + $limit = variable_get('image_import_page_size', 50); + + for ($n = 0; $n <= $limit; $n++) { + if ($context['sandbox']['progress']++ > $context['sandbox']['max']) { + $context['finished'] = 1; + break; + } + + $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; + mass_image_import_file($context['sandbox']['images'][$context['sandbox']['progress']]); + } +} + +/** + * Make a list of image files. + */ +function mass_image_import_get_files() { + $dirpath = variable_get('image_import_path', ''); + if (!file_check_directory($dirpath)) { + drupal_set_message( + t("You need to configure the import directory on the image import module's <a href='!admin-settings-import'>settings page</a>.", + array('!admin-settings-import' => url('admin/settings/image/image_import'))), 'error'); + return; + } + + // Get file names + $files = file_scan_directory($dirpath, '.*'); + if (!$files) { + return; + } + + // Sort + ksort($files); + + // Convert to indexed array + foreach ($files as $file) { + $images[] = $file; + } + + return $images; +} + +/** + * Import a single file. + */ +function mass_image_import_file($file) { + $dirpath = variable_get('image_import_path', ''); + $origname = $file->basename; + + // Debug + watchdog('bcc', 'Importing '. $origname); + + // Assembly import data + $file_metadata_modules = module_implements('file_metadata'); + + $problems = image_import_validate_file($file); + // Allow other modules to supply metadata about the images being imported. + // hook_file_metadata() may populate the $file properties 'title' and + // 'description'. + foreach ($file_metadata_modules as $module) { + $function = $module . '_file_metadata'; + $function($file); + } + + // Build form + if ($filepath = file_check_location($dirpath .'/'. $origname, $dirpath)) { + $args = array( + 'node_type' => 'image', + 'title' => isset($file->title) ? $file->title : basename($file->name), + 'body' => isset($file->body) ? $file->body : basename($file->name), + 'taxonomy' => isset($form_state['values']['taxonomy']) ? $form_state['values']['taxonomy'] : array(), + 'filepath' => $filepath, + 'origname' => $origname, + ); + } + + // Create the node object. + if ($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']); + } + else { + watchdog('image_import', 'There was an error that prevented %filename from being imported.', + array('%filename' => $args['filepath']), WATCHDOG_ERROR); + } +} + +/** + * Callback for image import. + */ +function mass_image_import_batch_finished($success, $results, $operations) { + if ($success) { + if ($results > 0) { + $message = format_plural(count($results), t('One processed.'), t('@count processed.')); + } + } + else { + $message = t('Finished with an error.'); + } + drupal_set_message($message); +} |