better_formats.module 19.3 KB
Newer Older
dragonwize's avatar
dragonwize committed
1
2
3
<?php
// $Id$

dragonwize's avatar
dragonwize committed
4
5
6
7
/**
 * @file
 * Enhances Drupal's core input format settings.
 *
dragonwize's avatar
dragonwize committed
8
 * Allows setting of defaults per role and content type,
dragonwize's avatar
dragonwize committed
9
 * controls format display options, works with CCK fields.
dragonwize's avatar
dragonwize committed
10
11
 */

dragonwize's avatar
dragonwize committed
12
/**
dragonwize's avatar
dragonwize committed
13
 * Implementation of hook_help().
dragonwize's avatar
dragonwize committed
14
15
16
17
 */
function better_formats_help($path, $arg) {
  switch ($path) {
    case 'admin/help/better_formats':
dragonwize's avatar
dragonwize committed
18
19
      $output = '<p>' . t('See the module README.txt file in the better_formats module directory for help.') . '</p>';
      break;
dragonwize's avatar
dragonwize committed
20

21
22
23
24
    case 'admin/settings/filters/defaults':
      $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>';
      $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>';
      break;
dragonwize's avatar
dragonwize committed
25
26

    default:
dragonwize's avatar
dragonwize committed
27
      $output = '';
dragonwize's avatar
dragonwize committed
28
29
30
31
  }
  return $output;
}

dragonwize's avatar
dragonwize committed
32
33
34
/**
 * Implementation of hook_perm().
 */
35
36
37
38
function better_formats_perm() {
  return array(
    'show format selection',
    'show format tips',
39
    'show more format tips link',
40
41
42
43
44
    'collapse format fieldset by default',
    'collapsible format selection',
  );
}

dragonwize's avatar
dragonwize committed
45
/**
dragonwize's avatar
dragonwize committed
46
 * Implementation of hook_menu().
dragonwize's avatar
dragonwize committed
47
48
49
 */
