/
media_browser_plus.module
2103 lines (2011 loc) · 72.3 KB
/
media_browser_plus.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Adds fields to the media browser forms for better UX
*/
/* ***************************************** */
/* INCLUDES */
/* ***************************************** */
// A registry of variable_get defaults.
require_once ( dirname(__FILE__) . '/includes/media_browser_plus.variables.inc');
/**
* Alter the media browser from to enable new UI
*/
function media_browser_plus_form_media_admin_alter(&$form, &$form_state) {
$path = drupal_get_path('module', 'media_browser_plus');
// add css for both views
$form['#attached']['library'][] = array('media_browser_plus', 'media_browser_plus');
$form['#attached']['library'][] = array('media_browser_plus', 'colorbox');
// set base href for javascript requests
$settings = media_browser_plus_main_view_javascript_settings();
//
$form['#attached']['js'][] = array(
'type' => 'setting',
'data' => $settings,
);
// check which mode we are in
if (isset($form_state['build_info']['args'][0]) && $form_state['build_info']['args'][0] != 'thumbnails') {
return _media_browser_plus_media_admin_list_alter($form, $form_state);
}
// check access rights
if (!media_browser_plus_access('media grid view')) {
drupal_access_denied();
return array();
}
// adding grid view js and css
$form['#attached']['js'][] = $path . '/js/media_browser_plus.admin.js';
drupal_add_library('system', 'ui.draggable');
drupal_add_library('system', 'ui.droppable');
// removing options form part
unset($form['options']);
if (!media_browser_plus_access('media list view') ||
!media_browser_plus_access('media grid view'))
unset($form['switch']);
//
$form['admin'] = media_browser_plus_grid_view_form();
$form['media-basket'] = media_browser_plus_media_basket_form();
//
if (media_access('preview')) {
$form['media-preview'] = media_browser_plus_media_preview_form();
}
return $form;
}
/**
* Enter description here
*
* @param $form
* @param $form_state
*/
function media_browser_plus_media_admin_validate($form, &$form_state) {
//copy input files to values
if ($form_state['values']['op'] != "Download") {
$form_state['values']['files'] = $form_state['input']['files'];
// and call the old validate
media_admin_validate($form, $form_state);
}
}
/**
* Enter description here
*
* @param $form
* @param $form_state
*/
function media_browser_plus_edit_multiple_redirect($form, &$form_state) {
$fids = implode(' ', array_keys(array_filter($form_state['values']['files'])));
$form_state['redirect'] = array(str_replace('%fids', $fids, 'admin/content/media/edit_multiple/%fids'), array('query' => array('destination' => 'admin/content/media/thumbnails')));
}
/**
* Enter description here
*
* @param $form
* @param $form_state
*/
function media_browser_plus_delete_multiple_redirect($form, &$form_state) {
$fids = implode(' ', array_keys(array_filter($form_state['values']['files'])));
$form_state['redirect'] = array(str_replace('%fids', $fids, 'admin/content/media/delete_multiple/%fids'), array('query' => array('destination' => 'admin/content/media/thumbnails')));
}
/**
* Puts all selected media items into a zip archive and returns the archive
* @TODO: check for internet sources etc. Only local files should be parsed here
*
* @param $form
* @param $form_state
*/
function media_browser_plus_download_images_submit($form, &$form_state) {
if (isset($form_state['input']['selected_media']) && media_access('download')) {
$ids = array_keys($form_state['input']['selected_media']);
// only load those
$conditions[] = array("property" => array("fid", array($ids), "IN"));
$media_entities = media_browser_plus_load_multiple(array('conditions' => $conditions, 'apply_filter' => FALSE, 'paging' => FALSE));
// create archive
apache_setenv('no-gzip', '1');
//
$name = 'media-download-' . md5(microtime() . uniqid());
$zipFile = '/tmp/' . $name . '.zip';
$zip = new ZipArchive;
$res = $zip->open($zipFile, ZipArchive::CREATE);
if ($res === TRUE && count($media_entities->results)) {
foreach ($media_entities->results as $media) {
$zip->addFile(drupal_realpath($media->uri), $media->filename);
}
$zip->close();
/* OLD
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$zipFile");
header("Content-Length: " . filesize($zipFile));
header("Pragma: no-cache");
header("Expires: 0");
*/
header("Cache-Control: public");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
//header("Content-Description: File Transfer");
//header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$zipFile");
//header("Content-Transfer-Encoding: binary");
header("Content-length: " . filesize($zipFile));
readfile($zipFile);
unlink($zipFile);
die();
}
else {
drupal_set_message(t('Failed to create download archive'), 'error');
}
}
}
/**
* used to alter the media list display
*/
function _media_browser_plus_media_admin_list_alter(&$form, &$form_state) {
// check access rights
if (!media_browser_plus_access('media list view')) {
drupal_access_denied();
return array();
}
$header = array(
'title' => array(
'data' => t('Title'),
'specifier' => 'filename',
'type' => 'property',
'field' => 'f.filename'
),
'type' => array(
'data' => t('Type'),
'specifier' => 'filemime',
'type' => 'property',
'field' => 'f.filemime'
),
'folder' => array(
'data' => t('Folder'),
'specifier' => array(
'field' => 'field_folder',
'column' => 'tid'),
'type' => 'field'
),
'size' => array(
'data' => t('Size'),
'specifier' => 'filesize',
'type' => 'property',
'field' => 'f.filesize'
),
'author' => array(
'data' => t('Author'),
'specifier' => 'uid',
'type' => 'property',
'field' => 'u.name'
),
'timestamp' => array(
'data' => t('Updated'),
'specifier' => 'timestamp',
'type' => 'property',
'sort' => 'desc',
'field' => 'f.timestamp'
),
'operations' => array(
'data' => t('Operations')
),
);
$destination = drupal_get_destination();
$options = array();
$media_entities = media_browser_plus_load_multiple(array("conditions" => array(), "header" => $header));
// gather the data from each media object
foreach ($media_entities->results as $media) {
// load folder name
$folder = "";
if (isset($media->field_folder[LANGUAGE_NONE][0]['tid']) &&
($term = taxonomy_term_load($media->field_folder[LANGUAGE_NONE][0]['tid']))) {
$folder = $term->name;
}
$options[$media->fid] = array(
'title' => theme('media_link', array('file' => $media)),
'folder' => $folder,
'type' => check_plain($media->filemime),
'size' => format_size($media->filesize),
'author' => theme('username', array('account' => $media)),
'timestamp' => format_date($media->timestamp, 'short'),
);
$options[$media->fid]['operations'] = l(t('edit'), 'media/' . $media->fid . '/edit', array('query' => $destination));
}
// grab old options
$ops = $form['options']['operation']['#options'];
// redo the options form
unset($form['options']);
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Operations'),
'#weight' => -10,
'operations' => array(
'#type' => 'container',
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
'operation' => array(
'#type' => 'select',
'#options' => $ops,
'#default_value' => 'delete',
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('media_admin_submit'),
'#validate' => array('media_admin_validate')
)
),
'folder' => array(
'#type' => 'container',
'selected_folder' => array(
'#type' => 'select',
'#title' => t('Media Folder*'),
'#required' => FALSE,
'#options' => _media_browser_plus_folder_list()
),
'submit' => array(
'#type' => 'container',
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
'submit' => array(
'#type' => 'submit',
'#value' => t('Change Folder'),
'#submit' => array('media_browser_plus_media_admin_folder_change_submit'),
'#validate' => array('media_admin_validate')
),
'notice' => array(
'#type' => 'item',
'#markup' => t('*: change will be applied to ALL selected media files')
),
)
)
);
$form['admin']['files']['#header'] = $header;
$form['admin']['files']['#options'] = $options;
// reset pager
unset($form['admin']['pager']);
$form['admin']['pager'] = array('#markup' => _media_browser_plus_pager($media_entities->page, $media_entities->pages));
//
return $form;
}
/**
* function called by the JS Fronted via AJAX Request to change the folder of a media object
*/
function media_browser_plus_change_folder($form, &$form_state) {
// parse values
$folder = (int) (str_replace("folder_load_", "", $form_state['input']['folder']));
$media = file_load((int) (str_replace("edit-files-", "", $form_state['input']['media'])));
// apply new folder
if (isset($media->field_folder[LANGUAGE_NONE][0]['tid'])) {
$media->field_folder[LANGUAGE_NONE][0]['tid'] = $folder;
}
else {
$media->field_folder = array(LANGUAGE_NONE => array(array('tid' => $folder)));
}
// save changes
media_browser_plus_file_move($folder, $media);
die("");
return $form;
}
/**
* function called by the JS Fronted via AJAX Request to get the media list for a given folder
*/
function media_browser_plus_thumbnailsJSON() {
if (isset($_GET['folder'])) {
$folder = (int) (str_replace("folder_load_", "", $_GET['folder']));
// create conditions
$conditions = array();
// check for filter set by library
if (isset($_GET['filter'])) {
$filter = drupal_json_decode($_GET['filter']);
// bugfix - $conditions = $filter;
foreach ($filter as $key => $value) {
// checking each filter
$valid = TRUE;
foreach ($value as $type => $params) {
foreach ($params as $param) {
if (strlen(trim($param)) == 0) {
$valid = FALSE;
break;
}
}
}
if ($valid) {
$conditions[] = $value;
}
}
}
// more conditions
$conditions[] = array("field" => array("field_folder", "tid", array($folder), "IN"));
$order = array(array("property" => array("fid", "DESC")));
$media_entities = media_browser_plus_load_multiple(array("conditions" => $conditions, "order" => $order));
module_load_include('inc', 'media', 'includes/media.browser');
foreach ($media_entities->results as $media) {
media_browser_build_media_item($media);
}
$output = array(
'media' => array_values($media_entities->results),
'folder_loaded' => 'folder_load_' . $folder,
'overall_count' => $media_entities->overall_count,
);
drupal_json_output($output);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function media_browser_plus_form_media_add_upload_multiple_alter(&$form, &$form_state) {
// This builds the tags textfield and adds the autocomplete handlers to it.
// The #element_validate may not be necessary because this never triggers
// hook_field_update() or hook_field_insert()
$form['field_tags'] = _media_browser_plus_tag_form();
$form['field_folder'] = _media_browser_plus_folder_form();
// Change the weight of the other form items so they appear in the right order
$form['upload']['#weight'] = 0;
$form['submit']['#weight'] = 2;
// Add an additional form submission callback that fires after the default.
$form['#submit'][] = 'media_browser_plus_submit';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function media_browser_plus_form_media_import_alter(&$form, &$form_state) {
$form['field_tags'] = _media_browser_plus_tag_form();
$form['field_folder'] = _media_browser_plus_folder_form();
// change submit
$form['submit']['#weight'] = 2;
$form['#submit'] = array('media_browser_plus_media_import_submit');
}
/**
* Changing the media import standard submit so we can use our own batch process
*
* @param $form
* @param $form_state
*/
function media_browser_plus_media_import_submit($form, &$form_state) {
if ($form_state['values']['op'] == 'Confirm') {
$files = $form_state['storage']['files'];
$batch = array(
'title' => t('Importing Media'),
'operations' => array(
array('media_browser_plus_media_import_batch_import_files', array($files, $form_state['values'])),
),
'finished' => 'media_browser_plus_media_import_batch_complete',
);
if (!empty($form_state['values']['field_tags'])) {
// Create any new taxonomy terms.
foreach ($form_state['values']['field_tags'] as $i => &$item) {
if ($item['tid'] == 'autocreate') {
$term = (object) $item;
unset($term->tid);
taxonomy_term_save($term);
$item['tid'] = $term->tid;
}
}
}
batch_set($batch);
return;
}
$form_state['rebuild'] = TRUE;
}
/**
* Batch process that only differs in the ability to apply the field values to the items
*
* @param $files
* @param $form_values
* @param $context
*/
function media_browser_plus_media_import_batch_import_files($files, $form_values, &$context) {
// split up attributes
//list($files, $form_values) = $attributes;
// need to repeat a lot of code here just to add the fields :-(
if (!isset($context['sandbox']['files'])) {
// This runs the first time the batch runs.
// This is stupid, but otherwise, I don't think it will work...
$context['results'] = array('success' => array(), 'errors' => array());
$context['sandbox']['max'] = count($files);
$context['sandbox']['files'] = $files;
}
$files =& $context['sandbox']['files'];
// Take a cut of files. Let's do 10 at a time.
$length = (count($files) > media_variable_get('import_batch_size')) ? media_variable_get('import_batch_size') : count($files);
$to_process = array_splice($files, 0, $length);
$image_in_message = '';
foreach ($to_process as $file) {
try {
$file_obj = media_parse_to_file($file);
$context['results']['success'][] = $file;
if (!$image_in_message) {
$media = file_load($file_obj->fid);
$image_in_message = field_view_field('file', $media, 'file', 'media_preview');
}
// adding fields here
$media = file_load($file_obj->fid);
$media->field_folder = array(LANGUAGE_NONE => array(array('tid' => $form_values['field_folder'])));
$media->field_tags = array(LANGUAGE_NONE => $form_values['field_tags']);
media_browser_plus_file_move($form_values['field_folder'], $media);
}
catch (Exception $e) {
$context['results']['errors'][] = $file . " Reason: " . $e->getMessage();
}
}
$context['message'] = t('Importing') . theme('item_list', array('items' => $to_process));
// Just for kicks, show an image we are importing
$context['message'] .= drupal_render($image_in_message);
$context['finished'] = ($context['sandbox']['max'] - count($files)) / $context['sandbox']['max'];
}
/**
* Same completed batch method as in media
*
* @param $success
* @param $results
* @param $operations
*/
function media_browser_plus_media_import_batch_complete($success, $results, $operations) {
if ($results['errors']) {
drupal_set_message(theme('item_list', array('items' => $results['errors'])), 'error');
}
if ($results['success']) {
drupal_set_message(theme('item_list', array('items' => $results['success'])));
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function media_browser_plus_form_media_internet_add_alter(&$form, &$form_state) {
// Add an additional form submission callback that fires after the default.
media_browser_plus_form_media_add_upload_alter($form, $form_state);
}
/**
* Form element validate handler for taxonomy term autocomplete element.
*
* Because media_browser_plus_form_media_add_upload_multiple_alter() adds a
* tags widget to a form that is not an entity editing form, but it is desired
* for taxonomy_autocomplete_validate() to have access to the settings of the
* field for which this widget is being added, this handler runs before
* taxonomy_autocomplete_validate(), and adds the expected information to
* $form_state, as expected by taxonomy_autocomplete_validate().
*/
function media_browser_plus_prepare_taxonomy_autocomplete_validate(&$element, &$form_state) {
// causes: Notice: Undefined index: #language in media_browser_plus_prepare_taxonomy_autocomplete_validate() (line 436 of ...sites/all/modules/media_browser_plus-HEAD/media_browser_plus.module).
// @TODO: fix
$form_state['field'][$element['#field_name']][$element['#language']]['field'] = field_info_field($element['#field_name']);
// fixes: undefined index #field_parents in field_widget_field()
if (!isset($element['#field_parents'])) {
$element['#field_parents'] = array();
}
}
/**
* Form submit handler for the media browser forms that create new media entities.
*
* Enhances the media creation process by populating field content for the newly
* created entities from user-submitted data and/or data available from a
* remote provider.
*
* @see media_browser_plus_form_media_add_upload_multiple_alter()
* @see media_browser_plus_form_media_internet_add_alter()
*/
function media_browser_plus_submit($form, &$form_state) {
// Grab the fids of the newly created media entities from the redirect query
// string that was created by the form's primary submit handler, and load the
// corresponding entities.
$fids = $form_state['redirect'][1]['query']['fid'];
if (!is_array($fids)) {
$fids = array($fids);
}
$media_entities = file_load_multiple($fids);
// If tags have been entered, apply them to each new entity.
if (!empty($form_state['values']['field_tags'])) {
// Create any new taxonomy terms.
foreach ($form_state['values']['field_tags'] as $i => &$item) {
if ($item['tid'] == 'autocreate') {
$term = (object) $item;
unset($term->tid);
taxonomy_term_save($term);
$item['tid'] = $term->tid;
}
unset($item);
}
foreach ($media_entities as $media) {
$media->field_tags[LANGUAGE_NONE] = $form_state['values']['field_tags'];
}
}
// apply folder
foreach ($media_entities as $media) {
$media->field_folder[LANGUAGE_NONE] = array(array('tid' => $form_state['values']['field_folder']));
}
// If the new media is from a 3rd party provider, and that provider also
// provides MRSS data about the media, then populate the title and description
// fields from that data.
if (!empty($form_state['values']['embed_code'])) {
$provider = media_internet_get_provider($form_state['values']['embed_code']);
if ($data = _media_browser_plus_metadata($provider)) {
foreach ($data as $field_name => $value) {
$field = field_info_field($field_name);
// Limiting value population only if the field is of type 'text' or
// 'text_long' isn't as extensible as would be ideal, but we need some
// protection against populating a field with incompatible content.
if (isset($field) && in_array($field['type'], array('text', 'text_long')) && isset($field['bundles']['media'])) {
foreach ($media_entities as $media) {
if (in_array($media->type, $field['bundles']['media']) && !isset($media->{$field_name}[LANGUAGE_NONE][0]['value'])) {
$media->{$field_name}[LANGUAGE_NONE][0]['value'] = $value;
}
}
}
}
}
}
foreach ($media_entities as $media) {
media_browser_plus_file_move($form_state['values']['field_folder'], $media);
}
}
/**
* Form submit handler for the media browser forms that edit media entities.
*
* Changes file's filesystem physical folder
*
* @see media_browser_plus_form_media_edit_alter()
*/
function media_browser_plus_edit_file_submit($form, &$form_state) {
$media = $form_state['file'];
$folder_id = $media->field_folder[LANGUAGE_NONE][0]['tid'];
// Only save it if the folder id is changed
if ($form['field_folder'][LANGUAGE_NONE]['#default_value'][0] != $folder_id) {
media_browser_plus_file_move($folder_id, $media);
}
}
/**
* Helper function to return metadata that is available from a 3rd party media provider, such as YouTube.
*
* @param $provider
* A provider object as returned by media_internet_get_provider().
*
* @return
* An array of media metadata available from the provider, keyed on field
* name.
*
* @see http://video.search.yahoo.com/mrss
* @see media_internet_get_provider()
* @see MediaInternetYouTubeHandler::getMRSS()
*/
function _media_browser_plus_metadata($provider) {
// @todo This is early, experimental code, still subject to much change. For
// now, we assume $provider->getMRSS() returns a SimpleXML element. We'll
// want to change this assumption and have it return an array instead, but
// that requires fixing media_retrieve_xml() to handle XML namespaces
// properly.
$data = array();
if (is_callable(array($provider, 'getMRSS')) && ($rss = $provider->getMRSS())) {
// MRSS is an extension of RSS, so the title field is available in the
// default (ATOM) namespace.
$data['media_title'] = (string) $rss->title;
// The MRSS extensions are in their own namespace.
$mrss = $rss->children('http://search.yahoo.com/mrss/');
$data['media_description'] = (string) $mrss->group->description;
}
$data = array_filter($data, 'strlen');
return $data;
}
/**
* Implements hook_preprocess_media_link().
*/
function media_browser_plus_preprocess_media_link(&$variables) {
// Use the value of the title field, when there is one, as the link text for
// all links that would otherwise default to the filename.
// @todo Solve generically using the 'label' key of hook_entity_indo(). See
// http://drupal.org/node/910396.
$media = file_load($variables['file']->fid);
if (empty($variables['file']->description) && isset($media->media_title[LANGUAGE_NONE][0]['value'])) {
$variables['file']->description = $media->media_title[LANGUAGE_NONE][0]['value'];
}
}
/**
* Implements hook_preprocess_media_thubmnail().
*/
function media_browser_plus_preprocess_media_thumbnail(&$variables) {
// See media_browser_plus_preprocess_media_link(). Same thing here, but for
// the links that appear underneath thumbnail previews.
$media = $variables['element']['#file'];
if (isset($media->media_title[LANGUAGE_NONE][0]['value'])) {
$variables['element']['#name'] = $media->media_title[LANGUAGE_NONE][0]['value'];
}
}
/**
* implements hook_form_FORM_ID_alter()
*
* Altering the add file upload form the include folder and tag options
*/
function media_browser_plus_form_media_add_upload_alter(&$form, &$form_state) {
// alter weight to display new forms in correct order
$form['file']['#weight'] = -5;
$form['submit']['#weight'] = 5;
$form['field_tags'] = _media_browser_plus_tag_form();
$form['field_folder'] = _media_browser_plus_folder_form();
$form['#submit'][] = 'media_browser_plus_submit';
}
/**
* implements hook_form_FORM_ID_alter()
*
* making sure the tagging form is below the folder form part
*/
function media_browser_plus_form_media_edit_alter(&$form, &$form_state) {
// setting the weight accordingly
$form['field_folder']['#weight'] = $form['field_tags']['#weight'] - 1;
$form['actions']['cancel'] = array(
'#type' => 'submit',
'#value' => t('Cancel'),
'#weight' => 20,
'#submit' => array('media_browser_plus_edit_cancel'),
);
$form['actions']['submit']['#submit'][] = 'media_browser_plus_edit_file_submit';
}
/**
* Enter description here
*
* @param $form
* @param $form_state
*/
function media_browser_plus_edit_cancel($form, &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
if (isset($destination['destination'])) {
$form_state['redirect'] = $destination['destination'];
}
}
/**
* returns the tag form element
*/
function _media_browser_plus_tag_form() {
return array(
'#weight' => 1,
'#language' => LANGUAGE_NONE,
'#field_name' => 'field_tags',
'#columns' => array('tid'),
'#title' => t('Tags'),
'#description' => t('Enter a comma-separated list of words to describe your media.'),
'#required' => FALSE,
'#delta' => 0,
'#type' => 'textfield',
'#default_value' => '',
'#autocomplete_path' => 'taxonomy/autocomplete/field_tags',
'#element_validate' => array('media_browser_plus_prepare_taxonomy_autocomplete_validate', 'taxonomy_autocomplete_validate'),
'#size' => 60,
);
}
/**
* returns a select form item with a selectable media folders
*/
function _media_browser_plus_folder_form() {
$list = _media_browser_plus_folder_list();
// build form and return
return array(
'#type' => 'select',
'#language' => LANGUAGE_NONE,
'#title' => t('Media Folder'),
'#field_name' => 'field_tags',
'#weigth' => -2,
'#options' => $list,
'#description' => t('Select a folder for the media to be put in')
);
}
/**
* implement hook_menu()
*/
function media_browser_plus_menu() {
$items['admin/content/media/change_folder'] = array(
'title' => 'Change Folder',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_browser_plus_change_folder'),
'type' => MENU_CALLBACK,
'access callback' => 'media_access',
'access arguments' => array('edit')
);
$items['admin/content/media/filter'] = array(
'title' => 'Media Filter',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_browser_plus_media_filter'),
'file' => 'includes/media_browser_plus.pages.inc',
'access callback' => 'media_access',
'access arguments' => array('filter'),
'type' => MENU_LOCAL_ACTION,
'context' => MENU_CONTEXT_INLINE
);
$items['admin/content/media/thumbnailsJSON'] = array(
'title' => 'Load Media Entities',
'page callback' => 'media_browser_plus_thumbnailsJSON',
'access callback' => 'media_access',
'access arguments' => array('preview')
);
$items['admin/config/media/media_browser_plus_settings'] = array(
'title' => 'Media Browser Plus Settings',
'description' => 'Change the behaviour and layout of the media browser plus UI',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_browser_plus_media_settings'),
'access callback' => 'media_access',
'access arguments' => array('administer')
);
$items['admin/content/media/edit_multiple/%'] = array(
'title' => 'Edit Media Items',
'page callback' => 'media_browser_plus_edit_multiple_form',
'page arguments' => array(4),
'file' => 'includes/media_browser_plus.pages.inc',
'access callback' => 'media_access',
'access arguments' => array('edit')
);
$items['admin/content/media/delete_multiple/%'] = array(
'title' => 'Delete Media Items',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_browser_plus_delete_multiple_form', 4),
'file' => 'includes/media_browser_plus.pages.inc',
'access callback' => 'media_access',
'access arguments' => array('edit')
);
// folder management disabled until menu bug has been fixed
$items['admin/content/media/folder_list'] = array(
'title' => 'Administer folders',
'description' => 'Manage your media folders',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_browser_plus_folder_list'),
'file' => 'includes/media_browser_plus.folders.inc',
'access callback' => 'media_browser_plus_access',
'access arguments' => array('administer media folders')
);
$items['admin/content/media/add_folder'] = array(
'title' => 'Add Folder',
'description' => 'Add a new media folder',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_browser_plus_folder_add'),
'access callback' => 'media_browser_plus_access',
'access arguments' => array('administer media folders'),
'file' => 'includes/media_browser_plus.folders.inc',
);
$items['admin/content/media/folder/%media_browser_plus_folder/edit'] = array(
'title' => 'Edit Folder',
'description' => 'Edit media folder',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_browser_plus_folder_edit', 4),
'access callback' => 'media_browser_plus_access',
'access arguments' => array('administer media folders'),
'file' => 'includes/media_browser_plus.folders.inc'
);
$items['admin/content/media/folder/%media_browser_plus_folder/delete'] = array(
'title' => 'Delete Folder',
'description' => 'Delete media folder',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_browser_plus_folder_delete', 4),
'access callback' => 'media_browser_plus_access',
'access arguments' => array('administer media folders'),
'file' => 'includes/media_browser_plus.folders.inc'
);
$items['admin/content/media/%file/preview'] = array(
'title' => 'Preview Media',
'description' => 'Preview Media Item',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_browser_plus_media_preview', 3),
'access callback' => 'media_access',
'access arguments' => array('preview'),
'file' => 'includes/media_browser_plus.pages.inc'
);
return $items;
}
/**
* implements hook_menu_alter()
*
* @param $items
*/
function media_browser_plus_menu_alter(&$items) {
$items['admin/content/media']['access arguments'] = array('access media backend');
$items['admin/content/media']['access callback'] = array('media_browser_plus_access');
}
/**
* implements hook_permission()
*/
function media_browser_plus_permission() {
return array(
'media grid view' => array(
'title' => t('Grid View'),
'description' => t('Allow users to use the grid view.'),
),
'media list view' => array(
'title' => t('List View'),
'description' => t('Allow users to use the list view.'),
),
'access media backend' => array(
'title' => t('Access media backend'),
'description' => t('Allow user to access the media backend according to their privileges.'),
),
'upload media' => array(
'title' => t('Upload media'),
'description' => t('Allow user to add media.'),
),
'download media' => array(
'title' => t('Download Media'),
'description' => t('Allows the user to download the original media items.'),
),
'filter media' => array(
'title' => t('Filter Media'),
'description' => t('Allows the user to filter the displayed media in the backend.'),
),
'administer media folders' => array(
'title' => t('Administer Media Folders'),
'description' => t('Allows the user to add, edite, delete and resort media folders.'), // @TODO better description
),
'preview media' => array(
'title' => t('Preview media'),
'description' => t('Allows the user to view a configurable preview of the original media item.'), // @TODO better description
),
);
}
/**
* Implements hook_library().
*/
function media_browser_plus_library() {
$library_path = variable_get('media_browser_plus_library_path', 'sites/all/libraries');
$stylesheet = variable_get('media_browser_plus_stylesheet', 'example1');
$libraries['colorbox'] = array(
'title' => 'Colorbox',
'website' => 'http://colorpowered.com/colorbox/',
'version' => '1.3.9',
'js' => array(
$library_path . '/colorbox/colorbox/jquery.colorbox-min.js' => array(),
),
'css' => array(
$library_path . '/colorbox/' . $stylesheet . '/colorbox.css' => array(
'type' => 'file',
'media' => 'screen',
),
),
);
$path = drupal_get_path('module', 'media_browser_plus');
$libraries['media_browser_plus'] = array(
'title' => 'Media Browser Plus',
'version' => '1',
'js' => array(
$path . '/js/media_browser_plus.admin.menu.js' => array(),
),
'css' => array(
$path . '/css/media_browser_plus.admin.css' => array(
'type' => 'file',
'media' => 'screen',
),
$path . '/css/colorbox.css' => array(
'type' => 'file',
'media' => 'screen',
),
),
);
return $libraries;
}
/**
* Enter description here ...
* @param $field
* @param $entity_type
* @param $entity
*/
function media_browser_plus_file_download_access($field, $entity_type, $entity) {
// only check against media entities
if ($entity_type == 'file') {
// check for media admin AND return true if found
return media_browser_plus_media_access($entity);
}
}
/**
* checks access to a given media entity
* @param $media_entity
*/
function media_browser_plus_media_access($media_entity) {
if (media_browser_plus_access('administer media')) {
return TRUE;
}
// start with ACCESS_ALLOW - by default media items are fully accessible
$access = MEDIA_ENTITY_ACCESS_ALLOW;
// collect all modules implementing hook_media_entity_access
foreach (module_implements('media_entity_access') as $module) {
// and invoke the hook
$return = module_invoke($module, 'media_entity_access', $media_entity);
// if no ALLOW or DENY was returned we assume IGNORE and check the next
if ($return != MEDIA_ENTITY_ACCESS_ALLOW && $return != MEDIA_ENTITY_ACCESS_DENY) {
continue;
}
// if we have a DENY we can return a complete false here
if ($return === MEDIA_ENTITY_ACCESS_DENY) {
return FALSE;
}
// otherwise it is an ALLOW and we save it
$access = MEDIA_ENTITY_ACCESS_ALLOW;
}
// check if we had one allow
return ($access === MEDIA_ENTITY_ACCESS_ALLOW);
}
/**
* revokes the general "view media" == "download media" access rule
*
* @param $grants
* @param $field
* @param $entity_type
* @param $entity
*/
function media_browser_plus_file_download_access_alter(&$grants, $field, $entity_type, $entity = NULL) {
if ($entity_type == 'file') {
unset($grants['file']);
}
}
/**
* implements hook_media_operations()
*/
function media_browser_plus_media_operations() {
return array(
'edit_multiple' => array(
'label' => t('Edit'),
'callback' => NULL,
'redirect' => 'admin/content/media/edit_multiple/%fids'
));
}
/**
* Manages access for media browser plus actions
*
* @param $op
*/
function media_browser_plus_access($op) {