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