[b354002] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * @file |
---|
| 5 | * Admin page callbacks for the filter module. |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | /** |
---|
| 9 | * Menu callback; Displays a list of all input formats and which |
---|
| 10 | * one is the default. |
---|
| 11 | * |
---|
| 12 | * @ingroup forms |
---|
| 13 | * @see filter_admin_overview_submit() |
---|
| 14 | */ |
---|
| 15 | function filter_admin_overview() { |
---|
| 16 | |
---|
| 17 | // Overview of all formats. |
---|
| 18 | $formats = filter_formats(); |
---|
| 19 | $error = FALSE; |
---|
| 20 | |
---|
| 21 | foreach ($formats as $id => $format) { |
---|
| 22 | $roles = array(); |
---|
| 23 | foreach (user_roles() as $rid => $name) { |
---|
| 24 | // Prepare a roles array with roles that may access the filter. |
---|
| 25 | if (strstr($format->roles, ",$rid,")) { |
---|
| 26 | $roles[] = $name; |
---|
| 27 | } |
---|
| 28 | } |
---|
| 29 | $default = ($id == variable_get('filter_default_format', 1)); |
---|
| 30 | $options[$id] = ''; |
---|
| 31 | $form[$format->name]['id'] = array('#value' => $id); |
---|
| 32 | $form[$format->name]['roles'] = array('#value' => $default ? t('All roles may use default format') : ($roles ? implode(', ', $roles) : t('No roles may use this format'))); |
---|
| 33 | $form[$format->name]['configure'] = array('#value' => l(t('configure'), 'admin/settings/filters/'. $id)); |
---|
| 34 | $form[$format->name]['delete'] = array('#value' => $default ? '' : l(t('delete'), 'admin/settings/filters/delete/'. $id)); |
---|
| 35 | } |
---|
| 36 | $form['default'] = array('#type' => 'radios', '#options' => $options, '#default_value' => variable_get('filter_default_format', 1)); |
---|
| 37 | $form['submit'] = array('#type' => 'submit', '#value' => t('Set default format')); |
---|
| 38 | return $form; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | function filter_admin_overview_submit($form, &$form_state) { |
---|
| 42 | // Process form submission to set the default format. |
---|
| 43 | if (is_numeric($form_state['values']['default'])) { |
---|
| 44 | drupal_set_message(t('Default format updated.')); |
---|
| 45 | variable_set('filter_default_format', $form_state['values']['default']); |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Theme the admin overview form. |
---|
| 51 | * |
---|
| 52 | * @ingroup themeable |
---|
| 53 | */ |
---|
| 54 | function theme_filter_admin_overview($form) { |
---|
| 55 | $rows = array(); |
---|
| 56 | foreach ($form as $name => $element) { |
---|
| 57 | if (isset($element['roles']) && is_array($element['roles'])) { |
---|
| 58 | $rows[] = array( |
---|
| 59 | drupal_render($form['default'][$element['id']['#value']]), |
---|
| 60 | check_plain($name), |
---|
| 61 | drupal_render($element['roles']), |
---|
| 62 | drupal_render($element['configure']), |
---|
| 63 | drupal_render($element['delete']) |
---|
| 64 | ); |
---|
| 65 | unset($form[$name]); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | $header = array(t('Default'), t('Name'), t('Roles'), array('data' => t('Operations'), 'colspan' => 2)); |
---|
| 69 | $output = theme('table', $header, $rows); |
---|
| 70 | $output .= drupal_render($form); |
---|
| 71 | |
---|
| 72 | return $output; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Menu callback; Display a filter format form. |
---|
| 77 | */ |
---|
| 78 | function filter_admin_format_page($format = NULL) { |
---|
| 79 | if (!isset($format->name)) { |
---|
| 80 | drupal_set_title(t("Add input format")); |
---|
| 81 | $format = (object)array('name' => '', 'roles' => '', 'format' => ''); |
---|
| 82 | } |
---|
| 83 | return drupal_get_form('filter_admin_format_form', $format); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * Generate a filter format form. |
---|
| 88 | * |
---|
| 89 | * @ingroup forms |
---|
| 90 | * @see filter_admin_format_form_validate() |
---|
| 91 | * @see filter_admin_format_form_submit() |
---|
| 92 | */ |
---|
| 93 | function filter_admin_format_form(&$form_state, $format) { |
---|
| 94 | $default = ($format->format == variable_get('filter_default_format', 1)); |
---|
| 95 | if ($default) { |
---|
| 96 | $help = t('All roles for the default format must be enabled and cannot be changed.'); |
---|
| 97 | $form['default_format'] = array('#type' => 'hidden', '#value' => 1); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | $form['name'] = array('#type' => 'textfield', |
---|
| 101 | '#title' => t('Name'), |
---|
| 102 | '#default_value' => $format->name, |
---|
| 103 | '#description' => t('Specify a unique name for this filter format.'), |
---|
| 104 | '#required' => TRUE, |
---|
| 105 | ); |
---|
| 106 | |
---|
| 107 | // Add a row of checkboxes for form group. |
---|
| 108 | $form['roles'] = array('#type' => 'fieldset', |
---|
| 109 | '#title' => t('Roles'), |
---|
| 110 | '#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.'), |
---|
| 111 | '#tree' => TRUE, |
---|
| 112 | ); |
---|
| 113 | |
---|
| 114 | foreach (user_roles() as $rid => $name) { |
---|
| 115 | $checked = strstr($format->roles, ",$rid,"); |
---|
| 116 | $form['roles'][$rid] = array('#type' => 'checkbox', |
---|
| 117 | '#title' => $name, |
---|
| 118 | '#default_value' => ($default || $checked), |
---|
| 119 | ); |
---|
| 120 | if ($default) { |
---|
| 121 | $form['roles'][$rid]['#disabled'] = TRUE; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | // Table with filters |
---|
| 125 | $all = filter_list_all(); |
---|
| 126 | $enabled = filter_list_format($format->format); |
---|
| 127 | |
---|
| 128 | $form['filters'] = array('#type' => 'fieldset', |
---|
| 129 | '#title' => t('Filters'), |
---|
| 130 | '#description' => t('Choose the filters that will be used in this filter format.'), |
---|
| 131 | '#tree' => TRUE, |
---|
| 132 | ); |
---|
| 133 | foreach ($all as $id => $filter) { |
---|
| 134 | $form['filters'][$id] = array('#type' => 'checkbox', |
---|
| 135 | '#title' => $filter->name, |
---|
| 136 | '#default_value' => isset($enabled[$id]), |
---|
| 137 | '#description' => module_invoke($filter->module, 'filter', 'description', $filter->delta), |
---|
| 138 | ); |
---|
| 139 | } |
---|
| 140 | if (!empty($format->format)) { |
---|
| 141 | $form['format'] = array('#type' => 'hidden', '#value' => $format->format); |
---|
| 142 | |
---|
| 143 | // Composition tips (guidelines) |
---|
| 144 | $tips = _filter_tips($format->format, FALSE); |
---|
| 145 | $extra = '<p>'. l(t('More information about formatting options'), 'filter/tips') .'</p>'; |
---|
| 146 | $tiplist = theme('filter_tips', $tips, FALSE, $extra); |
---|
| 147 | if (!$tiplist) { |
---|
| 148 | $tiplist = '<p>'. t('No guidelines available.') .'</p>'; |
---|
| 149 | } |
---|
| 150 | $group = '<p>'. t('These are the guidelines that users will see for posting in this input format. They are automatically generated from the filter settings.') .'</p>'; |
---|
| 151 | $group .= $tiplist; |
---|
| 152 | $form['tips'] = array('#value' => '<h2>'. t('Formatting guidelines') .'</h2>'. $group); |
---|
| 153 | } |
---|
| 154 | $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); |
---|
| 155 | |
---|
| 156 | return $form; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * Validate filter format form submissions. |
---|
| 161 | */ |
---|
| 162 | function filter_admin_format_form_validate($form, &$form_state) { |
---|
| 163 | if (!isset($form_state['values']['format'])) { |
---|
| 164 | $name = trim($form_state['values']['name']); |
---|
| 165 | $result = db_fetch_object(db_query("SELECT format FROM {filter_formats} WHERE name='%s'", $name)); |
---|
| 166 | if ($result) { |
---|
| 167 | form_set_error('name', t('Filter format names need to be unique. A format named %name already exists.', array('%name' => $name))); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * Process filter format form submissions. |
---|
| 174 | */ |
---|
| 175 | function filter_admin_format_form_submit($form, &$form_state) { |
---|
| 176 | $format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL; |
---|
| 177 | $current = filter_list_format($format); |
---|
| 178 | $name = trim($form_state['values']['name']); |
---|
| 179 | $cache = TRUE; |
---|
| 180 | |
---|
| 181 | // Add a new filter format. |
---|
| 182 | if (!$format) { |
---|
| 183 | $new = TRUE; |
---|
| 184 | db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $name); |
---|
| 185 | $format = db_result(db_query("SELECT MAX(format) AS format FROM {filter_formats}")); |
---|
| 186 | drupal_set_message(t('Added input format %format.', array('%format' => $name))); |
---|
| 187 | } |
---|
| 188 | else { |
---|
| 189 | drupal_set_message(t('The input format settings have been updated.')); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | db_query("DELETE FROM {filters} WHERE format = %d", $format); |
---|
| 193 | foreach ($form_state['values']['filters'] as $id => $checked) { |
---|
| 194 | if ($checked) { |
---|
| 195 | list($module, $delta) = explode('/', $id); |
---|
| 196 | // Add new filters to the bottom. |
---|
| 197 | $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10; |
---|
| 198 | db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format, $module, $delta, $weight); |
---|
| 199 | |
---|
| 200 | // Check if there are any 'no cache' filters. |
---|
| 201 | $cache &= !module_invoke($module, 'filter', 'no cache', $delta); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | // We store the roles as a string for ease of use. |
---|
| 206 | // We should always set all roles to TRUE when saving a default role. |
---|
| 207 | // We use leading and trailing comma's to allow easy substring matching. |
---|
| 208 | $roles = array(); |
---|
| 209 | if (isset($form_state['values']['roles'])) { |
---|
| 210 | foreach ($form_state['values']['roles'] as $id => $checked) { |
---|
| 211 | if ($checked) { |
---|
| 212 | $roles[] = $id; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | if (!empty($form_state['values']['default_format'])) { |
---|
| 217 | $roles = ','. implode(',', array_keys(user_roles())) .','; |
---|
| 218 | } |
---|
| 219 | else { |
---|
| 220 | $roles = ','. implode(',', $roles) .','; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | db_query("UPDATE {filter_formats} SET cache = %d, name='%s', roles = '%s' WHERE format = %d", $cache, $name, $roles, $format); |
---|
| 224 | |
---|
| 225 | cache_clear_all($format .':', 'cache_filter', TRUE); |
---|
| 226 | |
---|
| 227 | // If a new filter was added, return to the main list of filters. Otherwise, stay on edit filter page to show new changes. |
---|
| 228 | $return = 'admin/settings/filters'; |
---|
| 229 | if (!empty($new)) { |
---|
| 230 | $return .= '/'. $format; |
---|
| 231 | } |
---|
| 232 | $form_state['redirect'] = $return; |
---|
| 233 | return; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * Menu callback; confirm deletion of a format. |
---|
| 238 | * |
---|
| 239 | * @ingroup forms |
---|
| 240 | * @see filter_admin_delete_submit() |
---|
| 241 | */ |
---|
| 242 | function filter_admin_delete() { |
---|
| 243 | $format = arg(4); |
---|
| 244 | $format = db_fetch_object(db_query('SELECT * FROM {filter_formats} WHERE format = %d', $format)); |
---|
| 245 | |
---|
| 246 | if ($format) { |
---|
| 247 | if ($format->format != variable_get('filter_default_format', 1)) { |
---|
| 248 | $form['format'] = array('#type' => 'hidden', '#value' => $format->format); |
---|
| 249 | $form['name'] = array('#type' => 'hidden', '#value' => $format->name); |
---|
| 250 | |
---|
| 251 | return confirm_form($form, t('Are you sure you want to delete the input format %format?', array('%format' => $format->name)), 'admin/settings/filters', t('If you have any content left in this input format, it will be switched to the default input format. This action cannot be undone.'), t('Delete'), t('Cancel')); |
---|
| 252 | } |
---|
| 253 | else { |
---|
| 254 | drupal_set_message(t('The default format cannot be deleted.')); |
---|
| 255 | drupal_goto('admin/settings/filters'); |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | else { |
---|
| 259 | drupal_not_found(); |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /** |
---|
| 264 | * Process filter delete form submission. |
---|
| 265 | */ |
---|
| 266 | function filter_admin_delete_submit($form, &$form_state) { |
---|
| 267 | db_query("DELETE FROM {filter_formats} WHERE format = %d", $form_state['values']['format']); |
---|
| 268 | db_query("DELETE FROM {filters} WHERE format = %d", $form_state['values']['format']); |
---|
| 269 | |
---|
| 270 | $default = variable_get('filter_default_format', 1); |
---|
| 271 | // Replace existing instances of the deleted format with the default format. |
---|
| 272 | db_query("UPDATE {node_revisions} SET format = %d WHERE format = %d", $default, $form_state['values']['format']); |
---|
| 273 | db_query("UPDATE {comments} SET format = %d WHERE format = %d", $default, $form_state['values']['format']); |
---|
| 274 | db_query("UPDATE {boxes} SET format = %d WHERE format = %d", $default, $form_state['values']['format']); |
---|
| 275 | |
---|
| 276 | cache_clear_all($form_state['values']['format'] .':', 'cache_filter', TRUE); |
---|
| 277 | drupal_set_message(t('Deleted input format %format.', array('%format' => $form_state['values']['name']))); |
---|
| 278 | |
---|
| 279 | $form_state['redirect'] = 'admin/settings/filters'; |
---|
| 280 | return; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | /** |
---|
| 285 | * Menu callback; display settings defined by a format's filters. |
---|
| 286 | */ |
---|
| 287 | function filter_admin_configure_page($format) { |
---|
| 288 | drupal_set_title(t("Configure %format", array('%format' => $format->name))); |
---|
| 289 | return drupal_get_form('filter_admin_configure', $format); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | /** |
---|
| 293 | * Build a form to change the settings for a format's filters. |
---|
| 294 | * |
---|
| 295 | * @ingroup forms |
---|
| 296 | */ |
---|
| 297 | function filter_admin_configure(&$form_state, $format) { |
---|
| 298 | $list = filter_list_format($format->format); |
---|
| 299 | $form = array(); |
---|
| 300 | foreach ($list as $filter) { |
---|
| 301 | $form_module = module_invoke($filter->module, 'filter', 'settings', $filter->delta, $format->format); |
---|
| 302 | if (isset($form_module) && is_array($form_module)) { |
---|
| 303 | $form = array_merge($form, $form_module); |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | if (!empty($form)) { |
---|
| 308 | $form = system_settings_form($form); |
---|
| 309 | } |
---|
| 310 | else { |
---|
| 311 | $form['error'] = array('#value' => t('No settings are available.')); |
---|
| 312 | } |
---|
| 313 | $form['format'] = array('#type' => 'hidden', '#value' => $format->format); |
---|
| 314 | $form['#submit'][] = 'filter_admin_configure_submit'; |
---|
| 315 | return $form; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /** |
---|
| 319 | * Clear the filter's cache when configuration settings are saved. |
---|
| 320 | */ |
---|
| 321 | function filter_admin_configure_submit($form, &$form_state) { |
---|
| 322 | cache_clear_all($form_state['values']['format'] .':', 'cache_filter', TRUE); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | /** |
---|
| 326 | * Menu callback; display form for ordering filters for a format. |
---|
| 327 | */ |
---|
| 328 | function filter_admin_order_page($format) { |
---|
| 329 | drupal_set_title(t("Rearrange %format", array('%format' => $format->name))); |
---|
| 330 | return drupal_get_form('filter_admin_order', $format); |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | /** |
---|
| 334 | * Build the form for ordering filters for a format. |
---|
| 335 | * |
---|
| 336 | * @ingroup forms |
---|
| 337 | * @see theme_filter_admin_order() |
---|
| 338 | * @see filter_admin_order_submit() |
---|
| 339 | */ |
---|
| 340 | function filter_admin_order(&$form_state, $format = NULL) { |
---|
| 341 | // Get list (with forced refresh). |
---|
| 342 | $filters = filter_list_format($format->format); |
---|
| 343 | |
---|
| 344 | $form['weights'] = array('#tree' => TRUE); |
---|
| 345 | foreach ($filters as $id => $filter) { |
---|
| 346 | $form['names'][$id] = array('#value' => $filter->name); |
---|
| 347 | $form['weights'][$id] = array('#type' => 'weight', '#default_value' => $filter->weight); |
---|
| 348 | } |
---|
| 349 | $form['format'] = array('#type' => 'hidden', '#value' => $format->format); |
---|
| 350 | $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); |
---|
| 351 | |
---|
| 352 | return $form; |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | /** |
---|
| 356 | * Theme filter order configuration form. |
---|
| 357 | * |
---|
| 358 | * @ingroup themeable |
---|
| 359 | */ |
---|
| 360 | function theme_filter_admin_order($form) { |
---|
| 361 | $header = array(t('Name'), t('Weight')); |
---|
| 362 | $rows = array(); |
---|
| 363 | foreach (element_children($form['names']) as $id) { |
---|
| 364 | // Don't take form control structures. |
---|
| 365 | if (is_array($form['names'][$id])) { |
---|
| 366 | $form['weights'][$id]['#attributes']['class'] = 'filter-order-weight'; |
---|
| 367 | $rows[] = array( |
---|
| 368 | 'data' => array(drupal_render($form['names'][$id]), drupal_render($form['weights'][$id])), |
---|
| 369 | 'class' => 'draggable', |
---|
| 370 | ); |
---|
| 371 | } |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | $output = theme('table', $header, $rows, array('id' => 'filter-order')); |
---|
| 375 | $output .= drupal_render($form); |
---|
| 376 | |
---|
| 377 | drupal_add_tabledrag('filter-order', 'order', 'sibling', 'filter-order-weight', NULL, NULL, FALSE); |
---|
| 378 | |
---|
| 379 | return $output; |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | /** |
---|
| 383 | * Process filter order configuration form submission. |
---|
| 384 | */ |
---|
| 385 | function filter_admin_order_submit($form, &$form_state) { |
---|
| 386 | foreach ($form_state['values']['weights'] as $id => $weight) { |
---|
| 387 | list($module, $delta) = explode('/', $id); |
---|
| 388 | db_query("UPDATE {filters} SET weight = %d WHERE format = %d AND module = '%s' AND delta = %d", $weight, $form_state['values']['format'], $module, $delta); |
---|
| 389 | } |
---|
| 390 | drupal_set_message(t('The filter ordering has been saved.')); |
---|
| 391 | |
---|
| 392 | cache_clear_all($form_state['values']['format'] .':', 'cache_filter', TRUE); |
---|
| 393 | } |
---|