'. t('This is where the Import Magic happens. If there are images currently in the Directory you set up for import you will see them in the list below. You can customize the Title and body text of each of the nodes before submitting them all for creation. Do not forget to click the checkbox on the left that indicates you want to import this image into an imagefield node, or click the top checkbox to select or deslect all.') .'

'; case 'admin/settings/imagefield_import': return '

'. t('The Imagefield Import Path is where you want to transfer -likely FTP or SSH- your images that are in need of uploading, then select the targe field. You will be creating one node for each image. So here you are choosing what field you want your imported image to populate') .'

'; } } /** * Implementation of hook_perm(). * * Define Permissions for the module */ function imagefield_import_perm() { return array('import images'); } /** * Implementation of hook_menu(). * * 2 pages are created: * admin/content/imagefield_import (The Import Page), * and admin/settings/imagefield_import (The Settings Page). */ function imagefield_import_menu() { $items['admin/content/imagefield_import'] = array( 'title' => 'Imagefield Import', 'page callback' => 'drupal_get_form', 'page arguments' => array('imagefield_import_form', 2), 'access arguments' => array('import images'), 'type' => MENU_NORMAL_ITEM, 'description' => 'Import images into CCK imagefields from an import folder in filesystem.' ); $items['admin/settings/imagefield_import'] = array( 'title' => 'Imagefield Import', 'page callback' => 'drupal_get_form', 'page arguments' => array('imagefield_import_admin_settings', 2), 'access arguments' => array('administer site configuration'), 'type' => MENU_NORMAL_ITEM, 'description' => 'Import images into CCK imagefields from an import folder in filesystem.' ); return $items; } // Create the imagefield_import_form that was defined in callback arguments in hook_menu(). function imagefield_import_form() { $form = array(); //get the content type and field names $targetsetting = variable_get('imagefield_import_fieldname', FALSE); list($type, $field) = split(":::", $targetsetting); // Getting the path the user set as in import folder $dirpath = variable_get('imagefield_import_source_path', ''); // Check it's configuration out and throw a DSM if it isn't configured properly if (!file_check_directory($dirpath)) { drupal_set_message(t("You need to configure the import directory on the imagefield_import module's settings page.
If you don't have the proper permissions to do that, please contact the site administrator.", array('!admin-settings-imagefield_import' => url('admin/settings/imagefield_import'))), 'error'); return $form; } // Drupal Function file_scan_directory($dir, $mask) -- Load Up the $files array will all of the files in the import directory $files = file_scan_directory($dirpath, '.*'); // PHP function that sorts an array by key ksort($files); if ($files) { 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); $form['#node'] = new stdClass(); $form['#node']->type = $type; $form_id = $type .'_node_form'; taxonomy_form_alter(&$form, &$form_state, $form_id); unset($form['type']); unset($form['#node']); } // Put the image files into an array for the checkboxes and gather // additional information like dimensions and filesizes. Make sure that // 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'); foreach ($fields as $field) { $form['files'][$field][0] = NULL; //$form['files'][filesize][0], etc. = null } $filelist = array(0 => NULL); //cycle through each file that was found foreach ($files as $file) { // image_get_info is a Drupal Function That Gets Details about an image $info = image_get_info($file->filename); // first checks out the files, to make shure they're of a supported filetype // Then fills out the import form with information about each image (including 2 text areas for title and body) if ($info && isset($info['extension'])) { $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, ); } } // Remove our 0 elements. unset($filelist[0]); foreach ($fields as $field) { $form['files'][$field][0] = NULL; } // Put the titles into an array. $form['files']['title']['#tree'] = TRUE; $form['files']['body']['#tree'] = TRUE; // Store a copy of the list into a form value so we can compare it to what // they submit and not have to worry about files being added or removed from // the filesystem. $form['file_list'] = array('#type' => 'value', '#value' => $filelist, ); $form['import_file'] = array('#type' => 'checkboxes', '#options' => $filelist, ); $form['buttons']['submit'] = array( '#type' => 'submit', '#value' => t('Import'), ); $targetsetting = variable_get('imagefield_import_fieldname', FALSE); list($type, $field) = split(":::", $targetsetting); $form['conf_message'] = array('#type' => 'item', '#value' => t("Will import images into content type '$type', field '$field'. To configure this visit the !settings page.", array('!settings' => l("Imagefield import settings", "admin/settings/imagefield_import"))), ); } else { $form['import_file'] = array( '#type' => 'item', '#value' => t('No files were found'), ); } return $form; } //Theme the import form function theme_imagefield_import_form($form) { $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) { $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]), ); } $output .= theme('table', $header, $rows); } return $output . drupal_render($form); } function imagefield_import_form_submit($form, &$form_state) { $op = isset($form_state['values']['op']) ? $form_state['values']['op']: ''; if ($op == t('Import')) { $source_path = variable_get('imagefield_import_source_path', ''); 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); // 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); foreach (array_filter($form_state['values']['import_file']) as $index) { // Check the file is OK $filename = $form_state['values']['file_list'][$index]; $source_filepath = file_check_location($source_path .'/'. $filename, $source_path); if ($source_filepath and file_validate_is_image($source_filepath)) { $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]; $taxonomy = $form_state['values']['taxonomy']; if(_imagefield_import_process_form($file, $node_type, $title, $caption, $taxonomy)) file_delete($source_filepath); // delete original if it all went well. } else drupal_set_message('Problem with the file location:'.$source_filepath, 'error'); } // foreach( file ) } else drupal_set_message('Source directory has not been set in the settings?', 'error'); } } /** * @see: http://drupal.org/node/292904 */ function _imagefield_import_process_form($file_list, $node_type, $title, $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) { // Load the field and figure out the specific details. $field = content_fields($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)) return FALSE; $files[$field_name] = $file; } // Create the node object. return _imagefield_import_create_node($node_type, $files, $title, $caption, $taxonomy); } /** * @see: http://drupal.org/node/292904 */ function _imagefield_import_create_node($node_type, $files, $title, $caption, $taxonomy) { // 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_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); } else { $node->$field_name = array(0 => $file); } } $node = node_submit($node); node_save($node); return $node; } /** * Determine the widget's files directory * * @param $field CCK field * @return files directory path. * @see: http://drupal.org/node/292904 */ function _imagefield_import_widget_files_directory($field) { $widget_file_path = $field['widget']['file_path']; if (module_exists('token')) { global $user; $widget_file_path = token_replace($widget_file_path, 'user', $user); } return file_directory_path() .'/'. $widget_file_path; } /** * Create the imagefield_import_admin_settings that was called in callback arguments in hook_menu(). * * @return unknown */ function imagefield_import_admin_settings() { $form['file_paths'] = array( '#title' => t('Image file paths'), '#type' => 'fieldset', '#description' => t('

