<?php

/**
 * @file
 * Install, update, and uninstall functions for the Archibald LP21 module.
 */

/**
 * Implements hook_install().
 */
function archibald_lp21_install() {
  variable_set('archibald_lp21_curriculum_variant', 'V_EF');
}

/**
 * Implements hook_enable().
 */
function archibald_lp21_enable() {
  $t = get_t();
  drupal_set_message($t("In order to use Archibald LP21, you will have to take several additional steps. Please see the <a href='!link'>status</a> page for more information.", array(
    '!link' => url('admin/reports/status'),
  )), 'status');
}

/**
 * Implements hook_uninstall().
 */
function archibald_lp21_uninstall() {
  // Remove custom variables.
  foreach (array(
    'archibald_lp21_entities_added_fields',
    'archibald_lp21_curriculum_variant',
  ) as $variable) {
    variable_del($variable);
  }

  // Remove custom cache entries.
  cache_clear_all('archibald_lp21:*', 'cache_curricula', TRUE);
}

/**
 * Implements hook_field_schema().
 */
function archibald_lp21_field_schema($field) {
  switch ($field['type']) {
    case 'archibald_lp21':
      return array(
        'columns' => array(
          'item_ids' => array('type' => 'text'),
          'cycles' => array('type' => 'varchar', 'length' => 10),
        ),
      );
  }
}

/**
 * Implements hook_requirements().
 */
function archibald_lp21_requirements($phase) {
  $t = get_t();
  $return = array();

  if ($phase == 'runtime') {
    if (!archibald_load_default_partner()) {
      $return['archibald_lp21__lp21_data_imported'] = array(
        'title' => $t("Archibald LP21: curriculum data loaded"),
        'description' => $t("The LP21 curriculum data cannot be imported unless a default partner is set."),
        'value' => $t("No default partner"),
        'severity' => REQUIREMENT_ERROR,
      );
    }
    else {
      $variant = variable_get('archibald_lp21_curriculum_variant');
      $cache = cache_get('archibald_lp21:item_db:' . $variant, 'cache_curricula');

      $return['archibald_lp21__lp21_data_imported'] = array(
        'title' => $t("Archibald LP21: curriculum data loaded"),
        'description' => $t("The LP21 curriculum data can be imported and/or refreshed by visiting <a href='!link'>this link</a>.", array(
          '!link' => url('admin/archibald-lp21/load-lp21-data'),
        )),
        'value' => !empty($cache->data) ?
          $t("Imported") :
          $t("Not imported"),
        'severity' => !empty($cache->data) ?
          REQUIREMENT_OK :
          REQUIREMENT_ERROR,
      );
    }
  }

  $available_memory = ini_get("memory_limit");
  if ($available_memory == -1) {
    $available_memory = 2048;
  }
  $available_memory = preg_replace('/\D/', '', $available_memory);
  $return['archibald_lp21__memory_limit'] = array(
    'title' => $t("Archibald LP21: PHP recommended memory limit"),
    'description' => $t("Archibald LP21 must load a large XML file to parse curricula data. This can be very memory intensive. It is recommended to have at least 128M of memory available for PHP."),
    'value' => $available_memory,
    'severity' => $available_memory >= 128 ? REQUIREMENT_OK : REQUIREMENT_WARNING,
  );

  $xdebug_max_nesting_level = ini_get("xdebug.max_nesting_level");
  if (!empty($xdebug_max_nesting_level)) {
    $return['archibald_lp21__xdebug_nesting_level'] = array(
      'title' => $t("Archibald LP21: Xdebug nesting level"),
      'description' => $t("Archibald LP21 will parse a large XML file to get the curricula data. If the Xdebug max nesting level setting is too low, it will not be able to finish."),
      'value' => $xdebug_max_nesting_level,
      'severity' => $xdebug_max_nesting_level < 200 && $xdebug_max_nesting_level != -1 ?
        REQUIREMENT_ERROR :
        REQUIREMENT_OK,
    );
  }

  $max_execution_time = ini_get("max_execution_time");
  $return['archibald_lp21__max_execution_time'] = array(
    'title' => $t("Archibald LP21: PHP max execution time"),
    'description' => $t("Archibald LP21 will parse a large XML file to get the curricula data. This can be very time consuming, especially on older systems. On slower systems, it is recommended to have at least 60s of execution time."),
    'value' => $max_execution_time,
    'severity' => $max_execution_time < 60 && $max_execution_time >= 0 ?
      REQUIREMENT_WARNING :
      REQUIREMENT_OK,
  );

  return $return;
}
