View source
<?php
function flashnode_import($form_state) {
if ($form_state['submitted'] && array_filter($form_state['values']['files'])) {
return flashnode_import_confirm($form_state, array_filter($form_state['values']['files']));
}
$form['flashnode_import'] = flashnode_import_form();
return $form;
}
function flashnode_import_form() {
$form['help'] = array(
'#value' => t('
<p>This feature can be used to import files directly and create Flash nodes from them. This can be useful for importing batches of files that have been uploaded to the server, or to import files that are too large to be uploaded via the node creation form. Note that files that are imported do not respect file size limitations that would apply to files uploaded via the node form. Nodes that are created by this import function will @published.</p>
<p>The import function will scan the %directory directory and sub-directories to locate files for import.</p>', array(
'%directory' => base_path() . file_create_path(variable_get('flashnode_default_path', FLASHNODE_DEFAULT_PATH)),
'@published' => variable_get('flashnode_default_import_status', FLASHNODE_DEFAULT_IMPORT_STATUS) ? t('be published') : t('not be published'),
)),
);
$filesnotindb = _flashnode_filesnotindb();
if ($filesnotindb) {
$form['count'] = array(
'#value' => format_plural(count($filesnotindb), '1 file found.', '@count files found.') . t(' Select the file(s) you want to import, then click \'Import checked files\'.'),
);
}
else {
$form['count'] = array(
'#value' => t('No files were found for import.'),
);
}
$files = array();
foreach ($filesnotindb as $file) {
$files[$file] = '';
$form['file'][$file] = array(
'#value' => l(str_replace(file_create_path(variable_get('flashnode_default_path', FLASHNODE_DEFAULT_PATH)) . '/', '', $file), $GLOBALS['base_url'] . '/' . $file),
);
}
$form['files'] = array(
'#type' => 'checkboxes',
'#options' => $files,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Import checked files'),
);
$form['#theme'] = 'flashnode_import_form';
return $form;
}
function _flashnode_filesnotindb() {
$filesnotindb = array();
$result = db_query('SELECT filepath FROM {files} ORDER BY filepath ASC');
$filesindb = array();
while ($file = db_fetch_object($result)) {
$filesindb[] = file_create_path($file->filepath);
}
$filesonserver = _flashnode_directorytoarray(realpath(file_create_path(variable_get('flashnode_default_path', FLASHNODE_DEFAULT_PATH))), TRUE);
asort($filesonserver);
$root = realpath('.');
foreach ($filesonserver as $file) {
$file = preg_replace('@' . preg_quote($root) . '.@', '', $file);
$file = str_replace("\\", "/", $file);
if (!file_check_directory($file)) {
if (!in_array($file, $filesindb)) {
$filesnotindb[] = $file;
}
}
}
return $filesnotindb;
}
function _flashnode_directorytoarray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory . "/" . $file)) {
if ($recursive) {
$array_items = array_merge($array_items, _flashnode_directorytoarray($directory . "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\\/\\//si", "/", $file);
}
else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\\/\\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
function theme_flashnode_import_form($form) {
$output .= drupal_render($form['help']);
$output .= drupal_render($form['count']);
if (isset($form['file']) && is_array($form['file'])) {
$header = array(
theme('table_select_header_cell'),
t('File'),
);
foreach (element_children($form['file']) as $key) {
$row = array();
$row[] = drupal_render($form['files'][$key]);
$row[] = drupal_render($form['file'][$key]);
$rows[] = $row;
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($form['submit']);
}
return $output;
}
function flashnode_import_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}
function flashnode_import_confirm(&$form_state, $files = array()) {
$form['files'] = array(
'#prefix' => '<ul>',
'#suffix' => '</ul>',
'#tree' => TRUE,
);
foreach ($files as $file) {
$form['files'][$file] = array(
'#type' => 'hidden',
'#value' => $file,
'#prefix' => '<li>',
'#suffix' => check_plain(str_replace(file_create_path(variable_get('flashnode_default_path', FLASHNODE_DEFAULT_PATH)) . '/', '', $file)) . "</li>\n",
);
}
$form['operation'] = array(
'#type' => 'hidden',
'#value' => 'import',
);
$form['#submit'][] = 'flashnode_import_confirm_submit';
return confirm_form($form, t('Are you sure you want to import these items?'), 'admin/content/flashnode', '', t('Import'), t('Cancel'));
}
function flashnode_import_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
foreach ($form_state['values']['files'] as $file) {
flashnode_import_file($file);
}
}
$form_state['redirect'] = 'admin/content/node';
return;
}
function flashnode_import_file($file_to_import) {
global $user;
$node = new stdClass();
$file = new stdClass();
$node->title = check_plain(basename($file_to_import));
$node->uid = $user->uid;
$node->type = 'flashnode';
$node_options = variable_get('node_options_flashnode', array(
'status',
'promote',
));
foreach (array(
'promote',
'sticky',
'revision',
) as $key) {
$node->{$key} = in_array($key, $node_options);
}
$node->comment = variable_get('comment_flashnode', 2);
$node->status = variable_get('flashnode_default_import_status', FLASHNODE_DEFAULT_IMPORT_STATUS);
$info = image_get_info(realpath($file_to_import));
$node->flashnode['height'] = $info['height'];
$node->flashnode['width'] = $info['width'];
$file->filemime = $info['mime_type'];
$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());
$node->flashnode['import'] = TRUE;
$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 (!$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';
}
}
}
drupal_write_record('files', $file);
$node->flashnode['fid'] = db_last_insert_id('files', 'fid');
node_save($node);
$file_to_import = str_replace(file_create_path(variable_get('flashnode_default_path', FLASHNODE_DEFAULT_PATH)) . '/', '', $file_to_import);
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;
}