You are here

openlayers.install in Openlayers 7.2

Same filename and directory in other branches
  1. 6.2 openlayers.install
  2. 6 openlayers.install
  3. 7.3 openlayers.install

This file holds the functions for the installing and enabling of the openlayers module.

File

openlayers.install
View source
<?php

/**
 * @file
 * This file holds the functions for the installing
 * and enabling of the openlayers module.
 *
 * @ingroup openlayers
 */

/**
 * Implements hook_uninstall().
 */
function openlayers_uninstall() {

  // Get module variables
  global $conf;
  foreach (array_keys($conf) as $key) {

    // Find variables that have the module prefix
    if (strpos($key, 'openlayers_') === 0) {
      variable_del($key);
    }
  }
}

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

  // Maps table (ctools extras)
  $schema['openlayers_maps'] = array(
    'description' => 'Storage for User defined OpenLayers maps.',
    'export' => array(
      'key' => 'name',
      'key name' => 'Name',
      'primary key' => 'name',
      'identifier' => 'openlayers_maps',
      'default hook' => 'openlayers_maps',
      'api' => array(
        'owner' => 'openlayers',
        'api' => 'openlayers_maps',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'name' => array(
        'description' => 'The primary identifier for the map.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'title' => array(
        'description' => 'The title of the map.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'description' => array(
        'description' => 'The description of the map.',
        'type' => 'text',
        'not null' => TRUE,
      ),
      'data' => array(
        'description' => 'The serialized map.',
        'type' => 'text',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array(
      'name',
    ),
  );

  // Layer table (ctools extras)
  $schema['openlayers_layers'] = array(
    'description' => 'Storage for user defined OpenLayers layers.',
    'export' => array(
      'key' => 'name',
      'key name' => 'Name',
      'primary key' => 'name',
      'identifier' => 'openlayers_layers',
      'default hook' => 'openlayers_layers',
      'api' => array(
        'owner' => 'openlayers',
        'api' => 'openlayers_layers',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
        'description' => 'Layer name.',
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
        'description' => 'Layer title.',
      ),
      'description' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Layer description.',
      ),
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Layer data serialized.',
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array(
      'name',
    ),
  );

  // Projection table (ctools extras)
  // TODO: Translations missing?
  $schema['openlayers_projections'] = array(
    'description' => 'Storage for user defined OpenLayers projections.',
    'export' => array(
      'key' => 'identifier',
      'key name' => 'Identifier',
      'primary key' => 'identifier',
      'identifier' => 'openlayers_projections',
      'default hook' => 'openlayers_projections',
      'api' => array(
        'owner' => 'openlayers',
        'api' => 'openlayers_projections',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'identifier' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Opaque identifier. Guaranteed to be unique but does not have any other meaning.',
      ),
      'authority' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Projection authority.',
      ),
      'code' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Projection code.',
      ),
      'definition' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Projection definition (proj4 format).',
      ),
      'projectedextentleft' => array(
        'type' => 'numeric',
        'not null' => TRUE,
        'description' => "Leftmost value in this projection's coordinates",
        'precision' => 10,
        'scale' => 0,
      ),
      'projectedextentbottom' => array(
        'type' => 'numeric',
        'not null' => TRUE,
        'description' => "Bottommost value in this projection's coordinates",
        'precision' => 10,
        'scale' => 0,
      ),
      'projectedextentright' => array(
        'type' => 'numeric',
        'not null' => TRUE,
        'description' => "Rightmost value in this projection's coordinates",
        'precision' => 10,
        'scale' => 0,
      ),
      'projectedextenttop' => array(
        'type' => 'numeric',
        'not null' => TRUE,
        'description' => "Topmost value in this projection's coordinates",
        'precision' => 10,
        'scale' => 0,
      ),
      // TODO Hopefully never used
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Projection data serialized.',
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array(
      'identifier',
    ),
  );

  // Styles table (ctools extras)
  $schema['openlayers_styles'] = array(
    'description' => 'Storage for user defined OpenLayers styles.',
    'export' => array(
      'key' => 'name',
      'key name' => 'Name',
      'primary key' => 'name',
      'identifier' => 'openlayers_styles',
      'default hook' => 'openlayers_styles',
      'api' => array(
        'owner' => 'openlayers',
        'api' => 'openlayers_styles',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
        'description' => 'Style name.',
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
        'description' => 'Style title.',
      ),
      'description' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Style description.',
      ),
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Style data serialized.',
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array(
      'name',
    ),
  );
  return $schema;
}

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

  // We should not require a specfic version of the OpenLayers library, but
  // we should tell the administrator that specific versions are more
  // compatible than others.
  if ($phase == 'runtime') {
    $ol_library_version = 0;
    $current_library = variable_get('openlayers_source', OPENLAYERS_DEFAULT_LIBRARY);

    // As of 2.11, the hosted version supports the newest hosted
    // stable with an actual version number.  This used to be just under
    // the 'api' path.
    if (strpos($current_library, (string) OPENLAYERS_SUGGESTED_LIBRARY) !== FALSE) {
      $ol_library_version = OPENLAYERS_SUGGESTED_LIBRARY;
    }

    // Check if it is the default hosted.
    if ($current_library == 'http://openlayers.org/api/OpenLayers.js') {
      $ol_library_version = OPENLAYERS_HOSTED_API_LIBRARY;
    }

    // Finally, let's see if the client has sent us a value
    // from the UI module AJAX magic.
    $client = variable_get('openlayers_ui_version_check', '');
    if (strpos($client, (string) OPENLAYERS_SUGGESTED_LIBRARY) !== FALSE) {
      $ol_library_version = OPENLAYERS_SUGGESTED_LIBRARY;
    }

    // Check if suggest version.
    if ($ol_library_version == OPENLAYERS_SUGGESTED_LIBRARY) {
      $req['openlayers_old_presets'] = array(
        'title' => $t('OpenLayers library version'),
        'value' => $t('Using suggested compatible version %suggested.', array(
          '%suggested' => OPENLAYERS_SUGGESTED_LIBRARY,
        )),
        'severity' => REQUIREMENT_OK,
      );
    }
    else {
      $req['openlayers_old_presets'] = array(
        'title' => $t('OpenLayers library version'),
        'value' => $t('Not suggested compatible version.'),
        'description' => $t('Though you are very welcome to use whatever version of the OpenLayers library you want, it is suggested that you use version %suggested.  You are currently not using that version or we are unable to determine which version you are using.  Update your library settings at !settings.', array(
          '%suggested' => OPENLAYERS_SUGGESTED_LIBRARY,
          '!settings' => l(t('OpenLayers settings'), 'admin/structure/openlayers'),
        )),
        'severity' => REQUIREMENT_WARNING,
      );
    }
  }

  // There are some backwards compability for the shift from
  // preset to maps.  We want to communicate to administrators
  // that this will not be there forever.
  if ($phase == 'runtime') {

    // Check preset hook
    $presets = module_invoke_all('openlayers_presets');
    $found_presets = count($presets) > 0 ? TRUE : FALSE;

    // Check features
    $feature_names = array();
    $found_features = FALSE;
    if (module_exists('features')) {
      $features = features_get_features();
      foreach ($features as $feature) {

        // Only utilize enabled features and look for presets
        if ($feature->status > 0 && !empty($feature->info['features']['openlayers_map_presets'])) {
          $feature_names[] = $feature->name;
          $found_features = TRUE;
        }
      }
    }

    // Create requirement entries
    if ($found_presets) {
      $req['openlayers_old_presets'] = array(
        'title' => $t('OpenLayers Presets'),
        'value' => $t('Found old presets.'),
        'description' => $t('With the 7.x-2.x version of the OpenLayers module, map presets were renamed to just maps.  This has some implications on APIs and hooks.  One or more of of the modules installed on this site is utilizing the deprecated %hook.  Please <a href="@url">read the upgrade page</a>, then contact the module owner or fix the custom module yourself.', array(
          '%hook' => 'hook_openlayers_presets()',
          '@url' => url('http://drupal.org/node/1136810'),
        )),
        'severity' => REQUIREMENT_ERROR,
      );
    }
    if ($found_features) {
      $req['openlayers_old_features'] = array(
        'title' => $t('OpenLayers Presets'),
        'value' => $t('Found Features with presets.'),
        'description' => $t('With the 7.x-2.x version of the OpenLayers module, map presets were renamed to just maps.  This has some implications on APIs and hooks.  There are Features on this site that contain the old map presets.  Please <a href="@url">read the upgrade page</a>, and rebuild the following Features: %features', array(
          '@url' => url('http://drupal.org/node/1136810'),
          '%features' => implode(',', $feature_names),
        )),
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  return $req;
}

/**
 * Implements hook_update_N().
 *
 * Rename of presets to maps.
 */
function openlayers_update_7201() {

  // Change table name
  db_rename_table('openlayers_map_presets', 'openlayers_maps');

  // Set default map variable
  variable_set('openlayers_default_map', variable_get('openlayers_default_preset', 'default'));
  variable_del('openlayers_default_preset');
}

/**
 * Drop name indexes duplicates of primary key.
 */
function openlayers_update_7202() {
  db_drop_index('openlayers_layers', 'name');
  db_drop_index('openlayers_styles', 'name');
}

/**
 * Rename the 'baselayer' in 'isBaseLayer' in layers.
 */
function openlayers_update_7204() {
  openlayers_update_create_projection_table();
  foreach (openlayers_layers_load() as $key => $layer) {
    if ($layer->export_type != 1) {
      continue;
    }
    if (isset($layer->data['baselayer'])) {
      $layer->data['isBaseLayer'] = $layer->data['baselayer'];
      unset($layer->data['baselayer']);
      ctools_export_crud_save('openlayers_layers', $layer);
    }
  }
}

/**
 * Change the case of machine_name for layers, maps and styles.
 * Warning, it can break things.
 */
function openlayers_update_7205() {
  db_query('UPDATE {openlayers_layers} SET name = LOWER(name);');
  db_query('UPDATE {openlayers_styles} SET name = LOWER(name);');
  db_query('UPDATE {openlayers_maps} SET name = LOWER(name);');
}

/**
 * Rename the 'base_url' in 'url' in layer type TMS.
 */
function openlayers_update_7206() {
  openlayers_update_create_projection_table();
  foreach (openlayers_layers_load() as $key => $layer) {
    if ($layer->export_type != 1) {
      continue;
    }
    if ($layer->data['layer_type'] != 'openlayers_layer_type_tms') {
      continue;
    }
    if (isset($layer->data['base_url'])) {
      $layer->data['url'] = $layer->data['base_url'];
      unset($layer->data['base_url']);
      ctools_export_crud_save('openlayers_layers', $layer);
    }
  }
}

/**
 * Makes projections available that are required for update hooks to work properly.
 * Must by called at least once by any of the update hooks.
 */
function openlayers_update_create_projection_table() {
  static $hasRun = FALSE;
  if ($hasRun || db_table_exists('openlayers_projections')) {
    return;
  }
  else {
    $hasRun = TRUE;
  }

  // Create projection table
  $openlayers_projections_schema = array(
    'description' => 'Storage for user defined OpenLayers projections.',
    'export' => array(
      'key' => 'identifier',
      'key name' => 'Identifier',
      'primary key' => 'identifier',
      'identifier' => 'openlayers_projections',
      'default hook' => 'openlayers_projections',
      'api' => array(
        'owner' => 'openlayers',
        'api' => 'openlayers_projections',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'identifier' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Opaque identifier. Guaranteed to be unique but does not have any other meaning.',
      ),
      'authority' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Projection authority.',
      ),
      'code' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Projection code.',
      ),
      'definition' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Projection definition (proj4 format).',
      ),
      'projectedextentleft' => array(
        'type' => 'numeric',
        'not null' => TRUE,
        'description' => "Leftmost value in this projection's coordinates",
      ),
      'projectedextentbottom' => array(
        'type' => 'numeric',
        'not null' => TRUE,
        'description' => "Bottommost value in this projection's coordinates",
      ),
      'projectedextentright' => array(
        'type' => 'numeric',
        'not null' => TRUE,
        'description' => "Rightmost value in this projection's coordinates",
      ),
      'projectedextenttop' => array(
        'type' => 'numeric',
        'not null' => TRUE,
        'description' => "Topmost value in this projection's coordinates",
      ),
      // TODO Hopefully never used
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Projection data serialized.',
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array(
      'identifier',
    ),
  );
  db_create_table('openlayers_projections', $openlayers_projections_schema);
}

