filter.admin.inc 15.1 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
  foreach ($formats as $id => $format) {
    $roles = array();
    foreach (user_roles() as $rid => $name) {
      // Prepare a roles array with roles that may access the filter.
27
      if (strpos($format->roles, ",$rid,") !== FALSE) {
28 29 30 31 32
        $roles[] = $name;
      }
    }
    $default = ($id == variable_get('filter_default_format', 1));
    $options[$id] = '';
33
    $form[$id]['name'] = array('#markup' => $format->name);
34
    $form[$id]['roles'] = array('#markup' => $default ? t('All roles may use the default format') : ($roles ? implode(', ', $roles) : t('No roles may use this format')));
35 36
    $form[$id]['configure'] = array('#markup' => l(t('configure'), 'admin/settings/formats/' . $id));
    $form[$id]['delete'] = array('#markup' => $default ? '' : l(t('delete'), 'admin/settings/formats/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 54 55 56
      db_update('filter_format')
        ->fields(array('weight' => $data['weight']))
        ->condition('format', $id)
        ->execute();
57 58
    }
  }
59
  drupal_set_message(t('The text format ordering has been saved.'));
60 61 62 63 64 65 66 67 68
}

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

90
  drupal_add_tabledrag('text-format-order', 'order', 'sibling', 'text-format-order-weight');
91

92 93 94
  return $output;
}

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

106
/**
107
 * Generate a text format form.
108 109
 *
 * @ingroup forms
110 111
 * @see filter_admin_format_form_validate()
 * @see filter_admin_format_form_submit()
112
 */
113
function filter_admin_format_form(&$form_state, $format) {
114 115 116 117 118 119 120
  $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',
121
    '#title' => t('Name'),
122
    '#default_value' => $format->name,
123
    '#description' => t('Specify a unique name for this text format.'),
124 125 126 127 128 129
    '#required' => TRUE,
  );

  // Add a row of checkboxes for form group.
  $form['roles'] = array('#type' => 'fieldset',
    '#title' => t('Roles'),
130
    '#description' => $default ? $help : t('Choose which roles may use this text format. Note that roles with the "administer filters" permission can always use all text formats.'),
131 132 133 134
    '#tree' => TRUE,
  );

  foreach (user_roles() as $rid => $name) {
135
    $checked = strpos($format->roles, ",$rid,") !== FALSE;
136 137 138 139 140 141 142 143 144 145 146 147 148 149
    $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'),
150
    '#description' => t('Choose the filters that will be used in this text format.'),
151 152 153
    '#tree' => TRUE,
  );
  foreach ($all as $id => $filter) {
154
    $filter_info = module_invoke($filter->module, 'filter_info');
155 156 157
    $form['filters'][$id] = array('#type' => 'checkbox',
      '#title' => $filter->name,
      '#default_value' => isset($enabled[$id]),
158
      '#description' => $filter_info[$filter->delta]['description'],
159 160
    );
  }
161
  if (!empty($format->format)) {
162 163 164 165
    $form['format'] = array('#type' => 'hidden', '#value' => $format->format);

    // Composition tips (guidelines)
    $tips = _filter_tips($format->format, FALSE);
166
    $tiplist = theme('filter_tips', $tips, FALSE);
167
    if (!$tiplist) {
168
      $tiplist = '<p>' . t('No guidelines available.') . '</p>';
169
    }
170 171 172
    else {
      $tiplist .= theme('filter_tips_more_info');
    }
173
    $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>';
174
    $group .= $tiplist;
175
    $form['tips'] = array('#markup' => '<h2>' . t('Formatting guidelines') . '</h2>' . $group);
176 177 178 179 180 181 182
  }
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));

  return $form;
}

/**
183
 * Validate text format form submissions.
184 185 186 187
 */
function filter_admin_format_form_validate($form, &$form_state) {
  if (!isset($form_state['values']['format'])) {
    $name = trim($form_state['values']['name']);
188
    $result = db_query("SELECT format FROM {filter_format} WHERE name = :name", array(':name' => $name))->fetchField();
189
    if ($result) {
190
      form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $name)));
191 192 193 194 195
    }
  }
}

/**
196
 * Process text format form submissions.
197 198 199 200 201 202 203
 */
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;

204
  // Add a new text format.
205 206
  if (!$format) {
    $new = TRUE;
207 208 209 210
    db_insert('filter_format')
      ->fields(array('name' => $name))
      ->execute();
    $format = db_query("SELECT MAX(format) AS format FROM {filter_format}")->fetchField();
211
    drupal_set_message(t('Added text format %format.', array('%format' => $name)));
212 213
  }
  else {
214
    drupal_set_message(t('The text format settings have been updated.'));
215
  }
216 217 218 219
  db_delete('filter')
    ->condition('format', $format)
    ->execute();
  $query = db_insert('filter')->fields(array('format', 'module', 'delta', 'weight'));
220 221 222 223 224
  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;
225 226 227 228 229 230
      $query->values(array(
        'format' => $format,
        'module' => $module,
        'delta'  => $delta,
        'weight' => $weight,
      ));
231 232 233
      // Check if there are any 'no cache' filters.
      $cache &= !module_invoke($module, 'filter', 'no cache', $delta);
    }
234
    $query->execute();
