summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDries Buytaert2009-08-26 04:59:26 (GMT)
committerDries Buytaert2009-08-26 04:59:26 (GMT)
commit99a746f1ac9a8bf978beac96ca0d469c7c37655a (patch)
tree9e4848bd9a2cf98e48ea30e2eceb28e7c5d38183
parentfd164e9d0239ee95f6b6c9789ed6adfe2a5b0457 (diff)
- Patch #556832 by dropcube: add text format API to manipulate formats.
-rw-r--r--modules/filter/filter.admin.inc104
-rw-r--r--modules/filter/filter.module86
2 files changed, 102 insertions, 88 deletions
diff --git a/modules/filter/filter.admin.inc b/modules/filter/filter.admin.inc
index e3210e4..ad6570c 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 22c94dc..27b7a4e 100644
--- a/modules/filter/filter.module
+++ b/modules/filter/filter.module
@@ -145,6 +145,92 @@ function filter_format_load($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.
*/
function filter_admin_format_title($format) {