/**
 * Migrate configured projections so that other projections than those of EPSG can be used, too.
 */
function openlayers_update_7207() {
  openlayers_update_create_projection_table();

  // Migrate stored layers
  foreach (openlayers_layers_load(TRUE) as $key => $layer) {
    $projections = array();

    // Takeover srs key that no longer exists and migrate it to a projection
    if ($layer instanceof openlayers_layer_type_wms && array_key_exists('srs', $layer->data['options']) && !in_array($layer->data['options']['srs'], $layer->data['projection'])) {
      $projections[] = $layer->data['options']['srs'];
    }
    unset($layer->data['options']['srs']);
    foreach ($layer->data['projection'] as $projection) {
      if (is_string($projection)) {
        $projections[] = $projection;
      }
      else {
        throw new DrupalUpdateException("Unsupported projection type encountered in layer " . $key);
      }
    }
    $layer->data['projection'] = $projections;
    ctools_export_crud_save('openlayers_layers', $layer);
  }

  // The code for the projection changed but its semantics stayed.
  // Only update the display projection as servers might be in use that don't yet support the new code.
  foreach (openlayers_maps(TRUE) as $key => $map) {
    if ($map->data['displayProjection'] === 'EPSG:900913') {
      $map->data['displayProjection'] = 'EPSG:3857';
    }
    ctools_export_crud_save('openlayers_maps', $map);
  }
}

