Project

General

Profile

Paste
Download (11.7 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / forum / forum.admin.inc @ bd0d4c5c

1
<?php
2

    
3
/**
4
 * @file
5
 * Administrative page callbacks for the Forum module.
6
 */
7

    
8
/**
9
 * Page callback: Returns a form for creating a new forum or container.
10
 *
11
 * @param $type
12
 *   What is being added. Possible values are 'forum' and 'container'.
13
 * @param $edit
14
 *   (optional) Associative array containing a forum term to be edited.
15
 *   Defaults to an empty array.
16
 *
17
 * @return
18
 *   A form for creating a new forum or container.
19
 *
20
 * @see forum_menu()
21
 */
22
function forum_form_main($type, $edit = array()) {
23
  $edit = (array) $edit;
24
  if ((isset($_POST['op']) && $_POST['op'] == t('Delete')) || !empty($_POST['confirm'])) {
25
    return drupal_get_form('forum_confirm_delete', $edit['tid']);
26
  }
27
  switch ($type) {
28
    case 'forum':
29
      return drupal_get_form('forum_form_forum', $edit);
30
      break;
31
    case 'container':
32
      return drupal_get_form('forum_form_container', $edit);
33
      break;
34
  }
35
}
36

    
37
/**
38
 * Form constructor for adding and editing a forum.
39
 *
40
 * @param $edit
41
 *   (optional) Associative array containing a forum term to be added or edited.
42
 *   Defaults to an empty array.
43
 *
44
 * @see forum_form_submit()
45
 * @ingroup forms
46
 */
47
function forum_form_forum($form, &$form_state, $edit = array()) {
48
  $edit += array(
49
    'name' => '',
50
    'description' => '',
51
    'tid' => NULL,
52
    'weight' => 0,
53
  );
54
  $form['name'] = array('#type' => 'textfield',
55
    '#title' => t('Forum name'),
56
    '#default_value' => $edit['name'],
57
    '#maxlength' => 255,
58
    '#description' => t('Short but meaningful name for this collection of threaded discussions.'),
59
    '#required' => TRUE,
60
  );
61
  $form['description'] = array('#type' => 'textarea',
62
    '#title' => t('Description'),
63
    '#default_value' => $edit['description'],
64
    '#description' => t('Description and guidelines for discussions within this forum.'),
65
  );
66
  $form['parent']['#tree'] = TRUE;
67
  $form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'), 'forum');
68
  $form['weight'] = array('#type' => 'weight',
69
    '#title' => t('Weight'),
70
    '#default_value' => $edit['weight'],
71
    '#description' => t('Forums are displayed in ascending order by weight (forums with equal weights are displayed alphabetically).'),
72
  );
73

    
74
  $form['vid'] = array('#type' => 'hidden', '#value' => variable_get('forum_nav_vocabulary', ''));
75
  $form['actions'] = array('#type' => 'actions');
76
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
77
  if ($edit['tid']) {
78
    $form['actions']['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
79
    $form['tid'] = array('#type' => 'hidden', '#value' => $edit['tid']);
80
  }
81
  $form['#submit'][] = 'forum_form_submit';
82
  $form['#theme'] = 'forum_form';
83

    
84
  return $form;
85
}
86

    
87
/**
88
 * Form submission handler for forum_form_forum() and forum_form_container().
89
 */
90
function forum_form_submit($form, &$form_state) {
91
  if ($form['form_id']['#value'] == 'forum_form_container') {
92
    $container = TRUE;
93
    $type = t('forum container');
94
  }
95
  else {
96
    $container = FALSE;
97
    $type = t('forum');
98
  }
99

    
100
  $term = (object) $form_state['values'];
101
  $status = taxonomy_term_save($term);
102
  switch ($status) {
103
    case SAVED_NEW:
104
      if ($container) {
105
        $containers = variable_get('forum_containers', array());
106
        $containers[] = $term->tid;
107
        variable_set('forum_containers', $containers);
108
      }
109
      $form_state['values']['tid'] = $term->tid;
110
      drupal_set_message(t('Created new @type %term.', array('%term' => $form_state['values']['name'], '@type' => $type)));
111
      break;
112
    case SAVED_UPDATED:
113
      drupal_set_message(t('The @type %term has been updated.', array('%term' => $form_state['values']['name'], '@type' => $type)));
114
      // Clear the page and block caches to avoid stale data.
115
      cache_clear_all();
116
      break;
117
  }
118
  $form_state['redirect'] = 'admin/structure/forum';
119
  return;
120
}
121

    
122
/**
123
 * Returns HTML for a forum form.
124
 *
125
 * By default this does not alter the appearance of a form at all, but is
126
 * provided as a convenience for themers.
127
 *
128
 * @param $variables
129
 *   An associative array containing:
130
 *   - form: A render element representing the form.
131
 *
132
 * @ingroup themeable
133
 */
134
function theme_forum_form($variables) {
135
  return drupal_render_children($variables['form']);
136
}
137

    
138
/**
139
 * Form constructor for adding and editing forum containers.
140
 *
141
 * @param $edit
142
 *   (optional) Associative array containing a container term to be added or edited.
143
 *   Defaults to an empty array.
144
 *
145
 * @see forum_form_submit()
146
 * @ingroup forms
147
 */
148
function forum_form_container($form, &$form_state, $edit = array()) {
149
  $edit += array(
150
    'name' => '',
151
    'description' => '',
152
    'tid' => NULL,
153
    'weight' => 0,
154
  );
155
  // Handle a delete operation.
156
  $form['name'] = array(
157
    '#title' => t('Container name'),
158
    '#type' => 'textfield',
159
    '#default_value' => $edit['name'],
160
    '#maxlength' => 255,
161
    '#description' => t('Short but meaningful name for this collection of related forums.'),
162
    '#required' => TRUE
163
  );
164

    
165
  $form['description'] = array(
166
    '#type' => 'textarea',
167
    '#title' => t('Description'),
168
    '#default_value' => $edit['description'],
169
    '#description' => t('Description and guidelines for forums within this container.')
170
  );
171
  $form['parent']['#tree'] = TRUE;
172
  $form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'), 'container');
173
  $form['weight'] = array(
174
    '#type' => 'weight',
175
    '#title' => t('Weight'),
176
    '#default_value' => $edit['weight'],
177
    '#description' => t('Containers are displayed in ascending order by weight (containers with equal weights are displayed alphabetically).')
178
  );
179

    
180
  $form['vid'] = array(
181
    '#type' => 'hidden',
182
    '#value' => variable_get('forum_nav_vocabulary', ''),
183
  );
184
  $form['actions'] = array('#type' => 'actions');
185
  $form['actions']['submit'] = array(
186
    '#type' => 'submit',
187
    '#value' => t('Save')
188
  );
189
  if ($edit['tid']) {
190
    $form['actions']['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
191
    $form['tid'] = array('#type' => 'value', '#value' => $edit['tid']);
192
  }
193
  $form['#submit'][] = 'forum_form_submit';
194
  $form['#theme'] = 'forum_form';
195

    
196
  return $form;
197
}
198

    
199
/**
200
 * Form constructor for confirming deletion of a forum taxonomy term.
201
 *
202
 * @param $tid
203
 *   ID of the term to be deleted.
204
 *
205
 * @see forum_confirm_delete_submit()
206
 * @ingroup forms
207
 */
208
function forum_confirm_delete($form, &$form_state, $tid) {
209
  $term = taxonomy_term_load($tid);
210

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

    
214
  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'));
215
}
216

    
217
/**
218
 * Form submission handler for forum_confirm_delete().
219
 */
220
function forum_confirm_delete_submit($form, &$form_state) {
221
  taxonomy_term_delete($form_state['values']['tid']);
222
  drupal_set_message(t('The forum %term and all sub-forums have been deleted.', array('%term' => $form_state['values']['name'])));
223
  watchdog('content', 'forum: deleted %term and all its sub-forums.', array('%term' => $form_state['values']['name']));
224

    
225
  $form_state['redirect'] = 'admin/structure/forum';
226
  return;
227
}
228

    
229
/**
230
 * Form constructor for the forum settings page.
231
 *
232
 * @see forum_menu()
233
 * @see system_settings_form()
234
 * @ingroup forms
235
 */
236
function forum_admin_settings($form) {
237
  $number = drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100, 150, 200, 250, 300, 350, 400, 500));
238
  $form['forum_hot_topic'] = array('#type' => 'select',
239
    '#title' => t('Hot topic threshold'),
240
    '#default_value' => variable_get('forum_hot_topic', 15),
241
    '#options' => $number,
242
    '#description' => t('The number of replies a topic must have to be considered "hot".'),
243
  );
244
  $number = drupal_map_assoc(array(10, 25, 50, 75, 100));
245
  $form['forum_per_page'] = array('#type' => 'select',
246
    '#title' => t('Topics per page'),
247
    '#default_value' => variable_get('forum_per_page', 25),
248
    '#options' => $number,
249
    '#description' => t('Default number of forum topics displayed per page.'),
250
  );
251
  $forder = array(1 => t('Date - newest first'), 2 => t('Date - oldest first'), 3 => t('Posts - most active first'), 4 => t('Posts - least active first'));
252
  $form['forum_order'] = array('#type' => 'radios',
253
    '#title' => t('Default order'),
254
    '#default_value' => variable_get('forum_order', 1),
255
    '#options' => $forder,
256
    '#description' => t('Default display order for topics.'),
257
  );
258
  return system_settings_form($form);
259
}
260

    
261
/**
262
 * Form constructor for the forum overview form.
263
 *
264
 * Returns a form for controlling the hierarchy of existing forums and
265
 * containers.
266
 *
267
 * @see forum_menu()
268
 * @ingroup forms
269
 */
270
function forum_overview($form, &$form_state) {
271
  module_load_include('inc', 'taxonomy', 'taxonomy.admin');
272

    
273
  $vid = variable_get('forum_nav_vocabulary', '');
274
  $vocabulary = taxonomy_vocabulary_load($vid);
275
  $form = taxonomy_overview_terms($form, $form_state, $vocabulary);
276

    
277
  foreach (element_children($form) as $key) {
278
    if (isset($form[$key]['#term'])) {
279
      $term = $form[$key]['#term'];
280
      $form[$key]['view']['#href'] = 'forum/' . $term['tid'];
281
      if (in_array($form[$key]['#term']['tid'], variable_get('forum_containers', array()))) {
282
        $form[$key]['edit']['#title'] = t('edit container');
283
        $form[$key]['edit']['#href'] = 'admin/structure/forum/edit/container/' . $term['tid'];
284
      }
285
      else {
286
        $form[$key]['edit']['#title'] = t('edit forum');
287
        $form[$key]['edit']['#href'] = 'admin/structure/forum/edit/forum/' . $term['tid'];
288
      }
289
    }
290
  }
291

    
292
  // Remove the alphabetical reset.
293
  unset($form['actions']['reset_alphabetical']);
294

    
295
  // The form needs to have submit and validate handlers set explicitly.
296
  $form['#theme'] = 'taxonomy_overview_terms';
297
  $form['#submit'] = array('taxonomy_overview_terms_submit'); // Use the existing taxonomy overview submit handler.
298
  $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')));
299
  return $form;
300
}
301

    
302
/**
303
 * Returns a select box for available parent terms.
304
 *
305
 * @param $tid
306
 *   ID of the term that is being added or edited.
307
 * @param $title
308
 *   Title for the select box.
309
 * @param $child_type
310
 *   Whether the child is a forum or a container.
311
 *
312
 * @return
313
 *   A select form element.
314
 */
315
function _forum_parent_select($tid, $title, $child_type) {
316

    
317
  $parents = taxonomy_get_parents($tid);
318
  if ($parents) {
319
    $parent = array_shift($parents);
320
    $parent = $parent->tid;
321
  }
322
  else {
323
    $parent = 0;
324
  }
325

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

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

    
335
  $tree = taxonomy_get_tree($vid);
336
  $options[0] = '<' . t('root') . '>';
337
  if ($tree) {
338
    foreach ($tree as $term) {
339
      if (!in_array($term->tid, $exclude)) {
340
        $options[$term->tid] = str_repeat(' -- ', $term->depth) . $term->name;
341
      }
342
    }
343
  }
344
  if ($child_type == 'container') {
345
    $description = t('Containers are usually placed at the top (root) level, but may also be placed inside another container or forum.');
346
  }
347
  elseif ($child_type == 'forum') {
348
    $description = t('Forums may be placed at the top (root) level, or inside another container or forum.');
349
  }
350

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