'',
'form_key' => NULL,
'mandatory' => 0,
'pid' => 0,
'weight' => 0,
'extra' => array(
'filtering' => array(
'types' => array('gif', 'jpg', 'png'),
'addextensions' => '',
'size' => 800,
),
'savelocation' => '',
'width' => '',
'title_display' => 0,
'description' => '',
'attributes' => array(),
'private' => FALSE,
),
);
}
/**
* Implements _webform_theme_component().
*/
function _webform_theme_file() {
return array(
'webform_edit_file_extensions' => array(
'arguments' => array('element' => NULL),
'file' => 'components/file.inc',
),
'webform_render_file' => array(
'arguments' => array('element' => NULL),
'file' => 'components/file.inc',
),
'webform_display_file' => array(
'arguments' => array('element' => NULL),
'file' => 'components/file.inc',
),
);
}
/**
* Implements _webform_edit_component().
*/
function _webform_edit_file($component) {
$form = array();
$form['validation']['size'] = array(
'#type' => 'textfield',
'#title' => t('Max upload size'),
'#default_value' => $component['extra']['filtering']['size'],
'#description' => t('Enter the max file size a user may upload (in KB).'),
'#size' => 10,
'#field_suffix' => t('KB'),
'#parents' => array('extra', 'filtering', 'size'),
'#element_validate' => array('_webform_edit_file_size_validate'),
'#weight' => 1,
);
$form['validation']['extensions'] = array(
'#element_validate' => array('_webform_edit_file_extensions_validate'),
'#parents' => array('extra', 'filtering'),
'#theme' => 'webform_edit_file_extensions',
'#title' => t('Allowed file extensions'),
'#weight' => 2,
);
// Find the list of all currently valid extensions.
$current_types = isset($component['extra']['filtering']['types']) ? $component['extra']['filtering']['types'] : array();
$types = array('gif', 'jpg', 'png');
$form['validation']['extensions']['types']['webimages'] = array(
'#type' => 'checkboxes',
'#title' => t('Web images'),
'#options' => drupal_map_assoc($types),
'#default_value' => array_intersect($current_types, $types),
);
$types = array('bmp', 'eps', 'tif', 'pict', 'psd');
$form['validation']['extensions']['types']['desktopimages'] = array(
'#type' => 'checkboxes',
'#title' => t('Desktop images'),
'#options' => drupal_map_assoc($types),
'#default_value' => array_intersect($current_types, $types),
);
$types = array('txt', 'rtf', 'html', 'odf', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'xml');
$form['validation']['extensions']['types']['documents'] = array(
'#type' => 'checkboxes',
'#title' => t('Documents'),
'#options' => drupal_map_assoc($types),
'#default_value' => array_intersect($current_types, $types),
);
$types = array('avi', 'mov', 'mp3', 'ogg', 'wav');
$form['validation']['extensions']['types']['media'] = array(
'#type' => 'checkboxes',
'#title' => t('Media'),
'#options' => drupal_map_assoc($types),
'#default_value' => array_intersect($current_types, $types),
);
$types = array('bz2', 'dmg', 'gz', 'jar', 'rar', 'sit', 'tar', 'zip');
$form['validation']['extensions']['types']['archives'] = array(
'#type' => 'checkboxes',
'#title' => t('Archives'),
'#options' => drupal_map_assoc($types),
'#default_value' => array_intersect($current_types, $types),
);
$form['validation']['extensions']['addextensions'] = array(
'#type' => 'textfield',
'#title' => t('Additional extensions'),
'#default_value' => $component['extra']['filtering']['addextensions'],
'#description' => t('Enter a list of additional file extensions for this upload field, seperated by commas.
Entered extensions will be appended to checked items above.'),
'#size' => 20,
'#weight' => 3,
);
$form['extra']['savelocation'] = array(
'#type' => 'textfield',
'#title' => t('Upload directory'),
'#default_value' => $component['extra']['savelocation'],
'#description' => t('Webform uploads are always saved in the site files directory. You may optionally specify a subfolder to store your files.'),
'#weight' => 3,
'#field_prefix' => file_directory_path() . '/webform/',
'#element_validate' => array('_webform_edit_file_check_directory'),
'#after_build' => array('_webform_edit_file_check_directory'),
);
$form['display']['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => $component['extra']['width'],
'#description' => t('Width of the file field.') . ' ' . t('Leaving blank will use the default size.'),
'#size' => 5,
'#maxlength' => 10,
'#weight' => 4,
'#parents' => array('extra', 'width')
);
return $form;
}
/**
* A Form API element validate function to check filesize is numeric.
*/
function _webform_edit_file_size_validate($element) {
if (!empty($element['#value'])) {
if (!is_numeric($element['#value']) || intval($element['#value']) != $element['#value']) {
form_error($element, t('Max upload size must be a number in KB.'));
}
}
}
/**
* A Form API after build and validate function.
*
* Ensure that the destination directory exists and is writable.
*/
function _webform_edit_file_check_directory($element) {
$base_dir = file_directory_path() . '/webform';
$base_success = file_check_directory($base_dir, FILE_CREATE_DIRECTORY);
$destination_dir = $base_dir . '/' . $element['#value'];
// Sanity check input to prevent use parent (../) directories.
if (preg_match('/\.\.[\/\\\]/', $destination_dir . '/')) {
form_error($element, t('The save directory %directory is not valid.', array('%directory' => $destination_dir)));
}
else {
// Try to make the directory recursively before calling file_check_directory().
// This may be removed in D7, as recusive is the default there.
@mkdir($destination_dir, 0775, TRUE);
$destination_success = file_check_directory($destination_dir, FILE_CREATE_DIRECTORY);
if (!$base_success || !$destination_success) {
form_error($element, t('The save directory %directory could not be created. Check that the webform files directory is writtable.', array('%directory' => $destination_dir)));
}
}
return $element;
}
/**
* A Form API element validate function.
*
* Change the submitted values of the component so that all filtering extensions
* are saved as a single array.
*/
function _webform_edit_file_extensions_validate($element, &$form_state) {
// Predefined types.
$extensions = array();
foreach (element_children($element['types']) as $category) {
foreach (array_keys($element['types'][$category]['#value']) as $extension) {
if ($element['types'][$category][$extension]['#value']) {
$extensions[] = $extension;
}
}
}
// Additional types.
$additional_extensions = explode(',', $element['addextensions']['#value']);
foreach ($additional_extensions as $extension) {
$clean_extension = drupal_strtolower(trim($extension));
if (!empty($clean_extension) && !in_array($clean_extension, $extensions)) {
$extensions[] = $clean_extension;
}
}
form_set_value($element['types'], $extensions, $form_state);
}
/**
* Output the list of allowed extensions as checkboxes.
*/
function theme_webform_edit_file_extensions($element) {
drupal_add_js(drupal_get_path('module', 'webform') . '/js/webform-admin.js');
drupal_add_css(drupal_get_path('module', 'webform') . '/css/webform-admin.css');
// Format the components into a table.
$rows = array();
foreach (element_children($element['types']) as $filtergroup) {
$row = array();
$first_row = count($rows);
if ($element['types'][$filtergroup]['#type'] == 'checkboxes') {
$select_link = ' (' . t('select') . ')';
$row[] = $element['types'][$filtergroup]['#title'];
$element['types'][$filtergroup]['#title'] = NULL;
$row[] = array('data' => $select_link, 'width' => 40);
$row[] = array('data' => drupal_render($element['types'][$filtergroup]), 'class' => 'webform-file-extensions webform-select-group-' . $filtergroup);
$rows[] = array('data' => $row);
unset($element['types'][$filtergroup]);
}
}
// Add the row for additional types.
$row = array();
$title = $element['addextensions']['#title'];
$element['addextensions']['#title'] = NULL;
$row[] = array('data' => $title, 'colspan' => 2);
$row[] = drupal_render($element['addextensions']);
$rows[] = $row;
$header = array(array('data' => t('Category'), 'colspan' => '2'), array('data' => t('Types')));
// Create the table inside the form.
$table = theme('table', $header, $rows, array('class' => 'webform-file-extensions'));
$element['types']['table'] = array(
'#value' => theme('form_element', $element, $table),
);
return drupal_render($element);
}
/**
* Implements _webform_render_component().
*/
function _webform_render_file($component, $value = NULL, $filter = TRUE) {
$node = isset($component['nid']) ? node_load($component['nid']) : NULL;
// Normally every file component is given a unique ID based on its key.
if (isset($component['nid'])) {
$node = node_load($component['nid']);
$form_key = implode('_', webform_component_parent_keys($node, $component));
}
// If being used as a default though, we don't yet have a form key.
else {
$form_key = 'default';
}
$element[$form_key] = array(
'#type' => 'file',
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
//'#required' => $component['mandatory'], // Drupal core bug with required file uploads.
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
'#attributes' => $component['extra']['attributes'],
'#tree' => FALSE, // file_check_upload assumes a flat $_FILES structure.
'#element_validate' => array(
'_webform_validate_file',
'_webform_required_file', // Custom required routine.
),
'#pre_render' => array('webform_element_title_display'),
'#translatable' => array('title', 'description'),
'#webform_component' => $component,
);
$element['#webform_required'] = $component['mandatory'];
$element['#webform_form_key'] = $form_key;
$element['#weight'] = $component['weight'];
$element['#theme'] = 'webform_render_file';
$element['#title_display'] = $element[$form_key]['#title_display'];
$element['#theme_wrappers'] = array('webform_element_wrapper');
$element['#post_render'] = array('webform_element_wrapper');
// Change the 'width' option to the correct 'size' option.
if ($component['extra']['width'] > 0) {
$element[$form_key]['#size'] = $component['extra']['width'];
}
// Add a hidden element to store the FID for new files.
$element['_fid'] = array(
'#type' => 'hidden',
'#default_value' => '',
);
// Add a hidden element to store the FID for existing files.
$element['_old'] = array(
'#type' => 'hidden',
'#value' => isset($value[0]) ? $value[0] : NULL,
);
return $element;
}
/**
* Render a File component.
*/
function theme_webform_render_file($element) {
// Add information about the existing file, if any.
if (isset($element['#default_value'])) {
$element['_fid']['#value'] = $element['#default_value'];
}
$value = $element['_fid']['#value'] ? $element['_fid']['#value'] : $element['_old']['#value'];
if ($value && ($file = webform_get_file($value))) {
$firstchild = array_shift(element_children($element));
$element[$firstchild]['#suffix'] = ' ' . l(t('Download @filename', array('@filename' => webform_file_name($file->filepath))), webform_file_url($file->filepath)) . (isset($element['#suffix']) ? $element['#suffix'] : '');
$element[$firstchild]['#description'] = '