View source
<?php
function openlayers_ui_help($path, $arg) {
switch ($path) {
case 'admin/help#openlayers_ui':
return '<p>' . t('Provides a user interface to manage OpenLayers maps.') . '</p>';
case 'admin/build/openlayers':
return '<p>' . t('Configure where Drupal finds
the OpenLayers javascript library. Using a local or <a href="@olb">compacted
version</a> of OpenLayers can reduce filesize, but remember to set the Image Path and
CSS Path options in every preset, because OpenLayers is not able to find these resources
automatically. See the included documentation in /doc for
more details.', array(
'@olb' => 'http://trac.openlayers.org/wiki/Profiles',
)) . '</p>';
case 'admin/build/openlayers/styles':
return '<p>' . t('OpenLayer styles are stored versions of
<a href="@ols">javascript StyleMap objects</a>, but are much easier to
configure through an interface. Styles will typically define the icon,
fill color, border, and radius of points or polygons, so they are used
on overlays more often than base layers, which are not affected by vector
styles.', array(
"@ols" => 'http://trac.openlayers.org/wiki/Styles',
)) . '</p>';
case 'admin/build/openlayers/styles/add':
return '<p>' . t('The name, title, and description you give a style is for Drupal\'s
interal storage. The rest of the properties are documented on openlayers.org') . '</p>';
case 'admin/build/openlayers/presets':
return '<p>' . t('Presets are combinations of the layers, styles, and behaviors
which constitute maps. They also contain basic configuration like the
width and centerpoint of the map - everything except for a mechanism
to put a map on a page. For that, use the Views OpenLayers Map style.') . '</p>';
case 'admin/build/openlayers/layers':
return '<p>' . t('Layers are stored versions of javascript OpenLayers layer
objects. They point at a data source, like OpenStreetMap, Google Maps,
or Drupal itself, and sometimes define the zoom levels for which they apply.') . '</p>';
case 'admin/build/openlayers/layers/settings':
return '<p>' . t('Commercial layer providers like Google will require API keys
to ensure that users are\'t abusing the service and to enforce limits. Not all
of these keys are required, of course, only those that are used on a site.') . '</p>';
}
}
function openlayers_ui_perm() {
return array(
'administer openlayers',
);
}
function openlayers_ui_menu() {
$items = array();
$items['admin/build/openlayers'] = array(
'title' => 'OpenLayers',
'description' => 'Manage maps, layers, styles, and map behaviors.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_admin_settings',
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/build/openlayers/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -20,
);
$items['admin/build/openlayers/layers'] = array(
'title' => 'Layers',
'page callback' => 'openlayers_ui_object_list',
'page arguments' => array(
3,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.layers.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/build/openlayers/layers/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -20,
);
$items['admin/build/openlayers/layers/add'] = array(
'title' => 'Add',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_layers_types',
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.layers.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/build/openlayers/layers/add/%openlayers_layer_type'] = array(
'title' => 'Add',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_layers_form',
5,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.layers.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -15,
);
$items['admin/build/openlayers/layers/clone/%openlayers_layer'] = array(
'title' => 'Clone layer',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_layers_form',
5,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.layers.inc',
'type' => MENU_NORMAL_ITEM,
'tab_parent' => 'admin/build/openlayers/layers',
);
$items['admin/build/openlayers/layers/import'] = array(
'title' => 'Import',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_layers_import_form',
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.layers.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/build/openlayers/layers/%openlayers_layer/edit'] = array(
'title' => 'Edit layer',
'description' => 'Edit OpenLayers layer.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_layers_form',
4,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.layers.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/build/openlayers/layers/%openlayers_layer_export/delete'] = array(
'title' => 'Delete layer',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_object_delete',
3,
4,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.layers.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/build/openlayers/layers/%openlayers_layer_export/export'] = array(
'title' => 'Export layer',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_object_export',
3,
4,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.layers.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/build/openlayers/layers/settings'] = array(
'title' => 'API Keys',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_layers_settings',
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.layers.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/build/openlayers/styles'] = array(
'title' => 'Styles',
'page callback' => 'openlayers_ui_object_list',
'page arguments' => array(
3,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.styles.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/build/openlayers/styles/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -20,
);
$items['admin/build/openlayers/styles/add'] = array(
'title' => 'Add',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_styles_form',
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.styles.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/build/openlayers/styles/add/%openlayers_style'] = array(
'title' => 'Add',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_styles_form',
5,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.styles.inc',
'type' => MENU_NORMAL_ITEM,
'weight' => -10,
);
$items['admin/build/openlayers/styles/clone/%openlayers_style'] = array(
'title' => 'Clone style',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_styles_form',
5,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.styles.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/build/openlayers/styles/%openlayers_style/edit'] = array(
'title' => 'Edit style',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_styles_form',
4,
TRUE,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.styles.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/build/openlayers/styles/%openlayers_style/delete'] = array(
'title' => 'Delete style',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_object_delete',
3,
4,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.styles.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/build/openlayers/styles/%openlayers_style/export'] = array(
'title' => 'Export style',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_object_export',
3,
4,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.styles.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/build/openlayers/presets'] = array(
'title' => 'Presets',
'description' => 'Create OpenLayers map presets.',
'page callback' => 'openlayers_ui_object_list',
'page arguments' => array(
3,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.presets.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/build/openlayers/presets/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -20,
);
$items['admin/build/openlayers/presets/add'] = array(
'title' => 'Add',
'description' => 'Add OpenLayers map preset.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_presets_form',
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.presets.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/build/openlayers/presets/add/%openlayers_preset'] = array(
'title' => 'Clone Preset',
'description' => 'Add OpenLayers map preset.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_presets_form',
5,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.presets.inc',
'type' => MENU_NORMAL_ITEM,
'weight' => -10,
);
$items['admin/build/openlayers/presets/clone/%openlayers_preset'] = array(
'title' => 'Clone preset',
'description' => 'Clone OpenLayers map preset.',
'page callback' => 'openlayers_ui_presets_clone',
'page arguments' => array(
5,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.presets.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/build/openlayers/presets/%openlayers_preset/edit'] = array(
'title' => 'Edit preset',
'description' => 'Edit OpenLayers map preset.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_presets_form',
4,
TRUE,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.presets.inc',
'type' => MENU_LOCAL_TASK,
'tab_parent' => 'admin/build/openlayers/presets',
);
$items['admin/build/openlayers/presets/%openlayers_preset/delete'] = array(
'title' => 'Delete preset',
'description' => 'Delete OpenLayers map preset.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_object_delete',
3,
4,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.presets.inc',
'type' => MENU_LOCAL_TASK,
'tab_parent' => 'admin/build/openlayers/presets',
);
$items['admin/build/openlayers/presets/%openlayers_preset/export'] = array(
'title' => 'Export preset',
'description' => 'Export OpenLayers map preset.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'openlayers_ui_object_export',
3,
4,
),
'access arguments' => array(
'administer openlayers',
),
'file' => 'includes/openlayers_ui.presets.inc',
'type' => MENU_LOCAL_TASK,
'tab_parent' => 'admin/build/openlayers/presets',
);
$items['openlayers/ahah/preset'] = array(
'title' => 'OpenLayers AHAH',
'page callback' => 'openlayers_ui_preset_ahah',
'access callback' => TRUE,
'file' => 'includes/openlayers_ui.presets.inc',
'type' => MENU_CALLBACK,
);
$items['openlayers/ahah/style_plugin/%'] = array(
'title' => 'OpenLayers Style Plugin AHAH',
'page callback' => 'openlayers_ui_style_plugin_ahah',
'page arguments' => array(
3,
),
'access callback' => TRUE,
'file' => 'includes/openlayers_ui.styles.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function openlayers_ui_theme($existing, $type, $theme, $path) {
return array(
'openlayers_ui_form_layer_description' => array(
'arguments' => array(
'layer_title' => '',
'layer_description' => '',
),
'file' => 'includes/openlayers_ui.theme.inc',
),
'openlayers_ui_form_projection_description' => array(
'arguments' => array(
'projection_title' => '',
'available_layers' => array(),
'layers' => array(),
),
'file' => 'includes/openlayers_ui.theme.inc',
),
'openlayers_ui_presets_form' => array(
'arguments' => array(
'form' => array(),
),
'file' => 'includes/openlayers_ui.theme.inc',
),
'openlayers_ui_presets_form_layers' => array(
'arguments' => array(
'form' => array(),
),
'file' => 'includes/openlayers_ui.theme.inc',
),
'openlayers_ui_presets_form_behaviors' => array(
'arguments' => array(
'form' => array(),
),
'file' => 'includes/openlayers_ui.theme.inc',
),
);
}
function openlayers_ui_object_list($type) {
$rows = array();
switch ($type) {
case 'styles':
$objects = openlayers_styles(TRUE);
break;
case 'layers':
$objects = openlayers_layers_load(TRUE);
break;
case 'presets':
$objects = openlayers_presets(TRUE);
break;
}
if ($type == 'layers') {
$header = array(
t('Title'),
t('Type'),
t('Description'),
t('Actions'),
);
$layer_types = openlayers_layer_types();
}
elseif ($type == 'presets') {
$header = array(
t('Title'),
t('Layers'),
t('Description'),
t('Actions'),
);
$layers = openlayers_layers_load();
}
else {
$header = array(
t('Title'),
t('Description'),
t('Actions'),
);
}
foreach ($objects as $object) {
$row = array();
$links = array();
switch ($object->export_type) {
case EXPORT_IN_DATABASE | EXPORT_IN_CODE:
$row[] = filter_xss_admin($object->title) . ' <em><small>(' . t('Overridden') . ')</small></em>';
break;
case EXPORT_IN_DATABASE:
$row[] = filter_xss_admin($object->title) . ' <em><small>(' . t('Normal') . ')</small></em>';
break;
case EXPORT_IN_CODE:
$row[] = filter_xss_admin($object->title) . ' <em><small>(' . t('Default') . ')</small></em>';
break;
}
if ($type == 'layers') {
$row[] = filter_xss_admin($layer_types[$object->data['layer_type']]['title']);
}
elseif ($type == 'presets') {
if (!empty($object->data['layers'])) {
$layer_names = array();
foreach ($object->data['layers'] as $layer) {
$layer_names[] = empty($layers[$layer]->title) ? $layer : filter_xss_admin($layers[$layer]->title);
}
$row[] = '<small>' . implode(', ', $layer_names) . '</small>';
}
else {
$row[] = '';
}
}
$row[] = filter_xss_admin($object->description);
if ($object->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) {
$links[] = l(t('Edit'), "admin/build/openlayers/{$type}/{$object->name}/edit");
$links[] = l(t('Revert'), "admin/build/openlayers/{$type}/{$object->name}/delete");
}
elseif ($object->export_type == EXPORT_IN_DATABASE) {
$links[] = l(t('Edit'), "admin/build/openlayers/{$type}/{$object->name}/edit");
$links[] = l(t('Delete'), "admin/build/openlayers/{$type}/{$object->name}/delete");
}
$links[] = l(t('Export'), "admin/build/openlayers/{$type}/{$object->name}/export");
$links[] = l(t('Clone'), "admin/build/openlayers/{$type}/clone/{$object->name}");
$row[] = implode(' | ', $links);
$rows[] = $row;
}
$output = theme('table', $header, $rows);
return $output;
}
function openlayers_ui_object_delete(&$form_state, $type, $object) {
if ($object->export_type & EXPORT_IN_DATABASE) {
$form = array(
'type' => array(
'#type' => 'value',
'#value' => $type,
),
'object' => array(
'#type' => 'value',
'#value' => $object,
),
);
$message = $object->export_type & EXPORT_IN_CODE ? t('Are you sure you want to revert %object?', array(
'%object' => $object->title,
)) : t('Are you sure you want to delete %object?', array(
'%object' => $object->title,
));
return confirm_form($form, $message, 'admin/build/openlayers/' . $type);
}
}
function openlayers_ui_object_delete_submit($form, &$form_state) {
$type = $form_state['values']['type'];
$object = $form_state['values']['object'];
switch ($type) {
case 'styles':
$result = openlayers_style_delete($object);
break;
case 'layers':
$result = openlayers_layer_delete($object);
break;
case 'presets':
$result = openlayers_preset_delete($object);
break;
}
if ($result) {
drupal_set_message(t('%name was deleted.', array(
'%name' => $object->name,
)));
}
else {
drupal_set_message(t('Error when attempting to delete %name.', array(
'%name' => $object->name,
)), 'error');
}
$form_state['redirect'] = 'admin/build/openlayers/' . $type;
}
function openlayers_ui_object_export(&$form_state, $type, $object) {
switch ($type) {
case 'styles':
$api = 'openlayers_styles';
break;
case 'layers':
$api = 'openlayers_layers';
break;
case 'presets':
$api = 'openlayers_map_presets';
break;
}
ctools_include('export');
$schema = ctools_export_get_schema($api);
$identifier = $schema['export']['identifier'];
$code = '$items = array();' . "\n";
$code .= ctools_export_object($api, $object, '');
$code .= '$items["' . $object->name . '"] = $' . $identifier . ';' . "\n";
$code .= 'return $items;';
$rows = substr_count($code, "\n") + 1;
$form = array();
$form['export'] = array(
'#type' => 'textarea',
'#default_value' => $code,
'#rows' => $rows,
'#resizable' => FALSE,
);
$form['done'] = array(
'#type' => 'submit',
'#value' => t('Done'),
);
$form['#redirect'] = 'admin/build/openlayers/' . $type;
return $form;
}
function openlayers_ui_import_page(&$form_state) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Preset name'),
'#description' => t('Enter the name to use for this preset if it is different from the source preset. Leave blank to use the name of the preset.'),
);
$form['view'] = array(
'#type' => 'textarea',
'#title' => t('Paste preset code here'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Import'),
'#submit' => array(
'openlayers_ui_import_submit',
),
'#validate' => array(
'openlayers_ui_import_validate',
),
);
return $form;
}
function openlayers_ui_import_validate($form, &$form_state) {
$view = '';
ob_start();
eval($form_state['values']['preset']);
ob_end_clean();
if (!is_object($preset)) {
return form_error($form['view'], t('Unable to interpret preset code.'));
}
if (!empty($form_state['values']['name']) && preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
form_error($form['name'], t('Preset name must be alphanumeric or underscores only.'));
}
if ($form_state['values']['name']) {
$preset->name = $form_state['values']['name'];
}
$test = openlayers_preset_load($preset->name);
if ($test && $test->type != t('Default')) {
form_set_error('', t('A preset by that name already exists; please choose a different name'));
}
$form_state['preset'] =& $preset;
}
function openlayers_ui_get_projection_options() {
$options = array();
foreach (openlayers_layers_load() as $key => $layer) {
if ($layer->data['projection']) {
foreach ($layer->data['projection'] as $projection) {
$options[$projection] = $projection;
}
}
}
return $options;
}
function openlayers_ui_get_layer_options($type = 'baselayer', $projection = NULL) {
$options = array();
$layers = array();
foreach (openlayers_layers_load() as $l) {
$layers[$l->name] = openlayers_get_layer_object($l);
}
foreach ($layers as $key => $layer) {
if (!isset($projection) || in_array($projection, $layer->data['projection']) || !empty($layer->data['vector'])) {
if ($type == 'baselayer' && $layer->data['baselayer']) {
$options[$key] = theme('openlayers_ui_form_layer_description', $layer->title, $layer->description);
}
elseif ($type != 'baselayer' && empty($layer->data['baselayer'])) {
$options[$key] = theme('openlayers_ui_form_layer_description', $layer->title, $layer->description);
}
}
}
return $options;
}
function openlayers_ui_get_style_options() {
$options = array();
foreach (openlayers_styles() as $key => $style) {
$options[$key] = $style->title;
}
return $options;
}
function openlayers_dependency_widget($dependencies) {
if (is_array($dependencies) > 0) {
$dependency_form = array();
foreach ($dependencies as $dependency) {
$dependency_form[] = array(
'#type' => 'markup',
'#value' => "<div class='openlayers-dependency-flag'>\n <span class='openlayers-dependency-value'>{$dependency}</span>" . "<span class='openlayers-dependency-broken'>" . t('Dependency not found:') . ' ' . $dependency . "</span>" . "</div>",
);
}
return $dependency_form;
}
}