1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* This file contains the admin functions for the Media Bulk Upload module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Form callback for mass import.
|
10
|
*/
|
11
|
function media_bulk_upload_import($form, &$form_state) {
|
12
|
if (!isset($form_state['storage']['files'])) {
|
13
|
$form_state['storage']['step'] = 'choose';
|
14
|
$form_state['storage']['next_step'] = 'preview';
|
15
|
$form['directory'] = array(
|
16
|
'#type' => 'textfield',
|
17
|
'#title' => t('Directory'),
|
18
|
'#description' => t('Enter the absolute directory on the web server to look for files. Subdirectories inside this directory will not be scanned unless the Recurse through subdirectories box is checked.'),
|
19
|
'#required' => TRUE,
|
20
|
);
|
21
|
|
22
|
$form['recurse'] = array(
|
23
|
'#type' => 'checkbox',
|
24
|
'#title' => t('Recurse through subdirectories'),
|
25
|
'#description' => 'Iterate over the directory looking for files within subfolders that match the pattern.',
|
26
|
);
|
27
|
|
28
|
$form['to_directory'] = array(
|
29
|
'#type' => 'textfield',
|
30
|
'#title' => t('To Directory'),
|
31
|
'#description' => t('Enter the subdirectory of /sites/default/files where files will be copied. If empty the default directory will be use.'),
|
32
|
'#required' => FALSE,
|
33
|
);
|
34
|
|
35
|
$form['pattern'] = array(
|
36
|
'#type' => 'textarea',
|
37
|
'#title' => t('Pattern'),
|
38
|
'#description' => t("Only files matching these patterns will be imported. Enter one pattern per line. The '*' character is a wildcard. Example patterns are %png_example to import all PNG files.", array('%png_example' => '*.png')),
|
39
|
'#default_value' => '*',
|
40
|
'#required' => TRUE,
|
41
|
);
|
42
|
|
43
|
$form['actions'] = array('#type' => 'actions');
|
44
|
$form['actions']['submit'] = array(
|
45
|
'#type' => 'submit',
|
46
|
'#value' => t('Preview'),
|
47
|
);
|
48
|
$form['actions']['cancel'] = array(
|
49
|
'#type' => 'link',
|
50
|
'#title' => t('Cancel'),
|
51
|
'#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/content/file',
|
52
|
);
|
53
|
}
|
54
|
else {
|
55
|
$form['preview'] = array(
|
56
|
'#markup' => theme('item_list', array('items' => $form_state['storage']['files'])),
|
57
|
);
|
58
|
|
59
|
$form = confirm_form($form, t('Import these files?'), 'admin/content/file/import');
|
60
|
}
|
61
|
return $form;
|
62
|
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Validate handler for media_import().
|
67
|
*/
|
68
|
function media_bulk_upload_import_validate($form, &$form_state) {
|
69
|
if ($form_state['values']['op'] != t('Confirm')) {
|
70
|
$directory = $form_state['values']['directory'];
|
71
|
$pattern = $form_state['values']['pattern'];
|
72
|
$recurse = $form_state['values']['recurse'];
|
73
|
if (!is_dir($directory)) {
|
74
|
form_set_error('directory', t('The provided directory does not exist.'));
|
75
|
}
|
76
|
if (!is_readable($directory)) {
|
77
|
form_set_error('directory', t('The provided directory is not readable.'));
|
78
|
}
|
79
|
|
80
|
$pattern_quoted = preg_quote($pattern, '/');
|
81
|
$pattern_quoted = preg_replace('/(\r\n?|\n)/', '|', $pattern_quoted);
|
82
|
$pattern_quoted = strtr($pattern_quoted, array(
|
83
|
'\\|' => '|',
|
84
|
'\\*' => '.*',
|
85
|
'\\?' => '.?',
|
86
|
));
|
87
|
$files = file_scan_directory($directory, '/^(' . $pattern_quoted . ')$/i', array('recurse' => $recurse));
|
88
|
$files = array_keys($files);
|
89
|
if (empty($files)) {
|
90
|
form_set_error('pattern', t('No files were found in %directory matching the regular expression %pattern', array('%directory' => $directory, '%pattern' => $pattern_quoted)));
|
91
|
}
|
92
|
$form_state['storage']['files'] = $files;
|
93
|
$form_state['storage']['to_directory'] = $form_state['values']['to_directory'];
|
94
|
}
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Submit handler for media_import().
|
99
|
*/
|
100
|
function media_bulk_upload_import_submit($form, &$form_state) {
|
101
|
if ($form_state['values']['op'] == t('Confirm')) {
|
102
|
$files = $form_state['storage']['files'];
|
103
|
$params = array();
|
104
|
$params['to_directory'] = $form_state['storage']['to_directory'];
|
105
|
$batch = array(
|
106
|
'title' => t('Importing'),
|
107
|
'operations' => array(
|
108
|
array('media_bulk_upload_import_batch_import_files', array($files, $params)),
|
109
|
),
|
110
|
'finished' => 'media_bulk_upload_import_batch_import_complete',
|
111
|
'file' => drupal_get_path('module', 'media_bulk_upload') . '/includes/media_bulk_upload.admin.inc',
|
112
|
);
|
113
|
batch_set($batch);
|
114
|
return;
|
115
|
|
116
|
}
|
117
|
$form_state['rebuild'] = TRUE;
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* BatchAPI callback op for media import.
|
122
|
*/
|
123
|
function media_bulk_upload_import_batch_import_files($files, $params, &$context) {
|
124
|
if (!isset($context['sandbox']['files'])) {
|
125
|
// This runs the first time the batch runs.
|
126
|
// This is stupid, but otherwise, I don't think it will work...
|
127
|
$context['results'] = array('success' => array(), 'errors' => array());
|
128
|
$context['sandbox']['max'] = count($files);
|
129
|
$context['sandbox']['files'] = $files;
|
130
|
}
|
131
|
$files =& $context['sandbox']['files'];
|
132
|
|
133
|
// Take a cut of files. Let's do 10 at a time.
|
134
|
$import_batch_size = variable_get('media_bulk_upload_import_batch_size', 20);
|
135
|
$length = (count($files) > $import_batch_size) ? $import_batch_size : count($files);
|
136
|
$to_process = array_splice($files, 0, $length);
|
137
|
$image_in_message = '';
|
138
|
|
139
|
foreach ($to_process as $file) {
|
140
|
try {
|
141
|
$file_obj = media_parse_to_file($file, $params);
|
142
|
$context['results']['success'][] = $file;
|
143
|
if (!$image_in_message) {
|
144
|
// @todo Is this load step really necessary? When there's time, test
|
145
|
// this, and either remove it, or comment why it's needed.
|
146
|
$loaded_file = file_load($file_obj->fid);
|
147
|
$image_in_message = file_view_file($loaded_file, 'preview');
|
148
|
}
|
149
|
}
|
150
|
catch (Exception $e) {
|
151
|
$context['results']['errors'][] = $file . " Reason: " . $e->getMessage();
|
152
|
}
|
153
|
}
|
154
|
|
155
|
$context['message'] = "Importing " . theme('item_list', array('items' => $to_process));
|
156
|
// Show the image that is being imported.
|
157
|
$context['message'] .= drupal_render($image_in_message);
|
158
|
|
159
|
$context['finished'] = ($context['sandbox']['max'] - count($files)) / $context['sandbox']['max'];
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* BatchAPI complete callback for media import.
|
164
|
*/
|
165
|
function media_bulk_upload_import_batch_import_complete($success, $results, $operations) {
|
166
|
if ($results['errors']) {
|
167
|
drupal_set_message(theme('item_list', array('items' => $results['errors'])), 'error');
|
168
|
}
|
169
|
if ($results['success']) {
|
170
|
drupal_set_message(theme('item_list', array('items' => $results['success'])));
|
171
|
}
|
172
|
}
|