aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mass_image_import.info1
-rw-r--r--mass_image_import.module98
2 files changed, 69 insertions, 30 deletions
diff --git a/mass_image_import.info b/mass_image_import.info
index 40b9f10..cfb8233 100644
--- a/mass_image_import.info
+++ b/mass_image_import.info
@@ -4,3 +4,4 @@ description = Import tons of images in a single batch
core = 6.x
version = "6.x-0.1"
dependencies[] = image_import
+dependencies[] = imagefield
diff --git a/mass_image_import.module b/mass_image_import.module
index ae4c844..54edacf 100644
--- a/mass_image_import.module
+++ b/mass_image_import.module
@@ -42,23 +42,45 @@ function mass_image_import() {
* Mass import form.
*/
function mass_image_import_form(&$form_state) {
+ $node_types = node_get_types();
+ $node_codes = array();
+
+ foreach ($node_types as $item) {
+ $node_codes[$item->type] = $item->name;
+ }
+
+ $form['node_type'] = array(
+ '#type' => 'select',
+ '#title' => t('Node type'),
+ '#options' => $node_codes,
+ '#default_value' => variable_get('mass_image_import_nodetype', array()),
+ '#description' => t('Select node type to import images. In case the 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;
}
/**
* Handle post-validation form-submission.
+ *
+ * @todo
+ * Validation.
*/
function mass_image_import_form_submit($form, &$form_state) {
+ // Save form input.
+ variable_set('node_truncate_node_type', $type);
+
// Setup batch configuration
+ $type = $form_state['values']['node_type'];
$batch = array(
'title' => t('Importing images'),
- 'operations' => array(array('mass_image_import_batch', array())),
+ 'operations' => array(array('mass_image_import_batch', array($type))),
'finished' => 'mass_image_import_batch_finished',
- 'file' => drupal_get_path('module', 'bcc') . '/bcc.import.inc',
);
// Run batch for operations
@@ -69,7 +91,7 @@ function mass_image_import_form_submit($form, &$form_state) {
/**
* Batch operation callback for image import.
*/
-function mass_image_import_batch(&$context) {
+function mass_image_import_batch($type, &$context) {
if (empty($context['sandbox'])) {
// Log
watchdog('image', 'Starting mass image import.');
@@ -96,7 +118,7 @@ function mass_image_import_batch(&$context) {
'@file' => $image->filename,
));
- mass_image_import_file($image);
+ mass_image_import_file($image, $type);
$context['sandbox']['progress']++;
}
}
@@ -138,11 +160,8 @@ function mass_image_import_get_files() {
*
* @param $file
* Image object to import.
- *
- * @todo
- * Switch between image_create_node_from and mass_image_import_create_node_from.
*/
-function mass_image_import_file($file) {
+function mass_image_import_file($file, $type = 'image') {
$dirpath = variable_get('image_import_path', '');
$origname = $file->basename;
@@ -164,15 +183,20 @@ function mass_image_import_file($file) {
'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'])) {
- if ($node = mass_image_import_create_node_from($args['filepath'], $args['title'], $args['body'], $args['taxonomy'])) {
+ 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']);
}
@@ -198,7 +222,7 @@ function mass_image_import_batch_finished($success, $results, $operations) {
$message = t('Finished with an error.');
}
- // Log
+ // Log.
watchdog('image', 'Finished mass image import.');
drupal_set_message($message);
}
@@ -208,15 +232,12 @@ function mass_image_import_batch_finished($success, $results, $operations) {
*
* @see
* http://drupal.org/node/330421#comment-2806336
- *
- * @todo
- * Mime, content type, field name, taxonomy, etc.
*/
-function mass_image_import_create_node_from($filepath, $title, $body, $taxonomy) {
-
- $type = 'foto';
- $field = 'foto';
+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']);
// Make sure we can copy the file into our temp directory.
$original_path = $filepath;
@@ -235,24 +256,41 @@ function mass_image_import_create_node_from($filepath, $title, $body, $taxonomy)
drupal_write_record('files', $file);
// Create the node.
- $node = new StdClass();
- $node->type = $type;
+ $node = new StdClass();
+ $node->type = $type;
// Add image field.
- $files = array();
- $files[0] = (array) $file;
- $files[0]['status'] = 0;
+ $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_'. $field] = $files;
+ $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;
}
+
+/**
+ * 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'];
+ }
+ }
+}