t('Attach select'), 'label' => t('File Attach'), 'description' => t('Select a file from a local file location on the server.'), 'process' => 'filefield_source_attach_process', 'value' => 'filefield_source_attach_value', ); return $source; } /** * Implementation of hook_theme(). */ function filefield_source_attach_theme() { return array( 'filefield_source_attach_element' => array( 'arguments' => array('element' => NULL), 'file' => 'sources/attach.inc', ), ); } /** * Implementation of hook_filefield_source_settings(). */ function filefield_source_attach_settings($op, $field) { return array(); } /** * A #process callback to extend the filefield_widget element type. */ function filefield_source_attach_process($element, $edit, &$form_state, $form) { $element['filefield_attach'] = array( '#theme' => 'filefield_source_attach_element', '#weight' => 100.5, '#access' => empty($element['fid']['#value']), '#filefield_sources_hint_text' => FILEFIELD_SOURCE_ATTACH_HINT_TEXT, ); $allowed_extensions = implode('|', $element['#upload_validators']['filefield_validate_extensions']); $path = FILEFIELD_SOURCE_ATTACH_PATH; $options = _filefield_source_attach_get($path, $allowed_extensions); $element['filefield_attach']['path'] = array( '#type' => 'select', // '#title' => t('Select attachment'), '#description' => t('Select available files to attach. If a file is larger than the max upload site please use this method to add files. To add files to this select list you need to upload files using FTP.'), '#options' => $options, ); $element['filefield_attach']['transfer'] = array( '#type' => 'submit', '#value' => t('Transfer'), '#submit' => array('node_form_submit_build_node'), '#ahah' => array( 'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'], 'wrapper' => $element['#id'] .'-ahah-wrapper', 'method' => 'replace', 'effect' => 'fade', ), ); return $element; } function _filefield_source_attach_get($path, $allowed_extensions = '.*', $existing = array()) { $file_exists = array(); $file_attach_options = array(''); $file_attach = file_scan_directory($path, '.*', array('.', '..', 'CVS'), 0, TRUE, $key = 'filename', 0, 0); if (count($file_attach)) { foreach ($existing as $file_exist) { if (!in_array(basename($file_exist['filepath']), $file_exists)) { $file_exists[] = basename($file_exist['filepath']); } } if (module_exists('transliteration')) { $file_attach = _filefield_source_attach_transliteration_wrapper($file_attach); } foreach ($file_attach as $filename => $fileinfo) { $is_attached = ''; if (in_array($filename, $file_exists)) { $is_attached = '(A)'; } $filepath = str_replace(FILEFIELD_SOURCE_ATTACH_PATH, '', $filename); $filepath = trim($filepath, '/'); $file_attach_options[$filepath] = $is_attached . ' ' . $filepath; } } return $file_attach_options; } // parts of code taken from transliteration_init and http://drupal.org/node/438940#comment-1987312 function _filefield_source_attach_transliteration_wrapper($files) { require_once (drupal_get_path('module', 'transliteration') . '/transliteration.inc'); $langcode = NULL; if (!empty($_POST['language'])) { $languages = language_list(); $langcode = isset($languages[$_POST['language']]) ? $_POST['language'] : NULL; } $clean_files = array(); $rename_count = 0; foreach ($files as $path => $file) { $clean_path = $path; $new_name = FILEFIELD_SOURCE_ATTACH_PATH . '/' . transliteration_clean_filename($file->basename, $langcode); if ($path == $new_name) { $clean_files[$clean_path] = $file; continue; } if (rename($path, $new_name)) { $clean_path = $new_name; $file->filename = $new_name; $file->basename = basename($new_name); $file->name = substr($file->basename, 0, strrpos($file->basename, '.')); $rename_count++; } $clean_files[$clean_path] = $file; } if ($rename_count > 0) { drupal_set_message(format_plural($rename_count, 'File Attach: 1 file renamed by transliteration module.', 'File Attach: @count files renamed by transliteration module.')); } return $clean_files; } /** * A #filefield_value_callback function. */ function filefield_source_attach_value($element, &$item) { if (isset($item['filefield_attach']['path']) && $item['filefield_attach']['path'] !== '0') { $field = content_fields($element['#field_name'], $element['#type_name']); $filename = FILEFIELD_SOURCE_ATTACH_PATH . '/' . $item['filefield_attach']['path']; $temp = file_directory_temp(); if (!file_copy($filename, $temp)) { form_error($element, t('File attach copy error')); // debug error function // form_error($element, t('File attach copy error, debug:
$element=' . print_r($element, TRUE) . '$item=' . print_r($item, TRUE) . '$temp=' . print_r($temp, TRUE) . '')); return; } $filepath = $temp . '/' . basename($filename); // Perform basic extension check on the file before trying to transfer. $extensions = $field['widget']['file_extensions']; $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i'; if (!empty($extensions) && !preg_match($regex, $filename)) { form_error($element, t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions))); return; } if ($file = field_file_save_file($filepath, array(), filefield_widget_file_path($field))) { $item = array_merge($item, $file); } // Delete the temporary file. @unlink($filepath); } $item['filefield_attach']['path'] = ''; } /** * Theme the output of the autocomplete field. */ function theme_filefield_source_attach_element($element) { return '