Code coverage for /20081101/modules/forum/forum.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: forum.admin.inc,v 1.14 2008/10/12 04:30:06 webchick Exp $
3
4
/**
5
 * @file
6
 * Administrative page callbacks for the forum module.
7
 */
8
99
function forum_form_main($type, $edit = array()) {
106
  if ((isset($_POST['op']) && $_POST['op'] == t('Delete')) ||
!empty($_POST['confirm'])) {
110
    return drupal_get_form('forum_confirm_delete', $edit['tid']);
120
  }
13
  switch ($type) {
146
    case 'forum':
154
      return drupal_get_form('forum_form_forum', $edit);
160
      break;
172
    case 'container':
182
      return drupal_get_form('forum_form_container', $edit);
190
      break;
200
  }
210
}
22
23
/**
24
 * Returns a form for adding a forum to the forum vocabulary
25
 *
26
 * @param $edit Associative array containing a forum term to be added or
edited.
27
 * @ingroup forms
28
 * @see forum_form_submit()
29
 */
309
function forum_form_forum(&$form_state, $edit = array()) {
31
  $edit += array(
324
    'name' => '',
334
    'description' => '',
344
    'tid' => NULL,
354
    'weight' => 0,
360
  );
374
  $form['name'] = array('#type' => 'textfield',
384
    '#title' => t('Forum name'),
394
    '#default_value' => $edit['name'],
404
    '#maxlength' => 255,
414
    '#description' => t('Short but meaningful name for this collection of
threaded discussions.'),
424
    '#required' => TRUE,
43
  );
444
  $form['description'] = array('#type' => 'textarea',
454
    '#title' => t('Description'),
464
    '#default_value' => $edit['description'],
474
    '#description' => t('Description and guidelines for discussions within
this forum.'),
48
  );
494
  $form['parent']['#tree'] = TRUE;
504
  $form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'),
'forum');
514
  $form['weight'] = array('#type' => 'weight',
524
    '#title' => t('Weight'),
534
    '#default_value' => $edit['weight'],
544
    '#description' => t('Forums are displayed in ascending order by weight
(forums with equal weights are displayed alphabetically).'),
55
  );
56
574
  $form['vid'] = array('#type' => 'hidden', '#value' =>
variable_get('forum_nav_vocabulary', ''));
584
  $form['submit' ] = array('#type' => 'submit', '#value' => t('Save'));
594
  if ($edit['tid']) {
600
    $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
610
    $form['tid'] = array('#type' => 'hidden', '#value' => $edit['tid']);
620
  }
634
  $form['#submit'][] = 'forum_form_submit';
644
  $form['#theme'] = 'forum_form';
65
664
  return $form;
670
}
68
69
/**
70
 * Process forum form and container form submissions.
71
 */
729
function forum_form_submit($form, &$form_state) {
733
  if ($form['form_id']['#value'] == 'forum_form_container') {
741
    $container = TRUE;
751
    $type = t('forum container');
761
  }
77
  else {
782
    $container = FALSE;
792
    $type = t('forum');
80
  }
81
823
  $status = taxonomy_save_term($form_state['values']);
83
  switch ($status) {
843
    case SAVED_NEW:
853
      if ($container) {
861
        $containers = variable_get('forum_containers', array());
871
        $containers[] = $form_state['values']['tid'];
881
        variable_set('forum_containers', $containers);
891
      }
903
      drupal_set_message(t('Created new @type %term.', array('%term' =>
$form_state['values']['name'], '@type' => $type)));
913
      break;
920
    case SAVED_UPDATED:
930
      drupal_set_message(t('The @type %term has been updated.',
array('%term' => $form_state['values']['name'], '@type' => $type)));
940
      break;
950
  }
963
  $form_state['redirect'] = 'admin/build/forum';
973
  return;
980
}
99
100
/**
101
 * Returns a form for adding a container to the forum vocabulary
102
 *
103
 * @param $edit Associative array containing a container term to be added
or edited.
104
 * @ingroup forms
105
 * @see forum_form_submit()
106
 */
1079
function forum_form_container(&$form_state, $edit = array()) {
108
  $edit += array(
1092
    'name' => '',
1102
    'description' => '',
1112
    'tid' => NULL,
1122
    'weight' => 0,
1130
  );
114
  // Handle a delete operation.
1152
  $form['name'] = array(
1162
    '#title' => t('Container name'),
1172
    '#type' => 'textfield',
1182
    '#default_value' => $edit['name'],
1192
    '#maxlength' => 255,
1202
    '#description' => t('Short but meaningful name for this collection of
related forums.'),
121
    '#required' => TRUE
1222
  );
123
1242
  $form['description'] = array(
1252
    '#type' => 'textarea',
1262
    '#title' => t('Description'),
1272
    '#default_value' => $edit['description'],
1282
    '#description' => t('Description and guidelines for forums within this
container.')
1292
  );
1302
  $form['parent']['#tree'] = TRUE;
1312
  $form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'),
'container');
1322
  $form['weight'] = array(
1332
    '#type' => 'weight',
1342
    '#title' => t('Weight'),
1352
    '#default_value' => $edit['weight'],
1362
    '#description' => t('Containers are displayed in ascending order by
weight (containers with equal weights are displayed alphabetically).')
1372
  );
138
1392
  $form['vid'] = array(
1402
    '#type' => 'hidden',
1412
    '#value' => variable_get('forum_nav_vocabulary', ''),
142
  );
1432
  $form['submit'] = array(
1442
    '#type' => 'submit',
1452
    '#value' => t('Save')
1462
  );
1472
  if ($edit['tid']) {
1480
    $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
1490
    $form['tid'] = array('#type' => 'value', '#value' => $edit['tid']);
1500
  }
1512
  $form['#submit'][] = 'forum_form_submit';
1522
  $form['#theme'] = 'forum_form';
153
1542
  return $form;
1550
}
156
157
/**
158
 * Returns a confirmation page for deleting a forum taxonomy term.
159
 *
160
 * @param $tid ID of the term to be deleted
161
 */
1629
function forum_confirm_delete(&$form_state, $tid) {
1630
  $term = taxonomy_term_load($tid);
164
1650
  $form['tid'] = array('#type' => 'value', '#value' => $tid);
1660
  $form['name'] = array('#type' => 'value', '#value' => $term->name);
167
1680
  return confirm_form($form, t('Are you sure you want to delete the forum
%name?', array('%name' => $term->name)), 'admin/build/forum', t('Deleting a
forum or container will also delete its sub-forums, if any. To delete posts
in this forum, visit <a href="@content">content administration</a> first.
This action cannot be undone.', array('@content' =>
url('admin/content/node'))), t('Delete'), t('Cancel'));
1690
}
170
171
/**
172
 * Implementation of forms api _submit call. Deletes a forum after
confirmation.
173
 */
1749
function forum_confirm_delete_submit($form, &$form_state) {
1750
  taxonomy_del_term($form_state['values']['tid']);
1760
  drupal_set_message(t('The forum %term and all sub-forums have been
deleted.', array('%term' => $form_state['values']['name'])));
1770
  watchdog('content', 'forum: deleted %term and all its sub-forums.',
array('%term' => $form_state['values']['name']));
178
1790
  $form_state['redirect'] = 'admin/build/forum';
1800
  return;
1810
}
182
183
/**
184
 * Form builder for the forum settings page.
185
 *
186
 * @see system_settings_form()
187
 */
1889
function forum_admin_settings() {
1890
  $form = array();
1900
  $number = drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60,
80, 100, 150, 200, 250, 300, 350, 400, 500));
1910
  $form['forum_hot_topic'] = array('#type' => 'select',
1920
    '#title' => t('Hot topic threshold'),
1930
    '#default_value' => variable_get('forum_hot_topic', 15),
1940
    '#options' => $number,
1950
    '#description' => t('The number of posts a topic must have to be
considered "hot".'),
196
  );
1970
  $number = drupal_map_assoc(array(10, 25, 50, 75, 100));
1980
  $form['forum_per_page'] = array('#type' => 'select',
1990
    '#title' => t('Topics per page'),
2000
    '#default_value' => variable_get('forum_per_page', 25),
2010
    '#options' => $number,
2020
    '#description' => t('Default number of forum topics displayed per
page.'),
203
  );
2040
  $forder = array(1 => t('Date - newest first'), 2 => t('Date - oldest
first'), 3 => t('Posts - most active first'), 4 => t('Posts - least active
first'));
2050
  $form['forum_order'] = array('#type' => 'radios',
2060
    '#title' => t('Default order'),
2070
    '#default_value' => variable_get('forum_order', '1'),
2080
    '#options' => $forder,
2090
    '#description' => t('Default display order for topics.'),
210
  );
2110
  return system_settings_form($form);
2120
}
213
214
/**
215
 * Returns an overview list of existing forums and containers
216
 */
2179
function forum_overview(&$form_state) {
2183
  module_load_include('inc', 'taxonomy', 'taxonomy.admin');
219
2203
  $vid = variable_get('forum_nav_vocabulary', '');
2213
  $vocabulary = taxonomy_vocabulary_load($vid);
2223
  $form = taxonomy_overview_terms($form_state, $vocabulary);
2233
  drupal_set_title('Forums');
224
2253
  foreach (element_children($form) as $key) {
2263
    if (isset($form[$key]['#term'])) {
2273
      $term = $form[$key]['#term'];
2283
      $form[$key]['view']['#markup'] = l($term['name'], 'forum/' .
$term['tid']);
2293
      if (in_array($form[$key]['#term']['tid'],
variable_get('forum_containers', array()))) {
2303
        $form[$key]['edit']['#markup'] = l(t('edit container'),
'admin/build/forum/edit/container/' . $term['tid']);
2313
      }
232
      else {
2332
        $form[$key]['edit']['#markup'] = l(t('edit forum'),
'admin/build/forum/edit/forum/' . $term['tid']);
234
      }
2353
    }
2363
  }
237
238
  // Remove the alphabetical reset.
2393
  unset($form['reset_alphabetical']);
240
241
  // The form needs to have submit and validate handlers set explicitly.
2423
  $form['#theme'] = 'taxonomy_overview_terms';
2433
  $form['#submit'] = array('taxonomy_overview_terms_submit'); // Use the
existing taxonomy overview submit handler.
2443
  $form['#validate'] = array('taxonomy_overview_terms_validate');
2453
  $form['#empty_text'] = '<em>' . t('There are no existing containers or
forums. Containers and forums may be added using the <a
href="@container">add container</a> and <a href="@forum">add forum</a>
pages.', array('@container' => url('admin/build/forum/add/container'),
'@forum' => url('admin/build/forum/add/forum'))) . '</em>';
2463
  return $form;
2470
}
248
249
/**
250
 * Returns a select box for available parent terms
251
 *
252
 * @param $tid ID of the term which is being added or edited
253
 * @param $title Title to display the select box with
254
 * @param $child_type Whether the child is forum or container
255
 */
2569
function _forum_parent_select($tid, $title, $child_type) {
257
2586
  $parents = taxonomy_get_parents($tid);
2596
  if ($parents) {
2600
    $parent = array_shift($parents);
2610
    $parent = $parent->tid;
2620
  }
263
  else {
2646
    $parent = 0;
265
  }
266
2676
  $vid = variable_get('forum_nav_vocabulary', '');
2686
  $children = taxonomy_get_tree($vid, $tid);
269
270
  // A term can't be the child of itself, nor of its children.
2716
  foreach ($children as $child) {
2720
    $exclude[] = $child->tid;
2730
  }
2746
  $exclude[] = $tid;
275
2766
  $tree = taxonomy_get_tree($vid);
2776
  $options[0] = '<' . t('root') . '>';
2786
  if ($tree) {
2794
    foreach ($tree as $term) {
2804
      if (!in_array($term->tid, $exclude)) {
2814
        $options[$term->tid] = str_repeat(' -- ', $term->depth) .
$term->name;
2824
      }
2834
    }
2844
  }
2856
  if ($child_type == 'container') {
2862
    $description = t('Containers are usually placed at the top (root)
level, but may also be placed inside another container or forum.');
2872
  }
2884
  elseif ($child_type == 'forum') {
2894
    $description = t('Forums may be placed at the top (root) level, or
inside another container or forum.');
2904
  }
291
2926
  return array('#type' => 'select', '#title' => $title, '#default_value' =>
$parent, '#options' => $options, '#description' => $description,
'#required' => TRUE);
2930
}
2949