forum.admin.inc 11 KB
Newer Older
1 2 3 4 5 6 7 8
<?php
// $Id$

/**
 * @file
 * Administrative page callbacks for the forum module.
 */
function forum_form_main($type, $edit = array()) {
9
  $edit = (array) $edit;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
  if ((isset($_POST['op']) && $_POST['op'] == t('Delete')) || !empty($_POST['confirm'])) {
    return drupal_get_form('forum_confirm_delete', $edit['tid']);
  }
  switch ($type) {
    case 'forum':
      return drupal_get_form('forum_form_forum', $edit);
      break;
    case 'container':
      return drupal_get_form('forum_form_container', $edit);
      break;
  }
}

/**
 * Returns a form for adding a forum to the forum vocabulary
 *
 * @param $edit Associative array containing a forum term to be added or edited.
 * @ingroup forms
28
 * @see forum_form_submit()
29
 */
30
function forum_form_forum($form, &$form_state, $edit = array()) {
31 32 33 34 35 36 37 38 39 40
  $edit += array(
    'name' => '',
    'description' => '',
    'tid' => NULL,
    'weight' => 0,
  );
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('Forum name'),
    '#default_value' => $edit['name'],
    '#maxlength' => 255,
41
    '#description' => t('Short but meaningful name for this collection of threaded discussions.'),
42 43 44 45 46
    '#required' => TRUE,
  );
  $form['description'] = array('#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $edit['description'],
47
    '#description' => t('Description and guidelines for discussions within this forum.'),
48 49 50 51 52 53
  );
  $form['parent']['#tree'] = TRUE;
  $form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'), 'forum');
  $form['weight'] = array('#type' => 'weight',
    '#title' => t('Weight'),
    '#default_value' => $edit['weight'],
54
    '#description' => t('Forums are displayed in ascending order by weight (forums with equal weights are displayed alphabetically).'),
55 56 57
  );

  $form['vid'] = array('#type' => 'hidden', '#value' => variable_get('forum_nav_vocabulary', ''));
58
  $form['actions'] = array('#type' => 'actions');
59
  $form['actions']['submit' ] = array('#type' => 'submit', '#value' => t('Save'));
60
  if ($edit['tid']) {
61
    $form['actions']['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    $form['tid'] = array('#type' => 'hidden', '#value' => $edit['tid']);
  }
  $form['#submit'][] = 'forum_form_submit';
  $form['#theme'] = 'forum_form';

  return $form;
}

/**
 * Process forum form and container form submissions.
 */
function forum_form_submit($form, &$form_state) {
  if ($form['form_id']['#value'] == 'forum_form_container') {
    $container = TRUE;
    $type = t('forum container');
  }
  else {
    $container = FALSE;
    $type = t('forum');
  }

83 84
  $term = (object) $form_state['values'];
  $status = taxonomy_term_save($term);
85 86 87 88
  switch ($status) {
    case SAVED_NEW:
      if ($container) {
        $containers = variable_get('forum_containers', array());
89
        $containers[] = $term->tid;
90 91
        variable_set('forum_containers', $containers);
      }
92
      $form_state['values']['tid'] = $term->tid;
93 94 95 96
      drupal_set_message(t('Created new @type %term.', array('%term' => $form_state['values']['name'], '@type' => $type)));
      break;
    case SAVED_UPDATED:
      drupal_set_message(t('The @type %term has been updated.', array('%term' => $form_state['values']['name'], '@type' => $type)));
97 98
      // Clear the page and block caches to avoid stale data.
      cache_clear_all();
99 100
      break;
  }
101
  $form_state['redirect'] = 'admin/structure/forum';
102 103 104
  return;
}

105
/**
106
 * Returns HTML for a forum form.
107 108 109 110 111 112
 *
 * By default this does not alter the appearance of a form at all,
 * but is provided as a convenience for themers.
 *
 * @param $variables
 *   An associative array containing:
113 114
 *   - form: A render element representing the form.
 *
115 116 117 118 119 120
 * @ingroup themeable
 */