/**
 * Ensure projections carry an authority code.
 */
function openlayers_update_7208() {

  // Commit 95108d3fa82c34fc5dc5e6e159263232a891a27a introduced authority codes
  // for layers and maps but did not migrate existing data.
  foreach (openlayers_layers_load(TRUE) as $key => $layer) {
    $projections = array();
    foreach ($layer->data['projection'] as $projection) {
      if (mb_strpos($projection, 'EPSG:') === FALSE) {
        $projections[] = 'EPSG:' . $projection;
      }
      else {
        $projections[] = $projection;
      }
    }
    $layer->data['projection'] = $projections;
    ctools_export_crud_save('openlayers_layers', $layer);
  }
  foreach (openlayers_maps(TRUE) as $key => $map) {
    foreach (array(
      'projection',
      'displayProjection',
    ) as $projectionKey) {
      $projection = $map->data[$projectionKey];
      if (mb_strpos($projection, 'EPSG:') === FALSE) {
        $projection = 'EPSG:' . $projection;
      }
      else {
        $projection = $projection;
      }
      $map->data[$projectionKey] = $projection;
    }
    ctools_export_crud_save('openlayers_maps', $map);
  }
}

/**
 * Drop maxExtent from layers where layers don't handle it. OpenLayers will
 * then take the property from the map for the affected layers.
 */
