You are here

public function gmap_plugin_style_gmap::render in GMap Module 7.2

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 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 95
GMap style plugin.

Class

gmap_plugin_style_gmap

Code

public 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') {

      // "[0]['raw']['lat']";
      $lat_field = 'field_' . $this->options['geofield'];

      // "[0]['raw']['lon']";
      $lon_field = 'field_' . $this->options['geofield'];
    }
    else {
      if ($this->options['datasource'] == 'geolocation') {

        // "[0]['raw']['lat']";
        $lat_field = 'field_' . $this->options['geolocation'];

        // "[0]['raw']['lng']";
        $lon_field = 'field_' . $this->options['geolocation'];
      }
      else {
        if ($this->options['datasource'] == 'fields') {

          // "[0]['raw']['value']";
          $lat_field = 'field_' . $this->options['latfield'];

          // "[0]['raw']['value']";
          $lon_field = 'field_' . $this->options['lonfield'];
        }
      }
    }
  }

  // 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_info['type'] == 'gmap_marker' ? "field_{$marker_field_obj->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;
  }
  $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) {
    $markername = isset($this->options['markertype']) ? $this->options['markertype'] : 'drupal';
    $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};
      }
      elseif ($this->options['datasource'] == 'geofield') {
        $lat = isset($row->{$lat_field}[0]['raw']['lat']) ? (double) $row->{$lat_field}[0]['raw']['lat'] : NULL;
        $lon = isset($row->{$lon_field}[0]['raw']['lon']) ? (double) $row->{$lon_field}[0]['raw']['lon'] : NULL;
      }
      elseif ($this->options['datasource'] == 'geolocation') {
        $lat = isset($row->{$lat_field}[0]['raw']['lat']) ? (double) $row->{$lat_field}[0]['raw']['lat'] : NULL;
        $lon = isset($row->{$lon_field}[0]['raw']['lng']) ? (double) $row->{$lon_field}[0]['raw']['lng'] : NULL;
      }
      elseif ($this->options['datasource'] == 'fields') {
        $lat = isset($row->{$lat_field}[0]['raw']['value']) ? (double) $row->{$lat_field}[0]['raw']['value'] : NULL;
        $lon = isset($row->{$lon_field}[0]['raw']['value']) ? (double) $row->{$lon_field}[0]['raw']['value'] : NULL;

        // Field is Location CCK.
        if (!$lat || !$lon) {
          $lat = isset($row->{$lat_field}[0]['raw']['latitude']) ? $row->{$lat_field}[0]['raw']['latitude'] : NULL;
          $lon = isset($row->{$lon_field}[0]['raw']['longitude']) ? $row->{$lon_field}[0]['raw']['longitude'] : NULL;
        }
      }

      // $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) && ($lat != 0 && $lon != 0) && ($lat != 180 && $lon != 360)) {
        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' && !empty($row->{$marker_field})) {
          if ($marker_field_obj->field_info['type'] == 'image') {
            $image = $row->{$marker_field}[0]['rendered'];
            $marker_icon = image_style_url($image['#image_style'], $image['#item']['uri']);
          }
          elseif (!empty($row->{$marker_field}[0]['raw']['value'])) {
            $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($this->options['bubbletextfield']) && !empty($this->rendered_fields[$row_index][$this->options['bubbletextfield']])) {
          $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'],
            'animation' => $this->options['animation'],
          ),
        );
        if ($this->options['markers'] == 'field' && isset($marker_icon)) {
          $marker['opts']['icon'] = $marker_icon;
          unset($marker_icon);
        }

        // RMT mode.
        if ($this->options['enablermt']) {
          if ($this->options['rmtfieldraw']) {
            $marker['rmt'] = $row->{$rmt_field};
          }
          else {
            $marker['rmt'] = $this->rendered_fields[$row_index][$rmt_field_obj->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]++;
      }
    }

    // Don't draw empty maps.
    if (!empty($markers)) {
      $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;
      }
      if ($this->options['markers'] == 'field') {
        $map['behavior']['customicons'] = TRUE;
      }
      $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;
}