<?php

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

/**
 * Implements hook_enable().
 */
function archibald_per_enable() {
  $t = get_t();
  drupal_set_message($t("In order to use Archibald PER, 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_per_uninstall() {
  // Remove custom variables.
  foreach (array(
    'archibald_per_entities_added_fields',
  ) as $variable) {
    variable_del($variable);
  }

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

/**
 * Implements hook_field_schema().
 */
function archibald_per_field_schema($field) {
  switch ($field['type']) {
    case 'archibald_per':
      return array(
        'columns' => array(
          'item_ids' => array('type' => 'text'),
        ),
      );
  }
}

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

  if ($phase == 'runtime') {
    if ($cache = cache_get('archibald_per:item_db', 'cache_curricula')) {
      $needs_refresh = $cache->created <= REQUEST_TIME - ARCHIBALD_PER_REFRESH_INTERVAL;
    }
    $return['archibald_per__per_data_imported'] = array(
      'title' => $t("Archibald PER: curriculum data loaded"),
      'description' => $t("The PER curriculum data can be imported and/or refreshed by visiting <a href='!link'>this link</a>.", array(
        '!link' => url('admin/archibald-per/load-per-data'),
      )),
      'value' => !empty($cache->data) ?
        ($needs_refresh ? $t("Possibly outdated") : $t("Imported")) :
        $t("Not imported"),
      'severity' => !empty($cache->data) ?
        ($needs_refresh ? REQUIREMENT_WARNING : REQUIREMENT_OK) :
        REQUIREMENT_ERROR,
    );

    if (!empty($cache->data) && !empty($cache->created) && $needs_refresh) {
      $return['archibald_per__per_data_imported']['description'] .= '<br />' . $t("The cached PER data may be stale (last imported on %date). It is recommended to refresh it.", array(
        '%date' => format_date($cache->created),
      ));
    }
  }

  $xdebug_max_nesting_level = ini_get("xdebug.max_nesting_level");
  if (!empty($xdebug_max_nesting_level)) {
    $return['archibald_per__xdebug_nesting_level'] = array(
      'title' => $t("Archibald PER: Xdebug nesting level"),
      'description' => $t("Archibald PER will make a lot of cURL calls 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_per__max_execution_time'] = array(
    'title' => $t("Archibald PER: PHP max execution time"),
    'description' => $t("Archibald PER will need to gather a lot of information via cURL 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 30s of execution time."),
    'value' => $max_execution_time,
    'severity' => $max_execution_time < 30 && $max_execution_time >= 0 ?
      REQUIREMENT_WARNING :
      REQUIREMENT_OK,
  );

  return $return;
}
