2001-12-05 18:46:24 +00:00
<?php
// $Id$
2004-08-21 06:42:38 +00:00
/**
* @file
* Enable threaded discussions about general topics.
*/
2004-05-09 19:28:43 +00:00
/**
* Implementation of hook_help().
*/
function forum_help($section) {
2004-01-23 18:42:43 +00:00
switch ($section) {
2005-11-01 10:17:34 +00:00
case 'admin/help#forum':
$output = '<p>'. t('The forum module lets you create threaded discussion forums for a particular topic on your site. This is similar to a message board system such as phpBB. Forums are very useful because they allow community members to discuss topics with one another, and they are archived for future reference.') .'</p>';
2006-05-07 00:08:36 +00:00
$output .= '<p>'. t('Forums can be organized under what are called <em>containers</em>. Containers hold forums and, in turn, forums hold threaded discussions. Both containers and forums can be placed inside other containers and forums. By planning the structure of your containers and forums well, you make it easier for users to find a topic area of interest to them. Forum topics can be moved by selecting a different forum and can be left in the existing forum by selecting <em>leave a shadow copy</em>. Forum topics can also have their own URL.') .'</p>';
2005-11-01 10:17:34 +00:00
$output .= '<p>'. t('Forums module <strong>requires Taxonomy and Comments module</strong> be enabled.') .'</p>';
2006-08-18 12:17:00 +00:00
$output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@forum">Forum page</a>.', array('@forum' => 'http://drupal.org/handbook/modules/forum/')) .'</p>';
2005-11-01 10:17:34 +00:00
return $output;
2006-07-31 11:25:55 +00:00
case 'admin/content/forum':
2006-11-26 02:20:01 +00:00
return '<p>'. t('This is a list of existing containers and forums that you can edit. Containers hold forums and, in turn, forums hold threaded discussions. Both containers and forums can be placed inside other containers and forums. By planning the structure of your containers and forums well, you make it easier for users to find a topic area of interest to them.') .'</p>';
2006-07-31 11:25:55 +00:00
case 'admin/content/forum/add/container':
2006-11-26 02:20:01 +00:00
return '<p>'. t('Containers help you organize your forums. The job of a container is to hold, or contain, other forums that are related. For example, a container named "Food" might hold two forums named "Fruit" and "Vegetables".') .'</p>';
2006-07-31 11:25:55 +00:00
case 'admin/content/forum/add/forum':
2006-11-26 02:20:01 +00:00
return '<p>'. t('A forum holds discussion topics that are related. For example, a forum named "Fruit" might contain topics titled "Apples" and "Bananas".') .'</p>';
2006-11-28 09:48:45 +00:00
case 'admin/content/forum/settings':
return '<p>'. t('These settings provide the ability to fine tune the display of your forum topics.') .'</p>';
2004-01-23 18:42:43 +00:00
}
}
2006-03-07 19:03:02 +00:00
/**
* Implementation of hook_menu().
*/
2007-01-24 14:48:36 +00:00
function forum_menu() {
$items['node/add/forum'] = array(
'title' => t('Forum topic'),
'access arguments' => array('create forum topics'),
);
$items['forum'] = array(
'title' => t('Forums'),
'page callback' => 'forum_page',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
);
$items['admin/content/forum'] = array(
'title' => t('Forums'),
'description' => t('Control forums and their hierarchy and change forum settings.'),
'page callback' => 'forum_overview',
'access arguments' => array('administer forums'),
);
$items['admin/content/forum/list'] = array(
'title' => t('List'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/content/forum/add/container'] = array(
'title' => t('Add container'),
'page callback' => 'forum_form_main',
'page arguments' => array('container'),
'type' => MENU_LOCAL_TASK,
2007-01-31 21:26:56 +00:00
'parent' => 'admin/content/forum',
2007-01-24 14:48:36 +00:00
);
$items['admin/content/forum/add/forum'] = array(
'title' => t('Add forum'),
'page callback' => 'forum_form_main',
'page arguments' => array('forum'),
'type' => MENU_LOCAL_TASK,
2007-01-31 21:26:56 +00:00
'parent' => 'admin/content/forum',
2007-01-24 14:48:36 +00:00
);
$items['admin/content/forum/settings'] = array(
'title' => t('Settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('forum_admin_settings'),
'weight' => 5,
'type' => MENU_LOCAL_TASK,
2007-01-31 21:26:56 +00:00
'parent' => 'admin/content/forum',
2007-01-24 14:48:36 +00:00
);
2007-02-11 09:30:51 +00:00
$items['admin/content/forum/edit/%forum_term'] = array(
2007-01-24 14:48:36 +00:00
'page callback' => 'forum_form_main',
'type' => MENU_CALLBACK,
);
2007-02-11 09:30:51 +00:00
$items['admin/content/forum/edit/container/%forum_term'] = array(
2007-01-24 14:48:36 +00:00
'title' => t('Edit container'),
2007-02-11 09:30:51 +00:00
'page callback' => 'forum_form_main',
2007-01-24 14:48:36 +00:00
'page arguments' => array('container', 5),
2007-02-11 09:30:51 +00:00
'type' => MENU_CALLBACK,
2007-01-24 14:48:36 +00:00
);
2007-02-11 09:30:51 +00:00
$items['admin/content/forum/edit/forum/%forum_term'] = array(
2007-01-24 14:48:36 +00:00
'title' => t('Edit forum'),
'page callback' => 'forum_form_main',
'page arguments' => array('forum', 5),
2007-02-11 09:30:51 +00:00
'type' => MENU_CALLBACK,
2007-01-24 14:48:36 +00:00
);
return $items;
}
2006-03-07 19:03:02 +00:00
2007-01-24 14:48:36 +00:00
function forum_init() {
drupal_add_css(drupal_get_path('module', 'forum') .'/forum.css');
}
2006-03-07 19:03:02 +00:00
2007-02-11 09:30:51 +00:00
function forum_term_load($tid) {
2007-01-24 14:48:36 +00:00
return (array)taxonomy_get_term($tid);
2006-03-07 19:03:02 +00:00
}
2004-05-09 19:28:43 +00:00
/**
2005-08-29 19:58:49 +00:00
* Implementation of hook_node_info().
2004-05-09 19:28:43 +00:00
*/
2005-08-29 19:58:49 +00:00
function forum_node_info() {
2006-08-06 23:00:42 +00:00
return array(
'forum' => array(
2006-10-22 08:28:47 +00:00
'name' => t('Forum topic'),
2006-08-06 23:00:42 +00:00
'module' => 'forum',
'description' => t('Create a new topic for discussion in the forums.'),
'title_label' => t('Subject'),
)
);
2001-12-05 18:46:24 +00:00
}
2004-05-09 19:28:43 +00:00
/**
* Implementation of hook_access().
*/
2001-12-05 18:46:24 +00:00
function forum_access($op, $node) {
2004-11-07 19:28:24 +00:00
global $user;
2004-02-01 21:32:01 +00:00
if ($op == 'create') {
return user_access('create forum topics');
2002-09-15 13:00:12 +00:00
}
2004-11-07 19:28:24 +00:00
if ($op == 'update' || $op == 'delete') {
if (user_access('edit own forum topics') && ($user->uid == $node->uid)) {
return TRUE;
}
}
2001-12-05 18:46:24 +00:00
}
2004-05-09 19:28:43 +00:00
/**
* Implementation of hook_perm().
*/
2002-09-15 13:00:12 +00:00
function forum_perm() {
2005-01-28 15:47:03 +00:00
return array('create forum topics', 'edit own forum topics', 'administer forums');
2002-09-15 13:00:12 +00:00
}
2001-12-06 12:31:21 +00:00
2005-12-05 16:07:18 +00:00
/**
* Implementation of hook_nodeapi().
*/
function forum_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
2005-12-31 13:04:40 +00:00
case 'delete revision':
2005-12-05 16:07:18 +00:00
db_query('DELETE FROM {forum} WHERE vid = %d', $node->vid);
break;
}
}
2005-01-28 15:47:03 +00:00
/**
* Implementation of hook_taxonomy().
*/
2006-03-23 21:46:35 +00:00
function forum_taxonomy($op, $type, $term = NULL) {
2006-04-06 04:21:12 +00:00
if ($op == 'delete' && $term['vid'] == _forum_get_vid()) {
2006-03-23 21:46:35 +00:00
switch ($type) {
case 'term':
2006-04-06 04:21:12 +00:00
$results = db_query('SELECT f.nid FROM {forum} f WHERE f.tid = %d', $term['tid']);
2006-03-23 21:46:35 +00:00
while ($node = db_fetch_object($results)) {
// node_delete will also remove any association with non-forum vocabularies.
node_delete($node->nid);
}
// For containers, remove the tid from the forum_containers variable.
$containers = variable_get('forum_containers', array());
2006-05-08 15:16:16 +00:00
$key = array_search($term['tid'], $containers);
if ($key !== FALSE) {
2006-03-23 21:46:35 +00:00
unset($containers[$key]);
}
variable_set('forum_containers', $containers);
break;
case 'vocabulary':
variable_del('forum_nav_vocabulary');
2002-12-18 21:43:03 +00:00
}
2005-01-28 15:47:03 +00:00
}
}
2002-12-18 21:43:03 +00:00
2006-07-10 19:27:52 +00:00
function forum_admin_settings() {
2005-10-07 06:11:12 +00:00
$form = array();
2006-04-06 04:18:30 +00:00
$number = drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100, 150, 200, 250, 300, 350, 400, 500));
2006-03-07 19:03:02 +00:00
$form['forum_hot_topic'] = array('#type' => 'select',
'#title' => t('Hot topic threshold'),
'#default_value' => variable_get('forum_hot_topic', 15),
'#options' => $number,
'#description' => t('The number of posts a topic must have to be considered hot.'),
);
2005-01-28 15:47:03 +00:00
$number = drupal_map_assoc(array(10, 25, 50, 75, 100));
2006-03-07 19:03:02 +00:00
$form['forum_per_page'] = array('#type' => 'select',
'#title' => t('Topics per page'),
'#default_value' => variable_get('forum_per_page', 25),
'#options' => $number,
'#description' => t('The default number of topics displayed per page; links to browse older messages are automatically being displayed.'),
);
2005-01-28 15:47:03 +00:00
$forder = array(1 => t('Date - newest first'), 2 => t('Date - oldest first'), 3 => t('Posts - most active first'), 4=> t('Posts - least active first'));
2006-03-07 19:03:02 +00:00
$form['forum_order'] = array('#type' => 'radios',
'#title' => t('Default order'),
'#default_value' => variable_get('forum_order', '1'),
'#options' => $forder,
'#description' => t('The default display order for topics.'),
);
2006-08-18 18:58:47 +00:00
return system_settings_form($form);
2005-01-28 15:47:03 +00:00
}
2006-03-31 06:43:46 +00:00
/**
* Implementation of hook_form_alter().
*/
function forum_form_alter($form_id, &$form) {
// hide critical options from forum vocabulary
if ($form_id == 'taxonomy_form_vocabulary') {
if ($form['vid']['#value'] == _forum_get_vid()) {
$form['help_forum_vocab'] = array(
'#value' => t('This is the designated forum vocabulary. Some of the normal vocabulary options have been removed.'),
'#weight' => -1,
);
$form['nodes']['forum'] = array('#type' => 'checkbox', '#value' => 1, '#title' => t('forum topic'), '#attributes' => array('disabled' => '' ), '#description' => t('forum topic is affixed to the forum vocabulary.'));
$form['hierarchy'] = array('#type' => 'value', '#value' => 1);
unset($form['relations']);
unset($form['tags']);
unset($form['multiple']);
$form['required'] = array('#type' => 'value', '#value' => 1);
}
else {
unset($form['nodes']['forum']);
}
}
}
2004-05-09 19:28:43 +00:00
/**
* Implementation of hook_load().
*/
2002-09-15 13:00:12 +00:00
function forum_load($node) {
2005-08-30 15:22:29 +00:00
$forum = db_fetch_object(db_query('SELECT * FROM {forum} WHERE vid = %d', $node->vid));
2002-09-15 13:00:12 +00:00
return $forum;
}
2004-05-09 19:28:43 +00:00
/**
* Implementation of hook_block().
*
* Generates a block containing the currently active forum topics and the
* most recently added forum topics.
*/
2004-10-31 07:34:47 +00:00
function forum_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
2005-02-10 19:14:50 +00:00
$blocks[0]['info'] = t('Active forum topics');
$blocks[1]['info'] = t('New forum topics');
2004-10-31 07:34:47 +00:00
return $blocks;
2004-03-20 13:23:34 +00:00
2004-10-31 07:34:47 +00:00
case 'configure':
2005-10-11 19:44:35 +00:00
$form['forum_block_num_'. $delta] = array('#type' => 'select', '#title' => t('Number of topics'), '#default_value' => variable_get('forum_block_num_'. $delta, '5'), '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)));
2005-10-07 06:11:12 +00:00
return $form;
2002-11-01 10:47:20 +00:00
2004-10-31 07:34:47 +00:00
case 'save':
2005-03-03 20:02:07 +00:00
variable_set('forum_block_num_'. $delta, $edit['forum_block_num_'. $delta]);
2004-10-31 07:34:47 +00:00
break;
2002-11-09 13:59:36 +00:00
2004-10-31 07:34:47 +00:00
case 'view':
if (user_access('access content')) {
2005-02-10 19:14:50 +00:00
switch ($delta) {
case 0:
2005-03-03 20:02:07 +00:00
$title = t('Active forum topics');
2006-02-04 09:56:04 +00:00
$sql = db_rewrite_sql("SELECT n.nid, n.title, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 AND n.type = 'forum' ORDER BY l.last_comment_timestamp DESC");
2005-05-25 03:59:38 +00:00
$result = db_query_range($sql, 0, variable_get('forum_block_num_0', '5'));
if (db_num_rows($result)) {
$content = node_title_list($result);
}
2005-03-03 20:02:07 +00:00
break;
2005-02-10 19:14:50 +00:00
case 1:
2005-03-03 20:02:07 +00:00
$title = t('New forum topics');
$sql = db_rewrite_sql("SELECT n.nid, n.title, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.type = 'forum' AND n.status = 1 ORDER BY n.nid DESC");
2005-05-25 03:59:38 +00:00
$result = db_query_range($sql, 0, variable_get('forum_block_num_1', '5'));
if (db_num_rows($result)) {
$content = node_title_list($result);
}
2005-03-03 20:02:07 +00:00
break;
2005-02-10 19:14:50 +00:00
}
2002-10-16 18:23:39 +00:00
2004-10-31 07:34:47 +00:00
if ($content) {
2005-11-26 19:45:31 +00:00
$content .= '<div class="more-link">'. l(t('more'), 'forum', array('title' => t('Read the latest forum topics.'))) .'</div>';
2004-10-31 07:34:47 +00:00
}
2005-02-10 19:14:50 +00:00
$block['subject'] = $title;
2004-10-31 07:34:47 +00:00
$block['content'] = $content;
return $block;
}
}
2002-09-15 13:00:12 +00:00
}
2004-05-09 19:28:43 +00:00
/**
* Implementation of hook_view().
*/
- Patch #5347 by JonBob:
Here's a new patch that unifies the node/52 and book/view/52 paths for nodes. It involves a small change to hook_view(), which is discussed first:
Currently hook_view() expects node modules to return a themed node. However, each module does this the same way; they modify $node as necessary, then call theme('node', $node) and return the result. We can refactor this so that the calling function node_view() calls theme('node') instead. By doing this, it becomes possible for hook_nodeapi('view') to be called after hook_view() where the node contents are filtered, and before theme('node') where the body is enclosed in other HTML. This way the book module can insert its navigation into the body right before the theming.
Advantages of this refactoring:
- I can use it for book.module to remove the extra viewing path.
- The function of hook_nodeapi('view') becomes more like hook_view(), as neither will expect a return value.
- We more closely follow the flow of other nodeapi calls, which usually directly follow their corresponding specific node type hooks (instead of preceding them).
- The attachment.module people could use it to append their attachments in a list after the node.
- Gabor could use it instead of his filter perversion for his "articles in a series" module.
- A little less code in each view hook.
- The content hook is no longer needed, so that means even less code.
Disadvantages:
- Any modules written to use nodeapi('view') could be affected (but these would all be post-4.4 modules).
- Implementations of hook_view() would need to be updated (but return values would be ignored, so most would work without updates anyway).
Now the patch takes advantage of this API shift to inject its navigation at the end of all book nodes, regardless of the viewing path. In fact, since the paths become identical, I've removed the book/view handler entirely. We should probably provide an .htaccess rewrite for this (one is still needed for node/view/nn anyway). At the same time, there is a check in book_block() that shows the block appropriately on these pages.
2004-07-30 13:37:26 +00:00
function forum_view(&$node, $teaser = FALSE, $page = FALSE) {
2003-11-25 19:26:21 +00:00
if ($page) {
2007-02-11 09:30:51 +00:00
$vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', ''));
2003-09-20 15:11:41 +00:00
// Breadcrumb navigation
2004-06-18 15:04:37 +00:00
$breadcrumb = array();
$breadcrumb[] = array('path' => 'forum', 'title' => $vocabulary->name);
2004-01-25 21:11:01 +00:00
if ($parents = taxonomy_get_parents_all($node->tid)) {
2003-12-24 15:09:43 +00:00
$parents = array_reverse($parents);
foreach ($parents as $p) {
2004-06-18 15:04:37 +00:00
$breadcrumb[] = array('path' => 'forum/'. $p->tid, 'title' => $p->name);
2003-12-24 15:09:43 +00:00
}
}
2004-06-18 15:04:37 +00:00
$breadcrumb[] = array('path' => 'node/'. $node->nid);
menu_set_location($breadcrumb);
2002-11-08 13:23:29 +00:00
}
2003-12-16 21:06:34 +00:00
2004-10-23 17:18:23 +00:00
$node = node_prepare($node, $teaser);
2006-12-23 19:30:46 +00:00
if (!$teaser) {
$node->content['forum_navigation'] = array(
'#value' => theme('forum_topic_navigation', $node),
'#weight' => 100,
);
}
2006-08-10 15:42:33 +00:00
return $node;
2001-12-05 18:46:24 +00:00
}
2004-05-09 19:28:43 +00:00
/**
2006-08-31 21:58:36 +00:00
* Implementation of hook_submit().
*
* Check in particular that only a "leaf" term in the associated taxonomy
* vocabulary is selected, not a "container" term.
2004-05-09 19:28:43 +00:00
*/
2006-08-31 21:58:36 +00:00
function forum_submit(&$node) {
2003-03-07 22:11:44 +00:00
// Make sure all fields are set properly:
2004-02-01 21:32:01 +00:00
$node->icon = $node->icon ? $node->icon : '';
2004-01-25 21:11:01 +00:00
2006-08-31 21:58:36 +00:00
if ($node->taxonomy) {
2006-08-05 01:42:56 +00:00
// Get the forum terms from the (cached) tree
$tree = taxonomy_get_tree(_forum_get_vid());
if ($tree) {
foreach ($tree as $term) {
$forum_terms[] = $term->tid;
}
}
2004-01-25 21:11:01 +00:00
foreach ($node->taxonomy as $term) {
2006-08-05 01:42:56 +00:00
if (in_array($term, $forum_terms)) {
2005-11-12 02:54:13 +00:00
$node->tid = $term;
2004-01-25 21:11:01 +00:00
}
2003-03-07 22:11:44 +00:00
}
2006-08-05 01:42:56 +00:00
$old_tid = db_result(db_query_range("SELECT tid FROM {forum} WHERE nid = %d ORDER BY vid DESC", $node->nid, 0,1));
2006-08-07 15:04:16 +00:00
if ($old_tid) {
2006-08-05 01:42:56 +00:00
if (($node->tid != $old_tid) && $node->shadow) {
// A shadow copy needs to be created. Retain new term and add old term.
$node->taxonomy[] = $old_tid;
2004-09-14 05:48:02 +00:00
}
}
2003-01-15 23:01:42 +00:00
}
2006-08-31 21:58:36 +00:00
}
2003-01-15 23:01:42 +00:00
2006-08-31 21:58:36 +00:00
/**
* Implementation of hook_validate().
*
* Check in particular that only a "leaf" term in the associated taxonomy
* vocabulary is selected, not a "container" term.
*/
function forum_validate($node) {
if ($node->taxonomy) {
// Extract the node's proper topic ID.
$vocabulary = variable_get('forum_nav_vocabulary', '');
$containers = variable_get('forum_containers', array());
foreach ($node->taxonomy as $term) {
if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vocabulary))) {
if (in_array($term, $containers)) {
$term = taxonomy_get_term($term);
form_set_error('taxonomy', t('The item %forum is only a container for forums. Please select one of the forums below it.', array('%forum' => $term->name)));
}
}
}
}
2005-11-12 02:54:13 +00:00
}
2006-08-31 21:58:36 +00:00
/**
* Implementation of hook_update().
*/
function forum_update($node) {
if ($node->revision) {
2005-08-30 15:22:29 +00:00
db_query("INSERT INTO {forum} (nid, vid, tid) VALUES (%d, %d, %d)", $node->nid, $node->vid, $node->tid);
}
else {
db_query('UPDATE {forum} SET tid = %d WHERE vid = %d', $node->tid, $node->vid);
}
2004-09-14 05:48:02 +00:00
}
2004-05-09 19:28:43 +00:00
/**
* Implementation of hook_form().
*/
2004-07-04 16:50:02 +00:00
function forum_form(&$node) {
2006-08-06 23:00:42 +00:00
$type = node_get_types('type', $node);
2007-01-31 15:49:26 +00:00
$form['title'] = array('#type' => 'textfield', '#title' => check_plain($type->title_label), '#default_value' => !empty($node->title) ? $node->title : '', '#required' => TRUE, '#weight' => -5);
2005-09-23 08:47:13 +00:00
2007-01-31 15:49:26 +00:00
if (!empty($node->nid)) {
2007-02-12 17:47:08 +00:00
$forum_terms = taxonomy_node_get_terms_by_vocabulary(_forum_get_vid(), $node);
2003-01-15 23:01:42 +00:00
// if editing, give option to leave shadows
2005-12-06 12:20:24 +00:00
$shadow = (count($forum_terms) > 1);
2005-11-12 02:54:13 +00:00
$form['shadow'] = array('#type' => 'checkbox', '#title' => t('Leave shadow copy'), '#default_value' => $shadow, '#description' => t('If you move this topic, you can leave a link in the old forum to the new forum.'));
2003-01-15 23:01:42 +00:00
}
2002-09-15 13:00:12 +00:00
2007-01-31 15:49:26 +00:00
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => check_plain($type->body_label), '#default_value' => !empty($node->body) ? $node->body : '', '#rows' => 20, '#required' => TRUE);
- Patch #45530 by Morbus: filter_form shouldn't default to #weight 0
When a form element doesn't specify a #weight, it is assumed internally as #weight 0. However, to ensure that our form elements display visually *as they were defined in the array* we, in form_builder, count the number of elements, divide by 1000, and set that as the weight:
# Assign a decimal placeholder weight to preserve original array order
if (!isset($form[$key]['#weight'])) {
$form[$key]['#weight'] = $count/1000;
}
The above code will set the #weights of elements that have not defined a weight to something like 0 (first element in array definition), 0.001, 0.002, and so on. However, anytime a form element *explicitly* defines a #weight of 0, that #weight is kept at exactly 0, which would cause that form element to appear BEFORE the elements that didn't have a #weight defined (and thus received a #weight such as 0.002).
Consider the following pseudo example:
$form['game_title'] = array(
'#type' => 'textfield',
...
);
$form['game_description'] = array(
'#type' => 'textarea',
...
);
$form['game_format'] = filter_form(variable_get('game_format', NULL));
return $form;
Here, we're not definiing weights on our two textfields. We then add an filter_form. The second parameter of the filter_form is $weight, which defaults to 0. After this $form hits form_builder, we have weights 0 (game_title), 0.001 (game_description), and 0 (filter_form) respectively. This is then sorted by weight, which causes filter_form (the third element in the array) to appear BEFORE game_description (0 is lighter than 0.001).
The short lesson is: explicitly defining #weight 0 for a form element is probably a bad idea. This patch changes the default #weight of filter_form to NULL, instead of 0, and also removes any other explicit setting of #weight to 0 in core.
2006-01-20 09:04:34 +00:00
$form['body_filter']['format'] = filter_form($node->format);
2001-12-05 18:46:24 +00:00
2005-10-07 06:11:12 +00:00
return $form;
2001-12-05 18:46:24 +00:00
}
2005-12-10 19:58:40 +00:00
/**
* Implementation of hook_prepare; assign forum taxonomy when adding a topic from within a forum.
*/
function forum_prepare(&$node) {
2007-01-31 15:49:26 +00:00
if (empty($node->nid)) {
2005-12-10 19:58:40 +00:00
// new topic
2006-04-06 18:36:57 +00:00
$node->taxonomy[arg(3)]->vid = _forum_get_vid();
$node->taxonomy[arg(3)]->tid = arg(3);
2005-12-10 19:58:40 +00:00
}
}
2006-08-31 21:58:36 +00:00
/**
* Implementation of hook_insert().
*/
function forum_insert($node) {
db_query('INSERT INTO {forum} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $node->tid);
}
/**
* Implementation of hook_delete().
*/
function forum_delete(&$node) {
db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid);
2002-09-15 13:00:12 +00:00
}
2001-12-05 18:46:24 +00:00
2006-03-07 19:03:02 +00:00
/**
* Returns a form for adding a container to the forum vocabulary
*
* @param $edit Associative array containing a container term to be added or edited.
*/
function forum_form_container($edit = array()) {
// Handle a delete operation.
$form['name'] = array(
'#title' => t('Container name'),
'#type' => 'textfield',
'#default_value' => $edit['name'],
2007-01-25 22:14:06 +00:00
'#maxlength' => 255,
2006-03-07 19:03:02 +00:00
'#description' => t('The container name is used to identify related forums.'),
'#required' => TRUE
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $edit['description'],
'#description' => t('The container description can give users more information about the forums it contains.')
);
$form['parent']['#tree'] = TRUE;
$form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'), 'container');
$form['weight'] = array('#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $edit['weight'],
'#description' => t('When listing containers, those with with light (small) weights get listed before containers with heavier (larger) weights. Containers with equal weights are sorted alphabetically.')
);
$form['vid'] = array('#type' => 'hidden',
'#value' => _forum_get_vid());
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
if ($edit['tid']) {
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
$form['tid'] = array('#type' => 'value', '#value' => $edit['tid']);
}
2006-08-18 18:58:47 +00:00
$form['#base'] = 'forum_form';
2006-03-07 19:03:02 +00:00
2006-08-18 18:58:47 +00:00
return $form;
2006-03-07 19:03:02 +00:00
}
2006-09-23 08:10:40 +00:00
function forum_form_main($type, $edit = array()) {
if ($_POST['op'] == t('Delete') || $_POST['confirm']) {
return drupal_get_form('forum_confirm_delete', $edit['tid']);
}
switch ($type) {
case 'forum':
return drupal_get_form('forum_form_forum', $edit);
break;
case 'container':
return drupal_get_form('forum_form_container', $edit);
break;
}
}
2006-03-07 19:03:02 +00:00
/**
* Returns a form for adding a forum to the forum vocabulary
*
* @param $edit Associative array containing a forum term to be added or edited.
*/
function forum_form_forum($edit = array()) {
$form['name'] = array('#type' => 'textfield',
'#title' => t('Forum name'),
'#default_value' => $edit['name'],
2007-01-25 22:14:06 +00:00
'#maxlength' => 255,
2006-03-07 19:03:02 +00:00
'#description' => t('The forum name is used to identify related discussions.'),
'#required' => TRUE,
);
$form['description'] = array('#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $edit['description'],
'#description' => t('The forum description can give users more information about the discussion topics it contains.'),
);
$form['parent']['#tree'] = TRUE;
$form['parent'][0] = _forum_parent_select($edit['tid'], t('Parent'), 'forum');
$form['weight'] = array('#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $edit['weight'],
'#description' => t('When listing forums, those with lighter (smaller) weights get listed before containers with heavier (larger) weights. Forums with equal weights are sorted alphabetically.'),
);
$form['vid'] = array('#type' => 'hidden', '#value' => _forum_get_vid());
$form['submit' ] = array('#type' => 'submit', '#value' => t('Submit'));
if ($edit['tid']) {
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
$form['tid'] = array('#type' => 'hidden', '#value' => $edit['tid']);
}
2006-08-18 18:58:47 +00:00
$form['#base'] = 'forum_form';
2006-03-07 19:03:02 +00:00
2006-08-18 18:58:47 +00:00
return $form;
2006-03-07 19:03:02 +00:00
}
/**
* Process forum form and container form submissions.
*/
function forum_form_submit($form_id, $form_values) {
if ($form_id == 'forum_form_container') {
$container = TRUE;
$type = t('forum container');
}
else {
2006-07-05 11:45:51 +00:00
$container = FALSE;
2006-03-07 19:03:02 +00:00
$type = t('forum');
}
$status = taxonomy_save_term($form_values);
switch ($status) {
case SAVED_NEW:
if ($container) {
$containers = variable_get('forum_containers', array());
$containers[] = $form_values['tid'];
variable_set('forum_containers', $containers);
}
2006-08-18 12:17:00 +00:00
drupal_set_message(t('Created new @type %term.', array('%term' => $form_values['name'], '@type' => $type)));
2006-03-07 19:03:02 +00:00
break;
case SAVED_UPDATED:
2006-08-18 12:17:00 +00:00
drupal_set_message(t('The @type %term has been updated.', array('%term' => $form_values['name'], '@type' => $type)));
2006-03-07 19:03:02 +00:00
break;
}
2006-07-31 11:25:55 +00:00
return 'admin/content/forum';
2006-03-07 19:03:02 +00:00
}
/**
* Returns a confirmation page for deleting a forum taxonomy term.
*
* @param $tid ID of the term to be deleted
*/
2006-08-18 18:58:47 +00:00
function forum_confirm_delete($tid) {
2006-03-07 19:03:02 +00:00
$term = taxonomy_get_term($tid);
$form['tid'] = array('#type' => 'value', '#value' => $tid);
$form['name'] = array('#type' => 'value', '#value' => $term->name);
2006-09-23 08:10:40 +00:00
return confirm_form($form, t('Are you sure you want to delete the forum %name?', array('%name' => $term->name)), 'admin/content/forum', t('Deleting a forum or container will delete all sub-forums and associated posts as well. This action cannot be undone.'), t('Delete'), t('Cancel'));
2006-03-07 19:03:02 +00:00
}
/**
* Implementation of forms api _submit call. Deletes a forum after confirmation.
*/
function forum_confirm_delete_submit($form_id, $form_values) {
taxonomy_del_term($form_values['tid']);
2006-08-18 12:17:00 +00:00
drupal_set_message(t('The forum %term and all sub-forums and associated posts have been deleted.', array('%term' => $form_values['name'])));
watchdog('content', t('forum: deleted %term and all its sub-forums and associated posts.', array('%term' => $form_values['name'])));
2006-03-07 19:03:02 +00:00
2006-07-31 11:25:55 +00:00
return 'admin/content/forum';
2006-03-07 19:03:02 +00:00
}
/**
* Returns an overview list of existing forums and containers
*/
function forum_overview() {
$header = array(t('Name'), t('Operations'));
$tree = taxonomy_get_tree(_forum_get_vid());
if ($tree) {
foreach ($tree as $term) {
if (in_array($term->tid, variable_get('forum_containers', array()))) {
2007-01-25 21:49:32 +00:00
$rows[] = array(str_repeat(' -- ', $term->depth) .' '. l($term->name, 'forum/'. $term->tid), l(t('edit container'), 'admin/content/forum/edit/container/'. $term->tid));
2006-03-07 19:03:02 +00:00
}
else {
2007-01-25 21:49:32 +00:00
$rows[] = array(str_repeat(' -- ', $term->depth) .' '. l($term->name, 'forum/'. $term->tid), l(t('edit forum'), 'admin/content/forum/edit/forum/'. $term->tid));
2006-03-07 19:03:02 +00:00
}
}
}
else {
2006-08-18 12:17:00 +00:00
$rows[] = array(array('data' => '<em>' . t('There are no existing containers or forums. You may add some on the <a href="@container">add container</a> or <a href="@forum">add forum</a> pages.', array('@container' => url('admin/content/forum/add/container'), '@forum' => url('admin/content/forum/add/forum'))) . '</em>', 'colspan' => 2));
2006-03-07 19:03:02 +00:00
}
return theme('table', $header, $rows);
}
/**
* Returns a select box for available parent terms
*
* @param $tid ID of the term which is being added or edited
* @param $title Title to display the select box with
* @param $child_type Whether the child is forum or container
*/
function _forum_parent_select($tid, $title, $child_type) {
$parents = taxonomy_get_parents($tid);
if ($parents) {
$parent = array_shift($parents);
$parent = $parent->tid;
}
else {
$parent = 0;
}
$children = taxonomy_get_tree(_forum_get_vid(), $tid);
// A term can't be the child of itself, nor of its children.
foreach ($children as $child) {
$exclude[] = $child->tid;
}
$exclude[] = $tid;
$tree = taxonomy_get_tree(_forum_get_vid());
$options[0] = '<'. t('root') .'>';
if ($tree) {
foreach ($tree as $term) {
if (!in_array($term->tid, $exclude)) {
2006-08-25 08:55:04 +00:00
$options[$term->tid] = str_repeat(' -- ', $term->depth) . $term->name;
2006-03-07 19:03:02 +00:00
}
}
}
if ($child_type == 'container') {
$description = t('Containers are usually placed at the top (root) level of your forum but you can also place a container inside a parent container or forum.');
}
else if ($child_type == 'forum') {
$description = t('You may place your forum inside a parent container or forum, or at the top (root) level of your forum.');
}
return array('#type' => 'select', '#title' => $title, '#default_value' => $parent, '#options' => $options, '#description' => $description, '#required' => TRUE);
}
2006-05-18 14:58:57 +00:00
function forum_link_alter(&$node, &$links) {
2006-07-04 08:59:05 +00:00
foreach ($links as $module => $link) {
2006-05-18 14:58:57 +00:00
if (strstr($module, 'taxonomy_term')) {
2006-05-29 12:58:19 +00:00
// Link back to the forum and not the taxonomy term page. We'll only
// do this if the taxonomy term in question belongs to forums.
2006-07-04 08:59:05 +00:00
$tid = str_replace('taxonomy/term/', '', $link['href']);
2006-05-29 12:58:19 +00:00
$term = taxonomy_get_term($tid);
if ($term->vid == _forum_get_vid()) {
2006-07-04 08:59:05 +00:00
$links[$module]['href'] = str_replace('taxonomy/term', 'forum', $link['href']);
2006-05-29 12:58:19 +00:00
}
2006-05-18 14:58:57 +00:00
}
}
2006-03-07 19:03:02 +00:00
}
/**
* Returns the vocabulary id for forum navigation.
*/
function _forum_get_vid() {
$vid = variable_get('forum_nav_vocabulary', '');
if (empty($vid)) {
// Check to see if a forum vocabulary exists
$vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", 'forum'));
if (!$vid) {
2006-06-06 15:35:39 +00:00
// Create the forum vocabulary. Assign the vocabulary a low weight so
// it will appear first in forum topic create and edit forms.
2007-02-01 21:44:36 +00:00
$edit = array('name' => t('Forums'), 'multiple' => 0, 'required' => 1, 'hierarchy' => 1, 'relations' => 0, 'module' => 'forum', 'weight' => -10, 'nodes' => array('forum' => 1));
2006-03-07 19:03:02 +00:00
taxonomy_save_vocabulary($edit);
$vid = $edit['vid'];
}
variable_set('forum_nav_vocabulary', $vid);
}
return $vid;
}
2004-09-14 05:48:02 +00:00
/**
* Formats a topic for display
*
2004-10-19 18:02:31 +00:00
* @TODO Give a better description. Not sure where this function is used yet.
2004-09-14 05:48:02 +00:00
*/
2002-09-15 13:00:12 +00:00
function _forum_format($topic) {
2004-06-29 20:35:58 +00:00
if ($topic && $topic->timestamp) {
2006-08-18 12:17:00 +00:00
return t('@time ago<br />by !author', array('@time' => format_interval(time() - $topic->timestamp), '!author' => theme('username', $topic)));
2002-09-15 13:00:12 +00:00
}
else {
2006-07-29 17:56:41 +00:00
return t('n/a');
2002-09-15 13:00:12 +00:00
}
}
2004-09-14 05:48:02 +00:00
/**
* Returns a list of all forums for a given taxonomy id
*
2004-10-19 18:02:31 +00:00
* Forum objects contain the following fields
2004-09-14 05:48:02 +00:00
* -num_topics Number of topics in the forum
* -num_posts Total number of posts in all topics
* -last_post Most recent post for the forum
*
* @param $tid
* Taxonomy ID of the vocabulary that holds the forum list.
* @return
* Array of object containing the forum information.
*/
2002-09-15 13:00:12 +00:00
function forum_get_forums($tid = 0) {
2005-01-04 20:54:23 +00:00
2004-09-14 05:48:02 +00:00
$forums = array();
$_forums = taxonomy_get_tree(variable_get('forum_nav_vocabulary', ''), $tid);
2002-09-15 13:00:12 +00:00
2004-09-14 05:48:02 +00:00
if (count($_forums)) {
2002-09-15 13:00:12 +00:00
2004-09-14 05:48:02 +00:00
$counts = array();
2005-01-16 18:44:49 +00:00
$sql = "SELECT r.tid, COUNT(n.nid) AS topic_count, SUM(l.comment_count) AS comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {term_node} r ON n.nid = r.nid WHERE n.status = 1 AND n.type = 'forum' GROUP BY r.tid";
2005-01-29 22:02:37 +00:00
$sql = db_rewrite_sql($sql);
2005-01-16 18:44:49 +00:00
$_counts = db_query($sql, $forum->tid);
2004-09-14 05:48:02 +00:00
while ($count = db_fetch_object($_counts)) {
$counts[$count->tid] = $count;
}
2002-12-14 23:23:35 +00:00
}
2004-01-31 21:16:42 +00:00
2004-09-14 05:48:02 +00:00
foreach ($_forums as $forum) {
if (in_array($forum->tid, variable_get('forum_containers', array()))) {
$forum->container = 1;
}
2002-09-15 13:00:12 +00:00
2004-09-14 05:48:02 +00:00
if ($counts[$forum->tid]) {
$forum->num_topics = $counts[$forum->tid]->topic_count;
$forum->num_posts = $counts[$forum->tid]->topic_count + $counts[$forum->tid]->comment_count;
}
else {
$forum->num_topics = 0;
$forum->num_posts = 0;
}
// This query does not use full ANSI syntax since MySQL 3.x does not support
// table1 INNER JOIN table2 INNER JOIN table3 ON table2_criteria ON table3_criteria
2004-11-21 08:25:17 +00:00
// used to join node_comment_statistics to users.
2006-08-26 00:19:02 +00:00
$sql = "SELECT ncs.last_comment_timestamp, IF (ncs.last_comment_uid != 0, u2.name, ncs.last_comment_name) AS last_comment_name, ncs.last_comment_uid FROM {node} n INNER JOIN {users} u1 ON n.uid = u1.uid INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {users} u2 ON ncs.last_comment_uid=u2.uid WHERE n.status = 1 AND n.type='forum' AND tn.tid = %d ORDER BY ncs.last_comment_timestamp DESC";
2005-01-29 22:02:37 +00:00
$sql = db_rewrite_sql($sql);
2005-01-16 18:44:49 +00:00
$topic = db_fetch_object(db_query_range($sql, $forum->tid, 0, 1));
2005-01-04 20:54:23 +00:00
2007-01-05 05:32:23 +00:00
$last_post = new stdClass();
2004-09-14 05:48:02 +00:00
$last_post->timestamp = $topic->last_comment_timestamp;
$last_post->name = $topic->last_comment_name;
$last_post->uid = $topic->last_comment_uid;
$forum->last_post = $last_post;
2002-09-15 13:00:12 +00:00
2004-09-14 05:48:02 +00:00
$forums[$forum->tid] = $forum;
}
return $forums;
2002-09-15 13:00:12 +00:00
}
2006-02-09 08:09:55 +00:00
/**
* Calculate the number of nodes the user has not yet read and are newer
* than NODE_NEW_LIMIT.
*/
function _forum_topics_unread($term, $uid) {
2006-02-09 08:52:03 +00:00
$sql = "SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid AND tn.tid = %d LEFT JOIN {history} h ON n.nid = h.nid AND h.uid = %d WHERE n.status = 1 AND n.type = 'forum' AND n.created > %d AND h.nid IS NULL";
2005-01-29 22:02:37 +00:00
$sql = db_rewrite_sql($sql);
2006-02-09 08:09:55 +00:00
return db_result(db_query($sql, $term, $uid, NODE_NEW_LIMIT));
2002-09-15 13:00:12 +00:00
}
2003-01-15 23:01:42 +00:00
function forum_get_topics($tid, $sortby, $forum_per_page) {
2003-08-21 15:47:50 +00:00
global $user, $forum_topic_list_header;
2002-09-15 13:00:12 +00:00
2003-08-21 15:47:50 +00:00
$forum_topic_list_header = array(
2006-10-04 06:07:05 +00:00
array('data' => ' ', 'field' => NULL),
2004-02-01 21:32:01 +00:00
array('data' => t('Topic'), 'field' => 'n.title'),
2004-09-14 05:48:02 +00:00
array('data' => t('Replies'), 'field' => 'l.comment_count'),
2004-02-01 21:32:01 +00:00
array('data' => t('Created'), 'field' => 'n.created'),
2004-09-14 05:48:02 +00:00
array('data' => t('Last reply'), 'field' => 'l.last_comment_timestamp'),
2003-08-21 15:47:50 +00:00
);
2002-09-15 13:00:12 +00:00
2004-08-09 18:54:49 +00:00
$order = _forum_get_topic_order($sortby);
2003-10-07 18:16:41 +00:00
for ($i = 0; $i < count($forum_topic_list_header); $i++) {
2004-08-09 18:54:49 +00:00
if ($forum_topic_list_header[$i]['field'] == $order['field']) {
$forum_topic_list_header[$i]['sort'] = $order['sort'];
2003-08-21 15:47:50 +00:00
}
}
$term = taxonomy_get_term($tid);
2002-09-15 13:00:12 +00:00
2006-02-01 14:19:44 +00:00
$sql = db_rewrite_sql("SELECT n.nid, f.tid, n.title, n.sticky, u.name, u.uid, n.created AS timestamp, n.comment AS comment_mode, l.last_comment_timestamp, IF(l.last_comment_uid != 0, cu.name, l.last_comment_name) AS last_comment_name, l.last_comment_uid, l.comment_count AS num_comments FROM {node_comment_statistics} l, {users} cu, {term_node} r, {users} u, {forum} f, {node} n WHERE n.status = 1 AND l.last_comment_uid = cu.uid AND n.nid = l.nid AND n.nid = r.nid AND r.tid = %d AND n.uid = u.uid AND n.vid = f.vid");
2004-06-27 16:02:31 +00:00
$sql .= tablesort_sql($forum_topic_list_header, 'n.sticky DESC,');
2005-10-01 13:30:06 +00:00
$sql .= ', n.created DESC'; // Always add a secondary sort order so that the news forum topics are on top.
2003-01-15 23:01:42 +00:00
2005-01-29 22:02:37 +00:00
$sql_count = db_rewrite_sql("SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 AND n.type = 'forum'");
2002-09-15 13:00:12 +00:00
2005-01-17 19:00:03 +00:00
$result = pager_query($sql, $forum_per_page, 0, $sql_count, $tid);
2002-09-15 13:00:12 +00:00
while ($topic = db_fetch_object($result)) {
2003-01-15 23:01:42 +00:00
if ($user->uid) {
// folder is new if topic is new or there are new comments since last visit
2004-09-14 05:48:02 +00:00
if ($topic->tid != $tid) {
2003-01-15 23:01:42 +00:00
$topic->new = 0;
2002-09-15 13:00:12 +00:00
}
else {
2004-08-09 18:54:49 +00:00
$history = _forum_user_last_visit($topic->nid);
2004-09-14 05:48:02 +00:00
$topic->new_replies = comment_num_new($topic->nid, $history);
2004-01-29 22:00:31 +00:00
$topic->new = $topic->new_replies || ($topic->timestamp > $history);
2002-09-15 13:00:12 +00:00
}
2004-01-29 22:00:31 +00:00
}
else {
2004-05-09 19:28:43 +00:00
// Do not track "new replies" status for topics if the user is anonymous.
2003-01-15 23:01:42 +00:00
$topic->new_replies = 0;
$topic->new = 0;
2004-01-29 22:00:31 +00:00
}
2002-09-15 13:00:12 +00:00
2004-09-14 05:48:02 +00:00
if ($topic->num_comments > 0) {
2007-01-05 05:32:23 +00:00
$last_reply = new stdClass();
2004-09-14 05:48:02 +00:00
$last_reply->timestamp = $topic->last_comment_timestamp;
$last_reply->name = $topic->last_comment_name;
$last_reply->uid = $topic->last_comment_uid;
$topic->last_reply = $last_reply;
}
2002-09-15 13:00:12 +00:00
$topics[] = $topic;
}
2003-01-15 23:01:42 +00:00
return $topics;
2002-09-15 13:00:12 +00:00
}
2004-09-14 05:48:02 +00:00
/**
* Finds the first unread node for a given forum.
*/
2003-01-15 23:01:42 +00:00
function _forum_new($tid) {
2002-09-15 13:00:12 +00:00
global $user;
2005-01-16 18:44:49 +00:00
$sql = "SELECT n.nid FROM {node} n LEFT JOIN {history} h ON n.nid = h.nid AND h.uid = %d INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 AND n.type = 'forum' AND h.nid IS NULL AND n.created > %d ORDER BY created";
2005-01-29 22:02:37 +00:00
$sql = db_rewrite_sql($sql);
2005-01-16 18:44:49 +00:00
$nid = db_result(db_query_range($sql, $user->uid, $tid, NODE_NEW_LIMIT, 0, 1));
2002-09-15 13:00:12 +00:00
return $nid ? $nid : 0;
}
2004-05-09 19:28:43 +00:00
/**
2004-05-17 22:00:06 +00:00
* Menu callback; prints a forum listing.
2004-05-09 19:28:43 +00:00
*/
2004-11-06 11:54:09 +00:00
function forum_page($tid = 0) {
2006-08-20 05:57:41 +00:00
if (module_exists('taxonomy') && module_exists('comment')) {
2004-11-06 11:54:09 +00:00
$forum_per_page = variable_get('forum_per_page', 25);
$sortby = variable_get('forum_order', 1);
2002-09-15 13:00:12 +00:00
2004-11-06 11:54:09 +00:00
$forums = forum_get_forums($tid);
$parents = taxonomy_get_parents_all($tid);
if ($tid && !in_array($tid, variable_get('forum_containers', array()))) {
$topics = forum_get_topics($tid, $sortby, $forum_per_page);
2002-12-18 21:43:03 +00:00
}
2004-11-06 11:54:09 +00:00
2005-04-24 16:34:36 +00:00
return theme('forum_display', $forums, $topics, $parents, $tid, $sortby, $forum_per_page);
2001-12-05 18:46:24 +00:00
}
else {
2005-09-06 19:38:56 +00:00
drupal_set_message(t('The forum module requires both the taxonomy module and the comment module to be enabled and configured.'), 'error');
return ' ';
2001-12-05 18:46:24 +00:00
}
}
2003-11-09 23:27:22 +00:00
/**
2003-12-08 06:32:19 +00:00
* Format the forum body.
*
2004-09-09 05:51:08 +00:00
* @ingroup themeable
2003-12-08 06:32:19 +00:00
*/
2004-08-09 18:54:49 +00:00
function theme_forum_display($forums, $topics, $parents, $tid, $sortby, $forum_per_page) {
2004-03-20 13:23:34 +00:00
global $user;
2004-02-01 21:32:01 +00:00
// forum list, topics list, topic browser and 'add new topic' link
2003-02-15 11:39:56 +00:00
2007-02-11 09:30:51 +00:00
$vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', ''));
2005-03-16 21:07:59 +00:00
$title = $vocabulary->name;
2002-09-15 13:00:12 +00:00
2004-06-18 15:04:37 +00:00
// Breadcrumb navigation:
$breadcrumb = array();
2003-11-25 19:26:21 +00:00
if ($tid) {
2004-06-18 15:04:37 +00:00
$breadcrumb[] = array('path' => 'forum', 'title' => $title);
2003-11-25 19:26:21 +00:00
}
2002-09-15 13:00:12 +00:00
if ($parents) {
2003-08-15 15:17:23 +00:00
$parents = array_reverse($parents);
foreach ($parents as $p) {
2003-08-22 17:06:44 +00:00
if ($p->tid == $tid) {
$title = $p->name;
2002-09-15 13:00:12 +00:00
}
else {
2004-06-18 15:04:37 +00:00
$breadcrumb[] = array('path' => 'forum/'. $p->tid, 'title' => $p->name);
2002-09-15 13:00:12 +00:00
}
}
}
2005-03-16 21:07:59 +00:00
2007-01-10 15:17:51 +00:00
drupal_set_title(check_plain($title));
2005-03-16 21:07:59 +00:00
2004-06-18 15:04:37 +00:00
$breadcrumb[] = array('path' => $_GET['q']);
menu_set_location($breadcrumb);
2002-09-15 13:00:12 +00:00
2003-11-05 17:40:41 +00:00
if (count($forums) || count($parents)) {
2004-05-09 19:28:43 +00:00
$output = '<div id="forum">';
$output .= '<ul>';
2004-03-20 13:23:34 +00:00
if (user_access('create forum topics')) {
2004-05-09 19:28:43 +00:00
$output .= '<li>'. l(t('Post new forum topic.'), "node/add/forum/$tid") .'</li>';
2004-03-20 13:23:34 +00:00
}
else if ($user->uid) {
2004-05-09 19:28:43 +00:00
$output .= '<li>'. t('You are not allowed to post a new forum topic.') .'</li>';
2004-03-20 13:23:34 +00:00
}
else {
2007-02-15 11:40:19 +00:00
$output .= '<li>'. t('<a href="@login">Login</a> to post a new forum topic.', array('@login' => url('user/login', array('query' => drupal_get_destination())))) .'</li>';
2004-03-20 13:23:34 +00:00
}
2004-05-09 19:28:43 +00:00
$output .= '</ul>';
2004-03-20 13:23:34 +00:00
2004-02-01 21:32:01 +00:00
$output .= theme('forum_list', $forums, $parents, $tid);
2003-08-15 15:17:23 +00:00
2004-02-01 21:32:01 +00:00
if ($tid && !in_array($tid, variable_get('forum_containers', array()))) {
2004-08-09 18:54:49 +00:00
$output .= theme('forum_topic_list', $tid, $topics, $sortby, $forum_per_page);
2006-08-23 07:23:09 +00:00
drupal_add_feed(url('taxonomy/term/'. $tid .'/0/feed'), 'RSS - '. $title);
2003-11-05 17:40:41 +00:00
}
2004-05-09 19:28:43 +00:00
$output .= '</div>';
2003-11-05 17:40:41 +00:00
}
else {
2004-12-15 21:19:42 +00:00
drupal_set_title(t('No forums defined'));
2003-11-05 17:40:41 +00:00
$output = '';
2003-08-15 15:17:23 +00:00
}
2005-01-22 08:38:25 +00:00
return $output;
2003-08-15 15:17:23 +00:00
}
2003-11-09 23:27:22 +00:00
/**
2003-12-08 06:32:19 +00:00
* Format the forum listing.
*
2004-09-09 05:51:08 +00:00
* @ingroup themeable
2003-12-08 06:32:19 +00:00
*/
2003-11-09 23:27:22 +00:00
function theme_forum_list($forums, $parents, $tid) {
2003-08-15 15:17:23 +00:00
global $user;
2002-09-15 13:00:12 +00:00
if ($forums) {
2004-02-01 21:32:01 +00:00
$header = array(t('Forum'), t('Topics'), t('Posts'), t('Last post'));
2002-09-15 13:00:12 +00:00
foreach ($forums as $forum) {
2003-10-20 17:32:48 +00:00
if ($forum->container) {
2004-05-09 19:28:43 +00:00
$description = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n";
$description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n";
2004-01-21 18:34:18 +00:00
if ($forum->description) {
2006-04-07 15:32:17 +00:00
$description .= ' <div class="description">'. filter_xss_admin($forum->description) ."</div>\n";
2004-01-21 18:34:18 +00:00
}
$description .= "</div>\n";
2004-11-15 11:16:39 +00:00
$rows[] = array(array('data' => $description, 'class' => 'container', 'colspan' => '4'));
2002-11-08 13:23:29 +00:00
}
2003-10-20 17:32:48 +00:00
else {
2006-02-09 08:09:55 +00:00
$new_topics = _forum_topics_unread($forum->tid, $user->uid);
$forum->old_topics = $forum->num_topics - $new_topics;
if (!$user->uid) {
2004-01-21 18:34:18 +00:00
$new_topics = 0;
}
2002-09-15 13:00:12 +00:00
2004-05-09 19:28:43 +00:00
$description = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n";
$description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n";
2003-10-20 17:32:48 +00:00
2004-01-21 18:34:18 +00:00
if ($forum->description) {
2006-04-07 15:32:17 +00:00
$description .= ' <div class="description">'. filter_xss_admin($forum->description) ."</div>\n";
2004-01-21 18:34:18 +00:00
}
$description .= "</div>\n";
2003-08-15 15:17:23 +00:00
2007-01-31 16:01:17 +00:00
$row = array(
'data' => array(
array('data' => $description, 'class' => 'forum'),
2007-02-15 11:40:19 +00:00
array('data' => $forum->num_topics . ($new_topics ? '<br />'. l(format_plural($new_topics, '1 new', '@count new'), "forum/$forum->tid", array('fragment' => 'new')) : ''), 'class' => 'topics'),
2007-01-31 16:01:17 +00:00
array('data' => $forum->num_posts, 'class' => 'posts'),
array('data' => _forum_format($forum->last_post), 'class' => 'last-reply'),
),
);
if ($new_topics > 0) {
$row['class'] = 'new-topics';
}
$rows[] = $row;
2004-01-21 18:34:18 +00:00
}
2002-09-15 13:00:12 +00:00
}
2004-10-09 17:15:55 +00:00
return theme('table', $header, $rows);
2002-09-15 13:00:12 +00:00
}
}
2003-11-09 23:27:22 +00:00
/**
2003-12-08 06:32:19 +00:00
* Format the topic listing.
*
2004-09-09 05:51:08 +00:00
* @ingroup themeable
2003-12-08 06:32:19 +00:00
*/
2004-08-09 18:54:49 +00:00
function theme_forum_topic_list($tid, $topics, $sortby, $forum_per_page) {
global $forum_topic_list_header;
2002-09-15 13:00:12 +00:00
if ($topics) {
2003-08-15 15:17:23 +00:00
2002-09-15 13:00:12 +00:00
foreach ($topics as $topic) {
// folder is new if topic is new or there are new comments since last visit
2003-01-15 23:01:42 +00:00
if ($topic->tid != $tid) {
2003-08-15 15:17:23 +00:00
$rows[] = array(
2005-12-11 12:44:39 +00:00
array('data' => theme('forum_icon', $topic->new, $topic->num_comments, $topic->comment_mode, $topic->sticky), 'class' => 'icon'),
2005-03-31 09:25:33 +00:00
array('data' => check_plain($topic->title), 'class' => 'title'),
2004-02-01 21:32:01 +00:00
array('data' => l(t('This topic has been moved'), "forum/$topic->tid"), 'colspan' => '3')
2003-08-15 15:17:23 +00:00
);
2002-09-15 13:00:12 +00:00
}
else {
2003-08-15 15:17:23 +00:00
$rows[] = array(
2005-12-11 12:44:39 +00:00
array('data' => theme('forum_icon', $topic->new, $topic->num_comments, $topic->comment_mode, $topic->sticky), 'class' => 'icon'),
2004-06-18 15:04:37 +00:00
array('data' => l($topic->title, "node/$topic->nid"), 'class' => 'topic'),
2007-02-15 11:40:19 +00:00
array('data' => $topic->num_comments . ($topic->new_replies ? '<br />'. l(format_plural($topic->new_replies, '1 new', '@count new'), "node/$topic->nid", array('fragment' => 'new')) : ''), 'class' => 'replies'),
2004-02-01 21:32:01 +00:00
array('data' => _forum_format($topic), 'class' => 'created'),
2006-10-04 06:07:05 +00:00
array('data' => _forum_format(isset($topic->last_reply) ? $topic->last_reply : NULL), 'class' => 'last-reply')
2003-08-15 15:17:23 +00:00
);
2002-09-15 13:00:12 +00:00
}
}
2003-08-15 15:17:23 +00:00
}
2002-09-15 13:00:12 +00:00
2006-10-04 06:07:05 +00:00
$output = theme('table', $forum_topic_list_header, $rows);
2006-04-13 08:25:27 +00:00
$output .= theme('pager', NULL, $forum_per_page, 0);
2003-08-15 15:17:23 +00:00
2002-09-15 13:00:12 +00:00
return $output;
}
2005-12-11 12:44:39 +00:00
/**
* Format the icon for each individual topic.
*
* @ingroup themeable
*/
function theme_forum_icon($new_posts, $num_posts = 0, $comment_mode = 0, $sticky = 0) {
2002-09-15 13:00:12 +00:00
2005-12-11 12:44:39 +00:00
if ($num_posts > variable_get('forum_hot_topic', 15)) {
$icon = $new_posts ? 'hot-new' : 'hot';
2002-09-15 13:00:12 +00:00
}
else {
2005-12-11 12:44:39 +00:00
$icon = $new_posts ? 'new' : 'default';
2002-09-15 13:00:12 +00:00
}
2004-01-28 22:10:34 +00:00
2005-12-11 12:44:39 +00:00
if ($comment_mode == COMMENT_NODE_READ_ONLY || $comment_mode == COMMENT_NODE_DISABLED) {
$icon = 'closed';
}
if ($sticky == 1) {
$icon = 'sticky';
}
$output = theme('image', "misc/forum-$icon.png");
2004-01-28 22:10:34 +00:00
if ($new_posts) {
$output = "<a name=\"new\">$output</a>";
}
2006-02-10 05:05:44 +00:00
return $output;
}
/**
* Format the next/previous forum topic navigation links.
*
* @ingroup themeable
*/
function theme_forum_topic_navigation($node) {
$output = '';
// get previous and next topic
$sql = "SELECT n.nid, n.title, n.sticky, l.comment_count, l.last_comment_timestamp FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 AND n.type = 'forum' ORDER BY n.sticky DESC, ". _forum_get_topic_order_sql(variable_get('forum_order', 1));
$result = db_query(db_rewrite_sql($sql), $node->tid);
2006-08-05 00:26:36 +00:00
$stop = 0;
2006-02-10 05:05:44 +00:00
while ($topic = db_fetch_object($result)) {
if ($stop == 1) {
2007-01-05 05:32:23 +00:00
$next = new stdClass();
2006-02-10 05:05:44 +00:00
$next->nid = $topic->nid;
$next->title = $topic->title;
break;
}
if ($topic->nid == $node->nid) {
$stop = 1;
}
else {
2007-01-05 05:32:23 +00:00
$prev = new stdClass();
2006-02-10 05:05:44 +00:00
$prev->nid = $topic->nid;
$prev->title = $topic->title;
}
}
if ($prev || $next) {
2006-11-14 06:30:10 +00:00
$output .= '<div class="forum-topic-navigation clear-block">';
2006-02-10 05:05:44 +00:00
if ($prev) {
2006-02-15 20:47:35 +00:00
$output .= l(t('‹ ') . $prev->title, 'node/'. $prev->nid, array('class' => 'topic-previous', 'title' => t('Go to previous forum topic')));
2006-02-10 05:05:44 +00:00
}
2006-07-11 07:04:34 +00:00
if ($prev && $next) {
// Word break (a is an inline element)
$output .= ' ';
}
2006-02-10 05:05:44 +00:00
if ($next) {
2006-02-15 20:47:35 +00:00
$output .= l($next->title . t(' › '), 'node/'. $next->nid, array('class' => 'topic-next', 'title' => t('Go to next forum topic')));
2006-02-10 05:05:44 +00:00
}
$output .= '</div>';
}
2004-01-28 22:10:34 +00:00
return $output;
2002-09-15 13:00:12 +00:00
}
function _forum_user_last_visit($nid) {
global $user;
2004-01-29 22:00:31 +00:00
static $history = array();
if (empty($history)) {
2004-02-01 21:32:01 +00:00
$result = db_query('SELECT nid, timestamp FROM {history} WHERE uid = %d', $user->uid);
2002-09-15 13:00:12 +00:00
while ($t = db_fetch_object($result)) {
2004-01-29 22:00:31 +00:00
$history[$t->nid] = $t->timestamp > NODE_NEW_LIMIT ? $t->timestamp : NODE_NEW_LIMIT;
2002-09-15 13:00:12 +00:00
}
}
2006-10-04 06:07:05 +00:00
return isset($history[$nid]) ? $history[$nid] : NODE_NEW_LIMIT;
2002-09-15 13:00:12 +00:00
}
function _forum_get_topic_order($sortby) {
switch ($sortby) {
case 1:
2004-09-14 05:48:02 +00:00
return array('field' => 'l.last_comment_timestamp', 'sort' => 'desc');
2002-09-15 13:00:12 +00:00
break;
case 2:
2004-09-14 05:48:02 +00:00
return array('field' => 'l.last_comment_timestamp', 'sort' => 'asc');
2002-09-15 13:00:12 +00:00
break;
case 3:
2004-09-14 05:48:02 +00:00
return array('field' => 'l.comment_count', 'sort' => 'desc');
2002-09-15 13:00:12 +00:00
break;
case 4:
2004-09-14 05:48:02 +00:00
return array('field' => 'l.comment_count', 'sort' => 'asc');
2002-09-15 13:00:12 +00:00
break;
}
}
2004-08-09 18:54:49 +00:00
function _forum_get_topic_order_sql($sortby) {
$order = _forum_get_topic_order($sortby);
return $order['field'] .' '. $order['sort'];
}
2005-08-25 21:14:17 +00:00