You are here

webform_invitation.install in Webform Invitation 7.2

File

webform_invitation.install
View source
<?php

function webform_invitation_schema() {
  $schema['webform_invitation_codes'] = array(
    'description' => 'Table for storing generated form tokens.',
    'fields' => array(
      'cid' => array(
        'description' => 'The primary identifier for a code.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'nid' => array(
        'description' => 'The primary identifier for a node.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'code' => array(
        'description' => 'A code for the webform.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'time_generated' => array(
        'description' => 'The Unix timestamp when the code was generated.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'used' => array(
        'description' => 'The Unix timestamp when the code was used.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => NULL,
      ),
      'sid' => array(
        'description' => 'The submission ID using this code.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
      'used' => array(
        'used',
      ),
    ),
    'unique keys' => array(
      'nid_code' => array(
        'nid',
        'code',
      ),
    ),
    'primary key' => array(
      'cid',
    ),
  );
  $schema['webform_invitation'] = array(
    'description' => 'Table for storing invitation settings.',
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'invitation' => array(
        'description' => 'Use invitation codes?',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'cid' => array(
        'description' => 'Component ID of Webform Invitation textfield.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
    ),
  );
  return $schema;
}

/*
 *	Remove Webform Invitation textfields from
 *	existing webforms with invitation mode activated.
 */
function webform_invitation_disable() {
  $db_settings = db_select('webform_invitation', 'i')
    ->fields('i')
    ->condition('invitation', 1, '=')
    ->execute();
  while ($result = $db_settings
    ->fetchAssoc()) {
    $node = node_load($result['nid']);

    /*
     * The actual component ID is in $result['cid']
     * but we better check for the form_key
     */
    foreach ($node->webform['components'] as $id => $com) {
      if ($com['form_key'] == 'webform_invitation_code') {
        unset($node->webform['components'][$id]);
        node_save($node);
        break;
      }
    }
  }
}

/**
 * Add submission id column to codes table and component id column to base table.
 */
function webform_invitation_update_7200() {
  db_add_field('webform_invitation', 'cid', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'Component ID of Webform Invitation textfield.',
  ));
  db_add_field('webform_invitation_codes', 'sid', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'The submission ID using this code.',
  ));
}

/**
 * Ensure that existing webform entries in base table have the correct cid set.
 */
function webform_invitation_update_7201() {
  $db_settings = db_select('webform_invitation', 'i')
    ->fields('i')
    ->condition('invitation', 1, '=')
    ->execute();
  while ($result = $db_settings
    ->fetchAssoc()) {
    $node = node_load($result['nid']);

    /*
     * The actual component ID is in $result['cid']
     * but we better check for the form_key
     */
    foreach ($node->webform['components'] as $id => $com) {
      if ($com['form_key'] == 'webform_invitation_code') {
        db_merge('webform_invitation')
          ->key(array(
          'nid' => $result['nid'],
        ))
          ->fields(array(
          'cid' => $id,
        ))
          ->execute();
        break;
      }
    }
  }
}

/**
 * Alter generated column name since GENERATED is a MySQL reserved word as of 5.7.6.
 */
function webform_invitation_update_7202() {
  $schema = webform_invitation_schema();
  $field = $schema['webform_invitation_codes']['fields']['time_generated'];

  # We have no keys or indexes on this field. We can just change the field.
  db_change_field('webform_invitation_codes', 'generated', 'time_generated', $field);
}

Functions

Namesort descending Description
webform_invitation_disable
webform_invitation_schema
webform_invitation_update_7200 Add submission id column to codes table and component id column to base table.
webform_invitation_update_7201 Ensure that existing webform entries in base table have the correct cid set.
webform_invitation_update_7202 Alter generated column name since GENERATED is a MySQL reserved word as of 5.7.6.