You are here

commerce_cardonfile.install in Commerce Card on File 7.2

Same filename and directory in other branches
  1. 7 commerce_cardonfile.install

Installs the tables required by Commerce Card on File.

File

commerce_cardonfile.install
View source
<?php

/**
 * @file
 * Installs the tables required by Commerce Card on File.
 */

/**
 * Implements hook_uninstall().
 */
function commerce_cardonfile_uninstall() {
  field_delete_field('commerce_cardonfile_profile');

  // Purge field data now to allow cardonfile module to be uninstalled
  // if this is the only field remaining.
  field_purge_batch(10);
}

/**
 * Implements hook_schema().
 */
function commerce_cardonfile_schema() {
  $schema = array();
  $schema['commerce_cardonfile'] = array(
    'description' => 'The base table for the commerce_cardonfile entity type.',
    'fields' => array(
      'card_id' => array(
        'description' => 'Primary key: numeric card id.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'The {users}.uid of the card owner.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'order_id' => array(
        'description' => 'The {commerce_order}.order_id that originally supplied this card data, if card data was created from checkout.',
        'type' => 'int',
        'unsigned' => TRUE,
      ),
      'payment_method' => array(
        'description' => 'The method_id of the payment method that stored the card.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'instance_id' => array(
        'description' => 'The instance_id of the payment method that stored the card.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'remote_id' => array(
        'description' => 'The id of the card at the payment gateway.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'card_type' => array(
        'description' => 'The card type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'card_name' => array(
        'description' => 'The name on the card.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'card_number' => array(
        'description' => 'Truncated card number (last 4 digits).',
        'type' => 'varchar',
        'length' => 4,
        'not null' => TRUE,
        'default' => '',
      ),
      'card_exp_month' => array(
        'description' => 'Expiration month.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'card_exp_year' => array(
        'description' => 'Expiration year.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
      ),
      'instance_default' => array(
        'description' => 'Whether this is the default card for this payment method instance.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'status' => array(
        'description' => 'Card status: inactive (0), active (1), not deletable (2), declined (3).',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the card was first stored.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the card was last updated.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'card_id',
    ),
    'indexes' => array(
      'uid' => array(
        'uid',
      ),
      'order_id' => array(
        'order_id',
      ),
      'instance_id' => array(
        'instance_id',
      ),
    ),
    'foreign keys' => array(
      'creator' => array(
        'table' => 'users',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
      'order' => array(
        'table' => 'commerce_order',
        'columns' => array(
          'order_id' => 'order_id',
        ),
      ),
    ),
  );
  return $schema;
}

/**
 * Add the instance_default column to card data table
 */
function commerce_cardonfile_update_7101(&$sandbox) {
  if (!db_field_exists('commerce_card_data', 'instance_default')) {
    db_add_field('commerce_card_data', 'instance_default', array(
      'description' => 'Boolean indicating the default card for a payment instance ID.',
      'type' => 'int',
      'size' => 'tiny',
      'not null' => TRUE,
      'default' => 0,
    ));
  }
  return t('Card data table has been updated.');
}

/**
 * Update permission from 'manage' to 'view', 'edit', 'delete'.
 */
function commerce_cardonfile_update_7102(&$sandbox) {

  // Load utility functions.
  module_load_install('commerce');

  // Retrieve existing permissions.
  $result = db_select('role_permission', 'p')
    ->fields('p')
    ->condition('permission', 'manage own card data')
    ->execute();
  $records = $result
    ->fetchAll(PDO::FETCH_ASSOC);
  if (empty($records)) {
    return t('No permissions to update.');
  }

  // Update permission and views access from 'manaage' to 'view'.
  $map = array(
    'manage own card data' => 'view own card data',
  );
  commerce_update_rename_permissions($map);

  // Create 'edit' and 'delete' for 'manage' permissions to mimic old access.
  $inserts = array();
  foreach ($records as $record) {
    $inserts[] = array(
      'permission' => 'edit own card data',
    ) + $record;
    $inserts[] = array(
      'permission' => 'delete own card data',
    ) + $record;
  }
  $query = db_insert('role_permission')
    ->fields(array(
    'rid',
    'permission',
    'module',
  ));
  foreach ($inserts as $insert) {
    $query
      ->values($insert);
  }
  $query
    ->execute();
  return t('Role and custom View permissions updated to new permissions. Access checks in modules and permissions on default Views must be updated manually.');
}

/**
 * Change the card on file table name.
 */
function commerce_cardonfile_update_7103(&$sandbox) {
  if (db_table_exists('commerce_card_data')) {
    db_rename_table('commerce_card_data', 'commerce_cardonfile');
  }
  return t('Card data table has been updated.');
}

/**
 * Add the Customer Billing Profile field to the card on file entity.
 */
function commerce_cardonfile_update_7104(&$sandbox = NULL) {

  // No longer needed, fields are now created on hook_flush_caches().
}

/**
 * Adds the order_id column to the {commerce_cardonfile} table.
 */
function commerce_cardonfile_update_7205() {
  $spec = array(
    'description' => 'The {commerce_order}.order_id that originally supplied this card data, if card data was created from checkout.',
    'type' => 'int',
    'unsigned' => TRUE,
  );
  db_add_field('commerce_cardonfile', 'order_id', $spec);
  db_add_index('commerce_cardonfile', 'order_id', array(
    'order_id',
  ));
  return t('Added order_id column to commerce_cardonfile table.');
}

Functions

Namesort descending Description
commerce_cardonfile_schema Implements hook_schema().
commerce_cardonfile_uninstall Implements hook_uninstall().
commerce_cardonfile_update_7101 Add the instance_default column to card data table
commerce_cardonfile_update_7102 Update permission from 'manage' to 'view', 'edit', 'delete'.
commerce_cardonfile_update_7103 Change the card on file table name.
commerce_cardonfile_update_7104 Add the Customer Billing Profile field to the card on file entity.
commerce_cardonfile_update_7205 Adds the order_id column to the {commerce_cardonfile} table.