filter.admin.inc

Admin page callbacks for the filter module.

Archivo

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

Functions

Nombreorden descendente Descripción
filter_admin_configure Build a form to change the settings for a format's filters.
filter_admin_configure_page Menu callback; display settings defined by a format's filters.
filter_admin_configure_submit Clear the filter's cache when configuration settings are saved.
filter_admin_delete Menu callback; confirm deletion of a format.
filter_admin_delete_submit Process filter delete form submission.
filter_admin_format_form Generate a filter format form.
filter_admin_format_form_submit Process filter format form submissions.
filter_admin_format_form_validate Validate filter format form submissions.
filter_admin_format_page Menu callback; Display a filter format form.
filter_admin_order Build the form for ordering filters for a format.
filter_admin_order_page Menu callback; display form for ordering filters for a format.
filter_admin_order_submit Process filter order configuration form submission.
filter_admin_overview Menu callback; Displays a list of all input formats and which one is the default.
filter_admin_overview_submit
theme_filter_admin_order Theme filter order configuration form.
theme_filter_admin_overview Theme the admin overview form.