/[drupal]/contributions/modules/better_formats/better_formats.module
ViewVC logotype

Contents of /contributions/modules/better_formats/better_formats.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.27 - (show annotations) (download) (as text)
Wed Mar 18 15:25:54 2009 UTC (8 months, 1 week ago) by dragonwize
Branch: MAIN
CVS Tags: HEAD
Changes since 1.26: +2 -1 lines
File MIME type: text/x-php
Added #404618 by dragonwize, cursor: Panels comment support
1 <?php
2 // $Id: better_formats.module,v 1.23.2.9 2009/03/18 15:24:30 dragonwize Exp $
3
4 /**
5 * @file
6 * Enhances Drupal's core input format settings.
7 *
8 * Allows setting of defaults per role and content type,
9 * controls format display options, works with CCK fields.
10 */
11
12 /**
13 * Implementation of hook_help().
14 */
15 function better_formats_help($path, $arg) {
16 switch ($path) {
17 case 'admin/help/better_formats':
18 $output = '<p>' . t('See the module README.txt file in the better_formats module directory for help.') . '</p>';
19 break;
20
21 case 'admin/settings/filters/defaults':
22 $output = '<p>' . t('Set the global default formats per role for NEW nodes and comments. These settings will be applied to all nodes and comments in the site unless overriden by specific content type defaults.') . '</p>';
23 $output .= '<p>' . t('Arrange the roles to provide weight that will determine what format is selected when a user has more than one role. Remember, that all logged in users are automatically given the authenticated user role in addition to their other assigned roles. For example, if you have an admin role place it at the top and generally you would want your anonymous user role at the bottom.') . '</p>';
24 break;
25
26 default:
27 $output = '';
28 }
29 return $output;
30 }
31
32 /**
33 * Implementation of hook_perm().
34 */
35 function better_formats_perm() {
36 return array(
37 'show format selection',
38 'show format tips',
39 'show more format tips link',
40 'collapse format fieldset by default',
41 'collapsible format selection',
42 );
43 }
44
45 /**
46 * Implementation of hook_menu().
47 */
48 function better_formats_menu() {
49 $items = array();
50
51 $items['admin/settings/filters/settings'] = array(
52 'title' => 'Settings',
53 'description' => 'Manage input formats',
54 'page callback' => 'drupal_get_form',
55 'page arguments' => array('better_formats_settings_admin_form'),
56 'access arguments' => array('administer filters'),
57 'type' => MENU_LOCAL_TASK,
58 'weight' => 3,
59 'file' => 'better_formats_settings.admin.inc',
60 );
61 $items['admin/settings/filters/defaults'] = array(
62 'title' => 'Defaults',
63 'description' => 'Manage input formats',
64 'page callback' => 'drupal_get_form',
65 'page arguments' => array('better_formats_defaults_admin_form'),
66 'access arguments' => array('administer filters'),
67 'type' => MENU_LOCAL_TASK,
68 'weight' => 2,
69 'file' => 'better_formats_defaults.admin.inc',
70 );
71
72 return $items;
73 }
74
75 /**
76 * Implementation of hook_theme().
77 */
78 function better_formats_theme() {
79 return array(
80 'better_formats_defaults_admin_form' => array(
81 'template' => 'better-formats-defaults-admin-form',
82 'file' => 'better_formats_defaults.admin.inc',
83 'arguments' => array('form' => NULL),
84 ),
85 'better_formats_filter_tips_more_info' => array(
86 'arguments' => array(),
87 ),
88 );
89 }
90
91 /**
92 * Implementation of hook_form_alter().
93 */
94 function better_formats_form_alter(&$form, $form_state, $form_id) {
95 // Alter new node and comment forms.
96 // Using $form['#id'] instead of $form_id because $form_id is in the form of
97 // 'TYPE_node_form' which varies with the content type while $form['#id']
98 // is always 'node-form'.
99 switch ($form['#id']) {
100 case 'comment-form':
101 case 'panels-comment-form':
102 better_formats_set_comment_format($form);
103 break;
104
105 case 'node-form':
106 better_formats_set_node_format($form);
107 break;
108 }
109
110 // Alter role add/delete and node type forms.
111 switch ($form_id) {
112 case 'node_type_form':
113 if (variable_get('better_formats_per_node_type', FALSE)) {
114 better_formats_node_type_form($form, $form_state);
115 }
116 break;
117
118 case 'user_admin_new_role':
119 if (!in_array('better_formats_new_role', $form['#submit'])) {
120 $form['#submit'][] = 'better_formats_new_role';
121 }
122 break;
123
124 case 'user_admin_role':
125 if (isset($form_state['post']['op']) && $form_state['post']['op'] == 'Delete role') {
126 $form['#submit'][] = 'better_formats_delete_role';
127 }
128 break;
129 }
130 }
131
132 /**
133 * FAPI form to add to the content type edit form.
134 *
135 * @see better_formats_node_type_form_validate()
136 * @see better_formats_node_type_form_submit()
137 */
138 function better_formats_node_type_form(&$form, $form_state) {
139 // Add JS to enhance form.
140 drupal_add_js(drupal_get_path('module', 'better_formats') . '/better_formats_node_type_form.js');
141
142 $node_type = $form['#node_type']->type;
143
144 // Build array of all formats for allowed checkboxes.
145 $formats = filter_formats();
146 foreach ($formats as $format) {
147 $format_boxes[$format->format] = $format->name;
148 }
149
150 $key = 'better_formats';
151 $form[$key] = array(
152 '#type' => 'fieldset',
153 '#title' => t('Input format settings'),
154 '#access' => user_access('administer filters'),
155 '#collapsible' => TRUE,
156 // Setting collapsed to false because the wieght will not be hidden otherwise
157 // the fieldset will be collapsed via JS if enabled.
158 '#collapsed' => FALSE,
159 '#attributes' => array('class' => 'input-format-settings'),
160 );
161 $allowed_key = $key . '_allowed';
162 $form[$key][$allowed_key] = array(
163 '#type' => 'checkboxes',
164 '#title' => t('Allowed formats'),
165 '#default_value' => variable_get($allowed_key . '_' . $node_type, array()),
166 '#options' => $format_boxes,
167 '#description' => t('Limit the formats users have to choose from even if they have permission to use that format. This will NOT allow a user to use a format they do not have access rights to use. It will only hide additional formats they do have access rights to. If no boxes are checked, all formats that the user has permission to use will be allowed.'),
168 '#attributes' => array('class' => 'bf-allowed-formats'),
169 );
170
171 $dform = array(
172 '#tree' => TRUE,
173 '#theme' => 'better_formats_defaults_admin_form',
174 );
175
176 module_load_include('admin.inc', 'better_formats', 'better_formats_defaults');
177 $nform = better_formats_get_role_default_fields('node', $node_type);
178 $cform = better_formats_get_role_default_fields('comment', $node_type);
179
180 $form[$key]['better_formats_defaults'] = array_merge($dform, $nform, $cform);
181
182 // Attach our validate and submit handlers.
183 $form['#validate'][] = 'better_formats_node_type_form_validate';
184 $form['#submit'][] = 'better_formats_node_type_form_submit';
185 }
186
187 /**
188 * Handles validation of the addition to the content type edit form.
189 *
190 * @see better_formats_node_type_form()
191 * @see better_formats_node_type_form_submit()
192 */
193 function better_formats_node_type_form_validate($form, &$form_state) {
194 module_load_include('admin.inc', 'better_formats', 'better_formats_defaults');
195 better_formats_defaults_admin_form_validate($form, $form_state);
196 }
197
198 /**
199 * Handles submission of the addition to the content type edit form.
200 *
201 * @see better_formats_node_type_form()
202 * @see better_formats_node_type_form_validate()
203 */
204 function better_formats_node_type_form_submit($form, &$form_state) {
205 $node_type = trim($form_state['values']['type']);
206
207 // Remove current db entries.
208 $sql = "DELETE FROM {better_formats_defaults}
209 WHERE type='comment/%s' OR type='node/%s'";
210 db_query($sql, $node_type, $node_type);
211
212 // Insert defualt values into DB.
213 $sql = "INSERT INTO {better_formats_defaults}
214 VALUES (%d, '%s', %d, %d, %d)";
215 foreach ($form_state['values']['better_formats_defaults'] as $key => $values) {
216 if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) {
217 list($type, $rid) = explode('-', $key);
218 db_query($sql, $rid, $type . '/' . $node_type, $values['format'], 2, $values['weight']);
219 }
220 }
221
222 // Node module automatically stores all settings in variable table.
223 // BF saves format defaults to its own table so delete the unneeded variable.
224 variable_del('better_formats_defaults_' . $node_type);
225 }
226
227 /**
228 * Creates base format default entry for a newly created role.
229 *
230 * @see better_formats_form_alter()
231 */
232 function better_formats_new_role($form, &$form_state) {
233 // Get the ID for the role just created.
234 $sql = "SELECT rid
235 FROM {role}
236 ORDER BY rid DESC";
237 $row = db_fetch_object(db_query_range($sql, 0, 1));
238 $rid = $row->rid;
239
240 // Create stubs in per role table.
241 $sql = "INSERT INTO {better_formats_defaults}
242 VALUES (%d, '%s', %d, %d, %d)";
243 db_query($sql, $rid, 'node', 0, 1, 25);
244 db_query($sql, $rid, 'comment', 0, 1, 25);
245 }
246
247 /**
248 * Deletes role format default entries for roles being deleted.
249 *
250 * @see better_formats_form_alter()
251 */
252 function better_formats_delete_role($form, &$form_state) {
253 // Delete role from format manager table.
254 $sql = "DELETE FROM {better_formats_defaults}
255 WHERE rid = %d";
256 db_query($sql, $form['rid']['#value']);
257 }
258
259 /**
260 * Implementation of hook_node_type().
261 */
262 function better_formats_node_type($op, $info) {
263 if ($op === 'delete') {
264 // Delete per node type settings on node type delete.
265 $sql = "DELETE FROM {better_formats_defaults}
266 WHERE type IN ('node/%s', 'comment/%s')";
267 db_query($sql, $info->type, $info->type);
268
269 // Delete node type variables.
270 variable_del('better_formats_allowed_' . $info->type);
271 }
272 }
273
274 /**
275 * Implementation of hook_elements().
276 *
277 * Adds a process function to CCK's textarea FAPI element.
278 */
279 function better_formats_elements() {
280 return array(
281 'text_textarea' => array(
282 '#process' => array('better_formats_textarea_process'),
283 ),
284 );
285 }
286
287 /**
288 * Processes a CCK textarea element.
289 *
290 * Resets the textareas filter area with bettter_formats default.
291 * This function is used to affect CCK textareas not core fields.
292 *
293 * @see text_textarea_process()
294 */
295 function better_formats_textarea_process($element, $edit, $form_state, $form) {
296 $field = $form['#field_info'][$element['#field_name']];
297
298 if (!empty($field['text_processing'])) {
299 // Get core default for new or selected format for existing.
300 $filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
301 $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : $element['#default_value'][$filter_key];
302 $parents = array_merge($element['#parents'] , array($filter_key));
303 $default = better_formats_get_default_format('node', $form['type']['#value']);
304
305 // Overwrite format default if new node.
306 if (!isset($form_state['values']['nid']) || !isset($format)) {
307 $format = $default;
308 }
309 $format = filter_resolve_format($format);
310 // Set default format for cck textarea.
311 $element['#value'][$filter_key] = $format;
312 // Set filter selection form.
313 $element[$filter_key] = better_formats_filter_form($format, $default, $form['type']['#value'], 1, $parents);
314 }
315
316 return $element;
317 }
318
319 /**
320 * Processes formats for core node body fields.
321 *
322 * @see better_formats_form_alter()
323 */
324 function better_formats_set_node_format(&$form) {
325 // Set core body field.
326 if (isset($form['body_field'])) {
327 // Get default for new entries.
328 $default = better_formats_get_default_format('node', $form['type']['#value']);
329
330 if (empty($form['nid']['#value'])) {
331 // Set format to default for new entries.
332 $format = $default;
333 }
334 else {
335 // Get existing format for core body field.
336 $format = better_formats_get_current_format($form['body_field']['format']);
337 }
338
339 // Overwrite the filter form with our own.
340 $form['body_field']['format'] = better_formats_filter_form($format, $default, $form['type']['#value']);
341 }
342 }
343
344 /**
345 * Processes formats for core node comment form.
346 *
347 * @see better_formats_form_alter()
348 */
349 function better_formats_set_comment_format(&$form) {
350 if (isset($form['comment_filter']['format'])) {
351 $node = node_load($form['nid']['#value']);
352
353 // Get BF default format.
354 $default = better_formats_get_default_format('comment', $node->type);
355
356 if (empty($form['cid']['#value'])) {
357 // Set format to default for new entries.
358 $format = $default;
359 }
360 else {
361 // Get existing format for comment.
362 $format = better_formats_get_current_format($form['comment_filter']['format']);
363 }
364 // Overwrite the filter form with our own.
365 $form['comment_filter']['format'] = better_formats_filter_form($format, $default, $node->type);
366 }
367 }
368
369 /**
370 * Returns the format for an existing node or comment.
371 *
372 * @param $form
373 * FAPI form array.
374 * @return
375 * Format ID.
376 *
377 * @see better_formats_set_node_format()
378 * @see better_formats_set_comment_format()
379 */
380 function better_formats_get_current_format($form) {
381 // Default format to site default in case of error.
382 $format = FILTER_FORMAT_DEFAULT;
383 foreach (element_children($form) as $key) {
384 $element = $form[$key];
385 if ($element['#type'] === 'radio' && isset($element['#default_value'])) {
386 $format = $element['#default_value'];
387 break;
388 }
389 if ($element['#type'] === 'value' && isset($element['#value'])) {
390 $format = $element['#value'];
391 break;
392 }
393 }
394 return $format;
395 }
396
397 /**
398 * Returns the default format for an new node or comment.
399 *
400 * @param $mode
401 * 'node' or 'comment'. Describes the top level type of default.
402 * @return
403 * Format ID.
404 *
405 * @see better_formats_set_node_format()
406 * @see better_formats_set_comment_format()
407 * @see better_formats_textarea_process()
408 */
409 function better_formats_get_default_format($mode, $node_type = '') {
410 static $format;
411
412 // Default our type to the mode (node or comment).
413 $type = $mode;
414
415 // Check if per node type is enabled and set type accordingly.
416 $per_node_type = variable_get('better_formats_per_node_type', FALSE);
417 if ($per_node_type && $node_type) {
418 $type = $mode . '/' . $node_type;
419 }
420
421 // Only pull from the DB if we have not already checked for this specific type.
422 if (!isset($format[$type])) {
423 global $user;
424
425 $types = $type;
426 $format = array();
427 $roles = implode(',', array_keys($user->roles));
428
429 // Prepare types for SQL.
430 if ($per_node_type && $node_type) {
431 $types .= "','" . $mode;
432 }
433
434 // Get user's lowest weight role default.
435 $sql = "SELECT format
436 FROM {better_formats_defaults}
437 WHERE rid IN (%s) AND type IN ('$types')
438 ORDER BY type_weight DESC, weight ASC";
439 $row = db_fetch_object(db_query_range($sql, $roles, 0, 1));
440 $format[$type] = filter_resolve_format($row->format);
441 }
442
443 return $format[$type];
444 }
445
446
447 /**
448 * Better Formats version of filter_form().
449 *
450 * Copied from filter.module with slight modification to handle options for
451 * hiding filter selection and/or tips.
452 * The $node_type param was added to the signature to enable condition by
453 * content type.
454 *
455 * @see filter_form()
456 */
457 function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_format, $node_type = '', $weight = 1, $parents = array('format')) {
458 $value = filter_resolve_format($value);
459 $formats = filter_formats();
460 $show_selection = user_access('show format selection');
461 $show_tips = user_access('show format tips');
462 $show_tips_link = user_access('show more format tips link');
463 $per_node_type = variable_get('better_formats_per_node_type', FALSE);
464 $allowed_formats = variable_get('better_formats_allowed_' . $node_type, FALSE);
465
466 // Check if there are node type restrictions on allowed formats.
467 // If there are no retrictions set, we use the site globals as default.
468 if ($per_node_type && $allowed_formats) {
469 foreach ($formats as $key => $format) {
470 if (!in_array($format->format, $allowed_formats)) {
471 unset($formats[$key]);
472 }
473 }
474 }
475
476 // Ensure that our default value is allowed or change default to one that is.
477 if (isset($formats[$value])) {
478 // Use existing or BF default value if available.
479 $default = $value;
480 }
481 else if (isset($formats[$default_format])) {
482 // Use currently set BF default as a fallback.
483 $default = $default_format;
484 } else if (!empty($formats)) {
485 // Current and default format are not allowed, so use first allowed format.
486 reset($formats);
487 $default = key($formats);
488 }
489 else {
490 // Use core site default as a fallback if all else fails.
491 $default = filter_resolve_format(FILTER_FORMAT_DEFAULT);
492 }
493
494 if (count($formats) > 1 && $show_selection) {
495 $collapsed = user_access('collapse format fieldset by default');
496 $collapsible = user_access('collapsible format selection');
497 $fieldset_title = variable_get('better_formats_fieldset_title', '');
498
499 if (module_exists('i18n_strings') && $fieldset_title) {
500 $fieldset_title = tt($fieldset_title);
501 }
502 else {
503 $fieldset_title = $fieldset_title ? $fieldset_title : t('Input format');
504 }
505
506 $form = array(
507 '#type' => 'fieldset',
508 '#title' => $fieldset_title,
509 '#collapsible' => $collapsible,
510 '#collapsed' => $collapsed,
511 '#weight' => $weight,
512 '#element_validate' => array('filter_form_validate'),
513 );
514
515 // Multiple formats available: display radio buttons with tips.
516 foreach ($formats as $format) {
517 // Generate the parents as the autogenerator does, so we will have a
518 // unique id for each radio button.
519 $parents_for_id = array_merge($parents, array($format->format));
520 $form[$format->format] = array(
521 '#type' => 'radio',
522 '#title' => $format->name,
523 '#default_value' => $default,
524 '#return_value' => $format->format,
525 '#parents' => $parents,
526 '#id' => form_clean_id('edit-' . implode('-', $parents_for_id)),
527 );
528
529 if ($show_tips) {
530 $form[$format->format]['#description'] = theme('filter_tips', _filter_tips($format->format, FALSE));
531 }
532 else {
533 // Ensure expected filter_form() structure.
534 // see http://drupal.org/node/344169
535 $form[$format->format]['#description'] = '';
536 }
537 }
538
539 if ($show_tips_link) {
540 $extra = theme('better_formats_filter_tips_more_info');
541 $form[] = array('#value' => $extra);
542 }
543 else {
544 // Ensure expected filter_form() structure.
545 // see http://drupal.org/node/344169
546 $form[] = array('#value' => '');
547 }
548 }
549 else {
550 // Only one format available or hiding the form: use a hidden form item.
551 $format = $formats[$default];
552 $form[$format->format] = array(
553 '#type' => 'value',
554 '#value' => $format->format,
555 '#parents' => $parents,
556 );
557
558 if ($show_tips) {
559 $tips = _filter_tips($format->format, FALSE);
560 $form['format']['guidelines'] = array(
561 '#title' => t('Formatting guidelines'),
562 '#value' => theme('filter_tips', $tips, FALSE),
563 );
564 }
565 else {
566 // Ensure expected filter_form() structure.
567 // see http://drupal.org/node/344169
568 $form['format']['guidelines'] = array(
569 '#title' => t('Formatting guidelines'),
570 '#value' => '',
571 );
572 }
573
574 // Only show long tips link if there are guidelines to the format.
575 if ($show_tips_link) {
576 $extra = theme('better_formats_filter_tips_more_info');
577 $form[] = array('#value' => $extra);
578 }
579 else {
580 // Ensure expected filter_form() structure.
581 // see http://drupal.org/node/344169
582 $form[] = array('#value' => '');
583 }
584 }
585
586 return $form;
587 }
588
589 /**
590 * Theme function for fitler tips more info.
591 *
592 * This is copied from theme_fitler_tips_more_info() with small modifications.
593 *
594 * @return
595 * Filter tips more info HTML.
596 */
597 function theme_better_formats_filter_tips_more_info() {
598 $text = variable_get('better_formats_long_tips_link_text', '');
599 if (module_exists('i18n_strings') && $text) {
600 $text = tt($text);
601 }
602 else {
603 $text = $text ? $text : t('More information about formatting options');
604 }
605 return '<p>' . l($text, 'filter/tips') . '</p>';
606 }

  ViewVC Help
Powered by ViewVC 1.1.2