Project

General

Profile

Paste
Download (14.4 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / filter / filter.admin.inc @ 27e02aed

1
<?php
2

    
3
/**
4
 * @file
5
 * Administrative page callbacks for the Filter module.
6
 */
7

    
8
/**
9
 * Page callback: Form constructor for a form to list and reorder text formats.
10
 *
11
 * @ingroup forms
12
 * @see filter_menu()
13
 * @see filter_admin_overview_submit()
14
 */
15
function filter_admin_overview($form) {
16
  // Overview of all formats.
17
  $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
    $form['formats'][$id]['#is_fallback'] = ($id == $fallback_format);
25
    if ($form['formats'][$id]['#is_fallback']) {
26
      $form['formats'][$id]['name'] = array('#markup' => drupal_placeholder($format->name));
27
      $roles_markup = drupal_placeholder(t('All roles may use this format'));
28
    }
29
    else {
30
      $form['formats'][$id]['name'] = array('#markup' => check_plain($format->name));
31
      $roles = array_map('check_plain', filter_get_roles_by_format($format));
32
      $roles_markup = $roles ? implode(', ', $roles) : t('No roles may use this format');
33
    }
34
    $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
    $form['formats'][$id]['weight'] = array(
38
      '#type' => 'weight',
39
      '#title' => t('Weight for @title', array('@title' => $format->name)),
40
      '#title_display' => 'invisible',
41
      '#default_value' => $format->weight,
42
    );
43
  }
44
  $form['actions'] = array('#type' => 'actions');
45
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
46
  return $form;
47
}
48

    
49
/**
50
 * Form submission handler for filter_admin_overview().
51
 */
52
function filter_admin_overview_submit($form, &$form_state) {
53
  foreach ($form_state['values']['formats'] as $id => $data) {
54
    if (is_array($data) && isset($data['weight'])) {
55
      // Only update if this is a form element with weight.
56
      db_update('filter_format')
57
        ->fields(array('weight' => $data['weight']))
58
        ->condition('format', $id)
59
        ->execute();
60
    }
61
  }
62
  filter_formats_reset();
63
  drupal_set_message(t('The text format ordering has been saved.'));
64
}
65

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

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

    
96
  drupal_add_tabledrag('text-format-order', 'order', 'sibling', 'text-format-order-weight');
97

    
98
  return $output;
99
}
100

    
101
/**
102
 * Page callback: Displays the text format add/edit form.
103
 *
104
 * @param object|null $format
105
 *   (optional) An object representing a format, with the following properties:
106
 *   - format: A machine-readable name representing the ID of the text format
107
 *     to save. If this corresponds to an existing text format, that format
108
 *     will be updated; otherwise, a new format will be created.
109
 *   - name: The title of the text format.
110
 *   - cache: (optional) An integer indicating whether the text format is
111
 *     cacheable (1) or not (0). Defaults to 1.
112
 *   - status: (optional) An integer indicating whether the text format is
113
 *     enabled (1) or not (0). Defaults to 1.
114
 *   - weight: (optional) The weight of the text format, which controls its
115
 *     placement in text format lists. If omitted, the weight is set to 0.
116
 *     Defaults to NULL.
117
 *
118
 * @return
119
 *   A form array.
120
 *
121
 * @see filter_menu()
122
 */
123
function filter_admin_format_page($format = NULL) {
124
  if (!isset($format->name)) {
125
    drupal_set_title(t('Add text format'));
126
    $format = (object) array(
127
      'format' => NULL,
128
      'name' => '',
129
    );
130
  }
131
  return drupal_get_form('filter_admin_format_form', $format);
132
}
133

    
134
/**
135
 * Form constructor for the text format add/edit form.
136
 *
137
 * @param $format
138
 *   A format object having the properties:
139
 *   - format: A machine-readable name representing the ID of the text format to
140
 *     save. If this corresponds to an existing text format, that format will be
141
 *     updated; otherwise, a new format will be created.
142
 *   - name: The title of the text format.
143
 *   - cache: An integer indicating whether the text format is cacheable (1) or
144
 *     not (0). Defaults to 1.
145
 *   - status: (optional) An integer indicating whether the text format is
146
 *     enabled (1) or not (0). Defaults to 1.
147
 *   - weight: (optional) The weight of the text format, which controls its
148
 *     placement in text format lists. If omitted, the weight is set to 0.
149
 *
150
 * @see filter_admin_format_form_validate()
151
 * @see filter_admin_format_form_submit()
152
 * @ingroup forms
153
 */
