1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Common pages for the Media module.
|
6
|
*/
|
7
|
|
8
|
|
9
|
/**
|
10
|
* Menu callback; Edit multiple files on the same page using multiform module.
|
11
|
*
|
12
|
* @todo When http://drupal.org/node/1227706 is fixed, filter the $files
|
13
|
* array using file_access($file, 'edit').
|
14
|
*
|
15
|
* @see media_file_operation_edit_multiple()
|
16
|
*/
|
17
|
function media_file_page_edit_multiple($files) {
|
18
|
if (empty($files)) {
|
19
|
return MENU_ACCESS_DENIED;
|
20
|
}
|
21
|
|
22
|
$forms = array();
|
23
|
foreach ($files as $file) {
|
24
|
// To maintain unique form_ids, append the file id.
|
25
|
$forms[] = array('media_edit_' . $file->fid, $file);
|
26
|
}
|
27
|
|
28
|
$form = call_user_func_array('multiform_get_form', $forms);
|
29
|
$form['#attributes']['class'][] = 'media-multiedit-form';
|
30
|
|
31
|
// Add the title to each 'subform'.
|
32
|
foreach (element_children($form['multiform']) as $key) {
|
33
|
$fid = $form['multiform'][$key]['fid']['#value'];
|
34
|
$file = $files[$fid];
|
35
|
$title = t('<em>Edit @type</em> @title', array('@type' => $file->type, '@title' => $file->filename));
|
36
|
$form['multiform'][$key]['#prefix'] = '<h2>' . $title . '</h2>';
|
37
|
$form['multiform'][$key]['actions']['#access'] = FALSE;
|
38
|
}
|
39
|
|
40
|
if (isset($form['buttons']['Delete'])) {
|
41
|
$form['buttons']['Delete']['#access'] = FALSE;
|
42
|
}
|
43
|
|
44
|
// Add a cancel button at the bottom of the form.
|
45
|
$form['buttons']['cancel'] = array(
|
46
|
'#type' => 'link',
|
47
|
'#title' => t('Cancel'),
|
48
|
'#weight' => 50,
|
49
|
);
|
50
|
if (isset($_GET['destination'])) {
|
51
|
$form['buttons']['cancel']['#href'] = $_GET['destination'];
|
52
|
}
|
53
|
else if (user_access('administer files')) {
|
54
|
$form['buttons']['cancel']['#href'] = 'admin/content/file';
|
55
|
}
|
56
|
else {
|
57
|
$form['buttons']['cancel']['#href'] = '<front>';
|
58
|
}
|
59
|
|
60
|
// Override the page title since each file form sets a title.
|
61
|
drupal_set_title(t('Edit multiple files'));
|
62
|
|
63
|
// Allow other modules to alter the form.
|
64
|
drupal_alter('media_edit_multiple_form', $form);
|
65
|
|
66
|
return $form;
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* CTools modal callback for editing a file.
|
71
|
*/
|
72
|
function media_file_edit_modal($form, &$form_state, $file, $js) {
|
73
|
ctools_include('modal');
|
74
|
ctools_include('ajax');
|
75
|
|
76
|
$form_state['ajax'] = $js;
|
77
|
form_load_include($form_state, 'inc', 'file_entity', 'file_entity.pages');
|
78
|
|
79
|
$output = ctools_modal_form_wrapper('file_entity_edit', $form_state);
|
80
|
|
81
|
if ($js) {
|
82
|
$commands = $output;
|
83
|
|
84
|
if ($form_state['executed']) {
|
85
|
$commands = array(ctools_modal_command_dismiss(t('File saved')));
|
86
|
}
|
87
|
|
88
|
print ajax_render($commands);
|
89
|
exit();
|
90
|
}
|
91
|
|
92
|
// Otherwise, just return the output.
|
93
|
return $output;
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* File type migration page.
|
98
|
*
|
99
|
* Allows site administrator to execute migration of old/disabled/deleted
|
100
|
* file types to new ones.
|
101
|
*/
|
102
|
function media_upgrade_file_types($form, &$form_state) {
|
103
|
$migratable_types = _media_get_migratable_file_types();
|
104
|
|
105
|
// Silently return if there are no file types that need migration.
|
106
|
if (empty($migratable_types)) {
|
107
|
return array(
|
108
|
'message' => array(
|
109
|
'#markup' => t('There are no file types that need migration.'),
|
110
|
),
|
111
|
);
|
112
|
}
|
113
|
|
114
|
$form['message'] = array(
|
115
|
'message' => array(
|
116
|
'#markup' => t('This page allows you to migrate deprecated and/or disabled file types to new ones. It will migrate files from old type to new one and optionally migrate fields and delete old type.'),
|
117
|
),
|
118
|
);
|
119
|
|
120
|
$form['migrate_fields'] = array(
|
121
|
'#type' => 'checkbox',
|
122
|
'#title' => t('Migrate fields'),
|
123
|
'#default_value' => TRUE,
|
124
|
'#description' => t('Migrate fields and their values from old file types to new ones.'),
|
125
|
);
|
126
|
$form['delete_old_type'] = array(
|
127
|
'#type' => 'checkbox',
|
128
|
'#title' => t('Delete old type'),
|
129
|
'#default_value' => FALSE,
|
130
|
'#description' => t('Delete old file type if migration was successful and delete operation is possible (type is not exported in code).'),
|
131
|
);
|
132
|
$form['migrate_mimes'] = array(
|
133
|
'#type' => 'checkbox',
|
134
|
'#title' => t('Migrate type mime-type'),
|
135
|
'#default_value' => TRUE,
|
136
|
'#description' => t('Move mime-type from old type to new one.'),
|
137
|
);
|
138
|
|
139
|
$form['upgradable_types'] = array(
|
140
|
'#type' => 'fieldset',
|
141
|
'#title' => t('Upgradable file types'),
|
142
|
);
|
143
|
|
144
|
$options = array('- ' . t('Do not upgrade') . ' -');
|
145
|
foreach (file_type_get_enabled_types() as $type) {
|
146
|
$options[$type->type] = $type->label;
|
147
|
}
|
148
|
|
149
|
foreach ($migratable_types as $machine_name) {
|
150
|
$type = file_type_load($machine_name);
|
151
|
if (!$type) {
|
152
|
$type = new stdClass;
|
153
|
$type->label = $type->type = $machine_name;
|
154
|
}
|
155
|
$form['upgradable_types'][$machine_name] = array(
|
156
|
'#type' => 'select',
|
157
|
'#title' => $type->label,
|
158
|
'#options' => $options,
|
159
|
'#description' => t(
|
160
|
'Select file type which you want to migrate @type to. Select %no_upgrade if type should stay as it is.',
|
161
|
array('@type' => $type->label, '%no_upgrade' => '- ' . t('Do not upgrade') . ' -')),
|
162
|
);
|
163
|
}
|
164
|
|
165
|
$form['submit'] = array(
|
166
|
'#type' => 'submit',
|
167
|
'#value' => t('Start migraton'),
|
168
|
);
|
169
|
|
170
|
return $form;
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* File type migration page submit handler.
|
175
|
*/
|
176
|
function media_upgrade_file_types_submit($form, &$form_state) {
|
177
|
$migratable_types = _media_get_migratable_file_types();
|
178
|
$migrate = FALSE;
|
179
|
foreach ($migratable_types as $type) {
|
180
|
if ($form_state['values'][$type]) {
|
181
|
$migrate = TRUE;
|
182
|
break;
|
183
|
}
|
184
|
}
|
185
|
|
186
|
// Return silently if no types were selected for migration.
|
187
|
if (!$migrate) {
|
188
|
return;
|
189
|
}
|
190
|
|
191
|
// Use confirmation page/form.
|
192
|
$query = $form_state['values'];
|
193
|
unset($query['op']);
|
194
|
unset($query['submit']);
|
195
|
unset($query['form_id']);
|
196
|
unset($query['form_token']);
|
197
|
unset($query['form_build_id']);
|
198
|
|
199
|
$form_state['redirect'] = array(
|
200
|
'admin/structure/file-types/upgrade/confirm',
|
201
|
array('query' => $query),
|
202
|
);
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* File types migration confirmation page.
|
207
|
*/
|
208
|
function media_upgrade_file_types_confirm($form, &$form_state) {
|
209
|
return confirm_form(
|
210
|
$form,
|
211
|
t('Do you really want to migrate selected file types?'),
|
212
|
'admin/structure/file-types/upgrade',
|
213
|
NULL,
|
214
|
t('Migrate')
|
215
|
);
|
216
|
}
|
217
|
|
218
|
/**
|
219
|
* File types migration confirmation page sumit. Executes actual migration.
|
220
|
*/
|
221
|
function media_upgrade_file_types_confirm_submit($form, &$form_state) {
|
222
|
$migratable_types = _media_get_migratable_file_types();
|
223
|
foreach ($migratable_types as $type) {
|
224
|
if ($_GET[$type] && $bundle_new = file_type_load($_GET[$type])) {
|
225
|
// Old bundle might be deleted so let's fake some values.
|
226
|
$bundle_old = file_type_load($type);
|
227
|
if (empty($bundle_old)) {
|
228
|
$bundle_old = new stdClass;
|
229
|
$bundle_old->type = $type;
|
230
|
$bundle_old->mimetypes = array();
|
231
|
$bundle_old->export_type = 2;
|
232
|
}
|
233
|
|
234
|
// Migrate fields to new bundle.
|
235
|
if ($_GET['migrate_fields']) {
|
236
|
$old_fields = db_select('field_config_instance', 'fc')->fields('fc', array('field_name'))->condition('entity_type', 'file')->condition('bundle', $bundle_old->type)->execute()->fetchCol();
|
237
|
$new_fields = db_select('field_config_instance', 'fc')->fields('fc', array('field_name'))->condition('entity_type', 'file')->condition('bundle', $bundle_new->type)->execute()->fetchCol();
|
238
|
$fields_to_move = array_diff($old_fields, $new_fields);
|
239
|
$fields_to_drop = array_diff($old_fields, $fields_to_move);
|
240
|
|
241
|
db_update('field_config_instance')
|
242
|
->fields(array('bundle' => $bundle_new->type))
|
243
|
->condition('entity_type', 'file')
|
244
|
->condition('bundle', $bundle_old->type)
|
245
|
->condition('field_name', $fields_to_move, 'IN')
|
246
|
->execute();
|
247
|
|
248
|
db_delete('field_config_instance')
|
249
|
->condition('entity_type', 'file')
|
250
|
->condition('bundle', $bundle_old->type)
|
251
|
->condition('field_name', $fields_to_drop, 'IN')
|
252
|
->execute();
|
253
|
|
254
|
field_cache_clear();
|
255
|
module_invoke_all('field_attach_rename_bundle', 'file', $bundle_old->type, $bundle_new->type);
|
256
|
}
|
257
|
|
258
|
// Migrate mimetypes to new bundle.
|
259
|
if ($_GET['migrate_mimes']) {
|
260
|
$changed = FALSE;
|
261
|
foreach ($bundle_old->mimetypes as $mime) {
|
262
|
if (!file_entity_match_mimetypes($bundle_new->mimetypes, $mime)) {
|
263
|
$bundle_new->mimetypes[] = $mime;
|
264
|
$changed = TRUE;
|
265
|
}
|
266
|
}
|
267
|
|
268
|
if ($changed) {
|
269
|
file_type_save($bundle_new);
|
270
|
}
|
271
|
}
|
272
|
|
273
|
// Delete old bundle.
|
274
|
if ($_GET['delete_old_type'] && $bundle_old->export_type == 1) {
|
275
|
file_type_delete($bundle_old);
|
276
|
}
|
277
|
|
278
|
// Migrate files.
|
279
|
db_update('file_managed')
|
280
|
->fields(array('type' => $bundle_new->type))
|
281
|
->condition('type', $bundle_old->type)
|
282
|
->execute();
|
283
|
}
|
284
|
}
|
285
|
|
286
|
$form_state['redirect'] = 'admin/structure/file-types';
|
287
|
}
|