media.browser.inc

  1. cis7 sites/all/modules/ulmus/media/includes/media.browser.inc
  2. cle7 sites/all/modules/ulmus/media/includes/media.browser.inc
  3. ecd7 sites/all/modules/ulmus/media/includes/media.browser.inc
  4. elmsmedia7 sites/all/modules/ulmus/media/includes/media.browser.inc
  5. harmony7 sites/all/modules/ulmus/media/includes/media.browser.inc
  6. icor7 sites/all/modules/ulmus/media/includes/media.browser.inc
  7. meedjum_blog7 sites/all/modules/ulmus/media/includes/media.browser.inc
  8. mooc7 sites/all/modules/ulmus/media/includes/media.browser.inc

Summon plugins and render the media browser.

Functions

Namesort descending Description
media_attach_browser_js Attaches media browser javascript to an element.
media_browser Media browser page callback.
media_browser_build_media_item Adds properties to the file.
media_browser_js Helper function to define browser javascript.
media_browser_testbed Menu callback for testing the media browser.
media_get_browser_params For sanity in grammar.
media_set_browser_params Provides a singleton of the params passed to the media browser.

File

sites/all/modules/ulmus/media/includes/media.browser.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Summon plugins and render the media browser.
  5. */
  6. /**
  7. * Media browser page callback.
  8. */
  9. function media_browser($selected = NULL) {
  10. $output = array();
  11. $output['#attached']['library'][] = array('media', 'media_browser_page');
  12. $params = media_set_browser_params();
  13. // If one or more files have been selected, the browser interaction is now
  14. // complete. Return empty page content to the dialog which now needs to close,
  15. // but populate Drupal.settings with information about the selected files.
  16. if (isset($params['fid'])) {
  17. $fids = is_array($params['fid']) ? $params['fid'] : array($params['fid']);
  18. if (!is_numeric($fids[0])) {
  19. throw new Exception('Error selecting media, fid param is not an fid or an array of fids');
  20. }
  21. $files = file_load_multiple($fids);
  22. foreach ($files as $file) {
  23. media_browser_build_media_item($file);
  24. }
  25. $setting = array('media' => array('selectedMedia' => array_values($files)));
  26. drupal_add_js($setting, 'setting');
  27. return $output;
  28. }
  29. $plugins = media_get_browser_plugin_info();
  30. // Allow parameters to provide a list of enabled or disabled media browser
  31. // plugins.
  32. if (!empty($params['enabledPlugins'])) {
  33. $plugins = array_intersect_key($plugins, array_fill_keys($params['enabledPlugins'], 1));
  34. }
  35. elseif (!empty($params['disabledPlugins'])) {
  36. $plugins = array_diff_key($plugins, array_fill_keys($params['disabledPlugins'], 1));
  37. }
  38. // Render plugins.
  39. $plugin_output = array();
  40. foreach ($plugins as $key => $plugin_info) {
  41. // Support the old CTools style handler definition.
  42. if (!isset($plugin_info['class']) && !empty($plugin_info['handler'])) {
  43. if (is_string($plugin_info['handler'])) {
  44. $plugin_info['class'] = $plugin_info['handler'];
  45. }
  46. elseif (isset($plugin_info['handler']['class'])) {
  47. $plugin_info['class'] = $plugin_info['handler']['class'];
  48. }
  49. }
  50. if (empty($plugin_info['class']) || !class_exists($plugin_info['class'])) {
  51. continue;
  52. }
  53. $plugin = new $plugin_info['class']($plugin_info, $params);
  54. if ($plugin->access()) {
  55. $plugin_output[$key] = $plugin->view();
  56. if (!empty($plugin_output[$key]) && is_array($plugin_output[$key])) {
  57. $plugin_output[$key] += array(
  58. '#title' => $plugin_info['title'],
  59. '#weight' => isset($plugin_info['weight']) ? $plugin_info['weight'] : 0,
  60. );
  61. }
  62. else {
  63. unset($plugin_output[$key]);
  64. continue;
  65. }
  66. }
  67. else {
  68. continue;
  69. }
  70. // We need to get a submit and cancel button on each tab. If the plugin
  71. // is not returning a form element we need to add a submit button.
  72. // This is a fairly broad assumption.
  73. if (empty($plugin_output[$key]['#form']) && !empty($plugin_output[$key]['#markup'])) {
  74. $fake_buttons = '<div class="form-actions form-wrapper">';
  75. $fake_buttons .= l(t('Submit'), '', array(
  76. 'attributes' => array(
  77. 'class' => array('button', 'button-yes', 'fake-submit', $key),
  78. ),
  79. ));
  80. $fake_buttons .= l(t('Cancel'), '', array(
  81. 'attributes' => array(
  82. 'class' => array('button', 'button-no', 'fake-cancel', $key),
  83. ),
  84. ));
  85. $fake_buttons .= '</div>';
  86. $plugin_output[$key]['#markup'] .= $fake_buttons;
  87. }
  88. // I'm not sure if it is ever the case that a plugin form will ever have
  89. // the correct cancel button so we add it here. Put it inline with the
  90. // current submit button. This is a fairly broad assumption.
  91. if (!empty($plugin_output[$key]['form']['actions']) && !isset($plugin_output[$key]['form']['actions']['cancel'])) {
  92. $plugin_output[$key]['form']['actions']['cancel'] = array(
  93. '#type' => 'link',
  94. '#title' => t('Cancel'),
  95. '#href' => '',
  96. '#attributes' => array(
  97. 'class' => array(
  98. 'button',
  99. 'button-no',
  100. 'fake-cancel',
  101. $key,
  102. ),
  103. ),
  104. '#weight' => 100,
  105. );
  106. }
  107. }
  108. // Allow modules to change the tab names or whatever else they want to change
  109. // before we render. Perhaps this should be an alter on the theming function
  110. // that we should write to be making the tabs.
  111. drupal_alter('media_browser_plugins', $plugin_output);
  112. $tabs = array();
  113. $settings = array('media' => array('browser' => array()));
  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. // If this is a "ajax" style tab, add the href, otherwise an id. jQuery UI
  120. // will use an href value to load content from that url
  121. $tabid = 'media-tab-' . check_plain($key);
  122. if (!empty($plugin_output[$key]['#callback'])) {
  123. $href = $plugin_output[$key]['#callback'];
  124. }
  125. else {
  126. $attributes = array(
  127. 'class' => array('media-browser-tab'),
  128. 'id' => $tabid,
  129. 'data-tabid' => $key,
  130. );
  131. // Create a div for each tab's content.
  132. $plugin_output[$key] += array(
  133. '#prefix' => '<div '. drupal_attributes($attributes) . ">\n",
  134. '#suffix' => "</div>\n",
  135. );
  136. }
  137. $attributes = array(
  138. 'href' => '#' . $tabid,
  139. 'data-tabid' => $key,
  140. 'title' => $plugin_output[$key]['#title'],
  141. );
  142. $tabs[]['element'] = array(
  143. '#markup' => '<li><a' . drupal_attributes($attributes) . '>' . check_plain($plugin_output[$key]['#title']) . "</a></li>\n",
  144. );
  145. }
  146. drupal_add_js($settings, 'setting');
  147. $output['title'] = array(
  148. '#markup' => t('Select a file')
  149. );
  150. $output['tabset']['tabs'] = array(
  151. '#theme' => 'menu_local_tasks',
  152. '#attributes' => array('class' => array('tabs', 'primary')),
  153. '#primary' => $tabs,
  154. );
  155. $output['tabset']['panes'] = $plugin_output;
  156. return $output;
  157. }
  158. /**
  159. * Provides a singleton of the params passed to the media browser.
  160. *
  161. * This is useful in situations like form alters because callers can pass
  162. * id="wysiywg_form" or whatever they want, and a form alter could pick this up.
  163. * We may want to change the hook_media_browser_plugin_view() implementations to
  164. * use this function instead of being passed params for consistency.
  165. *
  166. * It also offers a chance for some meddler to meddle with them.
  167. *
  168. * @see media_browser()
  169. */
  170. function media_set_browser_params() {
  171. $params = &drupal_static(__FUNCTION__, array());
  172. if (empty($params)) {
  173. // Build out browser settings. Permissions- and security-related behaviors
  174. // should not rely on these parameters, since they come from the HTTP query.
  175. // @TODO make sure we treat parameters as user input.
  176. $params = drupal_get_query_parameters() + array(
  177. 'types' => array(),
  178. 'multiselect' => FALSE,
  179. );
  180. // Transform text 'true' and 'false' to actual booleans.
  181. foreach ($params as $k => $v) {
  182. if ($v === 'true') {
  183. $params[$k] = TRUE;
  184. }
  185. elseif ($v === 'false') {
  186. $params[$k] = FALSE;
  187. }
  188. }
  189. array_walk_recursive($params, 'media_recursive_check_plain');
  190. // Allow modules to alter the parameters.
  191. drupal_alter('media_browser_params', $params);
  192. }
  193. return $params;
  194. }
  195. /**
  196. * For sanity in grammar.
  197. *
  198. * @see media_set_browser_params()
  199. */
  200. function media_get_browser_params() {
  201. return media_set_browser_params();
  202. }
  203. /**
  204. * Attaches media browser javascript to an element.
  205. *
  206. * @param array $element
  207. * The element array to attach to.
  208. */
  209. function media_attach_browser_js(&$element) {
  210. $javascript = media_browser_js();
  211. foreach ($javascript as $key => $definitions) {
  212. foreach ($definitions as $definition) {
  213. $element['#attached'][$key][] = $definition;
  214. }
  215. }
  216. }
  217. /**
  218. * Helper function to define browser javascript.
  219. */
  220. function media_browser_js() {
  221. $settings = array(
  222. 'browserUrl' => url('media/browser', array(
  223. 'query' => array('render' => 'media-popup'),
  224. )
  225. ),
  226. 'styleSelectorUrl' => url('media/-media_id-/format-form', array(
  227. 'query' => array('render' => 'media-popup'),
  228. )
  229. ),
  230. );
  231. $js = array(
  232. 'library' => array(
  233. array('media', 'media_browser'),
  234. ),
  235. 'js' => array(
  236. array(
  237. 'data' => array('media' => $settings),
  238. 'type' => 'setting',
  239. ),
  240. ),
  241. );
  242. return $js;
  243. }
  244. /**
  245. * Menu callback for testing the media browser.
  246. */
  247. function media_browser_testbed($form) {
  248. media_attach_browser_js($form);
  249. $form['test_element'] = array(
  250. '#type' => 'media',
  251. '#media_options' => array(
  252. 'global' => array(
  253. 'types' => array('video', 'audio'),
  254. ),
  255. ),
  256. );
  257. $launcher = '<a href="#" id="launcher"> Launch Media Browser</a>';
  258. $form['options'] = array(
  259. '#type' => 'textarea',
  260. '#title' => 'Options (JSON)',
  261. '#rows' => 10,
  262. );
  263. $form['launcher'] = array(
  264. '#markup' => $launcher,
  265. );
  266. $form['result'] = array(
  267. '#type' => 'textarea',
  268. '#title' => 'Result',
  269. );
  270. $js = <<<EOF
  271. Drupal.behaviors.mediaTest = {
  272. attach: function(context, settings) {
  273. var delim = "---";
  274. var recentOptions = [];
  275. var recentOptionsCookie = jQuery.cookie("recentOptions");
  276. if (recentOptionsCookie) {
  277. recentOptions = recentOptionsCookie.split("---");
  278. }
  279. var recentSelectBox = jQuery('<select id="recent_options" style="width:100%"></select>').change(function() { jQuery('#edit-options').val(jQuery(this).val())});
  280. jQuery('.form-item-options').append('<label for="recent_options">Recent</a>');
  281. jQuery('.form-item-options').append(recentSelectBox);
  282. jQuery('.form-item-options').append(jQuery('<a href="#">Reset</a>').click(function() {alert('reset'); jQuery.cookie("recentOptions", null); window.location.reload(); }));
  283. jQuery.each(recentOptions, function (idx, val) {
  284. recentSelectBox.append(jQuery('<option></option>').val(val).html(val));
  285. });
  286. jQuery('#launcher').click(function () {
  287. jQuery('#edit-result').val('');
  288. var options = {};
  289. var optionsTxt = jQuery('#edit-options').val();
  290. if (optionsTxt) {
  291. // Store it in the recent box
  292. recentOptionsCookie += "---" + optionsTxt
  293. jQuery.cookie("recentOptions", recentOptionsCookie, { expires: 7 });
  294. recentSelectBox.append(jQuery('<option></option>').val(optionsTxt).html(optionsTxt));
  295. options = eval('(' + optionsTxt + ')');
  296. }
  297. Drupal.media.popups.mediaBrowser(Drupal.behaviors.mediaTest.mediaSelected, options);
  298. return false;
  299. });
  300. },
  301. mediaSelected: function(selectedMedia) {
  302. var result = JSON.stringify(selectedMedia);
  303. jQuery('#edit-result').val(result);
  304. }
  305. }
  306. EOF;
  307. drupal_add_js($js, array('type' => 'inline'));
  308. return $form;
  309. }
  310. /**
  311. * Adds properties to the file.
  312. *
  313. * Additional properties on this file are needed by the media browser JS code.
  314. */
  315. function media_browser_build_media_item($file) {
  316. $preview = media_get_thumbnail_preview($file);
  317. $file->preview = drupal_render($preview);
  318. $file->url = file_create_url($file->uri);
  319. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.