154
function filter_admin_format_form($form, &$form_state, $format) {
155
  $is_fallback = ($format->format == filter_fallback_format());
156

    
157
  $form['#format'] = $format;
158
  $form['#tree'] = TRUE;
159
  $form['#attached']['js'][] = drupal_get_path('module', 'filter') . '/filter.admin.js';
160
  $form['#attached']['css'][] = drupal_get_path('module', 'filter') . '/filter.css';
161

    
162
  $form['name'] = array(
163
    '#type' => 'textfield',
164
    '#title' => t('Name'),
165
    '#default_value' => $format->name,
166
    '#required' => TRUE,
167
  );
168
  $form['format'] = array(
169
    '#type' => 'machine_name',
170
    '#required' => TRUE,
171
    '#default_value' => $format->format,
172
    '#maxlength' => 255,
173
    '#machine_name' => array(
174
      'exists' => 'filter_format_exists',
175
    ),
176
    '#disabled' => !empty($format->format),
177
  );
178

    
179
  // Add user role access selection.
180
  $form['roles'] = array(
181
    '#type' => 'checkboxes',
182
    '#title' => t('Roles'),
183
    '#options' => array_map('check_plain', user_roles()),
184
    '#disabled' => $is_fallback,
185
  );
186
  if ($is_fallback) {
187
    $form['roles']['#description'] = t('All roles for this text format must be enabled and cannot be changed.');
188
  }
189
  if (!empty($format->format)) {
190
    // If editing an existing text format, pre-select its current permissions.
191
    $form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($format));
192
  }
193
  elseif ($admin_role = variable_get('user_admin_role', 0)) {
194
    // If adding a new text format and the site has an administrative role,
195
    // pre-select that role so as to grant administrators access to the new
196
    // text format permission by default.
197
    $form['roles']['#default_value'] = array($admin_role);
198
  }
199

    
200
  // Retrieve available filters and load all configured filters for existing
201
  // text formats.
202
  $filter_info = filter_get_filters();
203
  $filters = !empty($format->format) ? filter_list_format($format->format) : array();
204

    
205
  // Prepare filters for form sections.
206
  foreach ($filter_info as $name => $filter) {
207
    // Create an empty filter object for new/unconfigured filters.
208
    if (!isset($filters[$name])) {
209
      $filters[$name] = new stdClass();
210
      $filters[$name]->format = $format->format;
211
      $filters[$name]->module = $filter['module'];
212
      $filters[$name]->name = $name;
213
      $filters[$name]->status = 0;
214
      $filters[$name]->weight = $filter['weight'];
215
      $filters[$name]->settings = array();
216
    }
217
  }
218
  $form['#filters'] = $filters;
219

    
220
  // Filter status.
221
  $form['filters']['status'] = array(
222
    '#type' => 'item',
223
    '#title' => t('Enabled filters'),
224
    '#prefix' => '<div id="filters-status-wrapper">',
225
    '#suffix' => '</div>',
226
  );
227
  foreach ($filter_info as $name => $filter) {
228
    $form['filters']['status'][$name] = array(
229
      '#type' => 'checkbox',
230
      '#title' => $filter['title'],
231
      '#default_value' => $filters[$name]->status,
232
      '#parents' => array('filters', $name, 'status'),
233
      '#description' => $filter['description'],
234
      '#weight' => $filter['weight'],
235
    );
236
  }
237

    
238
  // Filter order (tabledrag).
239
  $form['filters']['order'] = array(
240
    '#type' => 'item',
241
    '#title' => t('Filter processing order'),
242
    '#theme' => 'filter_admin_format_filter_order',
243
  );
244
  foreach ($filter_info as $name => $filter) {
245
    $form['filters']['order'][$name]['filter'] = array(
246
      '#markup' => $filter['title'],
247
    );
248
    $form['filters']['order'][$name]['weight'] = array(
249
      '#type' => 'weight',
250
      '#title' => t('Weight for @title', array('@title' => $filter['title'])),
251
      '#title_display' => 'invisible',
252
      '#delta' => 50,
253
      '#default_value' => $filters[$name]->weight,
254
      '#parents' => array('filters', $name, 'weight'),
255
    );
256
    $form['filters']['order'][$name]['#weight'] = $filters[$name]->weight;
257
  }
258

    
259
  // Filter settings.
260
  $form['filter_settings_title'] = array(
261
    '#type' => 'item',
262
    '#title' => t('Filter settings'),
263
  );
264
  $form['filter_settings'] = array(
265
    '#type' => 'vertical_tabs',
266
  );
