<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: options_multimedia.class.php,v 1.1.4.3 2026/06/22 13:15:24 rtigero Exp $

if (stristr($_SERVER['REQUEST_URI'], ".class.php")) die("no access");

global $class_path;
require_once($class_path . "/options/options.class.php");

class options_multimedia extends options
{

    public function init_default_parameters()
    {
        parent::init_default_parameters();

        // Type de media
        $this->parameters['MEDIA_TYPE'][0]['value'] = 'other';

        // Paramètres de base pour l'iframe
        $this->parameters['WIDTH'][0]['value'] = '560';
        $this->parameters['HEIGHT'][0]['value'] = '315';

        // Paramètres YouTube (pour type vidéo)
        $this->parameters['AUTOPLAY'][0]['value'] = '0';
        $this->parameters['CONTROLS'][0]['value'] = '1';
        $this->parameters['LOOP'][0]['value'] = '0';
        $this->parameters['MUTE'][0]['value'] = '0';
        $this->parameters['REL'][0]['value'] = '1';
        $this->parameters['SHOWINFO'][0]['value'] = '1';
        $this->parameters['FS'][0]['value'] = '1';
        $this->parameters['CC_LOAD_POLICY'][0]['value'] = '0';
        $this->parameters['IV_LOAD_POLICY'][0]['value'] = '1';
        $this->parameters['START'][0]['value'] = '0';
        $this->parameters['END'][0]['value'] = '0';
        $this->parameters['PLAYLIST'][0]['value'] = '';

        // Paramètres pour type carte
        $this->parameters['MAP_ZOOM'][0]['value'] = '10';
        $this->parameters['MAP_LAT'][0]['value'] = '';
        $this->parameters['MAP_LNG'][0]['value'] = '';
        $this->parameters['MAP_TYPE'][0]['value'] = 'roadmap';

        // Paramètres pour type audio
        $this->parameters['AUDIO_CONTROLS'][0]['value'] = '1';
        $this->parameters['AUDIO_AUTOPLAY'][0]['value'] = '0';
        $this->parameters['AUDIO_LOOP'][0]['value'] = '0';

        // Paramètres hérités
        $this->parameters['REPEATABLE'][0]['value'] = '';
        $this->parameters['ISHTML'][0]['value'] = '1';
    }