function better_formats_menu() {
  $items   = array();
dragonwize's avatar
dragonwize committed
50

dragonwize's avatar
dragonwize committed
51
52
53
54
55
56
57
58
59
  $items['admin/settings/filters/settings'] = array(
    'title' => 'Settings',
    'description' => 'Manage input formats',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('better_formats_settings_admin_form'),
    'access arguments' => array('administer filters'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 3,
    'file' => 'better_formats_settings.admin.inc',
60
  );
dragonwize's avatar
dragonwize committed
61
62
63
64
65
66
67
68
69
  $items['admin/settings/filters/defaults'] = array(
    'title' => 'Defaults',
    'description' => 'Manage input formats',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('better_formats_defaults_admin_form'),
    'access arguments' => array('administer filters'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'file' => 'better_formats_defaults.admin.inc',
70
  );
dragonwize's avatar
dragonwize committed
71

dragonwize's avatar
dragonwize committed
72
73
74
75
  return $items;
}

/**
dragonwize's avatar
dragonwize committed
76
 * Implementation of hook_theme().
dragonwize's avatar
dragonwize committed
77
78
79
80
81
82
83
84
 */
function better_formats_theme() {
  return array(
    'better_formats_defaults_admin_form' => array(
      'template' => 'better-formats-defaults-admin-form',
      'file' => 'better_formats_defaults.admin.inc',
      'arguments' => array('form' => NULL),
    ),
85
86
87
    'better_formats_filter_tips_more_info' => array(
      'arguments' => array(),
    ),
dragonwize's avatar
dragonwize committed
88
89
90
91
  );
}

/**
dragonwize's avatar
dragonwize committed
92
93
 * Implementation of hook_form_alter().
 */
dragonwize's avatar
dragonwize committed
94
function better_formats_form_alter(&$form, $form_state, $form_id) {
dragonwize's avatar
dragonwize committed
95
96
97
  // Alter new node and comment forms.
  // Using $form['#id'] instead of $form_id because $form_id is in the form of
  // 'TYPE_node_form' which varies with the content type while $form['#id']
98
  // is always 'node-form'.
dragonwize's avatar
dragonwize committed
99
100
  switch ($form['#id']) {
    case 'comment-form':
101
    case 'panels-comment-form':
102
      better_formats_set_comment_format($form);
dragonwize's avatar
dragonwize committed
103
      break;
dragonwize's avatar
dragonwize committed
104

dragonwize's avatar
dragonwize committed
105
    case 'node-form':
106
      better_formats_set_node_format($form);
dragonwize's avatar
dragonwize committed
107
108
      break;
  }
dragonwize's avatar
dragonwize committed
109

110
  // Alter role add/delete and node type forms.
dragonwize's avatar
dragonwize committed
111
112
113
  switch ($form_id) {
    case 'node_type_form':
      if (variable_get('better_formats_per_node_type', FALSE)) {
114
        better_formats_node_type_form($form, $form_state);
dragonwize's avatar
dragonwize committed
115
116
      }
      break;
dragonwize's avatar
dragonwize committed
117

dragonwize's avatar
dragonwize committed
118
    case 'user_admin_new_role':
119
      if (!in_array('better_formats_new_role', $form['#submit'])) {
dragonwize's avatar
dragonwize committed
120
121
122
        $form['#submit'][] = 'better_formats_new_role';
      }
      break;
dragonwize's avatar
dragonwize committed
123

dragonwize's avatar
dragonwize committed
124
125
126
127
128
129
130
131
    case 'user_admin_role':
      if (isset($form_state['post']['op']) && $form_state['post']['op'] == 'Delete role') {
        $form['#submit'][] = 'better_formats_delete_role';
      }
      break;
  }
}

dragonwize's avatar
dragonwize committed
132
133
/**
 * FAPI form to add to the content type edit form.
dragonwize's avatar
dragonwize committed
134
 *
135
136
 * @see better_formats_node_type_form_validate()
 * @see better_formats_node_type_form_submit()
dragonwize's avatar
dragonwize committed
137
 */
138
function better_formats_node_type_form(&$form, $form_state) {
139
  // Add JS to enhance form.
dragonwize's avatar
dragonwize committed
140
  drupal_add_js(drupal_get_path('module', 'better_formats') . '/better_formats_node_type_form.js');
dragonwize's avatar
dragonwize committed
141

dragonwize's avatar
dragonwize committed
142
  $node_type = $form['#node_type']->type;
dragonwize's avatar
dragonwize committed
143

144
  // Build array of all formats for allowed checkboxes.
dragonwize's avatar
dragonwize committed
145
146
147
148
  $formats = filter_formats();
  foreach ($formats as $format) {
    $format_boxes[$format->format] = $format->name;
  }
dragonwize's avatar
dragonwize committed
149

dragonwize's avatar
dragonwize committed
150
151
152
153
154
155
  $key = 'better_formats';
  $form[$key] = array(
    '#type' => 'fieldset',
    '#title' => t('Input format settings'),
    '#access' => user_access('administer filters'),
    '#collapsible' => TRUE,
156
157
    // Setting collapsed to false because the wieght will not be hidden otherwise
    // the fieldset will be collapsed via JS if enabled.
dragonwize's avatar
dragonwize committed
158
159
    '#collapsed' => FALSE,
    '#attributes' => array('class' => 'input-format-settings'),
dragonwize's avatar
dragonwize committed
160
161
162
163
164
165
166
167
  );
  $allowed_key = $key . '_allowed';
  $form[$key][$allowed_key] = array(
    '#type' => 'checkboxes',
    '#title' => t('Allowed formats'),
    '#default_value' => variable_get($allowed_key . '_' . $node_type, array()),
    '#options' => $format_boxes,
    '#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.'),
dragonwize's avatar
dragonwize committed
168
    '#attributes' => array('class' => 'bf-allowed-formats'),
dragonwize's avatar
dragonwize committed
169
  );
dragonwize's avatar
dragonwize committed
170

dragonwize's avatar
dragonwize committed
171
172
173
174
  $dform = array(
    '#tree' => TRUE,
    '#theme' => 'better_formats_defaults_admin_form',
  );
dragonwize's avatar
dragonwize committed
175

176
  module_load_include('admin.inc', 'better_formats', 'better_formats_defaults');
dragonwize's avatar
dragonwize committed
177
178
  $nform = better_formats_get_role_default_fields('node', $node_type);
  $cform = better_formats_get_role_default_fields('comment', $node_type);
dragonwize's avatar
dragonwize committed
179

dragonwize's avatar
dragonwize committed
180
  $form[$key]['better_formats_defaults'] = array_merge($dform, $nform, $cform);
dragonwize's avatar
dragonwize committed
181

182
  // Attach our validate and submit handlers.
183
184
  $form['#validate'][] = 'better_formats_node_type_form_validate';
  $form['#submit'][] = 'better_formats_node_type_form_submit';
dragonwize's avatar
dragonwize committed
185
186
}

dragonwize's avatar
dragonwize committed
187
/**
188
 * Handles validation of the addition to the content type edit form.
dragonwize's avatar
dragonwize committed
189
 *
190
191
 * @see better_formats_node_type_form()
 * @see better_formats_node_type_form_submit()
dragonwize's avatar
dragonwize committed
192
 */
193
194
function better_formats_node_type_form_validate($form, &$form_state) {
  module_load_include('admin.inc', 'better_formats', 'better_formats_defaults');
195
196
197
  better_formats_defaults_admin_form_validate($form, $form_state);
}

dragonwize's avatar
dragonwize committed
198
/**
199
 * Handles submission of the addition to the content type edit form.
dragonwize's avatar
dragonwize committed
200
 *
201
202
 * @see better_formats_node_type_form()
 * @see better_formats_node_type_form_validate()
dragonwize's avatar
dragonwize committed
203
 */
204
205
function better_formats_node_type_form_submit($form, &$form_state) {
  $node_type = trim($form_state['values']['type']);
dragonwize's avatar
dragonwize committed
206

207
  // Remove current db entries.
dragonwize's avatar
dragonwize committed
208
209
210
211
  $sql = "DELETE FROM {better_formats_defaults}
          WHERE type='comment/%s' OR type='node/%s'";
  db_query($sql, $node_type, $node_type);

212
  // Insert defualt values into DB.
dragonwize's avatar
dragonwize committed
213
  $sql = "INSERT INTO {better_formats_defaults}
dragonwize's avatar
dragonwize committed
214
215
216
217
218
219
220
221
          VALUES (%d, '%s', %d, %d, %d)";
  foreach ($form_state['values']['better_formats_defaults'] as $key => $values) {
    if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) {
      list($type, $rid) = explode('-', $key);
      db_query($sql, $rid, $type . '/' . $node_type, $values['format'], 2, $values['weight']);
    }
  }

222
223
  // Node module automatically stores all settings in variable table.
  // BF saves format defaults to its own table so delete the unneeded variable.
224
  variable_del('better_formats_defaults_' . $node_type);
dragonwize's avatar
dragonwize committed
225
226
227
}

