You are here

function gmap_plugin_style_gmap::render in GMap Module 7

Same name and namespace in other branches
  1. 6.2 gmap_plugin_style_gmap.inc \gmap_plugin_style_gmap::render()
  2. 6 gmap_plugin_style_gmap.inc \gmap_plugin_style_gmap::render()
  3. 7.2 gmap_plugin_style_gmap.inc \gmap_plugin_style_gmap::render()

Render the display in this style.

Overrides views_plugin_style::render

File

./gmap_plugin_style_gmap.inc, line 87
GMap style plugin.

Class

gmap_plugin_style_gmap
Style plugin to render a map.

Code

function render() {
  if (isset($this->view->live_preview) && $this->view->live_preview) {
    return t('GMap views are not compatible with live preview.');
  }
  if (empty($this->row_plugin)) {
    vpr('gmap_plugin_style_gmap: Missing row plugin');
    return;
  }
  $defaults = gmap_defaults();
  if ($this->options['datasource'] == 'location') {
    $lat_field = 'gmap_lat';
    $lon_field = 'gmap_lon';
  }
  else {
    if ($this->options['datasource'] == 'geofield') {
      $lat_field = 'lat';
      $lon_field = 'lon';
    }
    else {
      if ($this->options['datasource'] == 'fields') {
        $lat_field_obj = $this->view->display_handler
          ->get_handler('field', $this->options['latfield']);
        $lon_field_obj = $this->view->display_handler
          ->get_handler('field', $this->options['lonfield']);
        $lat_field = $lat_field_obj->field_alias;
        $lon_field = $lon_field_obj->field_alias;
      }
    }
  }

  // Determine fieldname for marker field.
  if ($this->options['markers'] == 'field') {
    $marker_field_obj = $this->view->display_handler
      ->get_handler('field', $this->options['markerfield']);
    $marker_field = 'field_' . $marker_field_obj->field;
  }

  // Determine rmt field.
  if ($this->options['enablermt']) {
    $rmt_field_obj = $this->view->display_handler
      ->get_handler('field', $this->options['rmtfield']);
    $rmt_field = $rmt_field_obj->field_alias;
  }
  $markername = isset($this->options['markertype']) ? $this->options['markertype'] : 'drupal';
  $markertypes = variable_get('gmap_node_markers', array());
  if ($this->options['markers'] == 'nodetype') {
    $markertypes = variable_get('gmap_node_markers', array());
  }
  elseif ($this->options['markers'] == 'userrole') {
    $markertypes = variable_get('gmap_role_markers', array(
      DRUPAL_AUTHENTICATED_RID => 'drupal',
    ));
  }

  // Group the rows according to the grouping field, if specified.
  $sets = $this
    ->render_grouping($this->view->result, $this->options['grouping']);

  // Let's make sure the individual fields are rendered
  $this
    ->render_fields($this->view->result);

  // Render each group separately and concatenate.  Plugins may override this
  // method if they wish some other way of handling grouping.
  $output = '';
  foreach ($sets as $title => $records) {
    $markers = array();
    $offsets = array();
    $center_lat = NULL;
    $center_lon = NULL;
    $center_nid = NULL;
    $highlight_nid = NULL;
    $autoclick_nid = NULL;

    // We search nid argument used to center map, autoclick or highlight.
    if ($this->options['center_on_nodearg'] && ($nodehandler = $this->view->display_handler
      ->get_handler('argument', $this->options['center_on_nodearg_arg']))) {
      $center_nid = $nodehandler
        ->get_value();
    }

    // Use the distance contextual filter argument to center the map
    if ($this->options['center_on_proximityarg'] && $this->options['datasource'] == 'location' && module_exists('location')) {

      // Just grab this straight from the view
      $distance_contextual_filter = $this->view->display_handler
        ->get_handler('argument', 'distance');
      $center_lat = $distance_contextual_filter->value['latitude'];
      $center_lon = $distance_contextual_filter->value['longitude'];
    }
    if ($this->options['highlight_nodearg'] && ($nodehandler = $this->view->display_handler
      ->get_handler('argument', $this->options['highlight_nodearg_arg']))) {
      $highlight_nid = $nodehandler
        ->get_value();
    }
    if ($this->options['autoclick_on_nodearg'] && ($nodehandler = $this->view->display_handler
      ->get_handler('argument', $this->options['autoclick_on_nodearg_arg']))) {
      $autoclick_nid = $nodehandler
        ->get_value();
    }
    foreach ($records as $row_index => $row) {
      $this->view->row_index = $row_index;
      if ($this->options['datasource'] == 'location') {
        $lat = (double) $row->{$lat_field};
        $lon = (double) $row->{$lon_field};
      }
      else {
        if ($this->options['datasource'] == 'geofield') {
          $geofield_name = 'field_' . $this->options['geofield'];
          $geofield = isset($row->{$geofield_name}[0]['raw']) ? $row->{$geofield_name}[0]['raw'] : NULL;
          if ($geofield) {
            $lat = (double) $geofield[$lat_field];
            $lon = (double) $geofield[$lon_field];
          }
        }
        else {
          if ($this->options['datasource'] == 'fields') {
            $custom_field_latname = 'field_' . $lat_field;
            $custom_field_lonname = 'field_' . $lon_field;
            $custom_field_lat = isset($row->{$custom_field_latname}[0]['raw']) ? $row->{$custom_field_latname}[0]['raw'] : NULL;
            $custom_field_lon = isset($row->{$custom_field_lonname}[0]['raw']) ? $row->{$custom_field_lonname}[0]['raw'] : NULL;
            if ($custom_field_lat && $custom_field_lon) {
              $lat = (double) $row->{$lat_field};
              $lon = (double) $row->{$lon_field};
            }
          }
        }
      }

      // $row->nid is present in node views, views without node as the base table must include the nid field,
      // which will be in $row->node_nid if present.
      // If nid for a row is required use $row_nid.
      $row_nid = isset($row->nid) ? $row->nid : (isset($row->node_nid) ? $row->node_nid : NULL);

      // If this row will be used as center map then we keep its lon/lat
      // If there are multiple points on a single node take the first match
      if (!empty($center_nid) && !empty($row_nid) && $center_nid == $row_nid && ($center_lon === NULL || $center_lat === NULL)) {
        $center_lon = $lon;
        $center_lat = $lat;
      }
      if (!empty($lat) && !empty($lon)) {
        if ($this->options['markers'] == 'nodetype') {
          if (isset($markertypes[$row->gmap_node_type])) {
            $markername = $markertypes[$row->gmap_node_type];
          }
        }
        elseif ($this->options['markers'] == 'taxonomy') {
          if (!empty($row->gmap_node_marker)) {
            $markername = $row->gmap_node_marker;
          }
        }
        elseif ($this->options['markers'] == 'userrole') {
          if (!empty($row->gmap_role_marker)) {
            $markername = $markertypes[DRUPAL_AUTHENTICATED_RID];
            if (isset($markertypes[$row->gmap_role_marker])) {
              $markername = $markertypes[$row->gmap_role_marker];
            }
          }
        }
        elseif ($this->options['markers'] == 'field') {
          if (!empty($row->{$marker_field})) {
            $markername = $row->{$marker_field}[0]['raw']['value'];
          }
        }
        if (!isset($offsets[$markername])) {
          $offsets[$markername] = 0;
        }
        $tooltip = '';
        if ($this->options['tooltipenabled'] && !empty($this->rendered_fields[$row_index][$this->options['tooltipfield']])) {
          $tooltip = $this->rendered_fields[$row_index][$this->options['tooltipfield']];
        }
        $bubbletext = NULL;
        if ($this->options['bubbletextenabled'] && !empty($bubbletext_field_alias) && !empty($row->{$bubbletext_field_alias})) {
          $bubbletext = $this->rendered_fields[$row_index][$this->options['bubbletextfield']];
        }
        elseif ($this->options['bubbletextenabled'] && $bubbletext_field_obj->real_field == 'nothing') {
          $bubbletext = $this->rendered_fields[$row_index][$this->options['bubbletextfield']];
        }
        $marker = array(
          'latitude' => $lat,
          'longitude' => $lon,
          'markername' => $markername,
          'offset' => $offsets[$markername],
          'text' => $bubbletext,
          'autoclick' => !empty($autoclick_nid) && !empty($row_nid) && $autoclick_nid == $row_nid ? 1 : 0,
          'opts' => array(
            'title' => $tooltip,
            'highlight' => !empty($highlight_nid) && !empty($row_nid) && $highlight_nid == $row_nid ? 1 : 0,
            'highlightcolor' => $this->options['highlight_nodearg_color'],
          ),
        );

        // RMT mode.
        if ($this->options['enablermt']) {
          $marker['rmt'] = $row->{$rmt_field};
        }
        elseif (!empty($defaults['markermode'])) {

          // Marker mode: popup.
          if ($defaults['markermode'] == 1) {
            $marker['text'] = $this->row_plugin
              ->render($row);
          }
          elseif ($defaults['markermode'] == 2) {
            $marker['link'] = url('node/' . $row_nid);
          }
        }
        $markers[] = $marker;
        $offsets[$markername]++;
      }
    }
    if (!empty($markers)) {

      // Don't draw empty maps.
      $map = gmap_parse_macro($this->options['macro']);
      if ($this->options['enablermt']) {
        $map['rmtcallback'] = $this->options['rmtcallback'];
      }

      // If center lon/lat are not empty they are used to center map
      if (!empty($center_lon) && !empty($center_lat)) {
        $map['longitude'] = $center_lon;
        $map['latitude'] = $center_lat;
      }
      $map['markers'] = $markers;
      $output .= theme($this
        ->theme_functions(), array(
        'view' => $this->view,
        'options' => $this->options,
        'rows' => $map,
        'title' => $title,
      ));
    }
  }
  unset($this->view->row_index);
  return $output;
}