You are here

function openlayers_views_style_map::map_features in Openlayers 6

Map features

1 call to openlayers_views_style_map::map_features()
openlayers_views_style_map::render in modules/openlayers_views/views/openlayers_views_style_map.inc
Renders views (map)

File

modules/openlayers_views/views/openlayers_views_style_map.inc, line 334
This file holds style plugin for OpenLayers Views

Class

openlayers_views_style_map
@class Extension of the Views Plugin Syle for OpenLayers

Code

function map_features($records = array(), $group = NULL) {
  $features = array();
  $data_source = $this->options['data_source'];

  // Get list of fields in this view
  $handlers = $this->display->handler
    ->get_handlers('field');
  $fields = array();
  foreach ($handlers as $field_id => $handler) {
    $fields[$field_id] = $handler->definition;
    $fields[$field_id]['field_alias'] = $handler->field_alias;
  }

  // Build feature. We create one feature per field per row.
  foreach ($records as $id => $record) {
    $this->view->row_index = $id;
    $feature = array();

    // In order for any kind of substitution/replacement
    // token/convert to link/trim/etc. functionality to work, we
    // have to call advanced_render on each field in the record
    // in order and use its output to generate the attributes
    // for features.
    $rendered_record = array();
    foreach ($handlers as $hid => $handler) {

      // Render record fields to add to attributes
      $rendered_record[$handler->field_alias] = $handler
        ->advanced_render($record);
    }

    // Go through data sources
    // @@TODO: In theory, there could be multiple features per row., allow for multiple features per row
    $feature['wkt'] = array();
    foreach ($data_source['value'] as $data_source_value) {
      switch ($data_source_value) {
        case 'node_locations':

          // @@TODO: These fields are provided only if the fields are chosen in the interface.
          if (!empty($record->location_longitude) && !empty($record->location_latitude)) {
            $wkt_string = 'POINT(' . $record->location_longitude . ' ' . $record->location_latitude . ')';
            $feature['wkt'][] = $wkt_string;
            $feature['projection'] = '4326';
          }
          break;
        case 'geo':

          //@@TODO: Make it work on grouped multi-value fields geo. First http://drupal.org/node/446754 must be fixed.
          foreach ($data_source['geo_fields'] as $geo_field) {
            $geo_alias = $fields[$geo_field . '_geo']['field_alias'];
            $geo_record = $record->{$geo_alias};
            $wkt_object = geo_wkb_get_data($geo_record['wkb'], 'wkt');
            $wkt = $wkt_object['value'];
            if (!empty($wkt)) {
              $feature['wkt'][] = $wkt;
              $feature['projection'] = $geo_record['srid'];
            }
          }
          break;
        case 'other_latlon':
          $lat_field = $fields[$data_source['other_lat']]['field_alias'];
          $lon_field = $fields[$data_source['other_lon']]['field_alias'];
          $lon = $record->{$lon_field};
          $lat = $record->{$lat_field};
          if (!empty($lat) && !empty($lon)) {
            $feature['wkt'][] = 'POINT(' . $lon . ' ' . $lat . ')';
            $feature['projection'] = '4326';
          }
          break;
        case 'other_wkt':

          // Go through selected field
          foreach ($data_source['other_wkt'] as $wkt_field) {
            $wkt_alias = $fields[$wkt_field]['field_alias'];
            if ($wkt_alias == 'node_vid') {

              // It's a grouped multi-value.
              $wkt = array();
              foreach ($this->view->field[$wkt_field]->field_values[$record->{$wkt_alias}] as $wkt_item) {
                if ($wkt_item['openlayers_wkt']) {
                  $wkt[] = $wkt_item['openlayers_wkt'];
                }
              }
            }
            else {

              // It's a normal field item;
              $wkt = array(
                $record->{$wkt_alias},
              );
            }
            if (!empty($record->{$wkt_alias})) {
              $feature['wkt'] = array_merge($feature['wkt'], $wkt);

              // @@TODO: Allow different projections
              $feature['projection'] = '4326';
            }
          }
          break;
      }
    }

    // Fill in all attributes
    foreach ($fields as $fid => $field) {
      $field_alias = $field['field_alias'];
      $feature['attributes'][$field_alias] = $record->{$field_alias};
      $feature['attributes'][$field_alias . "_rendered"] = $rendered_record[$field_alias];
    }

    // Fill in tooltip attribute
    if (!empty($this->options['behaviors']['tooltip'])) {
      if ($this->options['behaviors']['tooltip'] == '#all_fields') {
        $feature['attributes']['openlayers_tooltip'] = '<div class="map-tooltip openlayers-map-tooltip"><div class="map-pop-up-inner">' . $this->row_plugin
          ->render($record) . '</div></div>';
      }
      else {
        $tooltip_field = $this->options['behaviors']['tooltip'];
        $tooltip_field_alias = $fields[$tooltip_field]['field_alias'];
        $feature['attributes']['openlayers_tooltip'] = '<div class="map-tooltip openlayers-map-tooltip"><div class="map-pop-up-inner">' . $rendered_record[$tooltip_field_alias] . '</div></div>';
      }
    }

    // Fill in popup attribute
    if (!empty($this->options['behaviors']['popup'])) {
      if ($this->options['behaviors']['popup'] == '#all_fields') {
        $feature['attributes']['openlayers_popup'] = '<div class="map-pop-up openlayers-map-pop-up"><div class="map-pop-up-inner">' . $this->row_plugin
          ->render($record) . '</div></div>';
      }
      else {
        $popup_field = $this->options['behaviors']['popup'];
        $popup_field_alias = $fields[$popup_field]['field_alias'];
        $feature['attributes']['openlayers_popup'] = '<div class="map-pop-up openlayers-map-pop-up"><div class="map-pop-up-inner">' . $rendered_record[$popup_field_alias] . '</div></div>';
      }
    }

    // Run feature styles through theme function
    $feature_style = theme('openlayers_views_feature_style', $this, $record, $group);
    if (is_array($feature_style) && !empty($feature_style)) {
      $feature['style'] = $feature_style;
    }

    // Run feature styles through custom php function
    if ($this->options['style']['snippet']) {
      $feature_style = eval($this->options['style']['snippet']);
      if (is_array($feature_style)) {
        $feature['style'] = $feature_style;
      }
    }

    // Only add features with WKT data
    if (!empty($feature['wkt'])) {
      $features[] = $feature;
    }
  }
  unset($this->view->row_index);
  return $features;
}