<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: print_options.class.php,v 1.1.2.3 2026/07/10 12:41:33 dgoron Exp $

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

class print_options {

    protected static $print_options = [];
    
    public static function get_uid_group($name)
    {
        switch ($name) {
            case 'number':
                return 'print_select_record';
            default:
                return 'print_'.$name.'_title';
        }
    }
    
    public static function get_legend_label($name)
    {
        global $msg;
        
        switch ($name) {
            case 'number':
                return $msg['print_select_record'];
            default:
                return $msg['print_'.$name.'_title'];
        }
    }
    
    public static function get_display_label_output($node_id, $value)
    {
        global $msg;
        
        switch ($node_id) {
            case 'outt':
                return $msg["print_output_writer"];
            case 'oute':
                return $msg["print_email"];
            default:
                return $msg["print_output_".$value];
        }
    }
    
    public static function get_display_element_output($node_id, $value)
    {
        global $charset;
        
        return "<div class='row'><input type='radio' name='output' id='".$node_id."' onClick='sel_part_gestion();' value='".htmlentities($value, ENT_QUOTES, $charset)."' " . (static::$print_options[$node_id] ? ' checked ' : '') . "/>
            <label for='".$node_id."'>&nbsp;".static::get_display_label_output($node_id, $value)."</label></div>";
    }
    
    public static function get_display_content_group_output()
    {
        global $opac_print_email, $empr_mail, $opac_print_email_recipients;
        global $opac_print_explnum;
        global $opac_rgaa_active;
        
        $display = static::get_display_element_output('outp', 'printer');
        $display .= static::get_display_element_output('pdf', 'pdf');
        $display .= static::get_display_element_output('outt', 'tt');
        
        // désactivation de l'envoi si le paramètre email_recipients est à 0 en étant déconnecté
        if (
            (($opac_print_email == 1) || ($opac_print_email == 2 && !empty($empr_mail))) &&
            $opac_print_email_recipients ||
            (!$opac_print_email_recipients && $_SESSION['id_empr_session'])) {
            $display .= static::get_display_element_output('oute', 'email');
        }
        if ($opac_print_explnum) {
            $display .= static::get_display_element_output('docnum', 'docnum');
        } else {
            // On conserve un champ caché pour éviter les erreurs javascript
            $display .= "<input type='hidden' id='docnum' value='0'/>";
        }
        if (!$opac_rgaa_active) {
            $display .= "
            <hr>
            &nbsp;&nbsp;";
        }
        return $display;
    }
    
    public static function get_display_element_number($node_id, $value)
    {
        global $msg, $charset;

        return "<div class='row'><input type='radio' name='number' onClick ='sel_part_gestion();' value='".htmlentities($value, ENT_QUOTES, $charset)."' id='".$node_id."' " . (static::$print_options[$node_id] ? ' checked ' : '') . "/>
			    <label for='".$node_id."'>&nbsp;{$msg["print_".$node_id."_records"]}</label></div>";
    }
    
    public static function get_display_content_group_number()
    {
        $display = static::get_display_element_number('all', '0');
        $display .= static::get_display_element_number('selected', '1');
        return $display;
    }
    
