- <?php
-
- * Implementation of hook_help().
- */
- function file_import_help($section = '') {
- switch ($section) {
- case 'admin/content/file_import':
- $output = '<p>'. t("Import multiple files and save them as node attachments. The files will be moved from their default location."). '<br/>'
- . t("Searching for files in %dirpath.", array('%dirpath' => realpath(variable_get('file_import_path', '')))) .'</p>';
- return $output;
- case 'node/'.arg(1).'/file_import':
- $output = '<p>'. t("Import multiple files and save them as node attachments. The files will be moved from their default location."). '<br/>'
- . t("Searching for files in %dirpath.", array('%dirpath' => realpath(variable_get('file_import_path', '')))) .'</p>';
- return $output;
- case 'admin/settings/file_import':
- return t("Configure the files import module's settings.");
- default:
- return null;
- }
- }
-
- * Implementation of hook_perm().
- */
- function file_import_perm() {
- return array('import files');
- }
-
- * Implementation of hook_menu().
- */
- function file_import_menu($may_cache) {
- $items = array();
- if ($may_cache) {
- $items[] = array(
- 'path' => 'admin/content/file_import',
- 'title' => t('File import'),
- 'callback' => 'drupal_get_form',
- 'callback arguments' => array('file_import_form'),
- 'access' => user_access('import files'),
- 'type' => MENU_NORMAL_ITEM,
- 'description' => t('Import files from the filesystem.')
- );
- $items[] = array(
- 'path' => 'admin/settings/file_import',
- 'title' => t('File import'),
- 'callback' => 'drupal_get_form',
- 'callback arguments' => array('file_import_admin_settings'),
- 'access' => user_access('administer site configuration'),
- 'type' => MENU_NORMAL_ITEM,
- 'description' => t('Change settings for the File Import module.')
- );
- $items['file_import/js'] = array(
- 'page callback' => 'file_import_js',
- 'access arguments' => array('import files'),
- 'type' => MENU_CALLBACK,
- );
- }
- else
- {
- if ((variable_get('file_import_tab', FALSE))&&(arg(0) == 'node' && is_numeric(arg(1)))) {
- $node = node_load(arg(1));
- if ((file_import_access_for_node($node))&&($node->nid)) {
- $items[] = array(
- 'path' => 'node/'. arg(1). '/file_import',
- 'title' => t('File import'),
- 'callback' => 'drupal_get_form',
- 'callback arguments' => array('file_import_form'),
- 'access' => user_access('import files'),
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 5);
- }
- }
- }
-
- return $items;
- }
-
-
- function file_import_form() {
- $form = array();
-
- $dirpath = variable_get('file_import_output_path', '');
- if (!file_check_directory($dirpath)) {
- drupal_set_message(t("You need to configure the output directory on the files import module's <a href='!admin-settings-file_import'>settings page</a>.", array('!admin-settings-file_import' => url('admin/settings/file_import'))), 'error');
- return $form;
- }
- $dirpath = variable_get('file_import_path', '');
- if (!file_check_directory($dirpath)) {
- drupal_set_message(t("You need to configure the import directory on the files import module's <a href='!admin-settings-file_import'>settings page</a>.", array('!admin-settings-file_import' => url('admin/settings/file_import'))), 'error');
- return $form;
- }
-
-
- $files = file_scan_directory($dirpath, '.*');
- ksort($files);
-
- if ($files) {
-
-
-
-
-
- $fields = array('filesize', 'dimensions', 'title', 'node');
- foreach ($fields as $field) {
- $form['files'][$field][0] = NULL;
- }
- $filelist = array(0 => NULL);
-
- if ((arg(0)=='node')&&($nid = (arg(1)))){
- $only_with_uploads = variable_get("only_with_uploads",TRUE);
-
-
- if ((is_numeric($nid))&&($n = node_load($nid))&&((!$only_with_uploads) || ($only_with_uploads && variable_get('upload_'. $n->type, 1))))
- $default_nid = $n->nid;
- else
- $default_nid = '';
- }
- foreach ($files as $file) {
- $filelist[] = substr($file->filename, strlen($dirpath) + 1);
- $form['files']['filesize'][] = array(
- '#type' => 'item',
- '#value' => format_size(filesize($file->filename)),
- );
- $form['files']['title'][] = array(
- '#type' => 'textfield',
- '#size' => 20,
- '#default_value' => basename($file->name),
- );
- $form['files']['node'][] = array(
- '#type' => 'textfield',
- '#size' => 20,
- '#default_value' => $default_nid,
- );
- }
-
- unset($filelist[0]);
- foreach ($fields as $field) {
- $form['files'][$field][0] = NULL;
- }
-
-
- $form['files']['title']['#tree'] = TRUE;
- $form['files']['node']['#tree'] = TRUE;
-
-
-
-
- $form['file_list'] = array(
- '#type' => 'value',
- '#value' => $filelist,
- );
- $form['import_file'] = array(
- '#type' => 'checkboxes',
- '#options' => $filelist,
- );
-
-
-
-
- $types = node_get_types();
- $only_with_uploads = variable_get("only_with_uploads",TRUE);
- $allowed_types = array();
- foreach ($types as $type){
- if ((!$only_with_uploads) || ($only_with_uploads && variable_get('upload_'. $type, 1))) {
- $allowed_types[] = "'".$type->type."'";
- }
- }
- if ($allowed_types) {
- $result = db_query("SELECT nid, title FROM {node} where type in (".implode(',',$allowed_types).") ORDER BY created DESC");
- $nids = array();
- while ($node = db_fetch_array($result)) {
- $nids[$node['nid']] = $node['nid'].' - '.$node['title'];
- }
- }
-
-
- $form['node_select'] = array(
- '#type' => 'fieldset',
- '#title' => t('Target node'),
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- '#weight' => -4,
- '#attributes' => array('id' => 'target_node'),
- '#description' => t('You can set target node to a bunch of selected files at once.').'<br/>'.t(' Select node from list below and press button to set the values.')
- );
- if ($default_nid) $form['node_select']['#collapsed'] = TRUE;
-
- $form['node_select']['combobox'] = array(
- '#type' => 'select',
- '#options' => $nids,
- '#default_value' => $default_nid,
- '#weight' => -4,
- '#id' => 'node_select_combo',
- '#description' => t('ID - Title'),
-
- );
- $form['node_select']['button'] = array(
- '#type' => 'button',
- '#value' => t('Set for selected rows'),
- '#id' => 'node_select_button'
- );
-
- $form['edit_titles'] = array(
- '#type' => 'fieldset',
- '#title' => t('Edit titles'),
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- '#weight' => -4,
- '#attributes' => array('id' => 'edit_titles'),
- );
- $form['edit_titles']['clear_titles'] = array(
- '#type' => 'button',
- '#value' => t('Clear titles'),
- '#id' => 'clear_titles',
- '#suffix' => '<br/><br/>',
- );
- $form['edit_titles']['cfl_titles'] = array(
- '#type' => 'button',
- '#value' => t('Capitalize first letter'),
- '#id' => 'cfl_titles',
- '#suffix' => '<br/>',
- );
- $form['edit_titles']['cfla_titles'] = array(
- '#type' => 'button',
- '#value' => t('Capitalize all words'),
- '#id' => 'cfla_titles',
- '#suffix' => '<br/><br/>',
- );
-
- $form['replace_titles'] = array(
- '#type' => 'fieldset',
- '#title' => t('RegEx replacement in titles'),
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- '#weight' => -4,
- '#attributes' => array('id' => 'replace_titles'),
- );
- $form['replace_titles']['replace_from'] = array(
- '#type' => 'textfield',
- '#size' => 25,
- '#default_value' => '[-_]',
- '#description' => t('Replace from'),
- );
- $form['replace_titles']['replace_to'] = array(
- '#type' => 'textfield',
- '#size' => 25,
- '#default_value' => ' ',
- '#description' => t('Replace to'),
- );
- $form['replace_titles']['replace_titles'] = array(
- '#type' => 'button',
- '#value' => t('Replace in selected'),
- '#id' => 'replace_titles_button',
- );
-
-
- $form['buttons']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Import'),
- );
- }
- else {
- $form['import_file'] = array(
- '#type' => 'item',
- '#value' => t('No files in import directory'),
- );
- }
-
- return $form;
- }
-
- function content_type($name) {
-
- $contenttype = 'application/octet-stream';
- $contenttypes = array( 'html' => 'text/html',
- 'htm' => 'text/html',
- 'txt' => 'text/plain',
- 'gif' => 'image/gif',
- 'jpg' => 'image/jpeg',
- 'png' => 'image/png',
- 'sxw' => 'application/vnd.sun.xml.writer',
- 'sxg' => 'application/vnd.sun.xml.writer.global',
- 'sxd' => 'application/vnd.sun.xml.draw',
- 'sxc' => 'application/vnd.sun.xml.calc',
- 'sxi' => 'application/vnd.sun.xml.impress',
- 'xls' => 'application/vnd.ms-excel',
- 'ppt' => 'application/vnd.ms-powerpoint',
- 'doc' => 'application/msword',
- 'rtf' => 'text/rtf',
- 'zip' => 'application/zip',
- 'mp3' => 'audio/mpeg',
- 'pdf' => 'application/pdf',
- 'tgz' => 'application/x-gzip',
- 'gz' => 'application/x-gzip',
- 'vcf' => 'text/vcf' );
-
- $name = ereg_replace("§", " ", $name);
- foreach ($contenttypes as $type_ext => $type_name) {
- if (preg_match ("/$type_ext$/i", $name)) $contenttype = $type_name;
- }
- return $contenttype;
- }
-
- function theme_file_import_form($form) {
- $output = '';
- if (isset($form['import_file']) && $form['import_file']['#type'] == 'checkboxes') {
- $header = array(theme('table_select_header_cell'), t('Name'), t('Size'), t('Title'), t('Target node ID'));
- $rows = array();
- foreach (element_children($form['import_file']) as $key) {
- $filename = $form['import_file'][$key]['#title'];
- unset($form['import_file'][$key]['#title']);
- $form['import_file'][$key]['#attributes']['checked'] = TRUE;
- $rows[] = array(
- drupal_render($form['import_file'][$key]),
- $filename,
- drupal_render($form['files']['filesize'][$key]),
- drupal_render($form['files']['title'][$key]),
- drupal_render($form['files']['node'][$key]),
- );
- }
- $output .= theme('table', $header, $rows);
- }
-
- drupal_add_js(drupal_get_path('module', 'file_import').'/file_import.js');
- $inline_script = '<script type="text/javascript"> file_import_form_change(); </script>';
- return $output . drupal_render($form). $inline_script;
- }
-
- * Checks if has acces to import files in node
- */
- function file_import_access_for_node($node){
- $only_with_uploads = variable_get("only_with_uploads",TRUE);
- return (!$only_with_uploads) || ($only_with_uploads && variable_get('upload_'. $node->type, 1));
- }
-
-
- function file_import_form_submit($form_id, $form_values) {
- $op = isset($form_values['op']) ? $form_values['op'] : '';
- if ($op == t('Import')) {
- $dirpath = variable_get('file_import_path', '');
- if (file_check_directory($dirpath)) {
- $i = 0;
- $list = variable_get('upload_list_default', TRUE);
-
- $nodes = array();
- foreach (array_filter($form_values['import_file']) as $index) {
-
- $node_index = $form_values['node'][$index];
- if (is_numeric($node_index))
- {
-
-
- if ((!$nodes[$node_index])&&($n = node_load($node_index))&&(file_import_access_for_node($n)))
- $nodes[$node_index] = $n;
-
- if ($nodes[$node_index])
- {
- $nid = $nodes[$node_index]->nid;
- $vid = $nodes[$node_index]->vid;
-
-
- if (!ini_get('safe_mode')) {
- set_time_limit(0);
- }
- $origname = $form_values['file_list'][$index];
- $filename = file_check_location($dirpath .'/'. $origname, $dirpath);
- $new_filename = variable_get('file_import_output_path', file_directory_path()).'/'. $origname;
-
-
- $info = image_get_info($filename);
- if ($info) {
- list($width, $height) = explode('x', variable_get('upload_max_resolution', 0));
- if ($width && $height) {
- $result = image_scale($filename, $filename, $width, $height);
- if ($result) {
- drupal_set_message(t('The image was resized to fit within the maximum allowed resolution of %resolution pixels.', array('%resolution' => variable_get('upload_max_resolution', 0))));
- }
- }
- }
-
- if (file_move($filename, $new_filename, FILE_EXISTS_RENAME))
- {
- $fid = db_next_id('{files}_fid');
- db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)",
- $fid,
- $nid,
- $origname,
- $filename,
- content_type($filename),
- filesize($filename));
-
- db_query("INSERT INTO {file_revisions} (fid, vid, description, list) VALUES (%d, %d, '%s', %d)",
- $fid,
- $vid,
- $form_values['title'][$index],
- $list);
- $i++;
- }
- }
- }
- }
-
- if ($i) {
- drupal_set_message(t('Successfully imported %count', array('%count' => format_plural($i, '1 file', '@count files'))));
- }
- else {
- drupal_set_message(t('No files files were imported.'));
- }
- }
- }
- }
-
- function file_import_admin_settings() {
- $form['file_import_path'] = array(
- '#type' => 'textfield',
- '#title' => t('Import path'),
- '#default_value' => variable_get('file_import_path', file_directory_path() .'/import'),
- '#after_build' => array('_file_import_settings_check_import_directory'),
- '#description' => t("The directory to import files from. Drupal will need to have write access to this directory so we can move the file.") .'<br />'
- . t("<strong>Note:</strong> a path begining with a <kbd>/</kbd> indicates the path is relative to the server's root, not the website's root. One starting without a <kbd>/</kbd> specifies a path relative to Drupal's root. For example: <kbd>/tmp/image</kbd> would be the temp directory off the root while <kbd>tmp/image</kbd> would be inside Drupal's directory."),
- '#required' => TRUE,
- );
-
- $form['file_import_output_path'] = array(
- '#type' => 'textfield',
- '#title' => t('Output path'),
- '#default_value' => variable_get('file_import_output_path', file_directory_path()),
- '#after_build' => array('_file_import_settings_check_output_directory'),
- '#description' => t("The directory to output files to. Drupal will need to have write access to this directory so we can move the file.") .'<br />'
- . t("<strong>Note:</strong> a path begining with a <kbd>/</kbd> indicates the path is relative to the server's root, not the website's root. One starting without a <kbd>/</kbd> specifies a path relative to Drupal's root. For example: <kbd>/tmp/image</kbd> would be the temp directory off the root while <kbd>tmp/image</kbd> would be inside Drupal's directory."),
- '#required' => TRUE,
- );
-
- $form['files_only_with_uploads'] = array(
- '#type' => 'checkbox',
- '#title' => t('Import to nodes which has permission to contain files only'),
- '#after_build' => array('_file_import_settings_only_with_uploads'),
- '#default_value' => variable_get('only_with_uploads', TRUE),
- );
-
- $form['file_import_tab'] = array(
- '#type' => 'checkbox',
- '#title' => t('Display tab on node pages'),
- '#after_build' => array('_file_import_tab'),
- '#default_value' => variable_get('file_import_tab', FALSE),
- );
-
- return system_settings_form($form);
- }
-
- * Checks the existence of the directory specified in $form_element.
- *
- * @param $form_element
- * The form element containing the name of the directory to check.
- * @see system_check_directory()
- */
- function _file_import_settings_check_import_directory($form_element) {
- $import_dir = $form_element['#value'];
- file_check_directory($import_dir, 0, $form_element['#parents'][0]);
- $files_dir = file_create_path(variable_get('files_default_path', 'files'));
- if (realpath($import_dir) == realpath($files_dir)) {
- form_set_error($form_element['#parents'][0], t("You can't import from the files module's directory. The import deletes the original files so you would just be asking for trouble."));
- }
- return $form_element;
- }
-
- function _file_import_settings_check_output_directory($form_element) {
- $output_dir = $form_element['#value'];
- file_check_directory($output_dir, 0, $form_element['#parents'][0]);
- return $form_element;
- }
-
- function _file_import_settings_only_with_uploads($form_element) {
- variable_set('only_with_uploads', $form_element['#value']);
- return $form_element;
- }
-
- function _file_import_tab($form_element) {
- variable_set('file_import_tab', $form_element['#value']);
- return $form_element;
- }
-