Newer
Older
<?php
// $Id$
/**
* @file
* FileField: Defines a CCK file field type.
*
* Uses content.module to store the fid and field specific metadata,
* and Drupal's {files} table to store the actual file data.
*
* This file contains CCK widget related functionality.
*/
/**
* @file
*
* FileField Widget Settings Hooks.
Darrel O'Pry
committed
* @todo: move description property to filefield_widget widget callbacks
* (filefield_widget_widget_{$op}).
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
*/
function filefield_widget_settings_form($widget) {
$form = array();
$form['file_extensions'] = array(
'#type' => 'textfield',
'#title' => t('Permitted upload file extensions'),
'#default_value' => is_string($widget['file_extensions']) ? $widget['file_extensions'] : 'txt',
'#size' => 64,
'#description' => t('Extensions a user can upload to this field. Separate extensions with a space and do not include the leading dot. Leaving this blank will allow users to upload a file with any extension.'),
'#weight' => 1,
);
$form['path_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Path settings'),
'#collapsible' => true,
'#collapsed' => true,
'#weight' => 6,
);
$form['path_settings']['file_path'] = array(
'#type' => 'textfield',
'#title' => t('File path'),
'#default_value' => is_string($widget['file_path']) ? $widget['file_path'] : '',
'#description' => t('Optional subdirectory within the "%dir" directory where files will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files'))),
'#element_validate' => array('_filefield_widget_settings_file_path_validate'),
'#suffix' => theme('token_help', 'user'),
);
$form['max_filesize'] = array(
'#type' => 'fieldset',
'#title' => t('File size restrictions'),
'#description' => t('Limits for the size of files that a user can upload. Note that these settings only apply to newly uploaded files, whereas existing files are not affected.'),
'#weight' => 6,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
// upload validator. @todo: consider replacing with global
// && node validate.
$form['max_filesize']['max_filesize_per_file'] = array(
'#type' => 'textfield',
'#title' => t('Maximum upload size per file'),
'#default_value' => is_string($widget['max_filesize_per_file'])
? $widget['max_filesize_per_file']
: '',
'#description' => t('Specify the size limit that applies to each file separately. Enter a value like "512" (bytes), "80K" (kilobytes) or "50M" (megabytes) in order to restrict the allowed file size. If you leave this this empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes.'),
'#element_validate' => array('_filefield_widget_settings_max_filesize_per_file_validate'),
);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
$form['max_filesize']['max_filesize_per_node'] = array(
'#type' => 'textfield',
'#title' => t('Maximum upload size per node'),
'#default_value' => is_string($widget['max_filesize_per_node'])
? $widget['max_filesize_per_node']
: '',
'#description' => t('Specify the total size limit for all files in field on a given node. Enter a value like "512" (bytes), "80K" (kilobytes) or "50M" (megabytes) in order to restrict the total size of a node. Leave this empty if there should be no size restriction.'),
'#element_validate' => array('_filefield_widget_settings_max_filesize_per_node_validate'),
);
return $form;
}
function filefield_widget_settings_save($widget) {
return array(
'file_extensions', 'file_path', 'max_filesize_per_file',
'max_filesize_per_node', 'file_widgets'
);
}
function _filefield_widget_settings_file_path_validate($element, &$form_state) {
// Strip slashes from the beginning and end of $widget['file_path']
$form_state['values']['file_path'] = trim($form_state['values']['file_path'], '\\/');
}
function _filefield_widget_settings_max_filesize_per_file_validate($element, &$form_state) {
if (empty($form_state['values']['max_filesize_per_file'])) {
return; // Empty means no size restrictions, so don't throw an error.
}
else if (!is_numeric(parse_size($form_state['values']['max_filesize_per_file']))) {
form_error($element, t('The "Maximum file size for each file" option must contain a valid value. You can either leave the text field empty or enter a string like "512" (bytes), "80K" (kilobytes) or "50M" (megabytes).'));
}
}
function _filefield_widget_settings_max_filesize_per_node_validate($element, &$form_state) {
if (empty($form_state['values']['max_filesize_per_node'])) {
return; // Empty means no size restrictions, so don't throw an error.
}
else if (!is_numeric(parse_size($form_state['values']['max_filesize_per_node']))) {
form_error($element, t('The "Maximum file size per node" option must contain a valid value. You can either leave the text field empty or enter a string like "512" (bytes), "80K" (kilobytes) or "50M" (megabytes).'));
}
}
Darrel O'Pry
committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function filefield_save_upload($element) {
$upload_name = $element['#field_name'] .'_'. $element['#delta'];
$field_instance = content_fields($element['#field_name'], $element['#type_name']);
if (empty($_FILES['files']['name'][$upload_name])) {
return 0;
}
$dest = $field_instance['widget']['file_path'];
if (module_exists('token')) {
global $user;
$dest = token_replace($dest, 'user', $user);
}
$dest = file_directory_path() .'/'. $dest;
if (!field_file_check_directory($dest, FILE_CREATE_DIRECTORY)) {
watchdog('imagefield', 'The upload directory %directory for the file field %field (content type %type) could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', array('%directory' => $dest, '%field' => $element['#field_name'], '%type' => $element['#type_name']));
form_set_error($upload_name, t('The file could not be uploaded.'));
return 0;
}
if (!$file = field_file_save_upload($upload_name, $element['#upload_validators'], $dest)) {
watchdog('imagefield', 'The file upload failed. %upload', array('%upload' => $upload_name));
form_set_error($upload_name, t('The Image upload failed.'));
return 0;
}
return $file['fid'];
}
/**
* FileField widget element callbacks.
*/
Darrel O'Pry
committed
function filefield_widget_value($element, $edit = FALSE) {
if (!$edit) {
$file = field_file_load($element['#default_value']['fid']);
$item = $element['#default_value'];
}
else {
Darrel O'Pry
committed
// uploads take priority over value of fid text field.
if ($fid = filefield_save_upload($element)) {
$edit['fid'] = $fid;
}
// load file.
Darrel O'Pry
committed
$file = field_file_load($edit['fid']);
$item = array(
'fid' => $edit['fid'],
Darrel O'Pry
committed
'data' => empty($edit['data']) ? array() : $edit['data'],
Darrel O'Pry
committed
);
}
Darrel O'Pry
committed
// merge file and item data so it is available to all widgets.
// if this widget is another type and leaning on filefield to do the dirty work....
// pass it back home.
$function = $element['#type'] .'_widget_value';
if (function_exists($function)) {
$item = array_merge($item, $function($element, $edit));
}
return $item;
Darrel O'Pry
committed
}
function filefield_widget_process($element, $edit, &$form_state, $form) {
Darrel O'Pry
committed
$item = $element['#value'];
$field_name = $element['#field_name'];
$delta = $element['#delta'];
// check remove buttons...
$remove_name = $element['#field_name'] .'_'. $element['#delta'] .'_remove_btn';
Darrel O'Pry
committed
if (isset($form_state['clicked_button'])) {
if ($form_state['clicked_button']['#name'] == $remove_name) {
$item['fid'] = 0;
}
Darrel O'Pry
committed
}
$field = content_fields($element['#field_name'], $element['#type_name']);
Darrel O'Pry
committed
Darrel O'Pry
committed
// figute out our fid...
Darrel O'Pry
committed
$element['fid'] = array('#type' => 'hidden', '#value' => $item['fid']);
Darrel O'Pry
committed
if ($item['fid'] != 0) {
$element['preview'] = array('#type' => 'markup', '#value' => theme($element['#type'] .'_preview', $item));
Darrel O'Pry
committed
}
Darrel O'Pry
committed
// placeholder.. will be serialized into the data column. this is a place for widgets
// to put additional data.
$element['data'] = array('#tree' => 'true');
$element['data']['description'] = array(
Darrel O'Pry
committed
'#type' => 'textfield',
'#title' => t('Description'),
Darrel O'Pry
committed
'#value' => isset($item['data']['description']) ? $item['data']['description'] : '',
Darrel O'Pry
committed
);
Darrel O'Pry
committed
if ($field['force_list_default']) {
$element['list'] = array(
'#type' => 'hidden',
'#value' => $field['list_default'],
);
}
else {
$element['list'] = array(
'#type' => 'checkbox',
'#title' => t('List'),
Darrel O'Pry
committed
'#value' => isset($item['list']) ? $item['list'] : $field['list_default'],
Darrel O'Pry
committed
'#attributes' => array('class' => 'filefield-list'),
);
}
Darrel O'Pry
committed
Darrel O'Pry
committed
foreach ($element['#upload_validators'] as $callback => $arguments) {
$help_func = $callback .'_help';
$desc[] = call_user_func_array($help_func, $arguments);
}
$element['upload'] = array(
'#name' => 'files['. $element['#field_name'] .'_'. $element['#delta'] .']',
'#type' => 'file',
'#title' => t('New Upload'),
'#description' => implode('<br />', $desc),
'#attributes' => array(
Darrel O'Pry
committed
'accept' => implode(',', array_filter(explode(' ', $field['widget']['file_extensions']))),
Darrel O'Pry
committed
)
);
if ($item['fid'] != 0) {
$element['upload']['#title'] = t('Replace');
}
Darrel O'Pry
committed
$element['#prefix'] = '<div id="'. $element['#id'] .'-ahah-wrapper" class="filefield-ahah-wrapper">';
$element['#suffix'] = '</div>';
Darrel O'Pry
committed
$element['upload_btn'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
Darrel O'Pry
committed
'#process' => array('form_expand_ahah'),
Darrel O'Pry
committed
'#submit' => array('node_form_submit_build_node'),
'#ahah' => array( // with JavaScript
Darrel O'Pry
committed
'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'],
'wrapper' => $element['#id'] .'-ahah-wrapper',
Darrel O'Pry
committed
'method' => 'replace',
'effect' => 'fade',
),
'#field_name' => $element['#field_name'],
'#delta' => $element['#delta'],
'#type_name' => $element['#type_name'],
'#upload_validators' => $element['#upload_validators'],
);
Darrel O'Pry
committed
$element['remove_btn'] = array(
'#name' => $element['#field_name'] .'_'. $element['#delta'] .'_remove_btn',
Darrel O'Pry
committed
'#type' => 'submit',
'#value' => t('Remove'),
Darrel O'Pry
committed
'#process' => array('filefield_widget_process_remove_btn', 'form_expand_ahah'),
'#submit' => array('filefield_widget_submit_remove_btn'),
Darrel O'Pry
committed
'#ahah' => array( // with JavaScript
Darrel O'Pry
committed
'path' => 'filefield/ahah/'. $element['#type_name'] .'/'. $element['#field_name'] .'/'. $element['#delta'],
'wrapper' => $element['#id'] .'-wrapper',
'method' => 'replace',
'effect' => 'fade',
Darrel O'Pry
committed
),
'#field_name' => $element['#field_name'],
'#delta' => $element['#delta'],
Darrel O'Pry
committed
);
}
return $element;
}
Darrel O'Pry
committed
function filefield_widget_validate($element, &$form_state) {
Darrel O'Pry
committed
function filefield_widget_submit_remove_btn($form, &$form_state) {
Darrel O'Pry
committed
node_form_submit_build_node($form, $form_state);
Darrel O'Pry
committed
}
Darrel O'Pry
committed
function _filefield_widget_validate($element, &$form_state) {
}
/**
* FormAPI theme function. Theme the output of an image field.
*/
function theme_filefield_widget($element) {
return theme('form_element', $element, $element['#children']);
Darrel O'Pry
committed
}
function theme_filefield_widget_preview($item) {
return '<div class="filefield-preview clear-block">'.
'<div class="filename">'. theme('filefield_file', $item) .'</div>'.
'<div class="filesize">'. format_size($item['filesize']) .'</div>'.
'<div class="filemime">'. $item['filemime'] .'</div>'.
'</div>';
}
function theme_filefield_widget_item($element) {
return '<div class="filefield-row clear-block">'.
'<div class="filefield-filename clear-block">'. drupal_render($element['preview']) . '</div>'.
'<div class="fielfield-edit clear-block">'.
'<div class="filefield-list">'. drupal_render($element['list']) . '</div>' .
'<div class="filefield-description">'. drupal_render($element['description']) . '</div>' .
'<div class="filefield-stuff">'. drupal_render($element) .'</div>'.
'</div>'.
'</div>';
}
Darrel O'Pry
committed
Darrel O'Pry
committed
/**
* #require validation for filetype fields.
*/
function filefield_node_form_validate($form, &$form_state) {
$type = content_types($form['type']['#value']);
foreach ($type['fields'] as $field_name => $field) {
Darrel O'Pry
committed
if (!(in_array($field['module'], array('imagefield', 'filefield'))) || !$field['required']) continue;
Darrel O'Pry
committed
$empty = $field['module'] .'_content_is_empty';
$valid = false;
$total_filesize = 0;
Darrel O'Pry
committed
foreach($form_state['values'][$field_name] as $delta => $item) {
if ($empty($item, $field)) continue;
Darrel O'Pry
committed
$valid = true;
$total_filesize += (int)$item['filesize'];
Darrel O'Pry
committed
}
if (!$valid) {
form_set_error($field_name, t('%title field is required.', array('%title' => $field['widget']['label'])));
}
$max_filesize = parse_size($field['widget']['max_filesize_per_node']);
if ($total_filesize > $max_filesize) {
form_set_error($field_name, t('Total filesize for %title, %tsize, exceeds field settings of %msize.',
array(
'%title' => $field['widget']['label'],
'%tsize' => format_size($total_filesize),
'%msize' => format_size($max_filesize)
)
));
}
Darrel O'Pry
committed
}
}