Projet

Général

Profil

Paste
Télécharger (7,66 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_bulk_upload / media_bulk_upload.module @ e4215af7

1
<?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']['title'] = t('Upload');
56
  $info['upload']['class'] = 'MediaBrowserBulkUpload';
57

    
58
}
59

    
60
/**
61
 * Implements hook_file_operations().
62
 */
63
function media_bulk_upload_file_operations() {
64
  return array(
65
    'edit_multiple' => array(
66
      'label' => t('Edit selected files'),
67
      'callback' => 'media_bulk_upload_file_operation_edit_multiple',
68
    ),
69
  );
70
}
71

    
72
/**
73
 * Implements hook_form_alter().
74
 */
75
function media_bulk_upload_form_alter(&$form, &$form_state, $form_id) {
76
  // If we're in the media browser, set the #media_browser key to true
77
  // so that if an ajax request gets sent to a different path, the form
78
  // still uses the media_browser_form_submit callback.
79
  if (current_path() == 'media/browser' && $form_id != 'views_exposed_form') {
80
    $form_state['#media_browser'] = TRUE;
81
  }
82

    
83
  // If the #media_browser key isset and is true we are using the browser
84
  // popup, so add the media_browser submit handler.
85
  if (!empty($form_state['#media_browser'])) {
86
    $form['#submit'][] = 'media_bulk_upload_browser_form_submit';
87
  }
88
}
89

    
90
/**
91
 * Submit handler; direction form submissions in the media browser.
92
 */
93
function media_bulk_upload_browser_form_submit($form, &$form_state) {
94
  $url = NULL;
95
  $parameters = array();
96

    
97
  // Multi upload.
98
  if (!empty($form_state['files'])) {
99
    $files = $form_state['files'];
100
    $url = 'media/browser';
101
    $parameters = array('query' => array('render' => 'media-popup', 'fid' => array_keys($files)));
102
    // Pass in "render_multi_edit_form" as a parameter to signal to the
103
    // media_browser() function that we need to edit multiple files.
104
    $parameters['query']['render_multi_edit_form'] = TRUE;
105
  }
106

    
107
  // If $url is set, we had some sort of upload, so redirect the form.
108
  if (!empty($url)) {
109
    $form_state['redirect'] = array($url, $parameters);
110
  }
111
}
112

    
113
/**
114
 * Implements hook_multiform_get_form_alter().
115
 *
116
 * Alter the multiform that is used after uploading multiple files.
117
 */
118
function media_bulk_upload_multiform_get_form_alter($form_state_save, &$redirect, $all_args) {
119

    
120
  // Was this form submitted while in the Media browser?
121
  if (isset($redirect['#media_browser']) && !empty($form_state_save['input'])) {
122

    
123
    // The $all_args should be an array of arrays, with the second element of
124
    // the inner array being the file. For details on this structure, see
125
    // the media_bulk_upload_file_page_edit_multiple() function.
126
    $fids = array();
127
    foreach ($all_args as $arg) {
128
      if (count($arg) == 2) {
129
        $file = $arg[1];
130
        $fids[] = $file->fid;
131
      }
132
    }
133

    
134
    // If we found something, instruct the Media browser to close and attach
135
    // the files to whatever they need to be attached to.
136
    if (!empty($fids)) {
137
      $url = 'media/browser';
138
      $parameters = array('query' => array('render' => 'media-popup', 'fid' => $fids));
139
      $redirect['redirect'] = array($url, $parameters);
140
    }
141
  }
142
}
143

    
144
/**
145
 * Return a URL for editing an files.
146
 *
147
 * Works with an array of fids or a single fid.
148
 *
149
 * @param mixed $fids
150
 *   An array of file IDs or a single file ID.
151
 */