/**
dragonwize's avatar
dragonwize committed
228
 * Creates base format default entry for a newly created role.
dragonwize's avatar
dragonwize committed
229
 *
dragonwize's avatar
dragonwize committed
230
 * @see better_formats_form_alter()
dragonwize's avatar
dragonwize committed
231
232
 */
function better_formats_new_role($form, &$form_state) {
233
  // Get the ID for the role just created.
dragonwize's avatar
dragonwize committed
234
235
  $sql = "SELECT rid
          FROM {role}
dragonwize's avatar
dragonwize committed
236
          ORDER BY rid DESC";
dragonwize's avatar
dragonwize committed
237
238
  $row = db_fetch_object(db_query_range($sql, 0, 1));
  $rid = $row->rid;
dragonwize's avatar
dragonwize committed
239

240
  // Create stubs in per role table.
dragonwize's avatar
dragonwize committed
241
  $sql = "INSERT INTO {better_formats_defaults}
242
          VALUES (%d, '%s', %d, %d, %d)";
243
244
  db_query($sql, $rid, 'node', 0, 1, 25);
  db_query($sql, $rid, 'comment', 0, 1, 25);
dragonwize's avatar
dragonwize committed
245
246
247
}

/**
dragonwize's avatar
dragonwize committed
248
 * Deletes role format default entries for roles being deleted.
dragonwize's avatar
dragonwize committed
249
 *
dragonwize's avatar
dragonwize committed
250
 * @see better_formats_form_alter()
dragonwize's avatar
dragonwize committed
251
252
 */
