1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* This file contains the admin functions for the Media module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Include media.pages.inc since it has some form definitions we will use.
|
10
|
*/
|
11
|
require_once dirname(__FILE__) . '/media.pages.inc';
|
12
|
|
13
|
/**
|
14
|
* Form callback for mass import.
|
15
|
*/
|
16
|
function media_import($form, &$form_state) {
|
17
|
if (!isset($form_state['storage']['files'])) {
|
18
|
$form_state['storage']['step'] = 'choose';
|
19
|
$form_state['storage']['next_step'] = 'preview';
|
20
|
$form['directory'] = array(
|
21
|
'#type' => 'textfield',
|
22
|
'#title' => t('Directory'),
|
23
|
'#description' => t('Enter the absolute directory on the web server to look for files. Subdirectories inside this directory will not be scanned.'),
|
24
|
'#required' => TRUE,
|
25
|
);
|
26
|
|
27
|
$form['pattern'] = array(
|
28
|
'#type' => 'textarea',
|
29
|
'#title' => t('Pattern'),
|
30
|
'#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')),
|
31
|
'#default_value' => '*',
|
32
|
'#required' => TRUE,
|
33
|
);
|
34
|
|
35
|
$form['actions'] = array('#type' => 'actions');
|
36
|
$form['actions']['submit'] = array(
|
37
|
'#type' => 'submit',
|
38
|
'#value' => t('Preview'),
|
39
|
);
|
40
|
$form['actions']['cancel'] = array(
|
41
|
'#type' => 'link',
|
42
|
'#title' => t('Cancel'),
|
43
|
'#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/content/file',
|
44
|
);
|
45
|
}
|
46
|
else {
|
47
|
$form['preview'] = array(
|
48
|
'#markup' => theme('item_list', array('items' => $form_state['storage']['files'])),
|
49
|
);
|
50
|
|
51
|
$form = confirm_form($form, t('Import these files?'), 'admin/content/file/import');
|
52
|
}
|
53
|
return $form;
|
54
|
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Validate handler for media_import().
|
59
|
*/
|
60
|
function media_import_validate($form, &$form_state) {
|
61
|
if ($form_state['values']['op'] != t('Confirm')) {
|
62
|
$directory = $form_state['values']['directory'];
|
63
|
$pattern = $form_state['values']['pattern'];
|
64
|
if (!is_dir($directory)) {
|
65
|
form_set_error('directory', t('The provided directory does not exist.'));
|
66
|
}
|
67
|
if (!is_readable($directory)) {
|
68
|
form_set_error('directory', t('The provided directory is not readable.'));
|
69
|
}
|
70
|
|
71
|
$pattern_quoted = preg_quote($pattern, '/');
|
72
|
$pattern_quoted = preg_replace('/(\r\n?|\n)/', '|', $pattern_quoted);
|
73
|
$pattern_quoted = strtr($pattern_quoted, array(
|
74
|
'\\|' => '|',
|
75
|
'\\*' => '.*',
|
76
|
'\\?' => '.?',
|
77
|
));
|
78
|
$files = file_scan_directory($directory, '/^(' . $pattern_quoted . ')$/', array('recurse' => FALSE));
|
79
|
$files = array_keys($files);
|
80
|
if (empty($files)) {
|
81
|
form_set_error('pattern', t('No files were found in %directory matching the regular expression %pattern', array('%directory' => $directory, '%pattern' => $pattern_quoted)));
|
82
|
}
|
83
|
$form_state['storage']['files'] = $files;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Submit handler for media_import().
|
89
|
*/
|
90
|
function media_import_submit($form, &$form_state) {
|
91
|
if ($form_state['values']['op'] == t('Confirm')) {
|
92
|
$files = $form_state['storage']['files'];
|
93
|
$batch = array(
|
94
|
'title' => t('Importing'),
|
95
|
'operations' => array(
|
96
|
array('media_import_batch_import_files', array($files)),
|
97
|
),
|
98
|
'finished' => 'media_import_batch_import_complete',
|
99
|
'file' => drupal_get_path('module', 'media') . '/includes/media.admin.inc',
|
100
|
);
|
101
|
batch_set($batch);
|
102
|
return;
|
103
|
|
104
|
}
|
105
|
$form_state['rebuild'] = TRUE;
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* BatchAPI callback op for media import.
|
110
|
*/
|
111
|
function media_import_batch_import_files($files, &$context) {
|
112
|
if (!isset($context['sandbox']['files'])) {
|
113
|
// This runs the first time the batch runs.
|
114
|
// This is stupid, but otherwise, I don't think it will work...
|
115
|
$context['results'] = array('success' => array(), 'errors' => array());
|
116
|
$context['sandbox']['max'] = count($files);
|
117
|
$context['sandbox']['files'] = $files;
|
118
|
}
|
119
|
$files =& $context['sandbox']['files'];
|
120
|
|
121
|
// Take a cut of files. Let's do 10 at a time.
|
122
|
$import_batch_size = variable_get('media__import_batch_size', 20);
|
123
|
$length = (count($files) > $import_batch_size) ? $import_batch_size : count($files);
|
124
|
$to_process = array_splice($files, 0, $length);
|
125
|
$image_in_message = '';
|
126
|
|
127
|
foreach ($to_process as $file) {
|
128
|
try {
|
129
|
$file_obj = media_parse_to_file($file);
|
130
|
$context['results']['success'][] = $file;
|
131
|
if (!$image_in_message) {
|
132
|
// @todo Is this load step really necessary? When there's time, test
|
133
|
// this, and either remove it, or comment why it's needed.
|
134
|
$loaded_file = file_load($file_obj->fid);
|
135
|
$image_in_message = file_view_file($loaded_file, 'preview');
|
136
|
}
|
137
|
}
|
138
|
catch (Exception $e) {
|
139
|
$context['results']['errors'][] = $file . " Reason: " . $e->getMessage();
|
140
|
}
|
141
|
}
|
142
|
|
143
|
$context['message'] = "Importing " . theme('item_list', array('items' => $to_process));
|
144
|
// Show the image that is being imported.
|
145
|
$context['message'] .= drupal_render($image_in_message);
|
146
|
|
147
|
$context['finished'] = ($context['sandbox']['max'] - count($files)) / $context['sandbox']['max'];
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* BatchAPI complete callback for media import.
|
152
|
*/
|
153
|
function media_import_batch_import_complete($success, $results, $operations) {
|
154
|
if ($results['errors']) {
|
155
|
drupal_set_message(theme('item_list', array('items' => $results['errors'])), 'error');
|
156
|
}
|
157
|
if ($results['success']) {
|
158
|
drupal_set_message(theme('item_list', array('items' => $results['success'])));
|
159
|
}
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* Admin configruation form for media browser settings.
|
164
|
*/
|
165
|
function media_admin_config_browser($form, &$form_state) {
|
166
|
$theme_options = array();
|
167
|
$theme_options[NULL] = 'Default administration theme';
|
168
|
foreach (list_themes() as $key => $theme) {
|
169
|
if ($theme->status) {
|
170
|
$theme_options[$key] = $theme->info['name'];
|
171
|
}
|
172
|
}
|
173
|
|
174
|
$form['media__dialog_theme'] = array(
|
175
|
'#type' => 'select',
|
176
|
'#title' => t('Media browser theme'),
|
177
|
'#options' => $theme_options,
|
178
|
'#description' => t("This theme will be used for all media related dialogs.
|
179
|
It can be different from your site's theme because many site themes do not
|
180
|
work well in the small windows which media uses."),
|
181
|
'#default_value' => variable_get('media__dialog_theme', ''),
|
182
|
);
|
183
|
|
184
|
// Additional configuration if the WYSIWYG module is enabled.
|
185
|
$form['wysiwyg'] = array(
|
186
|
'#type' => 'fieldset',
|
187
|
'#title' => t('WYSIWYG configuration'),
|
188
|
'#collapsible' => TRUE,
|
189
|
'#collapsed' => FALSE,
|
190
|
'#access' => module_exists('wysiwyg'),
|
191
|
);
|
192
|
$plugins = media_get_browser_plugin_info();
|
193
|
$form['wysiwyg']['media__wysiwyg_browser_plugins'] = array(
|
194
|
'#type' => 'checkboxes',
|
195
|
'#title' => t('Enabled browser plugins'),
|
196
|
'#options' => array(),
|
197
|
'#required' => FALSE,
|
198
|
'#default_value' => variable_get('media__wysiwyg_browser_plugins', array()),
|
199
|
'#description' => t('If no plugins are selected, they will all be available.'),
|
200
|
);
|
201
|
foreach ($plugins as $key => $plugin) {
|
202
|
$form['wysiwyg']['media__wysiwyg_browser_plugins']['#options'][$key] = !empty($plugin['title']) ? $plugin['title'] : $key;
|
203
|
}
|
204
|
$form['wysiwyg']['media__wysiwyg_upload_directory'] = array(
|
205
|
'#type' => 'textfield',
|
206
|
'#title' => t("File directory for uploaded media"),
|
207
|
'#default_value' => variable_get('media__wysiwyg_upload_directory', ''),
|
208
|
'#description' => t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'),
|
209
|
);
|
210
|
|
211
|
if (module_exists('token')) {
|
212
|
$form['wysiwyg']['media__wysiwyg_upload_directory']['#description'] .= t('This field supports tokens.');
|
213
|
$form['wysiwyg']['tokens'] = array(
|
214
|
'#theme' => 'token_tree',
|
215
|
'#dialog' => TRUE,
|
216
|
);
|
217
|
}
|
218
|
|
219
|
$form['wysiwyg']['media__wysiwyg_allowed_types'] = array(
|
220
|
'#type' => 'checkboxes',
|
221
|
'#title' => t('Allowed types in WYSIWYG'),
|
222
|
'#options' => file_entity_type_get_names(),
|
223
|
'#default_value' => variable_get('media__wysiwyg_allowed_types', array('audio', 'image', 'video', 'document')),
|
224
|
);
|
225
|
$form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
|
226
|
|
227
|
$form['#submit'][] = 'media_admin_config_browser_pre_submit';
|
228
|
return system_settings_form($form);
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* Manipulate values before form is submitted.
|
233
|
*/
|
234
|
function media_admin_config_browser_pre_submit(&$form, &$form_state) {
|
235
|
if (!$form_state['values']['media__dialog_theme']) {
|
236
|
variable_del('media__dialog_theme');
|
237
|
unset($form_state['values']['media__dialog_theme']);
|
238
|
}
|
239
|
$wysiwyg_browser_plugins = array_unique(array_values($form_state['values']['media__wysiwyg_browser_plugins']));
|
240
|
if (empty($wysiwyg_browser_plugins[0])) {
|
241
|
variable_del('media__wysiwyg_browser_plugins');
|
242
|
unset($form_state['values']['media__wysiwyg_browser_plugins']);
|
243
|
}
|
244
|
}
|