'. 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.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), ), ); }