Drupal will need to have write access to these directories so we can move and remove the files.

') . t("

Note: a path begining with a / indicates the path is relative to the server's root, not the website's root.") . t("One starting without a / specifies a path relative to Drupal's root.") . t("For example: /tmp/image would be the temp directory off the root of the server while tmp/image would be a path relative to the Drupal's directory."), ); $form['file_paths']['imagefield_import_source_path'] = array( '#type' => 'textfield', '#title' => t('Source path'), '#default_value' => variable_get('imagefield_import_source_path', file_directory_path() .'/images/import'), '#after_build' => array('_imagefield_import_settings_check_directory'), '#description' => t('Where to find the images to import.'), '#required' => TRUE, ); //examine the node types they have available and find ones that have an imagefield associated with them $types = content_types(); $matches = array(); $keys = array(); foreach ($types as $key => $type) { foreach ($type['fields'] as $field) { if ($field['type']="image") { $matches["$key:::$field[field_name]"] = $type['name'] .": ". $field['widget']['label']; $keys[] = "$key:::$field[field_name]"; } } } if (count($matches) == 0 ) { drupal_set_message("In order to import images into an imagefield, you must first have a node type that has an imagefield associated with it. "."Configure this on the ". l('Content Types', 'admin/content/types') .' Page', 'error'); } else { /* * see if there is no imagefield_import_fieldname variable set yet, * if there is not one, lets set it to the first field we found, just * in case the user does not hit the "Save configuration" button on this page */ if (!variable_get('imagefield_import_fieldname', TRUE) ) variable_set('imagefield_import_fieldname', $keys[0]); $form['imagefield_import_fieldname'] = array( '#type' => 'select', '#title' => 'Target field', '#description' => t('Select the imagefield you want to batch import photos into. When you do the import, nodes of your selected type will be auto created and the selected field will be populated with the imported image.'), '#options' => $matches, '#default_value' => variable_get('imagefield_import_fieldname', $keys[0]), '#required' => TRUE ); } return system_settings_form($form); } /** * Checks the existence of the directory specified in $form_element. * * @param $form_element * The form element containing the name of the directory to check. * @see system_check_directory() */ function _imagefield_import_settings_check_directory($form_element) { $import_dir = $form_element['#value']; file_check_directory($import_dir, 0, $form_element['#parents'][0]); $imagefield_dir = file_create_path(variable_get('imagefield_default_path', 'images')); if (realpath($import_dir) == realpath($imagefield_dir)) { form_set_error($form_element['#parents'][0], t("You can't import from the image module's directory. The import deletes the original files so you would just be asking for trouble.")); } return $form_element; } /** * Function Jacked Straight from the image.module * also allows other modules to use to create image nodes. * * @param $filepath * String filepath of an image file. Note that this file will be moved into * the image module's images directory. * @param $title * String to be used as the node's title. If this is ommitted the filename * will be used. * @param $body * String to be used as the node's body. * @param $taxonomy * Taxonomy terms to assign to the node if the taxonomy.module is installed. * @return * A node object if the node is created successfully or FALSE on error. */ function imagefield_create_node_from($filepath, $title = NULL, $body = '', $taxonomy = NULL) { global $user; if (!user_access('import images')) { drupal_access_denied(); } // Ensure it's a valid image. if (!$image_info = image_get_info($filepath)) { return FALE; } // Make sure we can copy the file into our temp directory. $original_path = $filepath; if (!file_copy($filepath, _image_filename($filepath, IMAGE_ORIGINAL, TRUE))) { return FALSE; } // Build the node. // determine the node type, and the target fieldname to import into $targetsetting = variable_get('imagefield_import_fieldname', FALSE); // we are assuming that $targetsetting is a string here, and not FALSE because this condition was checked in the function that calls us list($type, $field) = split(":::", $targetsetting); $node = new stdClass(); $node->type = $type; $node->uid = $user->uid; $node->name = $user->name; $node->title = isset($title) ? $title : basename($filepath); $node->body = $body; // Set the node's defaults... (copied this from node and comment.module) $node_options = variable_get('node_options_'. $node->type, array('status', 'promote')); $node->status = in_array('status', $node_options); $node->promote = in_array('promote', $node_options); if (module_exists('comment')) { $node->comment = variable_get("comment_{$node->type}", COMMENT_NODE_READ_WRITE); } if (module_exists('taxonomy')) { $node->taxonomy = $taxonomy; } $node->new_file = TRUE; $node->$field = array( array( 'fid' => 'upload', 'title' => basename($filepath), 'filename' => basename($filepath), 'filepath' => $filepath, 'filesize' => filesize($filepath), ), ); // Save the node. $node = node_submit($node); node_save($node); // Remove the original image now that the import has completed. file_delete($original_path); return $node; } /** * Creates an image filename. */ function _image_filename($filename, $label = IMAGE_ORIGINAL, $temp = FALSE) { $path = variable_get('image_default_path', 'images') .'/'; if ($temp) { $path .= 'temp/'; } $filename = basename($filename); // Insert the resized name in non-original images if ($label && ($label != IMAGE_ORIGINAL)) { $pos = strrpos($filename, '.'); if ($pos === FALSE) { // The file had no extension - which happens in really old image.module // versions, so figure out the extension. $image_info = image_get_info(file_create_path($path . $filename)); $filename = $filename .'.'. $label .'.'. $image_info['extension']; } else { $filename = substr($filename, 0, $pos) .'.'. $label . substr($filename, $pos); } } return file_create_path($path . $filename); } /** * Implementation of hook_theme(). */ function imagefield_import_theme() { return array( 'imagefield_import_form' => array( 'arguments' => array('form' => NULL), ), ); }