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['node_type'] = array( '#type' => 'select', '#title' => t('Node type'), '#options' => mass_image_import_types(), '#default_value' => variable_get('mass_image_import_node_type', array()), '#description' => t('Select node type to import images. For CCK, if node type contains more than one filefield, make sure that the imagefield is the first one.'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Import images'), ); return $form; } /** * Return the existing content types. * * @return * Content types. */ function mass_image_import_types() { $node_types = node_get_types(); $node_codes = array(); foreach ($node_types as $item) { $node_codes[$item->type] = $item->name; } return $node_codes; } /** * Implementation of hook_validate(). */ function mass_image_import_form_validate($form, $form_state) { if (!isset($form_state['values']['node_type'])) { form_set_error('node_type', t('Please choose a content type')); } else { $type = $form_state['values']['node_type']; $types = node_get_types(); if (!isset($types[$type])) { form_set_error('node_type', t('Invalid content type')); } } } /** * Handle post-validation form-submission. */ function mass_image_import_form_submit($form, &$form_state) { // Save form input. $type = $form_state['values']['node_type']; variable_set('mass_image_import_node_type', $type); // Setup batch configuration $batch = array( 'title' => t('Importing images'), 'operations' => array(array('mass_image_import_batch', array($type))), 'finished' => 'mass_image_import_batch_finished', ); // Run batch for operations batch_set($batch); batch_process(); } /** * Batch operation callback for image import. */ function mass_image_import_batch($type, &$context) { if (empty($context['sandbox'])) { // Log watchdog('image', 'Starting mass image import.'); $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']; $image = $context['sandbox']['images'][$context['sandbox']['progress']]; $context['results'][] = $image->filename; $context['message'] = t('Importing @file...', array( '@file' => $image->filename, )); mass_image_import_file($image, $type); $context['sandbox']['progress']++; } } /** * Callback for image import. */ function mass_image_import_batch_finished($success, $results, $operations) { if ($success) { if (count($results) > 0) { $message = format_plural(count($results), t('One image processed.'), t('@count images processed.')); } else { $message = t('No images to import.'); } } else { $message = t('Finished with an error.'); } // Log. watchdog('image', 'Finished mass image import.'); drupal_set_message($message); } /** * Make a list of image files. * * @return * List of available images for import. */ 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 settings page.", 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. * * @param $file * Image object to import. */ function mass_image_import_file($file, $type = 'image') { $dirpath = variable_get('image_import_path', ''); $origname = $file->basename; // Assembly import data. $file_metadata_modules = module_implements('file_metadata'); // Validate. $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) ? check_plain($file->title) : basename($file->name), 'body' => isset($file->body) ? check_plain($file->body) : basename($file->name), 'filepath' => $filepath, 'origname' => $origname, ); } // Create the node object. if ($type == 'image') { $node = image_create_node_from($args['filepath'], $args['title'], $args['body'], NULL); } else { $node = mass_image_import_create_node_from($args['filepath'], $args['title'], $args['body'], $type); } if ($node) { // 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); } } /** * Get the first filefield from a content type. * * @param $fields * Content type fields. * * @return * Field name. */ function mass_image_import_field($fields) { foreach ($fields as $field) { if ($field['type'] == 'filefield') { return $field['field_name']; } } } /** * Create an image entry. Contains some code from image_create_node_from(). * * @see * http://drupal.org/node/330421#comment-2806336 */ function mass_image_import_create_node_from($filepath, $title, $body, $type) { // Setup. $uid = 1; $info = content_types($type); $field = mass_image_import_field($info['fields']); // Copy file. $original_path = $filepath; $destpath = file_create_path(file_directory_path() . '/' . variable_get('image_default_path', 'images')); if (!file_copy($filepath, $destpath)) { return FALSE; } // File data. $file = new stdClass(); $file->filename = basename($filepath); $file->filepath = $filepath; $file->filemime = file_get_mimetype($filepath); $file->filesize = filesize($filepath); $file->uid = $uid; $file->timestamp = time(); drupal_write_record('files', $file); // Create node. $node = new stdClass(); $node->type = $type; // Add image field. $files = array(); $files[0] = (array) $file; $files[0]['status'] = 0; // Node form. $form_state = array(); $form_state['values']['type'] = $type; $form_state['values']['title'] = $title; $form_state['values']['body'] = $body; $form_state['values']['op'] = t('Save'); $form_state['values'][$field] = $files; // Submit. module_load_include('inc', 'node', 'node.pages'); drupal_execute($type .'_node_form', $form_state, $node); return $node; }