    public static function get_selector_notice_tpl($selected=0)
    {
        return notice_tpl_gen::gen_tpl_select("notice_tpl", $selected, "
		var div_sel = document.getElementById('sel_notice_tpl');
		var div_sel2 = document.getElementById('sel_notice_tpl2');
		var notice_tpl = document.getElementById('notice_tpl');
		var sel = notice_tpl.options[notice_tpl.selectedIndex].value;
		if (sel > 0) {
			div_sel.style.display = 'none';
			div_sel2.style.display = 'none';
		} else {
			div_sel.style.display = 'block';
			div_sel2.style.display = 'block';
        }");
    }
    
    public static function get_display_element_notice_tpl($selected=0)
    {
        global $msg, $charset;
        
        $display = "<div class='row'>";
        $display .= "<label class='visually-hidden'>".htmlentities($msg['print_type_sel_notice_tpl'], ENT_QUOTES, $charset)."</label>";
        $display .= static::get_selector_notice_tpl($selected);
        $display .= "</div>";
        return $display;
    }
    
    public static function get_display_element_type($node_id, $value)
    {
        global $msg, $charset;
        
        return "<div class='row'><input type='radio' name='type' value='".htmlentities($value, ENT_QUOTES, $charset)."' id='".$node_id."' " . (static::$print_options[$node_id] ? ' checked ' : '') . "/>
			    <label for='".$node_id."'>&nbsp;{$msg["print_type_".$node_id]}</label></div>";
    }
    
    public static function get_display_content_group_type()
    {
        global $opac_print_template_default;
        
        if ($opac_print_template_default) {
            $selected = $opac_print_template_default;
        } else {
            $selected = 0;
        }
        $display = static::get_display_element_notice_tpl($selected);
        $display .= "<div id='sel_notice_tpl' " . ($selected > 0 ? "style='display:none;'" : "style='display:block;'") . ">";
        $display .= static::get_display_element_type('isbd', 'ISBD');
        $display .= static::get_display_element_type('public', 'PUBLIC');
        $display .= "</div>";
        return $display;
    }
    
    public static function get_display_label_short($node_id)
    {
        global $msg;
        
        switch ($node_id) {
            case 's1':
                return $msg["print_short_format"];
            case 's0':
                return $msg["print_long_format"];
            default:
                return '';
        }
    }
    
    public static function get_display_element_short($node_id, $value)
    {
        global $charset;
        
        return "<div class='row'><input type='radio' name='short' value='".htmlentities($value, ENT_QUOTES, $charset)."' id='".$node_id."' " . (static::$print_options[$node_id] ? ' checked ' : '') . "/>
			    <label for='".$node_id."'>&nbsp;".static::get_display_label_short($node_id)."</label></div>";
    }
    
    public static function get_display_element_header()
    {
        global $msg;
        return "<div class='row'><input type='checkbox' name='header' id='header' value='1' " . (static::$print_options['header'] ? ' checked ' : '') . "/>&nbsp;
					<label for='header'>{$msg["print_header"]}</label></div>";
    }
    
    public static function get_display_element_vignette()
    {
        global $msg;
        return "<div class='row'><input type='checkbox' name='vignette' id='vignette' value='1' " . (static::$print_options['vignette'] ? ' checked ' : '') . "/>&nbsp;
					<label for='vignette'>{$msg["print_vignette"]}</label></div>";
    }
    
    public static function get_display_content_group_format()
    {
        $display = static::get_display_element_short('s1', '1');
        $display .= static::get_display_element_short('s0', '0');
        $display .= static::get_display_element_header();
        $display .= static::get_display_element_vignette();
        return $display;
    }
    
    public static function get_display_label_ex($node_id)
    {
        global $msg;
        
        switch ($node_id) {
            case 'ex1':
                return $msg["print_ex"];
            case 'ex0':
                return $msg["print_no_ex"];
        }
    }
    
    public static function get_display_element_ex($node_id, $value)
    {
        global $msg, $charset;
        global $opac_print_expl_default;
        
        switch ($node_id) {
            case 'ex1':
                $checked = ($opac_print_expl_default ? "checked" : "");
                break;
            case 'ex0':
                $checked = ($opac_print_expl_default ? "" : "checked");
                break;
        }
        return "<div class='row'><input type='radio' name='ex' value='".htmlentities($value, ENT_QUOTES, $charset)."' id='".$node_id."' {$checked}/>
			    <label for='".$node_id."'>&nbsp;".static::get_display_label_ex($node_id)."</label></div>";
    }
    
    public static function get_display_content_group_ex()
    {
        $display = static::get_display_element_ex('ex1', '1');
        $display .= static::get_display_element_ex('ex0', '0');
        return $display;
    }
    
    public static function get_display_label_full_records($node_id)
    {
        global $msg;
        
        switch ($node_id) {
            case 'full_records1':
                return $msg['print_full_records_yes'];
            case 'full_records0':
                return $msg['print_full_records_no'];
            default:
                return '';
        }
    }
    
    public static function get_display_element_full_records($node_id, $value)
    {
        global $charset;
        
        return "<div class='row'><input type='radio' name='full_records' id='".$node_id."' value='".htmlentities($value, ENT_QUOTES, $charset)."' " . (static::$print_options[$node_id] ? ' checked ' : '') . "/>
                <label for='".$node_id."'>&nbsp;".static::get_display_label_full_records($node_id)."</label></div>";
    }
    
    public static function get_display_content_group_full_records()
    {
        $display = static::get_display_element_full_records('full_records1', '1');
        $display .= static::get_display_element_full_records('full_records0', '0');
        return $display;
    }
    
    public static function get_display_content_group($name)
    {
        switch ($name) {
            case 'output':
                return static::get_display_content_group_output();
            case 'number':
                return static::get_display_content_group_number();
            case 'type':
                return static::get_display_content_group_type();
            case 'format':
                return static::get_display_content_group_format();
            case 'ex':
                return static::get_display_content_group_ex();
            case 'full_records':
                return static::get_display_content_group_full_records();
        }
    }
    
    public static function get_display_group($name)
    {
        global $charset;
        global $opac_rgaa_active;
        
        $uid = static::get_uid_group($name);
        $legend_label = static::get_legend_label($name);
        
        if ($opac_rgaa_active) {
            return "<fieldset class='print-options-fieldset'>
            <legend>". htmlentities($legend_label, ENT_QUOTES, $charset) ."</legend>
                ".static::get_display_content_group($name)."
            </fieldset>";
        } else {
            return "<b id='".$uid."' role='presentation' style='font-weight: bold'>". htmlentities($legend_label, ENT_QUOTES, $charset) ."</b>
                <blockquote role='group' aria-labelledby='".$uid."'>
                ".static::get_display_content_group($name)."
            	</blockquote>";
        }
    }
    
    public static function set_print_options($print_options) {
        static::$print_options = $print_options;
    }
}
