forum.install 6.71 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Install, update and uninstall functions for the forum module.
 */

8
/**
9
 * Implements hook_install().
10
 */
11
function forum_install() {
12
  // Set the weight of the forum.module to 1 so it is loaded after the taxonomy.module.
13 14 15 16
  db_update('system')
    ->fields(array('weight' => 1))
    ->condition('name', 'forum')
    ->execute();
17 18 19
  // 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'));
20 21
}

22 23 24
/**
 * Implements hook_enable().
 */
25
function forum_enable() {
26 27 28 29
  // 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');
30 31 32
  // Create the forum vocabulary if it does not exist.
  $vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', 0));
  if (!$vocabulary) {
33
    $edit = array(
34
      'name' => t('Forums'),
35 36
      'machine_name' => 'forums',
      'description' => t('Forum navigation vocabulary'),
37 38 39 40
      'hierarchy' => 1,
      'module' => 'forum',
      'weight' => -10,
    );
41 42
    $vocabulary = (object) $edit;
    taxonomy_vocabulary_save($vocabulary);
43 44 45 46 47 48
    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(
49
      'field_name' => 'taxonomy_forums',
50 51 52 53
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
54
            'vocabulary' => $vocabulary->machine_name,
55 56 57 58 59 60
            'parent' => 0,
          ),
        ),
      ),
    );
    field_create_field($field);
61

62 63 64 65 66 67 68 69 70
    // 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);
71

72 73
    // Create the instance on the bundle.
    $instance = array(
74
      'field_name' => 'taxonomy_forums',
75 76 77 78 79 80
      'entity_type' => 'node',
      'label' => $vocabulary->name,
      'bundle' => 'forum',
      'required' => TRUE,
      'widget' => array(
        'type' => 'options_select',
81
      ),
82 83 84 85 86 87 88 89 90
      'display' => array(
        'default' => array(
          'type' => 'taxonomy_term_reference_link',
         'weight' => 10,
        ),
        'teaser' => array(
          'type' => 'taxonomy_term_reference_link',
         'weight' => 10,
        ),
91
      ),
92 93 94
    );
    field_create_instance($instance);
  }
95

96 97 98 99
  // Ensure the forum node type is available.
  node_types_rebuild();
  $types = node_type_get_types();
  node_add_body_field($types['forum']);
100
}
101 102

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

109 110 111 112
  variable_del('forum_containers');
  variable_del('forum_hot_topic');
  variable_del('forum_per_page');
  variable_del('forum_order');
113 114
  variable_del('forum_block_num_active');
  variable_del('forum_block_num_new');
115
  variable_del('node_options_forum');
116 117 118 119 120

  field_delete_field('taxonomy_forums');
  // Purge field data now to allow taxonomy module to be uninstalled
  // if this is the only field remaining.
  field_purge_batch(10);
121
}
122 123

/**
124
 * Implements hook_schema().
125 126 127
 */
function forum_schema() {
  $schema['forum'] = array(
128
    'description' => 'Stores the relationship of nodes to forum terms.',
129
    'fields' => array(
130 131 132 133 134
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
135
        'description' => 'The {node}.nid of the node.',
136 137 138 139 140 141
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
142
        'description' => 'Primary Key: The {node}.vid of the node.',
143 144 145 146 147 148
      ),
      'tid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
149
        'description' => 'The {taxonomy_term_data}.tid of the forum term assigned to the node.',
150
      ),
151 152
    ),
    'indexes' => array(
153
      'forum_topic' => array('nid', 'tid'),
154
      'tid' => array('tid'),
155 156
    ),
    'primary key' => array('vid'),
157
    'foreign keys' => array(
158 159 160 161 162 163 164
      'forum_node' => array(
        'table' => 'node',
        'columns' => array(
          'nid' => 'nid',
          'vid' => 'vid',
        ),
      ),
165
    ),
166 167
  );

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
  $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(
221
      'forum_topics' => array('nid', 'tid', 'sticky', 'last_comment_timestamp'),
222 223
    ),
    'foreign keys' => array(
224 225 226 227 228 229 230 231 232 233
      'tracked_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'term' => array(
        'table' => 'taxonomy_term_data',
        'columns' => array(
          'tid' => 'tid',
        ),
      ),
234 235 236 237
    ),
  );


238 239
  return $schema;
}
240 241 242 243 244

/**
 * Implements hook_update_last_removed().
 */
function forum_update_last_removed() {
245
  return 7003;
246
}