[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/audio/contrib/import/ -> audio_import.pages.inc (source)

   1  <?php
   2  
   3  // $Id: audio_import.pages.inc,v 1.6 2009/05/14 19:13:23 drewish Exp $
   4  
   5  
   6  function audio_import_validate_file($file) {
   7    $errors = array();
   8    // TODO add in some validation to ensure that we only import audio files...
   9    return $errors;
  10  }
  11  
  12  function audio_import_form() {
  13    $form = array();
  14  
  15    $dirpath = variable_get('audio_import_path', '');
  16    if (!file_check_directory($dirpath)) {
  17      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-page' => url('admin/settings/audio_import'))), 'error');
  18      return $form;
  19    }
  20  
  21    $form['#dirpath'] = $dirpath;
  22    $form['#node_type'] = 'audio';
  23  
  24    // convert the extensions setting into a regex for file scanning
  25    $extensions = variable_get('audio_allowed_extensions', 'mp3 wav ogg');
  26    $extensions = preg_split('/[\s,]+/', $extensions, -1, PREG_SPLIT_NO_EMPTY);
  27    $filemask = '.*(\.'. implode('|\.', $extensions) .')$';
  28  
  29    $files = file_scan_directory($dirpath, $filemask);
  30    ksort($files);
  31  
  32    // When the form gets too large we end up running out of memory submitting it.
  33    // To avoid this we use a pager and rather than setting up all the variables
  34    // ourself we just send in a fake query and then select the desired part of
  35    // the files array.
  36    $page_size = variable_get('audio_import_page_size', 50);
  37    pager_query('SELECT %d', $page_size, 0, 'SELECT %d', array(count($files)));
  38    $files = array_slice($files, $GLOBALS['pager_page_array'][0] * $page_size, $page_size);
  39  
  40    if ($files) {
  41      if (module_exists('taxonomy')) {
  42        // here's a little hack to get the taxonomy controls onto our form
  43        $form['type'] = array('#type' => 'value', '#value' => $form['#node_type']);
  44        $form['#node'] = new stdClass();
  45        $form['#node']->type = $form['#node_type'];
  46        taxonomy_form_alter($form, array(), $form['#node_type'] .'_node_form');
  47        unset($form['type']);
  48        unset($form['#node']);
  49      }
  50  
  51      $form['token_help'] = array(
  52        '#title' => t('Token list'),
  53        '#type' => 'fieldset',
  54        '#collapsible' => TRUE,
  55        '#collapsed' => TRUE,
  56        '#description' => t('This is a list of the tokens that can be used in the title of audio nodes.'),
  57        'help' => array('#value' => theme('token_help', 'node')),
  58      );
  59  
  60      // Put the image files into an array for the checkboxes and gather
  61      // additional information like dimensions and filesizes. Make sure that
  62      // there's no 0th element, because a checkbox with a zero value is seen as
  63      // unchecked and won't be imported.
  64      $index = 0;
  65      foreach ($files as $file) {
  66        $index++;
  67        $filelist[$index] = substr($file->filename, strlen($dirpath) + 1);
  68  
  69        // Spit out the import form elements.
  70        $form['files']['import'][$index] = array(
  71          '#type' => 'checkbox',
  72          '#title' => substr($file->filename, strlen($dirpath) + 1),
  73        );
  74        $form['files']['filesize'][$index] = array(
  75          '#type' => 'item',
  76          '#value' => format_size(filesize($file->filename)),
  77        );
  78        $form['files']['title'][$index] = array(
  79          '#type' => 'textfield',
  80          '#size' => 20,
  81          '#default_value' => variable_get('audio_default_title_format', '[audio-tag-title-raw] by [audio-tag-artist-raw]'), // basename($file->name),
  82        );
  83        $form['files']['body'][$index] = array(
  84          '#type' => 'textfield',
  85          '#size' => 20,
  86        );
  87  
  88        // If there were problems don't let them import it
  89        $problems = audio_import_validate_file($file);
  90        if (count($problems)) {
  91          $form['files']['import'][$index]['#type'] = 'item';
  92          $form['files']['errors'][$index] = array(
  93            '#type' => 'markup',
  94            '#value' => '<em>'. implode(' ', $problems) .'</em>',
  95          );
  96          unset($form['files']['title'][$index]);
  97          unset($form['files']['body'][$index]);
  98        }
  99      }
 100  
 101      $form['pager'] = array('#value' => theme('pager', NULL, $page_size, 0));
 102  
 103      // Put the titles into an array.
 104      $form['files']['import']['#tree'] = TRUE;
 105      $form['files']['title']['#tree'] = TRUE;
 106      $form['files']['body']['#tree'] = TRUE;
 107  
 108      // Store a copy of the list into a form value so we can compare it to what
 109      // they submit and not have to worry about files being added or removed
 110      // from the filesystem.
 111      $form['file_list'] = array(
 112        '#type' => 'value',
 113        '#value' => $filelist,
 114      );
 115  
 116      $form['buttons']['submit'] = array(
 117        '#type' => 'submit',
 118        '#value' => t('Import'),
 119      );
 120    }
 121    else {
 122      $form['none_found'] = array(
 123        '#type' => 'item',
 124        '#value' => t('<em>No files were found.</em>'),
 125      );
 126    }
 127  
 128    return $form;
 129  }
 130  
 131  function theme_audio_import_form($form) {
 132    $output = drupal_render($form['token_help']);
 133  
 134    if (!empty($form['file_list']['#value'])) {
 135      $type = node_get_types('type', $form['#node_type']);
 136      $header = array(theme('table_select_header_cell'), t('Name'), t('Size'), check_plain($type->title_label), check_plain($type->body_label));
 137      $rows = array();
 138      foreach (element_children($form['files']['import']) as $key) {
 139        $filename = $form['files']['import'][$key]['#title'];
 140        unset($form['files']['import'][$key]['#title']);
 141        $row = array(
 142          array('data' => drupal_render($form['files']['import'][$key])),
 143          array('data' => $filename),
 144          array('data' => drupal_render($form['files']['filesize'][$key])),
 145        );
 146        if (!isset($form['files']['errors'][$key])) {
 147          $row[] = array('data' => drupal_render($form['files']['title'][$key]));
 148          $row[] = array('data' => drupal_render($form['files']['body'][$key]));
 149        }
 150        else {
 151          $row[] = array('colspan' => 2, 'data' => drupal_render($form['files']['errors'][$key]));
 152        }
 153  
 154        $rows[] = $row;
 155      }
 156      $output .= theme('table', $header, $rows);
 157    }
 158    return $output . drupal_render($form);
 159  }
 160  
 161  function audio_import_form_submit($form, &$form_state) {
 162    $batch = array(
 163      'title' => t('Importing audio'),
 164      'progress_message' => 'Importing @current of @total.',
 165      'operations' => array(),
 166      'finished' => '_audio_import_batch_finished',
 167      'file' => drupal_get_path('module', 'audio_import') .'/audio_import.pages.inc',
 168    );
 169  
 170    foreach (array_filter($form_state['values']['import']) as $index => $true) {
 171      $origname = $form_state['values']['file_list'][$index];
 172      if ($filepath = file_check_location($form['#dirpath'] .'/'. $origname, $form['#dirpath'])) {
 173        $args = array(
 174          'node_type' => $form['#node_type'],
 175          'title' => isset($form_state['values']['title'][$index]) ? $form_state['values']['title'][$index] : NULL,
 176          'body' => isset($form_state['values']['body'][$index]) ? $form_state['values']['body'][$index] : NULL,
 177          'taxonomy' => isset($form_state['values']['taxonomy']) ? $form_state['values']['taxonomy'] : array(),
 178          'filepath' => $filepath,
 179          'origname' => $origname,
 180        );
 181        $batch['operations'][] = array('_audio_import_batch_op', array($args));
 182      }
 183    }
 184  
 185    batch_set($batch);
 186  }
 187  
 188  function _audio_import_batch_op($args, &$context) {
 189    // Create the node object.
 190    if ($node = audio_api_insert($args['filepath'], $args['title'], $args['body'], array(), $args['taxonomy'])) {
 191      // Remove the original image now that the import has completed.
 192      file_delete($args['filepath']);
 193  
 194      $context['results']['good'][] = t('Imported %origname as <a href="!node-link">@node-title</a> @status <a href="!edit-link">[edit]</a>', array(
 195        '%origname' => $args['origname'],
 196        '!node-link' => url('node/'. $node->nid),
 197        '@node-title' => $node->title,
 198        '@status' => $node->status ? '' : t('(Unpublished)'),
 199        '!edit-link' => url('node/'. $node->nid .'/edit'),
 200      ));
 201    }
 202    else {
 203      watchdog('audio_import', 'There was an error that prevented %filename from being imported.', array('%filename' => $args['filepath']), WATCHDOG_ERROR);
 204      $context['results']['bad'][] = t('Error importing %filename.', array('%filename' => $args['filepath']));
 205    }
 206  
 207    $context['finished'] = 1;
 208  }
 209  
 210  function _audio_import_batch_finished($success, $results, $operations) {
 211    if (!$success) {
 212      if (count($results['bad'])) {
 213        drupal_set_message(t('There was a problem importing files: !bad-list', array('!bad-list' => theme('item_list', $results['bad']))), 'error');
 214      }
 215      else {
 216        drupal_set_message(t('There was a problem importing the files.'), 'error');
 217      }
 218    }
 219    if (count($results['good'])) {
 220      drupal_set_message(t('Successfully imported: !good-list', array('!good-list' => theme('item_list', $results['good']))));
 221    }
 222    watchdog('audio_import', 'Completed audio import.');
 223  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7