Projet

Général

Profil

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

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

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
      $view_mode = isset($params['view_mode']) ? $params['view_mode'] : 'preview';
28
      media_browser_build_media_item($file, $view_mode);
29
    }
30
    $setting = array('media' => array('selectedMedia' => array_values($files)));
31
    drupal_add_js($setting, 'setting');
32
    return $output;
33
  }
34

    
35
  $plugins = media_get_browser_plugin_info();
36

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

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

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

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

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

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

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

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

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

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

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

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

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

    
149
  return $output;
150
}
151

    
152
/**
153
 * Menu callback for testing the media browser.
154
 */
155
function media_browser_testbed($form) {
156
  $form['#attached']['library'][] = array('media', 'media_browser');
157
  $form['#attached']['library'][] = array('media', 'media_browser_settings');
158

    
159
  $form['test_element'] = array(
160
    '#type' => 'media',
161
    '#media_options' => array(
162
      'global' => array(
163
        'types' => array('video', 'audio'),
164
      ),
165
    ),
166
  );
167

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

    
170
  $form['options'] = array(
171
    '#type' => 'textarea',
172
    '#title' => 'Options (JSON)',
173
    '#rows' => 10,
174
  );
175

    
176
  $form['launcher'] = array(
177
    '#markup' => $launcher,
178
  );
179

    
180
  $form['result'] = array(
181
    '#type' => 'textarea',
182
    '#title' => 'Result',
183
  );
184

    
185
  $js = <<<EOF
186
    Drupal.behaviors.mediaTest = {
187
    attach: function(context, settings) {
188
      var delim = "---";
189
      var recentOptions = [];
190
      var recentOptionsCookie = jQuery.cookie("recentOptions");
191
      if (recentOptionsCookie) {
192
        recentOptions = recentOptionsCookie.split("---");
193
      }
194

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

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

    
201
      jQuery.each(recentOptions, function (idx, val) {
202
        recentSelectBox.append(jQuery('<option></option>').val(val).html(val));
203
      });
204

    
205

    
206
      jQuery('#launcher').click(function () {
207
        jQuery('#edit-result').val('');
208
        var options = {};
209
        var optionsTxt = jQuery('#edit-options').val();
210
        if (optionsTxt) {
211
          // Store it in the recent box
212
          recentOptionsCookie += "---" + optionsTxt
213
          jQuery.cookie("recentOptions", recentOptionsCookie, { expires: 7 });
214
          recentSelectBox.append(jQuery('<option></option>').val(optionsTxt).html(optionsTxt));
215
          options = eval('(' + optionsTxt + ')');
216
        }
217
        Drupal.media.popups.mediaBrowser(Drupal.behaviors.mediaTest.mediaSelected, options);
218
        return false;
219
      });
220
    },
221

    
222
    mediaSelected: function(selectedMedia) {
223
      var result = JSON.stringify(selectedMedia);
224
        jQuery('#edit-result').val(result);
225
    }
226
  }
227

    
228
EOF;
229

    
230
  drupal_add_js($js, array('type' => 'inline'));
231
  return $form;
232
}
233

    
234
/**
235
 * Adds additional properties to a file which are needed by the browser JS code.
236
 *
237
 * @param object $file
238
 *   A Drupal file object.
239
 */
240
function media_browser_build_media_item($file, $view_mode = 'preview') {
241
  $preview = media_get_thumbnail_preview($file, NULL, $view_mode);
242
  $file->preview = drupal_render($preview);
243
  $file->url = file_create_url($file->uri);
244
}