You are here

invite.install in Invite 7.4

Contains install and update functions for Invite.

File

invite.install
View source
<?php

/**
 * @file
 * Contains install and update functions for Invite.
 */

/**
 * Implements hook_schema().
 */
function invite_schema() {
  $schema = array();
  $schema['invite'] = array(
    'description' => 'The base table for invites.',
    'fields' => array(
      'iid' => array(
        'description' => 'The primary identifier for the invite.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'reg_code' => array(
        'description' => 'Stores the issued registration code.',
        'type' => 'varchar',
        'length' => 10,
        'not null' => TRUE,
        'default' => '',
      ),
      'type' => array(
        'description' => 'The type (bundle) of this invite.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'uid' => array(
        'description' => 'ID of Drupal user creator.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'invitee' => array(
        'description' => 'Drupal uid of the invitee upon registration.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'status' => array(
        'description' => 'Invitation status.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the invite was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'expiry' => array(
        'description' => 'The Unix timestamp when the invite will expire.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'joined' => array(
        'description' => 'Will be filled with the time the invite was accepted upon registration.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'canceled' => array(
        'description' => 'The Unix timestamp when the invite has been withdrawn.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'data' => array(
        'description' => 'Stores auxiliary data.',
        'type' => 'text',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array(
      'iid',
    ),
    'indexes' => array(
      'invitee' => array(
        'invitee',
      ),
    ),
    'foreign keys' => array(
      'user' => array(
        'table' => 'users',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
    ),
  );
  $schema['invite_type'] = array(
    'description' => 'Stores information about all defined invite types.',
    'fields' => array(
      'itid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique invite type ID.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'label' => array(
        'description' => 'The human-readable name of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'A brief description of this type.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'medium',
        'translatable' => TRUE,
      ),
      'data' => array(
        'description' => 'Stores auxiliary data.',
        'type' => 'text',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
    ) + entity_exportable_schema_fields(),
    'primary key' => array(
      'itid',
    ),
    'unique keys' => array(
      'type' => array(
        'type',
      ),
    ),
  );
  $schema['invite_sending_controller'] = array(
    'fields' => array(
      'type' => array(
        'description' => 'The machine-readable name of Invite Type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'module' => array(
        'description' => 'Module, which implements this controller.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'name' => array(
        'description' => 'The machine-readable name of this sending controller.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
    ),
    'unique keys' => array(
      'invite_type_controller' => array(
        'type',
        'name',
      ),
    ),
    'foreign keys' => array(
      'invite_type' => array(
        'table' => 'invite_type',
        'columns' => array(
          'type' => 'type',
        ),
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function invite_install() {

  // Set default expiry time 30 days.
  variable_set('invite_default_expiry_time', 30);
  drupal_set_message(t('Thank you for installing the <a href="@url_invite" target="blank">Invite</a>.', array(
    '@url_invite' => 'https://www.drupal.org/project/invite',
  )));

  // Notify the user they may want to install token.
  if (!module_exists('token')) {
    $t = get_t();
    drupal_set_message($t('If you install the <a href="!url" target="blank">Token</a>, Invite will be able to use token patterns.', array(
      '!url' => 'http://drupal.org/project/token',
    )));
  }
}

/**
 * Implements hook_install().
 */
function invite_uninstall() {
  $invite_vars = array(
    'invite_message_editable',
    'invite_default_mail_subject',
    'invite_default_mail_body',
    'invite_default_replace_tokens',
    'invite_message_editable',
    'invite_use_users_email_replyto',
    'invite_manual_from',
    'invite_use_users_email',
  );
  foreach ($invite_vars as $invite_var) {
    variable_del($invite_var);
  }
  $variables = db_select('variable', 'v')
    ->fields('v')
    ->condition('name', 'invite_type_%', 'LIKE')
    ->execute();
  foreach ($variables as $variable) {
    variable_del($variable->name);
  }
}

/**
 * Implements update from 2.x version to 4.x.
 */
function invite_update_7400() {
  db_rename_table('invite', 'invite_2x');
  db_rename_table('invite_notifications', 'invite_notifications_2x');

  // Create tables.
  $schema = invite_schema();
  foreach ($schema as $table_name => $specs) {
    db_create_table($table_name, $specs);
  }

  // We unable move the data because invite_by_email should be installed.
  // So all data transfer will be moved.
  variable_set('invite_version_updated', TRUE);
  variable_set('invite_default_expiry_time', 30);
  drupal_set_message(t('Invite was successfully installed. You could migrate !link.', array(
    '!link' => l(t('old invites'), 'admin/config/people/invite/migrate'),
  )));
}

/**
 * Adding 'status' to invite table.
 */
function invite_update_7401() {

  // The status field might already exist on the invite table with incorrect
  // settings.
  // We need to check if the status field exists in the invite table if it does
  // drop it.
  // We are going to overwrite all values in the status field later in this
  // update hook so dropping this field won't have any negative consequences.
  if (db_field_exists('invite', 'status')) {
    db_drop_field('invite', 'status');
  }

  // Add status field to invite table.
  $specs = array(
    'type' => 'int',
    'not null' => TRUE,
    'description' => 'Invitation status.',
    'default' => 1,
    'initial' => 1,
  );
  db_add_field('invite', 'status', $specs);

  // Calculate and store invite status from existing data.
  $invites = db_select('invite', 'i')
    ->fields('i', array(
    'iid',
    'created',
    'canceled',
    'joined',
    'expiry',
  ))
    ->execute();
  foreach ($invites as $invite) {

    // Default to Valid.
    $status = 1;

    // Withdrawn.
    if ($invite->canceled != 0) {
      $status = 2;
    }
    elseif ($invite->joined != 0) {
      $status = 3;
    }
    elseif ($invite->expiry < REQUEST_TIME) {
      $status = 4;
    }
    db_update('invite')
      ->fields(array(
      'status' => $status,
    ))
      ->condition('iid', $invite->iid)
      ->execute();
  }
}

Functions

Namesort descending Description
invite_install Implements hook_install().
invite_schema Implements hook_schema().
invite_uninstall Implements hook_install().
invite_update_7400 Implements update from 2.x version to 4.x.
invite_update_7401 Adding 'status' to invite table.