function better_formats_delete_role($form, &$form_state) {
253
  // Delete role from format manager table.
dragonwize's avatar
dragonwize committed
254
  $sql = "DELETE FROM {better_formats_defaults}
dragonwize's avatar
dragonwize committed
255
256
257
258
          WHERE rid = %d";
  db_query($sql, $form['rid']['#value']);
}

259
260
261
262
263
/**
 * Implementation of hook_node_type().
 */
function better_formats_node_type($op, $info) {
  if ($op === 'delete') {
264
    // Delete per node type settings on node type delete.
dragonwize's avatar
dragonwize committed
265
    $sql = "DELETE FROM {better_formats_defaults}
266
267
            WHERE type IN ('node/%s', 'comment/%s')";
    db_query($sql, $info->type, $info->type);
dragonwize's avatar
dragonwize committed
268

269
    // Delete node type variables.
270
271
272
273
    variable_del('better_formats_allowed_' . $info->type);
  }
}

dragonwize's avatar
dragonwize committed
274
/**
dragonwize's avatar
dragonwize committed
275
 * Implementation of hook_elements().
dragonwize's avatar
dragonwize committed
276
 *
277
 * Adds a process function to CCK's textarea FAPI element.
dragonwize's avatar
dragonwize committed
278
279
 */
function better_formats_elements() {
dragonwize's avatar
dragonwize committed
280
  return array(
dragonwize's avatar
dragonwize committed
281
282
283
    'text_textarea' => array(
      '#process' => array('better_formats_textarea_process'),
    ),
dragonwize's avatar
dragonwize committed
284
  );
dragonwize's avatar
dragonwize committed
285
286
287
}

/**
dragonwize's avatar
dragonwize committed
288
 * Processes a CCK textarea element.
dragonwize's avatar
dragonwize committed
289
 *
dragonwize's avatar
dragonwize committed
290
291
 * Resets the textareas filter area with bettter_formats default.
 * This function is used to affect CCK textareas not core fields.
dragonwize's avatar
dragonwize committed
292
 *
dragonwize's avatar
dragonwize committed
293
 * @see text_textarea_process()
dragonwize's avatar
dragonwize committed
294
295
 */
function better_formats_textarea_process($element, $edit, $form_state, $form) {
dragonwize's avatar
dragonwize committed
296
  $field = $form['#field_info'][$element['#field_name']];
dragonwize's avatar
dragonwize committed
297

298
299
  if (!empty($field['text_processing'])) {
    // Get core default for new or selected format for existing.
dragonwize's avatar
dragonwize committed
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];
dragonwize's avatar
dragonwize committed
302
    $parents    = array_merge($element['#parents'] , array($filter_key));
303
    $default    = better_formats_get_default_format('node', $form['type']['#value']);
dragonwize's avatar
dragonwize committed
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);
dragonwize's avatar
dragonwize committed
314
  }
dragonwize's avatar
dragonwize committed
315

dragonwize's avatar
dragonwize committed
316
  return $element;
dragonwize's avatar
dragonwize committed
317
318
}

dragonwize's avatar
dragonwize committed
319
320
/**
 * Processes formats for core node body fields.
dragonwize's avatar
dragonwize committed
321
 *
dragonwize's avatar
dragonwize committed
322
323
 * @see better_formats_form_alter()
 */
