filter.admin.inc 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
// $Id$

/**
 * @file
 * Admin page callbacks for the filter module.
 */

/**
10
 * Menu callback; Displays a list of all text formats and which
11 12 13
 * one is the default.
 *
 * @ingroup forms
14
 * @see filter_admin_overview_submit()
15 16 17 18 19 20 21
 */
function filter_admin_overview() {

  // Overview of all formats.
  $formats = filter_formats();
  $error = FALSE;

22
  $form = array('#tree' => TRUE);
23 24 25 26 27 28 29 30 31 32
  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;
      }
    }
    $default = ($id == variable_get('filter_default_format', 1));
    $options[$id] = '';
33
    $form[$id]['name'] = array('#markup' => $format->name);
34 35 36
    $form[$id]['roles'] = array('#markup' => $default ? t('All roles may use the default format') : ($roles ? implode(', ', $roles) : t('No roles may use this format')));
    $form[$id]['configure'] = array('#markup' => l(t('configure'), 'admin/settings/filter/' . $id));
    $form[$id]['delete'] = array('#markup' => $default ? '' : l(t('delete'), 'admin/settings/filter/delete/' . $id));
37
    $form[$id]['weight'] = array('#type' => 'weight', '#default_value' => $format->weight);
38 39
  }
  $form['default'] = array('#type' => 'radios', '#options' => $options, '#default_value' => variable_get('filter_default_format', 1));
40
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
41 42 43 44 45 46 47 48 49
  return $form;
}

function filter_admin_overview_submit($form, &$form_state) {
  // Process form submission to set the default format.
  if (is_numeric($form_state['values']['default'])) {
    drupal_set_message(t('Default format updated.'));
    variable_set('filter_default_format', $form_state['values']['default']);
  }
50 51 52
  foreach ($form_state['values'] as $id => $data) {
    if (is_array($data) && isset($data['weight'])) {
      // Only update if this is a form element with weight.
53
      db_query("UPDATE {filter_format} SET weight = %d WHERE format = %d", $data['weight'], $id);
54 55
    }
  }
56
  drupal_set_message(t('The text format ordering has been saved.'));
57 58 59 60 61 62 63 64 65
}

/**
 * Theme the admin overview form.
 *
 * @ingroup themeable
 */
function theme_filter_admin_overview($form) {
  $rows = array();
66
  foreach ($form as $id => $element) {
67
    if (isset($element['roles']) && is_array($element['roles'])) {
68
      $element['weight']['#attributes']['class'] = 'input-format-order-weight';
69
      $rows[] = array(
70
        'data' => array(
71
          check_plain($element['name']['#markup']),
72 73 74
          drupal_render($element['roles']),
          drupal_render($form['default'][$id]),
          drupal_render($element['weight']),
75
          drupal_render($element['edit']),
76 77 78
          drupal_render($element['delete']),
        ),
        'class' => 'draggable',
79
      );
80
      unset($form[$id]);
81 82
    }
  }
83 84
  $header = array(t('Name'), t('Roles'), t('Default'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
  $output = theme('table', $header, $rows, array('id' => 'input-format-order'));
85 86
  $output .= drupal_render($form);

87 88
  drupal_add_tabledrag('input-format-order', 'order', 'sibling', 'input-format-order-weight');

89 90 91
  return $output;
}

92
/**
93
 * Menu callback; Display a filter format form.
94 95 96
 */
function filter_admin_format_page($format = NULL) {
  if (!isset($format->name)) {
97
    drupal_set_title(t('Add text format'), PASS_THROUGH);
98 99 100 101 102
    $format = (object)array('name' => '', 'roles' => '', 'format' => '');
  }
  return drupal_get_form('filter_admin_format_form', $format);
}

103 104 105 106
/**
 * Generate a filter format form.
 *
 * @ingroup forms
107 108
 * @see filter_admin_format_form_validate()
 * @see filter_admin_format_form_submit()
109
 */
110
function filter_admin_format_form(&$form_state, $format) {
111 112 113 114 115 116 117
  $default = ($format->format == variable_get('filter_default_format', 1));
  if ($default) {
    $help = t('All roles for the default format must be enabled and cannot be changed.');
    $form['default_format'] = array('#type' => 'hidden', '#value' => 1);
  }

  $form['name'] = array('#type' => 'textfield',
118
    '#title' => t('Name'),
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    '#default_value' => $format->name,
    '#description' => t('Specify a unique name for this filter format.'),
    '#required' => TRUE,
  );

  // Add a row of checkboxes for form group.
  $form['roles'] = array('#type' => 'fieldset',
    '#title' => t('Roles'),
    '#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.'),
    '#tree' => TRUE,
  );

  foreach (user_roles() as $rid => $name) {
    $checked = strstr($format->roles, ",$rid,");
    $form['roles'][$rid] = array('#type' => 'checkbox',
      '#title' => $name,
      '#default_value' => ($default || $checked),
    );
    if ($default) {
      $form['roles'][$rid]['#disabled'] = TRUE;
    }
  }
  // Table with filters
  $all = filter_list_all();
  $enabled = filter_list_format($format->format);

  $form['filters'] = array('#type' => 'fieldset',
    '#title' => t('Filters'),
    '#description' => t('Choose the filters that will be used in this filter format.'),
    '#tree' => TRUE,
  );
  foreach ($all as $id => $filter) {
    $form['filters'][$id] = array('#type' => 'checkbox',
      '#title' => $filter->name,
      '#default_value' => isset($enabled[$id]),
      '#description' => module_invoke($filter->module, 'filter', 'description', $filter->delta),
    );
  }
157
  if (!empty($format->format)) {
158 159 160 161
    $form['format'] = array('#type' => 'hidden', '#value' => $format->format);

    // Composition tips (guidelines)
    $tips = _filter_tips($format->format, FALSE);
162
    $tiplist = theme('filter_tips', $tips, FALSE);
163
    if (!$tiplist) {
164
      $tiplist = '<p>' . t('No guidelines available.') . '</p>';
165
    }
166 167 168
    else {
      $tiplist .= theme('filter_tips_more_info');
    }
169
    $group = '<p>' . t('These are the guidelines that users will see for posting in this text format. They are automatically generated from the filter settings.') . '</p>';
170
    $group .= $tiplist;
171
    $form['tips'] = array('#markup' => '<h2>' . t('Formatting guidelines') . '</h2>' . $group);
172 173 174 175 176 177 178 179 180 181 182 183
  }
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));

  return $form;
}

