From e64bb939598eddf0c9d0aa432392006d503ce4a3 Mon Sep 17 00:00:00 2001
From: James Elliott <james.elliott@acquia.com>
Date: Tue, 20 Sep 2011 18:00:27 -0400
Subject: [PATCH] Issue #1016376: Expose the media types allowed for WYSIWYG variable in the UI

---
 includes/media.admin.inc |  163 ++++++++++++++++++++++++++++++++++++----------
 media.module             |    9 +++
 2 files changed, 138 insertions(+), 34 deletions(-)

diff --git a/includes/media.admin.inc b/includes/media.admin.inc
index b533089..c8a4051 100644
--- a/includes/media.admin.inc
+++ b/includes/media.admin.inc
@@ -16,7 +16,7 @@ require_once dirname(__FILE__) . '/media.pages.inc';
  */
 function media_admin($form, $form_state) {
   global $user;
-
+  
   $path = drupal_get_path('module', 'media');
 
   $form['#attached'] = array(
@@ -31,7 +31,7 @@ function media_admin($form, $form_state) {
 
   require_once dirname(__FILE__) . '/media.browser.inc';
   media_attach_browser_js($form);
-
+  
   $types = media_display_types();
 
   if (arg(3)) {
@@ -102,24 +102,24 @@ function media_admin_list() {
 
   // Build the sortable table header.
   $header = array(
-    'title' => array('data' => t('Title'), 'field' => 'f.filename'),
-    'type' => array('data' => t('Type'), 'field' => 'f.filemime'),
-    'size' => array('data' => t('Size'), 'field' => 'f.filesize'),
-    'author' => array('data' => t('Author'), 'field' => 'u.name'),
-    'timestamp' => array('data' => t('Updated'), 'field' => 'f.timestamp', 'sort' => 'asc'),
-    'operations' => array('data' => t('Operations')),
+   'title' => array('data' => t('Title'), 'field' => 'f.filename'),
+   'type' => array('data' => t('Type'), 'field' => 'f.filemime'),
+   'size' => array('data' => t('Size'), 'field' => 'f.filesize'),
+   'author' => array('data' => t('Author'), 'field' => 'u.name'),
+   'timestamp' => array('data' => t('Updated'), 'field' => 'f.timestamp', 'sort' => 'asc'),
+   'operations' => array('data' => t('Operations')),
   );
 
   $query = db_select('file_managed', 'f')->extend('PagerDefault')->extend('TableSort');
   $query->join('users', 'u', 'f.uid = u.uid');
 
   $result = $query
-    ->fields('f')
-    ->fields('u', array('name'))
-    ->condition('f.status', FILE_STATUS_PERMANENT)
-    ->limit($limit)
-    ->orderByHeader($header)
-    ->execute();
+   ->fields('f')
+   ->fields('u', array('name'))
+   ->condition('f.status', FILE_STATUS_PERMANENT)
+   ->limit($limit)
+   ->orderByHeader($header)
+   ->execute();
 
   $destination = drupal_get_destination();
   $files = array();
@@ -137,11 +137,11 @@ function media_admin_list() {
   }
 
   $form['files'] = array(
-    '#type' => 'tableselect',
-    '#header' => $header,
-    '#options' => $options,
-    '#empty' => t('No media available.'),
-    '#attributes' => array('class' => array('media-display-table', 'media-clear')),
+   '#type' => 'tableselect',
+   '#header' => $header,
+   '#options' => $options,
+   '#empty' => t('No media available.'),
+   '#attributes' => array('class' => array('media-display-table', 'media-clear')),
   );
   $form['pager'] = array('#markup' => theme('pager', array('tags' => NULL)));
 
@@ -168,19 +168,19 @@ function media_admin_thumbnails() {
   $rows = array();
   $options = array();
   $form['files'] = array(
-    '#tree' => TRUE,
-    '#prefix' => '<div class="media-display-thumbnails media-clear clearfix"><ul class="media-list-thumbnails">',
-    '#suffix' => '</ul></div>',
+   '#tree' => TRUE,
+   '#prefix' => '<div class="media-display-thumbnails media-clear clearfix"><ul class="media-list-thumbnails">',
+   '#suffix' => '</ul></div>',
   );
 
   foreach ($files as $file) {
-    $preview = media_get_thumbnail_preview($file, TRUE);
-    $form['files'][$file->fid] = array(
-      '#type' => 'checkbox',
-      '#title' => '',
-      '#prefix' => '<li>' . drupal_render($preview),
-      '#suffix' => '</li>',
-    );
+   $preview = media_get_thumbnail_preview($file, TRUE);
+   $form['files'][$file->fid] = array(
+     '#type' => 'checkbox',
+     '#title' => '',
+     '#prefix' => '<li>' . drupal_render($preview),
+     '#suffix' => '</li>',
+   );
   }
 
   $form['pager'] = array('#markup' => theme('pager', array('tags' => NULL)));
@@ -286,7 +286,7 @@ function media_admin_type_manage_form($form, &$form_state, $media_type) {
     '#type' => 'value',
     '#value' => $media_type->name,
   );
-
+  
   // If this Media type is handled by us, then we can put in some default
   // options. Otherwise, we leave it to the implementing module to form_alter.
   if ($media_type->type_callback == 'media_is_type') {
@@ -321,7 +321,7 @@ function media_admin_type_manage_form($form, &$form_state, $media_type) {
 
     // Options for allowed Streams.
     $options = array('public' => t('Public files'), 'private' => t('Private files'));
-    foreach (file_get_stream_wrappers() as $stream => $wrapper) {
+    foreach(file_get_stream_wrappers() as $stream => $wrapper) {
       $options[$stream] = $wrapper['name'];
     }
     unset($options['temporary']);
@@ -449,7 +449,7 @@ function media_import_validate($form, &$form_state) {
     }
     $pattern = !empty($pattern) ? $pattern :  '*';
     $files = glob("$directory/$pattern");
-    if (empty($files)) {
+    if (count($files) == 1) {
       form_set_error('pattern', t('No files were found in %directory matching %pattern', array('%directory' => $directory, '%pattern' => $pattern)));
     }
     $form_state['storage']['files'] = $files;
@@ -460,7 +460,7 @@ function media_import_validate($form, &$form_state) {
  * Submit handler for media_import().
  */
 function media_import_submit($form, &$form_state) {
-  if ($form_state['values']['op'] == t('Confirm')) {
+  if ($form_state['values']['op'] == 'Confirm') {
     $files = $form_state['storage']['files'];
     $batch = array(
       'title' => t('Importing'),
@@ -506,7 +506,7 @@ function media_import_batch_import_files($files, &$context) {
         $image_in_message = file_view_file($loaded_file, 'media_preview');
       }
     }
-    catch (Exception $e) {
+    catch(Exception $e) {
       $context['results']['errors'][] = $file . " Reason: " . $e->getMessage();
     }
   }
@@ -567,3 +567,98 @@ function media_admin_config_browser_pre_submit(&$form, &$form_state) {
     unset($form_state['values'][media_variable_name('dialog_theme')]);
   }
 }
+
+/**
+ * Confirmation form for rebuliding the file_managed table to include type
+ * in rows where there is no type.
+ */
+function media_admin_rebuild_types_form($form, &$form_state) {
+  $total = media_type_invalid_files_count();
+  if ($total == 0) {
+    media_variable_del('show_file_type_rebuild_nag');
+    // @TODO: Make this not sound stupid.
+    drupal_set_message('All files in the system have been assigned types. Media installation complete.');
+    drupal_goto('admin');
+  }
+  $form['total'] = array('#type' => 'value', '#value' => $total);
+  return confirm_form($form, 'Update types for existing files', 'admin/config/media', 'This process is required when installing media on an existing site.  Media needs to scan through existing files and identify the file type. <br/><strong>Update types for ' . $total . ' files?</strogn>');
+}
+
+/**
+ * @see media_admin_rebuild_types_form().
+ */
+function media_admin_rebuild_types_form_submit(&$form, &$form_state) {
+  $total = $form_state['values']['total'];
+
+  $batch = array(
+    'title' => t('Rebuilding type information for ' . $total . ' files'),
+    'operations' => array(
+      array('media_admin_rebuild_types_batch_op', array($total)),
+    ),
+    'finished' => 'media_admin_rebuild_types_batch_complete',
+    'file' => drupal_get_path('module', 'media') . '/includes/media.admin.inc',
+  );
+  batch_set($batch);
+}
+
+/**
+ * Batch operation for fixing the file_managed table for media, adding type values
+ * where no value exists.
+ */
+function media_admin_rebuild_types_batch_op($total, &$context) {
+  $per_run = media_variable_get('media_type_batch_update_per_run', 100);
+  $context['results'] = array_merge($context['results'], media_type_batch_update(FALSE, $per_run));
+  $context['finished'] = count($context['results']) / $total;
+}
+/**
+ * Sets a message informing the user how many file records were updated.
+ */
+function media_admin_rebuild_types_batch_complete($success, $results, $operations) {
+  if ($success) {
+    $message = format_plural(count($results), 'One file identified and given a type.', '@count files identified and given a type.');
+    media_variable_del('show_file_type_rebuild_nag');
+  }
+  drupal_set_message($message);
+}
+
+/**
+ * Provide a UI for enabling media types in the WYSIWYG.
+ */
+function media_admin_wysiwyg_types($form, &$form_state) {
+  $types = array();
+
+  foreach(media_type_get_types() as $type) {
+    $types[$type->name] = $type->label;
+  }
+
+  $form['types'] = array(
+    '#title' => t('Select the media types available in the WYSIWYG'),
+    '#type' => 'checkboxes',
+    '#options' => $types,
+    '#default_value' => media_variable_get('wysiwyg_allowed_types'),
+  );
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save'),
+  );
+
+  return $form;
+}
+
+/**
+ * Submit handler for media_admin_wysiwyg_types()
+ */
+function media_admin_wysiwyg_types_submit($form, &$form_state) {
+  $types = array();
+
+  foreach($form_state['values']['types'] as $key => $value) {
+    if ($value != '0') {
+      $types[] = $key;
+    }
+  }
+  
+  drupal_set_message('WYSIWYG media types updated');
+
+  media_variable_set('wysiwyg_allowed_types', $types);
+}
diff --git a/media.module b/media.module
index 3f1f617..eb91d26 100644
--- a/media.module
+++ b/media.module
@@ -78,6 +78,15 @@ function media_menu() {
     'file' => 'includes/media.admin.inc',
   );
 
+  $items['admin/config/media/wysiwyg_types'] = array(
+    'title' => 'WYSIWYG media types',
+    'description' => 'Configure media types available for the WYSIWYG',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('media_admin_wysiwyg_types'),
+    'access arguments' => array('administer media'),
+    'file' => 'includes/media.admin.inc',
+  );
+
   // Settings used for determining the type of media a file is.
   // @todo Find a new home for this that integrates with the file_entity module.
 //  $items['admin/config/media/types/manage/%media_type'] = array(
-- 
1.7.4.msysgit.0

