'bulkupload', 'access' => user_access('upload bulk items'), 'title' => t('Upload bulk images'), 'callback' => 'drupal_get_form', 'callback arguments' => array('_imagefield_bulkupload_form'), 'type' => MENU_NORMAL_ITEM ); return $items; } /** * */ function _imagefield_bulkupload_form($form_values = NULL) { global $user; $tmpdir = $form_values['tmpdir']; if(!$tmpdir) { $tmpdir = file_directory_temp().'/bulkupload_'.$user->uid; } $img_per_pg = $form_values['img_per_pg'] ? $form_values['img_per_pg'] : 10; // Multistep Setup $step = $form_values ? $form_values['step'] + 1 : 1; $form['step'] = array( '#type' => 'hidden', '#value' => $step); $form['#multistep'] = TRUE; $form['#redirect'] = FALSE; $form['tmpdir'] = array( '#type' => 'value', '#value' => $tmpdir); // Step 1 if($step == 1) { $form['#attributes'] = array('enctype' => "multipart/form-data"); $form['file'] = array( '#type' => 'file', '#description' => t('Use an archived file, such as a zip file, or a tar.gz file.')); $type_results = db_query('SELECT nfi.type_name, nt.name title, nfi.field_name, nfi.widget_settings FROM {node_field_instance} nfi INNER JOIN {node_type} nt ON nfi.type_name=nt.type WHERE nfi.widget_type="image"'); if(db_num_rows($type_results) == 0) { return array('#value' => t('There are no content types in use with a image field type. Please contact the administrator.')); } else if(db_num_rows($type_results) == 1) { $type = db_fetch_array($type_results); $form['nodetype_title'] = array( '#type' => 'item', '#title' => t('Node Type'), '#value' => $type['title']); $form['type_name'] = array( '#type' => 'hidden', '#value' => $type['type_name']); //i.e. image2 $form['field_name'] = array( '#type' => 'hidden', '#value' => $type['field_name']); //i.e. field_image } else { while($type = db_fetch_array($type_results)) { $type_options[$type['type_name']] = $type['title']; } $form['type_name'] = array( '#type' => 'select', '#title' => 'Node Type', '#options' => $type_options); } $settings = unserialize($type['widget_settings']); $form['max_resolution'] = array( '#type' => 'textfield', '#title' => t('Maximum resolution for Images'), '#default_value' => $settings['max_resolution'] ? $settings['max_resolution'] : 0, '#size' => 15, '#maxlength' => 10, '#description' => t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')); $form['img_per_pg'] = array( '#type' => 'select', '#title' => t('Images per Page'), '#default_value' => $img_per_pg, '#options' => array(5=>5, 10=>10, 15=>15, 20=>20, 30=>30), '#description' => t('This website has a limit of %timeout seconds before the ' .'webpage will stop loading, so there is a limitation to the number of images' .' that can be processed at a time.', array('%timeout'=>ini_get('max_execution_time')))); $form['submit'] = array( '#type' => 'submit', '#value' => t('Upload')); } else { // STEP 2 and up $form['field_name'] = array( '#type' => 'hidden', '#value' => $form_values['field_name']); $form['type_name'] = array( '#type' => 'hidden', '#value' => $form_values['type_name']); $form['max_resolution'] = array( '#type' => 'hidden', '#value' => $form_values['max_resolution']); if($handle = opendir($tmpdir)) { $form['img_per_pg'] = array( '#type' => 'select', '#title' => t('Images per Page'), '#default_value' => $img_per_pg, '#options' => array(5=>5, 10=>10, 15=>15, 20=>20, 30=>30), '#description' => t('This website has a limit of %timeout seconds before the ' .'webpage will stop loading, so there is a limitation to the number of images' .' that can be processed at a time.', array('%timeout'=>ini_get('max_execution_time')))); $form['images'] = array( '#type' => 'fieldset', '#tree' => TRUE, '#title' => t('Images')); // Traverse the image directory for titling the nodes $cnt = 0; while(++$cnt <= $img_per_pg) { if(($file = readdir($handle)) === false) break; if(!image_get_info($tmpdir.'/'.$file)) { --$cnt; continue; } // Not sure if image_scale is ideal, because it's time consuming. // 'tmp' directory is not always accessible by http, so the files // should be moved to the files directory. $thmbdir = $tmpdir.'/thumb'; file_check_directory($thmbdir,FILE_CREATE_DIRECTORY+FILE_MODIFY_PERMISSIONS); $thmbdir .= '/'; if(!image_scale($tmpdir.'/'.$file,$thmbdir.$file,140,275)) { $thumb = $tmpdir.'/'.$file; file_copy($thumb, $thmbdir.$file,FILE_EXISTS_REPLACE); } $form['images'][$file] = array( '#type' => 'textfield', '#title' => "Image Title", '#default_value' => $file, '#size' => 40, '#maxlength' => 255, '#description' => t('The title of the node created for this image.'), '#prefix' => '
', '#suffix' => '
'); } if(false === ($file = readdir($handle))) { $form['done'] = array( '#type' => 'hidden', '#value' => 1); } closedir($handle); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save and Continue')); } else { drupal_set_message('The directory could not be opened for reading.'); } } return $form; } function _imagefield_bulkupload_form_validate($form_id, $form_values) { if($form_values['step'] == 1 && !file_check_upload('file')) { form_set_error('', 'A valid file must be uploaded'); } } function _imagefield_bulkupload_form_submit($form_id, $form_values) { global $user; if($form_values['step'] == 1) { if($file = file_check_upload('file')) { _imagefield_bulkupload_unpack($file,$form_values['tmpdir']); } } if($form_values['step'] >= 2) { imagefield_clear_session(); // Save each image with the label given, and delete the tmp // images until no images are left. foreach($form_values['images'] as $filename => $title) { // Submit each image as a node $filetmppath = $form_values['tmpdir'].'/'.$filename; // Save the file $file['filename'] = $filename; $file['filepath'] = $filetmppath; $file['filemime'] = @mime_content_type($filetmppath); $file['filesize'] = @filesize($filetmppath); if($file = file_save_upload((object)$file,file_create_path('images/').$filename)) { $file = (array)$file; _imagefield_scale_image($file,$form_values['max_resolution']); $file['fid'] = db_next_id('{files}_fid'); unset($node); unset($values); $node['type'] = $form_values['type_name']; $values['title'] = $title; $values['body'] = ''; $values['name'] = $user->name; // Insert node w/o image drupal_execute($form_values['type_name'].'_node_form', $values, $node); // Add image by SQL $node = db_fetch_array(db_query("SELECT nid,vid FROM {node} ORDER BY nid DESC LIMIT 1")); $nid = $node['nid']; $vid = $node['vid']; db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s','%s','%s',%d)", $file['fid'], $nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']); $table = 'node_'.$form_values['type_name']; db_query('INSERT INTO {%s} (vid,nid,%s_fid,%s_title,%s_alt) VALUES (%d, %d, %d, "%s","%s")', $table,$form_values['field_name'],$form_values['field_name'],$form_values['field_name'], $vid, $nid, $file['fid'], $title, $title); } unlink($form_values['tmpdir'].'/thumb/'.$file['filename']); } } if($form_values['done']) { @rmdir($form_values['tmpdir']."/thumb"); @rmdir($form_values['tmpdir']); drupal_set_message(t('The images have been successfully added. Please check the nodes to verify the images were all attached to the nodes.')); drupal_goto("bulkupload"); } } function _imagefield_bulkupload_unpack(&$file,$tmpdir) { // Commands taken from http://cvs.drupal.org/viewcvs/drupal/contributions/modules/acidfree/acidfree.module?rev=1.111&view=markup file_check_directory($tmpdir,FILE_CREATE_DIRECTORY+FILE_MODIFY_PERMISSIONS); if(substr($file->filename,-4) == '.zip') { $e = "unzip -qq -j {$file->filepath} -d $tmpdir"; } else if(substr($file->filename,-7) == '.tar.gz') { $e = "tar -C $tmpdir -xzf {$file->filepath}"; } else { drupal_set_message("Filename must end with .zip or .tar.gz"); } if($e) { $message = system($e); if($message) drupal_set_message('Extracting of the images failed: '.$message); } unlink($file->filepath); }