Index: modules/filter/filter.module =================================================================== RCS file: /cvs/drupal/drupal/modules/filter/filter.module,v retrieving revision 1.273 diff -u -r1.273 filter.module --- modules/filter/filter.module 22 Aug 2009 14:34:20 -0000 1.273 +++ modules/filter/filter.module 22 Aug 2009 22:17:04 -0000 @@ -138,6 +138,100 @@ } /** + * Save a text format object to the database. + * + * @param $format + * A format object. + */ +function filter_format_save($format) { + $current = filter_list_format($format->format); + $format->name = trim($format->name); + $filters = $format->filters; + + // 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($format->roles)) { + foreach ($format->roles as $id => $checked) { + if ($checked) { + $roles[] = $id; + } + } + } + if (!empty($form_state['values']['default_format'])) { + $roles = ',' . implode(',', array_keys(user_roles())) . ','; + } + else { + $roles = ',' . implode(',', $roles) . ','; + } + $format->roles = $roles; + + // Add a new text format. + if (!$format->format) { + $status = drupal_write_record('filter_format', $format); + } + else { + $status = drupal_write_record('filter_format', $format, 'format'); + } + + db_delete('filter') + ->condition('format', $format->format) + ->execute(); + + $query = db_insert('filter')->fields(array('format', 'module', 'name', 'weight')); + foreach ($filters as $id => $checked) { + if ($checked) { + list($module, $filter_name) = explode('/', $id); + // Add new filters to the bottom. + $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10; + $query->values(array( + 'format' => $format->format, + 'module' => $module, + 'name' => $filter_name, + 'weight' => $weight, + )); + } + $query->execute(); + } + + cache_clear_all($format->format . ':', 'cache_filter', TRUE); + return $status; +} + +/** + * Delete a text format. + * + * @param $format + * The format to be deleted. + */ +function filter_format_delete($format) { + db_delete('filter_format') + ->condition('format', $format) + ->execute(); + db_delete('filter') + ->condition('format', $format) + ->execute(); + + $default = variable_get('filter_default_format', 1); + // Replace existing instances of the deleted format with the default format. + if (db_table_exists('comment')) { + db_update('comment') + ->fields(array('format' => $default)) + ->condition('format', $format) + ->execute(); + } + if (db_table_exists('box')) { + db_update('box') + ->fields(array('format' => $default)) + ->condition('format', $format) + ->execute(); + } + + cache_clear_all($format . ':', 'cache_filter', TRUE); +} + +/** * Display a text format form title. */ function filter_admin_format_title($format) { Index: modules/filter/filter.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/filter/filter.admin.inc,v retrieving revision 1.34 diff -u -r1.34 filter.admin.inc --- modules/filter/filter.admin.inc 22 Aug 2009 14:34:20 -0000 1.34 +++ modules/filter/filter.admin.inc 22 Aug 2009 22:16:47 -0000 @@ -197,78 +197,23 @@ * Process text 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; - - // Add a new text format. - if (!$format) { - $new = TRUE; - db_insert('filter_format') - ->fields(array('name' => $name)) - ->execute(); - $format = db_query("SELECT MAX(format) AS format FROM {filter_format}")->fetchField(); - drupal_set_message(t('Added text format %format.', array('%format' => $name))); - } - else { - drupal_set_message(t('The text format settings have been updated.')); - } - db_delete('filter') - ->condition('format', $format) - ->execute(); - $query = db_insert('filter')->fields(array('format', 'module', 'name', 'weight')); - foreach ($form_state['values']['filters'] as $id => $checked) { - if ($checked) { - list($module, $filter_name) = explode('/', $id); - // Add new filters to the bottom. - $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10; - $query->values(array( - 'format' => $format, - 'module' => $module, - 'name' => $filter_name, - 'weight' => $weight, - )); - // Check if there are any 'no cache' filters. - $cache &= !module_invoke($module, 'filter', 'no cache', $filter_name); - } - $query->execute(); - } - - // 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())) . ','; - } - else { - $roles = ',' . implode(',', $roles) . ','; - } - - db_update('filter_format') - ->fields(array( - 'cache' => $cache, - 'name' => $name, - 'roles' => $roles, - )) - ->condition('format', $format) - ->execute(); - - cache_clear_all($format . ':', 'cache_filter', TRUE); - + $format = (object) $form_state['values']; + $format->format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL; + $status = filter_format_save($format); + // 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/formats'; - if (!empty($new)) { - $return .= '/' . $format; - } + + switch ($status) { + case SAVED_NEW: + drupal_set_message(t('Added text format %format.', array('%format' => $format->name))); + $return .= '/' . $format->format; + break; + case SAVED_UPDATED: + drupal_set_message(t('The text format settings have been updated.')); + break; + } + $form_state['redirect'] = $return; return; } @@ -304,29 +249,7 @@ * Process filter delete form submission. */ function filter_admin_delete_submit($form, &$form_state) { - db_delete('filter_format') - ->condition('format', $form_state['values']['format']) - ->execute(); - db_delete('filter') - ->condition('format', $form_state['values']['format']) - ->execute(); - - $default = variable_get('filter_default_format', 1); - // Replace existing instances of the deleted format with the default format. - if (db_table_exists('comment')) { - db_update('comment') - ->fields(array('format' => $default)) - ->condition('format', $form_state['values']['format']) - ->execute(); - } - if (db_table_exists('box')) { - db_update('box') - ->fields(array('format' => $default)) - ->condition('format', $form_state['values']['format']) - ->execute(); - } - - cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE); + filter_format_delete($form_state['values']['format']); drupal_set_message(t('Deleted text format %format.', array('%format' => $form_state['values']['name']))); $form_state['redirect'] = 'admin/settings/formats';