1
<?php
2
defined('ABSPATH') || exit;
3
4
if (!class_exists('FRESH_OO_Property_Map_Widget') && class_exists('FRESH_OO_Widget_Base')) {
5
class FRESH_OO_Property_Map_Widget extends FRESH_OO_Widget_Base {
6
public function get_name(): string { return 'fresh-oo-property-map'; }
7
public function get_title(): string { return 'onOffice Objektkarte'; }
8
public function get_icon(): string { return 'eicon-google-maps'; }
9
public function get_keywords(): array { return ['onoffice','karte','map','immobilie','osm','google']; }
10
public function get_style_depends(): array { return ['fresh-oo-toolkit','fresh-oo-maps']; }
11
12
protected function register_controls(): void {
13
$this->start_controls_section('content',['label'=>'Karte']);
14
$this->add_control('provider',['label'=>'Kartenanbieter','type'=>\Elementor\Controls_Manager::SELECT,'default'=>'auto','options'=>[
15
'auto'=>'onOffice-Einstellung übernehmen','osm'=>'OpenStreetMap','google-maps'=>'Google Maps'
16
]]);
17
$this->add_control('respect_visibility',['label'=>'onOffice Kartenfreigabe beachten','type'=>\Elementor\Controls_Manager::SWITCHER,'default'=>'yes','description'=>'Blendet die Karte aus, wenn onOffice die Kartenanzeige deaktiviert oder eine virtuelle Adresse verwendet.']);
18
$this->add_control('show_marker',['label'=>'Marker anzeigen','type'=>\Elementor\Controls_Manager::SWITCHER,'default'=>'yes']);
19
$this->add_control('show_route',['label'=>'Routen-Link anzeigen','type'=>\Elementor\Controls_Manager::SWITCHER,'default'=>'yes']);
20
$this->add_control('route_label',['label'=>'Routen-Link Text','type'=>\Elementor\Controls_Manager::TEXT,'default'=>'Route öffnen','condition'=>['show_route'=>'yes']]);
21
$this->add_control('zoom',['label'=>'Zoom','type'=>\Elementor\Controls_Manager::NUMBER,'default'=>15,'min'=>3,'max'=>20]);
22
$this->end_controls_section();
23
24
$this->start_controls_section('style_map',['label'=>'Karte','tab'=>\Elementor\Controls_Manager::TAB_STYLE]);
25
$this->add_responsive_control('height',['label'=>'Höhe','type'=>\Elementor\Controls_Manager::SLIDER,'size_units'=>['px','vh'],'range'=>['px'=>['min'=>180,'max'=>900],'vh'=>['min'=>20,'max'=>100]],'default'=>['size'=>420,'unit'=>'px'],'selectors'=>['{{WRAPPER}} .fresh-oo-map__canvas'=>'height:{{SIZE}}{{UNIT}};']]);
26
$this->add_responsive_control('radius',['label'=>'Eckenradius','type'=>\Elementor\Controls_Manager::DIMENSIONS,'size_units'=>['px','%'],'selectors'=>['{{WRAPPER}} .fresh-oo-map__canvas'=>'border-radius:{{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};']]);
27
$this->add_group_control(\Elementor\Group_Control_Border::get_type(),['name'=>'border','selector'=>'{{WRAPPER}} .fresh-oo-map__canvas']);
28
$this->add_group_control(\Elementor\Group_Control_Box_Shadow::get_type(),['name'=>'shadow','selector'=>'{{WRAPPER}} .fresh-oo-map__canvas']);
29
$this->end_controls_section();
30
31
$this->start_controls_section('style_route',['label'=>'Routen-Link','tab'=>\Elementor\Controls_Manager::TAB_STYLE,'condition'=>['show_route'=>'yes']]);
32
$this->add_control('route_color',['label'=>'Textfarbe','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .fresh-oo-map__route'=>'color:{{VALUE}};']]);
33
$this->add_control('route_bg',['label'=>'Hintergrund','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .fresh-oo-map__route'=>'background:{{VALUE}};']]);
34
$this->add_group_control(\Elementor\Group_Control_Typography::get_type(),['name'=>'route_typo','selector'=>'{{WRAPPER}} .fresh-oo-map__route']);
35
$this->add_responsive_control('route_gap',['label'=>'Abstand zur Karte','type'=>\Elementor\Controls_Manager::SLIDER,'range'=>['px'=>['min'=>0,'max'=>80]],'selectors'=>['{{WRAPPER}} .fresh-oo-map__route'=>'margin-top:{{SIZE}}{{UNIT}};']]);
36
$this->add_responsive_control('route_padding',['label'=>'Innenabstand','type'=>\Elementor\Controls_Manager::DIMENSIONS,'size_units'=>['px','em'],'selectors'=>['{{WRAPPER}} .fresh-oo-map__route'=>'padding:{{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};']]);
37
$this->add_responsive_control('route_radius',['label'=>'Eckenradius','type'=>\Elementor\Controls_Manager::DIMENSIONS,'size_units'=>['px','%'],'selectors'=>['{{WRAPPER}} .fresh-oo-map__route'=>'border-radius:{{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};']]);
38
$this->end_controls_section();
39
}
40
41
private function provider(array $s): string {
42
$provider = (string)($s['provider'] ?? 'auto');
43
if ($provider === 'auto') $provider = (string)get_option('onoffice-maps-mapprovider','osm');
44
if ($provider === 'google-maps' && trim((string)get_option('onoffice-settings-googlemaps-key','')) === '') $provider = 'osm';
45
return in_array($provider,['osm','google-maps'],true) ? $provider : 'osm';
46
}
47
48
private function enqueue_provider(string $provider): void {
49
if ($provider === 'google-maps' && wp_script_is('google-maps','registered')) {
50
wp_enqueue_script('google-maps');
51
} else {
52
if (wp_style_is('leaflet-style','registered')) wp_enqueue_style('leaflet-style');
53
if (wp_script_is('leaflet-script','registered')) wp_enqueue_script('leaflet-script');
54
}
55
wp_enqueue_style('fresh-oo-maps');
56
wp_enqueue_script('fresh-oo-maps');
57
}
58
59
protected function render(): void {
60
$s = $this->get_settings_for_display();
61
$lat = (float)$this->meta('oo_breitengrad');
62
$lng = (float)$this->meta('oo_laengengrad');
63
if ($lat == 0.0 || $lng == 0.0) { $this->editor_empty('Keine Kartenkoordinaten für diese Immobilie vorhanden. Nach dem nächsten onOffice-Sync erneut prüfen.'); return; }
64
65
if (($s['respect_visibility'] ?? 'yes') === 'yes') {
66
$show = $this->meta('oo_showgooglemap');
67
$virtual = $this->meta('oo_virtualaddress');
68
if (($show !== '' && !$this->is_truthy($show)) || $this->is_truthy($virtual)) { $this->editor_empty('Die Karte ist für diese Immobilie in onOffice nicht freigegeben.'); return; }
69
}
70
71
$provider = $this->provider($s);
72
$this->enqueue_provider($provider);
73
$title = get_the_title($this->current_post_id());
74
$zoom = max(3,min(20,(int)($s['zoom'] ?? 15)));
75
$show_marker = ($s['show_marker'] ?? 'yes') === 'yes';
76
$id = 'fresh-oo-map-' . $this->get_id();
77
$address = trim(implode(' ', array_filter([
78
trim((string)$this->meta('oo_strasse') . ' ' . (string)$this->meta('oo_hausnummer')),
79
trim((string)$this->meta('oo_plz') . ' ' . (string)$this->meta('oo_ort')),
80
(string)$this->meta('oo_land'),
81
])));
82
$route = $provider === 'google-maps'
83
? 'https://www.google.com/maps/dir/?api=1&destination=' . rawurlencode($lat . ',' . $lng)
84
: 'https://www.openstreetmap.org/directions?to=' . rawurlencode($lat . ',' . $lng);
85
86
echo '<div class="fresh-oo-map fresh-oo-map--single">';
87
printf('<div id="%s" class="fresh-oo-map__canvas" data-fresh-oo-map="1" data-provider="%s" data-lat="%s" data-lng="%s" data-zoom="%d" data-marker="%s" data-title="%s" data-address="%s" role="region" aria-label="%s"></div>',
88
esc_attr($id), esc_attr($provider), esc_attr((string)$lat), esc_attr((string)$lng), $zoom, $show_marker?'1':'0', esc_attr($title), esc_attr($address), esc_attr(sprintf('Karte zur Immobilie %s',$title)));
89
if (($s['show_route'] ?? 'yes') === 'yes') {
90
echo '<a class="fresh-oo-map__route" href="'.esc_url($route).'" target="_blank" rel="noopener noreferrer">'.esc_html((string)($s['route_label'] ?? 'Route öffnen')).'</a>';
91
}
92
echo '</div>';
93
}
94
}
95
}
96