Projet

Général

Profil

Paste
Télécharger (9,34 ko) Statistiques
| Branche: | Révision:

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

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 one or more files have been selected, the browser interaction is now
18
  // complete. Return empty page content to the dialog which now needs to close,
19
  // but populate Drupal.settings with information about the selected files.
20
  if (isset($params['fid'])) {
21
    $fids = is_array($params['fid']) ? $params['fid'] : array($params['fid']);
22
    if (!is_numeric($fids[0])) {
23
      throw new Exception('Error selecting media, fid param is not an fid or an array of fids');
24
    }
25
    $files = file_load_multiple($fids);
26
    foreach ($files as $file) {
27
      media_browser_build_media_item($file);
28
    }
29
    $setting = array('media' => array('selectedMedia' => array_values($files)));
30
    drupal_add_js($setting, 'setting');
31
    return $output;
32
  }
33

    
34
  $plugins = media_get_browser_plugin_info();
35

    
36
  // Allow parameters to provide a list of enabled or disabled media browser
37
  // plugins.
38
  if (!empty($params['enabledPlugins'])) {
39
    $plugins = array_intersect_key($plugins, array_fill_keys($params['enabledPlugins'], 1));
40
  }
41
  elseif (!empty($params['disabledPlugins'])) {
42
    $plugins = array_diff_key($plugins, array_fill_keys($params['disabledPlugins'], 1));
43
  }
44

    
45
  // Render plugins.
46
  $plugin_output = array();
47
  foreach ($plugins as $key => $plugin_info) {
48
    // Support the old CTools style handler definition.
49
    if (!isset($plugin_info['class']) && !empty($plugin_info['handler'])) {
50
      if (is_string($plugin_info['handler'])) {
51
        $plugin_info['class'] = $plugin_info['handler'];
52
      }
53
      elseif (isset($plugin_info['handler']['class'])) {
54
        $plugin_info['class'] = $plugin_info['handler']['class'];
55
      }
56
    }
57

    
58
    if (empty($plugin_info['class']) || !class_exists($plugin_info['class'])) {
59
      continue;
60
    }
61

    
62
    $plugin = new $plugin_info['class']($plugin_info, $params);
63
    if ($plugin->access()) {
64
      $plugin_output[$key] = $plugin->view();
65
      if (!empty($plugin_output[$key]) && is_array($plugin_output[$key])) {
66
        $plugin_output[$key] += array(
67
          '#title' => $plugin_info['title'],
68
          '#weight' => isset($plugin_info['weight']) ? $plugin_info['weight'] : 0,
69
        );
70
      }
71
      else {
72
        unset($plugin_output[$key]);
73
        continue;
74
      }
75
    }
76
    else {
77
      continue;
78
    }
79

    
80
    // We need to ensure that a submit button is available on each tab. If the
81
    // plugin is not returning a form element we need to add a submit button.
82
    // This is a fairly broad assumption.
83
    if (empty($plugin_output[$key]['#form']) && !empty($plugin_output[$key]['#markup'])) {
84
      $fake_buttons = '<div class="form-actions form-wrapper">';
85
      $fake_buttons .= l(t('Submit'), '', array(
86
        'attributes' => array(
87
          'class' => array('button', 'button-yes', 'fake-submit', $key),
88
        ),
89
      ));
90
      $fake_buttons .= '</div>';
91
      $plugin_output[$key]['#markup'] .= $fake_buttons;
92
    }
93
  }
94

    
95
  // Allow modules to change the tab names or whatever else they want to change
96
  // before we render.  Perhaps this should be an alter on the theming function
97
  // that we should write to be making the tabs.
98
  drupal_alter('media_browser_plugins', $plugin_output);
99

    
100
  $tabs = array();
101
  $settings = array('media' => array('browser' => array()));
102

    
103
  foreach (element_children($plugin_output, TRUE) as $key) {
104
    // Add any JavaScript settings from the browser tab.
105
    if (!empty($plugin_output[$key]['#settings'])) {
106
      $settings['media']['browser'][$key] = $plugin_output[$key]['#settings'];
107
    }
108

    
109
    // If this is a "ajax" style tab, add the href, otherwise an id. jQuery UI
110
    // will use an href value to load content from that url
111
    $tabid = 'media-tab-' . check_plain($key);
112
    if (!empty($plugin_output[$key]['#callback'])) {
113
      $href = $plugin_output[$key]['#callback'];
114
    }
115
    else {
116
      $attributes = array(
117
        'class' => array('media-browser-tab'),
118
        'id' => $tabid,
119
        'data-tabid' => $key,
120
      );
121
      // Create a div for each tab's content.
122
      $plugin_output[$key] += array(
123
        '#prefix' => '<div '. drupal_attributes($attributes) . ">\n",
124
        '#suffix' => "</div>\n",
125
      );
126
    }
127

    
128
    $attributes = array(
129
      'href' => '#' . $tabid,
130
      'data-tabid' => $key,
131
      'title' => $plugin_output[$key]['#title'],
132
    );
133
    $tabs[]['element'] = array(
134
      '#markup' => '<li><a' . drupal_attributes($attributes) . '>' . check_plain($plugin_output[$key]['#title']) . "</a></li>\n",
135
    );
136
  }
