<?php
/**
 * @file
 * This is the installation file for the Background Process module
 */

/**
 * Implements of hook_enable().
 */
function background_process_enable() {
  $_SESSION['background_process_determine_default_service_host'] = TRUE;
}

/**
 * Implements of hook_schema().
 */
function background_process_schema() {
  $schema = array();

  $schema['background_process'] = array(
    'fields' => array(
      'handle' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'callback' => array(
        'type' => 'text',
        'not null' => FALSE,
      ),
      'args' => array(
        'type' => 'blob',
        'not null' => FALSE,
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'token' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'service_host' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'start_stamp' => array(
        'type' => 'varchar',
        'length' => '18',
        'not null' => FALSE,
      ),
      'exec_status' => array(
        'type' => 'int',
        'size' => 'normal',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('handle'),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function background_process_uninstall() {
  // Removing process variables.
  variable_del('background_process_service_timeout');
  variable_del('background_process_connection_timeout');
  variable_del('background_process_stream_timeout');
  variable_del('background_process_service_groups');
  variable_del('background_process_default_service_group');
  variable_del('background_process_service_hosts');
  variable_del('background_process_default_service_host');
  variable_del('background_process_cleanup_age');
  variable_del('background_process_queues');
  variable_del('background_process_derived_default_host');
  variable_del('background_process_token');
}

/**
 * Implements hook_requirements().
 */
function background_process_requirements($phase) {
  $response = array();
  switch ($phase) {
    case 'install':
      return $response;
    case 'runtime':
      $response['title'] = 'Background Process';
      $response['value'] = t('OK');
      $response['severity'] = REQUIREMENT_OK;
      if (ini_get('safe_mode')) {
        $desc = t('Safe mode enabled. Background Process is unable to control maximum execution time for background processes. This may cause background processes to end prematurely.');
        if ($response['severity'] < REQUIREMENT_WARNING) {
          $response['severity'] = REQUIREMENT_WARNING;
          $response['value'] = t('Safe mode enabled');
          $response['description'] = $desc;
        }
        else {
          $response['description'] .= '<br/>' . $desc;
        }
      }
      $result = array();
      $result['background_process'] = $response;
      return $result;
  }
}

/**
 * Major version upgrade of Drupal
 */
function background_process_update_7000(&$context) {
  $context['sandbox']['major_version_upgrade'] = array(
    7101 => TRUE,
    7102 => TRUE,
    7103 => TRUE,
    7104 => TRUE,
    7105 => TRUE,
    7106 => TRUE,
  );
}

/**
 * Add status column to background_process table.
 */
function background_process_update_7101() {
  if (!empty($context['sandbox']['major_version_upgrade'][7101])) {
    // This udate is already part of latest 6.x
    return;
  }
  db_add_field('background_process', 'status', array(
    'type' => 'int',
    'size' => 'normal',
    'not null' => TRUE,
    'default' => 0,
  ));
}

/**
 * Determine default service host
 */
function background_process_update_7102() {
}

/**
 * Determine default service host
 */
function background_process_update_7103() {
}

/**
 * Change start column from double to numeric
 */
function background_process_update_7104() {
  if (!empty($context['sandbox']['major_version_upgrade'][7104])) {
    // This udate is already part of latest 6.x
    return;
  }
  db_change_field('background_process', 'start', 'start', array(
    'type' => 'numeric',
    'precision' => '16',
    'scale' => '6',
    'not null' => FALSE,
  ));
}

/**
 * Re-determine default service host.
 */
function background_process_update_7105() {
  if (!empty($context['sandbox']['major_version_upgrade'][7105])) {
    // This udate is already part of latest 6.x
    return;
  }
  $_SESSION['background_process_determine_default_service_host'] = TRUE;
}

/**
 * Change schema to SQL 99 compliance
 */
function background_process_update_7106() {
  if (!empty($context['sandbox']['major_version_upgrade'][7106])) {
    // This udate is already part of latest 6.x
    return;
  }
  db_change_field('background_process', 'start', 'start_stamp', array(
    'type' => 'varchar',
    'length' => '18',
    'not null' => FALSE,
  ));
  db_change_field('background_process', 'status', 'exec_status', array(
    'type' => 'int',
    'size' => 'normal',
    'not null' => TRUE,
    'default' => 0,
  ));
}