Index: modules/filter/filter.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.admin.inc,v
retrieving revision 1.19
diff -u -p -r1.19 filter.admin.inc
--- modules/filter/filter.admin.inc	3 Dec 2008 19:43:21 -0000	1.19
+++ modules/filter/filter.admin.inc	23 Dec 2008 23:36:46 -0000
@@ -192,64 +192,37 @@ function filter_admin_format_form_valida
  * Process filter format form submissions.
  */
 function filter_admin_format_form_submit($form, &$form_state) {
-  $format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL;
-  $current = filter_list_format($format);
-  $name = trim($form_state['values']['name']);
-  $cache = TRUE;
+  $format = $form_state['values'];
+  $format += array(
+    'format' => NULL,
+    'name' => trim($format['name']),
+    'cache' => TRUE,
+    'filters' => array(),
+    'roles' => array(),
+    'default_format' => FALSE,
+    'new' => empty($format['format']),
+  );
+  $format['filters'] = array_filter($format['filters']);
+  $format['roles'] = array_filter($format['roles']);
 
-  // Add a new filter format.
-  if (!$format) {
-    $new = TRUE;
-    db_query("INSERT INTO {filter_format} (name) VALUES ('%s')", $name);
-    $format = db_result(db_query("SELECT MAX(format) AS format FROM {filter_format}"));
-    drupal_set_message(t('Added input format %format.', array('%format' => $name)));
-  }
-  else {
-    drupal_set_message(t('The input format settings have been updated.'));
-  }
-
-  db_query("DELETE FROM {filter} WHERE format = %d", $format);
-  foreach ($form_state['values']['filters'] as $id => $checked) {
-    if ($checked) {
-      list($module, $delta) = explode('/', $id);
-      // Add new filters to the bottom.
-      $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10;
-      db_query("INSERT INTO {filter} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format, $module, $delta, $weight);
-
-      // Check if there are any 'no cache' filters.
-      $cache &= !module_invoke($module, 'filter', 'no cache', $delta);
-    }
-  }
+  $format = filter_format_save($format);
 
-  // We store the roles as a string for ease of use.
-  // We should always set all roles to TRUE when saving a default role.
-  // We use leading and trailing comma's to allow easy substring matching.
-  $roles = array();
-  if (isset($form_state['values']['roles'])) {
-    foreach ($form_state['values']['roles'] as $id => $checked) {
-      if ($checked) {
-        $roles[] = $id;
-      }
-    }
-  }
-  if (!empty($form_state['values']['default_format'])) {
-    $roles = ',' . implode(',', array_keys(user_roles())) . ',';
+  if ($format['new']) {
+    drupal_set_message(t('Added input format %format.', array('%format' => $format['name'])));
   }
   else {
-    $roles = ',' . implode(',', $roles) . ',';
+    drupal_set_message(t('The input format settings have been updated.'));
   }
 
-  db_query("UPDATE {filter_format} SET cache = %d, name='%s', roles = '%s' WHERE format = %d", $cache, $name, $roles, $format);
+  cache_clear_all($format['format'] . ':', 'cache_filter', TRUE);
 
-  cache_clear_all($format . ':', 'cache_filter', TRUE);
-
-  // If a new filter was added, return to the main list of filters. Otherwise, stay on edit filter page to show new changes.
+  // If a new filter was added, return to the main list of filters. Otherwise,
+  // stay on edit filter page to show new changes.
   $return = 'admin/settings/filters';
-  if (!empty($new)) {
-    $return .= '/' . $format;
+  if ($format['new']) {
+    $return .= '/' . $format['format'];
   }
   $form_state['redirect'] = $return;
-  return;
 }
 
 /**
@@ -410,3 +383,45 @@ function filter_admin_order_submit($form
 
   cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE);
 }
+
+/**
+ * Save an input format.
+ *
+ * @param $format
+ *   An associative array containing an input format definition.
+ *
+ * @see filter_admin_form_submit()
+ */
+function filter_format_save($format) {
+  $current = filter_list_format($format['format']);
+
+  // Add a new filter format.
+  if (empty($format['format'])) {
+    db_query("INSERT INTO {filter_format} (name) VALUES ('%s')", $format['name']);
+    $format['format'] = db_result(db_query("SELECT MAX(format) AS format FROM {filter_format}"));
+  }
+
+  db_query("DELETE FROM {filter} WHERE format = %d", $format['format']);
+  foreach ($format['filters'] as $id => $checked) {
+    list($module, $delta) = explode('/', $id);
+    // Add new filters to the bottom.
+    $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10;
+    db_query("INSERT INTO {filter} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format['format'], $module, $delta, $weight);
+
+    // Check if there are any 'no cache' filters.
+    $format['cache'] &= !module_invoke($module, 'filter', 'no cache', $delta);
+  }
+
+  // We should always set all roles to TRUE when saving a default role.
+  if (!empty($form_state['values']['default_format'])) {
+    $format['roles'] = user_roles();
+  }
+  // We store the roles as a string for ease of use and use leading and trailing
+  // comma's to allow easy substring matching.
+  $roles = ',' . implode(',', array_keys($format['roles'])) . ',';
+
+  db_query("UPDATE {filter_format} SET cache = %d, name = '%s', roles = '%s' WHERE format = %d", $format['cache'], $format['name'], $roles, $format['format']);
+
+  return $format;
+}
+
