From 2135f33e85bdbeb7ee08aed3b7f19d4a979675bb Mon Sep 17 00:00:00 2001
From: James Elliott <james.elliott@acquia.com>
Date: Fri, 17 Jun 2011 11:42:57 -0400
Subject: [PATCH] Issue #1139514 by effulgentsia, james.elliott: Changed Overhaul the media browser code to not use an iframe, and be more understandable, maintainable, and extendable.

---
 includes/media.admin.inc   |    4 +-
 includes/media.browser.inc |    1 +
 includes/media.filter.inc  |   19 +++++++++++-----
 js/media.format_form.js    |   50 --------------------------------------------
 media.module               |   41 +++++++++++++++++------------------
 5 files changed, 36 insertions(+), 79 deletions(-)
 delete mode 100644 js/media.format_form.js

diff --git a/includes/media.admin.inc b/includes/media.admin.inc
index 4f3a704..e32d530 100644
--- a/includes/media.admin.inc
+++ b/includes/media.admin.inc
@@ -48,7 +48,7 @@ function media_admin($form, $form_state) {
     '#type' => 'submit',
     '#value' => t('Submit'),
     '#submit' => array('media_admin_submit'),
-    '#validate' => array('media_admin_delete_validate'),
+    '#validate' => array('media_admin_validate'),
   );
   
   module_load_include('inc', 'media', '/includes/media.browser');
