1 |
ca0757b9
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Primarily Drupal hooks.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_menu().
|
10 |
|
|
*/
|
11 |
|
|
function media_bulk_upload_menu() {
|
12 |
|
|
$items['admin/content/file/import'] = array(
|
13 |
|
|
'title' => 'Import files',
|
14 |
|
|
'description' => 'Import files into your media library.',
|
15 |
|
|
'page callback' => 'drupal_get_form',
|
16 |
|
|
'page arguments' => array('media_bulk_upload_import'),
|
17 |
|
|
'access arguments' => array('import media'),
|
18 |
|
|
'type' => MENU_LOCAL_ACTION,
|
19 |
|
|
'file' => 'includes/media_bulk_upload.admin.inc',
|
20 |
|
|
'weight' => 10,
|
21 |
|
|
);
|
22 |
|
|
$items['admin/content/file/thumbnails/import'] = $items['admin/content/file/import'];
|
23 |
|
|
|
24 |
|
|
// @todo Investigate passing file IDs in query string rather than a menu
|
25 |
|
|
// argument and then deprecate media_multi_load().
|
26 |
|
|
$items['admin/content/file/edit-multiple/%media_bulk_upload_multi'] = array(
|
27 |
|
|
'title' => 'Edit multiple files',
|
28 |
|
|
'page callback' => 'media_bulk_upload_file_page_edit_multiple',
|
29 |
|
|
'page arguments' => array(4),
|
30 |
|
|
'access callback' => '_media_bulk_upload_file_entity_access_recursive',
|
31 |
|
|
'access arguments' => array(4, 'update'),
|
32 |
|
|
'file' => 'includes/media_bulk_upload.pages.inc',
|
33 |
|
|
);
|
34 |
|
|
|
35 |
|
|
return $items;
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
/**
|
39 |
|
|
* Implements hook_permission().
|
40 |
|
|
*/
|
41 |
|
|
function media_bulk_upload_permission() {
|
42 |
|
|
return array(
|
43 |
|
|
'import media' => array(
|
44 |
|
|
'title' => t('Import media files from the local filesystem'),
|
45 |
|
|
'description' => t('Simple file importer'),
|
46 |
|
|
'restrict access' => TRUE,
|
47 |
|
|
),
|
48 |
|
|
);
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
/**
|
52 |
|
|
* Implements hook_media_browser_plugin_info_alter().
|
53 |
|
|
*/
|
54 |
|
|
function media_bulk_upload_media_browser_plugin_info_alter(&$info) {
|
55 |
|
|
$info['upload']['class'] = 'MediaBrowserBulkUpload';
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
|
|
* Implements hook_file_operations().
|
60 |
|
|
*/
|
61 |
|
|
function media_bulk_upload_file_operations() {
|
62 |
|
|
return array(
|
63 |
|
|
'edit_multiple' => array(
|
64 |
|
|
'label' => t('Edit selected files'),
|
65 |
|
|
'callback' => 'media_bulk_upload_file_operation_edit_multiple',
|
66 |
|
|
),
|
67 |
|
|
);
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
/**
|
71 |
|
|
* Implements hook_form_alter().
|
72 |
|
|
*/
|
73 |
|
|
function media_bulk_upload_form_alter(&$form, &$form_state, $form_id) {
|
74 |
|
|
// If we're in the media browser, set the #media_browser key to true
|
75 |
|
|
// so that if an ajax request gets sent to a different path, the form
|
76 |
|
|
// still uses the media_browser_form_submit callback.
|
77 |
|
|
if (current_path() == 'media/browser' && $form_id != 'views_exposed_form') {
|
78 |
|
|
$form_state['#media_browser'] = TRUE;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
// If the #media_browser key isset and is true we are using the browser
|
82 |
|
|
// popup, so add the media_browser submit handler.
|
83 |
|
|
if (!empty($form_state['#media_browser'])) {
|
84 |
|
|
$form['#submit'][] = 'media_bulk_upload_browser_form_submit';
|
85 |
|
|
}
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
/**
|
89 |
|
|
* Submit handler; direction form submissions in the media browser.
|
90 |
|
|
*/
|
91 |
|
|
function media_bulk_upload_browser_form_submit($form, &$form_state) {
|
92 |
|
|
$url = NULL;
|
93 |
|
|
$parameters = array();
|
94 |
|
|
|
95 |
|
|
// Multi upload.
|
96 |
|
|
if (!empty($form_state['files'])) {
|
97 |
|
|
$files = $form_state['files'];
|
98 |
|
|
$url = 'media/browser';
|
99 |
|
|
$parameters = array('query' => array('render' => 'media-popup', 'fid' => array_keys($files)));
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
// If $url is set, we had some sort of upload, so redirect the form.
|
103 |
|
|
if (!empty($url)) {
|
104 |
|
|
$form_state['redirect'] = array($url, $parameters);
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
/**
|
109 |
|
|
* Return a URL for editing an files.
|
110 |
|
|
*
|
111 |
|
|
* Works with an array of fids or a single fid.
|
112 |
|
|
*
|
113 |
|
|
* @param mixed $fids
|
114 |
|
|
* An array of file IDs or a single file ID.
|
115 |
|
|
*/
|
116 |
|
|
function media_bulk_upload_file_edit_url($fids) {
|
117 |
|
|
if (!is_array($fids)) {
|
118 |
|
|
$fids = array($fids);
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
if (count($fids) > 1) {
|
122 |
|
|
return 'admin/content/file/edit-multiple/' . implode(' ', $fids);
|
123 |
|
|
}
|
124 |
|
|
else {
|
125 |
|
|
return 'file/' . reset($fids) . '/edit';
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/**
|
130 |
|
|
* Callback for the edit operation.
|
131 |
|
|
*
|
132 |
|
|
* Redirects the user to the edit multiple files page.
|
133 |
|
|
*
|
134 |
|
|
* @param array $fids
|
135 |
|
|
* An array of file IDs.
|
136 |
|
|
*
|
137 |
|
|
* @see media_file_page_edit_multiple()
|
138 |
|
|
*/
|
139 |
|
|
function media_bulk_upload_file_operation_edit_multiple($fids) {
|
140 |
|
|
// The thumbnail browser returns TRUE/FALSE for each item, so use array keys.
|
141 |
|
|
$fids = array_keys(array_filter($fids));
|
142 |
|
|
drupal_goto(media_bulk_upload_file_edit_url($fids), array('query' => drupal_get_destination()));
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
/**
|
146 |
|
|
* Implements hook_forms().
|
147 |
|
|
*/
|
148 |
|
|
function media_bulk_upload_forms($form_id, $args) {
|
149 |
|
|
$forms = array();
|
150 |
|
|
// To support the multiedit form, each form has to have a unique ID.
|
151 |
|
|
// So we name all the forms media_edit_N where the first requested form is
|
152 |
|
|
// media_edit_0, 2nd is media_edit_1, etc.
|
153 |
|
|
module_load_include('inc', 'file_entity', 'file_entity.pages');
|
154 |
|
|
if ($form_id != 'media_edit' && (strpos($form_id, 'media_edit') === 0)) {
|
155 |
|
|
$forms[$form_id] = array(
|
156 |
|
|
'callback' => 'file_entity_edit',
|
157 |
|
|
);
|
158 |
|
|
}
|
159 |
|
|
return $forms;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
/**
|
163 |
|
|
* Access callback for the media-multi form.
|
164 |
|
|
*
|
165 |
|
|
* @param $files
|
166 |
|
|
* An array of files being editing on the multiform.
|
167 |
|
|
* @param $op
|
168 |
|
|
* A string containing the operation requested, such as 'update'.
|
169 |
|
|
* @return
|
170 |
|
|
* TRUE if the current user has access to edit all of the files, otherwise FALSE.
|
171 |
|
|
*/
|
172 |
|
|
function _media_bulk_upload_file_entity_access_recursive($files, $op) {
|
173 |
|
|
// Check that the current user can access each file.
|
174 |
|
|
if (!empty($files)) {
|
175 |
|
|
foreach ($files as $file) {
|
176 |
|
|
if (!file_entity_access($op, $file)) {
|
177 |
|
|
return FALSE;
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
return TRUE;
|
181 |
|
|
}
|
182 |
|
|
return FALSE;
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
/**
|
186 |
|
|
* Load callback for %media_multi placeholder in menu paths.
|
187 |
|
|
*
|
188 |
|
|
* @param string $fids
|
189 |
|
|
* Separated by space (e.g., "3 6 12 99"). This often appears as "+" within
|
190 |
|
|
* URLs (e.g., "3+6+12+99"), but Drupal automatically decodes paths when
|
191 |
|
|
* intializing $_GET['q'].
|
192 |
|
|
*
|
193 |
|
|
* @return array
|
194 |
|
|
* An array of corresponding file entities.
|
195 |
|
|
*/
|
196 |
|
|
function media_bulk_upload_multi_load($fids) {
|
197 |
|
|
return file_load_multiple(explode(' ', $fids));
|
198 |
|
|
} |