Simpletest Coverage - modules/filter/filter.admin.inc

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

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.