267

    
268
  foreach ($filter_info as $name => $filter) {
269
    if (isset($filter['settings callback']) && function_exists($filter['settings callback'])) {
270
      $function = $filter['settings callback'];
271
      // Pass along stored filter settings and default settings, but also the
272
      // format object and all filters to allow for complex implementations.
273
      $defaults = (isset($filter['default settings']) ? $filter['default settings'] : array());
274
      $settings_form = $function($form, $form_state, $filters[$name], $format, $defaults, $filters);
275
      if (!empty($settings_form)) {
276
        $form['filters']['settings'][$name] = array(
277
          '#type' => 'fieldset',
278
          '#title' => $filter['title'],
279
          '#parents' => array('filters', $name, 'settings'),
280
          '#weight' => $filter['weight'],
281
          '#group' => 'filter_settings',
282
        );
283
        $form['filters']['settings'][$name] += $settings_form;
284
      }
285
    }
286
  }
287

    
288
  $form['actions'] = array('#type' => 'actions');
289
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
290

    
291
  return $form;
292
}
293

    
294
/**
295
 * Returns HTML for a text format's filter order form.
296
 *
297
 * @param $variables
298
 *   An associative array containing:
299
 *   - element: A render element representing the form.
300
 *
301
 * @ingroup themeable
302
 */
303
function theme_filter_admin_format_filter_order($variables) {
304
  $element = $variables['element'];
305

    
306
  // Filter order (tabledrag).
307
  $rows = array();
308
  foreach (element_children($element, TRUE) as $name) {
309
    $element[$name]['weight']['#attributes']['class'][] = 'filter-order-weight';
310
    $rows[] = array(
311
      'data' => array(
312
        drupal_render($element[$name]['filter']),
313
        drupal_render($element[$name]['weight']),
314
      ),
315
      'class' => array('draggable'),
316
    );
317
  }
318
  $output = drupal_render_children($element);
319
  $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'filter-order')));
320
  drupal_add_tabledrag('filter-order', 'order', 'sibling', 'filter-order-weight', NULL, NULL, TRUE);
321

    
322
  return $output;
323
}
324

    
325
/**
326
 * Form validation handler for filter_admin_format_form().
327
 *
328
 * @see filter_admin_format_form_submit()
329
 */
330
function filter_admin_format_form_validate($form, &$form_state) {
331
  $format_format = trim($form_state['values']['format']);
332
  $format_name = trim($form_state['values']['name']);
333

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

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

    
344
/**
345
 * Form submission handler for filter_admin_format_form().
346
 *
347
 * @see filter_admin_format_form_validate()
348
 */
349
function filter_admin_format_form_submit($form, &$form_state) {
350
  // Remove unnecessary values.
351
  form_state_values_clean($form_state);
352

    
353
  // Add the submitted form values to the text format, and save it.
354
  $format = $form['#format'];
355
  foreach ($form_state['values'] as $key => $value) {
356
    $format->$key = $value;
357
  }
358
  $status = filter_format_save($format);
359

    
360
  // Save user permissions.
361
  if ($permission = filter_permission_name($format)) {
362
    foreach ($format->roles as $rid => $enabled) {
363
      user_role_change_permissions($rid, array($permission => $enabled));
364
    }
365
  }
366

    
367
  switch ($status) {
368
    case SAVED_NEW:
369
      drupal_set_message(t('Added text format %format.', array('%format' => $format->name)));
370
      break;
371

    
372
    case SAVED_UPDATED:
373
      drupal_set_message(t('The text format %format has been updated.', array('%format' => $format->name)));
374
      break;
375
  }
376
}
377

    
378
/**
379
 * Form constructor for the text format deletion confirmation form.
380
 *
381
 * @param $format
382
 *   An object representing a text format.
383
 *
384
 * @see filter_menu()
385
 * @see filter_admin_disable_submit()
386
 * @ingroup forms
387
 */
388
function filter_admin_disable($form, &$form_state, $format) {
389
  $form['#format'] = $format;
390

    
391
  return confirm_form($form,
392
    t('Are you sure you want to disable the text format %format?', array('%format' => $format->name)),
393
    'admin/config/content/formats',
394
    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.'),
395
    t('Disable')
396
  );
397
}
398

    
399
/**
400
 * Form submission handler for filter_admin_disable().
401
 */
402
function filter_admin_disable_submit($form, &$form_state) {
403
  $format = $form['#format'];
404
  filter_format_disable($format);
405
  drupal_set_message(t('Disabled text format %format.', array('%format' => $format->name)));
406

    
407
  $form_state['redirect'] = 'admin/config/content/formats';
408
}