<?php
defined('ABSPATH') || exit;
require_once __DIR__ . '/elementor-widget-base.php';
if (!class_exists('FRESH_OO_Property_Image_Widget') && class_exists('FRESH_OO_Widget_Base')) {
class FRESH_OO_Property_Image_Widget extends FRESH_OO_Widget_Base {
public function get_name(): string { return 'fresh-oo-property-image'; }
public function get_title(): string { return 'onOffice Immobilienbild'; }
public function get_icon(): string { return 'eicon-image'; }
public function get_keywords(): array { return ['onoffice','immobilie','bild','image','titelbild','status','badge']; }
protected function register_controls(): void {
$this->start_controls_section('content', ['label'=>'Bild']);
$this->add_control('source', [
'label'=>'Bildquelle','type'=>\Elementor\Controls_Manager::SELECT,'default'=>'featured',
'options'=>[
'featured'=>'Titelbild / Beitragsbild',
'gallery_first'=>'Erstes Galeriebild',
],
]);
$this->add_control('image_size', [
'label'=>'Bildgröße','type'=>\Elementor\Controls_Manager::SELECT,'default'=>'large',
'options'=>['thumbnail'=>'Thumbnail','medium'=>'Medium','medium_large'=>'Medium Large','large'=>'Large','full'=>'Original / Full'],
]);
$this->add_control('link_to', [
'label'=>'Link','type'=>\Elementor\Controls_Manager::SELECT,'default'=>'none',
'options'=>['none'=>'Keiner','media'=>'Mediendatei','post'=>'Immobilie'],
]);
$this->add_control('show_status', [
'label'=>'Objektstatus-Badge anzeigen','type'=>\Elementor\Controls_Manager::SWITCHER,
'return_value'=>'yes','default'=>'yes',
]);
$this->add_control('status_source', [
'label'=>'Statusfeld','type'=>\Elementor\Controls_Manager::SELECT,'default'=>'auto',
'options'=>['auto'=>'Automatisch (status2 → status)','oo_status2'=>'oo_status2','oo_status'=>'oo_status'],
'condition'=>['show_status'=>'yes'],
]);
$this->end_controls_section();
$this->start_controls_section('image_style', ['label'=>'Bild','tab'=>\Elementor\Controls_Manager::TAB_STYLE]);
$this->add_responsive_control('width', [
'label'=>'Breite','type'=>\Elementor\Controls_Manager::SLIDER,'size_units'=>['%','px','vw'],
'range'=>['%'=>['min'=>1,'max'=>100],'px'=>['min'=>50,'max'=>2000],'vw'=>['min'=>1,'max'=>100]],
'default'=>['unit'=>'%','size'=>100],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image'=>'width:{{SIZE}}{{UNIT}};'],
]);
$this->add_responsive_control('max_width', [
'label'=>'Max. Breite','type'=>\Elementor\Controls_Manager::SLIDER,'size_units'=>['%','px','vw'],
'range'=>['%'=>['min'=>1,'max'=>100],'px'=>['min'=>50,'max'=>2000],'vw'=>['min'=>1,'max'=>100]],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image'=>'max-width:{{SIZE}}{{UNIT}};'],
]);
$this->add_responsive_control('height', [
'label'=>'Höhe','type'=>\Elementor\Controls_Manager::SLIDER,'size_units'=>['px','vh'],
'range'=>['px'=>['min'=>60,'max'=>1400],'vh'=>['min'=>10,'max'=>100]],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image__img'=>'height:{{SIZE}}{{UNIT}};'],
]);
$this->add_control('fit', [
'label'=>'Objekt-Anpassung','type'=>\Elementor\Controls_Manager::SELECT,'default'=>'cover',
'options'=>['cover'=>'Cover','contain'=>'Contain','fill'=>'Fill','none'=>'Keine'],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image__img'=>'object-fit:{{VALUE}};'],
]);
$this->add_responsive_control('align', [
'label'=>'Ausrichtung','type'=>\Elementor\Controls_Manager::CHOOSE,'default'=>'left',
'options'=>[
'left'=>['title'=>'Links','icon'=>'eicon-text-align-left'],
'center'=>['title'=>'Mitte','icon'=>'eicon-text-align-center'],
'right'=>['title'=>'Rechts','icon'=>'eicon-text-align-right'],
],
'selectors'=>[
'{{WRAPPER}} .elementor-widget-container'=>'text-align:{{VALUE}};',
'{{WRAPPER}} .fresh-oo-property-image'=>'{{VALUE}}',
],
]);
$this->add_responsive_control('radius', [
'label'=>'Eckenradius','type'=>\Elementor\Controls_Manager::DIMENSIONS,'size_units'=>['px','%'],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image, {{WRAPPER}} .fresh-oo-property-image__img'=>'border-radius:{{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};'],
]);
$this->add_group_control(\Elementor\Group_Control_Border::get_type(), ['name'=>'border','selector'=>'{{WRAPPER}} .fresh-oo-property-image']);
$this->add_group_control(\Elementor\Group_Control_Box_Shadow::get_type(), ['name'=>'shadow','selector'=>'{{WRAPPER}} .fresh-oo-property-image']);
$this->end_controls_section();
$this->start_controls_section('badge_style', [
'label'=>'Objektstatus-Badge','tab'=>\Elementor\Controls_Manager::TAB_STYLE,
'condition'=>['show_status'=>'yes'],
]);
$this->add_responsive_control('badge_position', [
'label'=>'Position','type'=>\Elementor\Controls_Manager::SELECT,'default'=>'top-left',
'options'=>[
'top-left'=>'Oben links','top-right'=>'Oben rechts',
'bottom-left'=>'Unten links','bottom-right'=>'Unten rechts',
],
]);
$this->add_responsive_control('badge_offset_x', [
'label'=>'Horizontaler Abstand','type'=>\Elementor\Controls_Manager::SLIDER,'size_units'=>['px','%'],
'range'=>['px'=>['min'=>0,'max'=>150],'%'=>['min'=>0,'max'=>30]],
'default'=>['unit'=>'px','size'=>16],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image__badge-wrap'=>'--fresh-oo-badge-x:{{SIZE}}{{UNIT}};'],
]);
$this->add_responsive_control('badge_offset_y', [
'label'=>'Vertikaler Abstand','type'=>\Elementor\Controls_Manager::SLIDER,'size_units'=>['px','%'],
'range'=>['px'=>['min'=>0,'max'=>150],'%'=>['min'=>0,'max'=>30]],
'default'=>['unit'=>'px','size'=>16],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image__badge-wrap'=>'--fresh-oo-badge-y:{{SIZE}}{{UNIT}};'],
]);
$this->add_responsive_control('badge_padding', [
'label'=>'Innenabstand','type'=>\Elementor\Controls_Manager::DIMENSIONS,'size_units'=>['px','em'],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image__badge'=>'padding:{{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};'],
]);
$this->add_responsive_control('badge_radius', [
'label'=>'Radius','type'=>\Elementor\Controls_Manager::DIMENSIONS,'size_units'=>['px','%'],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image__badge'=>'border-radius:{{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};'],
]);
$this->add_group_control(\Elementor\Group_Control_Typography::get_type(), ['name'=>'badge_typo','selector'=>'{{WRAPPER}} .fresh-oo-property-image__badge']);
$this->add_group_control(\Elementor\Group_Control_Border::get_type(), ['name'=>'badge_border','selector'=>'{{WRAPPER}} .fresh-oo-property-image__badge']);
$this->add_group_control(\Elementor\Group_Control_Box_Shadow::get_type(), ['name'=>'badge_shadow','selector'=>'{{WRAPPER}} .fresh-oo-property-image__badge']);
$colors = [
'active'=>['Aktive Vermarktung','#DFF3E7','#17633A'],
'acquisition'=>['In Akquise','#EEEAFB','#51408C'],
'reserved'=>['Reserviert','#FFF1CC','#7A5600'],
'sold'=>['Verkauft / Vermietet','#F2E1E1','#7C2929'],
'other'=>['Sonstige','#EFEFEF','#333333'],
];
foreach ($colors as $group=>$cfg) {
$this->add_control($group.'_bg', [
'label'=>$cfg[0].' · Hintergrund','type'=>\Elementor\Controls_Manager::COLOR,'default'=>$cfg[1],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image__badge[data-status-group="'.$group.'"]'=>'background-color:{{VALUE}};'],
]);
$this->add_control($group.'_color', [
'label'=>$cfg[0].' · Text','type'=>\Elementor\Controls_Manager::COLOR,'default'=>$cfg[2],
'selectors'=>['{{WRAPPER}} .fresh-oo-property-image__badge[data-status-group="'.$group.'"]'=>'color:{{VALUE}};'],
]);
}
$this->end_controls_section();
}
private function image_id(array $s): int {
$post_id = $this->current_post_id();
if (!$post_id) return 0;
if (($s['source'] ?? 'featured') === 'gallery_first') {
$ids = get_post_meta($post_id, 'oo_gallery_ids', true);
if (is_array($ids)) {
foreach ($ids as $id) if (get_post_type((int)$id) === 'attachment') return (int)$id;
}
}
$featured = (int)get_post_thumbnail_id($post_id);
if ($featured) return $featured;
$ids = get_post_meta($post_id, 'oo_gallery_ids', true);
if (is_array($ids)) {
foreach ($ids as $id) if (get_post_type((int)$id) === 'attachment') return (int)$id;
}
return 0;
}
private function status_value(array $s): string {
$source = (string)($s['status_source'] ?? 'auto');
if ($source === 'oo_status2') return trim((string)$this->meta('oo_status2'));
if ($source === 'oo_status') return trim((string)$this->meta('oo_status'));
$v = trim((string)$this->meta('oo_status2'));
return $v !== '' ? $v : trim((string)$this->meta('oo_status'));
}
private function status_group(string $raw): string {
$v = strtolower(remove_accents(trim($raw)));
$v = str_replace(['-',' '], '_', $v);
if (str_contains($v,'reserv')) return 'reserved';
if (str_contains($v,'verkauft') || str_contains($v,'vermietet') || str_contains($v,'sold') || str_contains($v,'rented') || str_contains($v,'vermarktet')) return 'sold';
if (str_contains($v,'akquise') || str_contains($v,'acquisition')) return 'acquisition';
if (str_contains($v,'aktiv') || str_contains($v,'active') || str_contains($v,'vermarktung')) return 'active';
return 'other';
}
private function status_label(string $raw): string {
$v = strtolower(trim($raw));
$map = [
'aktive_vermarktung'=>'Aktive Vermarktung','aktiv'=>'Aktiv','active'=>'Aktiv',
'in_akquise'=>'In Akquise','akquise'=>'In Akquise',
'reserviert'=>'Reserviert','reserved'=>'Reserviert',
'verkauft'=>'Verkauft','sold'=>'Verkauft','vermietet'=>'Vermietet','rented'=>'Vermietet',
];
if (isset($map[$v])) return $map[$v];
if (is_numeric($raw)) return 'Status ' . $raw;
return ucwords(str_replace('_',' ',trim($raw)));
}
protected function render(): void {
$s = $this->get_settings_for_display();
$id = $this->image_id($s);
if (!$id) { $this->editor_empty('Für diese Immobilie ist noch kein Bild vorhanden.'); return; }
$size = (string)($s['image_size'] ?? 'large');
$src = wp_get_attachment_image_src($id, $size);
if (!$src) return;
$alt = trim((string)get_post_meta($id, '_wp_attachment_image_alt', true));
if ($alt === '') $alt = get_the_title($this->current_post_id());
$img = wp_get_attachment_image($id, $size, false, [
'class'=>'fresh-oo-property-image__img','alt'=>$alt,'loading'=>'lazy','decoding'=>'async'
]);
if (!$img) return;
$href = '';
if (($s['link_to'] ?? 'none') === 'media') {
$full = wp_get_attachment_image_src($id, 'full');
$href = $full ? $full[0] : '';
} elseif (($s['link_to'] ?? 'none') === 'post') {
$href = get_permalink($this->current_post_id());
}
$pos = (string)($s['badge_position'] ?? 'top-left');
echo '<figure class="fresh-oo-property-image">';
if ($href !== '') echo '<a class="fresh-oo-property-image__link" href="'.esc_url($href).'">';
echo $img; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ($href !== '') echo '</a>';
if (($s['show_status'] ?? 'yes') === 'yes') {
$raw = $this->status_value($s);
if ($raw !== '') {
echo '<span class="fresh-oo-property-image__badge-wrap fresh-oo-property-image__badge-wrap--'.esc_attr($pos).'">';
echo '<span class="fresh-oo-property-image__badge" data-status="'.esc_attr(sanitize_title($raw)).'" data-status-group="'.esc_attr($this->status_group($raw)).'">'.esc_html($this->status_label($raw)).'</span>';
echo '</span>';
}
}
echo '</figure>';
}
}
}