<?php

/**
 * @file
 * Install, update, and uninstall functions for Archibald Classification Systems.
 */

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

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

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

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

  if ($phase == 'runtime') {
    if (!archibald_load_default_partner()) {
      $return['archibald_stdlehr__stdlehr_data_imported'] = array(
        'title' => $t("Archibald Classification Systems: data loaded"),
        'description' => $t("The Classification Systems data cannot be imported unless a default partner is set."),
        'value' => $t("No default partner"),
        'severity' => REQUIREMENT_ERROR,
      );
    }
    else {
      if ($cache = cache_get('archibald_stdlehr:curriculum_data', 'cache_curricula')) {
        $needs_refresh = $cache->created <= REQUEST_TIME - ARCHIBALD_STDLEHR_REFRESH_INTERVAL;
      }
      $return['archibald_stdlehr__stdlehr_data_imported'] = array(
        'title' => $t("Archibald Classification Systems: data loaded"),
        'description' => $t("The Classification Systems data can be imported and/or refreshed by visiting <a href='!link'>this link</a>.", array(
          '!link' => url('admin/archibald-stdlehr/load-stdlehr-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_stdlehr__stdlehr_data_imported']['description'] .= '<br />' . $t("The cached Classification Systems data may be stale (last imported on %date). It is recommended to refresh it.", array(
          '%date' => format_date($cache->created),
        ));
      }
    }
  }

  return $return;
}
