<?php
// $Id: watcher.install,v 1.1.2.1.2.11 2009/07/13 12:19:32 solipsist Exp $
/**
 * @file
 * Watcher module installer.
 *
 * Watcher Module
 * by Jakob Persson of NodeOne <jakob@nodeone.se>
 * With ideas and feedback from Hans Dekker of Wordsy.com
 *
 * Module allows users to watch nodes and receive notifications when nodes
 * are updated or commented on.
 *
 * Sponsored by
 *   Wordsy - www.wordsy.com
 *   NodeOne - www.nodeone.se
 */

/**
 * Implementation of hook_install()
 */
function watcher_install() {
  if (db_table_exists('watcher_nodes') && db_table_exists('watcher_notify_queue') && db_table_exists('watcher_user_settings')) {
    drupal_set_message("Watcher's tables already exist in the database, this indicates Watcher has been installed previously. You can probably go ahead and use Watcher without any further actions.");
    return;
  }

  if (drupal_install_schema('watcher')) {
    drupal_set_message('Watcher was successfully installed.');
  }
}

/**
 * Implementation of hook_uninstall()
 */
function watcher_uninstall() {

  // Uninstall schemas
  drupal_uninstall_schema('watcher');

  // Empty site cache
  cache_clear_all('*', 'cache', true);
  cache_clear_all('*', 'cache_filter', true);
  cache_clear_all('*', 'cache_menu', true);
  cache_clear_all('*', 'cache_page', true);

  // Clear variables
  db_query("DELETE FROM {variable} WHERE name LIKE('%s%%') ", 'watcher_');
  cache_clear_all('variables', 'cache');

}

/**
 * Updates 5.x -> 6.x-1.0
 */
function watcher_update_6000() {
  $ret = array();
  db_add_field($ret, 'watcher_nodes', 'mail', array('type' => 'varchar', 'length' => 128, 'default' => null));
  db_drop_primary_key($ret, 'watcher_nodes');
  db_add_primary_key($ret, 'watcher_nodes', array('uid', 'nid', 'mail'));
  return $ret;
}

function watcher_update_6001() {
  $ret = array();
  db_add_field($ret, 'watcher_nodes', 'watch_for', array('type' => 'int', 'size' => 'tiny', 'default' => null));
  return $ret;
}

function watcher_update_6002() {
  $ret = array();
  $schema = watcher_schema();
  foreach ($schema as $table => $table_definition) {
    if ($table == 'watcher_user_settings') {
      foreach ($table_definition as $property => $property_definition) {
        if ($property == 'fields') {
          foreach ($property_definition as $field => $field_definition) {
            if ($field != 'uid') {
              // We cannot set default value to integer 0 for these fields using
              // db_field_set_default() due to a bug
              // See: http://drupal.org/node/517642
              // db_field_set_default($ret, $table, $field, 0);
              $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' SET DEFAULT '. 0);
            }
          }
        }
      }
    }
  }
  
  return $ret;
}


/**
 * Implementation of hook_schema()
 */
function watcher_schema() {
  $schema['watcher_nodes'] = array(
    'description' => 'Holds what users are watching what nodes',
    'fields' => array(
      'uid' => array(
        'description' => 'Holds UID of user',
        'type' => 'int',
        'not null' => true,
        'disp-width' => '10'
      ),
      'nid' => array(
        'description' => 'Holds NID of node',
        'type' => 'int',
        'not null' => true,
        'disp-width' => '10'
      ),
      'mail' => array(
        'description' => 'Holds email address of user (if user is anonymous)',
        'type' => 'varchar',
        'length' => 128,
        'default' => null
      ),
      'send_email' => array(
        'description' => 'Whether the user wants to be notified by email or not',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => true,
        'default' => 0,
        'disp-width' => '1'
      ),
      'added' => array(
        'description' => 'Timestamp of when row was added',
        'type' => 'int',
        'not null' => true,
        'disp-width' => '11'
      ),
      'watch_for' => array(
        'description' => 'What the user is watching for (anonymous users): 1 = all, 2 = comments, 3 = updates',
        'type' => 'int',
        'size' => 'tiny',
        'default' => null
      ),
    ),
    'primary key' => array('uid', 'nid', 'mail'),
  );

  $schema['watcher_notify_queue'] = array(
    'description' => 'Holds the queue of notifications',
    'fields' => array(
      'qid' => array(
        'description' => 'Queue item ID',
        'type' => 'serial',
        'not null' => true,
        'disp-width' => '32'
      ),
      'uid' => array(
        'description' => 'Recipient UID',
        'type' => 'int',
        'not null' => true,
        'disp-width' => '10'
      ),
      'mail' => array(
        'description' => 'Recipient email address',
        'type' => 'varchar',
        'length' => '64',
        'not null' => true
      ),
      'subject' => array(
        'description' => 'Message subject',
        'type' => 'varchar',
        'length' => '192',
        'not null' => true
      ),
      'message' => array(
        'description' => 'Notification message',
        'type' => 'text',
        'not null' => true
      )
    ),
    'primary key' => array('qid'),
  );

  $schema['watcher_user_settings'] = array(
    'description' => 'Holds personal user settings',
    'fields' => array(
      'uid' => array(
        'description' => 'User UID',
        'type' => 'int',
        'not null' => true,
        'disp-width' => '10',
      ),
      'automatic_enable_notifications' => array(
        'description' => 'Automatically enable notifications for the post when a user starts watching a post',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => true,
        'disp-width' => '1',
        'default' => 0
      ),
      'notifications_updates' => array(
        'description' => 'User will be notified about updates of posts the user watches',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => true,
        'disp-width' => '1',
        'default' => 0
      ),
     'notifications_new_comments' => array(
        'description' => 'User will be notified about new comments on posts the user watches',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => true,
        'disp-width' => '1',
        'default' => 0
      ),
      'autowatch_commented_on' => array(
        'description' => 'User will automatically watch nodes the user comments on',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => true,
        'disp-width' => '1',
        'default' => 0
      ),
      'autowatch_posted' => array(
        'description' => 'User will automatically watch nodes the user creates',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => true,
        'disp-width' => '1',
        'default' => 0
      ),
      'share_binder' => array(
        'description' => 'Whether the user\'s list of watched posts should be viewable by others',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => true,
        'disp-width' => '1',
        'default' => 0
      ),
      'custom' => array(
        'description' => 'Whether the user has customized the settings or whether they were derived from the defaults',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => true,
        'disp-width' => '1',
        'default' => 0
      )
    ),
    'primary key' => array('uid'),
  );
  
  return $schema;
}
