Files
2026-05-06 00:18:37 +02:00

600 lines
20 KiB
PHP

<?php
/**
* Yacht Calendar Widget for Elementor
*
* @package YachtBooking
*/
namespace YachtBooking;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use Elementor\Controls_Manager;
use Elementor\Widget_Base;
/**
* Yacht Calendar Widget Class
*/
class Calendar_Widget extends Widget_Base {
/**
* Get yachts for quick switcher.
*
* @return array<int, array<string, mixed>>
*/
private function get_yacht_switcher_items() {
$yachts = get_posts(
array(
'post_type' => 'yacht',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
)
);
$items = array();
foreach ( $yachts as $yacht ) {
$items[] = array(
'id' => (int) $yacht->ID,
'title' => get_the_title( $yacht ),
'description' => wp_kses_post( wpautop( $yacht->post_content ) ),
);
}
return $items;
}
/**
* Render quick yacht switcher.
*
* @param array<int, array<string, mixed>> $yachts Yacht items.
* @param int $active_id Active yacht ID.
* @return void
*/
private function render_yacht_switcher( $yachts, $active_id ) {
$other_yachts = array_filter(
$yachts,
static function( $yacht_item ) use ( $active_id ) {
return (int) $yacht_item['id'] !== (int) $active_id;
}
);
if ( empty( $other_yachts ) ) {
return;
}
?>
<div class="yacht-calendar-switcher" aria-label="<?php esc_attr_e( 'Szybki wybór jachtu', 'yacht-booking' ); ?>">
<span class="yacht-calendar-switcher-label"><?php esc_html_e( 'Zobacz też:', 'yacht-booking' ); ?></span>
<div class="yacht-calendar-switcher-buttons">
<?php foreach ( $other_yachts as $yacht_item ) : ?>
<button type="button"
class="yacht-calendar-switcher-button"
data-yacht-id="<?php echo esc_attr( $yacht_item['id'] ); ?>"
aria-pressed="false">
<?php echo esc_html( $yacht_item['title'] ); ?>
</button>
<?php endforeach; ?>
</div>
</div>
<?php
}
/**
* Get widget name
*
* @return string Widget name.
*/
public function get_name() {
return 'yacht-calendar';
}
/**
* Get widget title
*
* @return string Widget title.
*/
public function get_title() {
return esc_html__( 'Kalendarz Jachtu', 'yacht-booking' );
}
/**
* Get widget icon
*
* @return string Widget icon.
*/
public function get_icon() {
return 'eicon-calendar';
}
/**
* Get widget categories
*
* @return array Widget categories.
*/
public function get_categories() {
return array( 'basic' );
}
/**
* Get widget keywords
*
* @return array Widget keywords.
*/
public function get_keywords() {
return array( 'yacht', 'calendar', 'booking', 'kalendarz', 'rezerwacja' );
}
/**
* Register widget controls
*/
protected function register_controls() {
// Content Section - Yacht Selection
$this->start_controls_section(
'content_section',
array(
'label' => esc_html__( 'Ustawienia Kalendarza', 'yacht-booking' ),
'tab' => Controls_Manager::TAB_CONTENT,
)
);
// Yacht selector
$yachts = get_posts(
array(
'post_type' => 'yacht',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'fields' => 'ids',
)
);
$yacht_options = array();
foreach ( $yachts as $yacht_id ) {
$yacht_options[ $yacht_id ] = get_the_title( $yacht_id );
}
if ( empty( $yacht_options ) ) {
$yacht_options[0] = esc_html__( 'Brak jachtów - dodaj pierwszy jacht w panelu admin', 'yacht-booking' );
}
$this->add_control(
'yacht_id',
array(
'label' => esc_html__( 'Wybierz Jacht', 'yacht-booking' ),
'type' => Controls_Manager::SELECT,
'options' => $yacht_options,
'default' => array_key_first( $yacht_options ),
'label_block' => true,
)
);
$this->add_control(
'show_form',
array(
'label' => esc_html__( 'Pokaż formularz rezerwacji', 'yacht-booking' ),
'type' => Controls_Manager::SWITCHER,
'label_on' => esc_html__( 'Tak', 'yacht-booking' ),
'label_off' => esc_html__( 'Nie', 'yacht-booking' ),
'return_value' => 'yes',
'default' => 'yes',
)
);
$this->add_control(
'calendar_height',
array(
'label' => esc_html__( 'Wysokość kalendarza', 'yacht-booking' ),
'type' => Controls_Manager::SLIDER,
'range' => array(
'px' => array(
'min' => 400,
'max' => 1000,
),
),
'default' => array(
'size' => 600,
'unit' => 'px',
),
)
);
$this->end_controls_section();
// Style Section
$this->start_controls_section(
'style_section',
array(
'label' => esc_html__( 'Styl', 'yacht-booking' ),
'tab' => Controls_Manager::TAB_STYLE,
)
);
$this->add_control(
'primary_color',
array(
'label' => esc_html__( 'Kolor główny', 'yacht-booking' ),
'type' => Controls_Manager::COLOR,
'default' => '#0e2036',
)
);
$this->add_control(
'available_color',
array(
'label' => esc_html__( 'Kolor dni dostępnych', 'yacht-booking' ),
'type' => Controls_Manager::COLOR,
'default' => '#f5f9ff',
)
);
$this->add_control(
'booked_color',
array(
'label' => esc_html__( 'Kolor dni zajętych', 'yacht-booking' ),
'type' => Controls_Manager::COLOR,
'default' => '#bc1834',
)
);
$this->end_controls_section();
}
/**
* Render widget output on the frontend
*/
protected function render() {
$settings = $this->get_settings_for_display();
$yacht_id = ! empty( $settings['yacht_id'] ) ? (int) $settings['yacht_id'] : 0;
if ( ! $yacht_id ) {
if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
echo '<div class="yacht-calendar-notice" style="padding: 20px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 4px;">';
echo '<p style="margin: 0;"><strong>' . esc_html__( 'Uwaga:', 'yacht-booking' ) . '</strong> ';
echo esc_html__( 'Najpierw dodaj jacht w panelu administratora.', 'yacht-booking' ) . '</p>';
echo '</div>';
}
return;
}
$yacht = get_post( $yacht_id );
$yacht_switcher = $this->get_yacht_switcher_items();
$booking_enabled = Settings::is_booking_enabled();
$show_form = $booking_enabled && 'yes' === $settings['show_form'];
$calendar_id = 'yacht-calendar-' . $this->get_id();
$raw_height = ! empty( $settings['calendar_height']['size'] ) ? (int) $settings['calendar_height']['size'] : 600;
$height = $raw_height;
$primary_color = ! empty( $settings['primary_color'] ) ? $settings['primary_color'] : '#0e2036';
$available_bg = ! empty( $settings['available_color'] ) ? $settings['available_color'] : '#f5f9ff';
$booked_bg = ! empty( $settings['booked_color'] ) ? $settings['booked_color'] : '#bc1834';
$terms_url = Settings::get_terms_page_url();
?>
<div class="yacht-calendar-wrapper<?php echo $booking_enabled ? '' : ' yacht-calendar-view-only'; ?>"
data-yacht-id="<?php echo esc_attr( $yacht_id ); ?>"
data-calendar-id="<?php echo esc_attr( $calendar_id ); ?>"
data-primary-color="<?php echo esc_attr( $primary_color ); ?>"
data-available-bg="<?php echo esc_attr( $available_bg ); ?>"
data-booked-bg="<?php echo esc_attr( $booked_bg ); ?>"
data-yachts="<?php echo esc_attr( wp_json_encode( $yacht_switcher ) ); ?>"
data-booking-enabled="<?php echo $booking_enabled ? '1' : '0'; ?>">
<div class="yacht-calendar-header">
<h3 class="yacht-calendar-title">
<?php if ( $booking_enabled ) : ?>
<?php
/* translators: %s: yacht name */
printf( esc_html__( 'Rezerwacja: %s', 'yacht-booking' ), esc_html( $yacht->post_title ) );
?>
<?php else : ?>
<?php
/* translators: %s: yacht name */
printf( esc_html__( 'Dostępność: %s', 'yacht-booking' ), esc_html( $yacht->post_title ) );
?>
<?php endif; ?>
</h3>
<div class="yacht-calendar-description"<?php echo $yacht->post_content ? '' : ' style="display: none;"'; ?>>
<?php echo wp_kses_post( wpautop( $yacht->post_content ) ); ?>
</div>
</div>
<div class="yacht-calendar-instructions">
<p>
<?php if ( $booking_enabled ) : ?>
<strong><?php esc_html_e( 'Instrukcja:', 'yacht-booking' ); ?></strong>
<?php esc_html_e( 'Zielone dni są dostępne, czerwone są zajęte lub zablokowane. Wybierz termin, przeciągając myszą po dostępnych dniach.', 'yacht-booking' ); ?>
<?php else : ?>
<?php esc_html_e( 'Zielone dni są dostępne, czerwone są zajęte lub zablokowane. Aby zarezerwować jacht, skontaktuj się z nami telefonicznie lub mailowo.', 'yacht-booking' ); ?>
<?php endif; ?>
</p>
</div>
<div class="yacht-calendar-legend" aria-label="<?php esc_attr_e( 'Legenda kalendarza rezerwacji', 'yacht-booking' ); ?>">
<span class="yacht-legend-item">
<span class="yacht-legend-swatch" style="background-color: <?php echo esc_attr( $available_bg ); ?>;"></span>
<?php esc_html_e( 'Dostępny', 'yacht-booking' ); ?>
</span>
<span class="yacht-legend-item">
<span class="yacht-legend-swatch" style="background-color: <?php echo esc_attr( $booked_bg ); ?>;"></span>
<?php esc_html_e( 'Zajęty / zablokowany', 'yacht-booking' ); ?>
</span>
<span class="yacht-legend-item">
<span class="yacht-legend-swatch yacht-legend-swatch-past"></span>
<?php esc_html_e( 'Data przeszła', 'yacht-booking' ); ?>
</span>
</div>
<?php if ( ! $booking_enabled ) : ?>
<div class="yacht-inquiry-layout">
<div class="yacht-inquiry-calendar-col">
<div id="<?php echo esc_attr( $calendar_id ); ?>"
class="yacht-calendar"
style="height: <?php echo esc_attr( $height ); ?>px;">
</div>
</div>
<div class="yacht-inquiry-form-col">
<div class="yacht-inquiry-form-container">
<h4><?php esc_html_e( 'Zapytaj o rezerwację', 'yacht-booking' ); ?></h4>
<p class="yacht-inquiry-desc">
<?php esc_html_e( 'Wypełnij formularz, a skontaktujemy się z Tobą w sprawie rezerwacji.', 'yacht-booking' ); ?>
</p>
<form class="yacht-inquiry-form" data-yacht-id="<?php echo esc_attr( $yacht_id ); ?>">
<?php wp_nonce_field( 'yacht_inquiry_submit', 'yacht_inquiry_nonce' ); ?>
<div class="form-field">
<label for="inquiry_name_<?php echo esc_attr( $this->get_id() ); ?>">
<?php esc_html_e( 'Imię i nazwisko', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="text"
id="inquiry_name_<?php echo esc_attr( $this->get_id() ); ?>"
name="customer_name"
required>
</div>
<div class="form-field">
<label for="inquiry_email_<?php echo esc_attr( $this->get_id() ); ?>">
<?php esc_html_e( 'Email', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="email"
id="inquiry_email_<?php echo esc_attr( $this->get_id() ); ?>"
name="customer_email"
required>
</div>
<div class="form-field">
<label for="inquiry_phone_<?php echo esc_attr( $this->get_id() ); ?>">
<?php esc_html_e( 'Telefon', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="tel"
id="inquiry_phone_<?php echo esc_attr( $this->get_id() ); ?>"
name="customer_phone"
required>
</div>
<div class="form-field">
<label for="inquiry_dates_<?php echo esc_attr( $this->get_id() ); ?>">
<?php esc_html_e( 'Preferowane terminy', 'yacht-booking' ); ?>
</label>
<input type="text"
id="inquiry_dates_<?php echo esc_attr( $this->get_id() ); ?>"
name="preferred_dates"
placeholder="<?php esc_attr_e( 'np. 15-22 lipca', 'yacht-booking' ); ?>">
</div>
<div class="form-field">
<label for="inquiry_message_<?php echo esc_attr( $this->get_id() ); ?>">
<?php esc_html_e( 'Wiadomość', 'yacht-booking' ); ?>
</label>
<textarea id="inquiry_message_<?php echo esc_attr( $this->get_id() ); ?>"
name="message"
rows="3"
placeholder="<?php esc_attr_e( 'Dodatkowe pytania lub uwagi...', 'yacht-booking' ); ?>"></textarea>
</div>
<?php if ( $terms_url ) : ?>
<p class="booking-terms">
<?php
printf(
wp_kses_post( __( 'Wysyłając formularz akceptujesz %s.', 'yacht-booking' ) ),
'<a href="' . esc_url( $terms_url ) . '" target="_blank" rel="noopener">' . esc_html__( 'regulamin', 'yacht-booking' ) . '</a>'
);
?>
</p>
<?php endif; ?>
<div class="form-actions">
<button type="submit" class="yacht-booking-submit">
<?php esc_html_e( 'Wyślij zapytanie', 'yacht-booking' ); ?>
</button>
</div>
<div class="yacht-inquiry-response"></div>
</form>
</div>
</div>
</div>
<?php else : ?>
<div id="<?php echo esc_attr( $calendar_id ); ?>"
class="yacht-calendar"
style="height: <?php echo esc_attr( $height ); ?>px;">
</div>
<?php if ( $show_form ) : ?>
<div class="yacht-booking-form-container" style="display: none;">
<h4><?php esc_html_e( 'Formularz Rezerwacji', 'yacht-booking' ); ?></h4>
<form class="yacht-booking-form" data-yacht-id="<?php echo esc_attr( $yacht_id ); ?>">
<?php wp_nonce_field( 'yacht_booking_submit', 'yacht_booking_nonce' ); ?>
<div class="form-row">
<div class="form-field">
<label for="booking_start_date_<?php echo esc_attr( $this->get_id() ); ?>">
<?php esc_html_e( 'Data rozpoczęcia', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="text"
id="booking_start_date_<?php echo esc_attr( $this->get_id() ); ?>"
name="start_date"
class="booking-start-date"
readonly
required>
</div>
<div class="form-field">
<label for="booking_end_date_<?php echo esc_attr( $this->get_id() ); ?>">
<?php esc_html_e( 'Data zakończenia', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="text"
id="booking_end_date_<?php echo esc_attr( $this->get_id() ); ?>"
name="end_date"
class="booking-end-date"
readonly
required>
</div>
</div>
<div class="form-field">
<label for="booking_name_<?php echo esc_attr( $this->get_id() ); ?>">
<?php esc_html_e( 'Imię i nazwisko', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="text"
id="booking_name_<?php echo esc_attr( $this->get_id() ); ?>"
name="customer_name"
required>
</div>
<div class="form-field">
<label for="booking_email_<?php echo esc_attr( $this->get_id() ); ?>">
<?php esc_html_e( 'Email', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="email"
id="booking_email_<?php echo esc_attr( $this->get_id() ); ?>"
name="customer_email"
required>
</div>
<div class="form-field">
<label for="booking_phone_<?php echo esc_attr( $this->get_id() ); ?>">
<?php esc_html_e( 'Telefon', 'yacht-booking' ); ?> <span class="required">*</span>
</label>
<input type="tel"
id="booking_phone_<?php echo esc_attr( $this->get_id() ); ?>"
name="customer_phone"
required>
</div>
<?php if ( $terms_url ) : ?>
<p class="booking-terms">
<?php
printf(
wp_kses_post( __( 'Wysyłając formularz akceptujesz %s.', 'yacht-booking' ) ),
'<a href="' . esc_url( $terms_url ) . '" target="_blank" rel="noopener">' . esc_html__( 'regulamin', 'yacht-booking' ) . '</a>'
);
?>
</p>
<?php endif; ?>
<div class="form-actions">
<button type="submit" class="yacht-booking-submit">
<?php esc_html_e( 'Wyślij rezerwację', 'yacht-booking' ); ?>
</button>
</div>
<div class="yacht-booking-response"></div>
</form>
</div>
<?php endif; ?>
<?php endif; ?>
<?php $this->render_yacht_switcher( $yacht_switcher, $yacht_id ); ?>
</div>
<?php
}
/**
* Render widget output in the editor (Elementor preview)
*/
protected function content_template() {
?>
<#
var yachtId = settings.yacht_id;
var calendarId = 'yacht-calendar-preview-' + view.getID();
var height = settings.calendar_height.size || 600;
var showForm = 'yes' === settings.show_form;
#>
<div class="yacht-calendar-wrapper"
data-yacht-id="{{ yachtId }}"
data-calendar-id="{{ calendarId }}"
data-primary-color="{{ settings.primary_color }}"
data-available-bg="{{ settings.available_color }}"
data-booked-bg="{{ settings.booked_color }}">
<div class="yacht-calendar-switcher">
<span class="yacht-calendar-switcher-label">
<?php esc_html_e( 'Szybki wybór jachtu:', 'yacht-booking' ); ?>
</span>
<div class="yacht-calendar-switcher-buttons">
<button type="button" class="yacht-calendar-switcher-button is-active">
<?php esc_html_e( 'Aktualnie wybrany jacht', 'yacht-booking' ); ?>
</button>
<button type="button" class="yacht-calendar-switcher-button">
<?php esc_html_e( 'Inny jacht', 'yacht-booking' ); ?>
</button>
</div>
</div>
<div class="yacht-calendar-header">
<h3 class="yacht-calendar-title">
<?php esc_html_e( 'Kalendarz Rezerwacji Jachtu', 'yacht-booking' ); ?>
</h3>
<p class="yacht-calendar-notice" style="background: #e3f2fd; padding: 10px; border-radius: 4px;">
<strong><?php esc_html_e( 'Podgląd Elementor:', 'yacht-booking' ); ?></strong>
<?php esc_html_e( 'Kalendarz będzie widoczny na stronie frontu.', 'yacht-booking' ); ?>
</p>
</div>
<div class="yacht-calendar-instructions">
<p>
<strong><?php esc_html_e( 'Instrukcja:', 'yacht-booking' ); ?></strong>
<?php esc_html_e( 'Wybierz termin przeciągając po zielonych dniach. Czerwone dni są niedostępne.', 'yacht-booking' ); ?>
</p>
</div>
<div class="yacht-calendar-legend">
<span class="yacht-legend-item">
<span class="yacht-legend-swatch" style="background-color: <?php echo esc_attr( $available_bg ); ?>;"></span>
<?php esc_html_e( 'Dostępny', 'yacht-booking' ); ?>
</span>
<span class="yacht-legend-item">
<span class="yacht-legend-swatch" style="background: linear-gradient(135deg, <?php echo esc_attr( $available_bg ); ?> 0%, <?php echo esc_attr( $available_bg ); ?> 50%, <?php echo esc_attr( $booked_bg ); ?> 50%, <?php echo esc_attr( $booked_bg ); ?> 100%);"></span>
<?php esc_html_e( 'Dzień odbioru / zwrotu', 'yacht-booking' ); ?>
</span>
<span class="yacht-legend-item">
<span class="yacht-legend-swatch" style="background-color: <?php echo esc_attr( $booked_bg ); ?>;"></span>
<?php esc_html_e( 'Zajęty / zablokowany', 'yacht-booking' ); ?>
</span>
</div>
<div id="{{ calendarId }}"
class="yacht-calendar"
style="height: {{ height }}px; background: #f5f5f5; display: flex; align-items: center; justify-content: center;">
<p style="color: #666;">
<?php esc_html_e( 'Kalendarz FullCalendar zostanie załadowany na stronie frontu', 'yacht-booking' ); ?>
</p>
</div>
<# if ( showForm ) { #>
<div class="yacht-booking-form-container">
<h4><?php esc_html_e( 'Formularz Rezerwacji', 'yacht-booking' ); ?></h4>
<p style="background: #fff3cd; padding: 10px; border-radius: 4px;">
<?php esc_html_e( 'Formularz rezerwacji będzie aktywny na stronie frontu po wybraniu dat w kalendarzu.', 'yacht-booking' ); ?>
</p>
</div>
<# } #>
</div>
<?php
}
}