@@ -77,7 +77,7 @@ function media_admin($form, $form_state) {
  * Check if any files have been selected to perform the chosen
  * 'Update option' on.
  */
-function media_admin_delete_validate($form, &$form_state) {
+function media_admin_validate($form, &$form_state) {
   $files = array_filter($form_state['values']['files']);
 
   if (count($files) == 0) {
diff --git a/includes/media.browser.inc b/includes/media.browser.inc
index 810f410..54e9bf4 100644
--- a/includes/media.browser.inc
+++ b/includes/media.browser.inc
@@ -479,6 +479,7 @@ function media_browser_thumbnails($params) {
 
   $result = $query->execute();
   $ids = key_exists('file', $result) ? array_keys($result['file']) : FALSE;
+  $media_entities = array();
   if ($ids) {
     $media_entities = entity_load('file', $ids);
   }
diff --git a/includes/media.filter.inc b/includes/media.filter.inc
index aee2d21..f78f477 100644
--- a/includes/media.filter.inc
+++ b/includes/media.filter.inc
@@ -279,13 +279,21 @@ function _media_generate_tagMap($text) {
   return $tagmap;
 }
 
-function media_format($media) {
+/**
+ * AJAX callback that handles the media format form dialog.  Given a piece of media
+ * this callback will return a command to the AJAX handler to either display the
+ * media format form or to indicate that the form has been submitted and a format
+ * chosen for the piece of media.
+ * 
+ * @param type $file
+ */
+function media_format($file) {
   $commands = array();
   
   if (!isset($_POST['form_id'])) {
     $commands[] = array(
       'command' => 'mediaBrowserLaunch',
-      'data' => drupal_render(drupal_get_form('media_format_form', $media)),
+      'data' => drupal_render(drupal_get_form('media_format_form', $file)),
     );
   } else {
     list($form, $form_state) = ajax_get_form();
@@ -293,9 +301,9 @@ function media_format($media) {
     
     $commands[] = array(
       'command' => 'mediaFormatSelected',
-      'file' => $media,
+      'file' => $file,
       'viewMode' => $form_state['values']['format'],
-      'formattedMedia' => drupal_render(media_get_file_without_label($media, $form_state['values']['format'])),
+      'formattedMedia' => drupal_render(media_get_file_without_label($file, $form_state['values']['format'])),
       'instanceId' => $form_state['values']['trigger'],
     );
   }
@@ -315,7 +323,6 @@ function media_format_form($form, $form_state, $file) {
   $form['#attributes'] = array(
     'class' => array('format-form'),
   );
-  drupal_add_css(drupal_get_path('module', 'media') . '/css/media.thumbnails.css');
 
   $entity_info = entity_get_info('file');
   $view_modes = $entity_info['view modes'];
@@ -358,8 +365,8 @@ function media_format_form($form, $form_state, $file) {
   drupal_add_library('system', 'form');
 
   $path = drupal_get_path('module', 'media');
-  #$form['#attached']['js'][] = $path . '/js/media.format_form.js';
   $form['#attached']['css'][] = $path . '/css/media-format-form.css';
+  $form['#attached']['css'][] = $path . '/css/media.thumbnails.css';
 
   $form['heading'] = array(
     '#type' => 'markup',
diff --git a/js/media.format_form.js b/js/media.format_form.js
deleted file mode 100644
index d9cae89..0000000
--- a/js/media.format_form.js
+++ /dev/null
@@ -1,50 +0,0 @@
-
-/**
- *  @file
- *  Attach behaviors to formatter radio select when selecting a media's display
- *  formatter.
- */
-
-(function ($) {
-  
-//Drupal.media = Drupal.media || {};
-//Drupal.media.formatForm = Drupal.media.formatForm || {};
-//
-//Drupal.media.mediaFormatSelected = {};
-//
-//Drupal.behaviors.mediaFormatForm = {
-//  attach: function (context, settings) {
-//    // Add "Submit" and "Cancel" buttons inside the IFRAME that trigger the
-//    // behavior of the hidden "OK" and "Cancel" buttons that are outside the
-//    // IFRAME. See Drupal.media.browser.validateButtons() for more details.
-//    $('<a class="button fake-ok">Submit</a>').appendTo($('#media-format-form')).bind('click', Drupal.media.formatForm.submit);
-//    $('<a class="button fake-cancel">Cancel</a>').appendTo($('#media-format-form')).bind('click', Drupal.media.formatForm.submit);
-//
-//    if (Drupal.settings.media_format_form.autosubmit) {
-//      $('.button.fake-ok').click();
-//    }
-//  }
-//};
-//
-//Drupal.media.formatForm.getOptions = function () {
-//  // Get all the values
-//  var ret = {}; $.each($('#media-format-form fieldset#edit-options *').serializeArray(), function (i, field) { ret[field.name] = field.value; });
-//  return ret;
-//};
-//
-//Drupal.media.formatForm.getFormattedMedia = function () {
-//  var formatType = $("select#edit-format option:selected").val();
-//  return { type: formatType, options: Drupal.media.formatForm.getOptions(), html: Drupal.settings.media.formatFormFormats[formatType] };
-//};
-//
-//Drupal.media.formatForm.submit = function () {
-//  // @see Drupal.behaviors.mediaFormatForm.attach().
-//  var buttons = $(parent.window.document.body).find('#mediaStyleSelector').parent('.ui-dialog').find('.ui-dialog-buttonpane button');
-//  if ($(this).hasClass('fake-cancel')) {
-//    buttons[1].click();
-//  } else {
-//    buttons[0].click();
-//  }
-//}
-
-})(jQuery);
diff --git a/media.module b/media.module
index 725a8cb..c9ab410 100644
--- a/media.module
+++ b/media.module
@@ -151,7 +151,7 @@ function media_menu() {
     'type' => MENU_CALLBACK,
     'file' => 'includes/media.browser.inc',
     'delivery callback' => 'ajax_deliver',
-    'theme callback' => 'media_dialog_theme',
+    'theme callback' => 'ajax_base_page_theme',
   );
   
   $items['media/browser/cancel'] = array(
@@ -159,7 +159,7 @@ function media_menu() {
     'page callback' => 'media_browser_dismiss',
     'delivery callback' => 'ajax_deliver',
     'access callback' => TRUE,
-    'theme callback' => 'media_dialog_theme',
+    'theme callback' => 'ajax_base_page_theme',
     'type' => MENU_CALLBACK,
     'file' => 'includes/media.browser.inc',
   );
@@ -195,7 +195,7 @@ function media_menu() {
     'weight' => 0,
     'file' => 'includes/media.filter.inc',
     'delivery callback' => 'ajax_deliver',
-    'theme callback' => 'media_dialog_theme',
+    'theme callback' => 'ajax_base_page_theme',
   );
 
   $items['media/%file'] = array(
@@ -263,11 +263,6 @@ function media_menu() {
   return $items;
 }
 
-function media_dialog_theme() {
-  $ajax_theme = isset($_POST['ajax_page_state']['theme']) ? $_POST['ajax_page_state']['theme'] : variable_get('theme_default', 'bartik');
-  return $ajax_theme;
-}
-
 /**
  * Implements hook_admin_paths().
  */
@@ -433,6 +428,20 @@ function media_styles_style_flush($style) {
 }
 
 /**
+ * Implement hook_page_alter().
+ */
+function media_page_alter(&$page) {
+  // Show a nagging message when the media installation needs to be completed.
+  if (user_access('administer media') && media_variable_get('show_file_type_rebuild_nag')
+    // Prevent form submissions from creating duplicate messages.
+    && ($_SERVER['REQUEST_METHOD'] == 'GET')
+    // Show on all the admin pages, except the batch and the rebuild form.
+    && path_is_admin(current_path()) && (arg(0) != 'batch') && (current_path() != 'admin/config/media/rebuild_types')) {
+    drupal_set_message(t('Media module install is not complete. <a href="@type_rebuild_link">Finish the install</a>.', array('@type_rebuild_link' => url('admin/config/media/rebuild_types'))), 'warning', FALSE);
+  }
+}
+
+/**
  * Implements hook_element_info_alter().
  */
 function media_element_info_alter(&$types) {
@@ -914,7 +923,9 @@ function media_element_process(&$element, &$form_state, $form) {
       ),
     ),
   );
-
+  
+  $element['links']['launcher']['#ajax']['submit']['types'] = $element['#media_options']['global']['types'];
+  
   /**
    * This section handles fields on media when media is added as a field.
    * It is pretty unpolished, so hiding it for now.
@@ -951,18 +962,6 @@ function media_element_process(&$element, &$form_state, $form) {
 
   // Media browser attach code.
   $element['#attached']['js'][] = drupal_get_path('module', 'media') . '/js/media.js';
-
-//  $settings = array();
-//  $setting['media']['elements'][$element['#id']] = $element['#media_options'];
-//
-//  $element['#attached']['js'][] = array(
-//    'type' => 'setting',
-//    'data' => $setting,
-//  );
-
-  // hmm... Might need to think about this.
-  // All settings would likely apply to all media in a multi-value, but what about passing the existing fid?
-
   module_load_include('inc', 'media', 'includes/media.browser');
   media_attach_browser_js($element);
 
-- 
1.7.4.msysgit.0

