You are here

public function ip_geoloc_plugin_style_map::render in IP Geolocation Views & Maps 7

Transform the View result in a list of markers and render these on a map.

Overrides views_plugin_style::render

File

views/ip_geoloc_plugin_style_map.inc, line 126

Class

ip_geoloc_plugin_style_map

Code

public function render() {
  if (!empty($this->view->live_preview)) {
    return t('The preview function is incompatible with Google Maps so cannot be used. Please visit the page path or block to view your map.');
  }
  $render_start = microtime(TRUE);
  ip_geoloc_plugin_style_render_fields($this);
  $locations = ip_geoloc_plugin_style_extract_locations($this);
  $open_balloon_nos = array();
  $t_last = '<' . t('last') . '>';
  foreach (explode(',', $this->options['open_balloons']) as $no) {
    $open_balloon_nos[] = trim($no) == $t_last ? count($locations) - 1 : (int) $no - 1;
  }
  foreach ($locations as $key => $location) {
    $location->open = in_array($key, $open_balloon_nos);
  }
  $map_options = empty($this->options['map_options']) ? IP_GEOLOC_RECENT_VISITORS_MAP_OPTIONS : $this->options['map_options'];
  $map_div_style = trim($this->options['map_div_style']);
  $map_div_style = empty($map_div_style) ? IP_GEOLOC_MAP_DIV_DEFAULT_STYLE : ($map_div_style == '<none>' ? ' ' : check_plain($this->options['map_div_style']));
  $marker_color = empty($this->options['default_marker_color']) ? '' : $this->options['default_marker_color'];
  if (empty($this->options['visitor_marker'])) {

    // Default to standard red marker.
    $visitor_marker = TRUE;
  }
  else {
    $visitor_marker = trim($this->options['visitor_marker']);
    $visitor_marker = strpos($visitor_marker, 'none') === FALSE ? check_plain($visitor_marker) : FALSE;
  }
  $center_option = !isset($this->options['center_option']) ? IP_GEOLOC_MAP_CENTER_ON_FIRST_LOCATION : $this->options['center_option'];
  $center_latlng = FALSE;

  // Set the behaviour for the case that there are no locations to map.
  if (empty($locations)) {
    $ll = trim($this->options['empty_map_center']);
    if (empty($ll)) {

      // No map whatsoever.
      return;
    }
    if ($ll == t('visitor')) {
      $center_option = IP_GEOLOC_MAP_CENTER_ON_VISITOR;
    }
    else {

      // Empty map centered on coordinates provided.
      list($center_lat, $center_lng) = preg_split("/[\\s,]+/", $ll);
    }
  }
  if ($visitor_marker || $center_option == IP_GEOLOC_MAP_CENTER_ON_VISITOR) {

    // Perform database IP lookup as backup/replacement for HTML5 location
    // Visitor may be moving so ignore lat/long we have on the db.
    $resample = TRUE;

    // Do not store lat/long and city as it will obliterate the
    // reverse-geocoded one.
    $store = FALSE;

    // We only need lat/long, not full street address.
    $reverse_geocode = FALSE;
    $visitor_location = ip_geoloc_get_location_by_ip(ip_address(), $resample, $store, $reverse_geocode);
    if (!isset($visitor_location['latitude'])) {

      // If everything failed use whatever we have on the database.
      $visitor_location = db_query('SELECT * FROM {ip_geoloc} WHERE ip_address = :ip_address', array(
        ':ip_address' => ip_address(),
      ))
        ->fetchAssoc();
    }
    if (isset($visitor_location['latitude']) && isset($visitor_location['longitude'])) {
      $center_latlng = array(
        $visitor_location['latitude'],
        $visitor_location['longitude'],
      );
    }
  }
  if (!empty($locations) && $center_option == IP_GEOLOC_MAP_CENTER_OF_LOCATIONS || $center_option == IP_GEOLOC_MAP_CENTER_OF_LOCATIONS_WEIGHTED) {
    list($center_lat, $center_lng) = ip_geoloc_center_of_locations($locations, $center_option == IP_GEOLOC_MAP_CENTER_OF_LOCATIONS_WEIGHTED);
  }
  if (isset($center_lat) && isset($center_lng)) {
    $map_options = drupal_substr($map_options, 0, strrpos($map_options, '}'));
    $map_options = $map_options . ', "centerLat":' . $center_lat . ', "centerLng":' . $center_lng . '}';
  }
  global $user;
  $gps_roles = empty($this->options['gps_roles']) ? array(
    DRUPAL_ANONYMOUS_RID,
    DRUPAL_AUTHENTICATED_RID,
  ) : $this->options['gps_roles'];
  $gps_roles_applicable = array_intersect($gps_roles, array_keys($user->roles));
  $output = theme(array(
    'ip_geoloc_map',
  ), array(
    'view' => $this->view,
    'locations' => $locations,
    'div_id' => 'ip-geoloc-map-of-view-' . $this->view->name . '-' . $this->display->id,
    'map_options' => $map_options,
    'map_style' => $map_div_style,
    'marker_color' => $marker_color,
    'visitor_marker' => $visitor_marker,
    'center_option' => $center_option,
    'center_latlng' => $center_latlng,
    'visitor_location_gps' => !empty($gps_roles_applicable),
  ));
  ip_geoloc_debug(t('-- Google map preparation time: %sec s', array(
    '%sec' => number_format(microtime(TRUE) - $render_start, 2),
  )));
  return $output;
}