Index: contrib/import/audio_import.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/audio/contrib/import/audio_import.pages.inc,v
retrieving revision 1.1
diff -r1.1 audio_import.pages.inc
4a5,11
> 
> function audio_import_validate_file($filepath) {
>   $errors = array();
>   // TODO add in some validation to ensure that we only import audio files...
>   return $errors;
> }
> 
6c13
<   $dirpath = variable_get('audio_import_path', '');
---
>   $form = array();
7a15
>   $dirpath = variable_get('audio_import_path', '');
9,10c17,18
<     drupal_set_message(t("You need to configure the import directory on the audio import module's <a href='!admin-settings-audio_import'>settings page</a>.", array('!admin-settings-audio_import' => url('admin/settings/audio_import'))), 'error');
<     return array();
---
>     drupal_set_message(t("You need to configure the import directory on the audio import module's <a href='!settings-page'>settings page</a>.", array('!settings-pages' => url('admin/settings/audio_import'))), 'error');
>     return $form;
12a21,23
>   $form['#dirpath'] = $dirpath;
>   $form['#node_type'] = 'audio';
> 
18d28
<   $options = array();
21,25d30
<   $i = 1;
<   foreach ($files as $file) {
<     // remove our base path from the filename but keep any sub-directories
<     $options[$i++] = substr($file->filename, strlen($dirpath) + 1);
<   }
27,30c32,101
<   if ($options) {
<     // put a copy of the list into a value so we can compare it to what they
<     // submit and not have to worry about files being added or removed on the
<     // filesystem.
---
> 
>   if ($files) {
>     if (module_exists('taxonomy')) {
>       // here's a little hack to get the taxonomy controls onto our form
>       $form['type'] = array('#type' => 'value', '#value' => $form['#node_type']);
>       $form['#node'] = new stdClass();
>       $form['#node']->type = $form['#node_type'];
>       taxonomy_form_alter($form, array(), $form['#node_type'] .'_node_form');
>       unset($form['type']);
>       unset($form['#node']);
>     }
> 
>     $form['token_help'] = array(
>       '#title' => t('Token list'),
>       '#type' => 'fieldset',
>       '#collapsible' => TRUE,
>       '#collapsed' => TRUE,
>       '#description' => t('This is a list of the tokens that can be used in the title of audio nodes.'),
>       'help' => array('#value' => theme('token_help', '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.
>     $index = 0;
>     foreach ($files as $file) {
>       $index++;
>       $filelist[$index] = substr($file->filename, strlen($dirpath) + 1);
> 
>       // Spit out the import form elements.
>       $form['files']['import'][$index] = array(
>         '#type' => 'checkbox',
>         '#title' => substr($file->filename, strlen($dirpath) + 1),
>       );
>       $form['files']['filesize'][$index] = array(
>         '#type' => 'item',
>         '#value' => format_size(filesize($file->filename)),
>       );
>       $form['files']['title'][$index] = array(
>         '#type' => 'textfield',
>         '#size' => 20,
>         '#default_value' => variable_get('audio_default_title_format', '[audio-tag-title-raw] by [audio-tag-artist-raw]'), // basename($file->name),
>       );
>       $form['files']['body'][$index] = array(
>         '#type' => 'textfield',
>         '#size' => 20,
>       );
> 
>       // If there were problems don't let them import it
>       $problems = audio_import_validate_file($file->filename);
>       if (count($problems)) {
>         $form['files']['import'][$index]['#type'] = 'item';
>         $form['files']['errors'][$index] = array(
>           '#type' => 'markup',
>           '#value' => '<em>'. implode(' ', $problems) .'</em>',
>         );
>         unset($form['files']['title'][$index]);
>         unset($form['files']['caption'][$index]);
>       }
>     }
> 
>     // Put the titles into an array.
>     $form['files']['import']['#tree'] = TRUE;
>     $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.
33,37c104
<       '#value' => $options
<     );
<     $form['audio_import_files'] = array(
<       '#type' => 'checkboxes',
<       '#options' => $options,
---
>       '#value' => $filelist,
38a106
> 
41c109
<       '#value' => t('Import')
---
>       '#value' => t('Import'),
45c113
<     $form['audio_import_files'] = array(
---
>     $form['none_found'] = array(
47c115
<       '#value' => t('No files were found'),
---
>       '#value' => t('<em>No files were found.</em>'),
49a118
> 
55,56c124,126
<   if (isset($form['audio_import_files']) && $form['audio_import_files']['#type'] == 'checkboxes') {
<     $header = array(theme('table_select_header_cell'), t('Filename'));
---
>   if (!empty($form['file_list']['#value'])) {
>     $type = node_get_types('type', $form['#node_type']);
>     $header = array(theme('table_select_header_cell'), t('Name'), t('Size'), check_plain($type->title_label), check_plain($type->body_label));
58,63c128,134
<     foreach (element_children($form['audio_import_files']) as $key) {
<       $filename = $form['audio_import_files'][$key]['#title'];
<       unset($form['audio_import_files'][$key]['#title']);
<       $rows[] = array(
<         drupal_render($form['audio_import_files'][$key]),
<         $filename,
---
>     foreach (element_children($form['files']['import']) as $key) {
>       $filename = $form['files']['import'][$key]['#title'];
>       unset($form['files']['import'][$key]['#title']);
>       $row = array(
>         array('data' => drupal_render($form['files']['import'][$key])),
>         array('data' => $filename),
>         array('data' => drupal_render($form['files']['filesize'][$key])),
64a136,144
>       if (!isset($form['files']['errors'][$key])) {
>         $row[] = array('data' => drupal_render($form['files']['title'][$key]));
>         $row[] = array('data' => drupal_render($form['files']['body'][$key]));
>       }
>       else {
>         $row[] = array('colspan' => 2, 'data' => drupal_render($form['files']['errors'][$key]));
>       }
> 
>       $rows[] = $row;
72,99c152,173
<   $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
<   if ($op == t('Import')) {
<     $dirpath = variable_get('audio_import_path', '');
<     if (file_check_directory($dirpath)) {
<       $nodes = array();
<       $files = array();
<       foreach (array_filter($form_state['values']['audio_import_files']) as $index) {
<         // try to avoid php's script timeout with a bunch of large files or
<         // a slow machine
<         set_time_limit(0);
< 
<         $origname = $form_state['values']['file_list'][$index];
<         $filename = file_check_location($dirpath .'/'. $origname, $dirpath);
<         if ($filename) {
<           $node = audio_api_insert($filename);
<           if ($node) {
<             $nodes[] = t('%filename as <a href="!node-link">@node-title</a> <a href="!edit-link">[edit]</a>', array(
<               '%filename' => $origname,
<               '!node-link' => url('node/'. $node->nid),
<               '@node-title' => $node->title,
<               '!edit-link' => url('node/'. $node->nid .'/edit'),
<             ));
<           }
<           else {
<             drupal_set_message(t('Error importing %filename.', array('%filename' => $filename)));
<           }
<         }
<       }
---
>   $batch = array(
>     'title' => t('Importing audio'),
>     'progress_message' => 'Importing @current of @total.',
>     'operations' => array(),
>     'finished' => '_audio_import_batch_finished',
>     'file' => drupal_get_path('module', 'audio_import') .'/audio_import.pages.inc',
>   );
> 
>   foreach (array_filter($form_state['values']['import']) as $index => $true) {
>     $origname = $form_state['values']['file_list'][$index];
>     if ($filepath = file_check_location($form['#dirpath'] .'/'. $origname, $form['#dirpath'])) {
>       $args = array(
>         'node_type' => $form['#node_type'],
>         'title' => isset($form_state['values']['title'][$index]) ? $form_state['values']['title'][$index] : NULL,
>         'body' => isset($form_state['values']['body'][$index]) ? $form_state['values']['body'][$index] : NULL,
>         'taxonomy' => isset($form_state['values']['taxonomy']) ? $form_state['values']['taxonomy'] : array(),
>         'filepath' => $filepath,
>         'origname' => $origname,
>       );
>       $batch['operations'][] = array('_audio_import_batch_op', array($args));
>     }
>   }
101,107c175,203
<       // report back on our progress
<       if (count($nodes)) {
<         drupal_set_message(t("Successfully imported: ". theme('item_list', $nodes)));
<       }
<       else {
<         drupal_set_message(t('No audio files were imported.'));
<       }
---
>   batch_set($batch);
> }
> 
> function _audio_import_batch_op($args, &$context) {
>   // Create the node object.
>   if ($node = audio_api_insert($args['filepath'], $args['title'], $args['taxonomy'], $args['body'])) {
>     // Remove the original image now that the import has completed.
>     file_delete($args['filepath']);
> 
>     $context['results']['good'][] = t('Imported %origname as <a href="!node-link">@node-title</a> @status <a href="!edit-link">[edit]</a>', array(
>       '%origname' => $args['origname'],
>       '!node-link' => url('node/'. $node->nid),
>       '@node-title' => $node->title,
>       '@status' => $node->status ? '' : t('(Unpublished)'),
>       '!edit-link' => url('node/'. $node->nid .'/edit'),
>     ));
>   }
>   else {
>     watchdog('audio_import', 'There was an error that prevented %filename from being imported.', array('%filename' => $args['filepath']), WATCHDOG_ERROR);
>     $context['results']['bad'][] = t('Error importing %filename.', array('%filename' => $args['filepath']));
>   }
> 
>   $context['finished'] = 1;
> }
> 
> function _audio_import_batch_finished($success, $results, $operations) {
>   if (!$success) {
>     if (count($results['bad'])) {
>       drupal_set_message(t('There was a problem importing files: !bad-list', array('!bad-list' => theme('item_list', $results['bad']))), 'error');
108a205,210
>     else {
>       drupal_set_message(t('There was a problem importing the files.'), 'error');
>     }
>   }
>   if (count($results['good'])) {
>     drupal_set_message(t('Successfully imported: !good-list', array('!good-list' => theme('item_list', $results['good']))));
109a212
>   watchdog('audio_import', 'Completed audio import.');
