Code coverage for /20080809/modules/filter/filter.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: filter.admin.inc,v 1.13 2008/07/24 16:27:51 dries Exp $
3
4
/**
5
 * @file
6
 * Admin page callbacks for the filter module.
7
 */
8
9
/**
10
 * Menu callback; Displays a list of all input formats and which
11
 * one is the default.
12
 *
13
 * @ingroup forms
14
 * @see filter_admin_overview_submit()
15
 */
16129
function filter_admin_overview() {
17
18
  // Overview of all formats.
1911
  $formats = filter_formats();
2011
  $error = FALSE;
21
2211
  $form = array('#tree' => TRUE);
2311
  foreach ($formats as $id => $format) {
2411
    $roles = array();
2511
    foreach (user_roles() as $rid => $name) {
26
      // Prepare a roles array with roles that may access the filter.
2711
      if (strstr($format->roles, ",$rid,")) {
2811
        $roles[] = $name;
2911
      }
3011
    }
3111
    $default = ($id == variable_get('filter_default_format', 1));
3211
    $options[$id] = '';
3311
    $form[$id]['name'] = array('#markup' => $format->name);
3411
    $form[$id]['roles'] = array('#markup' => $default ? t('All roles may
use default format') : ($roles ? implode(', ', $roles) : t('No roles may
use this format')));
3511
    $form[$id]['configure'] = array('#markup' => l(t('configure'),
'admin/settings/filters/' . $id));
3611
    $form[$id]['delete'] = array('#markup' => $default ? '' :
l(t('delete'), 'admin/settings/filters/delete/' . $id));
3711
    $form[$id]['weight'] = array('#type' => 'weight', '#default_value' =>
$format->weight);
3811
  }
3911
  $form['default'] = array('#type' => 'radios', '#options' => $options,
'#default_value' => variable_get('filter_default_format', 1));
4011
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save
changes'));
4111
  return $form;
420
}
43
44129
function filter_admin_overview_submit($form, &$form_state) {
45
  // Process form submission to set the default format.
462
  if (is_numeric($form_state['values']['default'])) {
472
    drupal_set_message(t('Default format updated.'));
482
    variable_set('filter_default_format',
$form_state['values']['default']);
492
  }
502
  foreach ($form_state['values'] as $id => $data) {
512
    if (is_array($data) && isset($data['weight'])) {
52
      // Only update if this is a form element with weight.
532
      db_query("UPDATE {filter_formats} SET weight = %d WHERE format = %d",
$data['weight'], $id);
542
    }
552
  }
562
  drupal_set_message(t('The input format ordering has been saved.'));
572
}
58
59
/**
60
 * Theme the admin overview form.
61
 *
62
 * @ingroup themeable
63
 */
64129
function theme_filter_admin_overview($form) {
659
  $rows = array();
669
  foreach ($form as $id => $element) {
679
    if (isset($element['roles']) && is_array($element['roles'])) {
689
      $element['weight']['#attributes']['class'] =
'input-format-order-weight';
699
      $rows[] = array(
70
        'data' => array(
719
          check_plain($element['name']['#markup']),
729
          drupal_render($element['roles']),
739
          drupal_render($form['default'][$id]),
749
          drupal_render($element['weight']),
759
          drupal_render($element['configure']),
769
          drupal_render($element['delete']),
779
        ),
789
        'class' => 'draggable',
79
      );
809
      unset($form[$id]);
819
    }
829
  }
839
  $header = array(t('Name'), t('Roles'), t('Default'), t('Weight'),
array('data' => t('Operations'), 'colspan' => 2));
849
  $output = theme('table', $header, $rows, array('id' =>
'input-format-order'));
859
  $output .= drupal_render($form);
86
879
  drupal_add_tabledrag('input-format-order', 'order', 'sibling',
'input-format-order-weight');
88
899
  return $output;
900
}
91
92
/**
93
 * Menu callback; Display a filter format form.
94
 */
95129
function filter_admin_format_page($format = NULL) {
9613
  if (!isset($format->name)) {
974
    drupal_set_title(t("Add input format"));
984
    $format = (object)array('name' => '', 'roles' => '', 'format' => '');
994
  }
10013
  return drupal_get_form('filter_admin_format_form', $format);
1010
}
102
103
/**
104
 * Generate a filter format form.
105
 *
106
 * @ingroup forms
107
 * @see filter_admin_format_form_validate()
108
 * @see filter_admin_format_form_submit()
109
 */
110129
function filter_admin_format_form(&$form_state, $format) {
11113
  $default = ($format->format == variable_get('filter_default_format',
1));
11213
  if ($default) {
1130
    $help = t('All roles for the default format must be enabled and cannot
be changed.');
1140
    $form['default_format'] = array('#type' => 'hidden', '#value' => 1);
1150
  }
116
11713
  $form['name'] = array('#type' => 'textfield',
11813
    '#title' => t('Name'),
11913
    '#default_value' => $format->name,
12013
    '#description' => t('Specify a unique name for this filter format.'),
12113
    '#required' => TRUE,
122
  );
123
124
  // Add a row of checkboxes for form group.
12513
  $form['roles'] = array('#type' => 'fieldset',
12613
    '#title' => t('Roles'),
12713
    '#description' => $default ? $help : t('Choose which roles may use this
filter format. Note that roles with the "administer filters" permission can
always use all the filter formats.'),
12813
    '#tree' => TRUE,
129
  );
130
13113
  foreach (user_roles() as $rid => $name) {
13213
    $checked = strstr($format->roles, ",$rid,");
13313
    $form['roles'][$rid] = array('#type' => 'checkbox',
13413
      '#title' => $name,
13513
      '#default_value' => ($default || $checked),
136
    );
13713
    if ($default) {
1380
      $form['roles'][$rid]['#disabled'] = TRUE;
1390
    }
14013
  }
141
  // Table with filters
14213
  $all = filter_list_all();
14313
  $enabled = filter_list_format($format->format);
144
14513
  $form['filters'] = array('#type' => 'fieldset',
14613
    '#title' => t('Filters'),
14713
    '#description' => t('Choose the filters that will be used in this
filter format.'),
14813
    '#tree' => TRUE,
149
  );
15013
  foreach ($all as $id => $filter) {
15113
    $form['filters'][$id] = array('#type' => 'checkbox',
15213
      '#title' => $filter->name,
15313
      '#default_value' => isset($enabled[$id]),
15413
      '#description' => module_invoke($filter->module, 'filter',
'description', $filter->delta),
155
    );
15613
  }
15713
  if (!empty($format->format)) {
1589
    $form['format'] = array('#type' => 'hidden', '#value' =>
$format->format);
159
160
    // Composition tips (guidelines)
1619
    $tips = _filter_tips($format->format, FALSE);
1629
    $extra = '<p>' . l(t('More information about formatting options'),
'filter/tips') . '</p>';
1639
    $tiplist = theme('filter_tips', $tips, FALSE, $extra);
1649
    if (!$tiplist) {
1650
      $tiplist = '<p>' . t('No guidelines available.') . '</p>';
1660
    }
1679
    $group = '<p>' . t('These are the guidelines that users will see for
posting in this input format. They are automatically generated from the
filter settings.') . '</p>';
1689
    $group .= $tiplist;
1699
    $form['tips'] = array('#markup' => '<h2>' . t('Formatting guidelines')
. '</h2>' . $group);
1709
  }
17113
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save
configuration'));
172
17313
  return $form;
1740
}
175
176
/**
177
 * Validate filter format form submissions.
178
 */
179129
function filter_admin_format_form_validate($form, &$form_state) {
1805
  if (!isset($form_state['values']['format'])) {
1812
    $name = trim($form_state['values']['name']);
1822
    $result = db_fetch_object(db_query("SELECT format FROM {filter_formats}
WHERE name='%s'", $name));
1832
    if ($result) {
1840
      form_set_error('name', t('Filter format names need to be unique. A
format named %name already exists.', array('%name' => $name)));
1850
    }
1862
  }
1875
}
188
189
/**
190
 * Process filter format form submissions.
191
 */
192129
function filter_admin_format_form_submit($form, &$form_state) {
1935
  $format = isset($form_state['values']['format']) ?
$form_state['values']['format'] : NULL;
1945
  $current = filter_list_format($format);
1955
  $name = trim($form_state['values']['name']);
1965
  $cache = TRUE;
197
198
  // Add a new filter format.
1995
  if (!$format) {
2002
    $new = TRUE;
2012
    db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $name);
2022
    $format = db_result(db_query("SELECT MAX(format) AS format FROM
{filter_formats}"));
2032
    drupal_set_message(t('Added input format %format.', array('%format' =>
$name)));
2042
  }
205
  else {
2063
    drupal_set_message(t('The input format settings have been updated.'));
207
  }
208
2095
  db_query("DELETE FROM {filters} WHERE format = %d", $format);
2105
  foreach ($form_state['values']['filters'] as $id => $checked) {
2115
    if ($checked) {
2125
      list($module, $delta) = explode('/', $id);
213
      // Add new filters to the bottom.
2145
      $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10;
2155
      db_query("INSERT INTO {filters} (format, module, delta, weight)
VALUES (%d, '%s', %d, %d)", $format, $module, $delta, $weight);
216
217
      // Check if there are any 'no cache' filters.
2185
      $cache &= !module_invoke($module, 'filter', 'no cache', $delta);
2195
    }
2205
  }
221
222
  // We store the roles as a string for ease of use.
223
  // We should always set all roles to TRUE when saving a default role.
224
  // We use leading and trailing comma's to allow easy substring matching.
2255
  $roles = array();
2265
  if (isset($form_state['values']['roles'])) {
2275
    foreach ($form_state['values']['roles'] as $id => $checked) {
2285
      if ($checked) {
2294
        $roles[] = $id;
2304
      }
2315
    }
2325
  }
2335
  if (!empty($form_state['values']['default_format'])) {
2340
    $roles = ',' . implode(',', array_keys(user_roles())) . ',';
2350
  }
236
  else {
2375
    $roles = ',' . implode(',', $roles) . ',';
238
  }
239
2405
  db_query("UPDATE {filter_formats} SET cache = %d, name='%s', roles = '%s'
WHERE format = %d", $cache, $name, $roles, $format);
241
2425
  cache_clear_all($format . ':', 'cache_filter', TRUE);
243
244
  // If a new filter was added, return to the main list of filters.
Otherwise, stay on edit filter page to show new changes.
2455
  $return = 'admin/settings/filters';
2465
  if (!empty($new)) {
2472
    $return .= '/' . $format;
2482
  }
2495
  $form_state['redirect'] = $return;
2505
  return;
2510
}
252
253
/**
254
 * Menu callback; confirm deletion of a format.
255
 *
256
 * @ingroup forms
257
 * @see filter_admin_delete_submit()
258
 */
259129
function filter_admin_delete() {
2604
  $format = arg(4);
2614
  $format = db_fetch_object(db_query('SELECT * FROM {filter_formats} WHERE
format = %d', $format));
262
2634
  if ($format) {
2644
    if ($format->format != variable_get('filter_default_format', 1)) {
2654
      $form['format'] = array('#type' => 'hidden', '#value' =>
$format->format);
2664
      $form['name'] = array('#type' => 'hidden', '#value' =>
$format->name);
267
2684
      return confirm_form($form, t('Are you sure you want to delete the
input format %format?', array('%format' => $format->name)),
'admin/settings/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('Delete'), t('Cancel'));
2690
    }
270
    else {
2710
      drupal_set_message(t('The default format cannot be deleted.'));
2720
      drupal_goto('admin/settings/filters');
273
    }
2740
  }
275
  else {
2760
    drupal_not_found();
277
  }
2780
}
279
280
/**
281
 * Process filter delete form submission.
282
 */
283129
function filter_admin_delete_submit($form, &$form_state) {
2842
  db_query("DELETE FROM {filter_formats} WHERE format = %d",
$form_state['values']['format']);
2852
  db_query("DELETE FROM {filters} WHERE format = %d",
$form_state['values']['format']);
286
2872
  $default = variable_get('filter_default_format', 1);
288
  // Replace existing instances of the deleted format with the default
format.
2892
  db_query("UPDATE {node_revisions} SET format = %d WHERE format = %d",
$default, $form_state['values']['format']);
2902
  db_query("UPDATE {comments} SET format = %d WHERE format = %d", $default,
$form_state['values']['format']);
2912
  db_query("UPDATE {boxes} SET format = %d WHERE format = %d", $default,
$form_state['values']['format']);
292
2932
  cache_clear_all($form_state['values']['format'] . ':', 'cache_filter',
TRUE);
2942
  drupal_set_message(t('Deleted input format %format.', array('%format' =>
$form_state['values']['name'])));
295
2962
  $form_state['redirect'] = 'admin/settings/filters';
2972
  return;
2980
}
299
300
301
/**
302
 * Menu callback; display settings defined by a format's filters.
303
 */
304129
function filter_admin_configure_page($format) {
3056
  drupal_set_title(t("Configure %format", array('%format' =>
$format->name)));
3066
  return drupal_get_form('filter_admin_configure', $format);
3070
}
308
309
/**
310
 * Build a form to change the settings for a format's filters.
311
 *
312
 * @ingroup forms
313
 */
314129
function filter_admin_configure(&$form_state, $format) {
3156
  $list = filter_list_format($format->format);
3166
  $form = array();
3176
  foreach ($list as $filter) {
3186
    $form_module = module_invoke($filter->module, 'filter', 'settings',
$filter->delta, $format->format);
3196
    if (isset($form_module) && is_array($form_module)) {
3206
      $form = array_merge($form, $form_module);
3216
    }
3226
  }
323
3246
  if (!empty($form)) {
3256
    $form = system_settings_form($form);
3266
  }
327
  else {
3280
    $form['error'] = array('#markup' => t('No settings are available.'));
329
  }
3306
  $form['format'] = array('#type' => 'hidden', '#value' =>
$format->format);
3316
  $form['#submit'][] = 'filter_admin_configure_submit';
3326
  return $form;
3330
}
334
335
/**
336
 * Clear the filter's cache when configuration settings are saved.
337
 */
338129
function filter_admin_configure_submit($form, &$form_state) {
3392
  cache_clear_all($form_state['values']['format'] . ':', 'cache_filter',
TRUE);
3402
}
341
342
/**
343
 * Menu callback; display form for ordering filters for a format.
344
 */
345129
function filter_admin_order_page($format) {
3466
  drupal_set_title(t("Rearrange %format", array('%format' =>
$format->name)));
3476
  return drupal_get_form('filter_admin_order', $format);
3480
}
349
350
/**
351
 * Build the form for ordering filters for a format.
352
 *
353
 * @ingroup forms
354
 * @see theme_filter_admin_order()
355
 * @see filter_admin_order_submit()
356
 */
357129
function filter_admin_order(&$form_state, $format = NULL) {
358
  // Get list (with forced refresh).
3596
  $filters = filter_list_format($format->format);
360
3616
  $form['weights'] = array('#tree' => TRUE);
3626
  foreach ($filters as $id => $filter) {
3636
    $form['names'][$id] = array('#markup' => $filter->name);
3646
    $form['weights'][$id] = array('#type' => 'weight', '#default_value' =>
$filter->weight);
3656
  }
3666
  $form['format'] = array('#type' => 'hidden', '#value' =>
$format->format);
3676
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save
configuration'));
368
3696
  return $form;
3700
}
371
372
/**
373
 * Theme filter order configuration form.
374
 *
375
 * @ingroup themeable
376
 */
377129
function theme_filter_admin_order($form) {
3784
  $header = array(t('Name'), t('Weight'));
3794
  $rows = array();
3804
  foreach (element_children($form['names']) as $id) {
381
    // Don't take form control structures.
3824
    if (is_array($form['names'][$id])) {
3834
      $form['weights'][$id]['#attributes']['class'] =
'filter-order-weight';
3844
      $rows[] = array(
3854
        'data' => array(drupal_render($form['names'][$id]),
drupal_render($form['weights'][$id])),
3864
        'class' => 'draggable',
387
      );
3884
    }
3894
  }
390
3914
  $output = theme('table', $header, $rows, array('id' => 'filter-order'));
3924
  $output .= drupal_render($form);
393
3944
  drupal_add_tabledrag('filter-order', 'order', 'sibling',
'filter-order-weight', NULL, NULL, FALSE);
395
3964
  return $output;
3970
}
398
399
/**
400
 * Process filter order configuration form submission.
401
 */
402129
function filter_admin_order_submit($form, &$form_state) {
4032
  foreach ($form_state['values']['weights'] as $id => $weight) {
4042
    list($module, $delta) = explode('/', $id);
4052
    db_query("UPDATE {filters} SET weight = %d WHERE format = %d AND module
= '%s' AND delta = %d", $weight, $form_state['values']['format'], $module,
$delta);
4062
  }
4072
  drupal_set_message(t('The filter ordering has been saved.'));
408
4092
  cache_clear_all($form_state['values']['format'] . ':', 'cache_filter',
TRUE);
4102
}
411129