'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) { //Todo: tmpdir should be random, to avoid conflicts of multiple bulk uploads at once $tmpdir = $form_values['tmpdir'] ? $form_values['tmpdir'] : file_directory_temp().'/bulkupload'; $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 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['nodetype'] = array( '#type' => 'hidden', '#value' => $type['type_name']); $form['upload_field'] = array( '#type' => 'hidden', '#value' => $type['field_name']); } else { while($type = db_fetch_array($type_results)) { $type_options[$type['type_name']] = $type['title']; } $form['nodetype'] = array( '#type' => 'select', '#title' => 'Node Type', '#options' => $type_options); } $form['done'] = array( '#type' => 'checkbox', '#title' => t('Don\'t Edit Titles'), '#description' => t('Use filename as title for each file.')); $form['submit'] = array( '#type' => 'submit', '#value' => t('Upload')); } else { // STEP 2 and up 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(false !== ($file = readdir($handle)) && ++$cnt <= $img_per_pg) { if(image_get_info($tmpdir.'/'.$file) && strpos($file,'sm_') !== 0) { // 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. // Using 'sm_' to label thumbnails, but it might need to be changed // to a seperate directory to avoid naming collisions. if(!image_scale($tmpdir.'/'.$file,$tmpdir.'/sm_'.$file,140,275)) { $thumb = $tmpdir.'/'.$file; file_copy($thumb, $tmpdir.'/sm_'.$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['nodetype'] = array( '#type' => 'hidden', '#value' => $form_values['nodetype']); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save and Continue')); $form['upload_field'] = array( '#type' => 'hidden', '#value' => $form_values['upload_field']); } 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) { if($form_values['step'] == 1) { if($file = file_check_upload('file')) { _imagefield_bulkupload_unpack($file,$form_values['tmpdir']); } } if($form_values['step'] >= 2 || $form_values['done']) { // Save each image with the label given, and delete the tmp // images until no images are left. foreach($form_values['images'] as $file => $title) { // Submit each image as a node $filepath = $form_values['tmpdir'].'/'.$file; $node = array('type' => $form_values['nodetype']); $values['title'] = $title; $values['body'] = ''; $values['name'] = $user->name; $values[$form_values['upload_field']][0] = array( 'filename' => $file, 'filepath' => $filepath, 'filemime' => @mime_content_type($filepath), //'filesize' => @filesize($filepath), //'title' => $file, //'alt' => $file ); drupal_execute($form_values['nodetype'].'_node_form', $values, $node); //unlink($form_values['tmpdir'].'/'.$file); //unlink($form_values['tmpdir'].'/sm_'.$file); } } } 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); }