/**
 * Validate filter format form submissions.
 */
function filter_admin_format_form_validate($form, &$form_state) {
  if (!isset($form_state['values']['format'])) {
    $name = trim($form_state['values']['name']);
184
    $result = db_fetch_object(db_query("SELECT format FROM {filter_format} WHERE name='%s'", $name));
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    if ($result) {
      form_set_error('name', t('Filter format names need to be unique. A format named %name already exists.', array('%name' => $name)));
    }
  }
}

/**
 * Process filter 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);
  $name = trim($form_state['values']['name']);
  $cache = TRUE;

  // Add a new filter format.
  if (!$format) {
    $new = TRUE;
203 204
    db_query("INSERT INTO {filter_format} (name) VALUES ('%s')", $name);
    $format = db_result(db_query("SELECT MAX(format) AS format FROM {filter_format}"));
205
    drupal_set_message(t('Added text format %format.', array('%format' => $name)));
206 207
  }
  else {
208
    drupal_set_message(t('The text format settings have been updated.'));
209 210
  }

211
  db_query("DELETE FROM {filter} WHERE format = %d", $format);
212 213 214 215 216
  foreach ($form_state['values']['filters'] as $id => $checked) {
    if ($checked) {
      list($module, $delta) = explode('/', $id);
      // Add new filters to the bottom.
      $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10;
217
      db_query("INSERT INTO {filter} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format, $module, $delta, $weight);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

      // Check if there are any 'no cache' filters.
      $cache &= !module_invoke($module, 'filter', 'no cache', $delta);
    }
  }

  // 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'])) {
236
    $roles = ',' . implode(',', array_keys(user_roles())) . ',';
237 238
  }
  else {
239
    $roles = ',' . implode(',', $roles) . ',';
240 241
  }

242
  db_query("UPDATE {filter_format} SET cache = %d, name='%s', roles = '%s' WHERE format = %d", $cache, $name, $roles, $format);
243

244
  cache_clear_all($format . ':', 'cache_filter', TRUE);
245 246

  // If a new filter was added, return to the main list of filters. Otherwise, stay on edit filter page to show new changes.
247
  $return = 'admin/settings/filter';
248
  if (!empty($new)) {
249
    $return .= '/' . $format;
250 251 252 253 254 255 256 257 258
  }
  $form_state['redirect'] = $return;
  return;
}

/**
 * Menu callback; confirm deletion of a format.
 *
 * @ingroup forms
259
 * @see filter_admin_delete_submit()
260 261 262
 */
function filter_admin_delete() {
  $format = arg(4);
263
  $format = db_fetch_object(db_query('SELECT * FROM {filter_format} WHERE format = %d', $format));
264 265 266 267 268 269

  if ($format) {
    if ($format->format != variable_get('filter_default_format', 1)) {
      $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
      $form['name'] = array('#type' => 'hidden', '#value' => $format->name);

270
      return confirm_form($form, t('Are you sure you want to delete the text format %format?', array('%format' => $format->name)), 'admin/settings/filter', t('If you have any content left in this text format, it will be switched to the default text format. This action cannot be undone.'), t('Delete'), t('Cancel'));
271 272 273
    }
    else {
      drupal_set_message(t('The default format cannot be deleted.'));
274
      drupal_goto('admin/settings/filter');
275 276 277 278 279 280 281 282 283 284 285
    }
  }
  else {
    drupal_not_found();
  }
}

