--- imagefield_import-old.module 2008-12-14 05:26:52.000000000 +0100 +++ imagefield_import.module 2009-02-25 13:51:53.411030200 +0100 @@ -78,6 +78,7 @@ // PHP function that sorts an array by key ksort($files); if ($files) { + $form['caption'] = array( '#type' => 'textfield', '#title' => t('Caption'), '#size' => 30, '#required' => TRUE, '#description' => t('Define a caption the new node will be saved with.')); if (module_exists('taxonomy')) { // a little hack from the image_import.module to get the taxonmy controls onto our form--- $form['type'] = array('#type' => 'value', '#value' => $type); @@ -93,7 +94,7 @@ // there's no 0th element, because a checkbox with a zero value is seen as // unchecked and won't be imported. // a $fields multidimentional array for each of the files will need these elements - $fields = array('filesize', 'dimensions', 'title', 'body'); + $fields = array('filesize', 'dimensions', 'title', 'link'); foreach ($fields as $field) { $form['files'][$field][0] = NULL; //$form['files'][filesize][0], etc. = null @@ -109,8 +110,10 @@ $filelist[] = substr($file->filename, strlen($dirpath) + 1); $form['files']['filesize'][] = array( '#type' => 'item', '#value' => format_size(filesize($file->filename)), ); $form['files']['dimensions'][] = array( '#type' => 'item', '#value' => $info['width'] .'x'. $info['height'], ); - $form['files']['title'][] = array( '#type' => 'textfield', '#size' => 20, '#default_value' => basename($file->name), ); - $form['files']['body'][] = array( '#type' => 'textfield', '#size' => 20, ); + $form['files']['title'][] = array( '#type' => 'textfield', '#size' => 20, '#default_value' => '', ); + $form['files']['link'][] = array( '#type' => 'hidden', '#value' => $file->filename); +// $dirpath +// $form['files']['body'][] = array( '#type' => 'textfield', '#size' => 20, ); } } // Remove our 0 elements. @@ -134,27 +137,34 @@ else { $form['import_file'] = array( '#type' => 'item', '#value' => t('No files were found'), ); } + $form['delete_files'] = array( '#type' => 'checkbox', '#title' => t('Delete imported files after successfull node creation'), '#default_value' => 1); return $form; } //Theme the import form function theme_imagefield_import_form($form) { +// var_dump($form); die; $output = ""; - if (isset($form['conf_message'])) $output .= drupal_render($form['conf_message']); - if (!variable_get('imagefield_import_fieldname', FALSE) ) { - drupal_set_message("Could not import images. Please make sure to set Target Field on the ". l("Imagefield Settings", "admin/settings/imagefield_import") ." page.", "error"); - return; - } - if (isset($form['import_file']) && $form['import_file']['#type'] == 'checkboxes') { - $header = array(theme('table_select_header_cell'), t('Name'), t('Size'), t('Dimensions'), t('Title'), t('Body')); - $rows = array(); - foreach (element_children($form['import_file']) as $key) { + if (isset($form['conf_message'])) $output .= drupal_render($form['conf_message']); + if (!variable_get('imagefield_import_fieldname', FALSE) ) { + drupal_set_message("Could not import images. Please make sure to set Target Field on the ". l("Imagefield Settings", "admin/settings/imagefield_import") ." page.", "error"); + return; + } + if (isset($form['caption'])) $output .= drupal_render($form['caption']); + if (isset($form['import_file']) && $form['import_file']['#type'] == 'checkboxes') { + $header = array(theme('table_select_header_cell'), t('Name'), t('Size'), t('Dimensions'), t('Title'), /*t('Body')*/ t('Thumbnail')); + $rows = array(); + foreach (element_children($form['import_file']) as $key) { $filename = $form['import_file'][$key]['#title']; unset($form['import_file'][$key]['#title']); - $rows[] = array(drupal_render($form['import_file'][$key]), $filename, drupal_render($form['files']['filesize'][$key]), drupal_render($form['files']['dimensions'][$key]), drupal_render($form['files']['title'][$key]), drupal_render($form['files']['body'][$key]), ); + $rows[] = array(drupal_render($form['import_file'][$key]), $filename, drupal_render($form['files']['filesize'][$key]), drupal_render($form['files']['dimensions'][$key]), drupal_render($form['files']['title'][$key]), + l(theme_image($form['files']['link'][$key]['#value'], '', '', array('width' => '120'), FALSE), $form['files']['link'][$key]['#value'], array('attributes' => array('target' => '_new'), 'html' => TRUE ) ), + ); } $output .= theme('table', $header, $rows); +// unset($form['files']['link']); } + if (isset($form['delete_files'])) $output .= drupal_render($form['delete_files']); return $output . drupal_render($form); } @@ -165,10 +175,11 @@ if (file_check_directory($source_path)) { // determine the node type, and the target fieldname to import into $targetsetting = variable_get('imagefield_import_fieldname', FALSE); - list($node_type, $field) = split(":::", $targetsetting); + list($node_type, $field_name) = split(":::", $targetsetting); // try to avoid php's script timeout with a bunch of large files or a slow machine if (!ini_get('safe_mode')) set_time_limit(0); + $files = array(); foreach (array_filter($form_state['values']['import_file']) as $index) { // Check the file is OK $filename = $form_state['values']['file_list'][$index]; @@ -177,21 +188,27 @@ $file_info = image_get_info($source_filepath); $file_info['filepath'] = $source_filepath; // This is follwoing the method suggested by: http://drupal.org/node/292904 ... - $file = array($field => $file_info); - $title = $form_state['values']['title'][$index]; - $caption = $form_state['values']['body'][$index]; + $files[] = array('field_name' => $field_name, 'file_info' => $file_info, 'title' => $form_state['values']['title'][$index]); +// $caption = $form_state['values']['body'][$index]; $taxonomy = $form_state['values']['taxonomy']; - if (_imagefield_import_process_form($file, $node_type, $title, $caption, $taxonomy)) - // delete original if it all went well. - file_delete($source_filepath); } - else { + else drupal_set_message('Problem with the file location:'. $source_filepath, 'error'); - } } // foreach( file ) + if (count($files)) { +// var_dump($form_state); die; + $node = _imagefield_import_process_form($files, $node_type, $form_state['values']['caption'], $taxonomy); + if ($node) { + // delete original if it all went well. + if ($form_state['values']['delete_files'] == 1) foreach($files as $f) file_delete($f['file_info']['filepath']); + dsm($node); + drupal_goto('node/'.$node->nid.'/edit'); + } else drupal_set_message(t('Node creation problem.')); +// dsm($files); + } else drupal_set_message(t('No valid images found.')); } else { - drupal_set_message('Source directory has not been set in the settings?', 'error'); + drupal_set_message(t('Source directory has not been set in the settings?'), 'error'); } } } @@ -199,44 +216,47 @@ /** * see: http://drupal.org/node/292904 */ -function _imagefield_import_process_form($file_list, $node_type, $title, $caption, $taxonomy) { +function _imagefield_import_process_form($file_list, $node_type, $caption, $taxonomy) { // For field_file_save_file() module_load_include('inc', 'filefield', 'field_file'); // Loop over all the field/filepath pairs and create files objects. $files = array(); - foreach ($file_list as $field_name => $file_info) { + foreach ($file_list as $f) { // $f = array ($field_name => $file_info ) // Load the field and figure out the specific details. - $field = content_fields($field_name, $node_type); + $field = content_fields($f['field_name'], $node_type); $validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field)); $files_path = _imagefield_import_widget_files_directory($field); - if (!$file = field_file_save_file($file_info['filepath'], $validators, $files_path)) + if (!$file = field_file_save_file($f['file_info']['filepath'], $validators, $files_path)) return FALSE; - $files[$field_name] = $file; + $files[] = array( 'field_name' => $f['field_name'], 'file' => $file, 'title' => $f['title']); } // Create the node object. - return _imagefield_import_create_node($node_type, $files, $title, $caption, $taxonomy); + return _imagefield_import_create_node($node_type, $files, $caption, $taxonomy); } /** * see: http://drupal.org/node/292904 */ -function _imagefield_import_create_node($node_type, $files, $title, $caption, $taxonomy) { +function _imagefield_import_create_node($node_type, $files, $caption, $taxonomy) { + global $user; // For node_object_prepare() module_load_include('inc', 'node', 'node.pages'); $node = new stdClass(); $node->type = $node_type; - $node->title = $title; - $node->body = $caption; + $node->uid = $user->uid; + $node->name = $user->name; + $node->title = $caption; + $node->body = ''; node_object_prepare($node); - $node->field_caption[0] = array('value' => $caption); $node->taxonomy = $taxonomy; // Get the filefield module to do the saving and marking the record as permanent. - foreach ($files as $field_name => $file) { - if (isset($node->$field_name)) { - array_push($node->$field_name, $file); + foreach ($files as $f) { // $f = array( $field_name => $file) + $node->field_caption[] = array('value' => $f['title']); + if (isset($node->$f['field_name'])) { + array_push($node->$f['field_name'], $f['file']); } else { - $node->$field_name = array(0 => $file); + $node->$f['field_name'] = array(0 => $f['file']); } } $node = node_submit($node);