dragonwize's avatar
dragonwize committed
324
function better_formats_set_node_format(&$form) {
325
  // Set core body field.
dragonwize's avatar
dragonwize committed
326
  if (isset($form['body_field'])) {
327
    // Get default for new entries.
328
    $default = better_formats_get_default_format('node', $form['type']['#value']);
dragonwize's avatar
dragonwize committed
329

330
    if (empty($form['nid']['#value'])) {
331
      // Set format to default for new entries.
332
      $format = $default;
dragonwize's avatar
dragonwize committed
333
    }
dragonwize's avatar
dragonwize committed
334
    else {
335
      // Get existing format for core body field.
336
      $format = better_formats_get_current_format($form['body_field']['format']);
337
    }
dragonwize's avatar
dragonwize committed
338

339
    // Overwrite the filter form with our own.
340
    $form['body_field']['format'] = better_formats_filter_form($format, $default, $form['type']['#value']);
dragonwize's avatar
dragonwize committed
341
342
343
  }
}

dragonwize's avatar
dragonwize committed
344
345
/**
 * Processes formats for core node comment form.
dragonwize's avatar
dragonwize committed
346
 *
dragonwize's avatar
dragonwize committed
347
348
 * @see better_formats_form_alter()
 */
dragonwize's avatar
dragonwize committed
349
350
function better_formats_set_comment_format(&$form) {
  if (isset($form['comment_filter']['format'])) {
351
    $node = node_load($form['nid']['#value']);
dragonwize's avatar
dragonwize committed
352

353
    // Get BF default format.
354
    $default = better_formats_get_default_format('comment', $node->type);
dragonwize's avatar
dragonwize committed
355

356
    if (empty($form['cid']['#value'])) {
357
      // Set format to default for new entries.
358
      $format = $default;
dragonwize's avatar
dragonwize committed
359
    }
dragonwize's avatar
dragonwize committed
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);
dragonwize's avatar
dragonwize committed
366
367
368
  }
}

dragonwize's avatar
dragonwize committed
369
370
/**
 * Returns the format for an existing node or comment.
dragonwize's avatar
dragonwize committed
371
 *
372
373
374
375
 * @param $form
 *  FAPI form array.
 * @return
 *  Format ID.
dragonwize's avatar
dragonwize committed
376
 *
dragonwize's avatar
dragonwize committed
377
378
379
 * @see better_formats_set_node_format()
 * @see better_formats_set_comment_format()
 */
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
386
387
388
389
390
391
392
393
394
395
396
    if ($element['#type'] === 'radio' && isset($element['#default_value'])) {
      $format = $element['#default_value'];
      break;
    }
    if ($element['#type'] === 'value' && isset($element['#value'])) {
      $format = $element['#value'];
      break;
    }
  }
  return $format;
}

dragonwize's avatar
dragonwize committed
397
398
/**
 * Returns the default format for an new node or comment.
dragonwize's avatar
dragonwize committed
399
 *
400
 * @param $mode
dragonwize's avatar
dragonwize committed
401
 *  'node' or 'comment'. Describes the top level type of default.
402
403
 * @return
 *  Format ID.
dragonwize's avatar
dragonwize committed
404
 *
dragonwize's avatar
dragonwize committed
405
406
407
408
 * @see better_formats_set_node_format()
 * @see better_formats_set_comment_format()
 * @see better_formats_textarea_process()
 */
dragonwize's avatar
dragonwize committed
409
410
function better_formats_get_default_format($mode, $node_type = '') {
  static $format;
dragonwize's avatar
dragonwize committed
411

412
  // Default our type to the mode (node or comment).
dragonwize's avatar
dragonwize committed
413
  $type = $mode;
dragonwize's avatar
dragonwize committed
414

415
  // Check if per node type is enabled and set type accordingly.
dragonwize's avatar
dragonwize committed
416
  $per_node_type = variable_get('better_formats_per_node_type', FALSE);
dragonwize's avatar
dragonwize committed
417
418
419
420
  if ($per_node_type && $node_type) {
    $type = $mode . '/' . $node_type;
  }

421
422
  // Only pull from the DB if we have not already checked for this specific type.
  if (!isset($format[$type])) {
dragonwize's avatar
dragonwize committed
423
    global $user;
dragonwize's avatar
dragonwize committed
424

dragonwize's avatar
dragonwize committed
425
426
427
    $types  = $type;
    $format = array();
    $roles  = implode(',', array_keys($user->roles));
dragonwize's avatar
dragonwize committed
428

429
    // Prepare types for SQL.
dragonwize's avatar
dragonwize committed
430
431
432
    if ($per_node_type && $node_type) {
      $types .= "','" . $mode;
    }
dragonwize's avatar
dragonwize committed
433

434
    // Get user's lowest weight role default.
dragonwize's avatar
dragonwize committed
435
436
437
    $sql = "SELECT format
            FROM {better_formats_defaults}
            WHERE rid IN (%s) AND type IN ('$types')
dragonwize's avatar
dragonwize committed
438
            ORDER BY type_weight DESC, weight ASC";
439
440
    $row = db_fetch_object(db_query_range($sql, $roles, 0, 1));
    $format[$type] = filter_resolve_format($row->format);
dragonwize's avatar
dragonwize committed
441
  }
dragonwize's avatar
dragonwize committed
442

dragonwize's avatar
dragonwize committed
443
444
445
446
447
448
  return $format[$type];
}


