-
Notifications
You must be signed in to change notification settings - Fork 9
/
islandora_plupload.module
126 lines (117 loc) · 4.17 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
<?php
/**
* @file
* Inserts plupload file upload elements into Islandora forms.
*/
/**
* Implements hook_menu().
*/
function islandora_plupload_menu() {
$items = array();
$items['admin/islandora/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 (strpos($form_id, 'islandora') !== FALSE) {
islandora_plupload_alter_managed_files($form);
}
}
/**
* Recursively change managed_file elements to plupload.
*
* @param array $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)) {
foreach ($form as &$element) {
// There must be a better way to get the children.
if (isset($element['#type']) && $element['#type'] === 'fieldset') {
$children = element_children($element);
if (is_array($children) && (count($children) > 0)) {
foreach ($children as $key) {
islandora_plupload_alter_managed_files($form[$key]);
}
}
}
if (isset($element['#type']) && $element['#type'] === 'managed_file') {
$element['#type'] = 'plupload';
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';
$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.
*
* @TODO: handle form_state values where tree is TRUE (no Islandora cases yet)
*
* @param array $element
* The form element.
* @param array $form_state
* The form state.
*/
function islandora_plupload_element_validate($element, &$form_state) {
module_load_include('inc', 'islandora', 'includes/mime_detect');
if (isset($element['#type']) && $element['#type'] == 'plupload' && isset($form_state['values'][$element['#name']][0]) && is_array($form_state['values'][$element['#name']][0])) {
$file = plupload_file_uri_to_object($form_state['values'][$element['#name']][0]["tmppath"]);
$file->filename = $form_state['values'][$element['#name']][0]['name'];
$mime_detect = new MimeDetect();
$file->filemime = $mime_detect->getMimeType($file->filename);
islandora_plupload_file_save($file);
$form_state['values'][$element['#name']] = $file->fid;
}
elseif (isset($element['#type']) && $element['#type'] == 'plupload' && !is_numeric($form_state['values'][$element['#name']])) {
$form_state['values'][$element['#name']] = 0;
}
}
/**
* 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;
}