forum.admin.inc

  1. 7.x drupal-7.x/modules/forum/forum.admin.inc
  2. 6.x drupal-6.x/modules/forum/forum.admin.inc

Administrative page callbacks for the Forum module.

File

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