<?php
define('OPENLAYERS_MAP_ID_PREFIX', 'openlayers-map-auto-id');
function openlayers_help($path, $arg) {
switch ($path) {
case 'admin/help#openlayers':
$output = '<p>'. t('The OpenLayers module is the base module for the OpenLayer suite of modules, and provides the main API.') .'</p>';
}
return $output;
}
function openlayers_perm() {
return array('administer openlayers');
}
function openlayers_theme() {
return array(
'openlayers_map' => array(
'arguments' => array(
'map' => array(),
),
),
);
}
function openlayers_menu() {
$items = array();
$items['admin/settings/openlayers'] = array(
'title' => 'OpenLayers',
'description' => t('OpenLayers administrative settings.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('openlayers_admin_settings'),
'access arguments' => array('administer openlayers'),
'file' => 'includes/openlayers.admin.inc',
);
$items['admin/settings/openlayers/settings'] = array(
'title' => 'Settings',
'description' => t('Main settings for OpenLayers.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('openlayers_admin_settings'),
'access arguments' => array('administer openlayers'),
'file' => 'includes/openlayers.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -20,
);
$items['admin/settings/openlayers/defaults'] = array(
'title' => 'Defaults',
'description' => t('Configure defaults for OpenLayers maps.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('openlayers_admin_default_settings'),
'access arguments' => array('administer openlayers'),
'file' => 'includes/openlayers.defaults.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
return $items;
}
function theme_openlayers_map($map = array()) {
$output = '
<div id="'. $map['id'] .'" class="openlayers-map"></div>
';
return $output;
}
function openlayers_intialize() {
$success = TRUE;
static $included = FALSE;
if ($included == FALSE) {
$path = check_plain(variable_get('openlayers_source', 'http://openlayers.org/dev/OpenLayers.js'));
if (valid_url($path, TRUE)) {
drupal_set_html_head('<script src="'. check_url($path) .'" type="text/javascript"></script>');
}
else {
drupal_add_js($path);
}
drupal_add_css(drupal_get_path('module', 'openlayers') .'/openlayers.css', 'module');
drupal_add_js(drupal_get_path('module', 'openlayers') .'/js/openlayers.js', 'module');
$included = TRUE;
}
return $success;
}
function openlayers_render_map($map = array(), $render = TRUE) {
if (!is_array($map)) {
return FALSE;
}
if (openlayers_intialize() == FALSE) {
return FALSE;
}
if (!$map['id']) {
$map['id'] = _openlayers_create_map_id();
}
if (!$map['only_these_layers']) {
$saved_defaults = variable_get('openlayers_defaults', array());
$map = openlayers_merge_maps($saved_defaults, $map);
}
$system_defaults = _openlayers_get_map_defaults();
$map = openlayers_merge_maps($system_defaults, $map);
$map['layers'] = _openlayers_layers_process($map['layers'], $map);
$map['behaviors'] = _openlayers_behaviors_process($map['behaviors'], $map);
drupal_alter('openlayers_map', &$map);
$map['errors'] = openlayers_error_check_map($map);
if (!$map['errors'] || $render == FALSE) {
$openlayers = array(
'openlayers' => array(
'maps' => array(
$map['id'] => $map,
),
),
);
drupal_add_js($openlayers, 'setting');
$map['themed'] = theme('openlayers_map', $map);
}
return $map;
}
function openlayers_layers_get_info($reset = FALSE) {
static $info = array();
if (count($info) == 0 || $reset == TRUE) {
$info = module_invoke_all('openlayers_layers_info');
}
return $info;
}
function openlayers_behaviors_get_info($reset = FALSE) {
static $info = array();
if (count($info) == 0 || $reset == TRUE) {
$info = module_invoke_all('openlayers_behaviors_info');
}
return $info;
}
function openlayers_map_form($defaults = array()) {
module_load_include('inc', 'openlayers', '/includes/openlayers.form');
return _openlayers_map_form($defaults);
}
function openlayers_map_form_validate($map_form = array()) {
module_load_include('inc', 'openlayers', '/includes/openlayers.form');
return _openlayers_map_form_validate($map_form);
}
function openlayers_convert_form_to_map($values = array()) {
module_load_include('inc', 'openlayers', '/includes/openlayers.form');
return _openlayers_convert_form_to_map($values);
}
function openlayers_convert_map_to_form($map = array()) {
module_load_include('inc', 'openlayers', '/includes/openlayers.form');
return _openlayers_convert_map_to_form($map);
}
function openlayers_merge_maps($map1 = array(), $map2 = array()) {
if (is_array($map1) && is_array($map2)) {
foreach ($map2 as $k => $v) {
if (isset($map1[$k]) && is_array($v) && is_array($map1[$k])) {
$map1[$k] = openlayers_merge_maps($map1[$k], $v);
}
else {
$map1[$k] = $v;
}
}
}
return $map1;
}
function openlayers_error_check_map($map, $log_errors = TRUE) {
$errors = array();
foreach ($map['layers'] as $layer) {
if ($layer['projection']) {
if (!in_array($map['projection'], $layer['projection'])) {
$errors[] = t('The layer %layer_name cannot work with the map projection: EPSG: %map_proj', array(
'%layer_name' => $layer['name'],
'%map_proj' => $map['projection'],
));
}
}
}
if ($map['projection'] == '900913' || $map['projection'] == '3785') {
if (!$map['options']['maxExtent'] || !$map['options']['maxResolution']) {
$errors[] = t("You are using a web spherical mercador projection. However maxExtent or maxResolution are not set. ");
}
}
if ($map['projection'] == '4326' || $map['projection'] == '4269') {
if (
($map['options']['maxExtent']['top'] && $map['options']['maxExtent']['top'] > 90) ||
($map['options']['maxExtent']['bottom'] && $map['options']['maxExtent']['bottom'] < -90) ||
($map['options']['maxExtent']['left'] && $map['options']['maxExtent']['left'] < -180) ||
($map['options']['maxExtent']['right'] && $map['options']['maxExtent']['right'] > 180) ||
($map['options']['maxResoluton'] && $map['options']['maxResoluton'] > 180)
) {
$errors[] = t("Your Maximum Extents are set greater than 180/90 degrees. Try Maximum Extent of: -180,180,-90,90 and a Maximum Resolution of 1.40625");
}
}
if (count($errors) > 0 && $log_errors){
watchdog('openlayers', implode(', ', $errors), array(), WATCHDOG_ERROR);
}
return (count($errors) > 0) ? $errors : FALSE;
}
function openlayers_openlayers_layers_handler_info($map = array()) {
return array(
'WMS' => array(
'layer_handler' => 'WMS',
'js_file' => drupal_get_path('module', 'openlayers') .'/js/openlayers.layers.js',
),
'Vector' => array(
'layer_handler' => 'Vector',
'js_file' => drupal_get_path('module', 'openlayers') .'/js/openlayers.layers.js',
),
);
}
function openlayers_openlayers_layers_info() {
$info['openlayers_default_wms'] = array(
'name' => t('Default OpenLayers WMS'),
'description' => t('A simple basemap to get you started'),
'file' => drupal_get_path('module', 'openlayers') .'/includes/openlayers.layers.inc',
'callback' => 'openlayers_process_layers',
'projection' => array('4326', '900913', '4269'),
'baselayer' => TRUE,
);
return $info;
}
function _openlayers_layers_process($layers = array(), $map = array()) {
if (!$layers){
$layers = array();
}
$processed = array();
$layer_info = openlayers_layers_get_info();
if (!array_key_exists($map['default_layer'], $layers) && ($map['default_layer'])) {
$layers[$map['default_layer']] = $map['default_layer'];
}
foreach ($layers as $k => $layer) {
if (is_array($layer)) {
$processed[$k] = $layer;
}
else {
if (($info = $layer_info[$layer]) && is_array($layer_info[$layer])) {
if (is_file('./'. $info['file'])) {
require_once './'. $info['file'];
if (function_exists($info['callback'])) {
$result = call_user_func_array($info['callback'], $layer);
if (isset($result) && is_array($result)) {
$processed[$layer] = $result;
}
}
}
}
}
}
$handlers = module_invoke_all('openlayers_layers_handler_info', $map);
foreach ($processed as $k => $l) {
if (is_string($handlers[$l['type']]['layer_handler'])) {
$processed[$k]['layer_handler'] = $handlers[$l['type']]['layer_handler'];
if (is_string($handlers[$l['type']]['js_file'])) {
drupal_add_js($handlers[$l['type']]['js_file'], 'module');
}
}
}
return $processed;
}
function _openlayers_behaviors_process($behaviors = array(), &$map) {
if (!is_array($behaviors)) {
$behaviors = array();
}
if (!is_array($map)) {
$map = array();
}
$processed = array();
$behavior_info = openlayers_behaviors_get_info();
foreach ($behaviors as $k => $behavior) {
if (is_array($behaviors)) {
$processed[$behavior['id']] = $behavior;
}
else {
$processed[$behavior] = array(
'id' => $behavior,
'type' => $behavior,
);
}
}
foreach ($processed as $bkey => $behavior) {
$info = $behavior_info[$behavior['type']];
if (is_file($info['file'])) {
require_once $info['file'];
if (function_exists($info['callback'])) {
$result = call_user_func_array($info['callback'], array($behavior, &$map));
if (isset($result) && is_array($result)) {
$result['js_callback'] = $info['js_callback'];
$processed[$bkey] = $result;
}
}
if ($info['js_file']){
drupal_add_js($info['js_file'], 'module');
}
}
}
return $processed;
}
function _openlayers_create_map_id() {
$map_id = '';
static $map_count = 0;
$map_id = OPENLAYERS_MAP_ID_PREFIX .'-'. $map_count;
$map_count += 1;
return $map_id;
}
function _openlayers_get_map_defaults() {
$map_default = array(
'id' => _openlayers_create_map_id(),
'projection' => '4326',
'width' => 'auto',
'default_layer' => 'openlayers_default_wms',
'only_these_layers' => FALSE,
'height' => '300px',
'center' => array(
'lat' => '0',
'lon' => '0',
'zoom' => '2',
),
'options' => array(
'displayProjection' => '4326',
),
'controls' => array(
'LayerSwitcher' => TRUE,
'Navigation' => TRUE,
'PanZoomBar' => TRUE,
'MousePosition' => TRUE,
),
);
return $map_default;
}