Projet

Général

Profil

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

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

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']['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
    // Pass in "render_multi_edit_form" as a parameter to signal to the
101
    // media_browser() function that we need to edit multiple files.
102
    $parameters['query']['render_multi_edit_form'] = TRUE;
103
  }
104

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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