Code coverage for /20081101/modules/forum/forum.module

Line #Times calledCode
1
<?php
2
// $Id: forum.module,v 1.471 2008/10/24 19:03:12 dries Exp $
3
4
/**
5
 * @file
6
 * Provides discussion forums.
7
 */
8
9
/**
10
 * Implementation of hook_help().
11
 */
12166
function forum_help($path, $arg) {
13
  switch ($path) {
14131
    case 'admin/help#forum':
158
      $output = '<p>' . t('The forum module lets you create threaded
discussion forums with functionality similar to other message board
systems. Forums are useful because they allow community members to discuss
topics with one another while ensuring those conversations are archived for
later reference. The <a href="@create-topic">forum topic</a> menu item
(under <em>Create content</em> on the Navigation menu) creates the initial
post of a new threaded discussion, or thread.', array('@create-topic' =>
url('node/add/forum'))) . '</p>';
168
      $output .= '<p>' . t('A threaded discussion occurs as people leave
comments on a forum topic (or on other comments within that topic). A forum
topic is contained within a forum, which may hold many similar or related
forum topics. Forums are (optionally) nested within a container, which may
hold many similar or related forums. Both containers and forums may be
nested within other containers and forums, and provide structure for your
message board. By carefully planning this structure, you make it easier for
users to find and comment on a specific forum topic.') . '</p>';
178
      $output .= '<p>' . t('When administering a forum, note that:') .
'</p>';
188
      $output .= '<ul><li>' . t('a forum topic (and all of its comments)
may be moved between forums by selecting a different forum while editing a
forum topic.') . '</li>';
198
      $output .= '<li>' . t('when moving a forum topic between forums, the
<em>Leave shadow copy</em> option creates a link in the original forum
pointing to the new location.') . '</li>';
208
      $output .= '<li>' . t('selecting <em>Read only</em> under <em>Comment
settings</em> while editing a forum topic will lock (prevent new comments)
on the thread.') . '</li>';
218
      $output .= '<li>' . t('selecting <em>Disabled</em> under <em>Comment
settings</em> while editing a forum topic will hide all existing comments
on the thread, and prevent new ones.') . '</li></ul>';
228
      $output .= '<p>' . t('For more information, see the online handbook
entry for <a href="@forum">Forum module</a>.', array('@forum' =>
'http://drupal.org/handbook/modules/forum/')) . '</p>';
238
      return $output;
24131
    case 'admin/build/forum':
253
      return '<p>' . t('This page displays a list of existing forums and
containers. Containers (optionally) hold forums, and forums hold forum
topics (a forum topic is the initial post to a threaded discussion). To
provide structure, both containers and forums may be placed inside other
containers and forums. To rearrange forums and containers, grab a
drag-and-drop handle under the <em>Name</em> column and drag the forum or
container to a new location in the list. (Grab a handle by clicking and
holding the mouse while hovering over a handle icon.) Remember that your
changes will not be saved until you click the <em>Save</em> button at the
bottom of the page.') . '</p>';
26128
    case 'admin/build/forum/add/container':
271
      return '<p>' . t('By grouping related or similar forums, containers
help organize forums. For example, a container named "Food" may hold two
forums named "Fruit" and "Vegetables", respectively.') . '</p>';
28127
    case 'admin/build/forum/add/forum':
292
      return '<p>' . t('A forum holds related or similar forum topics (a
forum topic is the initial post to a threaded discussion). For example, a
forum named "Fruit" may contain forum topics titled "Apples" and "Bananas",
respectively.') . '</p>';
30125
    case 'admin/build/forum/settings':
310
      return '<p>' . t('These settings allow you to adjust the display of
your forum topics. The content types available for use within a forum may
be selected by editing the <em>Content types</em> on the <a
href="@forum-vocabulary">forum vocabulary page</a>.',
array('@forum-vocabulary' => url('admin/content/taxonomy/edit/vocabulary/'
. variable_get('forum_nav_vocabulary', '')))) . '</p>';
320
  }
33125
}
34
35
/**
36
 * Implementation of hook_theme().
37
 */
38166
function forum_theme() {
39
  return array(
40
    'forums' => array(
411
      'template' => 'forums',
421
      'arguments' => array('forums' => NULL, 'topics' => NULL, 'parents' =>
NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
431
    ),
44
    'forum_list' => array(
451
      'template' => 'forum-list',
461
      'arguments' => array('forums' => NULL, 'parents' => NULL, 'tid' =>
NULL),
471
    ),
48
    'forum_topic_list' => array(
491
      'template' => 'forum-topic-list',
501
      'arguments' => array('tid' => NULL, 'topics' => NULL, 'sortby' =>
NULL, 'forum_per_page' => NULL),
511
    ),
52
    'forum_icon' => array(
531
      'template' => 'forum-icon',
541
      'arguments' => array('new_posts' => NULL, 'num_posts' => 0,
'comment_mode' => 0, 'sticky' => 0),
551
    ),
56
    'forum_topic_navigation' => array(
571
      'template' => 'forum-topic-navigation',
581
      'arguments' => array('node' => NULL),
591
    ),
60
    'forum_submitted' => array(
611
      'template' => 'forum-submitted',
621
      'arguments' => array('topic' => NULL),
631
    ),
641
  );
650
}
66
67
/**
68
 * Fetch a forum term.
69
 *
70
 * @param $tid
71
 *   The ID of the term which should be loaded.
72
 *
73
 * @return
74
 *   An associative array containing the term data or FALSE if the term
cannot be loaded, or is not part of the forum vocabulary.
75
 */
76166
function forum_term_load($tid) {
770
  $result = db_query(db_rewrite_sql('SELECT t.tid, t.vid, t.name,
t.description, t.weight FROM {term_data} t WHERE t.tid = %d AND t.vid =
%d', 't', 'tid'), $tid, variable_get('forum_nav_vocabulary', ''));
780
  return db_fetch_array($result);
790
}
80
81
/**
82
 * Implementation of hook_menu().
83
 */
84166
function forum_menu() {
853
  $items['forum'] = array(
863
    'title' => 'Forums',
873
    'page callback' => 'forum_page',
883
    'access arguments' => array('access content'),
893
    'type' => MENU_SUGGESTED_ITEM,
90
  );
913
  $items['admin/build/forum'] = array(
923
    'title' => 'Forums',
933
    'description' => 'Control forums and their hierarchy and change forum
settings.',
943
    'page callback' => 'drupal_get_form',
953
    'page arguments' => array('forum_overview'),
963
    'access arguments' => array('administer forums'),
97
  );
983
  $items['admin/build/forum/list'] = array(
993
    'title' => 'List',
1003
    'type' => MENU_DEFAULT_LOCAL_TASK,
1013
    'weight' => -10,
102
  );
1033
  $items['admin/build/forum/add/container'] = array(
1043
    'title' => 'Add container',
1053
    'page callback' => 'forum_form_main',
1063
    'page arguments' => array('container'),
1073
    'access arguments' => array('administer forums'),
1083
    'type' => MENU_LOCAL_TASK,
1093
    'parent' => 'admin/build/forum',
110
  );
1113
  $items['admin/build/forum/add/forum'] = array(
1123
    'title' => 'Add forum',
1133
    'page callback' => 'forum_form_main',
1143
    'page arguments' => array('forum'),
1153
    'access arguments' => array('administer forums'),
1163
    'type' => MENU_LOCAL_TASK,
1173
    'parent' => 'admin/build/forum',
118
  );
1193
  $items['admin/build/forum/settings'] = array(
1203
    'title' => 'Settings',
1213
    'page callback' => 'drupal_get_form',
1223
    'page arguments' => array('forum_admin_settings'),
1233
    'access arguments' => array('administer forums'),
1243
    'weight' => 5,
1253
    'type' => MENU_LOCAL_TASK,
1263
    'parent' => 'admin/build/forum',
127
  );
1283
  $items['admin/build/forum/edit/%forum_term'] = array(
1293
    'page callback' => 'forum_form_main',
1303
    'access arguments' => array('administer forums'),
1313
    'type' => MENU_CALLBACK,
132
  );
1333
  $items['admin/build/forum/edit/container/%forum_term'] = array(
1343
    'title' => 'Edit container',
1353
    'page callback' => 'forum_form_main',
1363
    'page arguments' => array('container', 5),
1373
    'access arguments' => array('administer forums'),
1383
    'type' => MENU_CALLBACK,
139
  );
1403
  $items['admin/build/forum/edit/forum/%forum_term'] = array(
1413
    'title' => 'Edit forum',
1423
    'page callback' => 'forum_form_main',
1433
    'page arguments' => array('forum', 5),
1443
    'access arguments' => array('administer forums'),
1453
    'type' => MENU_CALLBACK,
146
  );
1473
  return $items;
1480
}
149
150
151
/**
152
 * Implementation of hook_init().
153
 */
154166
function forum_init() {
155165
  drupal_add_css(drupal_get_path('module', 'forum') . '/forum.css');
156165
}
157
158
/**
159
 * _forum_nodeapi_check_node_type 
160
 * 
161
 * @param mixed $node 
162
 * @param mixed $vocabulary
163
 * @access protected
164
 * @return bool
165
 */
166166
function _forum_nodeapi_check_node_type($node, $vocabulary) {
167
  // We are going to return if $node->type is not one of the node
168
  // types assigned to the forum vocabulary.  If forum_nav_vocabulary
169
  // is undefined or the vocabulary does not exist, it clearly cannot
170
  // be assigned to $node->type, so return to avoid E_ALL warnings.
17194
  if (empty($vocabulary)) {
1720
    return FALSE;
1730
  }
174
175
  // Operate only on node types assigned for the forum vocabulary.
17694
  if (!in_array($node->type, $vocabulary->nodes)) {
1770
    return FALSE;
1780
  }
179
18094
  return TRUE;
1810
}
182
183
/**
184
 * Implementation of hook_nodeapi_view().
185
 */
186166
function forum_nodeapi_view(&$node, $teaser, $page) {
18755
  $vid = variable_get('forum_nav_vocabulary', '');
18855
  $vocabulary = taxonomy_vocabulary_load($vid);
18955
  if (_forum_nodeapi_check_node_type($node, $vocabulary)) {
19055
    if ($page && taxonomy_node_get_terms_by_vocabulary($node, $vid) &&
$tree = taxonomy_get_tree($vid)) {
191
      // Get the forum terms from the (cached) tree
19255
      foreach ($tree as $term) {
19355
        $forum_terms[] = $term->tid;
19455
      }
19555
      foreach ($node->taxonomy as $term_id => $term) {
19655
        if (in_array($term_id, $forum_terms)) {
19755
          $node->tid = $term_id;
19855
        }
19955
      }
200
      // Breadcrumb navigation
20155
      $breadcrumb[] = l(t('Home'), NULL);
20255
      $breadcrumb[] = l($vocabulary->name, 'forum');
20355
      if ($parents = taxonomy_get_parents_all($node->tid)) {
20455
        $parents = array_reverse($parents);
20555
        foreach ($parents as $p) {
20655
          $breadcrumb[] = l($p->name, 'forum/' . $p->tid);
20755
        }
20855
      }
20955
      drupal_set_breadcrumb($breadcrumb);
210
21155
      if (!$teaser) {
21255
        $node->content['forum_navigation'] = array(
21355
          '#markup' => theme('forum_topic_navigation', $node),
21455
          '#weight' => 100,
215
        );
21655
      }
21755
    }
21855
  }
21955
}
220
221
/**
222
 * Implementation of hook_nodeapi_prepare().
223
 */
224166
function forum_nodeapi_prepare(&$node, $teaser, $page) {
22517
  $vid = variable_get('forum_nav_vocabulary', '');
22617
  $vocabulary = taxonomy_vocabulary_load($vid);
22717
  if (_forum_nodeapi_check_node_type($node, $vocabulary)) {
22817
    if (empty($node->nid)) {
229
      // New topic
23011
      $node->taxonomy[arg(3)]->vid = $vid;
23111
      $node->taxonomy[arg(3)]->tid = arg(3);
23211
    }
23317
  }
23417
}
235
236
/**
237
 * Implementation of hook_nodeapi_validate().
238
 * 
239
 * Check in particular that only a "leaf" term in the associated taxonomy.
240
 */
241166
function forum_nodeapi_validate(&$node, $teaser, $page) {
24214
  $vid = variable_get('forum_nav_vocabulary', '');
24314
  $vocabulary = taxonomy_vocabulary_load($vid);
24414
  if (_forum_nodeapi_check_node_type($node, $vocabulary)) {
245
    // vocabulary is selected, not a "container" term.
24614
    if ($node->taxonomy) {
247
      // Extract the node's proper topic ID.
24814
      $vocabulary = $vid;
24914
      $containers = variable_get('forum_containers', array());
25014
      foreach ($node->taxonomy as $term) {
25114
        if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid
= %d AND vid = %d', $term, $vocabulary))) {
25214
          if (in_array($term, $containers)) {
2532
            $term = taxonomy_term_load($term);
2542
            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)));
2552
          }
25614
        }
25714
      }
25814
    }
25914
  }
26014
}
261
262
/**
263
 * Implementation of hook_nodeapi_presave().
264
 *
265
 * Assign forum taxonomy when adding a topic from within a forum.
266
 */
267166
function forum_nodeapi_presave(&$node, $teaser, $page) {
26812
  $vid = variable_get('forum_nav_vocabulary', '');
26912
  $vocabulary = taxonomy_vocabulary_load($vid);
27012
  if (_forum_nodeapi_check_node_type($node, $vocabulary)) {
271
    // Make sure all fields are set properly:
27212
    $node->icon = !empty($node->icon) ? $node->icon : '';
273
27412
    if ($node->taxonomy && $tree = taxonomy_get_tree($vid)) {
275
      // Get the forum terms from the (cached) tree if we have a taxonomy.
27612
      foreach ($tree as $term) {
27712
        $forum_terms[] = $term->tid;
27812
      }
27912
      foreach ($node->taxonomy as $term_id) {
28012
        if (in_array($term_id, $forum_terms)) {
28112
          $node->tid = $term_id;
28212
        }
28312
      }
28412
      $old_tid = db_result(db_query_range("SELECT t.tid FROM {term_node} t
INNER JOIN {node} n ON t.vid = n.vid WHERE n.nid = %d ORDER BY t.vid DESC",
$node->nid, 0, 1));
28512
      if ($old_tid && isset($node->tid) && ($node->tid != $old_tid) &&
!empty($node->shadow)) {
286
        // A shadow copy needs to be created. Retain new term and add old
term.
2873
        $node->taxonomy[] = $old_tid;
2883
      }
28912
    }
29012
  }
29112
}
292
293
/**
294
 * Implementation of hook_nodeapi_update().
295
 */
296166
function forum_nodeapi_update(&$node, $teaser, $page) {
2973
  $vid = variable_get('forum_nav_vocabulary', '');
2983
  $vocabulary = taxonomy_vocabulary_load($vid);
2993
  if (_forum_nodeapi_check_node_type($node, $vocabulary)) {
3003
    if (empty($node->revision) && db_result(db_query('SELECT tid FROM
{forum} WHERE nid=%d', $node->nid))) {
3013
      if (!empty($node->tid)) {
3023
        db_query('UPDATE {forum} SET tid = %d WHERE vid = %d', $node->tid,
$node->vid);
3033
      }
304
      // The node is removed from the forum.
305
      else {
3060
        db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid);
307
      }
3083
    }
309
    else {
3100
      if (!empty($node->tid)) {
3110
        db_query('INSERT INTO {forum} (tid, vid, nid) VALUES (%d, %d, %d)',
$node->tid, $node->vid, $node->nid);
3120
      }
313
    }
3143
  }
3153
}
316
317
/**
318
 * Implementation of hook_nodeapi_insert().
319
 */
320166
function forum_nodeapi_insert(&$node, $teaser, $page) {
3219
  $vid = variable_get('forum_nav_vocabulary', '');
3229
  $vocabulary = taxonomy_vocabulary_load($vid);
3239
  if (_forum_nodeapi_check_node_type($node, $vocabulary)) {
3249
    if (!empty($node->tid)) {
3259
      db_query('INSERT INTO {forum} (tid, vid, nid) VALUES (%d, %d, %d)',
$node->tid, $node->vid, $node->nid);
3269
    }
3279
  }
3289
}
329
330
/**
331
 * Implementation of hook_nodeapi_delete().
332
 */
333166
function forum_nodeapi_delete(&$node, $teaser, $page) {
3343
  $vid = variable_get('forum_nav_vocabulary', '');
3353
  $vocabulary = taxonomy_vocabulary_load($vid);
3363
  if (_forum_nodeapi_check_node_type($node, $vocabulary)) {
3373
    db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid);
3383
  }
3393
}
340
341
/**
342
 * Implementation of hook_nodeapi_load().
343
 */
344166
function forum_nodeapi_load(&$node, $teaser, $page) {
34572
  $vid = variable_get('forum_nav_vocabulary', '');
34672
  $vocabulary = taxonomy_vocabulary_load($vid);
34772
  if (_forum_nodeapi_check_node_type($node, $vocabulary)) {
34872
    return db_fetch_array(db_query('SELECT tid AS forum_tid FROM {forum}
WHERE vid = %d', $node->vid));
3490
  }
3500
}
351
352
/**
353
 * Implementation of hook_node_info().
354
 */
355166
function forum_node_info() {
356
  return array(
357
    'forum' => array(
358152
      'name' => t('Forum topic'),
359152
      'base' => 'forum',
360152
      'description' => t('A <em>forum topic</em> is the initial post to a
new discussion thread within a forum.'),
361152
      'title_label' => t('Subject'),
362
    )
363152
  );
3640
}
365
366
/**
367
 * Implementation of hook_access().
368
 */
369166
function forum_access($op, $node, $account) {
370
  switch ($op) {
371150
    case 'create':
372144
      return user_access('create forum content', $account);
37371
    case 'update':
37465
      return user_access('edit any forum content', $account) ||
(user_access('edit own forum content', $account) && ($account->uid ==
$node->uid));
37567
    case 'delete':
37612
      return user_access('delete any forum content', $account) ||
(user_access('delete own forum content', $account) && ($account->uid ==
$node->uid));
3770
  }
37861
}
379
380
/**
381
 * Implementation of hook_perm().
382
 */
383166
function forum_perm() {
384
  $perms = array(
385
    'administer forums' => array(
3861
      'title' => t('Administer forums'),
3871
      'description' => t('Manage forums and configure forum administration
settings.'),
3881
    ),
3891
  );
3901
  $perms += node_list_permissions('forum');
3911
  return $perms;
3920
}
393
394
/**
395
 * Implementation of hook_taxonomy().
396
 */
397166
function forum_taxonomy($op, $type, $term = NULL) {
3985
  if ($op == 'delete' && $term['vid'] ==
variable_get('forum_nav_vocabulary', '')) {
399
    switch ($type) {
4000
      case 'term':
4010
        $results = db_query('SELECT tn.nid FROM {term_node} tn WHERE tn.tid
= %d', $term['tid']);
4020
        while ($node = db_fetch_object($results)) {
403
          // node_delete will also remove any association with non-forum
vocabularies.
4040
          node_delete($node->nid);
4050
        }
406
407
        // For containers, remove the tid from the forum_containers
variable.
4080
        $containers = variable_get('forum_containers', array());
4090
        $key = array_search($term['tid'], $containers);
4100
        if ($key !== FALSE) {
4110
          unset($containers[$key]);
4120
        }
4130
        variable_set('forum_containers', $containers);
4140
        break;
4150
      case 'vocabulary':
4160
        variable_del('forum_nav_vocabulary');
4170
    }
4180
  }
4195
}
420
421
/**
422
 * Implementation of hook_form_alter().
423
 */
424166
function forum_form_alter(&$form, $form_state, $form_id) {
42563
  $vid = variable_get('forum_nav_vocabulary', '');
42663
  if (isset($form['vid']) && $form['vid']['#value'] == $vid) {
427
    // Hide critical options from forum vocabulary.
4288
    if ($form_id == 'taxonomy_form_vocabulary') {
4292
      $form['help_forum_vocab'] = array(
4302
        '#markup' => t('This is the designated forum vocabulary. Some of
the normal vocabulary options have been removed.'),
4312
        '#weight' => -1,
432
      );
4332
      $form['content_types']['nodes']['#required'] = TRUE;
4342
      $form['hierarchy'] = array('#type' => 'value', '#value' => 1);
4352
      $form['settings']['required'] = array('#type' => 'value', '#value' =>
FALSE);
4362
      $form['settings']['relations'] = array('#type' => 'value', '#value'
=> FALSE);
4372
      $form['settings']['tags'] = array('#type' => 'value', '#value' =>
FALSE);
4382
      $form['settings']['multiple'] = array('#type' => 'value', '#value' =>
FALSE);
4392
      unset($form['delete']);
4402
    }
441
    // Hide multiple parents select from forum terms.
4426
    elseif ($form_id == 'taxonomy_form_term') {
4430
      $form['advanced']['parent']['#access'] = FALSE;
4440
    }
4458
  }
44663
  if ($form_id == 'forum_node_form') {
447
    // Make the vocabulary required for 'real' forum-nodes.
44817
    $vid = variable_get('forum_nav_vocabulary', '');
44917
    $form['taxonomy'][$vid]['#required'] = TRUE;
45017
    $form['taxonomy'][$vid]['#options'][''] = t('- Please choose -');
45117
  }
45263
}
453
454
/**
455
 * Implementation of hook_load().
456
 */
457166
function forum_load($node) {
45872
  $forum = db_fetch_object(db_query('SELECT * FROM {term_node} WHERE vid =
%d', $node->vid));
459
46072
  return $forum;
4610
}
462
463
/**
464
 * Implementation of hook_block().
465
 *
466
 * Generates a block containing the currently active forum topics and the
467
 * most recently added forum topics.
468
 */
469166
function forum_block($op = 'list', $delta = '', $edit = array()) {
470
  switch ($op) {
471133
    case 'list':
4726
      $blocks['active']['info'] = t('Active forum topics');
4736
      $blocks['new']['info'] = t('New forum topics');
4746
      return $blocks;
475
476130
    case 'configure':
4770
      $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)));
4780
      return $form;
479
480130
    case 'save':
4810
      variable_set('forum_block_num_' . $delta, $edit['forum_block_num_' .
$delta]);
4820
      break;
483
484130
    case 'view':
485130
      if (user_access('access content')) {
486
        switch ($delta) {
487130
          case 'active':
488130
            $title = t('Active forum topics');
489130
            $sql = db_rewrite_sql("SELECT n.nid, n.title, l.comment_count,
l.last_comment_timestamp FROM {node} n INNER JOIN {term_node} tn ON tn.vid
= n.vid INNER JOIN {term_data} td ON td.tid = tn.tid INNER JOIN
{node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 AND td.vid
= %d ORDER BY l.last_comment_timestamp DESC");
490130
            $result = db_query_range($sql,
variable_get('forum_nav_vocabulary', ''), 0,
variable_get('forum_block_num_active', '5'));
491130
            $content = node_title_list($result);
492130
            break;
493
494128
          case 'new':
495128
            $title = t('New forum topics');
496128
            $sql = db_rewrite_sql("SELECT n.nid, n.title, l.comment_count
FROM {node} n INNER JOIN {term_node} tn ON tn.vid = n.vid INNER JOIN
{term_data} td ON td.tid = tn.tid INNER JOIN {node_comment_statistics} l ON
n.nid = l.nid WHERE n.status = 1 AND td.vid = %d ORDER BY n.nid DESC");
497128
            $result = db_query_range($sql,
variable_get('forum_nav_vocabulary', ''), 0,
variable_get('forum_block_num_new', '5'));
498128
            $content = node_title_list($result);
499128
            break;
5000
        }
501
502130
        if (!empty($content)) {
503116
          $block['subject'] = $title;
504116
          $block['content'] = $content . theme('more_link', url('forum'),
t('Read the latest forum topics.'));
505116
          return $block;
5060
        }
50714
      }
50814
  }
50914
}
510
511
/**
512
 * Implementation of hook_form().
513
 */
514166
function forum_form(&$node, $form_state) {
51517
  $type = node_get_types('type', $node);
51617
  $form['title'] = array('#type' => 'textfield', '#title' =>
check_plain($type->title_label), '#default_value' => !empty($node->title) ?
$node->title : '', '#required' => TRUE, '#weight' => -5);
517
51817
  if (!empty($node->nid)) {
5196
    $vid = variable_get('forum_nav_vocabulary', '');
5206
    $forum_terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
521
    // if editing, give option to leave shadows
5226
    $shadow = (count($forum_terms) > 1);
5236
    $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.'));
5246
  }
525
52617
  $form['body_field'] = node_body_field($node, $type->body_label, 1);
527
52817
  $form['#submit'][] = 'forum_submit';
529
  // Assign the forum topic submit handler.
530
53117
  return $form;
5320
}
533
534
/**
535
 * Implementation of hook_term_path().
536
 */
537166
function forum_term_path($term) {
53855
  return 'forum/' . $term->tid;
5390
}
540
541
/**
542
 * Returns a list of all forums for a given taxonomy id
543
 *
544
 * Forum objects contain the following fields
545
 * -num_topics Number of topics in the forum
546
 * -num_posts Total number of posts in all topics
547
 * -last_post Most recent post for the forum
548
 *
549
 * @param $tid
550
 *   Taxonomy ID of the vocabulary that holds the forum list.
551
 * @return
552
 *   Array of object containing the forum information.
553
 */
554166
function forum_get_forums($tid = 0) {
555
55612
  $forums = array();
55712
  $vid = variable_get('forum_nav_vocabulary', '');
55812
  $_forums = taxonomy_get_tree($vid, $tid);
559
56012
  if (count($_forums)) {
561
5624
    $counts = array();
563
5644
    $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.vid = r.vid WHERE n.status = 1
GROUP BY r.tid";
5654
    $sql = db_rewrite_sql($sql);
5664
    $_counts = db_query($sql);
5674
    while ($count = db_fetch_object($_counts)) {
5684
      $counts[$count->tid] = $count;
5694
    }
5704
  }
571
57212
  foreach ($_forums as $forum) {
5734
    if (in_array($forum->tid, variable_get('forum_containers', array()))) {
5740
      $forum->container = 1;
5750
    }
576
5774
    if (!empty($counts[$forum->tid])) {
5784
      $forum->num_topics = $counts[$forum->tid]->topic_count;
5794
      $forum->num_posts = $counts[$forum->tid]->topic_count +
$counts[$forum->tid]->comment_count;
5804
    }
581
    else {
5820
      $forum->num_topics = 0;
5830
      $forum->num_posts = 0;
584
    }
585
586
    // This query does not use full ANSI syntax since MySQL 3.x does not
support
587
    // table1 INNER JOIN table2 INNER JOIN table3 ON table2_criteria ON
table3_criteria
588
    // used to join node_comment_statistics to users.
5894
    $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.vid = tn.vid 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 tn.tid = %d ORDER BY
ncs.last_comment_timestamp DESC";
5904
    $sql = db_rewrite_sql($sql);
5914
    $topic = db_fetch_object(db_query_range($sql, $forum->tid, 0, 1));
592
5934
    $last_post = new stdClass();
5944
    if (!empty($topic->last_comment_timestamp)) {
5954
      $last_post->timestamp = $topic->last_comment_timestamp;
5964
      $last_post->name = $topic->last_comment_name;
5974
      $last_post->uid = $topic->last_comment_uid;
5984
    }
5994
    $forum->last_post = $last_post;
600
6014
    $forums[$forum->tid] = $forum;
6024
  }
603
60412
  return $forums;
6050
}
606
607
/**
608
 * Calculate the number of nodes the user has not yet read and are newer
609
 * than NODE_NEW_LIMIT.
610
 */
611166
function _forum_topics_unread($term, $uid) {
6124
  $sql = "SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON
n.vid = tn.vid AND tn.tid = %d LEFT JOIN {history} h ON n.nid = h.nid AND
h.uid = %d WHERE n.status = 1 AND n.created > %d AND h.nid IS NULL";
6134
  $sql = db_rewrite_sql($sql);
6144
  return db_result(db_query($sql, $term, $uid, NODE_NEW_LIMIT));
6150
}
616
617166
function forum_get_topics($tid, $sortby, $forum_per_page) {
6188
  global $user, $forum_topic_list_header;
619
620
  $forum_topic_list_header = array(
6218
    NULL,
6228
    array('data' => t('Topic'), 'field' => 'n.title'),
6238
    array('data' => t('Replies'), 'field' => 'l.comment_count'),
6248
    array('data' => t('Created'), 'field' => 'n.created'),
6258
    array('data' => t('Last reply'), 'field' =>
'l.last_comment_timestamp'),
6268
  );
627
6288
  $order = _forum_get_topic_order($sortby);
6298
  for ($i = 0; $i < count($forum_topic_list_header); $i++) {
6308
    if ($forum_topic_list_header[$i]['field'] == $order['field']) {
6318
      $forum_topic_list_header[$i]['sort'] = $order['sort'];
6328
    }
6338
  }
634
6358
  $term = taxonomy_term_load($tid);
636
6378
  $sql = db_rewrite_sql("SELECT n.nid, r.tid, n.title, n.type, 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, f.tid AS forum_tid FROM
{node_comment_statistics} l INNER JOIN {node} n ON n.nid = l.nid INNER JOIN
{users} cu ON l.last_comment_uid = cu.uid INNER JOIN {term_node} r ON n.vid
= r.vid INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {forum} f ON n.vid
= f.vid WHERE n.status = 1 AND r.tid = %d");
6388
  $sql .= tablesort_sql($forum_topic_list_header, 'n.sticky DESC,');
6398
  $sql .= ', n.created DESC';  // Always add a secondary sort order so that
the news forum topics are on top.
640
6418
  $sql_count = db_rewrite_sql("SELECT COUNT(n.nid) FROM {node} n INNER JOIN
{term_node} r ON n.vid = r.vid AND r.tid = %d WHERE n.status = 1");
642
6438
  $result = pager_query($sql, $forum_per_page, 0, $sql_count, $tid);
6448
  $topics = array();
6458
  while ($topic = db_fetch_object($result)) {
6464
    if ($user->uid) {
647
      // folder is new if topic is new or there are new comments since last
visit
6484
      if ($topic->tid != $tid) {
6490
        $topic->new = 0;
6500
      }
651
      else {
6524
        $history = _forum_user_last_visit($topic->nid);
6534
        $topic->new_replies = comment_num_new($topic->nid, $history);
6544
        $topic->new = $topic->new_replies || ($topic->timestamp >
$history);
655
      }
6564
    }
657
    else {
658
      // Do not track "new replies" status for topics if the user is
anonymous.
6590
      $topic->new_replies = 0;
6600
      $topic->new = 0;
661
    }
662
6634
    if ($topic->num_comments > 0) {
6640
      $last_reply = new stdClass();
6650
      $last_reply->timestamp = $topic->last_comment_timestamp;
6660
      $last_reply->name = $topic->last_comment_name;
6670
      $last_reply->uid = $topic->last_comment_uid;
6680
      $topic->last_reply = $last_reply;
6690
    }
6704
    $topics[] = $topic;
6714
  }
672
6738
  return $topics;
6740
}
675
676
/**
677
 * Finds the first unread node for a given forum.
678
 */
679166
function _forum_new($tid) {
6800
  global $user;
681
6820
  $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 h.nid IS NULL AND n.created > %d ORDER BY created";
6830
  $sql = db_rewrite_sql($sql);
6840
  $nid = db_result(db_query_range($sql, $user->uid, $tid, NODE_NEW_LIMIT,
0, 1));
685
6860
  return $nid ? $nid : 0;
6870
}
688
689
/**
690
 * Process variables for forums.tpl.php
691
 *
692
 * The $variables array contains the following arguments:
693
 * - $forums
694
 * - $topics
695
 * - $parents
696
 * - $tid
697
 * - $sortby
698
 * - $forum_per_page
699
 *
700
 * @see forums.tpl.php
701
 */
702166
function template_preprocess_forums(&$variables) {
70312
  global $user;
704
70512
  $vid = variable_get('forum_nav_vocabulary', '');
70612
  $vocabulary = taxonomy_vocabulary_load($vid);
70712
  $title = !empty($vocabulary->name) ? $vocabulary->name : '';
708
709
  // Breadcrumb navigation:
71012
  $breadcrumb[] = l(t('Home'), NULL);
71112
  if ($variables['tid']) {
71212
    $breadcrumb[] = l($vocabulary->name, 'forum');
71312
  }
71412
  if ($variables['parents']) {
71512
    $variables['parents'] = array_reverse($variables['parents']);
71612
    foreach ($variables['parents'] as $p) {
71712
      if ($p->tid == $variables['tid']) {
71812
        $title = $p->name;
71912
      }
720
      else {
7214
        $breadcrumb[] = l($p->name, 'forum/' . $p->tid);
722
      }
72312
    }
72412
  }
72512
  drupal_set_breadcrumb($breadcrumb);
72612
  drupal_set_title($title);
727
72812
  if ($variables['forums_defined'] = count($variables['forums']) ||
count($variables['parents'])) {
729
    // Format the "post new content" links listing.
73012
    $forum_types = array();
731
732
    // Loop through all node types for forum vocabulary.
73312
    foreach ($vocabulary->nodes as $type) {
734
      // Check if the current user has the 'create' permission for this
node type.
73512
      if (node_access('create', $type)) {
736
        // Fetch the "General" name of the content type;
737
        // Push the link with title and url to the array.
73812
        $forum_types[$type] = array('title' => t('Post new @node_type',
array('@node_type' => node_get_types('name', $type))), 'href' =>
'node/add/' . str_replace('_', '-', $type) . '/' . $variables['tid']);
73912
      }
74012
    }
741
74212
    if (empty($forum_types)) {
743
      // The user is logged-in; but denied access to create any new forum
content type.
7440
      if ($user->uid) {
7450
        $forum_types['disallowed'] = array('title' => t('You are not
allowed to post new content in forum.'));
7460
      }
747
      // The user is not logged-in; and denied access to create any new
forum content type.
748
      else {
7490
        $forum_types['login'] = array('title' => t('<a
href="@login">Login</a> to post new content in forum.', array('@login' =>
url('user/login', array('query' => drupal_get_destination())))), 'html' =>
TRUE);
750
      }
7510
    }
75212
    $variables['links'] = $forum_types;
753
75412
    if (!empty($variables['forums'])) {
7554
      $variables['forums'] = theme('forum_list', $variables['forums'],
$variables['parents'], $variables['tid']);
7564
    }
757
    else {
7588
      $variables['forums'] = '';
759
    }
760
76112
    if ($variables['tid'] && !in_array($variables['tid'],
variable_get('forum_containers', array()))) {
7628
      $variables['topics'] = theme('forum_topic_list', $variables['tid'],
$variables['topics'], $variables['sortby'], $variables['forum_per_page']);
7638
      drupal_add_feed(url('taxonomy/term/' . $variables['tid'] .
'/0/feed'), 'RSS - ' . $title);
7648
    }
765
    else {
7664
      $variables['topics'] = '';
767
    }
768
769
    // Provide separate template suggestions based on what's being output.
Topic id is also accounted for.
770
    // Check both variables to be safe then the inverse. Forums with topic
ID's take precedence.
77112
    if ($variables['forums'] && !$variables['topics']) {
7724
      $variables['template_files'][] = 'forums-containers';
7734
      $variables['template_files'][] = 'forums-' . $variables['tid'];
7744
      $variables['template_files'][] = 'forums-containers-' .
$variables['tid'];
7754
    }
7768
    elseif (!$variables['forums'] && $variables['topics']) {
7778
      $variables['template_files'][] = 'forums-topics';
7788
      $variables['template_files'][] = 'forums-' . $variables['tid'];
7798
      $variables['template_files'][] = 'forums-topics-' .
$variables['tid'];
7808
    }
781
    else {
7820
      $variables['template_files'][] = 'forums-' . $variables['tid'];
783
    }
784
78512
  }
786
  else {
7870
    drupal_set_title(t('No forums defined'), PASS_THROUGH);
7880
    $variables['links'] = array();
7890
    $variables['forums'] = '';
7900
    $variables['topics'] = '';
791
  }
79212
}
793
794
/**
795
 * Process variables to format a forum listing.
796
 *
797
 * $variables contains the following information:
798
 * - $forums
799
 * - $parents
800
 * - $tid
801
 *
802
 * @see forum-list.tpl.php
803
 * @see theme_forum_list()
804
 */
805166
function template_preprocess_forum_list(&$variables) {
8064
  global $user;
8074
  $row = 0;
808
  // Sanitize each forum so that the template can safely print the data.
8094
  foreach ($variables['forums'] as $id => $forum) {
8104
    $variables['forums'][$id]->description = !empty($forum->description) ?
filter_xss_admin($forum->description) : '';
8114
    $variables['forums'][$id]->link = url("forum/$forum->tid");
8124
    $variables['forums'][$id]->name = check_plain($forum->name);
8134
    $variables['forums'][$id]->is_container = !empty($forum->container);
8144
    $variables['forums'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
8154
    $row++;
816
8174
    $variables['forums'][$id]->new_text = '';
8184
    $variables['forums'][$id]->new_url = '';
8194
    $variables['forums'][$id]->new_topics = 0;
8204
    $variables['forums'][$id]->old_topics = $forum->num_topics;
8214
    if ($user->uid) {
8224
      $variables['forums'][$id]->new_topics =
_forum_topics_unread($forum->tid, $user->uid);
8234
      if ($variables['forums'][$id]->new_topics) {
8244
        $variables['forums'][$id]->new_text =
format_plural($variables['forums'][$id]->new_topics, '1 new', '@count
new');
8254
        $variables['forums'][$id]->new_url = url("forum/$forum->tid",
array('fragment' => 'new'));
8264
      }
8274
      $variables['forums'][$id]->old_topics = $forum->num_topics -
$variables['forums'][$id]->new_topics;
8284
    }
8294
    $variables['forums'][$id]->last_reply = theme('forum_submitted',
$forum->last_post);
8304
  }
831
  // Give meaning to $tid for themers. $tid actually stands for term id.
8324
  $variables['forum_id'] = $variables['tid'];
8334
  unset($variables['tid']);
8344
}
835
836
/**
837
 * Preprocess variables to format the topic listing.
838
 *
839
 * $variables contains the following data:
840
 * - $tid
841
 * - $topics
842
 * - $sortby
843
 * - $forum_per_page
844
 *
845
 * @see forum-topic-list.tpl.php
846
 * @see theme_forum_topic_list()
847
 */
848166
function template_preprocess_forum_topic_list(&$variables) {
8498
  global $forum_topic_list_header;
850
851
  // Create the tablesorting header.
8528
  $ts = tablesort_init($forum_topic_list_header);
8538
  $header = '';
8548
  foreach ($forum_topic_list_header as $cell) {
8558
    $cell = tablesort_header($cell, $forum_topic_list_header, $ts);
8568
    $header .= _theme_table_cell($cell, TRUE);
8578
  }
8588
  $variables['header'] = $header;
859
8608
  if (!empty($variables['topics'])) {
8614
    $row = 0;
8624
    foreach ($variables['topics'] as $id => $topic) {
8634
      $variables['topics'][$id]->icon = theme('forum_icon', $topic->new,
$topic->num_comments, $topic->comment_mode, $topic->sticky);
8644
      $variables['topics'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
8654
      $row++;
866
867
      // We keep the actual tid in forum table, if it's different from the
868
      // current tid then it means the topic appears in two forums, one of
869
      // them is a shadow copy.
8704
      if ($topic->forum_tid != $variables['tid']) {
8710
        $variables['topics'][$id]->moved = TRUE;
8720
        $variables['topics'][$id]->title = check_plain($topic->title);
8730
        $variables['topics'][$id]->message = l(t('This topic has been
moved'), "forum/$topic->forum_tid");
8740
      }
875
      else {
8764
        $variables['topics'][$id]->moved = FALSE;
8774
        $variables['topics'][$id]->title = l($topic->title,
"node/$topic->nid");
8784
        $variables['topics'][$id]->message = '';
879
      }
8804
      $variables['topics'][$id]->created = theme('forum_submitted',
$topic);
8814
      $variables['topics'][$id]->last_reply = theme('forum_submitted',
isset($topic->last_reply) ? $topic->last_reply : NULL);
882
8834
      $variables['topics'][$id]->new_text = '';
8844
      $variables['topics'][$id]->new_url = '';
8854
      if ($topic->new_replies) {
8860
        $variables['topics'][$id]->new_text =
format_plural($topic->new_replies, '1 new', '@count new');
8870
        $variables['topics'][$id]->new_url = url("node/$topic->nid",
array('query' => comment_new_page_count($topic->num_comments,
$topic->new_replies, $topic), 'fragment' => 'new'));
8880
      }
889
8904
    }
8914
  }
892
  else {
893
    // Make this safe for the template
8944
    $variables['topics'] = array();
895
  }
896
  // Give meaning to $tid for themers. $tid actually stands for term id.
8978
  $variables['topic_id'] = $variables['tid'];
8988
  unset($variables['tid']);
899
9008
  $variables['pager'] = theme('pager', NULL, $variables['forum_per_page'],
0);
9018
}
902
903
/**
904
 * Process variables to format the icon for each individual topic.
905
 *
906
 * $variables contains the following data:
907
 * - $new_posts
908
 * - $num_posts = 0
909
 * - $comment_mode = 0
910
 * - $sticky = 0
911
 *
912
 * @see forum-icon.tpl.php
913
 * @see theme_forum_icon()
914
 */
915166
function template_preprocess_forum_icon(&$variables) {
9164
  $variables['hot_threshold'] = variable_get('forum_hot_topic', 15);
9174
  if ($variables['num_posts'] > $variables['hot_threshold']) {
9180
    $variables['icon'] = $variables['new_posts'] ? 'hot-new' : 'hot';
9190
  }
920
  else {
9214
    $variables['icon'] = $variables['new_posts'] ? 'new' : 'default';
922
  }
923
9244
  if ($variables['comment_mode'] == COMMENT_NODE_READ_ONLY ||
$variables['comment_mode'] == COMMENT_NODE_DISABLED) {
9250
    $variables['icon'] = 'closed';
9260
  }
927
9284
  if ($variables['sticky'] == 1) {
9290
    $variables['icon'] = 'sticky';
9300
  }
9314
}
932
933
/**
934
 * Preprocess variables to format the next/previous forum topic navigation
links.
935
 *
936
 * $variables contains $node.
937
 *
938
 * @see forum-topic-navigation.tpl.php
939
 * @see theme_forum_topic_navigation()
940
 */
941166
function template_preprocess_forum_topic_navigation(&$variables) {
94255
  $output = '';
943
944
  // get previous and next topic
94555
  $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 ORDER BY n.sticky DESC, " .
_forum_get_topic_order_sql(variable_get('forum_order', 1));
94655
  $result = db_query(db_rewrite_sql($sql), isset($variables['node']->tid) ?
$variables['node']->tid : 0);
947
94855
  $stop = $variables['prev'] = $variables['next'] = 0;
949
95055
  while ($topic = db_fetch_object($result)) {
95155
    if ($stop == 1) {
95247
      $variables['next'] = $topic->nid;
95347
      $variables['next_title'] = check_plain($topic->title);
95447
      $variables['next_url'] = url("node/$topic->nid");
95547
      break;
9560
    }
95755
    if ($topic->nid == $variables['node']->nid) {
95855
      $stop = 1;
95955
    }
960
    else {
96124
      $variables['prev'] = $topic->nid;
96224
      $variables['prev_title'] = check_plain($topic->title);
96324
      $variables['prev_url'] = url("node/$topic->nid");
964
    }
96555
  }
96655
}
967
968
/**
969
 * Process variables to format submission info for display in the forum
list and topic list.
970
 *
971
 * $variables will contain: $topic
972
 *
973
 * @see forum-submitted.tpl.php
974
 * @see theme_forum_submitted()
975
 */
976166
function template_preprocess_forum_submitted(&$variables) {
9778
  $variables['author'] = isset($variables['topic']->uid) ?
theme('username', $variables['topic']) : '';
9788
  $variables['time'] = isset($variables['topic']->timestamp) ?
format_interval(REQUEST_TIME - $variables['topic']->timestamp) : '';
9798
}
980
981166
function _forum_user_last_visit($nid) {
9824
  global $user;
9834
  static $history = array();
984
9854
  if (empty($history)) {
9864
    $result = db_query('SELECT nid, timestamp FROM {history} WHERE uid =
%d', $user->uid);
9874
    while ($t = db_fetch_object($result)) {
9884
      $history[$t->nid] = $t->timestamp > NODE_NEW_LIMIT ? $t->timestamp :
NODE_NEW_LIMIT;
9894
    }
9904
  }
9914
  return isset($history[$nid]) ? $history[$nid] : NODE_NEW_LIMIT;
9920
}
993
994166
function _forum_get_topic_order($sortby) {
995
  switch ($sortby) {
99663
    case 1:
99763
      return array('field' => 'l.last_comment_timestamp', 'sort' =>
'desc');
9980
      break;
9990
    case 2:
10000
      return array('field' => 'l.last_comment_timestamp', 'sort' => 'asc');
10010
      break;
10020
    case 3:
10030
      return array('field' => 'l.comment_count', 'sort' => 'desc');
10040
      break;
10050
    case 4:
10060
      return array('field' => 'l.comment_count', 'sort' => 'asc');
10070
      break;
10080
  }
10090
}
1010
1011166
function _forum_get_topic_order_sql($sortby) {
101255
  $order = _forum_get_topic_order($sortby);
101355
  return $order['field'] . ' ' . strtoupper($order['sort']);
10140
}
1015166