?AlkantarClanX12

Your IP : 216.73.217.134


Current Path : /home/rankinh/saintgilleslesbains-bis/wp-content/themes/divi-child/inc/shortcodes/
Upload File :
Current File : /home/rankinh/saintgilleslesbains-bis/wp-content/themes/divi-child/inc/shortcodes/annuaire.php

<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Module Divi Personnalisé : SGLB Annuaire Dynamique
 * Version avec espacement (gap) et largeur totale
 */
function sglb_register_annuaire_module() {
    if ( ! class_exists( 'ET_Builder_Module' ) ) return;

    class SGLB_Module_Annuaire extends ET_Builder_Module {
        public $slug       = 'sglb_annuaire';
        public $vb_support = 'off'; 

        public function init() {
            $this->name = 'SGLB - Annuaire Dynamique';
        }

        public function get_fields() {
            return array(
                'posts_number' => array(
                    'label'           => 'Nombre de fiches',
                    'type'            => 'text',
                    'default'         => '4',
                ),
                'category_slug' => array(
                    'label'           => 'Slug de la catégorie',
                    'type'            => 'text',
                ),
            );
        }

        public function render( $attrs, $content = null, $render_slug ) {
            $count = !empty($this->props['posts_number']) ? (int) $this->props['posts_number'] : 4;
            $cat   = $this->props['category_slug'];

            // Configuration des labels
            $labels_text = ['pepite'=>'Pépite', 'tested'=>'Testé par la rédaction', 'spot'=>'Spot du moment', 'highlight'=>'Mise en avant'];
            $label_colors = ['pepite'=>'#8e44ad', 'tested'=>'#27ae60', 'spot'=>'#e67e22', 'highlight'=>'#e74c3c'];

            $args = array(
                'post_type'      => 'sglb_listing',
                'posts_per_page' => $count,
                'post_status'    => 'publish',
            );

            if ( ! empty( $cat ) ) {
                $args['tax_query'] = array( array( 'taxonomy' => 'sglb_listing_cat', 'field' => 'slug', 'terms' => $cat ) );
            } else {
                $args['meta_query'] = array( array( 'key' => '_sglb_featured', 'value' => '1', 'compare' => '=' ) );
            }

            $query = new WP_Query( $args );
            if ( ! $query->have_posts() ) return '';

            $total_found = $query->post_count;
            $is_slider   = ( $count > 3 && $total_found > 3 );
            
            // On définit les colonnes pour la grille (1, 2 ou 3)
            $grid_columns = $is_slider ? 0 : $total_found;

            ob_start(); ?>
            <div class="sglb-annuaire-full-width" style="overflow: hidden; width: 100%;">
                
                <?php if ( $is_slider ) : ?>
                    <div class="swiper-container-annuaire swiper-container">
                        <div class="swiper-wrapper">
                            <?php while ( $query->have_posts() ) : $query->the_post(); 
                                $p_id = get_the_ID();
                                $slug_label = get_post_meta($p_id, '_sglb_label', true);
                                $color_label = $label_colors[$slug_label] ?? '#1e398c';
                            ?>
                                <div class="swiper-slide">
                                    <article class="sglb-listing-card">
                                        <a href="<?php the_permalink(); ?>" style="display:block; text-decoration:none; color:inherit;">
                                            <div class="card-image">
                                                <?php the_post_thumbnail('medium_large'); ?>
                                                <span class="card-badge-top" style="background-color: <?php echo esc_attr($color_label); ?>;">
                                                    <?php echo esc_html($labels_text[$slug_label] ?? 'À découvrir'); ?>
                                                </span>
                                            </div>
                                            <div class="card-content">
                                                <h3 class="card-title"><?php the_title(); ?></h3>
                                                <p class="card-location"><?php echo esc_html(get_post_meta($p_id, 'sglb_address', true)); ?></p>
                                            </div>
                                        </a>
                                    </article>
                                </div>
                            <?php endwhile; ?>
                        </div>
                        <div class="swiper-button-next"></div>
                        <div class="swiper-button-prev"></div>
                    </div>

                    <script>
                        (function($) {
                            $(document).ready(function() {
                                new Swiper('.swiper-container-annuaire', {
                                    slidesPerView: 1, spaceBetween: 30, // Espace pour le slider
                                    navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev' },
                                    breakpoints: { 768: { slidesPerView: 2 }, 1024: { slidesPerView: 3 } }
                                });
                            });
                        })(jQuery);
                    </script>

                <?php else : ?>
                    <div class="sglb-static-grid" style="display: grid; gap: 20px; grid-template-columns: repeat(<?php echo $grid_columns; ?>, 1fr); width: 100%;">
                        <?php while ( $query->have_posts() ) : $query->the_post(); 
                            $p_id = get_the_ID();
                            $slug_label = get_post_meta($p_id, '_sglb_label', true);
                            $color_label = $label_colors[$slug_label] ?? '#1e398c';
                        ?>
                            <article class="sglb-listing-card">
                                <a href="<?php the_permalink(); ?>" style="display:block; text-decoration:none; color:inherit;">
                                    <div class="card-image">
                                        <?php the_post_thumbnail('medium_large'); ?>
                                        <span class="card-badge-top" style="background-color: <?php echo esc_attr($color_label); ?>;">
                                            <?php echo esc_html($labels_text[$slug_label] ?? 'À découvrir'); ?>
                                        </span>
                                    </div>
                                    <div class="card-content">
                                        <h3 class="card-title"><?php the_title(); ?></h3>
                                        <p class="card-location"><?php echo esc_html(get_post_meta($p_id, 'sglb_address', true)); ?></p>
                                    </div>
                                </a>
                            </article>
                        <?php endwhile; ?>
                    </div>
                <?php endif; wp_reset_postdata(); ?>

            </div>

            <style>
                /* Style responsive pour la grille statique */
                @media (max-width: 980px) {
                    .sglb-static-grid { grid-template-columns: repeat(2, 1fr) !important; }
                }
                @media (max-width: 767px) {
                    .sglb-static-grid { grid-template-columns: 1fr !important; }
                }
            </style>
            <?php return ob_get_clean();
        }
    }
    new SGLB_Module_Annuaire;
}
add_action( 'et_builder_ready', 'sglb_register_annuaire_module' );