<?php

/**
 * @file
 * Install file for nodecomment
 */

function nodecomment_install() {
  drupal_install_schema('nodecomment');
  _nodecomment_install_type_create();
}

function _nodecomment_install_type_create() {
  // During install profiles, node and user modules aren't yet loaded.
  // Ensure they're loaded before calling node_get_types().
  drupal_load('module', 'node');
  drupal_load('module', 'user');
  $types = node_get_types();
  $types = array_change_key_case($types, CASE_LOWER);

  if (!in_array('comment', array_keys($types))) {
    // Create the comment content type.
    $nodecomment_node_type = array(
      'type' => 'comment',
      'name' => t('Comment'),
      'module' => 'node',
      'description' => t('A comment for use with the nodecomment module.'),
      'title_label' => t('Subject'),
      'body_label' => t('Body'),
      'custom' => TRUE,
      'modified' => TRUE,
      'locked' => FALSE,
    );
    $nodecomment_node_type = (object)_node_type_set_defaults($nodecomment_node_type);
    node_type_save($nodecomment_node_type);

    // Default to not promoted.
    variable_set('node_options_comment', array('status'));

    // Default to not allowing comments.
    variable_set('comment_comment', 0);

    cache_clear_all();
    module_load_include('inc', 'system', 'system.admin');
    system_modules();
    menu_rebuild();
    node_types_rebuild();
  }
}

function nodecomment_schema() {
  $schema['node_comments'] = array(
    'fields' => array(
      'cid'      => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'disp-width' => '11'),
      'pid'      => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'disp-width' => '11'),
      'nid'      => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'disp-width' => '11'),
      'hostname' => array('type' => 'varchar', 'length' => '128', 'not null' => TRUE, 'default' => ''),
      'thread'   => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE),
      'name'     => array('type' => 'varchar', 'length' => '60', 'not null' => FALSE),
      'uid'      => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'disp-width' => '11'),
      'mail'     => array('type' => 'varchar', 'length' => '64', 'not null' => FALSE),
      'homepage' => array('type' => 'varchar', 'length' => '255', 'not null' => FALSE)
    ),
    'primary key' => array('cid'),
    'indexes' => array(
      'lid' => array('nid'),
      // For quickly finding user comments.
      'nid_uid' => array('nid', 'uid')
    ),
  );
  return $schema;
}

function nodecomment_uninstall() {
  drupal_uninstall_schema('nodecomment');
  foreach (_nodecomment_vars() as $var) {
    variable_del($var);
  }
}

function nodecomment_update_6000() {
  // adding uid to schema. this is needed to avoid the (not verified) that theme_user now implements
  $ret = array();
  db_add_field($ret, 'node_comments', 'uid', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'disp-width' => '11'));
  return $ret;
}

function nodecomment_update_6001() {
  // the node_comments view changed to nodecomments.  update any node types that
  // use this view for comments.
  foreach(node_get_types() as $type) {
    if(variable_get('comment_view_'. $type->type, '') == 'node_comments') {
      variable_set('comment_view_'. $type->type, 'nodecomments');
    }
  }
  return array();
}

/**
 * Move comment settings into per content-type settings.
 */
function nodecomment_update_6002() {
  $ret = array();

  $comment_anonymous = variable_get('comment_anonymous', NULL);
  $comment_preview = variable_get('comment_preview', NULL);
  $comment_form_location = variable_get('comment_form_location', NULL);

  foreach(node_get_types() as $type) {
    if (!is_null($comment_anonymous)) {
      variable_set('comment_anonymous_'. $type->type, $comment_anonymous);
    }
    if (!is_null($comment_preview)) {
      variable_set('comment_preview_'. $type->type, $comment_preview);
    }
    if (!is_null($comment_form_location)) {
      variable_set('comment_form_location_'. $type->type, $comment_preview);
    }
    $ret[] = array('success' => TRUE, 'query' => t('Updated comment settings for %node_type.', array('%node_type' => $type->name)));
  }

  variable_del('comment_anonymous');
  variable_del('comment_preview');
  variable_del('comment_form_location');

  return $ret;
}

/**
 * Update 6001 added a uid column but didn't populate it.
 */
function nodecomment_update_6003() {
  $ret = array();
  $ret[] = update_sql('UPDATE {node_comments} c INNER JOIN {node} n ON c.cid = n.nid SET c.uid = n.uid');
  return $ret;
}

// update 6200 needs to update settings from the old nodecomment style to the
// new style so that comment.module is not confused.

/**
 * Update our variables to proper namespace.
 */
function nodecomment_update_6201() {
  $ret = array();
  variable_del('default_comment_type');
  $types = node_get_types();
  foreach ($types as $type => $info) {
    foreach (array('comment_type', 'comment_view', 'comment_plural') as $var) {
      $value = variable_get($var . '_' . $type, NULL);
      if (isset($value)) {
        variable_set('node_' . $var . '_' . $type, $value);
      }
      variable_del($var . '_' . $type);
    }
  }

  drupal_install_modules(array('comment'));

  return $ret;
}

/**
 * Get rid of redundant variables that only create confusion.
 */
function nodecomment_update_6301() {
  $ret = array();
  $types = node_get_types();
  foreach ($types as $type => $info) {
    $oldvar = 'node_comment_' . $type;
    $newvar = 'comment_' . $type;
    $value = variable_get($oldvar, NULL);
    if (isset($value)) {
      variable_set($newvar, $value);
    }
    variable_del($oldvar);
  }
  return $ret;
}

/**
 * Add an index to find user comments for a node.
 */
function nodecomment_update_6302() {
  $ret = array();
  db_add_index($ret, 'node_comments', 'nid_uid', array('nid', 'uid'));
  return $ret;
}

/**
 * Delete unused variables.
 */
function nodecomment_update_6303() {
  $ret = array();
  foreach (node_get_types() as $type => $info) {
    variable_del('node_comment_topic_review_' . $type);
  }
  return $ret;
}

/**
 * Disable commenting for node comments.
 */
function nodecomment_update_6304() {
  $ret = array();
  
  // Disable comment status for existing nodecomments. This is needed because
  // we now allow comments for nodecomments in a separate thread, and it was
  // impossible in 2.x. We need to be sure it's not a surprise but a conscious
  // decision to not confuse users.
  $comment_types = nodecomment_get_comment_types();
  db_query(
    "UPDATE {node} SET comment = 0
     WHERE type IN (" . db_placeholders($comment_types, 'varchar') . ")",
    $comment_types
  );
  // Also set default commenting to disabled.
  foreach ($comment_types as $type) {
    variable_set("comment_$type", 0);
  }

  return $ret;
}