function theme_forum_form($variables) {
  return drupal_render_children($variables['form']);
}

121 122 123 124 125
/**
 * Returns a form for adding a container to the forum vocabulary
 *
 * @param $edit Associative array containing a container term to be added or edited.
 * @ingroup forms
126
 * @see forum_form_submit()
127
 */
128
function forum_form_container($form, &$form_state, $edit = array()) {
129 130 131 132 133 134 135 136 137 138 139 140
  $edit += array(
    'name' => '',
    'description' => '',
    'tid' => NULL,
    'weight' => 0,
  );
  // Handle a delete operation.
  $form['name'] = array(
    '#title' => t('Container name'),
    '#type' => 'textfield',
    '#default_value' => $edit['name'],
    '#maxlength' => 255,
141
    '#description' => t('Short but meaningful name for this collection of related forums.'),
142 143 144 145 146 147 148
    '#required' => TRUE
  );

  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $edit['description'],
149
    '#description' => t('Description and guidelines for forums within this container.')
150 151 152 153 154 155 156
  );
  $form['parent']['#tree'] = TRUE;
  $form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'), 'container');
  $form['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#default_value' => $edit['weight'],
157
    '#description' => t('Containers are displayed in ascending order by weight (containers with equal weights are displayed alphabetically).')
158 159 160 161 162 163
  );

  $form['vid'] = array(
    '#type' => 'hidden',
    '#value' => variable_get('forum_nav_vocabulary', ''),
  );
164
  $form['actions'] = array('#type' => 'actions');
165
  $form['actions']['submit'] = array(
166 167 168 169
    '#type' => 'submit',
    '#value' => t('Save')
  );
  if ($edit['tid']) {
170
    $form['actions']['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
171 172 173 174 175 176 177 178 179 180 181 182 183
    $form['tid'] = array('#type' => 'value', '#value' => $edit['tid']);
  }
  $form['#submit'][] = 'forum_form_submit';
  $form['#theme'] = 'forum_form';

  return $form;
}

/**
 * Returns a confirmation page for deleting a forum taxonomy term.
 *
 * @param $tid ID of the term to be deleted
 */
184
function forum_confirm_delete($form, &$form_state, $tid) {
185
  $term = taxonomy_term_load($tid);
186 187 188 189

  $form['tid'] = array('#type' => 'value', '#value' => $tid);
  $form['name'] = array('#type' => 'value', '#value' => $term->name);

190
  return confirm_form($form, t('Are you sure you want to delete the forum %name?', array('%name' => $term->name)), 'admin/structure/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'))), t('Delete'), t('Cancel'));
191 192 193
}

/**
194
 * Implement forms api _submit call. Deletes a forum after confirmation.
195 196
 */
function forum_confirm_delete_submit($form, &$form_state) {
197
  taxonomy_term_delete($form_state['values']['tid']);
198 199
  drupal_set_message(t('The forum %term and all sub-forums have been deleted.', array('%term' => $form_state['values']['name'])));
  watchdog('content', 'forum: deleted %term and all its sub-forums.', array('%term' => $form_state['values']['name']));
200

201
  $form_state['redirect'] = 'admin/structure/forum';
202 203 204 205 206 207
  return;
}

/**
 * Form builder for the forum settings page.
 *
208
 * @see system_settings_form()
209
 */
210
function forum_admin_settings($form) {
211 212 213
  $number = drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100, 150, 200, 250, 300, 350, 400, 500));
  $form['forum_hot_topic'] = array('#type' => 'select',
    '#title' => t('Hot topic threshold'),
214
    '#default_value' => variable_get('forum_hot_topic', 15),
215
    '#options' => $number,
216
    '#description' => t('The number of replies a topic must have to be considered "hot".'),
217 218 219 220
  );
  $number = drupal_map_assoc(array(10, 25, 50, 75, 100));
  $form['forum_per_page'] = array('#type' => 'select',
    '#title' => t('Topics per page'),
221
    '#default_value' => variable_get('forum_per_page', 25),
222
    '#options' => $number,
223
    '#description' => t('Default number of forum topics displayed per page.'),
224 225 226 227
  );
  $forder = array(1 => t('Date - newest first'), 2 => t('Date - oldest first'), 3 => t('Posts - most active first'), 4 => t('Posts - least active first'));
  $form['forum_order'] = array('#type' => 'radios',
    '#title' => t('Default order'),
228
    '#default_value' => variable_get('forum_order', 1),
229
    '#options' => $forder,
230
    '#description' => t('Default display order for topics.'),
231
  );
