filter.admin.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the filter module.
  5. */
  6. /**
  7. * Menu callback; Displays a list of all text formats and allows them to be rearranged.
  8. *
  9. * @ingroup forms
  10. * @see filter_admin_overview_submit()
  11. */
  12. function filter_admin_overview($form) {
  13. // Overview of all formats.
  14. $formats = filter_formats();
  15. $fallback_format = filter_fallback_format();
  16. $form['#tree'] = TRUE;
  17. foreach ($formats as $id => $format) {
  18. // Check whether this is the fallback text format. This format is available
  19. // to all roles and cannot be disabled via the admin interface.
  20. $form['formats'][$id]['#is_fallback'] = ($id == $fallback_format);
  21. if ($form['formats'][$id]['#is_fallback']) {
  22. $form['formats'][$id]['name'] = array('#markup' => drupal_placeholder($format->name));
  23. $roles_markup = drupal_placeholder(t('All roles may use this format'));
  24. }
  25. else {
  26. $form['formats'][$id]['name'] = array('#markup' => check_plain($format->name));
  27. $roles = array_map('check_plain', filter_get_roles_by_format($format));
  28. $roles_markup = $roles ? implode(', ', $roles) : t('No roles may use this format');
  29. }
  30. $form['formats'][$id]['roles'] = array('#markup' => $roles_markup);
  31. $form['formats'][$id]['configure'] = array('#type' => 'link', '#title' => t('configure'), '#href' => 'admin/config/content/formats/' . $id);
  32. $form['formats'][$id]['disable'] = array('#type' => 'link', '#title' => t('disable'), '#href' => 'admin/config/content/formats/' . $id . '/disable', '#access' => !$form['formats'][$id]['#is_fallback']);
  33. $form['formats'][$id]['weight'] = array(
  34. '#type' => 'weight',
  35. '#title' => t('Weight for @title', array('@title' => $format->name)),
  36. '#title_display' => 'invisible',
  37. '#default_value' => $format->weight,
  38. );
  39. }
  40. $form['actions'] = array('#type' => 'actions');
  41. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
  42. return $form;
  43. }
  44. function filter_admin_overview_submit($form, &$form_state) {
  45. foreach ($form_state['values']['formats'] as $id => $data) {
  46. if (is_array($data) && isset($data['weight'])) {
  47. // Only update if this is a form element with weight.
  48. db_update('filter_format')
  49. ->fields(array('weight' => $data['weight']))
  50. ->condition('format', $id)
  51. ->execute();
  52. }
  53. }
  54. filter_formats_reset();
  55. drupal_set_message(t('The text format ordering has been saved.'));
  56. }
  57. /**
  58. * Returns HTML for the text format administration overview form.
  59. *
  60. * @param $variables
  61. * An associative array containing:
  62. * - form: A render element representing the form.
  63. *
  64. * @ingroup themeable
  65. */
  66. function theme_filter_admin_overview($variables) {
  67. $form = $variables['form'];
  68. $rows = array();
  69. foreach (element_children($form['formats']) as $id) {
  70. $form['formats'][$id]['weight']['#attributes']['class'] = array('text-format-order-weight');
  71. $rows[] = array(
  72. 'data' => array(
  73. drupal_render($form['formats'][$id]['name']),
  74. drupal_render($form['formats'][$id]['roles']),
  75. drupal_render($form['formats'][$id]['weight']),
  76. drupal_render($form['formats'][$id]['configure']),
  77. drupal_render($form['formats'][$id]['disable']),
  78. ),
  79. 'class' => array('draggable'),
  80. );
  81. }
  82. $header = array(t('Name'), t('Roles'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
  83. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'text-format-order')));
  84. $output .= drupal_render_children($form);
  85. drupal_add_tabledrag('text-format-order', 'order', 'sibling', 'text-format-order-weight');
  86. return $output;
  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(
  95. 'format' => NULL,
  96. 'name' => '',
  97. );
  98. }
  99. return drupal_get_form('filter_admin_format_form', $format);
  100. }
  101. /**
  102. * Generate a text format form.
  103. *
  104. * @ingroup forms
  105. * @see filter_admin_format_form_validate()
  106. * @see filter_admin_format_form_submit()
  107. */
  108. function filter_admin_format_form($form, &$form_state, $format) {
  109. $is_fallback = ($format->format == filter_fallback_format());
  110. $form['#format'] = $format;
  111. $form['#tree'] = TRUE;
  112. $form['#attached']['js'][] = drupal_get_path('module', 'filter') . '/filter.admin.js';
  113. $form['#attached']['css'][] = drupal_get_path('module', 'filter') . '/filter.css';
  114. $form['name'] = array(
  115. '#type' => 'textfield',
  116. '#title' => t('Name'),
  117. '#default_value' => $format->name,
  118. '#required' => TRUE,
  119. );
  120. $form['format'] = array(
  121. '#type' => 'machine_name',
  122. '#required' => TRUE,
  123. '#default_value' => $format->format,
  124. '#maxlength' => 255,
  125. '#machine_name' => array(
  126. 'exists' => 'filter_format_exists',
  127. ),
  128. '#disabled' => !empty($format->format),
  129. );
  130. // Add user role access selection.
  131. $form['roles'] = array(
  132. '#type' => 'checkboxes',
  133. '#title' => t('Roles'),
  134. '#options' => array_map('check_plain', user_roles()),
  135. '#disabled' => $is_fallback,
  136. );
  137. if ($is_fallback) {
  138. $form['roles']['#description'] = t('All roles for this text format must be enabled and cannot be changed.');
  139. }
  140. if (!empty($format->format)) {
  141. // If editing an existing text format, pre-select its current permissions.
  142. $form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($format));
  143. }
  144. elseif ($admin_role = variable_get('user_admin_role', 0)) {
  145. // If adding a new text format and the site has an administrative role,
  146. // pre-select that role so as to grant administrators access to the new
  147. // text format permission by default.
  148. $form['roles']['#default_value'] = array($admin_role);
  149. }
  150. // Retrieve available filters and load all configured filters for existing
  151. // text formats.
  152. $filter_info = filter_get_filters();
  153. $filters = !empty($format->format) ? filter_list_format($format->format) : array();
  154. // Prepare filters for form sections.
  155. foreach ($filter_info as $name => $filter) {
  156. // Create an empty filter object for new/unconfigured filters.
  157. if (!isset($filters[$name])) {
  158. $filters[$name] = new stdClass();
  159. $filters[$name]->format = $format->format;
  160. $filters[$name]->module = $filter['module'];
  161. $filters[$name]->name = $name;
  162. $filters[$name]->status = 0;
  163. $filters[$name]->weight = $filter['weight'];
  164. $filters[$name]->settings = array();
  165. }
  166. }
  167. $form['#filters'] = $filters;
  168. // Filter status.
  169. $form['filters']['status'] = array(
  170. '#type' => 'item',
  171. '#title' => t('Enabled filters'),
  172. '#prefix' => '<div id="filters-status-wrapper">',
  173. '#suffix' => '</div>',
  174. );
  175. foreach ($filter_info as $name => $filter) {
  176. $form['filters']['status'][$name] = array(
  177. '#type' => 'checkbox',
  178. '#title' => $filter['title'],
  179. '#default_value' => $filters[$name]->status,
  180. '#parents' => array('filters', $name, 'status'),
  181. '#description' => $filter['description'],
  182. '#weight' => $filter['weight'],
  183. );
  184. }
  185. // Filter order (tabledrag).
  186. $form['filters']['order'] = array(
  187. '#type' => 'item',
  188. '#title' => t('Filter processing order'),
  189. '#theme' => 'filter_admin_format_filter_order',
  190. );
  191. foreach ($filter_info as $name => $filter) {
  192. $form['filters']['order'][$name]['filter'] = array(
  193. '#markup' => $filter['title'],
  194. );
  195. $form['filters']['order'][$name]['weight'] = array(
  196. '#type' => 'weight',
  197. '#title' => t('Weight for @title', array('@title' => $filter['title'])),
  198. '#title_display' => 'invisible',
  199. '#delta' => 50,
  200. '#default_value' => $filters[$name]->weight,
  201. '#parents' => array('filters', $name, 'weight'),
  202. );
  203. $form['filters']['order'][$name]['#weight'] = $filters[$name]->weight;
  204. }
  205. // Filter settings.
  206. $form['filter_settings_title'] = array(
  207. '#type' => 'item',
  208. '#title' => t('Filter settings'),
  209. );
  210. $form['filter_settings'] = array(
  211. '#type' => 'vertical_tabs',
  212. );
  213. foreach ($filter_info as $name => $filter) {
  214. if (isset($filter['settings callback']) && function_exists($filter['settings callback'])) {
  215. $function = $filter['settings callback'];
  216. // Pass along stored filter settings and default settings, but also the
  217. // format object and all filters to allow for complex implementations.
  218. $defaults = (isset($filter['default settings']) ? $filter['default settings'] : array());
  219. $settings_form = $function($form, $form_state, $filters[$name], $format, $defaults, $filters);
  220. if (!empty($settings_form)) {
  221. $form['filters']['settings'][$name] = array(
  222. '#type' => 'fieldset',
  223. '#title' => $filter['title'],
  224. '#parents' => array('filters', $name, 'settings'),
  225. '#weight' => $filter['weight'],
  226. '#group' => 'filter_settings',
  227. );
  228. $form['filters']['settings'][$name] += $settings_form;
  229. }
  230. }
  231. }
  232. $form['actions'] = array('#type' => 'actions');
  233. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  234. return $form;
  235. }
  236. /**
  237. * Returns HTML for a text format's filter order form.
  238. *
  239. * @param $variables
  240. * An associative array containing:
  241. * - element: A render element representing the form.
  242. *
  243. * @ingroup themeable
  244. */
  245. function theme_filter_admin_format_filter_order($variables) {
  246. $element = $variables['element'];
  247. // Filter order (tabledrag).
  248. $rows = array();
  249. foreach (element_children($element, TRUE) as $name) {
  250. $element[$name]['weight']['#attributes']['class'][] = 'filter-order-weight';
  251. $rows[] = array(
  252. 'data' => array(
  253. drupal_render($element[$name]['filter']),
  254. drupal_render($element[$name]['weight']),
  255. ),
  256. 'class' => array('draggable'),
  257. );
  258. }
  259. $output = drupal_render_children($element);
  260. $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'filter-order')));
  261. drupal_add_tabledrag('filter-order', 'order', 'sibling', 'filter-order-weight', NULL, NULL, TRUE);
  262. return $output;
  263. }
  264. /**
  265. * Validate text format form submissions.
  266. */
  267. function filter_admin_format_form_validate($form, &$form_state) {
  268. $format_format = trim($form_state['values']['format']);
  269. $format_name = trim($form_state['values']['name']);
  270. // Ensure that the values to be saved later are exactly the ones validated.
  271. form_set_value($form['format'], $format_format, $form_state);
  272. form_set_value($form['name'], $format_name, $form_state);
  273. $result = db_query("SELECT format FROM {filter_format} WHERE name = :name AND format <> :format", array(':name' => $format_name, ':format' => $format_format))->fetchField();
  274. if ($result) {
  275. form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name)));
  276. }
  277. }
  278. /**
  279. * Process text format form submissions.
  280. */
  281. function filter_admin_format_form_submit($form, &$form_state) {
  282. // Remove unnecessary values.
  283. form_state_values_clean($form_state);
  284. // Add the submitted form values to the text format, and save it.
  285. $format = $form['#format'];
  286. foreach ($form_state['values'] as $key => $value) {
  287. $format->$key = $value;
  288. }
  289. $status = filter_format_save($format);
  290. // Save user permissions.
  291. if ($permission = filter_permission_name($format)) {
  292. foreach ($format->roles as $rid => $enabled) {
  293. user_role_change_permissions($rid, array($permission => $enabled));
  294. }
  295. }
  296. switch ($status) {
  297. case SAVED_NEW:
  298. drupal_set_message(t('Added text format %format.', array('%format' => $format->name)));
  299. break;
  300. case SAVED_UPDATED:
  301. drupal_set_message(t('The text format %format has been updated.', array('%format' => $format->name)));
  302. break;
  303. }
  304. }
  305. /**
  306. * Menu callback; confirm deletion of a format.
  307. *
  308. * @ingroup forms
  309. * @see filter_admin_disable_submit()
  310. */
  311. function filter_admin_disable($form, &$form_state, $format) {
  312. $form['#format'] = $format;
  313. return confirm_form($form,
  314. t('Are you sure you want to disable the text format %format?', array('%format' => $format->name)),
  315. 'admin/config/content/formats',
  316. 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.'),
  317. t('Disable')
  318. );
  319. }
  320. /**
  321. * Process filter disable form submission.
  322. */
  323. function filter_admin_disable_submit($form, &$form_state) {
  324. $format = $form['#format'];
  325. filter_format_disable($format);
  326. drupal_set_message(t('Disabled text format %format.', array('%format' => $format->name)));
  327. $form_state['redirect'] = 'admin/config/content/formats';
  328. }