<?php
// $Id: signature_forum.install,v 1.1.2.5.4.4 2009/07/14 16:54:08 liammcdermott Exp $

/**
 * @file
 * Installs, updates and uninstalls signature_forum module.
 */

/**
 * Implementation of hook_schema().
 */
function signature_forum_schema() {
  $schema['users_signature'] = array(
    'fields' => array(
      'uid'       => array(
        'description' => 'The {users}.uid this signature belongs to.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'signature' => array(
        'description' => 'The signature text.',
        'type' => 'text',
        'not null' => FALSE,
        'default' => NULL),
      'status' => array(
        'type' => 'int',
        'description' => 'A boolean indicator for whether or not a signature should be displayed by default: 1 means display, 0 means not displayed.',
        'size' => 'tiny',
        'not null' => FALSE,
        'default' => 0),
    ),
    'primary key' => array('uid'),
  );
  $schema['signature_post'] = array(
    'fields' => array(
      'delta' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'description' => 'The id of the attached object. nid for nodes and cid for comments.',
        'not null' => TRUE,
        'disp-width' => '11'),
      'type' => array(
        'description' => 'The type of comment to attach settings.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => ''),
      'status' => array(
        'type' => 'int',
        'description' => 'A boolean indicator for whether or not a signature should be displayed: 1 means display, 0 means not displayed.',
        'size' => 'tiny',
        'not null' => TRUE,
        'disp-width' => '11'),
    ),
    'primary key' => array('delta', 'type'),
  );

  return $schema;
}

/**
 * Implementation of hook_install().
 */
function signature_forum_install() {
  drupal_install_schema('signature_forum');
  db_query("INSERT INTO {users_signature} (uid, signature)
    SELECT uid, signature
    FROM {users}
    WHERE signature<>''");
  db_query("UPDATE {users} SET signature=''");
  signature_forum_build_signature_status();
}

/**
 * Set all existing comments to not have a signature to avoid duplicate signatures.
 */
function signature_forum_build_signature_status() {
  // Deal with nodecomments and standard nodes.
  db_query("INSERT INTO {signature_post} (delta, type, status)
    SELECT nid, 'node', 1
    FROM {node}");

  // Deal with standard Drupal comments.
  if (module_exists('comment')) {
    db_query("INSERT INTO {signature_post} (delta, type, status)
      SELECT cid, 'comment', 1
      FROM {comments}");
  }
}

/**
 * Implementation of hook_uninstall().
 */
function signature_forum_uninstall() {
  // Copy the signatures back to Drupal core
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("UPDATE {users}, {users_signature}
        SET {users}.signature={users_signature}.signature
        WHERE {users}.uid={users_signature}.uid");
      break;
    case 'pgsql':
      db_query("UPDATE {users} SET signature={users_signature}.signature FROM {users_signature}
        WHERE {users}.uid={users_signature}.uid");
      break;
  }
  drupal_uninstall_schema('signature_forum');
  variable_del('signature_forum_settings');
}

/**
 * Implementation of hook_update_n().
 */
function signature_forum_update_6100() {
  $schema['signature_post'] = array(
    'fields' => array(
      'delta' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'description' => 'The id of the attached object. nid for nodes and cid for comments.',
        'not null' => TRUE,
        'disp-width' => '11'),
      'type' => array(
        'description' => 'The type of comment to attach settings.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => ''),
      'status' => array(
        'type' => 'int',
        'description' => 'A boolean indicator for whether or not a signature should be displayed: 1 means display, 0 means not displayed.',
        'size' => 'tiny',
        'not null' => TRUE,
        'disp-width' => '11'),
    ),
    'primary key' => array('delta', 'type'),
  );
  $ret = array();
  db_add_field($ret, 'users_signature', 'status', array(
        'type' => 'int',
        'description' => 'A boolean indicator for whether or not a signature should be displayed by default: 1 means display, 0 means not displayed.',
        'size' => 'tiny',
        'not null' => FALSE,
        'initial' => 1,
  ));
  db_create_table($ret, 'signature_post', $schema['signature_post']);
  signature_forum_build_signature_status();
  return $ret;
}