232
  return system_settings_form($form);
233 234 235 236 237
}

/**
 * Returns an overview list of existing forums and containers
 */
238
function forum_overview($form, &$form_state) {
239
  module_load_include('inc', 'taxonomy', 'taxonomy.admin');
240 241

  $vid = variable_get('forum_nav_vocabulary', '');
242
  $vocabulary = taxonomy_vocabulary_load($vid);
243
  $form = taxonomy_overview_terms($form, $form_state, $vocabulary);
244 245 246 247

  foreach (element_children($form) as $key) {
    if (isset($form[$key]['#term'])) {
      $term = $form[$key]['#term'];
248
      $form[$key]['view']['#href'] = 'forum/' . $term['tid'];
249
      if (in_array($form[$key]['#term']['tid'], variable_get('forum_containers', array()))) {
250 251
        $form[$key]['edit']['#title'] = t('edit container');
        $form[$key]['edit']['#href'] = 'admin/structure/forum/edit/container/' . $term['tid'];
252 253
      }
      else {
254 255
        $form[$key]['edit']['#title'] = t('edit forum');
        $form[$key]['edit']['#href'] = 'admin/structure/forum/edit/forum/' . $term['tid'];
256
      }
257 258
    }
  }
259

260
  // Remove the alphabetical reset.
261
  unset($form['actions']['reset_alphabetical']);
262 263 264 265

  // The form needs to have submit and validate handlers set explicitly.
  $form['#theme'] = 'taxonomy_overview_terms';
  $form['#submit'] = array('taxonomy_overview_terms_submit'); // Use the existing taxonomy overview submit handler.
266
  $form['#empty_text'] = t('No containers or forums available. <a href="@container">Add container</a> or <a href="@forum">Add forum</a>.', array('@container' => url('admin/structure/forum/add/container'), '@forum' => url('admin/structure/forum/add/forum')));
267
  return $form;
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

/**
 * Returns a select box for available parent terms
 *
 * @param $tid ID of the term which is being added or edited
 * @param $title Title to display the select box with
 * @param $child_type Whether the child is forum or container
 */
function _forum_parent_select($tid, $title, $child_type) {

  $parents = taxonomy_get_parents($tid);
  if ($parents) {
    $parent = array_shift($parents);
    $parent = $parent->tid;
  }
  else {
    $parent = 0;
  }

  $vid = variable_get('forum_nav_vocabulary', '');
  $children = taxonomy_get_tree($vid, $tid);

  // A term can't be the child of itself, nor of its children.
  foreach ($children as $child) {
    $exclude[] = $child->tid;
  }
  $exclude[] = $tid;

  $tree = taxonomy_get_tree($vid);
298
  $options[0] = '<' . t('root') . '>';
299 300 301 302 303 304 305 306
  if ($tree) {
    foreach ($tree as $term) {
      if (!in_array($term->tid, $exclude)) {
        $options[$term->tid] = str_repeat(' -- ', $term->depth) . $term->name;
      }
    }
  }
  if ($child_type == 'container') {
307
    $description = t('Containers are usually placed at the top (root) level, but may also be placed inside another container or forum.');
308
  }
309
  elseif ($child_type == 'forum') {
310
    $description = t('Forums may be placed at the top (root) level, or inside another container or forum.');
311 312 313 314
  }

  return array('#type' => 'select', '#title' => $title, '#default_value' => $parent, '#options' => $options, '#description' => $description, '#required' => TRUE);
}