You are here

function invite_update_7401 in Invite 7.4

Adding 'status' to invite table.

File

./invite.install, line 244
Contains install and update functions for Invite.

Code

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();
  }
}