| 1 |
<?php
|
| 2 |
// $Id: forum.admin.inc,v 1.26 2009/10/08 07:58:45 webchick Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Administrative page callbacks for the forum module.
|
| 7 |
*/
|
| 8 |
function forum_form_main($type, $edit = array()) {
|
| 9 |
$edit = (array) $edit;
|
| 10 |
if ((isset($_POST['op']) && $_POST['op'] == t('Delete')) || !empty($_POST['confirm'])) {
|
| 11 |
return drupal_get_form('forum_confirm_delete', $edit['tid']);
|
| 12 |
}
|
| 13 |
switch ($type) {
|
| 14 |
case 'forum':
|
| 15 |
return drupal_get_form('forum_form_forum', $edit);
|
| 16 |
break;
|
| 17 |
case 'container':
|
| 18 |
return drupal_get_form('forum_form_container', $edit);
|
| 19 |
break;
|
| 20 |
}
|
| 21 |
}
|
| 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 |
*/
|
| 30 |
function forum_form_forum($form, &$form_state, $edit = array()) {
|
| 31 |
$edit += array(
|
| 32 |
'name' => '',
|
| 33 |
'description' => '',
|
| 34 |
'tid' => NULL,
|
| 35 |
'weight' => 0,
|
| 36 |
);
|
| 37 |
$form['name'] = array('#type' => 'textfield',
|
| 38 |
'#title' => t('Forum name'),
|
| 39 |
'#default_value' => $edit['name'],
|
| 40 |
'#maxlength' => 255,
|
| 41 |
'#description' => t('Short but meaningful name for this collection of threaded discussions.'),
|
| 42 |
'#required' => TRUE,
|
| 43 |
);
|
| 44 |
$form['description'] = array('#type' => 'textarea',
|
| 45 |
'#title' => t('Description'),
|
| 46 |
'#default_value' => $edit['description'],
|
| 47 |
'#description' => t('Description and guidelines for discussions within this forum.'),
|
| 48 |
);
|
| 49 |
$form['parent']['#tree'] = TRUE;
|
| 50 |
$form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'), 'forum');
|
| 51 |
$form['weight'] = array('#type' => 'weight',
|
| 52 |
'#title' => t('Weight'),
|
| 53 |
'#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['submit' ] = array('#type' => 'submit', '#value' => t('Save'));
|
| 59 |
if ($edit['tid']) {
|
| 60 |
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
|
| 61 |
$form['tid'] = array('#type' => 'hidden', '#value' => $edit['tid']);
|
| 62 |
}
|
| 63 |
$form['#submit'][] = 'forum_form_submit';
|
| 64 |
$form['#theme'] = 'forum_form';
|
| 65 |
|
| 66 |
return $form;
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Process forum form and container form submissions.
|
| 71 |
*/
|
| 72 |
function forum_form_submit($form, &$form_state) {
|
| 73 |
if ($form['form_id']['#value'] == 'forum_form_container') {
|
| 74 |
$container = TRUE;
|
| 75 |
$type = t('forum container');
|
| 76 |
}
|
| 77 |
else {
|
| 78 |
$container = FALSE;
|
| 79 |
$type = t('forum');
|
| 80 |
}
|
| 81 |
|
| 82 |
$term = (object) $form_state['values'];
|
| 83 |
$status = taxonomy_term_save($term);
|
| 84 |
switch ($status) {
|
| 85 |
case SAVED_NEW:
|
| 86 |
if ($container) {
|
| 87 |
$containers = variable_get('forum_containers', array());
|
| 88 |
$containers[] = $term->tid;
|
| 89 |
variable_set('forum_containers', $containers);
|
| 90 |
}
|
| 91 |
drupal_set_message(t('Created new @type %term.', array('%term' => $form_state['values']['name'], '@type' => $type)));
|
| 92 |
break;
|
| 93 |
case SAVED_UPDATED:
|
| 94 |
drupal_set_message(t('The @type %term has been updated.', array('%term' => $form_state['values']['name'], '@type' => $type)));
|
| 95 |
break;
|
| 96 |
}
|
| 97 |
$form_state['redirect'] = 'admin/structure/forum';
|
| 98 |
return;
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
* Theme forum forms.
|
| 103 |
*
|
| 104 |
* By default this does not alter the appearance of a form at all,
|
| 105 |
* but is provided as a convenience for themers.
|
| 106 |
*
|
| 107 |
* @param $variables
|
| 108 |
* An associative array containing:
|
| 109 |
* - form: An associative array containing the structure of the form.
|
| 110 |
* @ingroup themeable
|
| 111 |
*/
|
| 112 |
function theme_forum_form($variables) {
|
| 113 |
return drupal_render_children($variables['form']);
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
* Returns a form for adding a container to the forum vocabulary
|
| 118 |
*
|
| 119 |
* @param $edit Associative array containing a container term to be added or edited.
|
| 120 |
* @ingroup forms
|
| 121 |
* @see forum_form_submit()
|
| 122 |
*/
|
| 123 |
function forum_form_container($form, &$form_state, $edit = array()) {
|
| 124 |
$edit += array(
|
| 125 |
'name' => '',
|
| 126 |
'description' => '',
|
| 127 |
'tid' => NULL,
|
| 128 |
'weight' => 0,
|
| 129 |
);
|
| 130 |
// Handle a delete operation.
|
| 131 |
$form['name'] = array(
|
| 132 |
'#title' => t('Container name'),
|
| 133 |
'#type' => 'textfield',
|
| 134 |
'#default_value' => $edit['name'],
|
| 135 |
'#maxlength' => 255,
|
| 136 |
'#description' => t('Short but meaningful name for this collection of related forums.'),
|
| 137 |
'#required' => TRUE
|
| 138 |
);
|
| 139 |
|
| 140 |
$form['description'] = array(
|
| 141 |
'#type' => 'textarea',
|
| 142 |
'#title' => t('Description'),
|
| 143 |
'#default_value' => $edit['description'],
|
| 144 |
'#description' => t('Description and guidelines for forums within this container.')
|
| 145 |
);
|
| 146 |
$form['parent']['#tree'] = TRUE;
|
| 147 |
$form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'), 'container');
|
| 148 |
$form['weight'] = array(
|
| 149 |
'#type' => 'weight',
|
| 150 |
'#title' => t('Weight'),
|
| 151 |
'#default_value' => $edit['weight'],
|
| 152 |
'#description' => t('Containers are displayed in ascending order by weight (containers with equal weights are displayed alphabetically).')
|
| 153 |
);
|
| 154 |
|
| 155 |
$form['vid'] = array(
|
| 156 |
'#type' => 'hidden',
|
| 157 |
'#value' => variable_get('forum_nav_vocabulary', ''),
|
| 158 |
);
|
| 159 |
$form['submit'] = array(
|
| 160 |
'#type' => 'submit',
|
| 161 |
'#value' => t('Save')
|
| 162 |
);
|
| 163 |
if ($edit['tid']) {
|
| 164 |
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
|
| 165 |
$form['tid'] = array('#type' => 'value', '#value' => $edit['tid']);
|
| 166 |
}
|
| 167 |
$form['#submit'][] = 'forum_form_submit';
|
| 168 |
$form['#theme'] = 'forum_form';
|
| 169 |
|
| 170 |
return $form;
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* Returns a confirmation page for deleting a forum taxonomy term.
|
| 175 |
*
|
| 176 |
* @param $tid ID of the term to be deleted
|
| 177 |
*/
|
| 178 |
function forum_confirm_delete($form, &$form_state, $tid) {
|
| 179 |
$term = taxonomy_term_load($tid);
|
| 180 |
|
| 181 |
$form['tid'] = array('#type' => 'value', '#value' => $tid);
|
| 182 |
$form['name'] = array('#type' => 'value', '#value' => $term->name);
|
| 183 |
|
| 184 |
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'));
|
| 185 |
}
|
| 186 |
|
| 187 |
/**
|
| 188 |
* Implement forms api _submit call. Deletes a forum after confirmation.
|
| 189 |
*/
|
| 190 |
function forum_confirm_delete_submit($form, &$form_state) {
|
| 191 |
taxonomy_term_delete($form_state['values']['tid']);
|
| 192 |
drupal_set_message(t('The forum %term and all sub-forums have been deleted.', array('%term' => $form_state['values']['name'])));
|
| 193 |
watchdog('content', 'forum: deleted %term and all its sub-forums.', array('%term' => $form_state['values']['name']));
|
| 194 |
|
| 195 |
$form_state['redirect'] = 'admin/structure/forum';
|
| 196 |
return;
|
| 197 |
}
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Form builder for the forum settings page.
|
| 201 |
*
|
| 202 |
* @see system_settings_form()
|
| 203 |
*/
|
| 204 |
function forum_admin_settings($form) {
|
| 205 |
$number = drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100, 150, 200, 250, 300, 350, 400, 500));
|
| 206 |
$form['forum_hot_topic'] = array('#type' => 'select',
|
| 207 |
'#title' => t('Hot topic threshold'),
|
| 208 |
'#default_value' => 15,
|
| 209 |
'#options' => $number,
|
| 210 |
'#description' => t('The number of replies a topic must have to be considered "hot".'),
|
| 211 |
);
|
| 212 |
$number = drupal_map_assoc(array(10, 25, 50, 75, 100));
|
| 213 |
$form['forum_per_page'] = array('#type' => 'select',
|
| 214 |
'#title' => t('Topics per page'),
|
| 215 |
'#default_value' => 25,
|
| 216 |
'#options' => $number,
|
| 217 |
'#description' => t('Default number of forum topics displayed per page.'),
|
| 218 |
);
|
| 219 |
$forder = array(1 => t('Date - newest first'), 2 => t('Date - oldest first'), 3 => t('Posts - most active first'), 4 => t('Posts - least active first'));
|
| 220 |
$form['forum_order'] = array('#type' => 'radios',
|
| 221 |
'#title' => t('Default order'),
|
| 222 |
'#default_value' => '1',
|
| 223 |
'#options' => $forder,
|
| 224 |
'#description' => t('Default display order for topics.'),
|
| 225 |
);
|
| 226 |
return system_settings_form($form, TRUE);
|
| 227 |
}
|
| 228 |
|
| 229 |
/**
|
| 230 |
* Returns an overview list of existing forums and containers
|
| 231 |
*/
|
| 232 |
function forum_overview($form, &$form_state) {
|
| 233 |
module_load_include('inc', 'taxonomy', 'taxonomy.admin');
|
| 234 |
|
| 235 |
$vid = variable_get('forum_nav_vocabulary', '');
|
| 236 |
$vocabulary = taxonomy_vocabulary_load($vid);
|
| 237 |
$form = taxonomy_overview_terms($form, $form_state, $vocabulary);
|
| 238 |
|
| 239 |
foreach (element_children($form) as $key) {
|
| 240 |
if (isset($form[$key]['#term'])) {
|
| 241 |
$term = $form[$key]['#term'];
|
| 242 |
$form[$key]['view']['#markup'] = l($term['name'], 'forum/' . $term['tid']);
|
| 243 |
if (in_array($form[$key]['#term']['tid'], variable_get('forum_containers', array()))) {
|
| 244 |
$form[$key]['edit']['#markup'] = l(t('edit container'), 'admin/structure/forum/edit/container/' . $term['tid']);
|
| 245 |
}
|
| 246 |
else {
|
| 247 |
$form[$key]['edit']['#markup'] = l(t('edit forum'), 'admin/structure/forum/edit/forum/' . $term['tid']);
|
| 248 |
}
|
| 249 |
}
|
| 250 |
}
|
| 251 |
|
| 252 |
// Remove the alphabetical reset.
|
| 253 |
unset($form['reset_alphabetical']);
|
| 254 |
|
| 255 |
// The form needs to have submit and validate handlers set explicitly.
|
| 256 |
$form['#theme'] = 'taxonomy_overview_terms';
|
| 257 |
$form['#submit'] = array('taxonomy_overview_terms_submit'); // Use the existing taxonomy overview submit handler.
|
| 258 |
$form['#validate'] = array('taxonomy_overview_terms_validate');
|
| 259 |
$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')));
|
| 260 |
return $form;
|
| 261 |
}
|
| 262 |
|
| 263 |
/**
|
| 264 |
* Returns a select box for available parent terms
|
| 265 |
*
|
| 266 |
* @param $tid ID of the term which is being added or edited
|
| 267 |
* @param $title Title to display the select box with
|
| 268 |
* @param $child_type Whether the child is forum or container
|
| 269 |
*/
|
| 270 |
function _forum_parent_select($tid, $title, $child_type) {
|
| 271 |
|
| 272 |
$parents = taxonomy_get_parents($tid);
|
| 273 |
if ($parents) {
|
| 274 |
$parent = array_shift($parents);
|
| 275 |
$parent = $parent->tid;
|
| 276 |
}
|
| 277 |
else {
|
| 278 |
$parent = 0;
|
| 279 |
}
|
| 280 |
|
| 281 |
$vid = variable_get('forum_nav_vocabulary', '');
|
| 282 |
$children = taxonomy_get_tree($vid, $tid);
|
| 283 |
|
| 284 |
// A term can't be the child of itself, nor of its children.
|
| 285 |
foreach ($children as $child) {
|
| 286 |
$exclude[] = $child->tid;
|
| 287 |
}
|
| 288 |
$exclude[] = $tid;
|
| 289 |
|
| 290 |
$tree = taxonomy_get_tree($vid);
|
| 291 |
$options[0] = '<' . t('root') . '>';
|
| 292 |
if ($tree) {
|
| 293 |
foreach ($tree as $term) {
|
| 294 |
if (!in_array($term->tid, $exclude)) {
|
| 295 |
$options[$term->tid] = str_repeat(' -- ', $term->depth) . $term->name;
|
| 296 |
}
|
| 297 |
}
|
| 298 |
}
|
| 299 |
if ($child_type == 'container') {
|
| 300 |
$description = t('Containers are usually placed at the top (root) level, but may also be placed inside another container or forum.');
|
| 301 |
}
|
| 302 |
elseif ($child_type == 'forum') {
|
| 303 |
$description = t('Forums may be placed at the top (root) level, or inside another container or forum.');
|
| 304 |
}
|
| 305 |
|
| 306 |
return array('#type' => 'select', '#title' => $title, '#default_value' => $parent, '#options' => $options, '#description' => $description, '#required' => TRUE);
|
| 307 |
}
|