You are here

function flashnode_import_file in Flash Node 5.6

Same name and namespace in other branches
  1. 5.3 flashnode.module \flashnode_import_file()
  2. 6.3 flashnode.import.inc \flashnode_import_file()
  3. 6.2 flashnode.import.inc \flashnode_import_file()

Import function called by flashnode_import_confirm_submit It is called once for each file that is to be imported $file_to_import is a Drupal path to a file for import

1 call to flashnode_import_file()
flashnode_import_confirm_submit in ./flashnode.module
Submit handler for import confirmation This function does the actual import of each selected file

File

./flashnode.module, line 1373

Code

function flashnode_import_file($file_to_import) {

  // Need to access the user object
  global $user;

  // Initialise a node and file object
  $node = new stdClass();
  $file = new stdClass();

  // Populate basic data in to the node
  $node->title = check_plain(basename($file_to_import));
  $node->uid = $user->uid;
  $node->type = 'flashnode';
  $node->status = 0;

  // Try to get the file settings for this file, using image_get_info
  $info = image_get_info(realpath($file_to_import));
  $node->flashnode['height'] = $info['height'];
  $node->flashnode['width'] = $info['width'];
  $file->filemime = $info['mime_type'];

  // Set other flash node defaults
  $node->flashnode['display'] = variable_get('flashnode_default_display', 0);
  $node->flashnode['substitution'] = '!default';
  $node->flashnode['base'] = variable_get('flashnode_default_base', base_path() . file_directory_path());

  // Set a flag to tell flashnode_insert we are adding files via import
  $node->flashnode['import'] = TRUE;

  // Prepare the file object
  $file->filename = basename($file_to_import);
  $file->filepath = str_replace(file_create_path() . '/', '', $file_to_import);
  $file->filesize = filesize(realpath($file_to_import));

  // If we didn't get mime type from earlier attempt we will need to try and guess it from the extension
  if (!$file->filemime) {
    if (preg_match('@swf|flv|mp3$@i', $file->filename, $matches)) {
      switch (strtolower($matches[0])) {
        case 'mp3':
          $file->filemime = 'audio/mpeg';
          break;
        case 'flv':
        default:
          $file->filemime = 'application/octet-stream';
      }
    }
  }

  // Save the node
  node_save($node);

  // Add file to the files table
  $fid = db_next_id('{files}_fid');
  db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')", $fid, $node->nid, '_flashnode', $file->filepath, $file->filemime, $file->filesize);

  // Insert data in the flash table now we have the file number and the nid
  db_query("INSERT INTO {flashnode} (nid, vid, height, width, display, substitution, flashvars, base, fid) VALUES (%d, %d, %d, %d, %d, '%s', '%s', '%s', %d)", $node->nid, $node->vid, $node->flashnode['height'], $node->flashnode['width'], $node->flashnode['display'], $node->flashnode['substitution'], $node->flashnode['flashvars'], $node->flashnode['base'], $fid);

  // Show a status message
  // If successful, show message and write an entry to the watchdog
  // If unsuccessful only show on the screen
  // Reduce $file_to_import to simple name
  $file_to_import = str_replace(file_create_path(variable_get('flashnode_default_path', 'flash')) . '/', '', $file_to_import);

  // Show appropriate message - $node->nid only exists if node_save succeeded
  if ($node->nid) {
    drupal_set_message('Imported ' . $file_to_import);
    watchdog('content', t('@type: imported %title.', array(
      '@type' => t($node->type),
      '%title' => $node->title,
    )), WATCHDOG_NOTICE, l(t('view'), "node/{$node->nid}"));
  }
  else {
    drupal_set_message('Failed to import ' . $file_to_import, 'warning');
  }
  return;
}