You are here

commerce_registration_defer.install in Commerce Registration 7.2

Schema and installation hooks for commerce_registration_defer module.

File

modules/commerce_registration_defer/commerce_registration_defer.install
View source
<?php

/**
 * @file
 * Schema and installation hooks for commerce_registration_defer module.
 */

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

  // Create deferred states.
  $states = array(
    'deferred' => array(
      'label' => 'Deferred',
      'description' => 'Registration was deferred',
      'default_state' => 0,
      'active' => 0,
      'held' => 0,
      'show_on_form' => 0,
      'weight' => 2,
    ),
  );
  $registration_states = db_query('SELECT rs.name as state FROM {registration_state} rs')
    ->fetchCol();

  // Don't add the deferred states if they already exist.
  if ($registration_states) {
    $registration_states = array_flip($registration_states);
    $states = array_diff_key($states, $registration_states);
  }
  foreach ($states as $state_name => $state_label) {
    $registration_state = entity_create('registration_state', array(
      'name' => $state_name,
      'label' => $state_label['label'],
      'description' => $state_label['description'],
      'default_state' => $state_label['default_state'],
      'active' => $state_label['active'],
      'held' => $state_label['held'],
      'show_on_form' => $state_label['show_on_form'],
      'weight' => $state_label['weight'],
    ));
    $registration_state
      ->save();
  }
}

/**
 * Implements hook_uninstall().
 */
function commerce_registration_defer_uninstall() {

  // Only remove the deferred state if there are no registrations that have that
  // state.
  if (db_query('SELECT 1 FROM {registration} r WHERE r.state = :state limit 0, 1', array(
    ':state' => 'deferred',
  ))
    ->fetchField()) {
    return;
  }

  // Remove deferred states.
  $deferred_states = array(
    'deferred',
  );
  db_delete('registration_state')
    ->condition('name', $deferred_states, 'IN')
    ->execute();
}