filter.admin.inc 12.5 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 allows them to be rearranged.
11
12
 *
 * @ingroup forms
13
 * @see filter_admin_overview_submit()
14
 */
15
function filter_admin_overview($form) {
16
17
  // Overview of all formats.
  $formats = filter_formats();
18
  $fallback_format = filter_fallback_format();
19

20
  $form['#tree'] = TRUE;
21
  foreach ($formats as $id => $format) {
22
    // Check whether this is the fallback text format. This format is available
23
    // to all roles and cannot be disabled via the admin interface.
24
25
    $form['formats'][$id]['#is_fallback'] = ($id == $fallback_format);
    if ($form['formats'][$id]['#is_fallback']) {
26
27
      $form['formats'][$id]['name'] = array('#markup' => drupal_placeholder($format->name));
      $roles_markup = drupal_placeholder(t('All roles may use this format'));
28
    }
29
30
    else {
      $form['formats'][$id]['name'] = array('#markup' => check_plain($format->name));
31
      $roles = array_map('check_plain', filter_get_roles_by_format($format));
32
33
34
      $roles_markup = $roles ? implode(', ', $roles) : t('No roles may use this format');
    }
    $form['formats'][$id]['roles'] = array('#markup' => $roles_markup);
35
    $form['formats'][$id]['configure'] = array('#type' => 'link', '#title' => t('configure'), '#href' => 'admin/config/content/formats/' . $id);
36
    $form['formats'][$id]['disable'] = array('#type' => 'link', '#title' => t('disable'), '#href' => 'admin/config/content/formats/' . $id . '/disable', '#access' => !$form['formats'][$id]['#is_fallback']);
37
38
39
40
41
42
    $form['formats'][$id]['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for @title', array('@title' => $format->name)),
      '#title_display' => 'invisible',
      '#default_value' => $format->weight,
    );
43
  }
44
  $form['actions'] = array('#type' => 'actions');
45
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
46
47
48
49
  return $form;
}

function filter_admin_overview_submit($form, &$form_state) {
50
  foreach ($form_state['values']['formats'] as $id => $data) {
51
52
    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
  filter_formats_reset();
60
  drupal_set_message(t('The text format ordering has been saved.'));
61
62
63
}

/**
64
65
66
67
68
 * Returns HTML for the text format administration overview form.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
69
70
71
 *
 * @ingroup themeable
 */
72
73
74
function theme_filter_admin_overview($variables) {
  $form = $variables['form'];

75
  $rows = array();
76
77
78
79
80
81
82
83
  foreach (element_children($form['formats']) as $id) {
    $form['formats'][$id]['weight']['#attributes']['class'] = array('text-format-order-weight');
    $rows[] = array(
      'data' => array(
        drupal_render($form['formats'][$id]['name']),
        drupal_render($form['formats'][$id]['roles']),
        drupal_render($form['formats'][$id]['weight']),
        drupal_render($form['formats'][$id]['configure']),
84
        drupal_render($form['formats'][$id]['disable']),
85
86
87
      ),
      'class' => array('draggable'),
    );
88
  }
89
  $header = array(t('Name'), t('Roles'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
90
  $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'text-format-order')));
91
  $output .= drupal_render_children($form);
92

93
  drupal_add_tabledrag('text-format-order', 'order', 'sibling', 'text-format-order-weight');
94

95
96
97
  return $output;
}

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

112
/**
113
 * Generate a text format form.
114
115
 *
 * @ingroup forms
116
117
 * @see filter_admin_format_form_validate()
 * @see filter_admin_format_form_submit()
118
 */
