- Patch #556832 by dropcube: add text format API to manipulate formats.

merge-requests/26/head
Dries Buytaert 2009-08-26 04:59:26 +00:00
parent fd164e9d02
commit 99a746f1ac
2 changed files with 102 additions and 88 deletions

View File

@ -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;
$format = (object) $form_state['values'];
$format->format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL;
$status = filter_format_save($format);
// 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) . ',';
}
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';

View File

@ -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.
*/