<?php
// $Id: single_login.install,v 1.3.2.1 2009/04/01 15:32:35 sanduhrs Exp $

/**
 * Single Login is a session management system for Drupal.
 * 
 * @file
 * Single login install routines.
 */

function single_login_install() {
  drupal_install_schema('single_login');
  
  // needed for google analytics
  db_query("INSERT INTO {profile_fields} 
              (title, name, explanation, category, page, type, weight, required, register, visibility, autocomplete, options) 
            VALUES 
              ('Current Session ID', 'profile_current_session_id', 'User session ID','User Information', '', 'textfield', 0, 0, 0, 4, 0, '')");
}

function single_login_uninstall() {
  drupal_uninstall_schema('single_login');

  $fid = db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = 'profile_current_session_id'"));
  if ($fid) {
    db_query("DELETE FROM {profile_fields} WHERE fid = %d", $fid);
    db_query("DELETE FROM {profile_values} WHERE fid = %d", $fid);
  }
}

function single_login_schema() {
  $schema['single_login'] = array(
    'fields' => array(
      'single_login_id'  => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
      'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
      'counter' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'disp-width' => 0),
    ),
    'unique keys' => array(
      'uid' => array('uid')
    ),
    'indexes' => array(
      'counter' => array('counter')
    ),
    'primary key' => array('single_login_id'),
  );
  
  $schema['single_login_history'] = array(
    'fields' => array(
      'history_id'  => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'disp-width' => 10),
      'uid'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'disp-width' => 10),
      'session_id'  => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE),
      'date'      => array('type' => 'int', 'not null' => TRUE, 'disp-width' => 11),
      'ip'      => array('type' => 'varchar', 'length' => 15, 'not null' => TRUE),
      'browser'    => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
      'type'      => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => 'login'),
    ),
    'unique keys' => array(
      'session_id' => array('session_id')
    ),
    'indexes' => array(
      'uid' => array('uid')
    ),
    'primary key' => array('history_id'),
  );

  return $schema;
}
