<?php
// $Id$

/**
 * @file
 * Install, update and uninstall functions for the forum module.
 */

/**
 * Implements hook_install().
 */
function forum_install() {
  // Set the weight of the forum.module to 1 so it is loaded after the taxonomy.module.
  db_update('system')
    ->fields(array('weight' => 1))
    ->condition('name', 'forum')
    ->execute();
  // Forum topics are published by default, but do not have any other default
  // options set (for example, they are not promoted to the front page).
  variable_set('node_options_forum', array('status'));
}

/**
 * Implements hook_enable().
 */
function forum_enable() {
  // If we enable forum at the same time as taxonomy we need to call
  // field_associate_fields() as otherwise the field won't be enabled until
  // hook modules_enabled is called which takes place after hook_enable events.
  field_associate_fields('taxonomy');
  // Create the forum vocabulary if it does not exist.
  $vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', 0));
  if (!$vocabulary) {
    $edit = array(
      'name' => t('Forums'),
      'machine_name' => 'forums',
      'description' => t('Forum navigation vocabulary'),
      'hierarchy' => 1,
      'module' => 'forum',
      'weight' => -10,
    );
    $vocabulary = (object) $edit;
    taxonomy_vocabulary_save($vocabulary);
    variable_set('forum_nav_vocabulary', $vocabulary->vid);
  }

  // Create the 'taxonomy_forums' field if it doesn't already exist.
  if (!field_info_field('taxonomy_forums')) {
    $field = array(
      'field_name' => 'taxonomy_' . $vocabulary->machine_name,
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => $vocabulary->machine_name,
            'parent' => 0,
          ),
        ),
      ),
    );
    field_create_field($field);

    variable_set('forum_nav_vocabulary', $vocabulary->vid);

    // Create a default forum so forum posts can be created.
    $edit = array(
      'name' => t('General discussion'),
      'description' => '',
      'parent' => array(0),
      'vid' => $vocabulary->vid,
    );
    $term = (object) $edit;
    taxonomy_term_save($term);

    // Create the instance on the bundle.
    $instance = array(
      'field_name' => 'taxonomy_' . $vocabulary->machine_name,
      'entity_type' => 'node',
      'label' => $vocabulary->name,
      'bundle' => 'forum',
      'required' => TRUE,
      'widget' => array(
        'type' => 'options_select',
      ),
      'display' => array(
        'default' => array(
          'type' => 'taxonomy_term_reference_link',
         'weight' => 10,
        ),
        'teaser' => array(
          'type' => 'taxonomy_term_reference_link',
         'weight' => 10,
        ),
      ),
    );
    field_create_instance($instance);
  }

  // Ensure the forum node type is available.
  node_types_rebuild();
  $types = node_type_get_types();
  node_add_body_field($types['forum']);
}

/**
 * Implements hook_uninstall().
 */
function forum_uninstall() {
  // Load the dependent Taxonomy module, in case it has been disabled.
  drupal_load('module', 'taxonomy');

  variable_del('forum_containers');
  variable_del('forum_hot_topic');
  variable_del('forum_per_page');
  variable_del('forum_order');
  variable_del('forum_block_num_active');
  variable_del('forum_block_num_new');
  variable_del('node_options_forum');
}

/**
 * Implements hook_schema().
 */
function forum_schema() {
  $schema['forum'] = array(
    'description' => 'Stores the relationship of nodes to forum terms.',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {node}.nid of the node.',
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: The {node}.vid of the node.',
      ),
      'tid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {taxonomy_term_data}.tid of the forum term assigned to the node.',
      ),
    ),
    'indexes' => array(
      'forum_topic' => array('nid', 'tid'),
      'tid' => array('tid'),
    ),
    'primary key' => array('vid'),
    'foreign keys' => array(
      'forum_node' => array(
        'table' => 'node',
        'columns' => array(
          'nid' => 'nid',
          'vid' => 'vid',
        ),
      ),
    ),
  );

  $schema['forum_index'] = array(
    'description' => 'Maintains denormalized information about node/term relationships.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node}.nid this record tracks.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'title' => array(
        'description' => 'The title of this node, always treated as non-markup plain text.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'tid' => array(
         'description' => 'The term ID.',
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
         'default' => 0,
      ),
      'sticky' => array(
        'description' => 'Boolean indicating whether the node is sticky.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the node was created.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default'=> 0,
      ),
      'last_comment_timestamp' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
      ),
      'comment_count' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The total number of comments on this node.',
      ),
    ),
    'indexes' => array(
      'forum_topics' => array('tid', 'sticky', 'last_comment_timestamp'),
    ),
    'foreign keys' => array(
      'tracked_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'term' => array(
        'table' => 'taxonomy_term_data',
        'columns' => array(
          'tid' => 'tid',
        ),
      ),
    ),
  );


  return $schema;
}

/**
 * Add new index to forum table.
 */
function forum_update_7000() {
  db_drop_index('forum', 'nid');
  db_add_index('forum', 'forum_topic', array('nid', 'tid'));
}

/**
 * Create new {forum_index} table.
 */
function forum_update_7001() {
  $forum_index = array(
    'description' => 'Maintains denormalized information about node/term relationships.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node}.nid this record tracks.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'title' => array(
        'description' => 'The title of this node, always treated as non-markup plain text.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'tid' => array(
         'description' => 'The term ID.',
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
         'default' => 0,
      ),
      'sticky' => array(
        'description' => 'Boolean indicating whether the node is sticky.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the node was created.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default'=> 0,
      ),
      'last_comment_timestamp' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
      ),
      'comment_count' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The total number of comments on this node.',
      ),
    ),
    'indexes' => array(
      'forum_topics' => array('tid', 'sticky', 'last_comment_timestamp'),
    ),
    'foreign keys' => array(
      'tracked_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'term' => array(
        'table' => 'taxonomy_term_data',
        'columns' => array(
          'tid' => 'tid',
        ),
      ),
    ),
  );
  db_create_table('forum_index', $forum_index);

  $select = db_select('node', 'n');
  $forum_alias = $select->join('forum', 'f', 'n.vid = f.vid');
  $ncs_alias = $select->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
  $select
    ->fields('n', array('nid', 'title', 'sticky', 'created'))
    ->fields($forum_alias, array('tid'))
    ->fields($ncs_alias, array('last_comment_timestamp', 'comment_count'));

  db_insert('forum_index')
    ->fields(array('nid', 'title', 'sticky', 'created', 'tid', 'last_comment_timestamp', 'comment_count'))
    ->from($select)
    ->execute();
}