t('
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 are set to be unpublished.
The import function will scan the %directory directory and sub-directories to locate files for import.
', array('%directory' => base_path().file_create_path(variable_get('flashnode_default_path', FLASHNODE_DEFAULT_PATH)))), ); // Get the list of files that aren't in the database $filesnotindb = _flashnode_filesnotindb(); // Output count of files not in the database 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.'), ); } // Process each result in turn and build check box list $files=array(); foreach ($filesnotindb as $file) { $files[$file] = ''; // Can't use file_create_url as the links fail if the site uses private transfers so force a public url instead $form['file'][$file] = array('#value' => l(str_replace(file_create_path(variable_get('flashnode_default_path', FLASHNODE_DEFAULT_PATH)).'/', '', $file), $GLOBALS['base_url'] .'/'. $file)); } // Add list of files to checkboxes $form['files'] = array('#type' => 'checkboxes', '#options' => $files); // Submit button to process form $form['submit'] = array( '#type' => 'submit', '#value' => t('Import checked files'), ); // Specify theme for form $form['#theme'] = 'flashnode_import_form'; return $form; } /** * Return an array of files that are not currently in the {files} table */ function _flashnode_filesnotindb() { // Prepare array to hold results $filesnotindb = array(); // Get all the files out the {files} table and store as qualified path $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); } // Get all the files out of the directory structure $filesonserver = _flashnode_directorytoarray(realpath(file_create_path(variable_get('flashnode_default_path', FLASHNODE_DEFAULT_PATH))), TRUE); // Sort the rows to make it easier to compare to file listing in FTP asort($filesonserver); // Get the root path - will need this later $root = realpath('./'); // Process each result in turn foreach ($filesonserver as $file) { // Strip out the root path to leave just a drupal path $file = preg_replace('@'.preg_quote($root).'.@', '', $file); // Correct for Windows using \ $file = str_replace("\\", "/", $file); // Check it isn't a directory - not interested if (!file_check_directory($file)) { // Check to see if file is NOT in the database if (!in_array($file, $filesindb) ) { // If we get here we have a file that isn't in the database $filesnotindb[] = $file; } } } return $filesnotindb; } /** * Helper function - recurse directories and files in to an array * http://snippets.dzone.com/posts/show/155 */ 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; } /** * Theme flashnode_import_form */ function theme_flashnode_import_form($form) { // Render help message $output .= drupal_render($form['help']); // Render count $output .= drupal_render($form['count']); // If there are files found if (isset($form['file']) && is_array($form['file'])) { // Construct table of files $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; } // Render themed table $output .= theme('table', $header, $rows); // Render actions $output .= drupal_render($form['submit']); } // Return output return $output; } /** * Submit handler for flashnode_import_form - simply populates data before redisplaying form */ function flashnode_import_submit($form, &$form_state) { // Force rebuild to populate form data $form_state['rebuild'] = TRUE; } /** * Confirmation form for user to confirm import of selected items */ function flashnode_import_confirm(&$form_state, $files = array()) { $form['files'] = array('#prefix' => '