You are here

function openlayers_views_style_data::map_features in Openlayers 6.2

Same name and namespace in other branches
  1. 7.2 modules/openlayers_views/views/openlayers_views_style_data.inc \openlayers_views_style_data::map_features()

Map features

1 call to openlayers_views_style_data::map_features()
openlayers_views_style_data::render in modules/openlayers_views/views/openlayers_views_style_data.inc
Render the map features.

File

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

Class

openlayers_views_style_data
@class Extension of the Views Plugin Syle for OpenLayers

Code

function map_features($records = array()) {
  $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;
  }

  // Small fix to solve backwards compatability and bad commit
  // and release.  This should be in an update function.
  // See: http://drupal.org/node/895082
  $ds = $data_source['value'];
  if (is_array($ds)) {
    $ds = array_shift(array_keys($data_source['value']));
  }

  // Build feature. We create one feature per field per row.
  // @TODO: Redo the grouped-version of this style plugin.
  // Separate mapping for grouped views.  Currently only adds name and count.
  if (!empty($this->options['grouping'])) {
    $this->view->row_index = 0;
    foreach ($records as $id => $record) {
      $feature = array();
      $feature['attributes']['name'] = $id;
      switch ($ds) {
        case 'other_latlon':
          $lat_field = $fields[$data_source['other_lat']]['field_alias'];
          $lon_field = $fields[$data_source['other_lon']]['field_alias'];
          $lon = current($record)->{$lon_field};
          $lat = current($record)->{$lat_field};
          if (isset($lat) && isset($lon)) {
            $feature['wkt'] = 'POINT(' . $lon . ' ' . $lat . ')';
            $feature['projection'] = '4326';
          }
          break;
        case 'openlayers_wkt':
          $wkt_field = $fields[$data_source['openlayers_wkt']]['field_alias'];
          $wkt = current($record)->{$wkt_field};
          if ($wkt) {
            $feature['wkt'] = $wkt;
            $feature['projection'] = '4326';
          }
          break;
        case 'other_boundingbox':
          $top_field = $fields[$data_source['other_top']]['field_alias'];
          $right_field = $fields[$data_source['other_right']]['field_alias'];
          $left_field = $fields[$data_source['other_left']]['field_alias'];
          $bottom_field = $fields[$data_source['other_bottom']]['field_alias'];
          $top = current($record)->{$top_field};
          $right = current($record)->{$right_field};
          $left = current($record)->{$left_field};
          $bottom = current($record)->{$bottom_field};
          if (isset($lat) && isset($lon)) {
            $feature['wkt'] = 'MULTIPOLYGON(' . $left . ' ' . $top . ', ' . $right . ' ' . $top . ', ' . $right . ' ' . $bottom . ', ' . $left . ' ' . $bottom . ')';
            $feature['projection'] = '4326';
          }
          break;
      }
      $descriptions = array();
      $excluded_fields = array();
      foreach ($record as $k => $v) {
        foreach ($handlers as $hid => $handler) {
          $rendered_record[$handler->field_alias] = $handler
            ->advanced_render($v);
          if (!empty($handler->options['exclude'])) {
            $excluded_fields[] = $handler->field_alias;
          }
        }

        // Get aliases name
        $name_field = !empty($data_source['name_field']) ? $fields[$data_source['name_field']]['field_alias'] : NULL;
        $description_field = !empty($data_source['description_field']) ? $fields[$data_source['description_field']]['field_alias'] : NULL;

        // Create output
        $descriptions[] = theme('openlayers_views_group_display_item', $rendered_record[$name_field], $rendered_record[$description_field]);
      }
      $grouped_description = theme('item_list', $descriptions);
      $feature['attributes']['description'] = $grouped_description;
      $feature['attributes']['count'] = count($record);
      foreach ($fields as $fid => $field) {
        $field_alias = $field['field_alias'];
        if (!in_array($field_alias, $excluded_fields, TRUE)) {

          // Use regular field name as this is easier to understand
          $feature['attributes'][$fid . "_rendered"] = $rendered_record[$field_alias];
        }
      }

      // Only add features with WKT data
      if (!empty($feature['wkt'])) {
        $features[] = theme('openlayers_views_render_feature', $feature, $record);
      }
      $this->view->row_index++;
    }
    unset($this->view->row_index);
    return $features;
  }
  else {
    $excluded_fields = array();
    $records = array_shift($records);
    foreach ($records as $id => $record) {
      $feature = array();
      $rendered_record = array();

      // Note that excluded fields are still rendered in case they are used
      // as render tokens in later fields.
      foreach ($handlers as $hid => $handler) {
        $rendered_record[$handler->field_alias] = $handler
          ->advanced_render($record);
        if (!empty($handler->options['exclude'])) {
          $excluded_fields[] = $handler->field_alias;
        }
      }
      switch ($ds) {
        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 (isset($lat) && isset($lon)) {
            $feature['wkt'] = 'POINT(' . $lon . ' ' . $lat . ')';
            $feature['projection'] = '4326';
          }
          $excluded_fields[] = $lat_field;
          $excluded_fields[] = $lon_field;
          break;
        case 'openlayers_wkt':
          $wkt_field = $fields[$data_source['openlayers_wkt']]['field_alias'];
          $wkt = $record->{$wkt_field};
          if ($wkt) {
            $feature['wkt'] = $wkt;
            $feature['projection'] = '4326';
          }
          $excluded_fields[] = $wkt_field;
          break;
        case 'other_boundingbox':
          $top_field = $fields[$data_source['other_top']]['field_alias'];
          $right_field = $fields[$data_source['other_right']]['field_alias'];
          $left_field = $fields[$data_source['other_left']]['field_alias'];
          $bottom_field = $fields[$data_source['other_bottom']]['field_alias'];
          $top = $record->{$top_field};
          $right = $record->{$right_field};
          $left = $record->{$left_field};
          $bottom = $record->{$bottom_field};
          if (isset($top) && isset($left)) {
            $feature['wkt'] = 'MULTIPOLYGON(' . $left . ' ' . $top . ', ' . $right . ' ' . $top . ', ' . $right . ' ' . $bottom . ', ' . $left . ' ' . $bottom . ')';
            $feature['projection'] = '4326';
          }
          array_push($excluded_fields, $top_field, $right_field, $left_field, $bottom_field);
      }

      // Add the name and description attributes
      // as choosen through interface
      $name_field = NULL;
      $description_field = NULL;
      if (is_array($data_source) && $data_source['name_field']) {
        $name_field = $fields[$data_source['name_field']]['field_alias'];
        $feature['attributes']['name'] = $rendered_record[$name_field];
        $excluded_fields[] = $name_field;
        if (!empty($data_source['keep_orig_name_field'])) {
          $feature['attributes'][$data_source['name_field']] = $record->{$name_field};
        }
      }
      else {
        $feature['attributes']['name'] = '';
      }
      if (is_array($data_source) && $data_source['description_field']) {
        $description_field = $fields[$data_source['description_field']]['field_alias'];
        $feature['attributes']['description'] = $rendered_record[$description_field];
        $excluded_fields[] = $description_field;
        if (!empty($data_source['keep_orig_desc_field'])) {
          $feature['attributes'][$data_source['description_field']] = $record->{$description_field};
        }
      }
      else {
        $feature['attributes']['description'] = '';
      }

      // Fill in attributes that are not:
      // - Coordinate/bounding box fields
      // - Name/description (already processed)
      // - Views "excluded" fields
      foreach ($fields as $fid => $field) {
        $field_alias = $field['field_alias'];
        if (!in_array($field_alias, $excluded_fields, TRUE)) {

          // Use regular field name as this is easier to understand
          $feature['attributes'][$fid] = $record->{$field_alias};
          $feature['attributes'][$fid . "_rendered"] = $rendered_record[$field_alias];
        }
      }

      // Only add features with WKT data
      if (!empty($feature['wkt'])) {
        $features[] = theme('openlayers_views_render_feature', $feature, $record);
      }
    }
    return $features;
  }
}