Issue #1479454 by Hugo Wetterberg, galooph, dawehner, andypost, marcingy, heyrocker...
[project/drupal.git] / core / modules / filter / filter.admin.inc
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 * @see filter_menu()
12 * @see filter_admin_overview_submit()
13 * @ingroup forms
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 $form['formats'] = array(
22 '#type' => 'table',
23 '#header' => array(t('Name'), t('Roles'), t('Weight'), t('Operations')),
24 '#tabledrag' => array(
25 array('order', 'sibling', 'text-format-order-weight'),
26 ),
27 );
28 foreach ($formats as $id => $format) {
29 $form['formats'][$id]['#attributes']['class'][] = 'draggable';
30 $form['formats'][$id]['#weight'] = $format->weight;
31
32 $links = array();
33 $links['configure'] = array(
34 'title' => t('configure'),
35 'href' => "admin/config/content/formats/$id",
36 );
37 // Check whether this is the fallback text format. This format is available
38 // to all roles and cannot be disabled via the admin interface.
39 $form['formats'][$id]['#is_fallback'] = ($id == $fallback_format);
40 if ($form['formats'][$id]['#is_fallback']) {
41 $form['formats'][$id]['name'] = array('#markup' => drupal_placeholder($format->name));
42 $roles_markup = drupal_placeholder(t('All roles may use this format'));
43 }
44 else {
45 $form['formats'][$id]['name'] = array('#markup' => check_plain($format->name));
46 $roles = array_map('check_plain', filter_get_roles_by_format($format));
47 $roles_markup = $roles ? implode(', ', $roles) : t('No roles may use this format');
48 $links['disable'] = array(
49 'title' => t('disable'),
50 'href' => "admin/config/content/formats/$id/disable",
51 );
52 }
53
54 $form['formats'][$id]['roles'] = array('#markup' => $roles_markup);
55
56 $form['formats'][$id]['weight'] = array(
57 '#type' => 'weight',
58 '#title' => t('Weight for @title', array('@title' => $format->name)),
59 '#title_display' => 'invisible',
60 '#default_value' => $format->weight,
61 '#attributes' => array('class' => array('text-format-order-weight')),
62 );
63
64 $form['formats'][$id]['operations'] = array(
65 '#type' => 'operations',
66 '#links' => $links,
67 );
68 }
69 $form['actions'] = array('#type' => 'actions');
70 $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save changes'));
71 return $form;
72 }
73
74 /**
75 * Form submission handler for filter_admin_overview().
76 */
77 function filter_admin_overview_submit($form, &$form_state) {
78 $filter_formats = filter_formats();
79 foreach ($form_state['values']['formats'] as $id => $data) {
80 // Only update if this is a form element with weight.
81 if (is_array($data) && isset($data['weight'])) {
82 $filter_formats[$id]->set('weight', $data['weight']);
83 $filter_formats[$id]->save();
84 }
85 }
86 filter_formats_reset();
87 drupal_set_message(t('The text format ordering has been saved.'));
88 }
89
90 /**
91 * Page callback: Displays the text format add/edit form.
92 *
93 * @param object|null $format
94 * (optional) An object representing a format, with the following properties:
95 * - format: A machine-readable name representing the ID of the text format
96 * to save. If this corresponds to an existing text format, that format
97 * will be updated; otherwise, a new format will be created.
98 * - name: The title of the text format.
99 * - cache: An integer indicating whether the text format is cacheable (1) or
100 * not (0). Defaults to 1.
101 * - status: (optional) An integer indicating whether the text format is
102 * enabled (1) or not (0). Defaults to 1.
103 * - weight: (optional) The weight of the text format, which controls its
104 * placement in text format lists. If omitted, the weight is set to 0.
105 * Defaults to NULL.
106 *
107 * @return
108 * A form array.
109 *
110 * @see filter_menu()
111 */
112 function filter_admin_format_page($format = NULL) {
113 if (!isset($format->name)) {
114 drupal_set_title(t('Add text format'));
115
116 $format = entity_create('filter_format', array());
117 }
118 return drupal_get_form('filter_admin_format_form', $format);
119 }
120
121 /**
122 * Form constructor for the text format add/edit form.
123 *
124 * @param $format
125 * A format object having the properties:
126 * - format: A machine-readable name representing the ID of the text format to
127 * save. If this corresponds to an existing text format, that format will be
128 * updated; otherwise, a new format will be created.
129 * - name: The title of the text format.
130 * - cache: (optional) An integer indicating whether the text format is
131 * cacheable (1) or not (0). Defaults to 1.
132 * - status: (optional) An integer indicating whether the text format is
133 * enabled (1) or not (0). Defaults to 1.
134 * - weight: (optional) The weight of the text format, which controls its
135 * placement in text format lists. If omitted, the weight is set to 0.
136 *
137 * @see filter_admin_format_form_validate()
138 * @see filter_admin_format_form_submit()
139 * @ingroup forms
140 */
141 function filter_admin_format_form($form, &$form_state, $format) {
142 $is_fallback = ($format->format == filter_fallback_format());
143
144 $form['#format'] = $format;
145 $form['#tree'] = TRUE;
146 $form['#attached']['library'][] = array('filter', 'drupal.filter.admin');
147
148 $form['name'] = array(
149 '#type' => 'textfield',
150 '#title' => t('Name'),
151 '#default_value' => $format->name,
152 '#required' => TRUE,
153 '#weight' => -30,
154 );
155 $form['format'] = array(
156 '#type' => 'machine_name',
157 '#required' => TRUE,
158 '#default_value' => $format->format,
159 '#maxlength' => 255,
160 '#machine_name' => array(
161 'exists' => 'filter_format_exists',
162 'source' => array('name'),
163 ),
164 '#disabled' => !empty($format->format),
165 '#weight' => -20,
166 );
167
168 // Add user role access selection.
169 $form['roles'] = array(
170 '#type' => 'checkboxes',
171 '#title' => t('Roles'),
172 '#options' => array_map('check_plain', user_role_names()),
173 '#disabled' => $is_fallback,
174 '#weight' => -10,
175 );
176 if ($is_fallback) {
177 $form['roles']['#description'] = t('All roles for this text format must be enabled and cannot be changed.');
178 }
179 if (!empty($format->format)) {
180 // If editing an existing text format, pre-select its current permissions.
181 $form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($format));
182 }
183 elseif ($admin_role = config('user.settings')->get('admin_role')) {
184 // If adding a new text format and the site has an administrative role,
185 // pre-select that role so as to grant administrators access to the new
186 // text format permission by default.
187 $form['roles']['#default_value'] = array($admin_role);
188 }
189
190 // Retrieve available filters and load all configured filters for existing
191 // text formats.
192 $filter_info = filter_get_filters();
193 $filters = !empty($format->format) ? filter_list_format($format->format) : array();
194
195 // Prepare filters for form sections.
196 foreach ($filter_info as $name => $filter) {
197 // Create an empty filter object for new/unconfigured filters.
198 if (!isset($filters[$name])) {
199 $filters[$name] = new stdClass();
200 $filters[$name]->format = $format->format;
201 $filters[$name]->module = $filter['module'];
202 $filters[$name]->name = $name;
203 $filters[$name]->status = 0;
204 $filters[$name]->weight = $filter['weight'];
205 $filters[$name]->settings = array();
206 }
207 }
208 $form['#filters'] = $filters;
209
210 // Filter status.
211 $form['filters']['status'] = array(
212 '#type' => 'item',
213 '#title' => t('Enabled filters'),
214 '#prefix' => '<div id="filters-status-wrapper">',
215 '#suffix' => '</div>',
216 // This item is used as a pure wrapping container with heading. Ignore its
217 // value, since 'filters' should only contain filter definitions.
218 // @see http://drupal.org/node/1829202
219 '#input' => FALSE,
220 );
221 foreach ($filter_info as $name => $filter) {
222 $form['filters']['status'][$name] = array(
223 '#type' => 'checkbox',
224 '#title' => $filter['title'],
225 '#default_value' => $filters[$name]->status,
226 '#parents' => array('filters', $name, 'status'),
227 '#description' => $filter['description'],
228 '#weight' => $filter['weight'],
229 );
230 }
231
232 // Filter order (tabledrag).
233 $form['filters']['order'] = array(
234 '#type' => 'item',
235 '#title' => t('Filter processing order'),
236 '#theme' => 'filter_admin_format_filter_order',
237 // This item is used as a pure wrapping container with heading. Ignore its
238 // value, since 'filters' should only contain filter definitions.
239 // @see http://drupal.org/node/1829202
240 '#input' => FALSE,
241 );
242 foreach ($filter_info as $name => $filter) {
243 $form['filters']['order'][$name]['filter'] = array(
244 '#markup' => $filter['title'],
245 );
246 $form['filters']['order'][$name]['weight'] = array(
247 '#type' => 'weight',
248 '#title' => t('Weight for @title', array('@title' => $filter['title'])),
249 '#title_display' => 'invisible',
250 '#delta' => 50,
251 '#default_value' => $filters[$name]->weight,
252 '#parents' => array('filters', $name, 'weight'),
253 );
254 $form['filters']['order'][$name]['#weight'] = $filters[$name]->weight;
255 }
256
257 // Filter settings.
258 $form['filter_settings'] = array(
259 '#type' => 'vertical_tabs',
260 '#title' => t('Filter settings'),
261 );
262
263 foreach ($filter_info as $name => $filter) {
264 if (isset($filter['settings callback'])) {
265 $function = $filter['settings callback'];
266 // Pass along stored filter settings and default settings, but also the
267 // format object and all filters to allow for complex implementations.
268 $settings_form = $function($form, $form_state, $filters[$name], $format, $filter['default settings'], $filters);
269 if (!empty($settings_form)) {
270 $form['filters']['settings'][$name] = array(
271 '#type' => 'details',
272 '#title' => $filter['title'],
273 '#parents' => array('filters', $name, 'settings'),
274 '#weight' => $filter['weight'],
275 '#group' => 'filter_settings',
276 );
277 $form['filters']['settings'][$name] += $settings_form;
278 }
279 }
280 }
281
282 $form['actions'] = array('#type' => 'actions');
283 $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
284
285 return $form;
286 }
287
288 /**
289 * Returns HTML for a text format's filter order form.
290 *
291 * @param array $variables
292 * An associative array containing:
293 * - element: A render element representing the form.
294 *
295 * @ingroup themeable
296 */
297 function theme_filter_admin_format_filter_order($variables) {
298 $element = $variables['element'];
299
300 // Filter order (tabledrag).
301 $rows = array();
302 foreach (element_children($element, TRUE) as $name) {
303 $element[$name]['weight']['#attributes']['class'][] = 'filter-order-weight';
304 $rows[] = array(
305 'data' => array(
306 drupal_render($element[$name]['filter']),
307 drupal_render($element[$name]['weight']),
308 ),
309 'class' => array('draggable'),
310 );
311 }
312 $output = drupal_render_children($element);
313 $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'filter-order')));
314 drupal_add_tabledrag('filter-order', 'order', 'sibling', 'filter-order-weight', NULL, NULL, TRUE);
315
316 return $output;
317 }
318
319 /**
320 * Form validation handler for filter_admin_format_form().
321 *
322 * @see filter_admin_format_form_submit()
323 */
324 function filter_admin_format_form_validate($form, &$form_state) {
325 $format_format = trim($form_state['values']['format']);
326 $format_name = trim($form_state['values']['name']);
327
328 // Ensure that the values to be saved later are exactly the ones validated.
329 form_set_value($form['format'], $format_format, $form_state);
330 form_set_value($form['name'], $format_name, $form_state);
331
332 $filter_formats = entity_load_multiple('filter_format');
333 foreach ($filter_formats as $format) {
334 if ($format->name == $format_name && $format->format != $format_format) {
335 form_set_error('name', t('Text format names must be unique. A format named %name already exists.', array('%name' => $format_name)));
336 break;
337 }
338 }
339 }
340
341 /**
342 * Form submission handler for filter_admin_format_form().
343 *
344 * @see filter_admin_format_form_validate()
345 */
346 function filter_admin_format_form_submit($form, &$form_state) {
347 // Remove unnecessary values.
348 form_state_values_clean($form_state);
349
350 // Add the submitted form values to the text format, and save it.
351 $format = $form['#format'];
352 foreach ($form_state['values'] as $key => $value) {
353 $format->set($key, $value);
354 }
355 $status = $format->save();
356
357 // Save user permissions.
358 if ($permission = filter_permission_name($format)) {
359 foreach ($form_state['values']['roles'] as $rid => $enabled) {
360 user_role_change_permissions($rid, array($permission => $enabled));
361 }
362 }
363
364 switch ($status) {
365 case SAVED_NEW:
366 drupal_set_message(t('Added text format %format.', array('%format' => $format->name)));
367 break;
368
369 case SAVED_UPDATED:
370 drupal_set_message(t('The text format %format has been updated.', array('%format' => $format->name)));
371 break;
372 }
373 }
374
375 /**
376 * Page callback: Form constructor to confirm the text format deletion.
377 *
378 * @param $format
379 * An object representing a text format.
380 *
381 * @see filter_menu()
382 * @see filter_admin_disable_submit()
383 * @ingroup forms
384 */
385 function filter_admin_disable($form, &$form_state, $format) {
386 $form['#format'] = $format;
387
388 return confirm_form($form,
389 t('Are you sure you want to disable the text format %format?', array('%format' => $format->name)),
390 'admin/config/content/formats',
391 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.'),
392 t('Disable')
393 );
394 }
395
396 /**
397 * Form submission handler for filter_admin_disable().
398 */
399 function filter_admin_disable_submit($form, &$form_state) {
400 $format = $form['#format'];
401 filter_format_disable($format);
402 drupal_set_message(t('Disabled text format %format.', array('%format' => $format->name)));
403
404 $form_state['redirect'] = 'admin/config/content/formats';
405 }