You are here

function gmap_plugin_style_gmap::render in GMap Module 6

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

Render the display in this style.

File

./gmap_plugin_style_gmap.inc, line 79
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();
  $lat_field = 'gmap_lat';
  $lon_field = 'gmap_lon';

  // Determine fieldname for latitude and longitude fields.
  if ($this->options['datasource'] == 'fields') {
    $lat_fied_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_fied_obj->field_alias;
    $lon_field = $lon_field_obj->field_alias;
  }
  $tooltip_field = '';
  if ($this->options['tooltipenabled']) {
    $tooltip_field_obj = $this->view->display_handler
      ->get_handler('field', $this->options['tooltipfield']);
    $tooltip_field = $tooltip_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 = $marker_field_obj->field_alias;
  }

  // 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());
  }
  else {
    if ($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']);

  // 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;

    // We search nid argument used to center map
    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();
    }
    if ($this->options['highlight_nodearg'] && ($nodehandler = $this->view->display_handler
      ->get_handler('argument', $this->options['highlight_nodearg_arg']))) {
      $highlight_nid = $nodehandler
        ->get_value();
    }
    foreach ($records as $row_index => $row) {
      $this->view->row_index = $row_index;
      $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];
          }
        }
        else {
          if ($this->options['markers'] == 'taxonomy') {
            if (!empty($row->gmap_node_marker)) {
              $markername = $row->gmap_node_marker;
            }
          }
          else {
            if ($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];
                }
              }
            }
            else {
              if ($this->options['markers'] == 'field') {
                if (!empty($row->{$marker_field})) {
                  $markername = $row->{$marker_field};
                }
              }
            }
          }
        }
        if (!isset($offsets[$markername])) {
          $offsets[$markername] = 0;
        }
        $tooltip = "";
        if ($this->options['tooltipenabled'] && !empty($tooltip_field) && !empty($row->{$tooltip_field})) {
          $tooltip = $row->{$tooltip_field};
        }
        $marker = array(
          'latitude' => $lat,
          'longitude' => $lon,
          'markername' => $markername,
          'offset' => $offsets[$markername],
          '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};
        }
        else {

          // Marker mode: popup.
          if ($defaults['markermode'] == 1) {
            $marker['text'] = $this->row_plugin
              ->render($row);
          }
          else {
            if ($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(), $this->view, $this->options, $map, $title);
    }
  }
  unset($this->view->row_index);
  return $output;
}