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

Line #Times calledCode
1
<?php
2
// $Id: filter.admin.inc,v 1.16 2008/10/13 00:33:02 webchick 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
 */
16210
function filter_admin_overview() {
17
18
  // Overview of all formats.
1910
  $formats = filter_formats();
2010
  $error = FALSE;
21
2210
  $form = array('#tree' => TRUE);
2310
  foreach ($formats as $id => $format) {
2410
    $roles = array();
2510
    foreach (user_roles() as $rid => $name) {
26
      // Prepare a roles array with roles that may access the filter.
2710
      if (strstr($format->roles, ",$rid,")) {
2810
        $roles[] = $name;
2910
      }
3010
    }
3110
    $default = ($id == variable_get('filter_default_format', 1));
3210
    $options[$id] = '';
3310
    $form[$id]['name'] = array('#markup' => $format->name);
3410
    $form[$id]['roles'] = array('#markup' => $default ? t('All roles may
use default format') : ($roles ? implode(', ', $roles) : t('No roles may
use this format')));
3510
    $form[$id]['configure'] = array('#markup' => l(t('configure'),
'admin/settings/filters/' . $id));
3610
    $form[$id]['delete'] = array('#markup' => $default ? '' :
l(t('delete'), 'admin/settings/filters/delete/' . $id));
3710
    $form[$id]['weight'] = array('#type' => 'weight', '#default_value' =>
$format->weight);
3810
  }
3910
  $form['default'] = array('#type' => 'radios', '#options' => $options,
'#default_value' => variable_get('filter_default_format', 1));
4010
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save
changes'));
4110
  return $form;
420
}
43
44210
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
 */
64210
function theme_filter_admin_overview($form) {
658
  $rows = array();
668
  foreach ($form as $id => $element) {
678
    if (isset($element['roles']) && is_array($element['roles'])) {
688
      $element['weight']['#attributes']['class'] =
'input-format-order-weight';
698
      $rows[] = array(
70
        'data' => array(
718
          check_plain($element['name']['#markup']),
728
          drupal_render($element['roles']),
738
          drupal_render($form['default'][$id]),
748
          drupal_render($element['weight']),
758
          drupal_render($element['configure']),
768
          drupal_render($element['delete']),
778
        ),
788
        'class' => 'draggable',
79
      );
808
      unset($form[$id]);
818
    }
828
  }
838
  $header = array(t('Name'), t('Roles'), t('Default'), t('Weight'),
array('data' => t('Operations'), 'colspan' => 2));
848
  $output = theme('table', $header, $rows, array('id' =>
'input-format-order'));
858
  $output .= drupal_render($form);
86
878
  drupal_add_tabledrag('input-format-order', 'order', 'sibling',
'input-format-order-weight');
88
898
  return $output;
900
}
91
92
/**
93
 * Menu callback; Display a filter format form.
94
 */
95210
function filter_admin_format_page($format = NULL) {
9610
  if (!isset($format->name)) {
972
    drupal_set_title(t('Add input format'), PASS_THROUGH);
982
    $format = (object)array('name' => '', 'roles' => '', 'format' => '');
992
  }
10010
  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
 */
110210
function filter_admin_format_form(&$form_state, $format) {
11110
  $default = ($format->format == variable_get('filter_default_format', 1));
11210
  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
11710
  $form['name'] = array('#type' => 'textfield',
11810
    '#title' => t('Name'),
11910
    '#default_value' => $format->name,
12010
    '#description' => t('Specify a unique name for this filter format.'),
12110
    '#required' => TRUE,
122
  );
123
124
  // Add a row of checkboxes for form group.
12510
  $form['roles'] = array('#type' => 'fieldset',
12610
    '#title' => t('Roles'),
12710
    '#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.'),
12810
    '#tree' => TRUE,
129
  );
130
13110
  foreach (user_roles() as $rid => $name) {
13210
    $checked = strstr($format->roles, ",$rid,");
13310
    $form['roles'][$rid] = array('#type' => 'checkbox',
13410
      '#title' => $name,
13510
      '#default_value' => ($default || $checked),
136
    );
13710
    if ($default) {
1380
      $form['roles'][$rid]['#disabled'] = TRUE;
1390
    }
14010
  }
141
  // Table with filters
14210
  $all = filter_list_all();
14310
  $enabled = filter_list_format($format->format);
144
14510
  $form['filters'] = array('#type' => 'fieldset',
14610
    '#title' => t('Filters'),
14710
    '#description' => t('Choose the filters that will be used in this
filter format.'),
14810
    '#tree' => TRUE,
149
  );
15010
  foreach ($all as $id => $filter) {
15110
    $form['filters'][$id] = array('#type' => 'checkbox',
15210
      '#title' => $filter->name,
15310
      '#default_value' => isset($enabled[$id]),
15410
      '#description' => module_invoke($filter->module, 'filter',
'description', $filter->delta),
155
    );
15610
  }
15710
  if (!empty($format->format)) {
1588
    $form['format'] = array('#type' => 'hidden', '#value' =>
$format->format);
159
160
    // Composition tips (guidelines)
1618
    $tips = _filter_tips($format->format, FALSE);
1628
    $extra = '<p>' . l(t('More information about formatting options'),
'filter/tips') . '</p>';
1638
    $tiplist = theme('filter_tips', $tips, FALSE, $extra);
1648
    if (!$tiplist) {
1650
      $tiplist = '<p>' . t('No guidelines available.') . '</p>';
1660
    }
1678
    $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>';
1688
    $group .= $tiplist;
1698
    $form['tips'] = array('#markup' => '<h2>' . t('Formatting guidelines')
. '</h2>' . $group);
1708
  }
17110
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save
configuration'));
172
17310
  return $form;
1740
}
175
176
/**
177
 * Validate filter format form submissions.
178
 */
179210
function filter_admin_format_form_validate($form, &$form_state) {
1804
  if (!isset($form_state['values']['format'])) {
1811
    $name = trim($form_state['values']['name']);
1821
    $result = db_fetch_object(db_query("SELECT format FROM {filter_formats}
WHERE name='%s'", $name));
1831
    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
    }
1861
  }
1874
}
188
189
/**
190
 * Process filter format form submissions.
191
 */
192210
function filter_admin_format_form_submit($form, &$form_state) {
1934
  $format = isset($form_state['values']['format']) ?
$form_state['values']['format'] : NULL;
1944
  $current = filter_list_format($format);
1954
  $name = trim($form_state['values']['name']);
1964
  $cache = TRUE;
197
198
  // Add a new filter format.
1994
  if (!$format) {
2001
    $new = TRUE;
2011
    db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $name);
2021
    $format = db_result(db_query("SELECT MAX(format) AS format FROM
{filter_formats}"));
2031
    drupal_set_message(t('Added input format %format.', array('%format' =>
$name)));
2041
  }
205
  else {
2063
    drupal_set_message(t('The input format settings have been updated.'));
207
  }
208
2094
  db_query("DELETE FROM {filters} WHERE format = %d", $format);
2104
  foreach ($form_state['values']['filters'] as $id => $checked) {
2114
    if ($checked) {
2124
      list($module, $delta) = explode('/', $id);
213
      // Add new filters to the bottom.
2144
      $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10;
2154
      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.
2184
      $cache &= !module_invoke($module, 'filter', 'no cache', $delta);
2194
    }
2204
  }
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.
2254
  $roles = array();
2264
  if (isset($form_state['values']['roles'])) {
2274
    foreach ($form_state['values']['roles'] as $id => $checked) {
2284
      if ($checked) {
2293
        $roles[] = $id;
2303
      }
2314
    }
2324
  }
2334
  if (!empty($form_state['values']['default_format'])) {
2340
    $roles = ',' . implode(',', array_keys(user_roles())) . ',';
2350
  }
236
  else {
2374
    $roles = ',' . implode(',', $roles) . ',';
238
  }
239
2404
  db_query("UPDATE {filter_formats} SET cache = %d, name='%s', roles = '%s'
WHERE format = %d", $cache, $name, $roles, $format);
241
2424
  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.
2454
  $return = 'admin/settings/filters';
2464
  if (!empty($new)) {
2471
    $return .= '/' . $format;
2481
  }
2494
  $form_state['redirect'] = $return;
2504
  return;
2510
}
252
253
/**
254
 * Menu callback; confirm deletion of a format.
255
 *
256
 * @ingroup forms
257
 * @see filter_admin_delete_submit()
258
 */
259210
function filter_admin_delete() {
2602
  $format = arg(4);
2612
  $format = db_fetch_object(db_query('SELECT * FROM {filter_formats} WHERE
format = %d', $format));
262
2632
  if ($format) {
2642
    if ($format->format != variable_get('filter_default_format', 1)) {
2652
      $form['format'] = array('#type' => 'hidden', '#value' =>
$format->format);
2662
      $form['name'] = array('#type' => 'hidden', '#value' =>
$format->name);
267
2682
      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
 */
283210
function filter_admin_delete_submit($form, &$form_state) {
2841
  db_query("DELETE FROM {filter_formats} WHERE format = %d",
$form_state['values']['format']);
2851
  db_query("DELETE FROM {filters} WHERE format = %d",
$form_state['values']['format']);
286
2871
  $default = variable_get('filter_default_format', 1);
288
  // Replace existing instances of the deleted format with the default
format.
2891
  db_query("UPDATE {node_revisions} SET format = %d WHERE format = %d",
$default, $form_state['values']['format']);
2901
  db_query("UPDATE {comments} SET format = %d WHERE format = %d", $default,
$form_state['values']['format']);
2911
  db_query("UPDATE {boxes} SET format = %d WHERE format = %d", $default,
$form_state['values']['format']);
292
2931
  cache_clear_all($form_state['values']['format'] . ':', 'cache_filter',
TRUE);
2941
  drupal_set_message(t('Deleted input format %format.', array('%format' =>
$form_state['values']['name'])));
295
2961
  $form_state['redirect'] = 'admin/settings/filters';
2971
  return;
2980
}
299
300
301
/**
302
 * Menu callback; display settings defined by a format's filters.
303
 */
304210
function filter_admin_configure_page($format) {
3056
  drupal_set_title(t("Configure %format", array('%format' =>
$format->name)), PASS_THROUGH);
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
 */
314210
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
 */
338210
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
 */
345210
function filter_admin_order_page($format) {
3466
  drupal_set_title(t("Rearrange %format", array('%format' =>
$format->name)), PASS_THROUGH);
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
 */
357210
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
 */
377210
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
 */
402210
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
}
411210