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) { /* switch ($op) { case 'form': $form = array(); $form['attach_dir'] = array( '#type' => 'select', '#title' => t('Set file attach path'), '#default_value' => isset($field['attach_dir']) ? $field['attach_dir'] : '', '#description' => t('Set a directory location relative to base path (/) to enable attaching files from that directory. Leave blank to disable. The directory must exist before setting this value.'), ); return $form; case 'validate': break; case 'save': return array('attach_dir'); } */ 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, "(". $allowed_extensions .")$", array('.', '..', 'CVS'), 0, TRUE, $key = 'filename', 0, 0); $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']); } } 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; } /** * 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')); 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; } // The reason for this component is to get around large file uploads. // So we should not check the filesize. /* if (!empty($element['#upload_validators']['filefield_validate_size'][0])) { $max_size = $element['#upload_validators']['filefield_validate_size'][0]; $file_size = $info['download_content_length']; if ($file_size > $max_size) { form_error($element, t('The attached file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($file_size), '%maxsize' => format_size($max_size)))); return; } } field_file_save_file($filepath, $element['#upload_validators'], filefield_widget_file_path($field)); */ 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 '
' . theme('select', $element['path']) . theme('submit', $element['transfer']) . '
'; }