235 236 237 238 239 240 241 242 243 244 245 246 247 248
  }

  // 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'])) {
249
    $roles = ',' . implode(',', array_keys(user_roles())) . ',';
250 251
  }
  else {
252
    $roles = ',' . implode(',', $roles) . ',';
253 254
  }

255 256 257 258 259 260 261 262
  db_update('filter_format')
    ->fields(array(
      'cache' => $cache,
      'name'  => $name,
      'roles' => $roles,
    ))
    ->condition('format', $format)
    ->execute();
263

264
  cache_clear_all($format . ':', 'cache_filter', TRUE);
265 266

  // If a new filter was added, return to the main list of filters. Otherwise, stay on edit filter page to show new changes.
267
  $return = 'admin/settings/formats';
268
  if (!empty($new)) {
269
    $return .= '/' . $format;
270 271 272 273 274 275 276 277 278
  }
  $form_state['redirect'] = $return;
  return;
}

/**
 * Menu callback; confirm deletion of a format.
 *
 * @ingroup forms
279
 * @see filter_admin_delete_submit()
280 281 282
 */
function filter_admin_delete() {
  $format = arg(4);
283
  $format = db_query('SELECT * FROM {filter_format} WHERE format = :format', array(':format' => $format))->fetchObject();
284 285 286 287 288 289

  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);

290
      return confirm_form($form, t('Are you sure you want to delete the text format %format?', array('%format' => $format->name)), 'admin/settings/formats', 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'));
291 292 293
    }
    else {
      drupal_set_message(t('The default format cannot be deleted.'));
294
      drupal_goto('admin/settings/formats');
295 296 297 298 299 300 301 302 303 304 305
    }
  }
  else {
    drupal_not_found();
  }
}

/**
 * Process filter delete form submission.
 */
function filter_admin_delete_submit($form, &$form_state) {
306 307 308 309 310 311
  db_delete('filter_format')
    ->condition('format', $form_state['values']['format'])
    ->execute();
  db_delete('filter')
    ->condition('format', $form_state['values']['format'])
    ->execute();
312 313 314

  $default = variable_get('filter_default_format', 1);
  // Replace existing instances of the deleted format with the default format.
315 316 317 318 319 320 321 322 323 324 325 326
  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();
  }
327

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

331
  $form_state['redirect'] = 'admin/settings/formats';
332 333 334
  return;
}

335 336 337 338 339

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

344
/**
345
 * Build a form to change the settings for a format's filters.
346 347 348 349 350 351 352
 *
 * @ingroup forms
 */
function filter_admin_configure(&$form_state, $format) {
  $list = filter_list_format($format->format);
  $form = array();
  foreach ($list as $filter) {
353 354 355 356
    $filter_info = module_invoke($filter->module, 'filter_info');
    if (isset($filter_info[$filter->delta]['settings callback']) && drupal_function_exists($filter_info[$filter->delta]['settings callback'])) {
      $form_module = call_user_func($filter_info[$filter->delta]['settings callback'], $format->format);
    }
357 358 359 360 361 362
    if (isset($form_module) && is_array($form_module)) {
      $form = array_merge($form, $form_module);
    }
  }

  if (!empty($form)) {
363
    $form = system_settings_form($form, TRUE);
364 365
  }
  else {
366
    $form['error'] = array('#markup' => t('No settings are available.'));
367
  }
368 369
  $form['format'] = array('#type' => 'hidden', '#value' => $format->format);
  $form['#submit'][] = 'filter_admin_configure_submit';
370 371 372
  return $form;
}

373 374 375 376
/**
 * Clear the filter's cache when configuration settings are saved.
 */
function filter_admin_configure_submit($form, &$form_state) {
377
  cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE);
378 379
}

380 381
/**
 * Menu callback; display form for ordering filters for a format.
382 383
 */
function filter_admin_order_page($format) {
384
  drupal_set_title(t("Rearrange %format", array('%format' => $format->name)), PASS_THROUGH);
385 386 387 388 389
  return drupal_get_form('filter_admin_order', $format);
}

/**
 * Build the form for ordering filters for a format.
390 391
 *
 * @ingroup forms
392 393
 * @see theme_filter_admin_order()
 * @see filter_admin_order_submit()
394 395 396 397 398 399 400
 */
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) {
401
    $form['names'][$id] = array('#markup' => $filter->name);
402 403 404 405 406 407 408 409 410 411
    $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.
412 413
 *
 * @ingroup themeable
414 415 416 417 418 419 420
 */
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])) {
421 422 423 424 425
      $form['weights'][$id]['#attributes']['class'] = 'filter-order-weight';
      $rows[] = array(
        'data' => array(drupal_render($form['names'][$id]), drupal_render($form['weights'][$id])),
        'class' => 'draggable',
      );
426 427 428
    }
  }

429
  $output = theme('table', $header, $rows, array('id' => 'filter-order'));
430
  $output .= drupal_render_children($form);
431

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

434 435 436 437 438 439 440 441 442
  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);
443 444 445 446 447 448
    db_update('filter')
      ->fields(array('weight' => $weight))
      ->condition('format', $form_state['values']['format'])
      ->condition('module', $module)
      ->condition('delta', $delta)
      ->execute();
449 450 451
  }
  drupal_set_message(t('The filter ordering has been saved.'));

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