Drupal PHP Cross Reference Content Management Systems

Source: /modules/forum/forum.module - 1389 lines - 48884 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Provides discussion forums.
   6   */
   7  
   8  /**
   9   * Implements hook_help().
  10   */
  11  function forum_help($path, $arg) {
  12    switch ($path) {
  13      case 'admin/help#forum':
  14        $output = '';
  15        $output .= '<h3>' . t('About') . '</h3>';
  16        $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. In a forum, users post topics and threads in nested hierarchies, allowing discussions to be categorized and grouped. The forum hierarchy consists of:') . '</p>';
  17        $output .= '<ul>';
  18        $output .= '<li>' . t('Optional containers (for example, <em>Support</em>), which can hold:') . '</li>';
  19        $output .= '<ul><li>' . t('Forums (for example, <em>Installing Drupal</em>), which can hold:') . '</li>';
  20        $output .= '<ul><li>' . t('Forum topics submitted by users (for example, <em>How to start a Drupal 6 Multisite</em>), which start discussions and are starting points for:') . '</li>';
  21        $output .= '<ul><li>' . t('Threaded comments submitted by users (for example, <em>You have these options...</em>).') . '</li>';
  22        $output .= '</ul>';
  23        $output .= '</ul>';
  24        $output .= '</ul>';
  25        $output .= '</ul>';
  26        $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@forum">Forum module</a>.', array('@forum' => 'http://drupal.org/documentation/modules/forum')) . '</p>';
  27        $output .= '<h3>' . t('Uses') . '</h3>';
  28        $output .= '<dl>';
  29        $output .= '<dt>' . t('Setting up forum structure') . '</dt>';
  30        $output .= '<dd>' . t('Visit the <a href="@forums">Forums page</a> to set up containers and forums to hold your discussion topics.', array('@forums' => url('admin/structure/forum'))) . '</dd>';
  31        $output .= '<dt>' . t('Starting a discussion') . '</dt>';
  32        $output .= '<dd>' . t('The <a href="@create-topic">Forum topic</a> link on the <a href="@content-add">Add new content</a> page creates the first post of a new threaded discussion, or thread.', array('@create-topic' => url('node/add/forum'), '@content-add' => url('node/add'))) . '</dd>';
  33        $output .= '<dt>' . t('Navigation') . '</dt>';
  34        $output .= '<dd>' . t('Enabling the Forum module provides a default <em>Forums</em> menu item in the navigation menu that links to the <a href="@forums">Forums page</a>.', array('@forums' => url('forum'))) . '</dd>';
  35        $output .= '<dt>' . t('Moving forum topics') . '</dt>';
  36        $output .= '<dd>' . t('A forum topic (and all of its comments) may be moved between forums by selecting a different forum while editing a forum topic. 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.') . '</dd>';
  37        $output .= '<dt>' . t('Locking and disabling comments') . '</dt>';
  38        $output .= '<dd>' . t('Selecting <em>Closed</em> under <em>Comment settings</em> while editing a forum topic will lock (prevent new comments on) the thread. Selecting <em>Hidden</em> under <em>Comment settings</em> while editing a forum topic will hide all existing comments on the thread, and prevent new ones.') . '</dd>';
  39        $output .= '</dl>';
  40        return $output;
  41      case 'admin/structure/forum':
  42        $output = '<p>' . t('Forums contain forum topics. Use containers to group related forums.') . '</p>';
  43        $output .= theme('more_help_link', array('url' => 'admin/help/forum'));
  44        return $output;
  45      case 'admin/structure/forum/add/container':
  46        return '<p>' . t('Use containers to group related forums.') . '</p>';
  47      case 'admin/structure/forum/add/forum':
  48        return '<p>' . t('A forum holds related forum topics.') . '</p>';
  49      case 'admin/structure/forum/settings':
  50        return '<p>' . t('Adjust the display of your forum topics. Organize the forums on the <a href="@forum-structure">forum structure page</a>.', array('@forum-structure' => url('admin/structure/forum'))) . '</p>';
  51    }
  52  }
  53  
  54  /**
  55   * Implements hook_theme().
  56   */
  57  function forum_theme() {
  58    return array(
  59      'forums' => array(
  60        'template' => 'forums',
  61        'variables' => array('forums' => NULL, 'topics' => NULL, 'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
  62      ),
  63      'forum_list' => array(
  64        'template' => 'forum-list',
  65        'variables' => array('forums' => NULL, 'parents' => NULL, 'tid' => NULL),
  66      ),
  67      'forum_topic_list' => array(
  68        'template' => 'forum-topic-list',
  69        'variables' => array('tid' => NULL, 'topics' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
  70      ),
  71      'forum_icon' => array(
  72        'template' => 'forum-icon',
  73        'variables' => array('new_posts' => NULL, 'num_posts' => 0, 'comment_mode' => 0, 'sticky' => 0, 'first_new' => FALSE),
  74      ),
  75      'forum_submitted' => array(
  76        'template' => 'forum-submitted',
  77        'variables' => array('topic' => NULL),
  78      ),
  79      'forum_form' => array(
  80        'render element' => 'form',
  81        'file' => 'forum.admin.inc',
  82      ),
  83    );
  84  }
  85  
  86  /**
  87   * Implements hook_menu().
  88   */
  89  function forum_menu() {
  90    $items['forum'] = array(
  91      'title' => 'Forums',
  92      'page callback' => 'forum_page',
  93      'access arguments' => array('access content'),
  94      'file' => 'forum.pages.inc',
  95    );
  96    $items['forum/%forum_forum'] = array(
  97      'title' => 'Forums',
  98      'page callback' => 'forum_page',
  99      'page arguments' => array(1),
 100      'access arguments' => array('access content'),
 101      'file' => 'forum.pages.inc',
 102    );
 103    $items['admin/structure/forum'] = array(
 104      'title' => 'Forums',
 105      'description' => 'Control forum hierarchy settings.',
 106      'page callback' => 'drupal_get_form',
 107      'page arguments' => array('forum_overview'),
 108      'access arguments' => array('administer forums'),
 109      'file' => 'forum.admin.inc',
 110    );
 111    $items['admin/structure/forum/list'] = array(
 112      'title' => 'List',
 113      'type' => MENU_DEFAULT_LOCAL_TASK,
 114      'weight' => -10,
 115    );
 116    $items['admin/structure/forum/add/container'] = array(
 117      'title' => 'Add container',
 118      'page callback' => 'forum_form_main',
 119      'page arguments' => array('container'),
 120      'access arguments' => array('administer forums'),
 121      'type' => MENU_LOCAL_ACTION,
 122      'parent' => 'admin/structure/forum',
 123      'file' => 'forum.admin.inc',
 124    );
 125    $items['admin/structure/forum/add/forum'] = array(
 126      'title' => 'Add forum',
 127      'page callback' => 'forum_form_main',
 128      'page arguments' => array('forum'),
 129      'access arguments' => array('administer forums'),
 130      'type' => MENU_LOCAL_ACTION,
 131      'parent' => 'admin/structure/forum',
 132      'file' => 'forum.admin.inc',
 133    );
 134    $items['admin/structure/forum/settings'] = array(
 135      'title' => 'Settings',
 136      'page callback' => 'drupal_get_form',
 137      'page arguments' => array('forum_admin_settings'),
 138      'access arguments' => array('administer forums'),
 139      'weight' => 5,
 140      'type' => MENU_LOCAL_TASK,
 141      'parent' => 'admin/structure/forum',
 142      'file' => 'forum.admin.inc',
 143    );
 144    $items['admin/structure/forum/edit/container/%taxonomy_term'] = array(
 145      'title' => 'Edit container',
 146      'page callback' => 'forum_form_main',
 147      'page arguments' => array('container', 5),
 148      'access arguments' => array('administer forums'),
 149      'file' => 'forum.admin.inc',
 150    );
 151    $items['admin/structure/forum/edit/forum/%taxonomy_term'] = array(
 152      'title' => 'Edit forum',
 153      'page callback' => 'forum_form_main',
 154      'page arguments' => array('forum', 5),
 155      'access arguments' => array('administer forums'),
 156      'file' => 'forum.admin.inc',
 157    );
 158    return $items;
 159  }
 160  
 161  /**
 162   * Implements hook_menu_local_tasks_alter().
 163   */
 164  function forum_menu_local_tasks_alter(&$data, $router_item, $root_path) {
 165    global $user;
 166  
 167    // Add action link to 'node/add/forum' on 'forum' sub-pages.
 168    if ($root_path == 'forum' || $root_path == 'forum/%') {
 169      $tid = (isset($router_item['page_arguments'][0]) ? $router_item['page_arguments'][0]->tid : 0);
 170      $forum_term = forum_forum_load($tid);
 171      if ($forum_term) {
 172        $links = array();
 173        // Loop through all bundles for forum taxonomy vocabulary field.
 174        $field = field_info_field('taxonomy_forums');
 175        foreach ($field['bundles']['node'] as $type) {
 176          if (node_access('create', $type)) {
 177            $links[$type] = array(
 178              '#theme' => 'menu_local_action',
 179              '#link' => array(
 180                'title' => t('Add new @node_type', array('@node_type' => node_type_get_name($type))),
 181                'href' => 'node/add/' . str_replace('_', '-', $type) . '/' . $forum_term->tid,
 182              ),
 183            );
 184          }
 185        }
 186        if (empty($links)) {
 187          // Authenticated user does not have access to create new topics.
 188          if ($user->uid) {
 189            $links['disallowed'] = array(
 190              '#theme' => 'menu_local_action',
 191              '#link' => array(
 192                'title' => t('You are not allowed to post new content in the forum.'),
 193              ),
 194            );
 195          }
 196          // Anonymous user does not have access to create new topics.
 197          else {
 198            $links['login'] = array(
 199              '#theme' => 'menu_local_action',
 200              '#link' => array(
 201                'title' => t('<a href="@login">Log in</a> to post new content in the forum.', array(
 202                  '@login' => url('user/login', array('query' => drupal_get_destination())),
 203                )),
 204                'localized_options' => array('html' => TRUE),
 205              ),
 206            );
 207          }
 208        }
 209        $data['actions']['output'] = array_merge($data['actions']['output'], $links);
 210      }
 211    }
 212  }
 213  
 214  /**
 215   * Implements hook_entity_info_alter().
 216   */
 217  function forum_entity_info_alter(&$info) {
 218    // Take over URI construction for taxonomy terms that are forums.
 219    if ($vid = variable_get('forum_nav_vocabulary', 0)) {
 220      // Within hook_entity_info(), we can't invoke entity_load() as that would
 221      // cause infinite recursion, so we call taxonomy_vocabulary_get_names()
 222      // instead of taxonomy_vocabulary_load(). All we need is the machine name
 223      // of $vid, so retrieving and iterating all the vocabulary names is somewhat
 224      // inefficient, but entity info is cached across page requests, and an
 225      // iteration of all vocabularies once per cache clearing isn't a big deal,
 226      // and is done as part of taxonomy_entity_info() anyway.
 227      foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocabulary) {
 228        if ($vid == $vocabulary->vid) {
 229          $info['taxonomy_term']['bundles'][$machine_name]['uri callback'] = 'forum_uri';
 230        }
 231      }
 232    }
 233  }
 234  
 235  /**
 236   * Entity URI callback used in forum_entity_info_alter().
 237   */
 238  function forum_uri($forum) {
 239    return array(
 240      'path' => 'forum/' . $forum->tid,
 241    );
 242  }
 243  
 244  /**
 245   * Checks whether a node can be used in a forum, based on its content type.
 246   *
 247   * @param $node
 248   *   A node object.
 249   *
 250   * @return
 251   *   Boolean indicating if the node can be assigned to a forum.
 252   */
 253  function _forum_node_check_node_type($node) {
 254    // Fetch information about the forum field.
 255    $field = field_info_instance('node', 'taxonomy_forums', $node->type);
 256  
 257    return is_array($field);
 258  }
 259  
 260  /**
 261   * Implements hook_node_view().
 262   */
 263  function forum_node_view($node, $view_mode) {
 264    $vid = variable_get('forum_nav_vocabulary', 0);
 265    $vocabulary = taxonomy_vocabulary_load($vid);
 266    if (_forum_node_check_node_type($node)) {
 267      if ($view_mode == 'full' && node_is_page($node)) {
 268        // Breadcrumb navigation
 269        $breadcrumb[] = l(t('Home'), NULL);
 270        $breadcrumb[] = l($vocabulary->name, 'forum');
 271        if ($parents = taxonomy_get_parents_all($node->forum_tid)) {
 272          $parents = array_reverse($parents);
 273          foreach ($parents as $parent) {
 274            $breadcrumb[] = l($parent->name, 'forum/' . $parent->tid);
 275          }
 276        }
 277        drupal_set_breadcrumb($breadcrumb);
 278  
 279      }
 280    }
 281  }
 282  
 283  /**
 284   * Implements hook_node_validate().
 285   *
 286   * Checks in particular that the node is assigned only a "leaf" term in the
 287   * forum taxonomy.
 288   */
 289  function forum_node_validate($node, $form) {
 290    if (_forum_node_check_node_type($node)) {
 291      $langcode = $form['taxonomy_forums']['#language'];
 292      // vocabulary is selected, not a "container" term.
 293      if (!empty($node->taxonomy_forums[$langcode])) {
 294        // Extract the node's proper topic ID.
 295        $containers = variable_get('forum_containers', array());
 296        foreach ($node->taxonomy_forums[$langcode] as $delta => $item) {
 297          // If no term was selected (e.g. when no terms exist yet), remove the
 298          // item.
 299          if (empty($item['tid'])) {
 300            unset($node->taxonomy_forums[$langcode][$delta]);
 301            continue;
 302          }
 303          $term = taxonomy_term_load($item['tid']);
 304          if (!$term) {
 305            form_set_error('taxonomy_forums', t('Select a forum.'));
 306            continue;
 307          }
 308          $used = db_query_range('SELECT 1 FROM {taxonomy_term_data} WHERE tid = :tid AND vid = :vid',0 , 1, array(
 309            ':tid' => $term->tid,
 310            ':vid' => $term->vid,
 311          ))->fetchField();
 312          if ($used && in_array($term->tid, $containers)) {
 313            form_set_error('taxonomy_forums', t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', array('%forum' => $term->name)));
 314          }
 315        }
 316      }
 317    }
 318  }
 319  
 320  /**
 321   * Implements hook_node_presave().
 322   *
 323   * Assigns the forum taxonomy when adding a topic from within a forum.
 324   */
 325  function forum_node_presave($node) {
 326    if (_forum_node_check_node_type($node)) {
 327      // Make sure all fields are set properly:
 328      $node->icon = !empty($node->icon) ? $node->icon : '';
 329      reset($node->taxonomy_forums);
 330      $langcode = key($node->taxonomy_forums);
 331      if (!empty($node->taxonomy_forums[$langcode])) {
 332        $node->forum_tid = $node->taxonomy_forums[$langcode][0]['tid'];
 333        if (isset($node->nid)) {
 334          $old_tid = db_query_range("SELECT f.tid FROM {forum} f INNER JOIN {node} n ON f.vid = n.vid WHERE n.nid = :nid ORDER BY f.vid DESC", 0, 1, array(':nid' => $node->nid))->fetchField();
 335          if ($old_tid && isset($node->forum_tid) && ($node->forum_tid != $old_tid) && !empty($node->shadow)) {
 336            // A shadow copy needs to be created. Retain new term and add old term.
 337            $node->taxonomy_forums[$langcode][] = array('tid' => $old_tid);
 338          }
 339        }
 340      }
 341    }
 342  }
 343  
 344  /**
 345   * Implements hook_node_update().
 346   */
 347  function forum_node_update($node) {
 348    if (_forum_node_check_node_type($node)) {
 349      if (empty($node->revision) && db_query('SELECT tid FROM {forum} WHERE nid=:nid', array(':nid' => $node->nid))->fetchField()) {
 350        if (!empty($node->forum_tid)) {
 351          db_update('forum')
 352            ->fields(array('tid' => $node->forum_tid))
 353            ->condition('vid', $node->vid)
 354            ->execute();
 355        }
 356        // The node is removed from the forum.
 357        else {
 358          db_delete('forum')
 359            ->condition('nid', $node->nid)
 360            ->execute();
 361        }
 362      }
 363      else {
 364        if (!empty($node->forum_tid)) {
 365          db_insert('forum')
 366            ->fields(array(
 367              'tid' => $node->forum_tid,
 368              'vid' => $node->vid,
 369              'nid' => $node->nid,
 370            ))
 371            ->execute();
 372        }
 373      }
 374      // If the node has a shadow forum topic, update the record for this
 375      // revision.
 376      if (!empty($node->shadow)) {
 377        db_delete('forum')
 378          ->condition('nid', $node->nid)
 379          ->condition('vid', $node->vid)
 380          ->execute();
 381        db_insert('forum')
 382          ->fields(array(
 383            'nid' => $node->nid,
 384            'vid' => $node->vid,
 385            'tid' => $node->forum_tid,
 386          ))
 387          ->execute();
 388       }
 389    }
 390  }
 391  
 392  /**
 393   * Implements hook_node_insert().
 394   */
 395  function forum_node_insert($node) {
 396    if (_forum_node_check_node_type($node)) {
 397      if (!empty($node->forum_tid)) {
 398        $nid = db_insert('forum')
 399          ->fields(array(
 400            'tid' => $node->forum_tid,
 401            'vid' => $node->vid,
 402            'nid' => $node->nid,
 403          ))
 404          ->execute();
 405      }
 406    }
 407  }
 408  
 409  /**
 410   * Implements hook_node_delete().
 411   */
 412  function forum_node_delete($node) {
 413    if (_forum_node_check_node_type($node)) {
 414      db_delete('forum')
 415        ->condition('nid', $node->nid)
 416        ->execute();
 417      db_delete('forum_index')
 418        ->condition('nid', $node->nid)
 419        ->execute();
 420    }
 421  }
 422  
 423  /**
 424   * Implements hook_node_load().
 425   */
 426  function forum_node_load($nodes) {
 427    $node_vids = array();
 428    foreach ($nodes as $node) {
 429      if (_forum_node_check_node_type($node)) {
 430        $node_vids[] = $node->vid;
 431      }
 432    }
 433    if (!empty($node_vids)) {
 434      $query = db_select('forum', 'f');
 435      $query
 436        ->fields('f', array('nid', 'tid'))
 437        ->condition('f.vid', $node_vids);
 438      $result = $query->execute();
 439      foreach ($result as $record) {
 440        $nodes[$record->nid]->forum_tid = $record->tid;
 441      }
 442    }
 443  }
 444  
 445  /**
 446   * Implements hook_node_info().
 447   */
 448  function forum_node_info() {
 449    return array(
 450      'forum' => array(
 451        'name' => t('Forum topic'),
 452        'base' => 'forum',
 453        'description' => t('A <em>forum topic</em> starts a new discussion thread within a forum.'),
 454        'title_label' => t('Subject'),
 455      )
 456    );
 457  }
 458  
 459  /**
 460   * Implements hook_permission().
 461   */
 462  function forum_permission() {
 463    $perms = array(
 464      'administer forums' => array(
 465        'title' => t('Administer forums'),
 466      ),
 467    );
 468    return $perms;
 469  }
 470  
 471  /**
 472   * Implements hook_taxonomy_term_delete().
 473   */
 474  function forum_taxonomy_term_delete($term) {
 475    // For containers, remove the tid from the forum_containers variable.
 476    $containers = variable_get('forum_containers', array());
 477    $key = array_search($term->tid, $containers);
 478    if ($key !== FALSE) {
 479      unset($containers[$key]);
 480    }
 481    variable_set('forum_containers', $containers);
 482  }
 483  
 484  /**
 485   * Implements hook_comment_publish().
 486   *
 487   * This actually handles the insertion and update of published nodes since
 488   * comment_save() calls hook_comment_publish() for all published comments.
 489   */
 490  function forum_comment_publish($comment) {
 491    _forum_update_forum_index($comment->nid);
 492  }
 493  
 494  /**
 495   * Implements hook_comment_update().
 496   *
 497   * The Comment module doesn't call hook_comment_unpublish() when saving
 498   * individual comments, so we need to check for those here.
 499   */
 500  function forum_comment_update($comment) {
 501    // comment_save() calls hook_comment_publish() for all published comments,
 502    // so we need to handle all other values here.
 503    if (!$comment->status) {
 504      _forum_update_forum_index($comment->nid);
 505    }
 506  }
 507  
 508  /**
 509   * Implements hook_comment_unpublish().
 510   */
 511  function forum_comment_unpublish($comment) {
 512    _forum_update_forum_index($comment->nid);
 513  }
 514  
 515  /**
 516   * Implements hook_comment_delete().
 517   */
 518  function forum_comment_delete($comment) {
 519    _forum_update_forum_index($comment->nid);
 520  }
 521  
 522  /**
 523   * Implements hook_field_storage_pre_insert().
 524   */
 525  function forum_field_storage_pre_insert($entity_type, $entity, &$skip_fields) {
 526    if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) {
 527      $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
 528      foreach ($entity->taxonomy_forums as $language) {
 529        foreach ($language as $item) {
 530          $query->values(array(
 531            'nid' => $entity->nid,
 532            'title' => $entity->title,
 533            'tid' => $item['tid'],
 534            'sticky' => $entity->sticky,
 535            'created' => $entity->created,
 536            'comment_count' => 0,
 537            'last_comment_timestamp' => $entity->created,
 538          ));
 539        }
 540      }
 541      $query->execute();
 542    }
 543  }
 544  
 545  /**
 546   * Implements hook_field_storage_pre_update().
 547   */
 548  function forum_field_storage_pre_update($entity_type, $entity, &$skip_fields) {
 549    $first_call = &drupal_static(__FUNCTION__, array());
 550  
 551    if ($entity_type == 'node' && _forum_node_check_node_type($entity)) {
 552  
 553      // If the node is published, update the forum index.
 554      if ($entity->status) {
 555  
 556        // We don't maintain data for old revisions, so clear all previous values
 557        // from the table. Since this hook runs once per field, per object, make
 558        // sure we only wipe values once.
 559        if (!isset($first_call[$entity->nid])) {
 560          $first_call[$entity->nid] = FALSE;
 561          db_delete('forum_index')->condition('nid', $entity->nid)->execute();
 562        }
 563        $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
 564        foreach ($entity->taxonomy_forums as $language) {
 565          foreach ($language as $item) {
 566            $query->values(array(
 567              'nid' => $entity->nid,
 568              'title' => $entity->title,
 569              'tid' => $item['tid'],
 570              'sticky' => $entity->sticky,
 571              'created' => $entity->created,
 572              'comment_count' => 0,
 573              'last_comment_timestamp' => $entity->created,
 574            ));
 575          }
 576        }
 577        $query->execute();
 578        // The logic for determining last_comment_count is fairly complex, so
 579        // call _forum_update_forum_index() too.
 580        _forum_update_forum_index($entity->nid);
 581      }
 582  
 583      // When a forum node is unpublished, remove it from the forum_index table.
 584      else {
 585        db_delete('forum_index')->condition('nid', $entity->nid)->execute();
 586      }
 587  
 588    }
 589  }
 590  
 591  /**
 592   * Implements hook_form_FORM_ID_alter() for taxonomy_form_vocabulary().
 593   */
 594  function forum_form_taxonomy_form_vocabulary_alter(&$form, &$form_state, $form_id) {
 595    $vid = variable_get('forum_nav_vocabulary', 0);
 596    if (isset($form['vid']['#value']) && $form['vid']['#value'] == $vid) {
 597      $form['help_forum_vocab'] = array(
 598        '#markup' => t('This is the designated forum vocabulary. Some of the normal vocabulary options have been removed.'),
 599        '#weight' => -1,
 600      );
 601      // Forum's vocabulary always has single hierarchy. Forums and containers
 602      // have only one parent or no parent for root items. By default this value
 603      // is 0.
 604      $form['hierarchy']['#value'] = 1;
 605      // Do not allow to delete forum's vocabulary.
 606      $form['actions']['delete']['#access'] = FALSE;
 607    }
 608  }
 609  
 610  /**
 611   * Implements hook_form_FORM_ID_alter() for taxonomy_form_term().
 612   */
 613  function forum_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
 614     $vid = variable_get('forum_nav_vocabulary', 0);
 615     if (isset($form['vid']['#value']) && $form['vid']['#value'] == $vid) {
 616      // Hide multiple parents select from forum terms.
 617      $form['relations']['parent']['#access'] = FALSE;
 618    }
 619  }
 620  
 621  /**
 622   * Implements hook_form_BASE_FORM_ID_alter() for node_form().
 623   */
 624  function forum_form_node_form_alter(&$form, &$form_state, $form_id) {
 625    if (isset($form['taxonomy_forums'])) {
 626      $langcode = $form['taxonomy_forums']['#language'];
 627      // Make the vocabulary required for 'real' forum-nodes.
 628      $form['taxonomy_forums'][$langcode]['#required'] = TRUE;
 629      $form['taxonomy_forums'][$langcode]['#multiple'] = FALSE;
 630      if (empty($form['taxonomy_forums'][$langcode]['#default_value'])) {
 631        // If there is no default forum already selected, try to get the forum
 632        // ID from the URL (e.g., if we are on a page like node/add/forum/2, we
 633        // expect "2" to be the ID of the forum that was requested).
 634        $requested_forum_id = arg(3);
 635        $form['taxonomy_forums'][$langcode]['#default_value'] = is_numeric($requested_forum_id) ? $requested_forum_id : '';
 636      }
 637    }
 638  }
 639  
 640  /**
 641   * Implements hook_block_info().
 642   */
 643  function forum_block_info() {
 644    $blocks['active'] = array(
 645      'info' => t('Active forum topics'),
 646      'cache' => DRUPAL_CACHE_CUSTOM,
 647      'properties' => array('administrative' => TRUE),
 648    );
 649    $blocks['new'] = array(
 650      'info' => t('New forum topics'),
 651      'cache' => DRUPAL_CACHE_CUSTOM,
 652      'properties' => array('administrative' => TRUE),
 653    );
 654    return $blocks;
 655  }
 656  
 657  /**
 658   * Implements hook_block_configure().
 659   */
 660  function forum_block_configure($delta = '') {
 661    $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)));
 662    return $form;
 663  }
 664  
 665  /**
 666   * Implements hook_block_save().
 667   */
 668  function forum_block_save($delta = '', $edit = array()) {
 669    variable_set('forum_block_num_' . $delta, $edit['forum_block_num_' . $delta]);
 670  }
 671  
 672  /**
 673   * Implements hook_block_view().
 674   *
 675   * Generates a block containing the currently active forum topics and the most
 676   * recently added forum topics.
 677   */
 678  function forum_block_view($delta = '') {
 679    $query = db_select('forum_index', 'f')
 680      ->fields('f')
 681      ->addTag('node_access');
 682    switch ($delta) {
 683      case 'active':
 684        $title = t('Active forum topics');
 685        $query
 686          ->orderBy('f.last_comment_timestamp', 'DESC')
 687          ->range(0, variable_get('forum_block_num_active', '5'));
 688        break;
 689  
 690      case 'new':
 691        $title = t('New forum topics');
 692        $query
 693          ->orderBy('f.created', 'DESC')
 694          ->range(0, variable_get('forum_block_num_new', '5'));
 695        break;
 696    }
 697  
 698    $block['subject'] = $title;
 699    // Cache based on the altered query. Enables us to cache with node access enabled.
 700    $block['content'] = drupal_render_cache_by_query($query, 'forum_block_view');
 701    $block['content']['#access'] = user_access('access content');
 702    return $block;
 703  }
 704  
 705  /**
 706   * Render API callback: Lists nodes based on the element's #query property.
 707   *
 708   * This function can be used as a #pre_render callback.
 709   *
 710   * @see forum_block_view()
 711   */
 712  function forum_block_view_pre_render($elements) {
 713    $result = $elements['#query']->execute();
 714    if ($node_title_list = node_title_list($result)) {
 715      $elements['forum_list'] = $node_title_list;
 716      $elements['forum_more'] = array('#theme' => 'more_link', '#url' => 'forum', '#title' => t('Read the latest forum topics.'));
 717    }
 718    return $elements;
 719  }
 720  
 721  /**
 722   * Implements hook_form().
 723   */
 724  function forum_form($node, $form_state) {
 725    $type = node_type_get_type($node);
 726    $form['title'] = array(
 727      '#type' => 'textfield',
 728      '#title' => check_plain($type->title_label),
 729      '#default_value' => !empty($node->title) ? $node->title : '',
 730      '#required' => TRUE, '#weight' => -5
 731    );
 732  
 733    if (!empty($node->nid)) {
 734      $forum_terms = $node->taxonomy_forums;
 735      // If editing, give option to leave shadows.
 736      $shadow = (count($forum_terms) > 1);
 737      $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.'));
 738      $form['forum_tid'] = array('#type' => 'value', '#value' => $node->forum_tid);
 739    }
 740  
 741    return $form;
 742  }
 743  
 744  /**
 745   * Returns a tree of all forums for a given taxonomy term ID.
 746   *
 747   * @param $tid
 748   *   (optional) Taxonomy term ID of the forum. If not given all forums will be
 749   *   returned.
 750   *
 751   * @return
 752   *   A tree of taxonomy objects, with the following additional properties:
 753   *   - num_topics: Number of topics in the forum.
 754   *   - num_posts: Total number of posts in all topics.
 755   *   - last_post: Most recent post for the forum.
 756   *   - forums: An array of child forums.
 757   */
 758  function forum_forum_load($tid = NULL) {
 759    $cache = &drupal_static(__FUNCTION__, array());
 760  
 761    // Return a cached forum tree if available.
 762    if (!isset($tid)) {
 763      $tid = 0;
 764    }
 765    if (isset($cache[$tid])) {
 766      return $cache[$tid];
 767    }
 768  
 769    $vid = variable_get('forum_nav_vocabulary', 0);
 770  
 771    // Load and validate the parent term.
 772    if ($tid) {
 773      $forum_term = taxonomy_term_load($tid);
 774      if (!$forum_term || ($forum_term->vid != $vid)) {
 775        return $cache[$tid] = FALSE;
 776      }
 777    }
 778    // If $tid is 0, create an empty object to hold the child terms.
 779    elseif ($tid === 0) {
 780      $forum_term = (object) array(
 781        'tid' => 0,
 782      );
 783    }
 784  
 785    // Determine if the requested term is a container.
 786    if (!$forum_term->tid || in_array($forum_term->tid, variable_get('forum_containers', array()))) {
 787      $forum_term->container = 1;
 788    }
 789  
 790    // Load parent terms.
 791    $forum_term->parents = taxonomy_get_parents_all($forum_term->tid);
 792  
 793    // Load the tree below.
 794    $forums = array();
 795    $_forums = taxonomy_get_tree($vid, $tid);
 796  
 797    if (count($_forums)) {
 798      $query = db_select('node', 'n');
 799      $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
 800      $query->join('forum', 'f', 'n.vid = f.vid');
 801      $query->addExpression('COUNT(n.nid)', 'topic_count');
 802      $query->addExpression('SUM(ncs.comment_count)', 'comment_count');
 803      $counts = $query
 804        ->fields('f', array('tid'))
 805        ->condition('n.status', 1)
 806        ->groupBy('tid')
 807        ->addTag('node_access')
 808        ->execute()
 809        ->fetchAllAssoc('tid');
 810    }
 811  
 812    foreach ($_forums as $forum) {
 813      // Determine if the child term is a container.
 814      if (in_array($forum->tid, variable_get('forum_containers', array()))) {
 815        $forum->container = 1;
 816      }
 817  
 818      // Merge in the topic and post counters.
 819      if (!empty($counts[$forum->tid])) {
 820        $forum->num_topics = $counts[$forum->tid]->topic_count;
 821        $forum->num_posts = $counts[$forum->tid]->topic_count + $counts[$forum->tid]->comment_count;
 822      }
 823      else {
 824        $forum->num_topics = 0;
 825        $forum->num_posts = 0;
 826      }
 827  
 828      // Query "Last Post" information for this forum.
 829      $query = db_select('node', 'n');
 830      $query->join('users', 'u1', 'n.uid = u1.uid');
 831      $query->join('forum', 'f', 'n.vid = f.vid AND f.tid = :tid', array(':tid' => $forum->tid));
 832      $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
 833      $query->join('users', 'u2', 'ncs.last_comment_uid = u2.uid');
 834      $query->addExpression('CASE ncs.last_comment_uid WHEN 0 THEN ncs.last_comment_name ELSE u2.name END', 'last_comment_name');
 835  
 836      $topic = $query
 837        ->fields('ncs', array('last_comment_timestamp', 'last_comment_uid'))
 838        ->condition('n.status', 1)
 839        ->orderBy('last_comment_timestamp', 'DESC')
 840        ->range(0, 1)
 841        ->addTag('node_access')
 842        ->execute()
 843        ->fetchObject();
 844  
 845      // Merge in the "Last Post" information.
 846      $last_post = new stdClass();
 847      if (!empty($topic->last_comment_timestamp)) {
 848        $last_post->created = $topic->last_comment_timestamp;
 849        $last_post->name = $topic->last_comment_name;
 850        $last_post->uid = $topic->last_comment_uid;
 851      }
 852      $forum->last_post = $last_post;
 853  
 854      $forums[$forum->tid] = $forum;
 855    }
 856  
 857    // Cache the result, and return the tree.
 858    $forum_term->forums = $forums;
 859    $cache[$tid] = $forum_term;
 860    return $forum_term;
 861  }
 862  
 863  /**
 864   * Calculates the number of new posts in a forum that the user has not yet read.
 865   *
 866   * Nodes are new if they are newer than NODE_NEW_LIMIT.
 867   *
 868   * @param $term
 869   *   The term ID of the forum.
 870   * @param $uid
 871   *   The user ID.
 872   *
 873   * @return
 874   *   The number of new posts in the forum that have not been read by the user.
 875   */
 876  function _forum_topics_unread($term, $uid) {
 877    $query = db_select('node', 'n');
 878    $query->join('forum', 'f', 'n.vid = f.vid AND f.tid = :tid', array(':tid' => $term));
 879    $query->leftJoin('history', 'h', 'n.nid = h.nid AND h.uid = :uid', array(':uid' => $uid));
 880    $query->addExpression('COUNT(n.nid)', 'count');
 881    return $query
 882      ->condition('status', 1)
 883      ->condition('n.created', NODE_NEW_LIMIT, '>')
 884      ->isNull('h.nid')
 885      ->addTag('node_access')
 886      ->execute()
 887      ->fetchField();
 888  }
 889  
 890  /**
 891   * Gets all the topics in a forum.
 892   *
 893   * @param $tid
 894   *   The term ID of the forum.
 895   * @param $sortby
 896   *   One of the following integers indicating the sort criteria:
 897   *   - 1: Date - newest first.
 898   *   - 2: Date - oldest first.
 899   *   - 3: Posts with the most comments first.
 900   *   - 4: Posts with the least comments first.
 901   * @param $forum_per_page
 902   *   The maximum number of topics to display per page.
 903   *
 904   * @return
 905   *   A list of all the topics in a forum.
 906   */
 907  function forum_get_topics($tid, $sortby, $forum_per_page) {
 908    global $user, $forum_topic_list_header;
 909  
 910    $forum_topic_list_header = array(
 911      NULL,
 912      array('data' => t('Topic'), 'field' => 'f.title'),
 913      array('data' => t('Replies'), 'field' => 'f.comment_count'),
 914      array('data' => t('Last reply'), 'field' => 'f.last_comment_timestamp'),
 915    );
 916  
 917    $order = _forum_get_topic_order($sortby);
 918    for ($i = 0; $i < count($forum_topic_list_header); $i++) {
 919      if ($forum_topic_list_header[$i]['field'] == $order['field']) {
 920        $forum_topic_list_header[$i]['sort'] = $order['sort'];
 921      }
 922    }
 923  
 924    $query = db_select('forum_index', 'f')->extend('PagerDefault')->extend('TableSort');
 925    $query->fields('f');
 926    $query
 927      ->condition('f.tid', $tid)
 928      ->addTag('node_access')
 929      ->orderBy('f.sticky', 'DESC')
 930      ->orderByHeader($forum_topic_list_header)
 931      ->limit($forum_per_page);
 932  
 933    $count_query = db_select('forum_index', 'f');
 934    $count_query->condition('f.tid', $tid);
 935    $count_query->addExpression('COUNT(*)');
 936    $count_query->addTag('node_access');
 937  
 938    $query->setCountQuery($count_query);
 939    $result = $query->execute();
 940    $nids = array();
 941    foreach ($result as $record) {
 942      $nids[] = $record->nid;
 943    }
 944    if ($nids) {
 945      $query = db_select('node', 'n')->extend('TableSort');
 946      $query->fields('n', array('title', 'nid', 'type', 'sticky', 'created', 'uid'));
 947      $query->addField('n', 'comment', 'comment_mode');
 948  
 949      $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
 950      $query->fields('ncs', array('cid', 'last_comment_uid', 'last_comment_timestamp', 'comment_count'));
 951  
 952      $query->join('forum_index', 'f', 'f.nid = ncs.nid');
 953      $query->addField('f', 'tid', 'forum_tid');
 954  
 955      $query->join('users', 'u', 'n.uid = u.uid');
 956      $query->addField('u', 'name');
 957  
 958      $query->join('users', 'u2', 'ncs.last_comment_uid = u2.uid');
 959  
 960      $query->addExpression('CASE ncs.last_comment_uid WHEN 0 THEN ncs.last_comment_name ELSE u2.name END', 'last_comment_name');
 961  
 962      $query
 963        ->orderBy('f.sticky', 'DESC')
 964        ->orderByHeader($forum_topic_list_header)
 965        ->condition('n.nid', $nids);
 966  
 967      $result = $query->execute();
 968    }
 969    else {
 970      $result = array();
 971    }
 972  
 973    $topics = array();
 974    $first_new_found = FALSE;
 975    foreach ($result as $topic) {
 976      if ($user->uid) {
 977        // A forum is new if the topic is new, or if there are new comments since
 978        // the user's last visit.
 979        if ($topic->forum_tid != $tid) {
 980          $topic->new = 0;
 981        }
 982        else {
 983          $history = _forum_user_last_visit($topic->nid);
 984          $topic->new_replies = comment_num_new($topic->nid, $history);
 985          $topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history);
 986        }
 987      }
 988      else {
 989        // Do not track "new replies" status for topics if the user is anonymous.
 990        $topic->new_replies = 0;
 991        $topic->new = 0;
 992      }
 993  
 994      // Make sure only one topic is indicated as the first new topic.
 995      $topic->first_new = FALSE;
 996      if ($topic->new != 0 && !$first_new_found) {
 997        $topic->first_new = TRUE;
 998        $first_new_found = TRUE;
 999      }
1000  
1001      if ($topic->comment_count > 0) {
1002        $last_reply = new stdClass();
1003        $last_reply->created = $topic->last_comment_timestamp;
1004        $last_reply->name = $topic->last_comment_name;
1005        $last_reply->uid = $topic->last_comment_uid;
1006        $topic->last_reply = $last_reply;
1007      }
1008      $topics[] = $topic;
1009    }
1010  
1011    return $topics;
1012  }
1013  
1014  /**
1015   * Preprocesses variables for forums.tpl.php.
1016   *
1017   * @param $variables
1018   *   An array containing the following elements:
1019   *   - forums: An array of all forum objects to display for the given taxonomy
1020   *     term ID. If tid = 0 then all the top-level forums are displayed.
1021   *   - topics: An array of all the topics in the current forum.
1022   *   - parents: An array of taxonomy term objects that are ancestors of the
1023   *     current term ID.
1024   *   - tid: Taxonomy term ID of the current forum.
1025   *   - sortby: One of the following integers indicating the sort criteria:
1026   *     - 1: Date - newest first.
1027   *     - 2: Date - oldest first.
1028   *     - 3: Posts with the most comments first.
1029   *     - 4: Posts with the least comments first.
1030   *   - forum_per_page: The maximum number of topics to display per page.
1031   *
1032   * @see forums.tpl.php
1033   */
1034  function template_preprocess_forums(&$variables) {
1035    global $user;
1036  
1037    $vid = variable_get('forum_nav_vocabulary', 0);
1038    $vocabulary = taxonomy_vocabulary_load($vid);
1039    $title = !empty($vocabulary->name) ? $vocabulary->name : '';
1040  
1041    // Breadcrumb navigation:
1042    $breadcrumb[] = l(t('Home'), NULL);
1043    if ($variables['tid']) {
1044      $breadcrumb[] = l($vocabulary->name, 'forum');
1045    }
1046    if ($variables['parents']) {
1047      $variables['parents'] = array_reverse($variables['parents']);
1048      foreach ($variables['parents'] as $p) {
1049        if ($p->tid == $variables['tid']) {
1050          $title = $p->name;
1051        }
1052        else {
1053          $breadcrumb[] = l($p->name, 'forum/' . $p->tid);
1054        }
1055      }
1056    }
1057    drupal_set_breadcrumb($breadcrumb);
1058    drupal_set_title($title);
1059  
1060    if ($variables['forums_defined'] = count($variables['forums']) || count($variables['parents'])) {
1061      if (!empty($variables['forums'])) {
1062        $variables['forums'] = theme('forum_list', $variables);
1063      }
1064      else {
1065        $variables['forums'] = '';
1066      }
1067  
1068      if ($variables['tid'] && !in_array($variables['tid'], variable_get('forum_containers', array()))) {
1069        $variables['topics'] = theme('forum_topic_list', $variables);
1070        drupal_add_feed('taxonomy/term/' . $variables['tid'] . '/feed', 'RSS - ' . $title);
1071      }
1072      else {
1073        $variables['topics'] = '';
1074      }
1075  
1076      // Provide separate template suggestions based on what's being output. Topic id is also accounted for.
1077      // Check both variables to be safe then the inverse. Forums with topic ID's take precedence.
1078      if ($variables['forums'] && !$variables['topics']) {
1079        $variables['theme_hook_suggestions'][] = 'forums__containers';
1080        $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
1081        $variables['theme_hook_suggestions'][] = 'forums__containers__' . $variables['tid'];
1082      }
1083      elseif (!$variables['forums'] && $variables['topics']) {
1084        $variables['theme_hook_suggestions'][] = 'forums__topics';
1085        $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
1086        $variables['theme_hook_suggestions'][] = 'forums__topics__' . $variables['tid'];
1087      }
1088      else {
1089        $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
1090      }
1091  
1092    }
1093    else {
1094      drupal_set_title(t('No forums defined'));
1095      $variables['forums'] = '';
1096      $variables['topics'] = '';
1097    }
1098  }
1099  
1100  /**
1101   * Preprocesses variables for forum-list.tpl.php.
1102   *
1103   * @param $variables
1104   *   An array containing the following elements:
1105   *   - forums: An array of all forum objects to display for the given taxonomy
1106   *     term ID. If tid = 0 then all the top-level forums are displayed.
1107   *   - parents: An array of taxonomy term objects that are ancestors of the
1108   *     current term ID.
1109   *   - tid: Taxonomy term ID of the current forum.
1110   *
1111   * @see forum-list.tpl.php
1112   * @see theme_forum_list()
1113   */
1114  function template_preprocess_forum_list(&$variables) {
1115    global $user;
1116    $row = 0;
1117    // Sanitize each forum so that the template can safely print the data.
1118    foreach ($variables['forums'] as $id => $forum) {
1119      $variables['forums'][$id]->description = !empty($forum->description) ? filter_xss_admin($forum->description) : '';
1120      $variables['forums'][$id]->link = url("forum/$forum->tid");
1121      $variables['forums'][$id]->name = check_plain($forum->name);
1122      $variables['forums'][$id]->is_container = !empty($forum->container);
1123      $variables['forums'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
1124      $row++;
1125  
1126      $variables['forums'][$id]->new_text = '';
1127      $variables['forums'][$id]->new_url = '';
1128      $variables['forums'][$id]->new_topics = 0;
1129      $variables['forums'][$id]->old_topics = $forum->num_topics;
1130      $variables['forums'][$id]->icon_class = 'default';
1131      $variables['forums'][$id]->icon_title = t('No new posts');
1132      if ($user->uid) {
1133        $variables['forums'][$id]->new_topics = _forum_topics_unread($forum->tid, $user->uid);
1134        if ($variables['forums'][$id]->new_topics) {
1135          $variables['forums'][$id]->new_text = format_plural($variables['forums'][$id]->new_topics, '1 new', '@count new');
1136          $variables['forums'][$id]->new_url = url("forum/$forum->tid", array('fragment' => 'new'));
1137          $variables['forums'][$id]->icon_class = 'new';
1138          $variables['forums'][$id]->icon_title = t('New posts');
1139        }
1140        $variables['forums'][$id]->old_topics = $forum->num_topics - $variables['forums'][$id]->new_topics;
1141      }
1142      $variables['forums'][$id]->last_reply = theme('forum_submitted', array('topic' => $forum->last_post));
1143    }
1144    // Give meaning to $tid for themers. $tid actually stands for term id.
1145    $variables['forum_id'] = $variables['tid'];
1146    unset($variables['tid']);
1147  }
1148  
1149  /**
1150   * Preprocesses variables for forum-topic-list.tpl.php.
1151   *
1152   * @param $variables
1153   *   An array containing the following elements:
1154   *   - tid: Taxonomy term ID of the current forum.
1155   *   - topics: An array of all the topics in the current forum.
1156   *   - forum_per_page: The maximum number of topics to display per page.
1157   *
1158   * @see forum-topic-list.tpl.php
1159   * @see theme_forum_topic_list()
1160   */
1161  function template_preprocess_forum_topic_list(&$variables) {
1162    global $forum_topic_list_header;
1163  
1164    // Create the tablesorting header.
1165    $ts = tablesort_init($forum_topic_list_header);
1166    $header = '';
1167    foreach ($forum_topic_list_header as $cell) {
1168      $cell = tablesort_header($cell, $forum_topic_list_header, $ts);
1169      $header .= _theme_table_cell($cell, TRUE);
1170    }
1171    $variables['header'] = $header;
1172  
1173    if (!empty($variables['topics'])) {
1174      $row = 0;
1175      foreach ($variables['topics'] as $id => $topic) {
1176        $variables['topics'][$id]->icon = theme('forum_icon', array('new_posts' => $topic->new, 'num_posts' => $topic->comment_count, 'comment_mode' => $topic->comment_mode, 'sticky' => $topic->sticky, 'first_new' => $topic->first_new));
1177        $variables['topics'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
1178        $row++;
1179  
1180        // We keep the actual tid in forum table, if it's different from the
1181        // current tid then it means the topic appears in two forums, one of
1182        // them is a shadow copy.
1183        if ($variables['tid'] != $topic->forum_tid) {
1184          $variables['topics'][$id]->moved = TRUE;
1185          $variables['topics'][$id]->title = check_plain($topic->title);
1186          $variables['topics'][$id]->message = l(t('This topic has been moved'), "forum/$topic->forum_tid");
1187        }
1188        else {
1189          $variables['topics'][$id]->moved = FALSE;
1190          $variables['topics'][$id]->title = l($topic->title, "node/$topic->nid");
1191          $variables['topics'][$id]->message = '';
1192        }
1193        $variables['topics'][$id]->created = theme('forum_submitted', array('topic' => $topic));
1194        $variables['topics'][$id]->last_reply = theme('forum_submitted', array('topic' => isset($topic->last_reply) ? $topic->last_reply : NULL));
1195  
1196        $variables['topics'][$id]->new_text = '';
1197        $variables['topics'][$id]->new_url = '';
1198        if ($topic->new_replies) {
1199          $variables['topics'][$id]->new_text = format_plural($topic->new_replies, '1 new', '@count new');
1200          $variables['topics'][$id]->new_url = url("node/$topic->nid", array('query' => comment_new_page_count($topic->comment_count, $topic->new_replies, $topic), 'fragment' => 'new'));
1201        }
1202  
1203      }
1204    }
1205    else {
1206      // Make this safe for the template.
1207      $variables['topics'] = array();
1208    }
1209    // Give meaning to $tid for themers. $tid actually stands for term id.
1210    $variables['topic_id'] = $variables['tid'];
1211    unset($variables['tid']);
1212  
1213    $variables['pager'] = theme('pager');
1214  }
1215  
1216  /**
1217   * Preprocesses variables for forum-icon.tpl.php.
1218   *
1219   * @param $variables
1220   *   An array containing the following elements:
1221   *   - new_posts: Indicates whether or not the topic contains new posts.
1222   *   - num_posts: The total number of posts in all topics.
1223   *   - comment_mode: An integer indicating whether comments are open, closed,
1224   *     or hidden.
1225   *   - sticky: Indicates whether the topic is sticky.
1226   *   - first_new: Indicates whether this is the first topic with new posts.
1227   *
1228   * @see forum-icon.tpl.php
1229   * @see theme_forum_icon()
1230   */
1231  function template_preprocess_forum_icon(&$variables) {
1232    $variables['hot_threshold'] = variable_get('forum_hot_topic', 15);
1233    if ($variables['num_posts'] > $variables['hot_threshold']) {
1234      $variables['icon_class'] = $variables['new_posts'] ? 'hot-new' : 'hot';
1235      $variables['icon_title'] = $variables['new_posts'] ? t('Hot topic, new comments') : t('Hot topic');
1236    }
1237    else {
1238      $variables['icon_class'] = $variables['new_posts'] ? 'new' : 'default';
1239      $variables['icon_title'] = $variables['new_posts'] ? t('New comments') : t('Normal topic');
1240    }
1241  
1242    if ($variables['comment_mode'] == COMMENT_NODE_CLOSED || $variables['comment_mode'] == COMMENT_NODE_HIDDEN) {
1243      $variables['icon_class'] = 'closed';
1244      $variables['icon_title'] = t('Closed topic');
1245    }
1246  
1247    if ($variables['sticky'] == 1) {
1248      $variables['icon_class'] = 'sticky';
1249      $variables['icon_title'] = t('Sticky topic');
1250    }
1251  }
1252  
1253  /**
1254   * Preprocesses variables for forum-submitted.tpl.php.
1255   *
1256   * The submission information will be displayed in the forum list and topic
1257   * list.
1258   *
1259   * @param $variables
1260   *   An array containing the following elements:
1261   *   - topic: The topic object.
1262   *
1263   * @see forum-submitted.tpl.php
1264   * @see theme_forum_submitted()
1265   */
1266  function template_preprocess_forum_submitted(&$variables) {
1267    $variables['author'] = isset($variables['topic']->uid) ? theme('username', array('account' => $variables['topic'])) : '';
1268    $variables['time'] = isset($variables['topic']->created) ? format_interval(REQUEST_TIME - $variables['topic']->created) : '';
1269  }
1270  
1271  /**
1272   * Gets the last time the user viewed a node.
1273   *
1274   * @param $nid
1275   *   The node ID.
1276   *
1277   * @return
1278   *   The timestamp when the user last viewed this node, if the user has
1279   *   previously viewed the node; otherwise NODE_NEW_LIMIT.
1280   */
1281  function _forum_user_last_visit($nid) {
1282    global $user;
1283    $history = &drupal_static(__FUNCTION__, array());
1284  
1285    if (empty($history)) {
1286      $result = db_query('SELECT nid, timestamp FROM {history} WHERE uid = :uid', array(':uid' => $user->uid));
1287      foreach ($result as $t) {
1288        $history[$t->nid] = $t->timestamp > NODE_NEW_LIMIT ? $t->timestamp : NODE_NEW_LIMIT;
1289      }
1290    }
1291    return isset($history[$nid]) ? $history[$nid] : NODE_NEW_LIMIT;
1292  }
1293  
1294  /**
1295   * Gets topic sorting information based on an integer code.
1296   *
1297   * @param $sortby
1298   *   One of the following integers indicating the sort criteria:
1299   *   - 1: Date - newest first.
1300   *   - 2: Date - oldest first.
1301   *   - 3: Posts with the most comments first.
1302   *   - 4: Posts with the least comments first.
1303   *
1304   * @return
1305   *   An array with the following values:
1306   *   - field: A field for an SQL query.
1307   *   - sort: 'asc' or 'desc'.
1308   */
1309  function _forum_get_topic_order($sortby) {
1310    switch ($sortby) {
1311      case 1:
1312        return array('field' => 'f.last_comment_timestamp', 'sort' => 'desc');
1313        break;
1314      case 2:
1315        return array('field' => 'f.last_comment_timestamp', 'sort' => 'asc');
1316        break;
1317      case 3:
1318        return array('field' => 'f.comment_count', 'sort' => 'desc');
1319        break;
1320      case 4:
1321        return array('field' => 'f.comment_count', 'sort' => 'asc');
1322        break;
1323    }
1324  }
1325  
1326  /**
1327   * Updates the taxonomy index for a given node.
1328   *
1329   * @param $nid
1330   *   The ID of the node to update.
1331   */
1332  function _forum_update_forum_index($nid) {
1333    $count = db_query('SELECT COUNT(cid) FROM {comment} c INNER JOIN {forum_index} i ON c.nid = i.nid WHERE c.nid = :nid AND c.status = :status', array(
1334      ':nid' => $nid,
1335      ':status' => COMMENT_PUBLISHED,
1336    ))->fetchField();
1337  
1338    if ($count > 0) {
1339      // Comments exist.
1340      $last_reply = db_query_range('SELECT cid, name, created, uid FROM {comment} WHERE nid = :nid AND status = :status ORDER BY cid DESC', 0, 1, array(
1341        ':nid' => $nid,
1342        ':status' => COMMENT_PUBLISHED,
1343      ))->fetchObject();
1344      db_update('forum_index')
1345        ->fields( array(
1346          'comment_count' => $count,
1347          'last_comment_timestamp' => $last_reply->created,
1348        ))
1349        ->condition('nid', $nid)
1350        ->execute();
1351    }
1352    else {
1353      // Comments do not exist.
1354      $node = db_query('SELECT uid, created FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
1355      db_update('forum_index')
1356        ->fields( array(
1357          'comment_count' => 0,
1358          'last_comment_timestamp' => $node->created,
1359        ))
1360        ->condition('nid', $nid)
1361        ->execute();
1362    }
1363  }
1364  
1365  /**
1366   * Implements hook_rdf_mapping().
1367   */
1368  function forum_rdf_mapping() {
1369    return array(
1370      array(
1371        'type' => 'node',
1372        'bundle' => 'forum',
1373        'mapping' => array(
1374          'rdftype' => array('sioc:Post', 'sioct:BoardPost'),
1375          'taxonomy_forums' => array(
1376            'predicates' => array('sioc:has_container'),
1377            'type' => 'rel',
1378          ),
1379        ),
1380      ),
1381      array(
1382        'type' => 'taxonomy_term',
1383        'bundle' => 'forums',
1384        'mapping' => array(
1385          'rdftype' => array('sioc:Container', 'sioc:Forum'),
1386        ),
1387      ),
1388    );
1389  }

title

Description

title

Description

title

Description

title

title

Body