Projet

Général

Profil

Paste
Télécharger (8,51 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / includes / media.browser.inc @ e4215af7

1
<?php
2

    
3
/**
4
 * @file
5
 * Summon plugins and render the media browser.
6
 */
7

    
8
/**
9
 * Media browser page callback.
10
 */
11
function media_browser($selected = NULL) {
12
  $output = array();
13
  $output['#attached']['library'][] = array('media', 'media_browser_page');
14

    
15
  $params = media_get_browser_params();
16

    
17
  // If we just did a multiple upload, do the multiform file edit. The flag that
18
  // tells us that we need to do this is $params['render_multi_edit_form'].
19
  if (variable_get('media_bulk_upload_edit', TRUE) && isset($params['render_multi_edit_form']) && isset($params['fid']) && module_exists('media_bulk_upload')) {
20
    module_load_include('inc', 'media_bulk_upload', 'includes/media_bulk_upload.pages');
21
    $files = file_load_multiple($params['fid']);
22
    $multi_edit_form = media_bulk_upload_file_page_edit_multiple($files);
23
    $multi_edit_form['buttons']['cancel']['#access'] = FALSE;
24
    return $multi_edit_form;
25
  }
26

    
27
  // If one or more files have been selected, the browser interaction is now
28
  // complete. Return empty page content to the dialog which now needs to close,
29
  // but populate Drupal.settings with information about the selected files.
30
  if (isset($params['fid'])) {
31
    $fids = is_array($params['fid']) ? $params['fid'] : array($params['fid']);
32
    if (!is_numeric($fids[0])) {
33
      throw new Exception('Error selecting media, fid param is not an fid or an array of fids');
34
    }
35
    $files = file_load_multiple($fids);
36
    foreach ($files as $file) {
37
      $view_mode = isset($params['view_mode']) ? $params['view_mode'] : 'preview';
38
      media_browser_build_media_item($file, $view_mode);
39
    }
40
    $setting = array('media' => array('selectedMedia' => array_values($files)));
41
    drupal_add_js($setting, 'setting');
42
    return $output;
43
  }
44

    
45
  $plugins = media_get_browser_plugin_info();
46

    
47
  // Allow parameters to provide a list of enabled or disabled media browser
48
  // plugins.
49
  if (!empty($params['enabledPlugins'])) {
50
    $plugins = array_intersect_key($plugins, array_fill_keys($params['enabledPlugins'], 1));
51
  }
52
  elseif (!empty($params['disabledPlugins'])) {
53
    $plugins = array_diff_key($plugins, array_fill_keys($params['disabledPlugins'], 1));
54
  }
55

    
56
  // Render plugins.
57
  $plugin_output = array();
58
  foreach ($plugins as $key => $plugin_info) {
59
    // Support the old CTools style handler definition.
60
    if (!isset($plugin_info['class']) && !empty($plugin_info['handler'])) {
61
      if (is_string($plugin_info['handler'])) {
62
        $plugin_info['class'] = $plugin_info['handler'];
63
      }
64
      elseif (isset($plugin_info['handler']['class'])) {
65
        $plugin_info['class'] = $plugin_info['handler']['class'];
66
      }
67
    }
68

    
69
    if (empty($plugin_info['class']) || !class_exists($plugin_info['class'])) {
70
      continue;
71
    }
72

    
73
    $plugin = new $plugin_info['class']($plugin_info, $params);
74
    if ($plugin->access()) {
75
      $plugin_output[$key] = $plugin->view();
76
      if (!empty($plugin_output[$key]) && is_array($plugin_output[$key])) {
77
        $plugin_output[$key] += array(
78
          '#title' => $plugin_info['title'],
79
          '#weight' => isset($plugin_info['weight']) ? $plugin_info['weight'] : 0,
80
        );
81
      }
82
      else {
83
        unset($plugin_output[$key]);
84
        continue;
85
      }
86
    }
87
    else {
88
      continue;
89
    }
90

    
91
    // We need to ensure that a submit button is available on each tab. If the
92
    // plugin is not returning a form element we need to add a submit button.
93
    // This is a fairly broad assumption.
94
    if (empty($plugin_output[$key]['#form']) && !empty($plugin_output[$key]['#markup'])) {
95
      $fake_buttons = '<div class="form-actions form-wrapper">';
96
      $fake_buttons .= l(t('Submit'), '', array(
97
        'attributes' => array(
98
          'class' => array('button', 'button-yes', 'fake-submit', $key),
99
        ),
100
      ));
101
      $fake_buttons .= '</div>';
102
      $plugin_output[$key]['#markup'] .= $fake_buttons;
103
    }
104
  }
105

    
106
  // Allow modules to change the tab names or whatever else they want to change
107
  // before we render.  Perhaps this should be an alter on the theming function
108
  // that we should write to be making the tabs.
109
  drupal_alter('media_browser_plugins', $plugin_output);
110

    
111
  $tabs = array();
112
  $settings = array('media' => array('browser' => array()));
113

    
114
  foreach (element_children($plugin_output, TRUE) as $key) {
115
    // Add any JavaScript settings from the browser tab.
116
    if (!empty($plugin_output[$key]['#settings'])) {
117
      $settings['media']['browser'][$key] = $plugin_output[$key]['#settings'];
118
    }
119

    
120
    // If this is a "ajax" style tab, add the href, otherwise an id. jQuery UI.
121
    // Will use an href value to load content from that url.
122
    $tabid = 'media-tab-' . check_plain($key);
123
    if (!empty($plugin_output[$key]['#callback'])) {
124
      $href = $plugin_output[$key]['#callback'];
125
    }
126
    else {
127
      $attributes = array(
128
        'class' => array('media-browser-tab'),
129
        'id' => $tabid,
130
        'data-tabid' => $key,
131
      );
132
      // Create a div for each tab's content.
133
      $plugin_output[$key] += array(
134
        '#prefix' => '<div ' . drupal_attributes($attributes) . ">\n",
135
        '#suffix' => "</div>\n",
136
      );
137
    }
138

    
139
    $attributes = array(
140
      'href' => '#' . $tabid,
141
      'data-tabid' => $key,
142
      'title' => $plugin_output[$key]['#title'],
143
    );
144
    $tabs[]['element'] = array(
145
      '#markup' => '<li><a' . drupal_attributes($attributes) . '>' . check_plain($plugin_output[$key]['#title']) . "</a></li>\n",
146
    );
147
  }
148

    
149
  drupal_add_js($settings, 'setting');
150

    
151
  $output['tabset']['tabs'] = array(
152
    '#theme' => 'menu_local_tasks',
153
    '#attributes' => array('class' => array('tabs', 'primary')),
154
    '#primary' => $tabs,
155
  );
156

    
157
  $output['tabset']['panes'] = $plugin_output;
158

    
159
  return $output;
160
}
161

    
162
/**
163
 * Menu callback for testing the media browser.
164
 */
165
function media_browser_testbed($form) {
166
  $form['#attached']['library'][] = array('media', 'media_browser');
167
  $form['#attached']['library'][] = array('media', 'media_browser_settings');
168

    
169
  $form['test_element'] = array(
170
    '#type' => 'media',
171
    '#media_options' => array(
172
      'global' => array(
173
        'types' => array('video', 'audio'),
174
      ),
175
    ),
176
  );
177

    
178
  $launcher = '<a href="#" id="launcher"> Launch Media Browser</a>';
179

    
180
  $form['options'] = array(
181
    '#type' => 'textarea',
182
    '#title' => 'Options (JSON)',
183
    '#rows' => 10,
184
  );
185

    
186
  $form['launcher'] = array(
187
    '#markup' => $launcher,
188
  );
189

    
190
  $form['result'] = array(
191
    '#type' => 'textarea',
192
    '#title' => 'Result',
193
  );
194

    
195
  $js = <<<EOF
196
    Drupal.behaviors.mediaTest = {
197
    attach: function(context, settings) {
198
      var delim = "---";
199
      var recentOptions = [];
200
      var recentOptionsCookie = jQuery.cookie("recentOptions");
201
      if (recentOptionsCookie) {
202
        recentOptions = recentOptionsCookie.split("---");
203
      }
204

    
205
      var recentSelectBox = jQuery('<select id="recent_options" style="width:100%"></select>').change(function() { jQuery('#edit-options').val(jQuery(this).val())});
206

    
207
      jQuery('.form-item-options').append('<label for="recent_options">Recent</a>');
208
      jQuery('.form-item-options').append(recentSelectBox);
209
      jQuery('.form-item-options').append(jQuery('<a href="#">Reset</a>').click(function() {alert('reset'); jQuery.cookie("recentOptions", null); window.location.reload(); }));
210

    
211
      jQuery.each(recentOptions, function (idx, val) {
212
        recentSelectBox.append(jQuery('<option></option>').val(val).html(val));
213
      });
214

    
215

    
216
      jQuery('#launcher').click(function () {
217
        jQuery('#edit-result').val('');
218
        var options = {};
219
        var optionsTxt = jQuery('#edit-options').val();
220
        if (optionsTxt) {
221
          // Store it in the recent box
222
          recentOptionsCookie += "---" + optionsTxt
223
          jQuery.cookie("recentOptions", recentOptionsCookie, { expires: 7 });
224
          recentSelectBox.append(jQuery('<option></option>').val(optionsTxt).html(optionsTxt));
225
          options = eval('(' + optionsTxt + ')');
226
        }
227
        Drupal.media.popups.mediaBrowser(Drupal.behaviors.mediaTest.mediaSelected, options);
228
        return false;
229
      });
230
    },
231

    
232
    mediaSelected: function(selectedMedia) {
233
      var result = JSON.stringify(selectedMedia);
234
        jQuery('#edit-result').val(result);
235
    }
236
  }
237

    
238
EOF;
239

    
240
  drupal_add_js($js, array('type' => 'inline'));
241
  return $form;
242
}
243

    
244
/**
245
 * Adds additional properties to a file which are needed by the browser JS code.
246
 *
247
 * @param object $file
248
 *   A Drupal file object.
249
 */
250
function media_browser_build_media_item($file, $view_mode = 'preview') {
251
  $preview = media_get_thumbnail_preview($file, NULL, $view_mode);
252
  $file->preview = drupal_render($preview);
253
  $file->url = file_create_url($file->uri);
254
}