View source
<?php
namespace Drupal\ip_geoloc\Plugin\views\style;
use Drupal\core\form\FormStateInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\ip_geoloc\Services\IpGeoLocAPI;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
require_once 'ip_geoloc_plugin_style.inc';
class IpGeoLocPluginStyleOpenLayers extends StylePluginBase implements ContainerFactoryPluginInterface {
public $moduleHandler;
public $api;
public function __construct(array $configuration, $plugin_id, $plugin_definition, IpGeoLocAPI $api, ModuleHandler $module_handler, IpGeoLocGlobal $ip_geoloc_global, TranslationManager $string_translation, IpGeoLocSession $ip_geoloc_session, IpGeoLocViewsPluginStyle $ip_geoloc_view_plugin_style, RequestStack $request_stack) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->api = $api;
$this->module_handler = $module_handler;
$this->ip_geoloc_global = $ip_geoloc_global;
$this->string_translation = $string_translation;
$this->session = $ip_geoloc_session;
$this->view_plugin_style = $ip_geoloc_view_plugin_style;
$this->request_stack = $request_stack;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('ip_geoloc.api'), $container
->get('module_handler'), $container
->get('ip_geoloc.global'), $container
->get('string_translation'), $container
->get('ip_geoloc.session'), $container
->get('ip_geoloc.views_plugin_style'), $container
->get('request_stack'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$latitude = $this->moduleHandler
->moduleExists('location') ? 'location_latitude' : 'ip_geoloc_latitude';
$longitude = $this->moduleHandler
->moduleExists('location') ? 'location_longitude' : ($latitude == 'ip_geoloc_latitude' ? 'ip_geoloc_longitude' : $latitude);
$options['ip_geoloc_views_plugin_latitude'] = [
'default' => $latitude,
];
$options['ip_geoloc_views_plugin_longitude'] = [
'default' => $longitude,
];
$options['differentiator'] = [
'contains' => [
'differentiator_field' => [
'default' => '',
],
],
];
$options['default_marker_color'] = [
'default' => '',
];
$options['center_option'] = [
'default' => IP_GEOLOC_MAP_CENTER_ON_FIRST_LOCATION,
];
$options['argument'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['map'] = [
'#type' => 'select',
'#title' => t('Map'),
'#description' => t('The OpenLayers map used to place the view locations on. You can configure map and markers <a href="/admin/structure/openlayers/maps">here</a>.'),
'#options' => openlayers_map_options(),
'#default_value' => isset($this->options['map']) ? $this->options['map'] : \Drupal::state()
->get('openlayers_default_map', 'default'),
];
$form_state
->set('renderer', 'openlayers');
$this->view_plugin_style
->pluginStyleBulkOfForm($this, $form, $form_state);
unset($form['empty_map_center']);
$form['center_option']['#options'][IP_GEOLOC_MAP_CENTER_ON_LOCATION_FROM_ARGUMENT] = t('Use lat/lon coordinate arguments from a contextual filter');
$argument_handlers = $this->view->display_handler
->get_handlers('argument');
if (!empty($argument_handlers)) {
$argument_options = [
'' => '<' . t('none') . '>',
];
foreach ($argument_handlers as $key => $handler) {
$argument_options[$key] = $handler->definition['group'] . ': ' . $handler->definition['title'];
}
$form['argument'] = [
'#type' => 'select',
'#title' => t('Choose a contextual filter to pull data from'),
'#options' => $argument_options,
'#default_value' => $this->options['argument'],
'#weight' => 100,
];
}
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$this->view_plugin_style
->pluginStyleBulkOfFormValidate($form, $form_state);
}
public function render() {
$map = openlayers_map_load(isset($this->options['map']) ? $this->options['map'] : \Drupal::state()
->get('openlayers_default_map', 'default'));
$map = openlayers_build_map($map->data);
if (empty($map['errors'])) {
ip_geoloc_plugin_style_render_fields($this);
$locations = ip_geoloc_plugin_style_extract_locations($this);
$center_option = !isset($this->options['center_option']) ? IP_GEOLOC_MAP_CENTER_ON_FIRST_LOCATION : $this->options['center_option'];
$visitor_location = $this->api
->getVisitorLocation();
if ($center_option == IP_GEOLOC_MAP_CENTER_ON_VISITOR && ($center_set = isset($visitor_location['longitude']))) {
$longitude = $visitor_location['longitude'];
$latitude = $visitor_location['latitude'];
$map['center']['initial']['centerpoint'] = "{$longitude},{$latitude}";
}
elseif (($center_option == IP_GEOLOC_MAP_CENTER_OF_LOCATIONS || $center_option == IP_GEOLOC_MAP_CENTER_OF_LOCATIONS_WEIGHTED) && !empty($locations)) {
list($latitude, $longitude) = ip_geoloc_center_of_locations($locations, $center_option == IP_GEOLOC_MAP_CENTER_OF_LOCATIONS_WEIGHTED);
$map['center']['initial']['centerpoint'] = "{$longitude},{$latitude}";
$center_set = TRUE;
}
elseif ($center_option == IP_GEOLOC_MAP_CENTER_ON_LOCATION_FROM_ARGUMENT) {
if ($handler = $this->view->display_handler
->get_handler('argument', $this->options['argument'])) {
$latitude = $handler->value['lat'];
$longitude = $handler->value['lon'];
$map['center']['initial']['centerpoint'] = "{$longitude},{$latitude}";
$center_set = TRUE;
}
}
foreach ($locations as $location) {
if (isset($location->lon)) {
$lon = $location->lon;
$lat = $location->lat;
}
elseif (isset($location->longitude)) {
$lon = $location->longitude;
$lat = $location->latitude;
}
else {
continue;
}
if (empty($center_set) && $center_option == IP_GEOLOC_MAP_CENTER_ON_FIRST_LOCATION) {
$map['center']['initial']['centerpoint'] = "{$lon},{$lat}";
$center_set = TRUE;
}
$feature = [
'attributes' => [
'description' => $location->balloon_text,
],
'projection' => 'EPSG:4326',
'wkt' => "POINT({$lon} {$lat})",
];
$layer = isset($location->marker_color) && is_numeric($location->marker_color) ? $location->marker_color : 1;
$map['layers'][IP_GEOLOC_MARKER_LAYER . $layer]['features'][] = $feature;
}
if (!empty($map['hide_empty_map'])) {
$num_location_marker_layers = \Drupal::state()
->get('ip_geoloc_num_location_marker_layers', IP_GEOLOC_DEF_NUM_MARKER_LAYERS);
for ($layer = 1; $layer <= $num_location_marker_layers; $layer++) {
if (!empty($map['layers'][IP_GEOLOC_MARKER_LAYER . $layer]['features'])) {
$some_location_layers = TRUE;
break;
}
}
if (empty($some_location_layers) && empty($map['layers'][IP_GEOLOC_VISITOR_MARKER_LAYER]['features'])) {
return '';
}
}
$js = [
'openlayers' => [
'maps' => [
$map['id'] => $map,
],
],
];
drupal_add_js($js, 'setting');
$container_width = $map['width'];
$container_height = $map['height'];
$map['width'] = strpos($map['width'], '%') > 0 ? '100%' : $map['width'];
$map['height'] = strpos($map['height'], '%') > 0 ? '100%' : $map['height'];
$output = theme([
'ip_geoloc_openlayers',
], [
'view' => $this->view,
'map' => $map,
'container_width' => $container_width,
'container_height' => $container_height,
]);
}
else {
$output = implode('<br/>', $map['errors']);
}
return $output;
}
}