function flashnode_import_file in Flash Node 6.3
Same name and namespace in other branches
- 5.6 flashnode.module \flashnode_import_file()
- 5.3 flashnode.module \flashnode_import_file()
- 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.import.inc - Submit handler for import confirmation This function does the actual import of each selected file
File
- ./
flashnode.import.inc, line 254
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';
// Get default node options for flashnode
$node_options = variable_get('node_options_flashnode', array(
'status',
'promote',
));
// Process the array to add items to node
foreach (array(
'promote',
'sticky',
'revision',
) as $key) {
$node->{$key} = in_array($key, $node_options);
}
// Apply comment setting - use value 2 as default. Could refer to a constant but
// if comment module not enabled then COMMENT_NODE_READ_WRITE is not defined
$node->comment = variable_get('comment_flashnode', 2);
// Assign publish status (over-ride node defaults with import default)
$node->status = variable_get('flashnode_default_import_status', FLASHNODE_DEFAULT_IMPORT_STATUS);
// 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', FLASHNODE_TEASER_AND_BODY);
$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 - currently suppresses file validation
$node->flashnode['import'] = TRUE;
// Prepare the file object
$file->uid = $user->uid;
$file->filename = basename($file_to_import);
$file->filepath = $file_to_import;
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = time();
$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';
}
}
}
// Write file to the database
drupal_write_record('files', $file);
// Add fid to the flashnode object
$node->flashnode['fid'] = db_last_insert_id('files', 'fid');
// Save the node
node_save($node);
// 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', FLASHNODE_DEFAULT_PATH)) . '/', '', $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_args = array(
'@type' => $node->type,
'%title' => $node->title,
);
$node_link = l(t('view'), 'node/' . $node->nid);
watchdog('content', '@type: imported %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
}
else {
drupal_set_message('Failed to import ' . $file_to_import, 'warning');
}
return;
}