    public function get_content_form()
    {
        global $msg, $charset;

        $content_form = "
        <script>
        function updateMultimediaForm() {
            var mediaType = document.getElementById('MEDIA_TYPE').value;
            var videoSection = document.getElementById('video_specific_fields');
            var audioSection = document.getElementById('audio_specific_fields');
            var mapSection = document.getElementById('map_specific_fields');

            // Masquer toutes les sections spécifiques par défaut
            if (videoSection) videoSection.style.display = 'none';
            if (audioSection) audioSection.style.display = 'none';
            if (mapSection) mapSection.style.display = 'none';

            // Afficher la section correspondante selon le type
            if (mediaType === 'video') {
                // Pour vidéo : afficher seulement les champs vidéo
                if (videoSection) videoSection.style.display = 'block';
            } else if (mediaType === 'audio') {
                // Pour audio : afficher les champs vidéo (communs) ET les champs audio spécifiques
                if (audioSection) audioSection.style.display = 'block';
            } else if (mediaType === 'map') {
                // Pour carte : afficher seulement les champs carte
                if (mapSection) mapSection.style.display = 'block';
            }
            // Pour 'other' : ne rien afficher (seulement les champs communs)
        }

        // Initialiser au chargement
        document.addEventListener('DOMContentLoaded', function() {
            updateMultimediaForm();
        });
        </script>";

        // Sélecteur de type de média
        $content_form .= "<h3>" . $msg["parperso_multimedia_type"] . "</h3>";
        $content_form .= "<table>";
        $media_type_options = [
            'video' => $msg["parperso_multimedia_type_video"],
            'audio' => $msg["parperso_multimedia_type_audio"],
            'map' => $msg["parperso_multimedia_type_map"],
            'other' => $msg["parperso_multimedia_type_other"]
        ];

        $current_type = $this->parameters['MEDIA_TYPE'][0]['value'];
        $content_form .= "
        <tr>
            <td>" . $msg["parperso_multimedia_type"] . "</td>
            <td>
                <select id='MEDIA_TYPE' name='MEDIA_TYPE' onchange='updateMultimediaForm()'>";

        foreach ($media_type_options as $value => $label) {
            $selected = ($current_type == $value) ? "selected='selected'" : "";
            $content_form .= "<option value='" . $value . "' " . $selected . ">" . $label . "</option>";
        }

        $content_form .= "
                </select>
            </td>
        </tr>";
        $content_form .= "</table>";

        // Dimensions de l'iframe (commun)
        $content_form .= "<h3>" . $msg["parperso_multimedia_iframe_dimensions"] . "</h3>";
        $content_form .= "<table>";
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_width"], 'WIDTH', 'number');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_height"], 'HEIGHT', 'number');
        $content_form .= "</table>";

        // Champs spécifiques pour Vidéo/Audio
        $display_video = ($current_type === 'video') ? '' : "style='display:none'";
        $content_form .= "
        <div id='video_specific_fields' " . $display_video . ">
            <h3>" . $msg["parperso_multimedia_iframe_settings"] . "</h3>
            <h4>" . $msg["parperso_multimedia_video_settings"] . "</h4>
            <table>";

        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_autoplay"], 'AUTOPLAY', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_controls"], 'CONTROLS', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_loop"], 'LOOP', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_mute"], 'MUTE', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_rel"], 'REL', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_showinfo"], 'SHOWINFO', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_fs"], 'FS', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_cc_load_policy"], 'CC_LOAD_POLICY', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_iv_load_policy"], 'IV_LOAD_POLICY', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_start"], 'START', 'number');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_end"], 'END', 'number');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_playlist_id"], 'PLAYLIST', 'text');
        $content_form .= "</table>
        </div>";

        // Champs spécifiques pour Audio
        $display_audio = ($current_type === 'audio') ? '' : "style='display:none'";
        $content_form .= "
        <div id='audio_specific_fields' " . $display_audio . ">
            <h4>" . $msg["parperso_multimedia_audio_settings"] . "</h4>
            <table>";

        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_audio_controls"], 'AUDIO_CONTROLS', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_audio_autoplay"], 'AUDIO_AUTOPLAY', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_audio_loop"], 'AUDIO_LOOP', 'checkbox');
        $content_form .= "</table>
        </div>";

        // Champs spécifiques pour Carte
        $display_map = ($current_type === 'map') ? '' : "style='display:none'";
        $content_form .= "
        <div id='map_specific_fields' " . $display_map . ">
            <h3>" . $msg["parperso_multimedia_map_settings"] . "</h3>
            <table>";

        $map_type_options = ['roadmap', 'satellite', 'hybrid', 'terrain'];
        $current_map_type = $this->parameters['MAP_TYPE'][0]['value'];
        $content_form .= "
        <tr>
            <td>" . $msg["parperso_multimedia_map_type"] . "</td>
            <td>
                <select name='MAP_TYPE'>";

        foreach ($map_type_options as $map_type) {
            $selected = ($current_map_type == $map_type) ? "selected='selected'" : "";
            $content_form .= "<option value='" . $map_type . "' " . $selected . ">" . ucfirst($map_type) . "</option>";
        }

        $content_form .= "
                </select>
            </td>
        </tr>";

        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_map_zoom"], 'MAP_ZOOM', 'number');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_map_lat"], 'MAP_LAT', 'text');
        $content_form .= $this->get_line_content_form($msg["parperso_multimedia_map_lng"], 'MAP_LNG', 'text');
        $content_form .= "</table>
        </div>";

        // Paramètres hérités
        $content_form .= "<h3>" . $msg["parperso_multimedia_advanced_settings"] . "</h3>";
        $content_form .= "<table>";
        $content_form .= $this->get_line_content_form($msg["persofield_textrepeat"], 'REPEATABLE', 'checkbox');
        $content_form .= $this->get_line_content_form($msg["persofield_textishtml"], 'ISHTML', 'checkbox');
        $content_form .= "</table>";

        return $content_form;
    }

    public function set_parameters_from_form()
    {
        global $WIDTH, $HEIGHT, $AUTOPLAY, $CONTROLS, $LOOP, $MUTE, $REL, $SHOWINFO, $FS, $CC_LOAD_POLICY, $IV_LOAD_POLICY, $START, $END, $PLAYLIST, $REPEATABLE, $ISHTML;
        global $MEDIA_TYPE;
        global $MAP_ZOOM, $MAP_LAT, $MAP_LNG, $MAP_TYPE;
        global $AUDIO_CONTROLS, $AUDIO_AUTOPLAY, $AUDIO_LOOP;

        parent::set_parameters_from_form();

        // Type de média
        $this->parameters['MEDIA_TYPE'][0]['value'] = $MEDIA_TYPE ? $MEDIA_TYPE : 'other';

        // Dimensions
        $this->parameters['WIDTH'][0]['value'] = intval($WIDTH);
        $this->parameters['HEIGHT'][0]['value'] = intval($HEIGHT);

        // Paramètres YouTube (checkbox)
        $this->parameters['AUTOPLAY'][0]['value'] = $AUTOPLAY ? 1 : 0;
        $this->parameters['CONTROLS'][0]['value'] = $CONTROLS ? 1 : 0;
        $this->parameters['LOOP'][0]['value'] = $LOOP ? 1 : 0;
        $this->parameters['MUTE'][0]['value'] = $MUTE ? 1 : 0;
        $this->parameters['REL'][0]['value'] = $REL ? 1 : 0;
        $this->parameters['SHOWINFO'][0]['value'] = $SHOWINFO ? 1 : 0;
        $this->parameters['FS'][0]['value'] = $FS ? 1 : 0;
        $this->parameters['CC_LOAD_POLICY'][0]['value'] = $CC_LOAD_POLICY ? 1 : 0;
        $this->parameters['IV_LOAD_POLICY'][0]['value'] = $IV_LOAD_POLICY ? 1 : 0;

        // Paramètres numériques
        $this->parameters['START'][0]['value'] = intval($START);
        $this->parameters['END'][0]['value'] = intval($END);

        // Paramètres textuels
        $this->parameters['PLAYLIST'][0]['value'] = stripslashes($PLAYLIST);

        // Paramètres pour type carte
        $this->parameters['MAP_ZOOM'][0]['value'] = intval($MAP_ZOOM);
        $this->parameters['MAP_LAT'][0]['value'] = stripslashes($MAP_LAT);
        $this->parameters['MAP_LNG'][0]['value'] = stripslashes($MAP_LNG);
        $this->parameters['MAP_TYPE'][0]['value'] = $MAP_TYPE ? $MAP_TYPE : 'roadmap';

        // Paramètres pour type audio
        $this->parameters['AUDIO_CONTROLS'][0]['value'] = $AUDIO_CONTROLS ? 1 : 0;
        $this->parameters['AUDIO_AUTOPLAY'][0]['value'] = $AUDIO_AUTOPLAY ? 1 : 0;
        $this->parameters['AUDIO_LOOP'][0]['value'] = $AUDIO_LOOP ? 1 : 0;

        // Paramètres hérités
        $this->parameters['REPEATABLE'][0]['value'] = $REPEATABLE ? 1 : 0;
        $this->parameters['ISHTML'][0]['value'] = $ISHTML ? 1 : 0;
    }
}