/**
 * Process filter delete form submission.
 */
function filter_admin_delete_submit($form, &$form_state) {
286 287
  db_query("DELETE FROM {filter_format} WHERE format = %d", $form_state['values']['format']);
  db_query("DELETE FROM {filter} WHERE format = %d", $form_state['values']['format']);
288 289 290

  $default = variable_get('filter_default_format', 1);
  // Replace existing instances of the deleted format with the default format.
291 292
  db_query("UPDATE {node_revision} SET format = %d WHERE format = %d", $default, $form_state['values']['format']);
  db_query("UPDATE {comment} SET format = %d WHERE format = %d", $default, $form_state['values']['format']);
293
  db_query("UPDATE {box} SET format = %d WHERE format = %d", $default, $form_state['values']['format']);
294

295
  cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE);
296
  drupal_set_message(t('Deleted text format %format.', array('%format' => $form_state['values']['name'])));
297

298
  $form_state['redirect'] = 'admin/settings/filter';
299 300 301
  return;
}

302 303 304 305 306

/**
 * Menu callback; display settings defined by a format's filters.
 */
function filter_admin_configure_page($format) {
307
  drupal_set_title(t("Configure %format", array('%format' => $format->name)), PASS_THROUGH);
308 309 310
  return drupal_get_form('filter_admin_configure', $format);
}

311
/**
312
 * Build a form to change the settings for a format's filters.
313 314 315 316 317 318 319 320 321 322 323 324 325 326
 *
 * @ingroup forms
 */
function filter_admin_configure(&$form_state, $format) {
  $list = filter_list_format($format->format);
  $form = array();
  foreach ($list as $filter) {
    $form_module = module_invoke($filter->module, 'filter', 'settings', $filter->delta, $format->format);
    if (isset($form_module) && is_array($form_module)) {
      $form = array_merge($form, $form_module);
    }
  }

  if (!empty($form)) {
327
    $form = system_settings_form($form, TRUE);
328 329
  }
  else {
330
    $form['error'] = array('#markup' => t('No settings are available.'));
331
  }
332 333
  $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
  $form['#submit'][] = 'filter_admin_configure_submit';
334 335 336
  return $form;
}

337 338 339 340
/**
 * Clear the filter's cache when configuration settings are saved.
 */
function filter_admin_configure_submit($form, &$form_state) {
341
  cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE);
342 343
}

344 345
/**
 * Menu callback; display form for ordering filters for a format.
346 347
 */
function filter_admin_order_page($format) {
348
  drupal_set_title(t("Rearrange %format", array('%format' => $format->name)), PASS_THROUGH);
349 350 351 352 353
  return drupal_get_form('filter_admin_order', $format);
}

/**
 * Build the form for ordering filters for a format.
354 355
 *
 * @ingroup forms
356 357
 * @see theme_filter_admin_order()
 * @see filter_admin_order_submit()
358 359 360 361 362 363 364
 */
function filter_admin_order(&$form_state, $format = NULL) {
  // Get list (with forced refresh).
  $filters = filter_list_format($format->format);

  $form['weights'] = array('#tree' => TRUE);
  foreach ($filters as $id => $filter) {
365
    $form['names'][$id] = array('#markup' => $filter->name);
366 367 368 369 370 371 372 373 374 375
    $form['weights'][$id] = array('#type' => 'weight', '#default_value' => $filter->weight);
  }
  $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));

  return $form;
}

/**
 * Theme filter order configuration form.
376 377
 *
 * @ingroup themeable
378 379 380 381 382 383 384
 */
function theme_filter_admin_order($form) {
  $header = array(t('Name'), t('Weight'));
  $rows = array();
  foreach (element_children($form['names']) as $id) {
    // Don't take form control structures.
    if (is_array($form['names'][$id])) {
385 386 387 388 389
      $form['weights'][$id]['#attributes']['class'] = 'filter-order-weight';
      $rows[] = array(
        'data' => array(drupal_render($form['names'][$id]), drupal_render($form['weights'][$id])),
        'class' => 'draggable',
      );
390 391 392
    }
  }

393
  $output = theme('table', $header, $rows, array('id' => 'filter-order'));
394 395
  $output .= drupal_render($form);

396
  drupal_add_tabledrag('filter-order', 'order', 'sibling', 'filter-order-weight', NULL, NULL, FALSE);
Dries's avatar
Dries committed
397

398 399 400 401 402 403 404 405 406
  return $output;
}

/**
 * Process filter order configuration form submission.
 */
function filter_admin_order_submit($form, &$form_state) {
  foreach ($form_state['values']['weights'] as $id => $weight) {
    list($module, $delta) = explode('/', $id);
407
    db_query("UPDATE {filter} SET weight = %d WHERE format = %d AND module = '%s' AND delta = %d", $weight, $form_state['values']['format'], $module, $delta);
408 409 410
  }
  drupal_set_message(t('The filter ordering has been saved.'));

411
  cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE);
412
}