function openlayers_update_7209() {
  foreach (openlayers_layers_load(TRUE) as $key => $layer) {
    if ($layer instanceof openlayers_layer_type_google || $layer instanceof openlayers_layer_type_bing || $layer instanceof openlayers_layer_type_cloudmade || $layer instanceof openlayers_layer_type_geojson || $layer instanceof openlayers_layer_type_kml || $layer instanceof openlayers_layer_type_osm || $layer instanceof openlayers_layer_type_raw || $layer instanceof openlayers_layer_type_wms) {
      unset($layer->data['maxExtent']);
    }
    ctools_export_crud_save('openlayers_layers', $layer);
  }
}

Related topics

Functions

Namesort descending Description
openlayers_requirements Implements hook_requirements().
openlayers_schema Implements hook_schema().
openlayers_uninstall Implements hook_uninstall().
openlayers_update_7201 Implements hook_update_N().
openlayers_update_7202 Drop name indexes duplicates of primary key.
openlayers_update_7204 Rename the 'baselayer' in 'isBaseLayer' in layers.
openlayers_update_7205 Change the case of machine_name for layers, maps and styles. Warning, it can break things.
openlayers_update_7206 Rename the 'base_url' in 'url' in layer type TMS.
openlayers_update_7207 Migrate configured projections so that other projections than those of EPSG can be used, too.
openlayers_update_7208 Ensure projections carry an authority code.
openlayers_update_7209 Drop maxExtent from layers where layers don't handle it. OpenLayers will then take the property from the map for the affected layers.
openlayers_update_create_projection_table Makes projections available that are required for update hooks to work properly. Must by called at least once by any of the update hooks.