/**
 * Better Formats version of filter_form().
dragonwize's avatar
dragonwize committed
449
450
451
452
 *
 * Copied from filter.module with slight modification to handle options for
 * hiding filter selection and/or tips.
 * The $node_type param was added to the signature to enable condition by
453
 * content type.
dragonwize's avatar
dragonwize committed
454
455
 *
 * @see filter_form()
dragonwize's avatar
dragonwize committed
456
 */
457
function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_format, $node_type = '', $weight = 1, $parents = array('format')) {
458
459
460
461
  $value           = filter_resolve_format($value);
  $formats         = filter_formats();
  $show_selection  = user_access('show format selection');
  $show_tips       = user_access('show format tips');
462
  $show_tips_link  = user_access('show more format tips link');
463
464
  $per_node_type   = variable_get('better_formats_per_node_type', FALSE);
  $allowed_formats = variable_get('better_formats_allowed_' . $node_type, FALSE);
dragonwize's avatar
dragonwize committed
465

466
467
  // Check if there are node type restrictions on allowed formats.
  // If there are no retrictions set, we use the site globals as default.
468
  if ($per_node_type && $allowed_formats) {
dragonwize's avatar
dragonwize committed
469
    foreach ($formats as $key => $format) {
470
      if (!in_array($format->format, $allowed_formats)) {
dragonwize's avatar
dragonwize committed
471
472
473
474
        unset($formats[$key]);
      }
    }
  }
dragonwize's avatar
dragonwize committed
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
480
481
    $default = $value;
  }
  else if (isset($formats[$default_format])) {
482
    // Use currently set BF default as a fallback.
483
    $default = $default_format;
484
485
  } else if (!empty($formats)) {
    // Current and default format are not allowed, so use first allowed format.
486
487
    reset($formats);
    $default = key($formats);
488
489
  }
  else {
490
    // Use core site default as a fallback if all else fails.
491
492
    $default = filter_resolve_format(FILTER_FORMAT_DEFAULT);
  }
dragonwize's avatar
dragonwize committed
493

