You are here

function openlayers_presets_ui_presets_manage_add in Openlayers 6

Menu Callback for Add Preset

2 string references to 'openlayers_presets_ui_presets_manage_add'
openlayers_presets_ui_menu in modules/openlayers_presets_ui/openlayers_presets_ui.module
Implementation of hook_menu().
openlayers_presets_ui_presets_manage in modules/openlayers_presets_ui/includes/openlayers_presets_ui.ui.inc
Menu Callback for Preset Routing

File

modules/openlayers_presets_ui/includes/openlayers_presets_ui.ui.inc, line 90
This file holds the functions for the openlayers presets ui

Code

function openlayers_presets_ui_presets_manage_add(&$form_state, $preset = array(), $edit = FALSE) {
  $form = array();
  $layers = openlayers_layers_get_info();
  $baselayers = array();
  $overlays = array();
  $available_controls = _openlayers_presets_ui_available_controls();
  $styles = openlayers_styles_get_info();
  $available_styles = array();

  // Ensure we are not editing any coded presets
  if ($preset['preset_coded'] && $edit) {
    drupal_set_message(t('Cannot edit default preset.'), 'error');
    drupal_goto('admin/settings/openlayers/presets');
  }

  // If cloning or editing, attempt to get preset
  if (empty($preset)) {
    $default_map = openlayers_get_default_map();

    // Unset preset name
    unset($default_map['preset_name']);
  }
  else {
    $default_map = $preset['preset_data'];

    // Push the preset data into the map
    $default_map['preset_title'] = $preset['preset_title'];
    $default_map['preset_description'] = $preset['preset_description'];
  }

  // Update map for new form
  if (!empty($form_state['values'])) {
    $form_map = _openlayers_presets_ui_convert_form_to_map($form_state['values']);
    $default_map = openlayers_merge_maps($default_map, $form_map);
  }

  // Change map to form values
  $defaults = _openlayers_presets_ui_convert_map_to_form($default_map);

  // Merge with form values
  $defaults = is_array($form_state['values']) ? $form_state['values'] + $defaults : $defaults;
  $rendered_map = is_array($default_map) ? openlayers_render_map($default_map) : array();

  // Get layer options
  $layer_options = array(
    'overlays' => array(),
    'baselayers' => array(),
  );

  // Determine which layer is a baselayer, and which is an overlay, and add
  // approrpriate descriptions to the options array.
  foreach ($layers as $layer_key => $layer) {
    $description = theme('openlayers_presets_ui_form_layer_description', $layer['name'], $layer['description']);
    if ($layer['baselayer']) {
      $layer_options['baselayers'][$layer_key] = $description;
    }
    else {
      $layer_options['overlays'][$layer_key] = $description;
    }
  }

  // Define list of projections
  $sorted_projections = _openlayers_presets_ui_get_projections($layers);
  $projections = array();
  foreach ($sorted_projections as $projection => $available_layers) {
    $projections[$projection] = theme('openlayers_presets_ui_form_projection_description', $projection, $available_layers, $layers);
  }

  // Set the "Other" option for our easy projection selector
  $projections['other'] = t("Other");

  // Unset All
  unset($projections['all']);

  // Default projection
  $default_proj = $defaults['projections']['projection'];

  // Check for other, which will show all layers available
  if (!$default_proj || $defaults['projections']['easy_projection'] == 'other') {
    $base_options = $layer_options['baselayers'];
    $overlay_options = $layer_options['overlays'];
  }
  else {

    // Create specific layer options for given projection
    foreach ($sorted_projections[$default_proj] as $p => $l) {
      if ($layer_options['baselayers'][$l]) {
        $base_options[$l] = $layer_options['baselayers'][$l];
      }
      if ($layer_options['overlays'][$l]) {
        $overlay_options[$l] = $layer_options['overlays'][$l];
      }
    }
  }

  // Options
  if ($defaults['options']['automatic_options']) {
    $defaults['options']['displayProjection'] = '4326';

    // Check project
    if ($default_proj == '900913' || $default_proj == '3785') {
      $defaults['options']['maxResolution'] = '156543.0339';
      $defaults['options']['maxExtent']['left'] = '-20037508.34';
      $defaults['options']['maxExtent']['right'] = '20037508.34';
      $defaults['options']['maxExtent']['bottom'] = '-20037508.34';
      $defaults['options']['maxExtent']['top'] = '20037508.34';
    }
    else {
      $defaults['options']['maxResolution'] = '';
      $defaults['options']['maxExtent']['left'] = '';
      $defaults['options']['maxExtent']['right'] = '';
      $defaults['options']['maxExtent']['bottom'] = '';
      $defaults['options']['maxExtent']['top'] = '';
    }
  }

  // Make style options array
  foreach ($styles as $k_style => $style) {
    $available_styles[$k_style] = $style['name'];
  }

  // Centering map
  $centering_map = _openlayers_presets_ui_form_center_map($defaults);

  // Form Properties
  $form['#tree'] = TRUE;
  $form['#cache'] = TRUE;

  // Pass editing along
  $form['preset_edit'] = array(
    '#type' => 'value',
    '#value' => $edit,
  );

  // Pass along map data, since it can hold stuff
  // that the UI cannot account for
  $form['default_map'] = array(
    '#type' => 'value',
    '#value' => $default_map,
  );

  // Preview of map
  $form['map_preview'] = array(
    '#type' => 'item',
    '#title' => t('Example Map'),
    '#description' => t('This is a preview of current map before saving.'),
    '#value' => $rendered_map['themed'],
  );

  // Preset properties
  $form['preset_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Preset Name'),
    '#description' => t('This is the machine readable identifier.  This should be all lowercase characters, numbers, or underscores (_).'),
    '#maxlength' => 255,
    '#default_value' => $defaults['preset_name'],
    '#disabled' => $edit,
  );

  // If edit, set value
  if ($edit) {
    $form['preset_name']['#value'] = $defaults['preset_name'];
  }
  $form['preset_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Preset Title'),
    '#description' => t('This is the descriptive title of the preset and will show up most often in the interface.'),
    '#maxlength' => 255,
    '#default_value' => $defaults['preset_title'],
  );
  $form['preset_description'] = array(
    '#type' => 'textarea',
    '#title' => t('Preset Description'),
    '#description' => t('This is full description of the preset and is mostly used on the preset overview list page.'),
    '#rows' => 2,
    '#default_value' => $defaults['preset_description'],
  );

  // Map general properties
  $form['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Width'),
    '#description' => t('Please use a CSS Width value.'),
    '#default_value' => $defaults['width'],
    '#maxlength' => 128,
  );
  $form['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Height'),
    '#description' => t('Please use a CSS Height value.'),
    '#default_value' => $defaults['height'],
    '#maxlength' => 128,
  );
  $form['image_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Image Path'),
    '#description' => t('The path to a directory for OpenLayers to look for UI graphics. If blank, default graphics are used.'),
    '#default_value' => $defaults['image_path'],
  );

  // Map center properties
  $form['center'] = array(
    '#type' => 'fieldset',
    '#title' => t('Center'),
    '#description' => t('Where the map will center itself initially. You may use the small map to help you set your center and zoom.  Pan and zoom where you want the centering defaults to be.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['center']['helpmap'] = array(
    '#value' => '<div class="form-item openlayers-center-helpmap" style="display:block">' . $centering_map['themed'] . '</div>',
  );
  $form['center']['lat'] = array(
    '#type' => 'textfield',
    '#title' => t('Latitude'),
    '#description' => t('Latitude or X value for centering.'),
    '#default_value' => $defaults['center']['lat'],
    '#attributes' => array(
      'class' => 'openlayers-form-lat',
    ),
    '#size' => 25,
  );
  $form['center']['lon'] = array(
    '#type' => 'textfield',
    '#title' => t('Longitude'),
    '#description' => t('Longitude or Y value for centering.'),
    '#default_value' => $defaults['center']['lon'],
    '#attributes' => array(
      'class' => 'openlayers-form-lon',
    ),
    '#size' => 25,
  );
  $form['center']['zoom'] = array(
    '#type' => 'textfield',
    '#title' => t('Zoom Level'),
    '#description' => t('Zoom level for centering.'),
    '#default_value' => $defaults['center']['zoom'],
    '#attributes' => array(
      'class' => 'openlayers-form-zoom',
    ),
    '#size' => 25,
  );

  // Map controls
  $form['controls'] = array(
    '#type' => 'fieldset',
    '#title' => t('Controls'),
    '#description' => t('Controls on the map that allow the user to interface with the map.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  // Go through avilable controls
  foreach ($available_controls as $c_key => $control) {
    $form['controls'][$c_key] = array(
      '#type' => 'checkbox',
      '#title' => $control['title'],
      '#description' => $control['description'],
      '#default_value' => $defaults['controls'][$c_key],
    );
  }

  // Vector Styles
  $form['styles'] = array(
    '#type' => 'fieldset',
    '#title' => t('Vector Styles'),
    '#description' => t('Styles used on features in vector layers.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['styles']['default'] = array(
    '#type' => 'select',
    '#title' => t('Default Style'),
    '#description' => t('Default style for features in a vector.'),
    '#options' => $available_styles,
    '#default_value' => $defaults['styles']['default'],
  );
  $form['styles']['select'] = array(
    '#type' => 'select',
    '#title' => t('Select Style'),
    '#description' => t('Default style for features in a vector that are selected.'),
    '#options' => $available_styles,
    '#default_value' => $defaults['styles']['select'],
  );
  $form['styles']['temporary'] = array(
    '#type' => 'select',
    '#title' => t('Temporary Style'),
    '#description' => t('Default style for any temporary features in a vector.  This will also be used for rollovers for things like Tooltips.'),
    '#options' => $available_styles,
    '#default_value' => $defaults['styles']['temporary'],
  );

  // Projections
  $form['projections'] = array(
    '#type' => 'fieldset',
    '#title' => t('Projection'),
    '#description' => t('Select projection for map.  This will affect which layers are available to choose from.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  // This will create an easy way to select projections and have layers change accordingly
  $form['projections']['easy_projection'] = array(
    '#type' => 'radios',
    '#title' => t('Projection'),
    '#description' => t('Select the EPSG code of the !link_proj for your map. The list next to each projection is the layers that support this projection.', array(
      '!link_proj' => l(t('geographical projection'), 'http://en.wikipedia.org/wiki/Map_projection'),
    )),
    '#default_value' => $defaults['projections']['easy_projection'],
    '#options' => $projections,
    '#attributes' => array(
      'class' => 'openlayers-form-easy-projection',
    ),
  );
  $form['projections']['projection'] = array(
    '#type' => 'textfield',
    '#title' => t('Projection'),
    '#description' => t('The EPSG code of the geographical projection for your map.'),
    '#attributes' => array(
      'class' => 'openlayers-form-projection',
    ),
    '#default_value' => $defaults['projections']['projection'],
    '#maxlength' => 16,
    '#ahah' => array(
      'path' => 'openlayers/ahah/preset',
      'wrapper' => 'openlayers-layers-select',
      'event' => 'change',
    ),
  );

  // Start AHAH Wrapper
  $form['openlayers-ahah-wrapper-start'] = array(
    '#value' => '<div id="openlayers-layers-select">',
  );

  // Layers
  $form['layers'] = array(
    '#type' => 'fieldset',
    '#title' => t('Layers'),
    '#description' => t('Layer settings.  The Layer options will change based on the projection chosen above.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['layers']['baselayers'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Base Layers'),
    '#description' => t('Select the base layers to make available for your map. This list is determined by the projection you select for your map.'),
    '#options' => $base_options,
    '#attributes' => array(
      'class' => 'openlayers-form-baselayers',
    ),
    '#multiple' => TRUE,
    '#default_value' => $defaults['layers']['baselayers'] ? $defaults['layers']['baselayers'] : array(),
  );
  $form['layers']['default_layer'] = array(
    '#type' => 'radios',
    '#title' => t('Default Base Layer'),
    '#description' => t('The default base layer to use when rendering maps.  This will be included whether it is in the base layers or not.'),
    '#options' => $base_options,
    '#attributes' => array(
      'class' => 'openlayers-form-default-layer',
    ),
    '#default_value' => $defaults['layers']['default_layer'],
  );
  $form['layers']['overlays'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Overlay Layers'),
    '#description' => t('Select the overlay layers to make available for your map.'),
    '#options' => $overlay_options,
    '#attributes' => array(
      'class' => 'openlayers-form-overlays',
    ),
    '#multiple' => TRUE,
    '#default_value' => $defaults['layers']['overlays'] ? $defaults['layers']['overlays'] : array(),
  );

  // Map options properties
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options'),
    '#description' => t('Set additional options'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['options']['automatic_options'] = array(
    '#type' => 'checkbox',
    '#title' => t('Automatic Options'),
    '#description' => t('If this is checked, options will be automatically filled for you.'),
    '#default_value' => isset($defaults['options']['automatic_options']) ? $defaults['options']['automatic_options'] : TRUE,
    '#maxlength' => 6,
  );
  $form['options']['displayProjection'] = array(
    '#type' => 'textfield',
    '#title' => t('Display Projection'),
    '#description' => t('This is the projection that is presented to your users as the interface for viewing / editing location data. You will most likely want use 4326 for lat/lon coordinates.'),
    '#default_value' => $defaults['options']['displayProjection'],
    '#maxlength' => 6,
  );
  $form['options']['maxResolution'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum Resolution'),
    '#description' => t('The maximum number of pixels / map unit at the highest zoom.'),
    '#default_value' => $defaults['options']['maxResolution'],
    '#attributes' => array(
      'class' => 'openlayers-form-maxResolution',
    ),
  );
  $form['options']['maxExtent'] = array(
    '#type' => 'fieldset',
    '#title' => t('Maximum Extent'),
    '#description' => t('Set the bounds for the edges of your map. Set them according to the units of your projection.'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['options']['maxExtent']['left'] = array(
    '#type' => 'textfield',
    '#title' => t('Left'),
    '#default_value' => $defaults['options']['maxExtent']['left'],
    '#attributes' => array(
      'class' => 'openlayers-form-maxExtent-left',
    ),
  );
  $form['options']['maxExtent']['right'] = array(
    '#type' => 'textfield',
    '#title' => t('Right'),
    '#default_value' => $defaults['options']['maxExtent']['right'],
    '#attributes' => array(
      'class' => 'openlayers-form-maxExtent-right',
    ),
  );
  $form['options']['maxExtent']['bottom'] = array(
    '#type' => 'textfield',
    '#title' => t('Bottom'),
    '#default_value' => $defaults['options']['maxExtent']['bottom'],
    '#attributes' => array(
      'class' => 'openlayers-form-maxExtent-bottom',
    ),
  );
  $form['options']['maxExtent']['top'] = array(
    '#type' => 'textfield',
    '#title' => t('Top'),
    '#default_value' => $defaults['options']['maxExtent']['top'],
    '#attributes' => array(
      'class' => 'openlayers-form-maxExtent-top',
    ),
  );

  // End AHAH Wrapper
  $form['openlayers-ahah-wrapper-end'] = array(
    '#value' => '</div>',
  );

  // Add a submit button that is the submit handler for the
  // AHAH change event on projections
  $form['openlayers_projection_ahah'] = array(
    '#type' => 'submit',
    '#value' => t('Update Projections'),
    '#submit' => array(
      'openlayers_presets_manage_add_projection_submit',
    ),
  );

  // Save button
  $form['openlayers_save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  // Cancel button
  $form['openlayers_cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
  );

  // Add JavaScript
  drupal_add_js(drupal_get_path('module', 'openlayers_presets_ui') . '/js/openlayers_presets_ui.ui.js', 'module');

  // Add CSS
  drupal_add_css(drupal_get_path('module', 'openlayers_presets_ui') . '/openlayers_presets_ui.css');
  return $form;
}