forked from mitchmac/islandora_plupload
-
Notifications
You must be signed in to change notification settings - Fork 8
/
islandora_plupload.module
217 lines (203 loc) · 8.05 KB
/
islandora_plupload.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
56
57
58
59
60
61
62
63
64
65
66
67
68
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* @file
* Inserts plupload file upload elements into Islandora forms.
*/
/**
* Implements hook_menu().
*/
function islandora_plupload_menu() {
$items = array();
$items['admin/islandora/tools/plupload'] = array(
'title' => 'Plupload',
'access arguments' => array('administer islandora plupload'),
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_plupload_admin_form'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_permission().
*/
function islandora_plupload_permission() {
return array(
'administer islandora plupload' => array(
'title' => t('Administer Islandora Plupload'),
),
);
}
/**
* Implements hook_form_alter().
*/
function islandora_plupload_form_alter(&$form, &$form_state, $form_id) {
if ($form_id !== 'islandora_default_thumbs_admin' && strpos($form_id, 'islandora') !== FALSE) {
islandora_plupload_alter_managed_files($form);
}
}
/**
* Recursively change managed_file elements to plupload.
*
* @param array|null $form
* A form or element array.
*/
function islandora_plupload_alter_managed_files(&$form) {
$islandora_plupload_max = variable_get('islandora_plupload_max_filesize', 3000);
if (is_array($form) && !empty($form)) {
$children = element_children($form);
foreach ($children as $element_key) {
$element =& $form[$element_key];
islandora_plupload_alter_managed_files($element);
if (isset($element['#type']) && $element['#type'] === 'managed_file' && !(isset($element['#islandora_plupload_do_not_alter']) && $element['#islandora_plupload_do_not_alter'])) {
// The Plupload and Islandora Plupload modules create a bug that
// prevents users from adding and replacing content that accepts any
// file type as a result of validating a file's extension (ex. .jpg)
// against an empty string.
if (isset($form['file']['#upload_validators']['file_validate_extensions']) && !$form['file']['#upload_validators']['file_validate_extensions'][0]) {
$element['#process'] = array(
'islandora_plupload_element_processs',
);
}
$element['#type'] = 'plupload';
// Element value validation is hard-coded to occur in form.inc
// function _form_validate() before the elements own validation hooks
// are run. We must ignore the #required property of plupload elements,
// and use our own #upload_required property. So that we can validate
// the unconventinal values used for submitted plupload elements in our
// own element validation handler rather than the one provided by
// Drupal.
$element['#upload_required'] = isset($element['#required']) ? $element['#required'] : FALSE;
$element['#required'] = FALSE;
unset($element['#description']);
$element['#upload_validators']['file_validate_size'] = array($islandora_plupload_max * 1024 * 1024);
$settings = array(
'max_file_size' => $islandora_plupload_max . 'MB',
'chunk_size' => variable_get('islandora_plupload_chunk_size', 15) . 'MB',
'max_file_count' => 1,
);
$element['#plupload_settings'] = $settings;
$element['#submit_element'] = '#edit-next,#edit-submit';
$element['#element_validate'][] = 'islandora_plupload_element_validate';
drupal_add_js(drupal_get_path('module', 'islandora_plupload') . '/js/element.js', array('type' => 'file', 'scope' => 'footer'));
}
}
}
}
/**
* Plupload element validation. Format the value as a managed_file would be.
*
* @param array $element
* The form element.
* @param array $form_state
* The form state.
*/
function islandora_plupload_element_validate(array $element, array &$form_state) {
module_load_include('inc', 'islandora', 'includes/mime_detect');
$messages = islandora_plupload_element_file_validate($element, $form_state);
$element_value =& drupal_array_get_nested_value($form_state['values'], $element['#parents']);
// Need to check to make sure there is a value here as we could have
// optional plupload elements in forms.
$first_element_value = isset($element_value) ? reset($element_value) : FALSE;
if ($first_element_value && is_array($first_element_value)) {
$file = plupload_file_uri_to_object($first_element_value["tmppath"]);
$file->filename = $first_element_value['name'];
$mime_detect = new MimeDetect();
$file->filemime = $mime_detect->getMimeType($file->filename);
$file->status = 0;
islandora_plupload_file_save($file);
$element_value = $file->fid;
}
elseif (!is_numeric($element_value)) {
$element_value = 0;
if ($element['#upload_required']) {
$messages[] = t('@name field is required.', array('@name' => $element['#title']));
}
}
// Drupal will only print one error per element.
$message = implode('<br/>', $messages);
if ($message) {
form_error($element, filter_xss($message, array('br')));
}
}
/**
* Validate a plupload based element with file_validate.
*
* Can handle multiple files.
*
* @param array $element
* The form element.
* @param array $form_state
* The form state.
*
* @return array
* List of translated messages.
*/
function islandora_plupload_element_file_validate(array $element, array &$form_state) {
module_load_include('inc', 'islandora', 'includes/mime_detect');
$messages = array();
$element_value =& drupal_array_get_nested_value($form_state['values'], $element['#parents']);
// Validate each file.
$rejected_file_count = 0;
foreach ($element_value as $file_value) {
// Adapted from @see: plupload_element_validate() to facilitate full file
// validation.
$file_object = plupload_file_uri_to_object($file_value["tmppath"]);
$destination = variable_get('file_default_scheme', 'public') . '://' . $file_value['name'];
$destination = file_stream_wrapper_uri_normalize($destination);
$file_object->filename = drupal_basename($destination);
$file_object->filemime = file_get_mimetype($destination);
$issues = file_validate($file_object, $element['#upload_validators']);
if ($issues) {
$rejected_file_count++;
}
foreach ($issues as $issue) {
$messages[] = t('@file: @issue', array('@file' => $file_value['name'], '@issue' => $issue));
}
}
if ($rejected_file_count > 0 && count($element_value) != $rejected_file_count) {
$messages[] = t('There were validation errors with some of the uploaded files. After addressing the issue(s) please resubmit all files.');
}
return $messages;
}
/**
* Copy of Drupal file_save that buries errors using filesize() on files > 2GB.
*/
function islandora_plupload_file_save($file) {
$file->timestamp = REQUEST_TIME;
if (!isset($file->filesize)) {
$file->filesize = @filesize($file->uri);
}
module_invoke_all('file_presave', $file);
module_invoke_all('entity_presave', $file, 'file');
if (empty($file->fid)) {
drupal_write_record('file_managed', $file);
// Inform modules about the newly added file.
module_invoke_all('file_insert', $file);
module_invoke_all('entity_insert', $file, 'file');
}
unset($file->original);
return $file;
}
/**
* Custom process callback for the plupload element.
*
* XXX: Simply unsets the 'file_validate_extensions' portion of the list of
* upload validators. This will squash any attempts to specify a custom list of
* extension validators. Allowing for custom lists of extension validators AND
* for extension validation to be bypassed would require ripping out a lot of
* hard-baked functionality in the pluploader; we may need to revisit if this
* becomes necessary, but the current use cases call for no validation.
*
* XXX: Reference Islandora Research data module.
*/
function islandora_plupload_element_processs($element) {
unset($element['#upload_validators']['file_validate_extensions']);
if (isset($element['#description'])) {
$element['#description'] = theme('file_upload_help', array(
'description' => '',
'upload_validators' => $element['#upload_validators'],
));
}
return $element;
}