| 1 |
<?php
|
| 2 |
// $Id: filter.admin.inc,v 1.49 2009/10/13 15:39:41 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Admin page callbacks for the filter module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 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 |
// 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 deleted 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' => theme('placeholder', array('text' => $format->name)));
|
| 27 |
$roles_markup = theme('placeholder', array('text' => t('All roles may use this format')));
|
| 28 |
}
|
| 29 |
else {
|
| 30 |
$form['formats'][$id]['name'] = array('#markup' => check_plain($format->name));
|
| 31 |
$roles = 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]['delete'] = array('#type' => 'link', '#title' => t('delete'), '#href' => 'admin/config/content/formats/' . $id . '/delete', '#access' => !$form['formats'][$id]['#is_fallback']);
|
| 37 |
$form['formats'][$id]['weight'] = array('#type' => 'weight', '#default_value' => $format->weight);
|
| 38 |
}
|
| 39 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
|
| 40 |
return $form;
|
| 41 |
}
|
| 42 |
|
| 43 |
function filter_admin_overview_submit($form, &$form_state) {
|
| 44 |
foreach ($form_state['values']['formats'] as $id => $data) {
|
| 45 |
if (is_array($data) && isset($data['weight'])) {
|
| 46 |
// Only update if this is a form element with weight.
|
| 47 |
db_update('filter_format')
|
| 48 |
->fields(array('weight' => $data['weight']))
|
| 49 |
->condition('format', $id)
|
| 50 |
->execute();
|
| 51 |
}
|
| 52 |
}
|
| 53 |
filter_formats_reset();
|
| 54 |
drupal_set_message(t('The text format ordering has been saved.'));
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Theme the text format administration overview form.
|
| 59 |
*
|
| 60 |
* @ingroup themeable
|
| 61 |
*/
|
| 62 |
function theme_filter_admin_overview($variables) {
|
| 63 |
$form = $variables['form'];
|
| 64 |
|
| 65 |
$rows = array();
|
| 66 |
foreach (element_children($form['formats']) as $id) {
|
| 67 |
$form['formats'][$id]['weight']['#attributes']['class'] = array('text-format-order-weight');
|
| 68 |
$rows[] = array(
|
| 69 |
'data' => array(
|
| 70 |
drupal_render($form['formats'][$id]['name']),
|
| 71 |
drupal_render($form['formats'][$id]['roles']),
|
| 72 |
drupal_render($form['formats'][$id]['weight']),
|
| 73 |
drupal_render($form['formats'][$id]['configure']),
|
| 74 |
drupal_render($form['formats'][$id]['delete']),
|
| 75 |
),
|
| 76 |
'class' => array('draggable'),
|
| 77 |
);
|
| 78 |
}
|
| 79 |
$header = array(t('Name'), t('Roles'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
|
| 80 |
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'text-format-order')));
|
| 81 |
$output .= drupal_render_children($form);
|
| 82 |
|
| 83 |
drupal_add_tabledrag('text-format-order', 'order', 'sibling', 'text-format-order-weight');
|
| 84 |
|
| 85 |
return $output;
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Menu callback; Display a text format form.
|
| 90 |
*/
|
| 91 |
function filter_admin_format_page($format = NULL) {
|
| 92 |
if (!isset($format->name)) {
|
| 93 |
drupal_set_title(t('Add text format'));
|
| 94 |
$format = (object) array('name' => '', 'format' => 0);
|
| 95 |
}
|
| 96 |
return drupal_get_form('filter_admin_format_form', $format);
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Generate a text format form.
|
| 101 |
*
|
| 102 |
* @ingroup forms
|
| 103 |
* @see filter_admin_format_form_validate()
|
| 104 |
* @see filter_admin_format_form_submit()
|
| 105 |
*/
|
| 106 |
function filter_admin_format_form($form, &$form_state, $format) {
|
| 107 |
$is_fallback = ($format->format == filter_fallback_format());
|
| 108 |
if ($is_fallback) {
|
| 109 |
$help = t('All roles for this text format must be enabled and cannot be changed.');
|
| 110 |
}
|
| 111 |
|
| 112 |
$form['name'] = array(
|
| 113 |
'#type' => 'textfield',
|
| 114 |
'#title' => t('Name'),
|
| 115 |
'#default_value' => $format->name,
|
| 116 |
'#description' => t('Specify a unique name for this text format.'),
|
| 117 |
'#required' => TRUE,
|
| 118 |
);
|
| 119 |
|
| 120 |
// Add a row of checkboxes for form group.
|
| 121 |
$form['roles'] = array('#type' => 'fieldset',
|
| 122 |
'#title' => t('Roles'),
|
| 123 |
'#description' => $is_fallback ? $help : t('Choose which roles may use this text format. Note that roles with the "administer filters" permission can always use all text formats.'),
|
| 124 |
'#tree' => TRUE,
|
| 125 |
);
|
| 126 |
$checked = filter_get_roles_by_format($format);
|
| 127 |
foreach (user_roles() as $rid => $name) {
|
| 128 |
$form['roles'][$rid] = array('#type' => 'checkbox',
|
| 129 |
'#title' => $name,
|
| 130 |
'#default_value' => ($is_fallback || isset($checked[$rid])),
|
| 131 |
);
|
| 132 |
if ($is_fallback) {
|
| 133 |
$form['roles'][$rid]['#disabled'] = TRUE;
|
| 134 |
}
|
| 135 |
}
|
| 136 |
// Table with filters
|
| 137 |
$filter_info = filter_get_filters();
|
| 138 |
$filters = filter_list_format($format->format, TRUE);
|
| 139 |
|
| 140 |
$form['filters'] = array('#type' => 'fieldset',
|
| 141 |
'#title' => t('Filters'),
|
| 142 |
'#description' => t('Choose the filters that will be used in this text format.'),
|
| 143 |
'#tree' => TRUE,
|
| 144 |
);
|
| 145 |
foreach ($filter_info as $name => $filter) {
|
| 146 |
$form['filters'][$name]['status'] = array(
|
| 147 |
'#type' => 'checkbox',
|
| 148 |
'#title' => $filter['title'],
|
| 149 |
'#default_value' => !empty($filters[$name]->status),
|
| 150 |
'#description' => $filter['description'],
|
| 151 |
);
|
| 152 |
}
|
| 153 |
if (!empty($format->format)) {
|
| 154 |
$form['format'] = array('#type' => 'hidden', '#value' => $format->format);
|
| 155 |
|
| 156 |
// Composition tips (guidelines)
|
| 157 |
$tips = _filter_tips($format->format, FALSE);
|
| 158 |
$tiplist = theme('filter_tips', array('tips' => $tips, 'long' => FALSE));
|
| 159 |
if (!$tiplist) {
|
| 160 |
$tiplist = '<p>' . t('No guidelines available.') . '</p>';
|
| 161 |
}
|
| 162 |
else {
|
| 163 |
$tiplist .= theme('filter_tips_more_info');
|
| 164 |
}
|
| 165 |
$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>';
|
| 166 |
$group .= $tiplist;
|
| 167 |
$form['tips'] = array('#markup' => '<h2>' . t('Formatting guidelines') . '</h2>' . $group);
|
| 168 |
}
|
| 169 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
|
| 170 |
|
| 171 |
return $form;
|
| 172 |
}
|
| 173 |
|
| 174 |
/**
|
| 175 |
* Validate text format form submissions.
|
| 176 |
*/
|
| 177 |
function filter_admin_format_form_validate($form, &$form_state) {
|
| 178 |
if (!isset($form_state['values']['format'])) {
|
| 179 |
$format_name = trim($form_state['values']['name']);
|
| 180 |
$result = db_query("SELECT format FROM {filter_format} WHERE name = :name", array(':name' => $format_name))->fetchField();
|
| 181 |
if ($result) {
|
| 182 |
form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name)));
|
| 183 |
}
|
| 184 |
}
|
| 185 |
}
|
| 186 |
|
| 187 |
/**
|
| 188 |
* Process text format form submissions.
|
| 189 |
*/
|
| 190 |
function filter_admin_format_form_submit($form, &$form_state) {
|
| 191 |
$format = (object) $form_state['values'];
|
| 192 |
$format->format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL;
|
| 193 |
$status = filter_format_save($format);
|
| 194 |
|
| 195 |
if ($permission = filter_permission_name($format)) {
|
| 196 |
foreach ($format->roles as $rid => $enabled) {
|
| 197 |
user_role_change_permissions($rid, array($permission => $enabled));
|
| 198 |
}
|
| 199 |
}
|
| 200 |
|
| 201 |
switch ($status) {
|
| 202 |
case SAVED_NEW:
|
| 203 |
drupal_set_message(t('Added text format %format.', array('%format' => $format->name)));
|
| 204 |
break;
|
| 205 |
|
| 206 |
case SAVED_UPDATED:
|
| 207 |
drupal_set_message(t('The text format %format has been updated.', array('%format' => $format->name)));
|
| 208 |
break;
|
| 209 |
}
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Menu callback; confirm deletion of a format.
|
| 214 |
*
|
| 215 |
* @ingroup forms
|
| 216 |
* @see filter_admin_delete_submit()
|
| 217 |
*/
|
| 218 |
function filter_admin_delete($form, &$form_state, $format) {
|
| 219 |
$form['#format'] = $format;
|
| 220 |
|
| 221 |
return confirm_form($form,
|
| 222 |
t('Are you sure you want to delete the text format %format?', array('%format' => $format->name)),
|
| 223 |
'admin/config/content/formats',
|
| 224 |
t('If you have any content left in this text format, it will be switched to the %fallback text format. This action cannot be undone.', array('%fallback' => filter_fallback_format_title())),
|
| 225 |
t('Delete'),
|
| 226 |
t('Cancel')
|
| 227 |
);
|
| 228 |
}
|
| 229 |
|
| 230 |
/**
|
| 231 |
* Process filter delete form submission.
|
| 232 |
*/
|
| 233 |
function filter_admin_delete_submit($form, &$form_state) {
|
| 234 |
$format = $form['#format'];
|
| 235 |
filter_format_delete($format);
|
| 236 |
drupal_set_message(t('Deleted text format %format.', array('%format' => $format->name)));
|
| 237 |
|
| 238 |
$form_state['redirect'] = 'admin/config/content/formats';
|
| 239 |
}
|
| 240 |
|
| 241 |
/**
|
| 242 |
* Menu callback; display settings defined by a format's filters.
|
| 243 |
*/
|
| 244 |
function filter_admin_configure_page($format) {
|
| 245 |
drupal_set_title(t("Configure %format", array('%format' => $format->name)), PASS_THROUGH);
|
| 246 |
return drupal_get_form('filter_admin_configure', $format);
|
| 247 |
}
|
| 248 |
|
| 249 |
/**
|
| 250 |
* Build a form to change the settings for filters in a text format.
|
| 251 |
*
|
| 252 |
* The form is built by merging the results of 'settings callback' for each
|
| 253 |
* enabled filter in the given format.
|
| 254 |
*
|
| 255 |
* @ingroup forms
|
| 256 |
*/
|
| 257 |
function filter_admin_configure($form, &$form_state, $format) {
|
| 258 |
$filters = filter_list_format($format->format);
|
| 259 |
$filter_info = filter_get_filters();
|
| 260 |
|
| 261 |
$form['#format'] = $format;
|
| 262 |
foreach ($filters as $name => $filter) {
|
| 263 |
if (isset($filter_info[$name]['settings callback']) && function_exists($filter_info[$name]['settings callback'])) {
|
| 264 |
// Pass along stored filter settings and default settings, but also the
|
| 265 |
// format object and all filters to allow for complex implementations.
|
| 266 |
$defaults = (isset($filter_info[$name]['default settings']) ? $filter_info[$name]['default settings'] : array());
|
| 267 |
$settings_form = $filter_info[$name]['settings callback']($form, $form_state, $filters[$name], $defaults, $format, $filters);
|
| 268 |
if (isset($settings_form) && is_array($settings_form)) {
|
| 269 |
$form['settings'][$name] = array(
|
| 270 |
'#type' => 'fieldset',
|
| 271 |
'#title' => check_plain($filter->title),
|
| 272 |
);
|
| 273 |
$form['settings'][$name] += $settings_form;
|
| 274 |
}
|
| 275 |
}
|
| 276 |
}
|
| 277 |
|
| 278 |
if (empty($form['settings'])) {
|
| 279 |
$form['error'] = array('#markup' => t('No settings are available.'));
|
| 280 |
return $form;
|
| 281 |
}
|
| 282 |
$form['settings']['#tree'] = TRUE;
|
| 283 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
|
| 284 |
|
| 285 |
return $form;
|
| 286 |
}
|
| 287 |
|
| 288 |
/**
|
| 289 |
* Form submit handler for text format filter configuration form.
|
| 290 |
*
|
| 291 |
* @see filter_admin_configure()
|
| 292 |
*/
|
| 293 |
function filter_admin_configure_submit($form, &$form_state) {
|
| 294 |
$format = $form['#format'];
|
| 295 |
|
| 296 |
foreach ($form_state['values']['settings'] as $name => $settings) {
|
| 297 |
db_update('filter')
|
| 298 |
->fields(array(
|
| 299 |
'settings' => serialize($settings),
|
| 300 |
))
|
| 301 |
->condition('format', $format->format)
|
| 302 |
->condition('name', $name)
|
| 303 |
->execute();
|
| 304 |
}
|
| 305 |
|
| 306 |
// Clear the filter's cache when configuration settings are saved.
|
| 307 |
cache_clear_all($format->format . ':', 'cache_filter', TRUE);
|
| 308 |
|
| 309 |
drupal_set_message(t('The configuration options have been saved.'));
|
| 310 |
}
|
| 311 |
|
| 312 |
/**
|
| 313 |
* Menu callback; display form for ordering filters for a format.
|
| 314 |
*/
|
| 315 |
function filter_admin_order_page($format) {
|
| 316 |
drupal_set_title(t("Rearrange %format", array('%format' => $format->name)), PASS_THROUGH);
|
| 317 |
return drupal_get_form('filter_admin_order', $format);
|
| 318 |
}
|
| 319 |
|
| 320 |
/**
|
| 321 |
* Build the form for ordering filters for a format.
|
| 322 |
*
|
| 323 |
* @ingroup forms
|
| 324 |
* @see theme_filter_admin_order()
|
| 325 |
* @see filter_admin_order_submit()
|
| 326 |
*/
|
| 327 |
function filter_admin_order($form, &$form_state, $format = NULL) {
|
| 328 |
// Get list (with forced refresh).
|
| 329 |
$filters = filter_list_format($format->format);
|
| 330 |
|
| 331 |
$form['weights'] = array('#tree' => TRUE);
|
| 332 |
foreach ($filters as $id => $filter) {
|
| 333 |
$form['names'][$id] = array('#markup' => $filter->title);
|
| 334 |
$form['weights'][$id] = array('#type' => 'weight', '#default_value' => $filter->weight);
|
| 335 |
}
|
| 336 |
$form['format'] = array('#type' => 'hidden', '#value' => $format->format);
|
| 337 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
|
| 338 |
|
| 339 |
return $form;
|
| 340 |
}
|
| 341 |
|
| 342 |
/**
|
| 343 |
* Theme filter order configuration form.
|
| 344 |
*
|
| 345 |
* @ingroup themeable
|
| 346 |
*/
|
| 347 |
function theme_filter_admin_order($variables) {
|
| 348 |
$form = $variables['form'];
|
| 349 |
|
| 350 |
$header = array(t('Name'), t('Weight'));
|
| 351 |
$rows = array();
|
| 352 |
foreach (element_children($form['names']) as $id) {
|
| 353 |
// Don't take form control structures.
|
| 354 |
if (is_array($form['names'][$id])) {
|
| 355 |
$form['weights'][$id]['#attributes']['class'] = array('filter-order-weight');
|
| 356 |
$rows[] = array(
|
| 357 |
'data' => array(drupal_render($form['names'][$id]), drupal_render($form['weights'][$id])),
|
| 358 |
'class' => array('draggable'),
|
| 359 |
);
|
| 360 |
}
|
| 361 |
}
|
| 362 |
|
| 363 |
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'filter-order')));
|
| 364 |
$output .= drupal_render_children($form);
|
| 365 |
|
| 366 |
drupal_add_tabledrag('filter-order', 'order', 'sibling', 'filter-order-weight', NULL, NULL, FALSE);
|
| 367 |
|
| 368 |
return $output;
|
| 369 |
}
|
| 370 |
|
| 371 |
/**
|
| 372 |
* Process filter order configuration form submission.
|
| 373 |
*/
|
| 374 |
function filter_admin_order_submit($form, &$form_state) {
|
| 375 |
foreach ($form_state['values']['weights'] as $name => $weight) {
|
| 376 |
db_merge('filter')
|
| 377 |
->key(array(
|
| 378 |
'format' => $form_state['values']['format'],
|
| 379 |
'name' => $name,
|
| 380 |
))
|
| 381 |
->fields(array(
|
| 382 |
'weight' => $weight,
|
| 383 |
))
|
| 384 |
->execute();
|
| 385 |
}
|
| 386 |
drupal_set_message(t('The filter ordering has been saved.'));
|
| 387 |
|
| 388 |
cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE);
|
| 389 |
}
|