Skip to content
xc.module 29.2 KiB
Newer Older
<?php
/**
 * @file
 * XC module functions
 *
 * @copyright (c) 2010-2011 eXtensible Catalog Organization
 */

/**
 * Implements hook_permission().
 */
function xc_permission() {
  return array(
    'administer xc' => array(
      'title' => t('administer xc'),
      'description' => t('Administer eXtensible Catalog'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function xc_menu() {
  $items['admin/xc'] = array(
    'title' => 'eXtensible Catalog (XC)',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array('administer xc'),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['admin/xc/common'] = array(
    'title' => 'Common functions',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array('administer xc'),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['admin/xc/common/setup_checklist'] = array(
    'title' => 'Setup checklist',
    'description' => 'Check that whether every necessary component has been set correctly.',
    'page callback' => 'xc_setup_checklist',
    'access arguments' => array('administer xc'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['admin/xc/common/turn_on_alerts'] = array(
    'title' => 'Turn on alerts',
    'description' => 'If the site admin is alerted once, the site will not report the problem again. After he solve the problem, he have to turn this feature on, to activate email sending',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('xc_turn_on_alerts_form'),
    'access arguments' => array('administer xc'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Implements hook_menu_link_alter().
 *
 * @param $item (Array)
 *   Reference to menu item to choose
 */
function xc_menu_link_alter(&$item) {
  if ($item['link_path'] == 'xc/login') {
    $item['options']['query'] = drupal_get_destination();
  }
}

/**
 * Implements hook_menu_alter().
 *
 * Changes the user/view menu's title from View to View My Account
 *
 * @param $items (Array)
 *   Reference to the menu items
 */
function xc_menu_alter(&$items) {
  // TODO: this should be optional.
  // 1) create an admin form
  // 2) use variable_get('xc_user_view_link_title', 'View My Account');
  $items['user/%user/view']['title'] = 'View My Account';
}

/**
 * Wrapper function for the drupal_set_message, which write a message
 * to the top of the current page
 *
 * @param $module
 *   The name of the module, which sends message
 * @param $message
 *   The content of message
 * @param $type
 *   The type of message {@see drupal_set_message}
 */
function xc_set_message($module, $message, $type = 'error') {
  drupal_set_message(filter_xss('<strong>' . $module . ':</strong> ' . $message), $type);
}

/**
 * Theme preprocessor used to place selector in body classes for easier theming.
 *
 * Adds tab-xc-search, tab-xc-browse or tab-my-admin depending different criteria.
 * The purpose is to notify the HTML which XC tab should be utilized.
 *
 * @param $variables
 */
function xc_preprocess_page(&$variables) {
  global $_xc_search_no_results;

  // get existing classes of <body> element
Kiraly Peter's avatar
Kiraly Peter committed
  // TODO: in D7 body_classes has not the same meaning as in D6
  if (isset($variables['body_classes'])) {
    $body_classes = explode(' ', $variables['body_classes']);
  }
  else {
    $body_classes = array();
  }

  $active_link = 'xc/search';
  $xc_class = '';
  if (!$_xc_search_no_results && preg_match('{^admin\/xc\/search\/full\b}', $_GET['q'])
       || (arg(0) == 'node'
          && in_array('node-type-xc-manifestation', $body_classes))) {

    if (isset($_GET['caller'])
         && in_array($_GET['caller'], array('xc-browse', 'xc-search', 'my-account'))) {
      $xc_class = 'tab-' . $_GET['caller'];

      switch ($_GET['caller']) {
        case 'xc-browse':
          $browse_commons = xc_browse_cache('browse_commons');
          if (!is_null($browse_commons)) {
            $active_link = 'xc_browse/' . $browse_commons->args[1];
          }
          break;
        case 'my-account':
          $active_link = 'xc/search';
          break;
        case 'xc-search':
        default:
          $active_link = 'xc/search';
          break;
      }
    }
  }
  elseif (!$_xc_search_no_results &&
      preg_match('{^(search\/xc_search|xc\/search)\b}', $_GET['q'])) {
    $xc_class = 'tab-xc-search';
    $active_link = 'xc/search';
  }
  elseif (arg(0) == 'xc_browse') {
    $xc_class = 'tab-xc-browse';
    $active_link = 'xc_browse/' . arg(1);
  }
  elseif (arg(0) == 'user') {
    $xc_class = 'tab-my-account';
    $active_link = 'xc/search';
  }
  elseif (arg(0) == 'demo-login') {
    $xc_class = 'tab-login';
    $active_link = 'xc/search';
  }
  elseif ($_GET['q'] == 'admin/xc/search/more_facets') {
    $xc_class = 'tab-xc-search';
    $active_link = 'xc/search';
  }
  elseif (arg(0) == 'admin') {
    $xc_class = 'tab-admin';
    $active_link = 'xc/search';
  }

  $variables['active_xc_primary_link'] = $active_link;
  $body_classes[] = $xc_class;
  $variables['body_classes'] = implode(' ', array_unique($body_classes));

  // display primary tabs in we come from browse to node
  if (arg(0) == 'node' && isset($_GET['caller']) && $_GET['caller'] == 'xc-browse') {
    $tabs_primary = xc_browse_cache('tabs_primary');
    if ($tabs_primary) {
      // Add browse' primary local tasks tab
      $variables['browse_tabs'] = $tabs_primary;
    }
  }
}

/**
 * Gets the checklist of the important XC settings
 *
 * It provides a help to admin what and where to set in order to achive a properly working site
 */
function xc_setup_checklist() {
  $output = '';

  // intro
  $output .= '<p>' . t('The following list informs you about the readiness of eXtensible Catalog module for using. This list does not guarantee, that every settings is correct, and everything will work fine, since here the Toolkit does not check the consistency of the values, only the existence of some settings. But in case of trouble it helps you to find a possible cause.') . '<p>';

  // The container of the check results
  $check_items = array();

  // images
  $status_ok = theme('image', array('path' => 'misc/watchdog-ok.png', 'width' => '', 'height' => '', 'alt' => t('ok'), 'title' => t('ok')));
  $status_warning = theme('image', array('path' => 'misc/watchdog-warning.png', 'width' => '', 'height' => '', 'alt' => t('warning'), 'title' => t('warning')));
  $status_error = theme('image', array('path' => 'misc/watchdog-error.png', 'width' => '', 'height' => '', 'alt' => t('error'), 'title' => t('error')));

  $xc_modules = array(
    'xc',
    'xc_util',
    'xc_metadata',
    'xc_sql',
    'xc_schema',
    'ncip',
    'xc_ncip_provider',
    'oaiharvester',
    'xc_oaiharvester_bridge',
    'xc_external',
    'syndetics',
    'xc_account',
    'xc_auth',
    'xc_ils',
    'xc_import_export',
    'xc_oai_dc',
    'xc_solr',
    'xc_index',
    'xc_search',
    'ezproxy_url_rewrite',
    'xc_browse',
    'xc_statistics',
    'xc_dewey',
    'xc_xissn',
    'xc_wordnet',
  );

  $disabled_modules = array();
  $unexistent_tables = array();
  foreach ($xc_modules as $xc_module) {
    if (!module_exists($xc_module)) {
      $disabled_modules[] = $xc_module;
    }
    else {
      module_load_include('install', $xc_module);
      $schema = module_invoke($xc_module, 'schema');
      if (empty($schema)) {
        continue;
      }
      foreach ($schema as $table => $table_def) {
        if (!db_table_exists($table)) {
          $unexistent_tables[] = $table;
        }
      }
    }
  }

  if (!empty($disabled_modules)) {
    $list = array();
    foreach ($disabled_modules as $module) { // edit-status-xc-import-export
      $info = unserialize(db_query("SELECT info FROM {system} WHERE name = :name AND type = :type", array(':name' => $module, ':type' => 'module'))->fetchField());
      $list[] = l($info['name'], 'admin/modules/list', array('fragment' => 'edit-status-' . str_replace('_', '-', $module)));
    }
    $check_items[] = array(
      $status_warning,
      format_plural(count($list),
        'The following module is not installed: !list. You can install it at the !module_page.',
        'The following modules are not installed: !list. You can install them at the !module_page.',
         array(
        '!list' => join(', ', $list),
        '!module_page' => l(t('module page'), 'admin/modules/list'),
      )),
    );
  }
  else {
    $check_items[] = array($status_ok, t('All XC modules are installed.'));
  }

  if (!empty($unexistent_tables)) {
    $check_items[] = array(
      $status_error,
      t('The following tables are missing: !list', array('!list' => join(', ', $unexistent_tables))),
    );
  }
  else {
    $check_items[] = array($status_ok, t('All XC database tables are installed.'));
  }

  // check storage locations
  $locations = xc_location_get_all();
  if (!empty($locations)) {
    $problems = array();
    foreach ($locations as $location) {
      if (!is_array($location->types)) {
        $problems[] = t('%location_name location has only one storage type, the other is missing. Edit !location_link.',
          array(
          '%location_name' => $location->name,
          '!location_link' => l(t('location'), 'admin/xc/metadata/location/' . $location->id),
        ));
      }

      if (!$location->has_solr_type) {
        $problems[] = t('%location location does not store records into Solr. Edit !location_link.',
          array(
          '%location' => $location->name,
          '!location_link' => l(t('location'), 'admin/xc/metadata/location/' . $location->id),
        ));
      }
    }
    if (!empty($problems)) {
      $check_items[] = array($status_error, theme('item_list', array('items' => $problems)));
    }
    else {
      $check_items[] = array($status_ok, t('<em>Storage locations</em> are available to use.'));
    }
  }
  else {
    $title = t('Create storage locations');
    $items = array(
      t('goto !admin › !xc › !metadata › !location › !add',
        array(
        '!admin' => l(t('Administer'), 'admin'),
        '!xc' => l(t('eXtensible Catalog (XC)'), 'admin/xc'),
        '!metadata' => l(t('Metadata Storage Configuration and Utilities'), 'admin/xc/metadata'),
        '!location' => l(t('Storage locations'), 'admin/xc/metadata/location'),
        '!add' => l(t('Add storage location'), 'admin/xc/metadata/location/add'),
      )),
      t('fill the form:')
        . theme('item_list', array('items' => array(
          t('<em>Name:</em> default'),
          t('<em>Description:</em> default location'),
        ))),
      t('click <em>Continue</em>'),
      t('accept everything as is if the Solr runs on the default <em>host</em>, <em>port</em> and <em>path</em>. Modify the default values, if your Solr instance is customized'),
      t('click <em>Add storage location</em>'),
    );
    $check_items[] = array($status_error, theme('item_list', array('items' => $items, 'title' => $title, 'type' => 'ol')));
  }

  require_once drupal_get_path('module', 'xc_metadata') . '/includes/xc_metadata.format.inc';
  // check formats
  $formats = xc_format_get();
  if (!empty($formats)) {
    require_once drupal_get_path('module', 'xc_metadata') . '/includes/xc_metadata.format.inc';

    $disabled_formats = array();
    foreach ($formats as $type => $format) {
      if (!xc_format_is_enabled($type)) {
        $disabled_formats[] = $format['name'];
      }
    }
    if (!empty($disabled_formats)) {
      $check_items[] = array(
        $status_warning,
        t('The metadata schemas are disabled: %formats. You should enable these !here if you want to use them.',
        array(
          '%formats' => join(', ', $disabled_formats),
          '!here' => l(t('here'), 'admin/xc/metadata/format'),
        )),
      );
    }
    else {
      $check_items[] = array($status_ok, t('<em>Metadata schemas</em> are available.'));
    }
  }
  else {
    $check_items[] = array($status_error, t('There are no metadata schema available for XC Drupal Toolkit. You must first !install a schema module.',
      array('!install' => l(t('install'), 'admin/modules'))));
  }

  // Checking registered NCIP providers
  $ncip_providers = xc_ncip_provider_get_all();
  if (!empty($ncip_providers)) {
    $check_items[] = array($status_ok, t('<em>NCIP servers</em> are available to use.'));
  }
  else {
    $title = t('Setup NCIP');
    $items = array(
      t('goto !admin › !xc › !ncip › !provider › !add',
        array(
        '!admin' => l(t('Administer'), 'admin'),
        '!xc' => l(t('eXtensible Catalog (XC)'), 'admin/xc'),
        '!ncip' => l(t('NCIP servers'), 'admin/xc/ncip'),
        '!provider' => l(t('NCIP servers'), 'admin/xc/ncip/provider'),
        '!add' => l(t('Add NCIP server'), 'admin/xc/ncip/provider/add'),
      )),
      t('fill the form:')
        . theme('item_list', array('items' => array(
          t('<em>Name</em>: the library name, which provide the NCIP interface (like "Rush Rhees Library")'),
          t('<em>Description</em>: any convenient description about the service or the library'),
          t('<em>Host</em>: the host IP or server name'),
          t('<em>Port</em>: the port number'),
          t('<em>Path</em>: the path part of NCIP (like "/NCIPToolkit_v0.3/")'),
        )))
        . t('<em>To Agency Id</em> section')
        . theme('item_list', array('items' => array(
          t('<em>Scheme</em>: an agency scheme\'s URL'),
          t('<em>Value</em>: the name of the scheme owner (like "University of Rochester")'),
        ))),
      t('click <em>Add NCIP server</em>'),
    );
    $check_items[] = array($status_error, theme('item_list', array('items' => $items, 'title' => $title, 'type' => 'ol')));
  }

  // checking registrated OAI-PMH data repositories
  $provider_count = count(oaiharvester_provider_get_all_names());
  if ($provider_count > 0) {
    $check_items[] = array($status_ok, t('<em>OAI-PMH repositories</em> are available to harvest.'));
  }
  else {
    $title = t('Register an OAI-PMH repository');
    $items = array(
      t('goto !admin › !xc › !harvester › !repository › !add',
        array(
        '!admin' => l(t('Administer'), 'admin'),
        '!xc' => l(t('eXtensible Catalog (XC)'), 'admin/xc'),
        '!harvester' => l(t('Metadata Harvester'), 'admin/xc/harvester'),
        '!repository' => l(t('Repositories'), 'admin/xc/harvester/repository'),
        '!add' => l(t('Add repository'), 'admin/xc/harvester/repository/add'),
      )),
      t('fill the form:')
        . theme('item_list', array('items' => array(
          t('<em>Name</em>: the name of the OAI-PMH repository'),
          t('<em>Url</em>: The base url of the OAI-PMH service'),
        ))),
      t('click <em>Submit and validate</em>'),
    );
    $check_items[] = array($status_error, theme('item_list', array('items' => $items, 'title' => $title, 'type' => 'ol')));
  }

  // Check ILS mapping
  $bibid_mappings = xc_ils_get_all_mappings();
  if (!empty($bibid_mappings)) {
    $check_items[] = array($status_ok, t('<em>Integrated Library Systems </em> are configured.'));
  }
  else {
    $title = t('Setup Integrated Library Systems (ILS) to connect metadata records to NCIP servers for circulation status, call number, and other information.');
    $items = array(
      t('goto !admin › !xc › !ils › !add',
        array(
        '!admin' => l(t('Administer'), 'admin'),
        '!xc' => l(t('eXtensible Catalog (XC)'), 'admin/xc'),
        '!ils' => l(t('ILS (NCIP) Record Mapping Settings'), 'admin/xc/ils'),
        '!add' => l(t('Add'), 'admin/xc/ils/add'),
      )),
      t('fill the form:')
        . theme('item_list', array('items' => array(
          t('<em>NCIP server</em>: select an NCIP server from the list'),
          t('<em>Bib identifier field</em>: any field which identify the bib record (like "xc:recordID|@type=NRU" in the XC record XML schema)'),
        ))),
    );
    $check_items[] = array($status_error, theme('item_list', array('items' => $items, 'title' => $title, 'type' => 'ol')));
  }

  // check authentication
  if (module_exists('xc_auth')) {
    $auth_types = xc_auth_type_get_all();
    if (empty($auth_types)) {
      $title = t('No login forms are available. Create login forms.');

      $items = array(
        t('goto !admin › !xc › !auth › !type › !add',
          array(
          '!admin' => l(t('Administer'), 'admin'),
          '!xc' => l(t('eXtensible Catalog (XC)'), 'admin/xc'),
          '!auth' => l(t('Authentication'), 'admin/xc/auth'),
          '!type' => l(t('Login forms'), 'admin/xc/auth/type'),
          '!add' => l(t('Add login form'), 'admin/xc/auth/type/add'),
        )),
        t('select an <em>Authentication method</em>'),
        t('click on <em>Continue</em> button'),
        t('fill the form:')
          . theme('item_list', array('items' => array(
            t('<em>Name</em>: the name of the the authentication type (examples are NetID, Barcode)'),
            t('<em>Description</em>: provide a short description here'),
            t('<em>Labels and Help Text</em>: customize the login form here'),
            t('<em>NCIP Server and Authentication Type</em>: settings regarding NCIP'),
            t('<em>Permissions automatically applied to new uers</em>: settings regarding to roles'),
          ))),
        t('click <em>Add login form</em> button'),
      );
      $check_items[] = array($status_error, theme('item_list', array('items' => $items, 'title' => $title, 'type' => 'ol')));
    }
    else {
      $check_items[] = array($status_ok, t('Login forms configured.'));
    }

    $login_blocks = xc_auth_login_block_get_all();
    if (empty($login_blocks)) {
      $title = t('No login blocks or pages exist on this system. Add a login block and page.');

      $items = array(
        t('goto !admin › !xc › !auth › !block › !add',
          array(
          '!admin' => l(t('Administer'), 'admin'),
          '!xc' => l(t('eXtensible Catalog (XC)'), 'admin/xc'),
          '!auth' => l(t('Authentication'), 'admin/xc/auth'),
          '!block' => l(t('Login blocks and pages'), 'admin/xc/auth/block'),
          '!add' => l(t('Add login blocks and pages'), 'admin/xc/auth/block/add'),
        )),
        t('select a <em>Login block type</em>'),
        t('click on <em>Continue</em> button'),
        t('fill the form:')
          . theme('item_list', array('items' => array(
            t('<em>Name</em>: the name of the the login form (like "Default login page")'),
            t('<em>Description</em>: provide a short description'),
            t('<em>URL alias</em>: provide the Drupal URL alias for the login page.'),
          ))),
        t('click on <em>Continue</em> button'),
        t('fill all addtional forms'),
        t('click <em>Save</em> button'),
      );
      $check_items[] = array($status_error, theme('item_list', array('items' => $items, 'title' => $title, 'type' => 'ol')));
    }
    else {
      $check_items[] = array($status_ok, t('Login forms and pages configured.'));
    }
  }

  // check harvest schedule
  $has_schedule = (boolean) db_query('SELECT count(*) FROM {oaiharvester_harvester_schedules}')->fetchField();
  if ($has_schedule) {
    $check_items[] = array($status_ok, t('<em>Scheduled harvests</em> are available.'));
  }
  else {
    $title = t('Create a scheduled harvest.');
    $items = array(
      t('goto !admin › !xc › !harvester › !schedule › !add',
        array(
        '!admin' => l(t('Administer'),               'admin'),
        '!xc' => l(t('eXtensible Catalog (XC)'), 'admin/xc'),
        '!harvester' => l(t('Metadata Harvester'),       'admin/xc/harvester'),
        '!schedule' => l(t('Scheduled harvests'),        'admin/xc/harvester/schedule'),
        '!add' => l(t('Add scheduled harvest'),     'admin/xc/harvester/schedule/step1'),
      )),
      t('fill the form:')
        . theme('item_list', array('items' => array(
          t('<em>Select repository</em>: select from the list'),
          t('<em>Schedule</em>: select hourly, daily, or weekly.'),
        ))),
      t('click <em>Next</em>'),
      t('fill the form:')
        . theme('item_list', array('items' => array(
          t('Choose Metadata Format: any format the OAI data provider supports'),
          t('Choose Set: you can choose a given set, or select none, which will harvest all records'),
          t('Choose Parsing Mode: Regular expression based if the response contains lots of records (hundreds). Otherwise you can choose the default DOM based process. Regex method is much more faster, but DOM is more reliable. Except extraordinary case regex is suggested. If you run into a problem with the regex method, please contact us, and choose DOM.'),
          t('Do harvester cache responses? It is useful only in testing phase, but could be misleading in a production server, because it does not reflect the changes on the OAI-PMH server. If you do not want to use the cache more, set it to now, and delete the cache directory. It is the default file directory\'s oaiharvester_http_cache subdirectory.)'),
          t('Storage locations: these are the storages you set up as the first step in this list. Choose at least one location, if you would like our module handle records.'),
          t('Run "preparing metadata for search" step? Check this if you want to index records with Solr.'),
        ))),
      t('Click <em>Submit</em>'),
    );
    $check_items[] = array($status_error, theme('item_list', array('items' => $items, 'title' => $title, 'type' => 'ol')));
  }

  $roles_without_permissions = array();
  $result = db_query('SELECT * FROM {role_permission}');
  $permission_collector = array();
  foreach ($result as $permission) {
    if (!isset($permission_collector[$permission->rid])) {
      $permission_collector[$permission->rid] = array();
    }
    if (!in_array($permission->permission, $permission_collector[$permission->rid])) {
      $permission_collector[$permission->rid][] = $permission->permission;
    }
  }
  foreach ($permission_collector as $rid => $permissions) {
    if (!in_array('access metadata', $permissions)) {
      $name = db_query('SELECT name FROM {role} WHERE rid = :rid', array(':rid' => $rid))->fetchField();
      if (!in_array($name, $roles_without_permissions)) {
        $roles_without_permissions[] = $name;
      }
    }
  }
  if (empty($roles_without_permissions)) {
    $check_items[] = array($status_ok, t('All roles can access XC metadata.'));
  }
  else {
Kiraly Peter's avatar
Kiraly Peter committed
    $check_items[] = array($status_warning,
      t('The following roles cannot access XC metadata: %names. You can set permissions at <em>xc_metadata module</em> section of !permission_page.', array(
        '%names' => join(', ', $roles_without_permissions),
        '!permission_page' => l(
          t('permission administration page'),
Kiraly Peter's avatar
Kiraly Peter committed
          'admin/people/permissions',
          array('fragment' => 'module-xc_metadata')),
      )));
  }

  $tables_with_default_values = array(
    'xc_browse' => array(
      'xc_browse_ui',
      'xc_browse_tab',
      'xc_browse_element',
      'xc_browse_list',
    ),
    'xc_index' => array(
      'xc_index_attribute_to_field',
      'xc_index_field_to_facet',
      'xc_index_field_type',
      'xc_index_super_location',
      'xc_index_sortable_field',
      'xc_index_facet',
    ),
    'xc_metadata' => array(
      'xc_location',
    ),
    'xc_search' => array(
      'xc_search_mlt',
      'xc_search_highlighter',
      'xc_search_date_facet_properties',
      'xc_search_field_facet_properties',
      'xc_search_facet_group',
      'xc_search_facet_field',
      'xc_search_ui',
      'xc_search_sortoption',
      'xc_search_location',
    ),
    'xc_solr' => array(
      'xc_solr_field_type',
      'xc_solr_servers',
    ),
    'xc_sql' => array(
      'xc_sql_storage',
    ),
  );
  $not_populated = array();
  foreach ($tables_with_default_values as $module => $tables) {
    foreach ($tables as $table) {
      // TODO Please convert this statement to the D7 database API syntax.
      $populated = (boolean) db_query('SELECT count(*) FROM {' . $table . '}')->fetchField();
      if (!$populated) {
        $not_populated[$module][] = $table;
      }
    }
  }
  if (empty($not_populated)) {
    $check_items[] = array($status_ok, t('All default values were successfully imported into the appropriate tables.'));
  }
  else {
    $check_items[] = array($status_error, t('The default values for the following modules were not successfully imported into the appropriate tables: @modules', array('@modules' => join(', ', array_keys($not_populated)))));
  }

  if (module_exists('xc_metadata')) {
    require_once drupal_get_path('module', 'xc_metadata') . '/includes/xc_metadata.location.inc';
    $locations = xc_location_get_all();
    $pings = xc_solr_ping($locations);
    foreach ($pings as $ping) {
      if (!$ping['running']) {
        $check_items[] = array($status_error, t('Solr at @url is not running. Please start this Apache Solr instance.', array('@url' => $ping['url'])));
      }
      else {
        $check_items[] = array($status_ok, t('Solr at @url is running', array('@url' => $ping['url'])));
      }
    }
  }

  if (module_exists('xc_wordnet')) {
    global $_xc_wordnet;
    if (!isset($_xc_wordnet)) {
      xc_wordnet_init_solr();
    }

    if (is_null($_xc_wordnet)) {
      $check_items[] = array($status_error, t('Solr for WordNet is not set correctly. Please go to the WordNet integration !admin_page and register it.',
        array('!admin_page' => l(t('administration page'), 'admin/xc/wordnet/settings'))));
    }
    else {
      $result = $_xc_wordnet->ping();
      if (!$result) {
        $check_items[] = array($status_error, t('Solr for Wordnet at @url is not running. Please start this Apache Solr instance.', array('@url' => $_xc_wordnet->getUrl())));
      }
      else {
        $check_items[] = array($status_ok, t('Solr for Wordnet at @url is running.', array('@url' => $_xc_wordnet->getUrl())));
      }
    }
  }

  // TODO: variable_get('syndetics_client_code', ''), and other variable_get..
  if (module_exists('syndetics')) {
    if (variable_get('syndetics_client_code', FALSE) === FALSE) {
      $check_items[] = array($status_warning, t('The institutional code for Syndetics Solution has not been registered.  Configure it in !syndetics_page',
        array('!syndetics_page' => l(t('Syndetics Solution Services settings page'), 'admin/xc/syndetics'))));
    }
    else {
      $check_items[] = array($status_ok, t('The institutional code for Syndetics Solution is already set.'));
    }
  }

  $output .= theme('table', array('header' => array(t('status'), t('message')), 'rows' => $check_items));

  return $output;
}

/**
 * Get an XC settings
 *
 * @param $name (string)
 *   The field name
 * @param $default (mixed)
 *   A default value if there is no such a field in xc_settings table
 */
function xc_settings_get($name, $default) {
tombui's avatar
tombui committed
  $xc_settings = db_query('SELECT * FROM {xc_settings} WHERE id = :id', array(':id' => 1))->fetchObject();
  if (isset($xc_settings->$name)) {
    return $xc_settings->$name;
  }
  else {
    return $default;
  }
}

/**
 * Set an XC settings
 *
 * @param $name (string)
 *   The key
 * @param $value
 *   Any valid value regarding to the field definition
 */
function xc_settings_set($name, $value) {
  $schema = drupal_get_schema('xc_settings');
  if (isset($schema['fields'][$name])) {
    // $xc_settings = db_fetch_object(db_query('SELECT * FROM {xc_settings} WHERE id = 1'));
    // $xc_settings->$name = $value;
    if ($schema['fields'][$name]['type'] == 'int') {
      // TODO Please review the conversion of this statement to the D7 database API syntax.
      /* db_query("UPDATE {xc_settings} SET %s = %d WHERE id = 1", $name, $value) */
      db_update('xc_settings')
  ->fields(array(
        '%s' => $name,
      ))
  ->condition('id', 1)
  ->execute();
    }
    else {
      // TODO Please review the conversion of this statement to the D7 database API syntax.
      /* db_query("UPDATE {xc_settings} SET %s = '%s' WHERE id = 1", $name, $value) */
      db_update('xc_settings')
  ->fields(array(
        '%s' => $name,
      ))
  ->condition('id', 1)
  ->execute();
    }
    // drupal_write_record('xc_settings', $xc_settings, 'id');
  }
  else {
    xc_log_error('xc', 'Usage of undefined field ' . $name . ' in xc_settings_set()');
  }
}

/**
 * Form to set the xc_ncip_provider_admin_alerted to 0 if it is not 0 already.
 *
 * @param $form_state (array)
 *   The FAPI form state
 */
function xc_turn_on_alerts_form($form, &$form_state) {
  $alerted = xc_settings_get('xc_ncip_provider_admin_alerted', 0);

  if ($alerted) {
    $form['message'] = array(
      '#type' => 'item',
      '#markup' => t('If the site admin is alerted once, the site will not report the problem again. After he solve the problem, he have to turn this feature on, to activate email sending.'),
    );

    return confirm_form($form, t('Do you want to turn on alerts?'), '', '', t('Activate alerting email again'), t('Cancel'));
  }
  else {
    $form['message'] = array(
      '#type' => 'item',
      '#markup' => t('The site admin has not been alerted recently, so no action needed this time.'),
    );

    return $form;
  }
}

/**
 * Handles the xc_turn_on_alerts_form submission.
 *
 * Sets the xc_ncip_provider_admin_alerted to 0, which means, that in case of problems
 * the admin can be alerted by email again.
 *
 * @param $form (array)
 * @param $form_state (array)
 */
function xc_turn_on_alerts_form_submit($form, &$form_state) {
  xc_settings_set('xc_ncip_provider_admin_alerted', 0);
}