forum.admin.inc

  1. drupal
    1. 6 modules/forum/forum.admin.inc
    2. 7 modules/forum/forum.admin.inc
    3. 8 core/modules/forum/forum.admin.inc

Administrative page callbacks for the forum module.

Functions & methods

NameDescription
forum_admin_settingsForm builder for the forum settings page.
forum_confirm_deleteReturns a confirmation page for deleting a forum taxonomy term.
forum_confirm_delete_submitImplement forms api _submit call. Deletes a forum after confirmation.
forum_form_containerReturns a form for adding a container to the forum vocabulary
forum_form_forumReturns a form for adding a forum to the forum vocabulary
forum_form_main@file Administrative page callbacks for the forum module.
forum_form_submitProcess forum form and container form submissions.
forum_overviewReturns an overview list of existing forums and containers
theme_forum_formReturns HTML for a forum form.
_forum_parent_selectReturns a select box for available parent terms

File

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