1
<?php
2
/** Elementor widget: object-specific onOffice inquiry form. */
3
defined('ABSPATH') || exit;
4
5
if (!class_exists('FRESH_OO_Inquiry_Form_Widget') && class_exists('FRESH_OO_Widget_Base')) {
6
class FRESH_OO_Inquiry_Form_Widget extends FRESH_OO_Widget_Base {
7
public function get_name(): string { return 'fresh-oo-inquiry-form'; }
8
public function get_title(): string { return 'onOffice Objektanfrage'; }
9
public function get_icon(): string { return 'eicon-form-horizontal'; }
10
11
private function form_options(): array {
12
global $wpdb;
13
$options = ['' => '— Kontaktformular wählen —'];
14
$table = $wpdb->prefix . 'oo_plugin_forms';
15
$exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table));
16
if ($exists !== $table) return $options;
17
$rows = $wpdb->get_results("SELECT name, form_type FROM {$table} WHERE form_type='contact' ORDER BY form_id", ARRAY_A);
18
foreach ((array)$rows as $row) {
19
$name = trim((string)($row['name'] ?? ''));
20
if ($name !== '') $options[$name] = $name;
21
}
22
return $options;
23
}
24
25
protected function register_controls(): void {
26
$this->start_controls_section('content', ['label' => 'Formular']);
27
$forms = $this->form_options();
28
$default = array_key_exists('Kontakt', $forms) ? 'Kontakt' : (array_key_first(array_slice($forms, 1, null, true)) ?: '');
29
$this->add_control('form_name', [
30
'label' => 'onOffice Kontaktformular',
31
'type' => \Elementor\Controls_Manager::SELECT,
32
'options' => $forms,
33
'default' => $default,
34
'description' => 'Verwendet ein Kontaktformular aus dem offiziellen onOffice-Plugin und verknüpft die Anfrage automatisch mit der aktuellen Immobilie.',
35
]);
36
$this->add_control('show_heading', ['label'=>'Überschrift anzeigen','type'=>\Elementor\Controls_Manager::SWITCHER,'default'=>'yes']);
37
$this->add_control('heading', ['label'=>'Überschrift','type'=>\Elementor\Controls_Manager::TEXT,'default'=>'Interesse an dieser Immobilie?','condition'=>['show_heading'=>'yes']]);
38
$this->add_control('heading_tag', ['label'=>'HTML-Tag','type'=>\Elementor\Controls_Manager::SELECT,'default'=>'h2','options'=>['h2'=>'H2','h3'=>'H3','h4'=>'H4','div'=>'DIV'],'condition'=>['show_heading'=>'yes']]);
39
$this->add_control('show_context', ['label'=>'Objektbezug anzeigen','type'=>\Elementor\Controls_Manager::SWITCHER,'default'=>'yes']);
40
$this->add_control('context_text', ['label'=>'Text davor','type'=>\Elementor\Controls_Manager::TEXT,'default'=>'Anfrage zu','condition'=>['show_context'=>'yes']]);
41
$this->end_controls_section();
42
43
$this->start_controls_section('box_style', ['label'=>'Box','tab'=>\Elementor\Controls_Manager::TAB_STYLE]);
44
$this->add_common_card_style_controls('box', '{{WRAPPER}} .fresh-oo-inquiry');
45
$this->add_responsive_control('box_gap', ['label'=>'Abstand','type'=>\Elementor\Controls_Manager::SLIDER,'range'=>['px'=>['min'=>0,'max'=>80]],'selectors'=>['{{WRAPPER}} .fresh-oo-inquiry'=>'gap:{{SIZE}}{{UNIT}};']]);
46
$this->end_controls_section();
47
48
$this->start_controls_section('heading_style', ['label'=>'Überschrift','tab'=>\Elementor\Controls_Manager::TAB_STYLE,'condition'=>['show_heading'=>'yes']]);
49
$this->add_control('heading_color', ['label'=>'Farbe','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .fresh-oo-inquiry__heading'=>'color:{{VALUE}};']]);
50
$this->add_group_control(\Elementor\Group_Control_Typography::get_type(), ['name'=>'heading_typo','selector'=>'{{WRAPPER}} .fresh-oo-inquiry__heading']);
51
$this->end_controls_section();
52
53
$this->start_controls_section('context_style', ['label'=>'Objektbezug','tab'=>\Elementor\Controls_Manager::TAB_STYLE,'condition'=>['show_context'=>'yes']]);
54
$this->add_control('context_color', ['label'=>'Farbe','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .fresh-oo-inquiry__context'=>'color:{{VALUE}};']]);
55
$this->add_group_control(\Elementor\Group_Control_Typography::get_type(), ['name'=>'context_typo','selector'=>'{{WRAPPER}} .fresh-oo-inquiry__context']);
56
$this->end_controls_section();
57
58
$this->start_controls_section('form_style', ['label'=>'Formular','tab'=>\Elementor\Controls_Manager::TAB_STYLE]);
59
$this->add_control('label_color', ['label'=>'Label-Farbe','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .oo-form label'=>'color:{{VALUE}};']]);
60
$this->add_group_control(\Elementor\Group_Control_Typography::get_type(), ['name'=>'label_typo','selector'=>'{{WRAPPER}} .oo-form label']);
61
$this->add_control('field_text', ['label'=>'Feld-Text','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .oo-form input, {{WRAPPER}} .oo-form textarea, {{WRAPPER}} .oo-form select'=>'color:{{VALUE}};']]);
62
$this->add_control('field_bg', ['label'=>'Feld-Hintergrund','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .oo-form input:not([type="submit"]), {{WRAPPER}} .oo-form textarea, {{WRAPPER}} .oo-form select'=>'background-color:{{VALUE}};']]);
63
$this->add_group_control(\Elementor\Group_Control_Border::get_type(), ['name'=>'field_border','selector'=>'{{WRAPPER}} .oo-form input:not([type="submit"]), {{WRAPPER}} .oo-form textarea, {{WRAPPER}} .oo-form select']);
64
$this->add_responsive_control('field_radius', ['label'=>'Feld-Radius','type'=>\Elementor\Controls_Manager::DIMENSIONS,'size_units'=>['px','%'],'selectors'=>['{{WRAPPER}} .oo-form input:not([type="submit"]), {{WRAPPER}} .oo-form textarea, {{WRAPPER}} .oo-form select'=>'border-radius:{{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};']]);
65
$this->add_responsive_control('field_padding', ['label'=>'Feld-Innenabstand','type'=>\Elementor\Controls_Manager::DIMENSIONS,'size_units'=>['px','em'],'selectors'=>['{{WRAPPER}} .oo-form input:not([type="submit"]), {{WRAPPER}} .oo-form textarea, {{WRAPPER}} .oo-form select'=>'padding:{{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};']]);
66
$this->end_controls_section();
67
68
$this->start_controls_section('button_style', ['label'=>'Button','tab'=>\Elementor\Controls_Manager::TAB_STYLE]);
69
$this->add_control('button_bg', ['label'=>'Hintergrund','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .oo-form input[type="submit"]'=>'background-color:{{VALUE}}!important;']]);
70
$this->add_control('button_color', ['label'=>'Text','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .oo-form input[type="submit"]'=>'color:{{VALUE}}!important;']]);
71
$this->add_control('button_hover_bg', ['label'=>'Hover-Hintergrund','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .oo-form input[type="submit"]:hover'=>'background-color:{{VALUE}}!important;']]);
72
$this->add_control('button_hover_color', ['label'=>'Hover-Text','type'=>\Elementor\Controls_Manager::COLOR,'selectors'=>['{{WRAPPER}} .oo-form input[type="submit"]:hover'=>'color:{{VALUE}}!important;']]);
73
$this->add_group_control(\Elementor\Group_Control_Typography::get_type(), ['name'=>'button_typo','selector'=>'{{WRAPPER}} .oo-form input[type="submit"]']);
74
$this->add_responsive_control('button_padding', ['label'=>'Innenabstand','type'=>\Elementor\Controls_Manager::DIMENSIONS,'size_units'=>['px','em'],'selectors'=>['{{WRAPPER}} .oo-form input[type="submit"]'=>'padding:{{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;']]);
75
$this->add_responsive_control('button_radius', ['label'=>'Radius','type'=>\Elementor\Controls_Manager::DIMENSIONS,'size_units'=>['px','%'],'selectors'=>['{{WRAPPER}} .oo-form input[type="submit"]'=>'border-radius:{{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;']]);
76
$this->end_controls_section();
77
}
78
79
protected function render(): void {
80
$s = $this->get_settings_for_display();
81
$post_id = $this->current_post_id();
82
if (!$post_id || get_post_type($post_id) !== FRESH_OnOffice_Elementor_Bridge::CPT) {
83
$this->editor_empty('Dieses Widget benötigt eine onOffice-Immobilie als Vorschau.');
84
return;
85
}
86
$estate_id = (int)get_post_meta($post_id, FRESH_OnOffice_Elementor_Bridge::META_OO_ID, true);
87
if (!$estate_id) $estate_id = (int)get_post_meta($post_id, 'oo_id', true);
88
if (!$estate_id) {
89
$this->editor_empty('Keine onOffice Objekt-ID gefunden.');
90
return;
91
}
92
$form = trim((string)($s['form_name'] ?? ''));
93
if ($form === '' || !shortcode_exists('oo_form')) {
94
$this->editor_empty('Kein onOffice Kontaktformular verfügbar.');
95
return;
96
}
97
98
global $wp_query, $wp;
99
$old_query = $wp_query ? $wp_query->get('estate_id', null) : null;
100
$old_wp = isset($wp->query_vars['estate_id']) ? $wp->query_vars['estate_id'] : null;
101
if ($wp_query) $wp_query->set('estate_id', $estate_id);
102
if ($wp) $wp->query_vars['estate_id'] = $estate_id;
103
104
$output = do_shortcode('[oo_form form="' . esc_attr($form) . '"]');
105
106
if ($wp_query) $wp_query->set('estate_id', $old_query);
107
if ($wp) {
108
if ($old_wp === null) unset($wp->query_vars['estate_id']);
109
else $wp->query_vars['estate_id'] = $old_wp;
110
}
111
112
if (trim($output) === '') {
113
$this->editor_empty('Das ausgewählte onOffice Formular konnte nicht gerendert werden.');
114
return;
115
}
116
117
echo '<section class="fresh-oo-inquiry">';
118
if (($s['show_heading'] ?? '') === 'yes') {
119
$tag = in_array(($s['heading_tag'] ?? 'h2'), ['h2','h3','h4','div'], true) ? $s['heading_tag'] : 'h2';
120
echo '<' . esc_attr($tag) . ' class="fresh-oo-inquiry__heading">' . esc_html((string)($s['heading'] ?? '')) . '</' . esc_attr($tag) . '>';
121
}
122
if (($s['show_context'] ?? '') === 'yes') {
123
$prefix = trim((string)($s['context_text'] ?? 'Anfrage zu'));
124
$title = get_the_title($post_id);
125
$number = trim((string)get_post_meta($post_id, 'oo_objektnr_extern', true));
126
echo '<div class="fresh-oo-inquiry__context">';
127
if ($prefix !== '') echo esc_html($prefix) . ' ';
128
echo '<strong>' . esc_html($title) . '</strong>';
129
if ($number !== '') echo ' <span>· ' . esc_html($number) . '</span>';
130
echo '</div>';
131
}
132
echo '<div class="fresh-oo-inquiry__form" data-estate-id="' . esc_attr((string)$estate_id) . '">' . $output . '</div>';
133
echo '</section>';
134
}
135
}
136
}
137