494
495
496
  if (count($formats) > 1 && $show_selection) {
    $collapsed      = user_access('collapse format fieldset by default');
    $collapsible    = user_access('collapsible format selection');
497
    $fieldset_title = variable_get('better_formats_fieldset_title', '');
dragonwize's avatar
dragonwize committed
498

499
500
501
502
503
    if (module_exists('i18n_strings') && $fieldset_title) {
      $fieldset_title = tt($fieldset_title);
    }
    else {
      $fieldset_title = $fieldset_title ? $fieldset_title : t('Input format');
504
    }
dragonwize's avatar
dragonwize committed
505

dragonwize's avatar
dragonwize committed
506
507
    $form = array(
      '#type' => 'fieldset',
508
      '#title' => $fieldset_title,
509
510
      '#collapsible' => $collapsible,
      '#collapsed' => $collapsed,
dragonwize's avatar
dragonwize committed
511
512
513
      '#weight' => $weight,
      '#element_validate' => array('filter_form_validate'),
    );
dragonwize's avatar
dragonwize committed
514

dragonwize's avatar
dragonwize committed
515
516
517
518
519
520
521
522
523
524
525
    // Multiple formats available: display radio buttons with tips.
    foreach ($formats as $format) {
      // Generate the parents as the autogenerator does, so we will have a
      // unique id for each radio button.
      $parents_for_id = array_merge($parents, array($format->format));
      $form[$format->format] = array(
        '#type' => 'radio',
        '#title' => $format->name,
        '#default_value' => $default,
        '#return_value' => $format->format,
        '#parents' => $parents,
526
        '#id' => form_clean_id('edit-' . implode('-', $parents_for_id)),
dragonwize's avatar
dragonwize committed
527
      );
dragonwize's avatar
dragonwize committed
528

529
      if ($show_tips) {
530
        $form[$format->format]['#description'] = theme('filter_tips', _filter_tips($format->format, FALSE));
dragonwize's avatar
dragonwize committed
531
      }
532
      else {
533
        // Ensure expected filter_form() structure.
dragonwize's avatar
dragonwize committed
534
        // see http://drupal.org/node/344169
535
        $form[$format->format]['#description'] = '';
536
      }
dragonwize's avatar
dragonwize committed
537
    }
dragonwize's avatar
dragonwize committed
538

539
    if ($show_tips_link) {
540
      $extra = theme('better_formats_filter_tips_more_info');
541
542
543
      $form[] = array('#value' => $extra);
    }
    else {
544
      // Ensure expected filter_form() structure.
545
546
547
      // see http://drupal.org/node/344169
      $form[] = array('#value' => '');
    }
dragonwize's avatar
dragonwize committed
548
  }
dragonwize's avatar
dragonwize committed
549
  else {
550
    // Only one format available or hiding the form: use a hidden form item.
dragonwize's avatar
dragonwize committed
551
552
    $format = $formats[$default];
    $form[$format->format] = array(
dragonwize's avatar
dragonwize committed
553
554
      '#type' => 'value',
      '#value' => $format->format,
555
      '#parents' => $parents,
dragonwize's avatar
dragonwize committed
556
    );
dragonwize's avatar
dragonwize committed
557

558
    if ($show_tips) {
559
      $tips = _filter_tips($format->format, FALSE);
dragonwize's avatar
dragonwize committed
560
561
      $form['format']['guidelines'] = array(
        '#title' => t('Formatting guidelines'),
562
        '#value' => theme('filter_tips', $tips, FALSE),
dragonwize's avatar
dragonwize committed
563
      );
dragonwize's avatar
dragonwize committed
564
    }
dragonwize's avatar
dragonwize committed
565
    else {
566
      // Ensure expected filter_form() structure.
dragonwize's avatar
dragonwize committed
567
      // see http://drupal.org/node/344169
568
569
570
571
      $form['format']['guidelines'] = array(
        '#title' => t('Formatting guidelines'),
        '#value' => '',
      );
572
    }
dragonwize's avatar
dragonwize committed
573

574
    // Only show long tips link if there are guidelines to the format.
575
576
577
    if ($show_tips_link) {
      $extra = theme('better_formats_filter_tips_more_info');
      $form[] = array('#value' => $extra);
dragonwize's avatar
dragonwize committed
578
    }
579
580
    else {
      // Ensure expected filter_form() structure.
581
      // see http://drupal.org/node/344169
582
      $form[] = array('#value' => '');
583
    }
dragonwize's avatar
dragonwize committed
584
  }
dragonwize's avatar
dragonwize committed
585

dragonwize's avatar
dragonwize committed
586
587
588
  return $form;
}

589
590
/**
 * Theme function for fitler tips more info.
dragonwize's avatar
dragonwize committed
591
 *
592
 * This is copied from theme_fitler_tips_more_info() with small modifications.
dragonwize's avatar
dragonwize committed
593
 *
594
595
596
 * @return
 *  Filter tips more info HTML.
 */
597
598
function theme_better_formats_filter_tips_more_info() {
  $text = variable_get('better_formats_long_tips_link_text', '');
599
600
601
602
603
604
  if (module_exists('i18n_strings') && $text) {
    $text = tt($text);
  }
  else {
    $text = $text ? $text : t('More information about formatting options');
  }
605
606
  return '<p>' . l($text, 'filter/tips') . '</p>';
}