152
function media_bulk_upload_file_edit_url($fids) {
153
  if (!is_array($fids)) {
154
    $fids = array($fids);
155
  }
156

    
157
  if (count($fids) > 1) {
158
    return 'admin/content/file/edit-multiple/' . implode(' ', $fids);
159
  }
160
  else {
161
    return 'file/' . reset($fids) . '/edit';
162
  }
163
}
164

    
165
/**
166
 * Callback for the edit operation.
167
 *
168
 * Redirects the user to the edit multiple files page.
169
 *
170
 * @param array $fids
171
 *   An array of file IDs.
172
 *
173
 * @see media_file_page_edit_multiple()
174
 */
175
function media_bulk_upload_file_operation_edit_multiple($fids) {
176
  // The thumbnail browser returns TRUE/FALSE for each item, so use array keys.
177
  $fids = array_keys(array_filter($fids));
178
  drupal_goto(media_bulk_upload_file_edit_url($fids), array('query' => drupal_get_destination()));
179
}
180

    
181
/**
182
 * Implements hook_forms().
183
 */
184
function media_bulk_upload_forms($form_id, $args) {
185
  $forms = array();
186
  // To support the multiedit form, each form has to have a unique ID.
187
  // So we name all the forms media_edit_N where the first requested form is
188
  // media_edit_0, 2nd is media_edit_1, etc.
189
  module_load_include('inc', 'file_entity', 'file_entity.pages');
190
  if ($form_id != 'media_edit' && (strpos($form_id, 'media_edit') === 0)) {
191
    $forms[$form_id] = array(
192
      'callback' => 'file_entity_edit',
193
      'wrapper_callback' => 'media_bulk_upload_prepare_edit_form',
194
    );
195
  }
196
  return $forms;
197
}
198

    
199
function media_bulk_upload_prepare_edit_form($form, &$form_state) {
200
  form_load_include($form_state, 'inc', 'file_entity', 'file_entity.pages');
201
}
202

    
203
/**
204
 * Access callback for the media-multi form.
205
 *
206
 * @param $files
207
 *   An array of files being editing on the multiform.
208
 * @param $op
209
 *   A string containing the operation requested, such as 'update'.
210
 * @return
211
 *   TRUE if the current user has access to edit all of the files, otherwise FALSE.
212
 */
213
function _media_bulk_upload_file_entity_access_recursive($files, $op) {
214
  // Check that the current user can access each file.
215
  if (!empty($files)) {
216
    foreach ($files as $file) {
217
      if (!file_entity_access($op, $file)) {
218
        return FALSE;
219
      }
220
    }
221
    return TRUE;
222
  }
223
  return FALSE;
224
}
225

    
226
/**
227
 * Load callback for %media_multi placeholder in menu paths.
228
 *
229
 * @param string $fids
230
 *   Separated by space (e.g., "3 6 12 99"). This often appears as "+" within
231
 *   URLs (e.g., "3+6+12+99"), but Drupal automatically decodes paths when
232
 *   intializing $_GET['q'].
233
 *
234
 * @return array
235
 *   An array of corresponding file entities.
236
 */
237
function media_bulk_upload_multi_load($fids) {
238
  return file_load_multiple(explode(' ', $fids));
239
}
240

    
241
/**
242
 * Implements hook_form_FORM_ID_alter().
243
 */
244
function media_bulk_upload_form_media_admin_config_browser_alter(&$form, &$form_state) {
245

    
246
  $form['bulk_upload'] = array(
247
    '#type' => 'fieldset',
248
    '#title' => t('Bulk Upload'),
249
    '#collapsible' => TRUE,
250
    '#collapsed' => TRUE,
251
  );
252

    
253
  $form['bulk_upload']['media_bulk_upload_edit'] = array(
254
    '#type' => 'checkbox',
255
    '#title' => t("Bulk upload edit"),
256
    '#description' => t("After bulk uploads on entity edit pages, show edit form for all uploaded files in a single popup."),
257
    '#default_value' => variable_get('media_bulk_upload_edit', TRUE),
258
  );
259
}