1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* API extensions of Drupal core's file.inc.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* The {file_managed}.type value when the file type has not yet been determined.
|
10
|
*/
|
11
|
define('FILE_TYPE_NONE', 'undefined');
|
12
|
|
13
|
/**
|
14
|
* Returns information about file formatters from hook_file_formatter_info().
|
15
|
*
|
16
|
* @param string $formatter_type
|
17
|
* (optional) A file formatter type name. If omitted, all file formatter
|
18
|
* will be returned.
|
19
|
*
|
20
|
* @return string|array
|
21
|
* Either a file formatter description, as provided by
|
22
|
* hook_file_formatter_info(), or an array of all existing file formatters,
|
23
|
* keyed by formatter type name.
|
24
|
*/
|
25
|
function file_info_formatter_types($formatter_type = NULL) {
|
26
|
$info = &drupal_static(__FUNCTION__);
|
27
|
if (!isset($info)) {
|
28
|
$info = module_invoke_all('file_formatter_info');
|
29
|
drupal_alter('file_formatter_info', $info);
|
30
|
uasort($info, '_file_entity_sort_weight_label');
|
31
|
}
|
32
|
if ($formatter_type) {
|
33
|
if (isset($info[$formatter_type])) {
|
34
|
return $info[$formatter_type];
|
35
|
}
|
36
|
}
|
37
|
else {
|
38
|
return $info;
|
39
|
}
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* Clears caches that are related to file entity.
|
44
|
*
|
45
|
* Clears all cached configuration related to file types, formatters, and
|
46
|
* display settings.
|
47
|
*/
|
48
|
function file_info_cache_clear() {
|
49
|
// Clear the CTools managed caches.
|
50
|
ctools_include('export');
|
51
|
ctools_export_load_object_reset('file_type');
|
52
|
ctools_export_load_object_reset('file_display');
|
53
|
|
54
|
// Clear the formatter type cache, managed by file_info_formatter_types().
|
55
|
drupal_static_reset('file_info_formatter_types');
|
56
|
|
57
|
// Clear file type caches.
|
58
|
drupal_static_reset('file_type_get_names');
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Construct a drupal_render() style array from an array of loaded files.
|
63
|
*
|
64
|
* @param array $files
|
65
|
* An array of files as returned by file_load_multiple().
|
66
|
* @param string $view_mode
|
67
|
* View mode.
|
68
|
* @param int $weight
|
69
|
* An integer representing the weight of the first file in the list.
|
70
|
* @param string $langcode
|
71
|
* A string indicating the language field values are to be shown in. If no
|
72
|
* language is provided the current content language is used.
|
73
|
*
|
74
|
* @return array
|
75
|
* An array in the format expected by drupal_render().
|
76
|
*/
|
77
|
function file_view_multiple($files, $view_mode = 'full', $weight = 0, $langcode = NULL) {
|
78
|
$build = array();
|
79
|
|
80
|
$entities_by_view_mode = entity_view_mode_prepare('file', $files, $view_mode, $langcode);
|
81
|
foreach ($entities_by_view_mode as $entity_view_mode => $entities) {
|
82
|
field_attach_prepare_view('file', $entities, $entity_view_mode, $langcode);
|
83
|
entity_prepare_view('file', $entities, $langcode);
|
84
|
|
85
|
foreach ($entities as $entity) {
|
86
|
$build['files'][$entity->fid] = file_view($entity, $entity_view_mode, $langcode);
|
87
|
}
|
88
|
}
|
89
|
|
90
|
foreach ($files as $file) {
|
91
|
$build['files'][$file->fid]['#weight'] = $weight;
|
92
|
$weight++;
|
93
|
}
|
94
|
// Sort here, to preserve the input order of the entities that were passed to
|
95
|
// this function.
|
96
|
uasort($build['files'], 'element_sort');
|
97
|
$build['files']['#sorted'] = TRUE;
|
98
|
|
99
|
return $build;
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Generate an array for rendering the given file.
|
104
|
*
|
105
|
* @param object $file
|
106
|
* A file object.
|
107
|
* @param string $view_mode
|
108
|
* View mode.
|
109
|
* @param string $langcode
|
110
|
* (optional) A language code to use for rendering. Defaults to the global
|
111
|
* content language of the current request.
|
112
|
*
|
113
|
* @return array
|
114
|
* An array as expected by drupal_render().
|
115
|
*/
|
116
|
function file_view($file, $view_mode = 'full', $langcode = NULL) {
|
117
|
if (!isset($langcode)) {
|
118
|
$langcode = $GLOBALS['language_content']->language;
|
119
|
}
|
120
|
|
121
|
// Populate $file->content with a render() array.
|
122
|
file_build_content($file, $view_mode, $langcode);
|
123
|
|
124
|
$build = $file->content;
|
125
|
// We don't need duplicate rendering info in $file->content.
|
126
|
unset($file->content);
|
127
|
|
128
|
$build += array(
|
129
|
'#theme' => 'file_entity',
|
130
|
'#file' => $file,
|
131
|
'#view_mode' => $view_mode,
|
132
|
'#language' => $langcode,
|
133
|
);
|
134
|
|
135
|
// Add contextual links for this file, except when the file is already being
|
136
|
// displayed on its own page. Modules may alter this behavior (for example,
|
137
|
// to restrict contextual links to certain view modes) by implementing
|
138
|
// hook_file_view_alter().
|
139
|
if (!empty($file->fid) && !($view_mode == 'full' && file_entity_is_page($file))) {
|
140
|
$build['#contextual_links']['file'] = array('file', array($file->fid));
|
141
|
}
|
142
|
|
143
|
// Allow modules to modify the structured file.
|
144
|
$type = 'file';
|
145
|
drupal_alter(array('file_view', 'entity_view'), $build, $type);
|
146
|
|
147
|
return $build;
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* Builds a structured array representing the file's content.
|
152
|
*
|
153
|
* @param object $file
|
154
|
* A file object.
|
155
|
* @param string $view_mode
|
156
|
* View mode, e.g. 'default', 'full', etc.
|
157
|
* @param string $langcode
|
158
|
* (optional) A language code to use for rendering. Defaults to the global
|
159
|
* content language of the current request.
|
160
|
*/
|
161
|
function file_build_content($file, $view_mode = 'full', $langcode = NULL) {
|
162
|
if (!isset($langcode)) {
|
163
|
$langcode = $GLOBALS['language_content']->language;
|
164
|
}
|
165
|
|
166
|
// Remove previously built content, if exists.
|
167
|
$file->content = array();
|
168
|
|
169
|
// In case of a multiple view, file_view_multiple() already ran the
|
170
|
// 'prepare_view' step. An internal flag prevents the operation from running
|
171
|
// twice.
|
172
|
// Allow modules to change the view mode.
|
173
|
$view_mode = key(entity_view_mode_prepare('file', array($file->fid => $file), $view_mode, $langcode));
|
174
|
field_attach_prepare_view('file', array($file->fid => $file), $view_mode, $langcode);
|
175
|
entity_prepare_view('file', array($file->fid => $file), $langcode);
|
176
|
|
177
|
// Build the actual file display.
|
178
|
// @todo Figure out how to clean this crap up.
|
179
|
$file->content['file'] = file_view_file($file, $view_mode, $langcode);
|
180
|
if (isset($file->content['file'])) {
|
181
|
if (isset($file->content['file']['#theme']) && $file->content['file']['#theme'] != 'file_link') {
|
182
|
unset($file->content['file']['#file']);
|
183
|
}
|
184
|
unset($file->content['file']['#view_mode']);
|
185
|
unset($file->content['file']['#language']);
|
186
|
}
|
187
|
else {
|
188
|
unset($file->content['file']);
|
189
|
}
|
190
|
|
191
|
// Build fields content.
|
192
|
$file->content += field_attach_view('file', $file, $view_mode, $langcode);
|
193
|
|
194
|
$links = array();
|
195
|
$file->content['links'] = array(
|
196
|
'#theme' => 'links__file',
|
197
|
'#pre_render' => array('drupal_pre_render_links'),
|
198
|
'#attributes' => array('class' => array('links', 'inline')),
|
199
|
);
|
200
|
$file->content['links']['file'] = array(
|
201
|
'#theme' => 'links__file__file',
|
202
|
'#links' => $links,
|
203
|
'#attributes' => array('class' => array('links', 'inline')),
|
204
|
);
|
205
|
|
206
|
// Allow modules to make their own additions to the file.
|
207
|
module_invoke_all('file_view', $file, $view_mode, $langcode);
|
208
|
module_invoke_all('entity_view', $file, 'file', $view_mode, $langcode);
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* Generate an array for rendering just the file portion of a file entity.
|
213
|
*
|
214
|
* @param object $file
|
215
|
* A file object.
|
216
|
* @param string|array $displays
|
217
|
* Can be either:
|
218
|
* - the name of a view mode;
|
219
|
* - or an array of custom display settings, as returned by file_displays().
|
220
|
* @param string $langcode
|
221
|
* (optional) A language code to use for rendering. Defaults to the global
|
222
|
* content language of the current request.
|
223
|
*
|
224
|
* @return array
|
225
|
* An array as expected by drupal_render().
|
226
|
*/
|
227
|
function file_view_file($file, $displays = 'full', $langcode = NULL) {
|
228
|
if (!isset($langcode)) {
|
229
|
$langcode = $GLOBALS['language_content']->language;
|
230
|
}
|
231
|
|
232
|
// Prepare incoming display specifications.
|
233
|
if (is_string($displays)) {
|
234
|
// Allow modules to change the view mode.
|
235
|
$view_mode = key(entity_view_mode_prepare('file', array($file->fid => $file), $displays, $langcode));
|
236
|
$displays = file_displays($file->type, $view_mode);
|
237
|
}
|
238
|
else {
|
239
|
$view_mode = '_custom_display';
|
240
|
}
|
241
|
drupal_alter('file_displays', $displays, $file, $view_mode);
|
242
|
_file_sort_array_by_weight($displays);
|
243
|
|
244
|
// Since $file->alt and $file->title were set in file_entity_file_load()
|
245
|
// (which is a language-agnostic hook) they will not be in the correct
|
246
|
// language if the file is being displayed in a language other than the
|
247
|
// default one. Set them again here, using the correct language. This must
|
248
|
// run after hook_file_displays_alter() since the Media module sets
|
249
|
// $file->alt and $file->title again during that hook.
|
250
|
if ($langcode != $GLOBALS['language_content']->language) {
|
251
|
$languages = language_list();
|
252
|
if (isset($languages[$langcode])) {
|
253
|
if (!function_exists('file_entity_set_title_alt_properties')) {
|
254
|
module_load_include('file.inc', 'file_entity');
|
255
|
}
|
256
|
file_entity_set_title_alt_properties(array($file), $languages[$langcode]);
|
257
|
}
|
258
|
}
|
259
|
|
260
|
// Since $file->alt and $file->title were set in file_entity_file_load()
|
261
|
// (which is a language-agnostic hook) they will not be in the correct
|
262
|
// language if the file is being displayed in a language other than the
|
263
|
// default one. Set them again here, using the correct language. This must
|
264
|
// run after hook_file_displays_alter() since the Media module sets
|
265
|
// $file->alt and $file->title again during that hook.
|
266
|
if ($langcode != $GLOBALS['language_content']->language) {
|
267
|
$languages = language_list();
|
268
|
if (isset($languages[$langcode])) {
|
269
|
$replace_options = array(
|
270
|
'language' => $languages[$langcode],
|
271
|
);
|
272
|
$file->title = file_entity_replace_title($file, $replace_options);
|
273
|
$file->alt = file_entity_replace_alt($file, $replace_options);
|
274
|
}
|
275
|
}
|
276
|
|
277
|
// Attempt to display the file with each of the possible displays. Stop after
|
278
|
// the first successful one. See file_displays() for details.
|
279
|
$element = NULL;
|
280
|
foreach ($displays as $formatter_type => $display) {
|
281
|
if (!empty($display['status'])) {
|
282
|
$formatter_info = file_info_formatter_types($formatter_type);
|
283
|
// Under normal circumstances, the UI prevents enabling formatters for
|
284
|
// incompatible MIME types. In case this was somehow circumvented (for
|
285
|
// example, a module updated its formatter definition without updating
|
286
|
// existing display settings), perform an extra check here.
|
287
|
if (isset($formatter_info['mime types'])) {
|
288
|
if (!file_entity_match_mimetypes($formatter_info['mime types'], $file->filemime)) {
|
289
|
continue;
|
290
|
}
|
291
|
}
|
292
|
if (isset($formatter_info['view callback']) && ($function = $formatter_info['view callback']) && function_exists($function)) {
|
293
|
$display['type'] = $formatter_type;
|
294
|
if (!empty($formatter_info['default settings'])) {
|
295
|
if (empty($display['settings'])) {
|
296
|
$display['settings'] = array();
|
297
|
}
|
298
|
$display['settings'] += $formatter_info['default settings'];
|
299
|
}
|
300
|
$element = $function($file, $display, $langcode);
|
301
|
if (isset($element)) {
|
302
|
break;
|
303
|
}
|
304
|
}
|
305
|
}
|
306
|
}
|
307
|
|
308
|
// As a last resort, fall back to showing a link to the file.
|
309
|
if (!isset($element)) {
|
310
|
$element = array(
|
311
|
'#theme' => 'file_link',
|
312
|
'#file' => $file,
|
313
|
);
|
314
|
}
|
315
|
|
316
|
// Add defaults and return the element.
|
317
|
$element += array(
|
318
|
'#file' => $file,
|
319
|
'#view_mode' => $view_mode,
|
320
|
'#language' => $langcode,
|
321
|
);
|
322
|
|
323
|
return $element;
|
324
|
}
|
325
|
|
326
|
/**
|
327
|
* @defgroup file_displays File displays API
|
328
|
* @{
|
329
|
* Functions to load and save information about file displays.
|
330
|
*/
|
331
|
|
332
|
/**
|
333
|
* Returns an array of displays to use for a file type in a given view mode.
|
334
|
*
|
335
|
* It is common for a site to be configured with broadly defined file types
|
336
|
* (e.g., 'video'), and to have different files of this type require different
|
337
|
* displays (for example, the code required to display a YouTube video is
|
338
|
* different than the code required to display a local QuickTime video).
|
339
|
* Therefore, the site administrator can configure multiple displays for a given
|
340
|
* file type. This function returns all of the displays that the administrator
|
341
|
* enabled for the given file type in the given view mode. file_view_file() then
|
342
|
* invokes each of these, and passes the specific file to display. Each display
|
343
|
* implementation can inspect the file, and either return a render array (if it
|
344
|
* is capable of displaying the file), or return nothing (if it is incapable of
|
345
|
* displaying the file). The first render array returned is the one used.
|
346
|
*
|
347
|
* @param string $file_type
|
348
|
* The type of file.
|
349
|
* @param string $view_mode
|
350
|
* The view mode.
|
351
|
*
|
352
|
* @return array
|
353
|
* An array keyed by the formatter type name. Each item in the array contains
|
354
|
* the following key/value pairs:
|
355
|
* - status: Whether this display is enabled. If not TRUE, file_view_file()
|
356
|
* skips over it.
|
357
|
* - weight: An integer that determines the order of precedence within the
|
358
|
* returned array. The lowest weight display capable of displaying the file
|
359
|
* is used.
|
360
|
* - settings: An array of key/value pairs specific to the formatter type. See
|
361
|
* hook_file_formatter_info() for details.
|
362
|
*
|
363
|
* @see hook_file_formatter_info()
|
364
|
* @see file_view_file()
|
365
|
*/
|
366
|
function file_displays($file_type, $view_mode) {
|
367
|
$cache = &drupal_static(__FUNCTION__, array());
|
368
|
|
369
|
// If the requested view mode isn't configured to use a custom display for its
|
370
|
// fields, then don't use a custom display for its file either.
|
371
|
if ($view_mode != 'default') {
|
372
|
$view_mode_settings = field_view_mode_settings('file', $file_type);
|
373
|
$view_mode = !empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default';
|
374
|
}
|
375
|
|
376
|
if (!isset($cache[$file_type][$view_mode])) {
|
377
|
// Load the display configurations for the file type and view mode. If none
|
378
|
// exist for the view mode, use the default view mode.
|
379
|
$displays = file_displays_load($file_type, $view_mode, TRUE);
|
380
|
if (empty($displays) && $view_mode != 'default') {
|
381
|
$cache[$file_type][$view_mode] = file_displays($file_type, 'default');
|
382
|
}
|
383
|
else {
|
384
|
// Convert the display objects to arrays and remove unnecessary keys.
|
385
|
foreach ($displays as $formatter_name => $display) {
|
386
|
$displays[$formatter_name] = array_intersect_key((array) $display, drupal_map_assoc(array('status', 'weight', 'settings')));
|
387
|
}
|
388
|
$cache[$file_type][$view_mode] = $displays;
|
389
|
}
|
390
|
}
|
391
|
|
392
|
return $cache[$file_type][$view_mode];
|
393
|
}
|
394
|
|
395
|
/**
|
396
|
* Returns an array of {file_display} objects for the file type and view mode.
|
397
|
*/
|
398
|
function file_displays_load($file_type, $view_mode, $key_by_formatter_name = FALSE) {
|
399
|
ctools_include('export');
|
400
|
|
401
|
$display_names = array();
|
402
|
$prefix = $file_type . '__' . $view_mode . '__';
|
403
|
foreach (array_keys(file_info_formatter_types()) as $formatter_name) {
|
404
|
$display_names[] = $prefix . $formatter_name;
|
405
|
}
|
406
|
$displays = ctools_export_load_object('file_display', 'names', $display_names);
|
407
|
|
408
|
if ($key_by_formatter_name) {
|
409
|
$prefix_length = strlen($prefix);
|
410
|
$rekeyed_displays = array();
|
411
|
foreach ($displays as $name => $display) {
|
412
|
$rekeyed_displays[substr($name, $prefix_length)] = $display;
|
413
|
}
|
414
|
$displays = $rekeyed_displays;
|
415
|
}
|
416
|
|
417
|
return $displays;
|
418
|
}
|
419
|
|
420
|
/**
|
421
|
* Saves a {file_display} object to the database.
|
422
|
*/
|
423
|
function file_display_save($display) {
|
424
|
ctools_include('export');
|
425
|
ctools_export_crud_save('file_display', $display);
|
426
|
file_info_cache_clear();
|
427
|
}
|
428
|
|
429
|
/**
|
430
|
* Creates a new {file_display} object.
|
431
|
*/
|
432
|
function file_display_new($file_type, $view_mode, $formatter_name) {
|
433
|
ctools_include('export');
|
434
|
$display = ctools_export_crud_new('file_display');
|
435
|
file_info_cache_clear();
|
436
|
$display->name = implode('__', array($file_type, $view_mode, $formatter_name));
|
437
|
return $display;
|
438
|
}
|
439
|
|
440
|
/**
|
441
|
* @} End of "defgroup file_displays".
|
442
|
*/
|
443
|
|
444
|
/**
|
445
|
* @defgroup file_types File types API
|
446
|
* @{
|
447
|
* Functions to load and save information about file types.
|
448
|
*/
|
449
|
|
450
|
/**
|
451
|
* Load a file type configuration object.
|
452
|
*
|
453
|
* @param string $name
|
454
|
* The file type machine name to load.
|
455
|
*
|
456
|
* @return object
|
457
|
* The file type object, or FALSE if it does not exist.
|
458
|
*/
|
459
|
function file_type_load($name) {
|
460
|
ctools_include('export');
|
461
|
$type = ctools_export_crud_load('file_type', $name);
|
462
|
return isset($type) ? $type : FALSE;
|
463
|
}
|
464
|
|
465
|
/**
|
466
|
* Load multiple file type configuration objects.
|
467
|
*
|
468
|
* @param array $names
|
469
|
* An array of file type machine names to load.
|
470
|
*
|
471
|
* @return array
|
472
|
* An array of file type objects, keyed by machine name.
|
473
|
*/
|
474
|
function file_type_load_multiple(array $names) {
|
475
|
ctools_include('export');
|
476
|
return ctools_export_crud_load_multiple('file_type', $names);
|
477
|
}
|
478
|
|
479
|
/**
|
480
|
* Load all file type configuration objects.
|
481
|
*
|
482
|
* This includes all enabled and disabled file types.
|
483
|
*
|
484
|
* @param bool $reset
|
485
|
* If TRUE, the static cache of all file types will be flushed prior to
|
486
|
* loading. This can be important on listing pages where file types might
|
487
|
* have changed on the page load.
|
488
|
*
|
489
|
* @return array
|
490
|
* An array of file type objects, keyed by machine name.
|
491
|
*
|
492
|
* @see file_type_get_enabled_types()
|
493
|
* @see file_type_get_disabled_types()
|
494
|
*/
|
495
|
function file_type_load_all($reset = FALSE) {
|
496
|
ctools_include('export');
|
497
|
return ctools_export_crud_load_all('file_type', $reset);
|
498
|
}
|
499
|
|
500
|
/**
|
501
|
* Returns an array of enabled file types.
|
502
|
*/
|
503
|
function file_type_get_enabled_types() {
|
504
|
$types = file_type_load_all();
|
505
|
return array_filter($types, 'file_type_is_enabled');
|
506
|
}
|
507
|
|
508
|
/**
|
509
|
* Returns an array of disabled file types.
|
510
|
*/
|
511
|
function file_type_get_disabled_types() {
|
512
|
$types = file_type_load_all();
|
513
|
return array_filter($types, 'file_type_is_disabled');
|
514
|
}
|
515
|
|
516
|
/**
|
517
|
* Returns TRUE if a file type is enabled, FALSE otherwise.
|
518
|
*/
|
519
|
function file_type_is_enabled($type) {
|
520
|
return empty($type->disabled);
|
521
|
}
|
522
|
|
523
|
/**
|
524
|
* Returns TRUE if a file type is disabled, FALSE otherwise.
|
525
|
*/
|
526
|
function file_type_is_disabled($type) {
|
527
|
return !empty($type->disabled);
|
528
|
}
|
529
|
|
530
|
/**
|
531
|
* Returns an array of valid file extensions.
|
532
|
*/
|
533
|
function file_type_get_valid_extensions($type) {
|
534
|
include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc';
|
535
|
$mapping = file_mimetype_mapping();
|
536
|
|
537
|
$type_extensions = array();
|
538
|
$type_ext_keys = array();
|
539
|
if (!empty($type->mimetypes)) {
|
540
|
foreach ($mapping['mimetypes'] as $ext_key => $mimetype) {
|
541
|
if (file_entity_match_mimetypes($mimetype, $type->mimetypes)) {
|
542
|
$type_ext_keys[] = $ext_key;
|
543
|
}
|
544
|
}
|
545
|
|
546
|
if ($type_ext_keys) {
|
547
|
$type_extensions = array_intersect($mapping['extensions'], $type_ext_keys);
|
548
|
$type_extensions = array_keys($type_extensions);
|
549
|
}
|
550
|
}
|
551
|
|
552
|
return $type_extensions;
|
553
|
}
|
554
|
|
555
|
/**
|
556
|
* Updates an existing file type or creates a new one.
|
557
|
*
|
558
|
* This function can be called on its own, or via the CTools exportables
|
559
|
* 'save callback' for {file_type} objects.
|
560
|
*/
|
561
|
function file_type_save($type) {
|
562
|
// Get the old type object, so we now can issue the correct insert/update
|
563
|
// queries.
|
564
|
if (!empty($type->old_type) && $type->old_type != $type->type) {
|
565
|
$rename_bundle = TRUE;
|
566
|
$old_type = file_type_load($type->old_type);
|
567
|
}
|
568
|
else {
|
569
|
$rename_bundle = FALSE;
|
570
|
$old_type = file_type_load($type->type);
|
571
|
}
|
572
|
|
573
|
// The type and label fields are required, but description is optional.
|
574
|
if (!isset($type->description)) {
|
575
|
$type->description = '';
|
576
|
}
|
577
|
$fields = array(
|
578
|
'type' => $type->type,
|
579
|
'label' => $type->label,
|
580
|
'description' => $type->description,
|
581
|
'mimetypes' => serialize($type->mimetypes),
|
582
|
);
|
583
|
|
584
|
// Update an existing type object, whether with a modified 'type' property or
|
585
|
// not.
|
586
|
if ($old_type) {
|
587
|
if ($old_type->export_type & EXPORT_IN_DATABASE) {
|
588
|
db_update('file_type')
|
589
|
->fields($fields)
|
590
|
->condition('type', $old_type->type)
|
591
|
->execute();
|
592
|
}
|
593
|
else {
|
594
|
db_insert('file_type')
|
595
|
->fields($fields)
|
596
|
->execute();
|
597
|
}
|
598
|
if ($rename_bundle) {
|
599
|
field_attach_rename_bundle('file', $old_type->type, $type->type);
|
600
|
}
|
601
|
module_invoke_all('file_type_update', $type);
|
602
|
$status = SAVED_UPDATED;
|
603
|
}
|
604
|
// Insert a new type object.
|
605
|
else {
|
606
|
db_insert('file_type')
|
607
|
->fields($fields)
|
608
|
->execute();
|
609
|
field_attach_create_bundle('file', $type->type);
|
610
|
module_invoke_all('file_type_insert', $type);
|
611
|
$status = SAVED_NEW;
|
612
|
}
|
613
|
|
614
|
// Clear the necessary caches.
|
615
|
file_info_cache_clear();
|
616
|
|
617
|
// Ensure the type has the correct export_type in case the $type parameter
|
618
|
// continues to be used by the calling function after this function completes.
|
619
|
if (empty($type->export_type)) {
|
620
|
$type->export_type = 0;
|
621
|
}
|
622
|
$type->export_type |= EXPORT_IN_DATABASE;
|
623
|
|
624
|
return $status;
|
625
|
}
|
626
|
|
627
|
/**
|
628
|
* Deletes a file type from the database.
|
629
|
*
|
630
|
* This function can be called on its own, or via the CTools exportables
|
631
|
* 'delete callback' for {file_type} objects.
|
632
|
*
|
633
|
* @param object|string $type
|
634
|
* Either a loaded file type object or the machine-name of the type.
|
635
|
*/
|
636
|
function file_type_delete($type) {
|
637
|
$type = is_string($type) ? file_type_load($type) : $type;
|
638
|
|
639
|
db_delete('file_type')
|
640
|
->condition('type', $type->type)
|
641
|
->execute();
|
642
|
|
643
|
// Remove this type from CToolS status variable.
|
644
|
$status = variable_get('default_file_type', array());
|
645
|
unset($status[$type->type]);
|
646
|
variable_set('default_file_type', $status);
|
647
|
|
648
|
file_info_cache_clear();
|
649
|
|
650
|
// After deleting from the database, check if the type still exists as a
|
651
|
// code-provided default type. If not, consider the type fully deleted and
|
652
|
// invoke the needed hooks.
|
653
|
if (!file_type_load($type->type)) {
|
654
|
field_attach_delete_bundle('file', $type->type);
|
655
|
module_invoke_all('file_type_delete', $type);
|
656
|
}
|
657
|
}
|
658
|
|
659
|
|
660
|
/**
|
661
|
* Enable a file type.
|
662
|
*
|
663
|
* @param string $type
|
664
|
* Type of the file_type to disable
|
665
|
*/
|
666
|
function file_type_enable($type) {
|
667
|
ctools_include('export');
|
668
|
ctools_export_crud_enable('file_type', $type);
|
669
|
}
|
670
|
|
671
|
|
672
|
/**
|
673
|
* Disable a file type.
|
674
|
*
|
675
|
* @param string $type
|
676
|
* Type of the file_type to disable
|
677
|
*/
|
678
|
function file_type_disable($type) {
|
679
|
ctools_include('export');
|
680
|
ctools_export_crud_disable('file_type', $type);
|
681
|
}
|
682
|
|
683
|
|
684
|
/**
|
685
|
* Determines file type for a given file.
|
686
|
*
|
687
|
* @param object $file
|
688
|
* File object.
|
689
|
*
|
690
|
* @return string
|
691
|
* Machine name of file type that should be used for given file.
|
692
|
*/
|
693
|
function file_get_type($file) {
|
694
|
$types = module_invoke_all('file_type', $file);
|
695
|
drupal_alter('file_type', $types, $file);
|
696
|
|
697
|
return empty($types) ? NULL : reset($types);
|
698
|
}
|
699
|
|
700
|
/**
|
701
|
* @} End of "defgroup file_types".
|
702
|
*/
|
703
|
|
704
|
/**
|
705
|
* Sorts an array by weight.
|
706
|
*
|
707
|
* Helper function to sort an array by the value of each item's 'weight' key,
|
708
|
* while preserving relative order of items that have equal weight.
|
709
|
*/
|
710
|
function _file_sort_array_by_weight(&$a) {
|
711
|
$i = 0;
|
712
|
foreach ($a as $key => $item) {
|
713
|
if (!isset($a[$key]['weight'])) {
|
714
|
$a[$key]['weight'] = 0;
|
715
|
}
|
716
|
$original_weight[$key] = $a[$key]['weight'];
|
717
|
$a[$key]['weight'] += $i / 1000;
|
718
|
$i++;
|
719
|
}
|
720
|
uasort($a, 'drupal_sort_weight');
|
721
|
foreach ($a as $key => $item) {
|
722
|
$a[$key]['weight'] = $original_weight[$key];
|
723
|
}
|
724
|
}
|
725
|
|
726
|
/**
|
727
|
* User sort function to sort by weight, then label/name.
|
728
|
*/
|
729
|
function _file_entity_sort_weight_label($a, $b) {
|
730
|
$a_weight = isset($a['weight']) ? $a['weight'] : 0;
|
731
|
$b_weight = isset($b['weight']) ? $b['weight'] : 0;
|
732
|
if ($a_weight == $b_weight) {
|
733
|
$a_label = isset($a['label']) ? $a['label'] : '';
|
734
|
$b_label = isset($b['label']) ? $b['label'] : '';
|
735
|
return strcasecmp($a_label, $b_label);
|
736
|
}
|
737
|
else {
|
738
|
return $a_weight < $b_weight ? -1 : 1;
|
739
|
}
|
740
|
}
|
741
|
|
742
|
/**
|
743
|
* Returns a file object which can be passed to file_save().
|
744
|
*
|
745
|
* @param string $uri
|
746
|
* A string containing the URI, path, or filename.
|
747
|
* @param bool $use_existing
|
748
|
* (Optional) If TRUE and there's an existing file in the {file_managed}
|
749
|
* table with the passed in URI, then that file object is returned.
|
750
|
* Otherwise, a new file object is returned. Default is TRUE.
|
751
|
*
|
752
|
* @return object|bool
|
753
|
* A file object, or FALSE on error.
|
754
|
*
|
755
|
* @todo This should probably be named
|
756
|
* file_load_by_uri($uri, $create_if_not_exists).
|
757
|
* @todo Remove this function when http://drupal.org/node/685818 is fixed.
|
758
|
*/
|
759
|
function file_uri_to_object($uri, $use_existing = TRUE) {
|
760
|
$file = FALSE;
|
761
|
$uri = file_stream_wrapper_uri_normalize($uri);
|
762
|
|
763
|
if ($use_existing) {
|
764
|
// We should always attempt to re-use a file if possible.
|
765
|
$files = entity_load('file', FALSE, array('uri' => $uri));
|
766
|
$file = !empty($files) ? reset($files) : FALSE;
|
767
|
}
|
768
|
|
769
|
if (empty($file)) {
|
770
|
$file = new stdClass();
|
771
|
$file->uid = $GLOBALS['user']->uid;
|
772
|
$file->filename = drupal_basename($uri);
|
773
|
$file->uri = $uri;
|
774
|
$file->filemime = file_get_mimetype($uri);
|
775
|
// This is gagged because some uris will not support it.
|
776
|
$file->filesize = @filesize($uri);
|
777
|
$file->timestamp = REQUEST_TIME;
|
778
|
$file->status = FILE_STATUS_PERMANENT;
|
779
|
}
|
780
|
|
781
|
return $file;
|
782
|
}
|
783
|
|
784
|
/**
|
785
|
* Delete multiple files.
|
786
|
*
|
787
|
* Unlike core's file_delete(), this function does not care about file usage
|
788
|
* or skip on invalid URIs. Just deletes the damn file like it should.
|
789
|
*
|
790
|
* @param array $fids
|
791
|
* An array of file IDs.
|
792
|
*/
|
793
|
function file_delete_multiple(array $fids) {
|
794
|
$transaction = db_transaction();
|
795
|
if (!empty($fids) && $files = file_load_multiple($fids)) {
|
796
|
try {
|
797
|
foreach ($files as $fid => $file) {
|
798
|
module_invoke_all('file_delete', $file);
|
799
|
module_invoke_all('entity_delete', $file, 'file');
|
800
|
// Skip calling field_attach_delete as file_entity_file_delete()
|
801
|
// does this.
|
802
|
// field_attach_delete('file', $file);
|
803
|
// Remove this file from the search index if needed.
|
804
|
// This code is implemented in file_entity module rather than in search
|
805
|
// module, because node module is implementing search module's API,
|
806
|
// not the other way around.
|
807
|
if (module_exists('search')) {
|
808
|
search_reindex($fid, 'file');
|
809
|
}
|
810
|
|
811
|
// Make sure the file is deleted before removing its row from the
|
812
|
// database, so UIs can still find the file in the database.
|
813
|
if (file_valid_uri($file->uri)) {
|
814
|
file_unmanaged_delete($file->uri);
|
815
|
}
|
816
|
}
|
817
|
|
818
|
db_delete('file_managed')
|
819
|
->condition('fid', $fids, 'IN')
|
820
|
->execute();
|
821
|
db_delete('file_usage')
|
822
|
->condition('fid', $fids, 'IN')
|
823
|
->execute();
|
824
|
}
|
825
|
catch (Exception $e) {
|
826
|
$transaction->rollback();
|
827
|
watchdog_exception('file', $e);
|
828
|
throw $e;
|
829
|
}
|
830
|
|
831
|
// Clear the page and block and file_load_multiple caches.
|
832
|
entity_get_controller('file')->resetCache();
|
833
|
}
|
834
|
}
|