You are here

public function IpGeoLocPluginStyleMap::render in IP Geolocation Views & Maps 8

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

Overrides StylePluginBase::render

File

src/Plugin/views/style/IpGeoLocPluginStyleMap.php, line 186

Class

IpGeoLocPluginStyleMap
Views Style plugin extension for Map.

Namespace

Drupal\ip_geoloc\Plugin\views\style

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.');
  }
  $this->view_plugin_style
    ->pluginStyleRenderFields($this);
  $locations = $this->view_plugin_style
    ->pluginStyleExtractLocations($this);
  $map_options = empty($this->options['map_options']) ? IP_GEOLOC_RECENT_VISITORS_MAP_OPTIONS : $this->options['map_options'];
  $map_div_style = empty($this->options['map_div_style']) ? IP_GEOLOC_MAP_DIV_DEFAULT_STYLE : SafeMarkup::checkPlain($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 ? SafeMarkup::checkPlain($visitor_marker) : FALSE;
  }
  $center_option = !isset($this->options['center_option']) ? IP_GEOLOC_MAP_CENTER_ON_FIRST_LOCATION : $this->options['center_option'];
  $center_latlng = [];
  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 existing lat/long.
    $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 = $this->api
      ->getLocationByIp($this->request_stack
      ->getCurrentRequest()
      ->getClientIp(), $resample, $store, $reverse_geocode);
    if (isset($visitor_location['latitude']) && isset($visitor_location['longitude'])) {
      $center_latlng = [
        $visitor_location['latitude'],
        $visitor_location['longitude'],
      ];
    }
  }
  if (empty($locations)) {
    $ll = trim($this->options['empty_map_center']);
    if (empty($ll)) {

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

      // Empty map centered on coordinates provided.
      list($center_lat, $center_lng) = preg_split("/[\\s,]+/", $ll);
    }
  }
  elseif ($center_option == IP_GEOLOC_MAP_CENTER_OF_LOCATIONS || $center_option == IP_GEOLOC_MAP_CENTER_OF_LOCATIONS_WEIGHTED) {
    list($center_lat, $center_lng) = $this->api
      ->centerOfLocations($locations, $center_option == IP_GEOLOC_MAP_CENTER_OF_LOCATIONS_WEIGHTED);
  }
  if (isset($center_lat) && isset($center_lng)) {
    $map_options = Unicode::substr($map_options, 0, strrpos($map_options, '}'));
    $map_options = $map_options . ', "centerLat":' . $center_lat . ', "centerLng":' . $center_lng . '}';
  }
  $account = \Drupal::currentUser();
  $gps_roles = empty($this->options['gps_roles']) ? [
    DRUPAL_ANONYMOUS_RID,
    DRUPAL_AUTHENTICATED_RID,
  ] : $this->options['gps_roles'];
  $gps_roles_applicable = array_intersect($gps_roles, array_keys($account->roles));

  // drupal_add_js(IP_GEOLOC_GOOGLE_MAPS, ['type' => 'external', 'group' => JS_LIBRARY]);
  // drupal_add_js(drupal_get_path('module', 'ip_geoloc') . '/js/ip_geoloc_gmap_multi_visitor.js');
  if (!isset($map_options)) {
    $map_options = IP_GEOLOC_RECENT_VISITORS_MAP_OPTIONS;
  }
  $div_id = 'ip-geoloc-map-of-view-' . $this->view->storage
    ->id() . '-' . $this->view->current_display;
  return $this->api
    ->outputMapMultiLocation($locations, $div_id, $map_options, $map_div_style, $marker_color, $visitor_marker, $center_option, $center_latlng, !empty($gps_roles_applicable));
}