137

    
138
  drupal_add_js($settings, 'setting');
139

    
140
  $output['tabset']['tabs'] = array(
141
    '#theme' => 'menu_local_tasks',
142
    '#attributes' => array('class' => array('tabs', 'primary')),
143
    '#primary' => $tabs,
144
  );
145

    
146
  $output['tabset']['panes'] = $plugin_output;
147

    
148
  return $output;
149
}
150

    
151
/**
152
 * Attaches media browser javascript to an element.
153
 *
154
 * @param array $element
155
 *   The element array to attach to.
156
 */
157
function media_attach_browser_js(&$element) {
158
  $javascript = media_browser_js();
159
  foreach ($javascript as $key => $definitions) {
160
    foreach ($definitions as $definition) {
161
      $element['#attached'][$key][] = $definition;
162
    }
163
  }
164
}
165

    
166
/**
167
 * Helper function to define browser javascript.
168
 */
169
function media_browser_js() {
170
  $settings = array(
171
    'browserUrl' => url('media/browser', array(
172
      'query' => array(
173
        'render' => 'media-popup'
174
      ))
175
    ),
176
    'styleSelectorUrl' => url('media/-media_id-/format-form', array(
177
      'query' => array(
178
        'render' => 'media-popup'
179
      ))
180
    ),
181
    'dialogOptions' => array(
182
      'dialogclass' => variable_get('media_dialogclass', 'media-wrapper'),
183
      'modal' => (boolean)variable_get('media_modal', TRUE),
184
      'draggable' => (boolean)variable_get('media_draggable', FALSE),
185
      'resizable' => (boolean)variable_get('media_resizable', FALSE),
186
      'minwidth' => (int)variable_get('media_minwidth', 500),
187
      'width' => (int)variable_get('media_width', 670),
188
      'height' => (int)variable_get('media_height', 280),
189
      'position' => variable_get('media_position', 'center'),
190
      'overlay' => array(
191
        'backgroundcolor' => variable_get('media_backgroundcolor', '#000000'),
192
        'opacity' => (float)variable_get('media_opacity', 0.4),
193
      ),
194
      'zindex' => (int)variable_get('media_zindex', 10000),
195
    ),
196
  );
197

    
198
  return array(
199
    'library' => array(
200
      array('media', 'media_browser'),
201
    ),
202
    'js' => array(
203
      array(
204
       'data' => array('media' => $settings),
205
       'type' => 'setting',
206
      ),
207
    ),
208
  );
209
}
210

    
211
/**
212
 * Menu callback for testing the media browser.
213
 */
214
function media_browser_testbed($form) {
215
  media_attach_browser_js($form);
216

    
217
  $form['test_element'] = array(
218
    '#type' => 'media',
219
    '#media_options' => array(
220
      'global' => array(
221
        'types' => array('video', 'audio'),
222
      ),
223
    ),
224
  );
225

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

    
228
  $form['options'] = array(
229
    '#type' => 'textarea',
230
    '#title' => 'Options (JSON)',
231
    '#rows' => 10,
232
  );
233

    
234
  $form['launcher'] = array(
235
    '#markup' => $launcher,
236
  );
237

    
238
  $form['result'] = array(
239
    '#type' => 'textarea',
240
    '#title' => 'Result',
241
  );
242

    
243
  $js = <<<EOF
244
    Drupal.behaviors.mediaTest = {
245
    attach: function(context, settings) {
246
      var delim = "---";
247
      var recentOptions = [];
248
      var recentOptionsCookie = jQuery.cookie("recentOptions");
249
      if (recentOptionsCookie) {
250
        recentOptions = recentOptionsCookie.split("---");
251
      }
252

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

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

    
259
      jQuery.each(recentOptions, function (idx, val) {
260
        recentSelectBox.append(jQuery('<option></option>').val(val).html(val));
261
      });
262

    
263

    
264
      jQuery('#launcher').click(function () {
265
        jQuery('#edit-result').val('');
266
        var options = {};
267
        var optionsTxt = jQuery('#edit-options').val();
268
        if (optionsTxt) {
269
          // Store it in the recent box
270
          recentOptionsCookie += "---" + optionsTxt
271
          jQuery.cookie("recentOptions", recentOptionsCookie, { expires: 7 });
272
          recentSelectBox.append(jQuery('<option></option>').val(optionsTxt).html(optionsTxt));
273
          options = eval('(' + optionsTxt + ')');
274
        }
275
        Drupal.media.popups.mediaBrowser(Drupal.behaviors.mediaTest.mediaSelected, options);
276
        return false;
277
      });
278
    },
279

    
280
    mediaSelected: function(selectedMedia) {
281
      var result = JSON.stringify(selectedMedia);
282
        jQuery('#edit-result').val(result);
283
    }
284
  }
285

    
286
EOF;
287

    
288
  drupal_add_js($js, array('type' => 'inline'));
289
  return $form;
290
}
291

    
292
/**
293
 * Adds properties to the file.
294
 *
295
 * Additional properties on this file are needed by the media browser JS code.
296
 */
297
function media_browser_build_media_item($file) {
298
  $preview = media_get_thumbnail_preview($file);
299
  $file->preview = drupal_render($preview);
300
  $file->url = file_create_url($file->uri);
301
}