diff --git a/modules/filter/filter.admin.inc b/modules/filter/filter.admin.inc index e3210e4ec34a7eda6d2b6484e439dfc8f1ccc6a6..ad6570c13569ea7297d41495728483228567a834 100644 --- a/modules/filter/filter.admin.inc +++ b/modules/filter/filter.admin.inc @@ -197,74 +197,24 @@ function filter_admin_format_form_validate($form, &$form_state) { * 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); - $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' => $format_name)) - ->execute(); - $format = db_query("SELECT MAX(format) AS format FROM {filter_format}")->fetchField(); - drupal_set_message(t('Added text format %format.', array('%format' => $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', 'name', 'weight')); - foreach ($form_state['values']['filters'] as $name => $checked) { - if ($checked) { - // Add new filters to the bottom. - $weight = isset($current[$name]->weight) ? $current[$name]->weight : 10; - $query->values(array( - 'format' => $format, - 'name' => $name, - 'weight' => $weight, - )); - } - $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) . ','; - } + $format = (object) $form_state['values']; + $format->format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL; + $status = filter_format_save($format); - db_update('filter_format') - ->fields(array( - 'cache' => $cache, - 'name' => $format_name, - 'roles' => $roles, - )) - ->condition('format', $format) - ->execute(); - - 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/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; } @@ -300,29 +250,7 @@ function filter_admin_delete() { * 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'; diff --git a/modules/filter/filter.module b/modules/filter/filter.module index 22c94dc701e4e0c65e02cc8990da212dcfcc819e..27b7a4edbd55a06a628074d02f8d6c0cfe372004 100644 --- a/modules/filter/filter.module +++ b/modules/filter/filter.module @@ -144,6 +144,92 @@ function filter_format_load($arg) { return filter_formats($arg); } +/** + * Save a text format object to the database. + * + * @param $format + * A format object. + */ +function 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 the default format. + // We use leading and trailing comma's to allow easy substring matching. + $roles = array_filter($format->roles); + if ($format->format == variable_get('filter_default_format', 1)) { + $roles = ',' . implode(',', array_keys(user_roles())) . ','; + } + else { + $roles = ',' . implode(',',array_keys($roles)) . ','; + } + $format->roles = $roles; + $format->name = trim($format->name); + + // Add a new text format. + if (empty($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(); + + // Get the filters currently active in the format, to add new filters + // to the bottom. + $current = filter_list_format($format->format); + $query = db_insert('filter')->fields(array('format', 'name', 'weight')); + $filters = $format->filters; + + foreach (array_keys(array_filter($filters)) as $name) { + // Add new filters to the bottom. + $weight = isset($current[$name]->weight) ? $current[$name]->weight : 10; + $query->values(array( + 'format' => $format->format, + 'name' => $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. */