119
function filter_admin_format_form($form, &$form_state, $format) {
120
  $is_fallback = ($format->format == filter_fallback_format());
121

122
123
  $form['#format'] = $format;
  $form['#tree'] = TRUE;
124
125
  $form['#attached']['js'][] = drupal_get_path('module', 'filter') . '/filter.admin.js';
  $form['#attached']['css'][] = drupal_get_path('module', 'filter') . '/filter.css';
126

127
128
  $form['name'] = array(
    '#type' => 'textfield',
129
    '#title' => t('Name'),
130
131
132
    '#default_value' => $format->name,
    '#required' => TRUE,
  );
133
134
135
136
137
138
  $form['format'] = array(
    '#type' => 'machine_name',
    '#required' => TRUE,
    '#default_value' => $format->format,
    '#maxlength' => 255,
    '#machine_name' => array(
139
      'exists' => 'filter_format_exists',
140
141
142
    ),
    '#disabled' => !empty($format->format),
  );
143

144
145
146
  // Add user role access selection.
  $form['roles'] = array(
    '#type' => 'checkboxes',
147
    '#title' => t('Roles'),
148
    '#options' => array_map('check_plain', user_roles()),
149
    '#disabled' => $is_fallback,
150
  );
151
152
153
  if ($is_fallback) {
    $form['roles']['#description'] = t('All roles for this text format must be enabled and cannot be changed.');
  }
154
155
156
157
158
159
160
161
162
163
  if (!empty($format->format)) {
    // If editing an existing text format, pre-select its current permissions.
    $form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($format));
  }
  elseif ($admin_role = variable_get('user_admin_role', 0)) {
    // If adding a new text format and the site has an administrative role,
    // pre-select that role so as to grant administrators access to the new
    // text format permission by default.
    $form['roles']['#default_value'] = array($admin_role);
  }
164

165
166
  // Retrieve available filters and load all configured filters for existing
  // text formats.
167
  $filter_info = filter_get_filters();
168
  $filters = !empty($format->format) ? filter_list_format($format->format) : array();
169

170
  // Prepare filters for form sections.
171
  foreach ($filter_info as $name => $filter) {
172
173
    // Create an empty filter object for new/unconfigured filters.
    if (!isset($filters[$name])) {
174
      $filters[$name] = new stdClass();
175
176
177
      $filters[$name]->format = $format->format;
      $filters[$name]->module = $filter['module'];
      $filters[$name]->name = $name;
178
      $filters[$name]->status = 0;
179
      $filters[$name]->weight = $filter['weight'];
180
181
      $filters[$name]->settings = array();
    }
182
183
184
185
186
187
188
189
190
191
192
193
  }
  $form['#filters'] = $filters;

  // Filter status.
  $form['filters']['status'] = array(
    '#type' => 'item',
    '#title' => t('Enabled filters'),
    '#prefix' => '<div id="filters-status-wrapper">',
    '#suffix' => '</div>',
  );
  foreach ($filter_info as $name => $filter) {
    $form['filters']['status'][$name] = array(
194
195
      '#type' => 'checkbox',
      '#title' => $filter['title'],
196
      '#default_value' => $filters[$name]->status,
197
      '#parents' => array('filters', $name, 'status'),
198
      '#description' => $filter['description'],
199
      '#weight' => $filter['weight'],
200
201
    );
  }
202

203
204
205
206
207
208
209
210
211
212
213
214
  // Filter order (tabledrag).
  $form['filters']['order'] = array(
    '#type' => 'item',
    '#title' => t('Filter processing order'),
    '#theme' => 'filter_admin_format_filter_order',
  );
  foreach ($filter_info as $name => $filter) {
    $form['filters']['order'][$name]['filter'] = array(
      '#markup' => $filter['title'],
    );
    $form['filters']['order'][$name]['weight'] = array(
      '#type' => 'weight',
215
216
      '#title' => t('Weight for @title', array('@title' => $filter['title'])),
      '#title_display' => 'invisible',
217
218
219
220
      '#delta' => 50,
      '#default_value' => $filters[$name]->weight,
      '#parents' => array('filters', $name, 'weight'),
    );
221
    $form['filters']['order'][$name]['#weight'] = $filters[$name]->weight;
222
  }
223

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  // Filter settings.
  $form['filter_settings_title'] = array(
    '#type' => 'item',
    '#title' => t('Filter settings'),
  );
  $form['filter_settings'] = array(
    '#type' => 'vertical_tabs',
  );

  foreach ($filter_info as $name => $filter) {
    if (isset($filter['settings callback']) && function_exists($filter['settings callback'])) {
      $function = $filter['settings callback'];
      // Pass along stored filter settings and default settings, but also the
      // format object and all filters to allow for complex implementations.
      $defaults = (isset($filter['default settings']) ? $filter['default settings'] : array());
      $settings_form = $function($form, $form_state, $filters[$name], $format, $defaults, $filters);
      if (!empty($settings_form)) {
        $form['filters']['settings'][$name] = array(
          '#type' => 'fieldset',
          '#title' => $filter['title'],
          '#parents' => array('filters', $name, 'settings'),
          '#weight' => $filter['weight'],
          '#group' => 'filter_settings',
        );
        $form['filters']['settings'][$name] += $settings_form;
      }
250
    }
251
252
  }

