You are here

function ip_geoloc_plugin_style_bulk_of_form in IP Geolocation Views & Maps 7

Same name and namespace in other branches
  1. 8 src/Plugin/views/style/ip_geoloc_plugin_style.inc \ip_geoloc_plugin_style_bulk_of_form()

The bulk of the plugin style form.

3 calls to ip_geoloc_plugin_style_bulk_of_form()
ip_geoloc_plugin_style_leaflet::options_form in views/ip_geoloc_plugin_style_leaflet.inc
Implements options_form().
ip_geoloc_plugin_style_map::options_form in views/ip_geoloc_plugin_style_map.inc
Create the options form.
ip_geoloc_plugin_style_openlayers::options_form in views/ip_geoloc_plugin_style_openlayers.inc
Create the options form.

File

views/ip_geoloc_plugin_style.inc, line 27
ip_geoloc_plugin_style.inc

Code

function ip_geoloc_plugin_style_bulk_of_form($views_plugin_style, &$weight, &$form, &$form_state) {

  // We only ever want to offer at most 1 grouping field.
  if (!empty($form['grouping']) && count($form['grouping']) > 1) {
    $form['grouping'] = array_slice($form['grouping'], 0, 1);
  }
  $fields = ip_geoloc_get_display_fields($views_plugin_style->display->handler, TRUE);
  $field_options = $fields;
  $type_field_name = t('Type a field name below');
  $field_options['---'] = '[' . $type_field_name . ']';
  $selected_options = $views_plugin_style->options['ip_geoloc_views_plugin_latitude'];
  if (!is_array($selected_options)) {
    $selected_options = array(
      $selected_options,
    );
  }
  $default_text = '';
  if (!empty($views_plugin_style->options['ip_geoloc_views_plugin_latitude_text'])) {
    $default_text = $views_plugin_style->options['ip_geoloc_views_plugin_latitude_text'];
    $selected_options['---'] = '---';
  }
  $form['ip_geoloc_views_plugin_latitude'] = array(
    '#title' => t('Name of latitude field in Views query'),
    '#type' => 'select',
    '#multiple' => TRUE,
    '#options' => $field_options,
    '#default_value' => $selected_options,
    '#weight' => $weight++,
  );
  $form['ip_geoloc_views_plugin_latitude_text'] = array(
    '#type' => 'textfield',
    '#default_value' => $default_text,
    '#states' => array(),
    '#description' => t("If you have multiple fields to enter, separate these by comma's."),
    '#weight' => $weight++,
  );
  $explanation1 = t("Select the field(s) holding latitude from the select-box above. Make sure you have added it to the Fields list of this View or it won't show up above. Fields from the <strong>Geofield, Get Locations</strong> and <strong>Geolocation</strong> modules can all be selected this way.");
  $explanation2 = t('Your latitude may reside in a field with a special name. This is the case when using the <strong>Location</strong> module or when you have used <strong>relationships</strong> to pull in lat & lon. To input special names, select <em>[Type a field name below]</em> and enter the field\'s machine name. This <a target="_field_names" href="!url">list of all field names</a> may help in figuring out the correct name.', array(
    '!url' => url('admin/reports/fields'),
  ));
  $explanation3 = t('If you use a view based on the <strong>Location</strong> module, the special name is <strong>location_latitude</strong>.');
  $form['ip_geoloc_views_plugin_latitude_desc'] = array(
    '#type' => 'item',
    '#markup' => $explanation1 . '<br/><strong>' . t('Special names') . ': </strong>' . "{$explanation2}<br/>{$explanation3}",
    '#weight' => $weight++,
  );
  $default_text = '';
  $selected_option = $views_plugin_style->options['ip_geoloc_views_plugin_longitude'];
  if (!empty($views_plugin_style->options['ip_geoloc_views_plugin_longitude_text'])) {
    $default_text = $views_plugin_style->options['ip_geoloc_views_plugin_longitude_text'];
    $selected_option = '---';
  }
  $form['ip_geoloc_views_plugin_longitude'] = array(
    '#title' => t('Name of longitude field in Views query'),
    '#type' => 'select',
    '#options' => $field_options,
    '#default_value' => $selected_option,
    '#weight' => $weight++,
  );
  $form['ip_geoloc_views_plugin_longitude_text'] = array(
    '#type' => 'textfield',
    '#default_value' => $default_text,
    '#states' => array(
      'visible' => array(
        ':input[name="style_options[ip_geoloc_views_plugin_longitude]"]' => array(
          'value' => '---',
        ),
      ),
    ),
    '#weight' => $weight++,
  );
  $form['ip_geoloc_views_plugin_longitude_desc'] = array(
    '#type' => 'item',
    '#markup' => t('Select <em>&lt;none&gt;</em>, except when latitude and longitude are separate fields, as in the <strong>Location</strong> module. In these cases, select <em>[Type a field name below]</em> and enter the field name. For the Location module, enter <strong>location_longitude</strong>.'),
    '#weight' => $weight++,
  );
  $differentiator = isset($form_state['differentiator']) ? $form_state['differentiator'] : $views_plugin_style->options['differentiator']['differentiator_field'];
  if (isset($form_state['triggering_element'])) {

    // Get here when any form element with #ajax was changed/clicked causing
    // an auto-rebuild of the form. Can't put this in an ajax handler, as these
    // are called AFTER the form rebuild, so too late.
    if (strpos($form_state['triggering_element']['#id'], 'differentiator-differentiator-field') > 0) {

      // Get here when it was the differentiator drop-down that was changed.
      $differentiator = $form_state['triggering_element']['#value'];
      unset($form_state['num_associations']);
    }
  }
  $form_state['differentiator'] = $differentiator;

  // Or AJAX won't work!
  $form_state['no_cache'] = FALSE;
  $is_openlayers = $form_state['renderer'] == 'openlayers';
  $is_google = $form_state['renderer'] == 'google';
  if ($is_openlayers || $is_google) {
    $form['default_marker_color'] = array(
      '#title' => t('Default location marker'),
      '#type' => 'select',
      '#default_value' => $views_plugin_style->options['default_marker_color'],
      '#options' => $is_openlayers ? ip_geoloc_openlayers_marker_layers() : ip_geoloc_marker_colors(),
      '#description' => t('Select an image to use for all location markers whose images are not overridden by the <strong>location differentiator</strong> below.'),
      '#attributes' => array(
        'class' => $is_openlayers ? array(
          'marker-color-ol',
        ) : array(
          'marker-color',
        ),
      ),
      '#weight' => $weight++,
    );
  }
  if (!$is_openlayers) {
    $path = drupal_get_path('module', 'ip_geoloc');
    $css_file = strpos(ip_geoloc_marker_directory(), 'amarkers') ? 'ip_geoloc_admin_select_amarkers.css' : 'ip_geoloc_admin_select_markers.css';
    $form['default_marker_color']['#attached']['css'] = array(
      "{$path}/css/{$css_file}",
    );
  }
  $descr = t('You may designate <em>any</em> field from your view as a <strong>location differentiator</strong>. Locations that match the same corresponding differentiator value will have the same marker image on the map. Examples of location differentiators are content type and taxonomy term. You can enter numeric and alphabetic <em>ranges</em> too, e.g. price ranges, like 100--125.');

  // Add wrapper for differentiator drop-down, association table and buttons.
  // The id in the prefix must match the AJAX submit handlers below.
  $form['differentiator'] = array(
    '#type' => 'fieldset',
    '#title' => t('Location differentiator and associated markers'),
    '#description' => $descr,
    '#prefix' => '<div id="differentiator-wrapper">',
    '#suffix' => '</div>',
    '#weight' => $weight++,
  );
  $instances = ip_geoloc_get_field_instances($differentiator);
  $instance = reset($instances);
  if (empty($differentiator)) {
    $description = t('Optionally select a location differentiator.');
  }
  else {
    $description = t('Below associate %differentiator values with marker images.', array(
      '%differentiator' => empty($instance) ? $fields[$differentiator] : $instance['label'],
    )) . '<br/>';
    $field = field_info_field($differentiator);
    if (!$field || $field['module'] == 'text') {
      $description .= t('You may enter a range of values by separating "from" and "to" by a <strong>double hyphen</strong>.<br/>Example: <strong>A--ZZ</strong><br/>You may omit "from" or "to" to create open-ended ranges.');
    }
    elseif ($field['module'] == 'number') {
      $description .= t('You may enter a numeric range of by separating "from" and "to" by a <strong>double hyphen</strong>.<br/>Example: <strong>2.5--7.95</strong><br/>You may omit "from" or "to" to create open-ended ranges.');
    }
    $description .= '<br/>' . t('You can refer to View URL arguments via !1, !2 etc.') . '<br/>' . t('To use components of complex field types like Date or AddressField use replacement tokens in the <em>Global: Custom text</em> field.');
    $description .= '<br/>' . t('Font icon character and class columns apply to Leaflet only.');
  }
  $form['differentiator']['differentiator_field'] = array(
    '#title' => t('Location differentiator'),
    '#type' => 'select',
    '#default_value' => $differentiator,
    '#options' => $fields,
    '#description' => $description,
    '#ajax' => array(
      'callback' => '_ip_geoloc_plugin_style_refresh_color_table_js',
      'wrapper' => 'differentiator-wrapper',
    ),
  );
  if (!empty($differentiator)) {

    // Container for the differentiator color associations table.
    $form['differentiator']['color_table'] = array(
      '#theme' => 'ip_geoloc_plugin_style_differentiator_color_table',
    );
    _ip_geoloc_plugin_style_differentiator_color_table_form($form, $form_state);
    $form['differentiator']['add-another'] = array(
      '#type' => 'submit',
      '#value' => empty($form_state['num_associations']) ? t('Add an association') : t('Add another association'),
      '#weight' => 1,
      '#submit' => array(
        '_ip_geoloc_plugin_style_add_association_submit',
      ),
      '#ajax' => array(
        'callback' => '_ip_geoloc_plugin_style_refresh_color_table_js',
        'wrapper' => 'differentiator-wrapper',
        // Or 'none' or 'slide'.
        'effect' => 'fade',
        // Or 'slow' or number of millisec.
        'speed' => 'fast',
      ),
    );
    if (!empty($form_state['num_associations'])) {
      $form['differentiator']['remove'] = array(
        '#type' => 'submit',
        '#value' => t('Remove bottom association'),
        '#weight' => 2,
        '#submit' => array(
          '_ip_geoloc_plugin_style_remove_association_submit',
        ),
        '#ajax' => array(
          'callback' => '_ip_geoloc_plugin_style_refresh_color_table_js',
          'wrapper' => 'differentiator-wrapper',
          // Or 'fade' or 'slide'.
          'effect' => 'none',
          // Or 'slow' or number of millisec.
          'speed' => 'fast',
        ),
      );
    }
  }
  $desc = '';
  if ($is_openlayers) {
    $desc = t('*) If you want a fixed center, visit the "Center & Bounds" vertical tab on the <a target="_ol_map_edit" href="@url">map edit page</a>. This is also where you set the initial zoom level.', array(
      '@url' => url('admin/structure/openlayers/maps/' . $views_plugin_style->options['map'] . '/edit'),
    ));
  }
  elseif ($is_google) {
    $desc = t('*) If you choose the first option you may center the map via the special <strong>Map options</strong> <em>"centerLat"</em> and <em>"centerLng"</em>.<br/>Example: %center_example', array(
      '%center_example' => '{ "mapTypeId":"terrain", "centerLat":-37.8, "centerLng":145 }',
    ));
  }
  $form['center_option'] = array(
    '#title' => t('Map centering options'),
    '#type' => 'radios',
    '#default_value' => $views_plugin_style->options['center_option'],
    '#options' => array(
      IP_GEOLOC_MAP_CENTER_FIXED => t('Fixed center; see note below *)'),
      IP_GEOLOC_MAP_CENTER_ON_FIRST_LOCATION => t('Use the first location returned by the view as the center of the map.'),
      IP_GEOLOC_MAP_CENTER_ON_VISITOR => t("Center the map on the visitor's current location, at the specified zoom level."),
      IP_GEOLOC_MAP_CENTER_OF_LOCATIONS => t('Use the center of the rectangle whose sides are defined by the left-most, right-most, top and bottom locations (this option is insensitive to location clusters).'),
      IP_GEOLOC_MAP_CENTER_OF_LOCATIONS_WEIGHTED => t('Use the center of gravity based on all locations, except the visitor location (this option is sensitive to location clusters)'),
    ),
    '#description' => $desc,
    '#weight' => $weight++,
  );
  $form['empty_map_center'] = array(
    '#title' => t('No locations behaviour'),
    '#type' => 'textfield',
    '#size' => 30,
    '#default_value' => $views_plugin_style->options['empty_map_center'],
    '#description' => t('a) Show empty map: enter <em>latitude, longitude</em> of the map center.<br/>b) Show empty map centered on visitor location: enter the word %visitor.<br/>c) Show nothing: if this field is left blank, the map canvas will be suppressed when there are no locations to show. If you wish, you may use the <strong>No results behavior</strong> panel in the Advanced section of the Views UI to show an alternative text or content.', array(
      '%visitor' => t('visitor'),
    )),
    '#weight' => $weight++,
  );
  return $form;
}