Index: modules/filter.module =================================================================== RCS file: /cvs/drupal/drupal/modules/filter.module,v retrieving revision 1.71 diff -u -r1.71 filter.module --- modules/filter.module 25 Aug 2005 21:14:16 -0000 1.71 +++ modules/filter.module 13 Sep 2005 21:56:58 -0000 @@ -27,18 +27,18 @@ case 'admin/filters': return t(' -

Input formats define a way of processing user-supplied text in Drupal. Every input format has its own settings of which filters to apply. Possible filters include stripping out malicious HTML and making URLs clickable.

-

Users can choose between the available input formats when submitting content.

-

Below you can configure which input formats are available to which roles, as well as choose a default input format (used for imported content, for example).

'); +

input filters define a way of processing user-supplied text in Drupal. Every input filter has its own settings of which filters to apply. Possible filters include stripping out malicious HTML and making URLs clickable.

+

Users can choose between the available input filters when submitting content.

+

Below you can configure which input filters are available to which roles, as well as choose a default input filter (used for imported content, for example).

'); case 'admin/filters/'. arg(2): return t(' -

Every filter performs one particular change on the user input, for example stripping out malicious HTML or making URLs clickable. Choose which filters you want to apply to text in this input format.

-

If you notice some filters are causing conflicts in the output, you can rearrange them.

', array('%configure' => url('admin/filters/'. arg(2) .'/configure'), '%order' => url('admin/filters/'. arg(2) .'/order'))); +

Every filter performs one particular change on the user input, for example stripping out malicious HTML or making URLs clickable. Choose which filters you want to apply to text in this input filter.

+

If you notice some filters are causing conflicts in the output, you can rearrange them.

', array('%configure' => url('admin/filters/'. arg(2) .'/configure'))); case 'admin/filters/'. arg(2) .'/configure': return t(' -

If you cannot find the settings for a certain filter, make sure you\'ve enabled it on the list filters tab first.

', array('%url' => url('admin/filters/'. arg(2) .'/list'))); +

If you cannot find the settings for a certain filter, make sure you\'ve enabled it on the view tab first. Note, not all input filters have settings.

', array('%url' => url('admin/filters/'. arg(2)))); case 'admin/filters/'. arg(2) .'/order': return t(' @@ -201,11 +201,22 @@ $items = array(); if ($may_cache) { - $items[] = array('path' => 'admin/filters', 'title' => t('input formats'), + $items[] = array('path' => 'admin/filters', 'title' => t('input filters'), 'callback' => 'filter_admin_overview', 'access' => user_access('administer filters')); - $items[] = array('path' => 'admin/filters/delete', 'title' => t('delete input format'), + $items[] = array('path' => 'admin/filters/list', 'title' => t('list'), + 'callback' => 'filter_admin_overview', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'access' => user_access('administer filters')); + + $items[] = array('path' => 'admin/filters/add', 'title' => t('add input filter'), + 'callback' => 'filter_admin_add', + 'type' => MENU_LOCAL_TASK, + 'weight' => 1, + 'access' => user_access('administer filters')); + + $items[] = array('path' => 'admin/filters/delete', 'title' => t('delete input filter'), 'callback' => 'filter_admin_delete', 'type' => MENU_CALLBACK, 'access' => user_access('administer filters')); @@ -219,13 +230,15 @@ $formats = filter_formats(); if (isset($formats[arg(2)])) { - $items[] = array('path' => 'admin/filters/'. arg(2), 'title' => t("'%format' input format", array('%format' => $formats[arg(2)]->name)), - 'callback' => 'filter_admin_filters', + $items[] = array('path' => 'admin/filters/'. arg(2), 'title' => t("'%format' input filter", array('%format' => $formats[arg(2)]->name)), + 'callback' => 'filter_admin_format', + 'callback arguments' => array('format' => $formats[arg(2)]), 'type' => MENU_CALLBACK, 'access' => user_access('administer filters')); - $items[] = array('path' => 'admin/filters/'. arg(2) .'/list', 'title' => t('list'), - 'callback' => 'filter_admin_filters', + $items[] = array('path' => 'admin/filters/'. arg(2) .'/list', 'title' => t('view'), + 'callback' => 'filter_admin_format', + 'callback arguments' => array('format' => $formats[arg(2)]), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => 0, 'access' => user_access('administer filters')); @@ -256,43 +269,35 @@ } /** - * Menu callback; allows administrators to set up input formats. + * Menu callback; allows administrators to set up input filters. */ function filter_admin_overview() { - // Process form submission - switch ($_POST['op']) { - case t('Save input formats'): - filter_admin_save(); - break; - case t('Add input format'): - filter_admin_add(); - break; + // Process form submission to set the default format + if ($_POST['op'] == t('Save default format')) { + filter_admin_save_default(); } // Overview of all formats. $formats = filter_formats(); - $roles = user_roles(); $error = false; - $header = array(t('Name'), t('Default')); - foreach ($roles as $name) { - $header[] = $name; - } - $header[] = array('data' => t('Operations'), 'colspan' => 2); + $header = array(t('Default'), t('Name'), t('Roles'), array('data' => t('Operations'), 'colspan' => 2)); $rows = array(); foreach ($formats as $id => $format) { + $roles = array(); + foreach (user_roles() as $rid => $name) { + //prepare a roles array with roles that may access the filter + if (strstr($format->roles, ",$rid,")){ + $roles[] = $name; + } + } $row = array(); $default = ($id == variable_get('filter_default_format', 1)); - $row[] = form_textfield('', "name][$id", $format->name, 15, 255); $row[] = form_radio('', 'default', $id, $default); - - foreach ($roles as $rid => $name) { - $checked = strstr($format->roles, ",$rid,"); - $row[] = form_checkbox('', "roles][$id][$rid", 1, $default || $checked, NULL, $default ? array('disabled' => 'disabled') : NULL); - } - + $row[] = $format->name; + $row[] = $roles ? implode(', ',$roles) : t('No roles may use this format'); $row[] = l(t('configure'), 'admin/filters/'. $id); $row[] = $default ? '' : l('delete', 'admin/filters/delete/'. $id); @@ -300,73 +305,35 @@ } $group = theme('table', $header, $rows); - $group .= form_submit(t('Save input formats')); - $output = '

'. t('Permissions and settings') . '

' . form($group); - - // Form to add a new format. - $group = t("

To add a new input format, type its name here. After it has been added, you can configure its options.

"); - $form = form_textfield(t('Name'), 'name', '', 30, 255); - $form .= form_submit(t('Add input format')); - $group .= form($form); - $output .= '

'. t('Add new input format') .'

'. $group; + $group .= form_submit(t('Save default format')); + $output = form($group); return $output; } /** - * Save input formats on the overview page. + * Save input filters on the overview page. */ -function filter_admin_save() { +function filter_admin_save_default() { $edit = $_POST['edit']; - + drupal_set_message(t('Default format saved.')); variable_set('filter_default_format', $edit['default']); - - foreach ($edit['name'] as $id => $name) { - $name = trim($name); - - if (strlen($name) == 0) { - drupal_set_message(t('The input format name must be specified.')); - drupal_goto('admin/filters'); - } - else { - db_query("UPDATE {filter_formats} SET name='%s' WHERE format = %d", $name, $id); - } - } - - // We store the roles as a string for ease of use. - // We use leading and trailing comma's to allow easy substring matching. - foreach ($edit['roles'] as $id => $format) { - $roles = ','; - foreach ($format as $rid => $value) { - if ($value) { - $roles .= $rid .','; - } - } - db_query("UPDATE {filter_formats} SET roles = '%s' WHERE format = %d", $roles, $id); - } - - drupal_set_message(t('The input format settings have been updated.')); - drupal_goto('admin/filters'); } /** - * Add a new input format. + * Add a new input filter. */ function filter_admin_add() { - $edit = $_POST['edit']; - - $name = trim($edit['name']); + if ($_POST['op']) { + $edit = $_POST['edit']; + filter_admin_filters_save($format->format, $edit); - if (strlen($name) == 0) { - drupal_set_message(t('The input format must have a name.')); + drupal_set_message(t('Added input filter %format.', array('%format' => theme('placeholder', $edit['name'])))); drupal_goto('admin/filters'); } else { - db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $name); + return filter_admin_format_form($format); } - - drupal_set_message(t('Input format %format has been created.', array('%format' => theme('placeholder', $edit['name'])))); - drupal_goto('admin/filters'); } /** @@ -380,13 +347,13 @@ db_query("DELETE FROM {filters} WHERE format = %d", $edit['format']); $default = variable_get('filter_default_format', 1); - db_query("UPDATE {node} SET format = %d WHERE format = %d", $default, $edit['format']); + db_query("UPDATE {node_revisions} SET format = %d WHERE format = %d", $default, $edit['format']); db_query("UPDATE {comments} SET format = %d WHERE format = %d", $default, $edit['format']); db_query("UPDATE {boxes} SET format = %d WHERE format = %d", $default, $edit['format']); cache_clear_all('filter:'. $edit['format'], true); - drupal_set_message(t('Input format %format has been removed.', array('%format' => theme('placeholder', $edit['name'])))); + drupal_set_message(t('Deleted input filter %format.', array('%format' => theme('placeholder', $edit['name'])))); } drupal_goto('admin/filters'); } @@ -397,9 +364,9 @@ $extra = form_hidden('format', $format->format); $extra .= form_hidden('name', $format->name); $output = theme('confirm', - t('Are you sure you want to delete the input format %format?', array('%format' => theme('placeholder', $format->name))), + t('Are you sure you want to delete the input filter %format?', array('%format' => theme('placeholder', $format->name))), 'admin/filters', - t('If you have any content left in this input format, it will be switched to the default input format. This action cannot be undone.'), + t('If you have any content left in this input filter, it will be switched to the default input filter. This action cannot be undone.'), t('Delete'), t('Cancel'), $extra); @@ -409,43 +376,23 @@ /** * Menu callback; configure the filters for a format. */ -function filter_admin_filters() { - $format = arg(2); - +function filter_admin_format($format) { // Handle saving of weights. if ($_POST['op']) { - filter_admin_filters_save($format, $_POST['edit']); + $edit = $_POST['edit']; + filter_admin_filters_save($format->format, $edit); } - $all = filter_list_all(); - $enabled = filter_list_format($format); - - // Table with filters - $header = array(t('Enabled'), t('Name'), t('Description')); - $rows = array(); - foreach ($all as $id => $filter) { - $row = array(); - $row[] = form_checkbox('', $id, 1, isset($enabled[$id])); - $row[] = $filter->name; - $row[] = module_invoke($filter->module, 'filter', 'description', $filter->delta); - - $rows[] = $row; - } - $form = theme('table', $header, $rows); - if (!$empty) { - $form .= form_submit(t('Save configuration')); - } - - $output .= '

'. t('Filters') .'

'. form($form); + $output = filter_admin_format_form($format); // Composition tips (guidelines) - $tips = _filter_tips($format, false); + $tips = _filter_tips($format->format, false); $extra = l(t('More information about formatting options'), 'filter/tips'); $tiplist = theme('filter_tips', $tips, false, $extra); if (!$tiplist) { $tiplist = t('

No guidelines available.

'); } - $group = t('

These are the guidelines that users will see for posting in this input format. They are automatically generated from the filter settings.

'); + $group = t('

These are the guidelines that users will see for posting in this input filter. They are automatically generated from the filter settings.

'); $group .= $tiplist; $output .= '

'. t('Formatting guidelines') .'

'. $group; @@ -453,13 +400,54 @@ } /** + * Renders a form for a format + */ +function filter_admin_format_form($format) { + $default = ($format->format == variable_get('filter_default_format', 1)); + + //Add the name of the object + $form = form_group(t('Name'), form_textfield(t('Name'), 'name', $format->name, 60, 127, t('Give the name of this filter format'), NULL, TRUE)); + + //Add a row of checkboxes for.form group + foreach (user_roles() as $rid => $name) { + $checked = strstr($format->roles, ",$rid,"); + $group .= form_checkbox($name, 'roles]['.$rid, 1, ($default || $checked), NULL, $default ? array('disabled' => 'disabled') : NULL); + } + if ($default) { + $help = t('All roles for the default format must be enabled and cannot be changed.'); + $group .= form_hidden('default_format', 1); + } + $form .= form_group(t('Roles'), $group, $default ? $help : t('Choose which roles may use this filter format.')); + + // Table with filters + $all = filter_list_all(); + $enabled = filter_list_format($format->format); + + $group = ''; + foreach ($all as $id => $filter) { + $group .= form_checkbox($filter->name, $id, 1, isset($enabled[$id]), module_invoke($filter->module, 'filter', 'description', $filter->delta)); + } + + $form .= form_group(t('Filters'), $group, t('Choose the filters that will be used in this filter format')); + $form .= form_submit(t('Save configuration')); + + return form($form); +} + +/** * Save enabled/disabled status for filters in a format. */ function filter_admin_filters_save($format, $toggles) { $current = filter_list_format($format); - + $edit = $_POST['edit']; $cache = true; + if (!$format) { //add a new filter format. + db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $name); + $result = db_fetch_object(db_query("SELECT MAX(format) AS format FROM {filter_formats}")); + $format = $result->format; + } + db_query("DELETE FROM {filters} WHERE format = %d", $format); foreach ($toggles as $id => $checked) { if ($checked) { @@ -473,13 +461,26 @@ } } - // Update the format's 'no cache' flag. - db_query('UPDATE {filter_formats} SET cache = %d WHERE format = %d', (int)$cache, $format); - - cache_clear_all('filter:'. $format, true); - - drupal_set_message(t('The input format has been updated.')); - drupal_goto('admin/filters/'. arg(2) .'/list'); + $name = trim($edit['name']); + if (strlen($name) == 0) { + form_set_error('name', t('You must enter a name for this input filter.')); + drupal_goto('admin/filters/add'); + } + else { + // We store the roles as a string for ease of use. + // we should always set all roles to true when saving a default role. disabled checkboxes may not always return TRUE. + // We use leading and trailing comma's to allow easy substring matching. + $roles = ','; + foreach ($edit['roles'] as $rid => $value) { + if ($value || $edit['default_format']) { + $roles .= $rid .','; + } + } + db_query("UPDATE {filter_formats} SET cache = %d, name='%s', roles = '%s' WHERE format = %d", (int)$cache, $name, $roles, $format); + drupal_set_message(t('The input filter settings have been updated.')); + cache_clear_all('filter:'. $format, true); + drupal_goto($_GET['q']); + } } /** @@ -519,8 +520,7 @@ drupal_set_message(t('The filter ordering has been saved.')); cache_clear_all('filter:'. $format, true); - - drupal_goto('admin/filters/'. arg(2) .'/order'); + drupal_goto($_GET['q']); } /** @@ -548,13 +548,13 @@ } /** - * Retrieve a list of input formats. + * Retrieve a list of input filters. */ function filter_formats() { global $user; static $formats; - // Administrators can always use all input formats. + // Administrators can always use all input filters. $all = user_access('administer filters'); if (!isset($formats)) { @@ -609,7 +609,7 @@ } /** - * Check if text in a certain input format is allowed to be cached. + * Check if text in a certain input filter is allowed to be cached. */ function filter_format_allowcache($format) { static $cache = array(); @@ -746,8 +746,7 @@ $output .= theme('filter_tips', $tips); $output .= ''; } - $group = theme('form_element', NULL, $output, $extra, NULL, _form_get_error($name)); - return form_group_collapsible(t('Input format'), $group, TRUE); + return theme('form_element', t('input filter'), $output, $extra, NULL, _form_get_error($name)); } else { // Only one format available: use a hidden form item and only show tips. @@ -826,7 +825,7 @@ $multiple = count($tips) > 1; if ($multiple) { - $output = t('Input formats') .':'; + $output = t('input filters') .':'; } if (count($tips)) { @@ -925,11 +924,11 @@ */ function _filter_html_settings($format) { $group = form_radios(t('Filter HTML tags'), "filter_html_$format", variable_get("filter_html_$format", FILTER_HTML_STRIP), array(FILTER_HTML_STRIP => t('Strip tags'), FILTER_HTML_ESCAPE => t('Escape tags')), t('How to deal with HTML tags in user-contributed content. If set to "Strip tags", dangerous tags are removed (see below). If set to "Escape tags", all HTML is escaped and presented as it was typed.')); - $group .= form_textfield(t('Allowed HTML tags'), "allowed_html_$format", variable_get("allowed_html_$format", '