253
  $form['actions'] = array('#type' => 'actions');
254
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
255
256
257
258

  return $form;
}

259
/**
260
261
262
263
264
 * Returns HTML for a text format's filter order form.
 *
 * @param $variables
 *   An associative array containing:
 *   - element: A render element representing the form.
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
 *
 * @ingroup themeable
 */
function theme_filter_admin_format_filter_order($variables) {
  $element = $variables['element'];

  // Filter order (tabledrag).
  $rows = array();
  foreach (element_children($element, TRUE) as $name) {
    $element[$name]['weight']['#attributes']['class'][] = 'filter-order-weight';
    $rows[] = array(
      'data' => array(
        drupal_render($element[$name]['filter']),
        drupal_render($element[$name]['weight']),
      ),
      'class' => array('draggable'),
    );
  }
  $output = drupal_render_children($element);
  $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'filter-order')));
  drupal_add_tabledrag('filter-order', 'order', 'sibling', 'filter-order-weight', NULL, NULL, TRUE);

  return $output;
}

290
/**
291
 * Validate text format form submissions.
292
293
 */
function filter_admin_format_form_validate($form, &$form_state) {
294
295
296
297
298
299
300
301
302
303
  $format_format = trim($form_state['values']['format']);
  $format_name = trim($form_state['values']['name']);

  // Ensure that the values to be saved later are exactly the ones validated.
  form_set_value($form['format'], $format_format, $form_state);
  form_set_value($form['name'], $format_name, $form_state);

  $result = db_query("SELECT format FROM {filter_format} WHERE name = :name AND format <> :format", array(':name' => $format_name, ':format' => $format_format))->fetchField();
  if ($result) {
    form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name)));
304
305
306
307
  }
}

/**
308
 * Process text format form submissions.
309
310
 */
function filter_admin_format_form_submit($form, &$form_state) {
311
312
313
  // Remove unnecessary values.
  form_state_values_clean($form_state);

314
315
316
317
318
  // Add the submitted form values to the text format, and save it.
  $format = $form['#format'];
  foreach ($form_state['values'] as $key => $value) {
    $format->$key = $value;
  }
319
  $status = filter_format_save($format);
320

321
  // Save user permissions.
322
323
324
325
326
327
  if ($permission = filter_permission_name($format)) {
    foreach ($format->roles as $rid => $enabled) {
      user_role_change_permissions($rid, array($permission => $enabled));
    }
  }

328
329
330
331
  switch ($status) {
    case SAVED_NEW:
      drupal_set_message(t('Added text format %format.', array('%format' => $format->name)));
      break;
332

333
    case SAVED_UPDATED:
334
      drupal_set_message(t('The text format %format has been updated.', array('%format' => $format->name)));
335
      break;
336
337
338
339
340
341
342
  }
}

/**
 * Menu callback; confirm deletion of a format.
 *
 * @ingroup forms
343
 * @see filter_admin_disable_submit()
344
 */
345
function filter_admin_disable($form, &$form_state, $format) {
346
347
348
  $form['#format'] = $format;

  return confirm_form($form,
349
    t('Are you sure you want to disable the text format %format?', array('%format' => $format->name)),
350
    'admin/config/content/formats',
351
    t('Disabled text formats are completely removed from the administrative interface, and any content stored with that format will not be displayed. This action cannot be undone.'),
352
    t('Disable')
353
  );
354
355
356
}

/**
357
 * Process filter disable form submission.
358
 */
359
function filter_admin_disable_submit($form, &$form_state) {
360
  $format = $form['#format'];
361
362
  filter_format_disable($format);
  drupal_set_message(t('Disabled text format %format.', array('%format' => $format->name)));
363

364
  $form_state['redirect'] = 'admin/config/content/formats';
365
366
}