<?php

$startTime = microtime( true );
header( 'content-type: text/html; charset=utf-8' );

if (! isset($_GET['gMonat']) && ! isset($_GET['gJahr']) ) {
    header ( 'Expires: ' . date('r', mktime(0,0,0, date('m'), 1+date('d'), date('y') ) ) );
    require_once 'lib/FtxMondPhase.php';
    require_once 'lib/FtxSonne.php';
}
require_once 'feiertage.php';

if ( ! isset( $_GET['jahr'] ) ) { $_GET['jahr'] = date('Y'); }
$jahr   = intval($_GET['jahr']);
$gJahr  = isset($_GET['gJahr']);

if ( ! isset( $_GET['monat'] ) ) { $_GET['monat'] = date('m') ; }
$monat  = intval($_GET['monat']);
$gMonat = isset($_GET['gMonat']);

if ( ! isset( $_GET['tag'] ) ) { $_GET['tag'] = date('d'); }
$tag    = intval($_GET['tag'] );

$cJahr='';
$cMonat='';

if ( $gJahr ) {
    $iStart = 1;
    $gMonat = 0;
    $iTage = 365 + date('L', mktime(0,0,0, 1,1,$jahr) );
    $cJahr  = 'checked';
    $monat = 1;
    $tag = 1;

} elseif ( $gMonat ) {
    $iStart = 1;
    $iTage = date( 't', mktime(0, 0, 0, $monat, 1, $jahr ) );
    $cMonat = 'checked';
    $tag = 1;
} else {
    $iStart = $tag;
    $iTage = $iStart;
}

?><!doctype html>
<html>
    <head>
        <title>Kalender-Test</title>
        <style>
            html,body {margin:0; padding:0;background-color:#fafafa;}
            h1 {margin-top:0;padding:.45rem .75rem .45rem .75rem;background-color:#ffb300;border-bottom: 3px solid gray; font-size:1.5rem; color:999; }
            #content {padding:0 1em 0 1rem;}
            #Ausgabe .codes {display:none;}
            #Formular {display:none; border:1px solid gray;padding:1rem;}
            #Formular:target {display:block;}
            ul { margin-bottom: 1rem;}
            #Ausgabe > ul > li { margin-bottom: 1rem;}
            .datum {font-weight:bold;}

        </style>
    </head>
    <body>
        <h1>fastix.org: Schneller PHP-Kalender</h1>
        <div id='content'>
            <section id="Formular">
                <form action="<?=str_replace( '?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI'] );?>">
                    <p>
                        <label style="display:inline-block;width:5em" for="jahr">Jahr: </label><input id="jahr" value="<?=intval($_GET['jahr']);?>" type="number" min="1900" max="2300" style="width:4em" name="jahr" />
                        <input type="checkbox" id="gJahr" name="gJahr" <?=$cJahr;?> /> <label for="gJahr"> ganzes Jahr</label><br />
                    <p /><p>
                        <label style="display:inline-block;width:5em" for="monat">Monat: </label><input id="monat" value="<?=intval($_GET['monat']);?>" type="number" min="1" max="12" style="width:4em" name="monat" />
                            <input type="checkbox" id="gMonat" name="gMonat" <?=$cMonat;?> /> <label for="gMonat"> ganzer Monat</label><br />
                    <p /><p>
                        <label style="display:inline-block;width:5em" for="tag">Tag: </label><input id="tag" value="<?=intval($_GET['tag']);?>" type="number" min="1" max="31" style="width:4em" name="tag" /><br />
                    <p />
                        <input type="checkbox" name="show[BRD]" id="BRD"<?php if(isset($_GET['show']['BRD'])){echo  " checked  "; } ?>/> <label for="BRD">Zeige alle bundesweite, gesetzliche Feiertage</label>
                        <br>
                        <input type="checkbox" name="show[BRD_HF]" id="BRD_HF"<?php if(isset($_GET['show']['BRD_HF'])){echo  " checked  "; } ?>/> <label for="BRD_HF">Zeige die <q>Halbfeiertage</q> (24. Dezember, 31. Dezember)</label>
                    </p>
                    <p>Zeige zusätzlich:</p>
                    <p>
                        <input type="checkbox" name="show[BRD_BB]" id="BRD_BB"<?php if(isset($_GET['show']['BRD_BB'])){echo  " checked  "; } ?>/> <label for="BRD_BB">die Feiertage in Brandenburg</label>,<br>
                        <input type="checkbox" name="show[BRD_BL]" id="BRD_BL"<?php if(isset($_GET['show']['BRD_BL'])){echo  " checked  "; } ?>/> <label for="BRD_BL">die Feiertage in Berlin</label>,<br>
                        <input type="checkbox" name="show[BRD_BW]" id="BRD_BW"<?php if(isset($_GET['show']['BRD_BW'])){echo  " checked  "; } ?>/> <label for="BRD_BW">die Feiertage in Baden-Württemberg</label>,<br>
                        <input type="checkbox" name="show[BRD_BY]" id="BRD_BY"<?php if(isset($_GET['show']['BRD_BY'])){echo  " checked  "; } ?>/> <label for="BRD_BY">die Feiertage in Bayern</label>,<br>
                        <input type="checkbox" name="show[BRD_BYKAT]" id="BRD_BYKAT"<?php if(isset($_GET['show']['BRD_BYKAT'])){echo  " checked  "; } ?>/> <label for="BRD_BYKAT">die Feiertage in Bayern (nur überwiegend katholische Gemeinden)</label>,<br>
                        <input type="checkbox" name="show[BRD_AUGSB]" id="BRD_AUGSB"<?php if(isset($_GET['show']['BRD_AUGSB'])){echo  " checked  "; } ?>/> <label for="BRD_AUGSB">die Feiertage in der Stadt Augsburg</label>,<br>
                        <input type="checkbox" name="show[BRD_HB]" id="BRD_HB"<?php if(isset($_GET['show']['BRD_HB'])){echo  " checked  "; } ?>/> <label for="BRD_HB">die Feiertage in Bremen</label>,<br>
                        <input type="checkbox" name="show[BRD_HE]" id="BRD_HE"<?php if(isset($_GET['show']['BRD_HE'])){echo  " checked  "; } ?>/> <label for="BRD_HE">die Feiertage in Hessen</label>,<br>
                        <input type="checkbox" name="show[BRD_HH]" id="BRD_HH"<?php if(isset($_GET['show']['BRD_HH'])){echo  " checked  "; } ?>/> <label for="BRD_HH">die Feiertage in Hamburg</label>,<br>
                        <input type="checkbox" name="show[BRD_MV]" id="BRD_MV"<?php if(isset($_GET['show']['BRD_MV'])){echo  " checked  "; } ?>/> <label for="BRD_MV">die Feiertage in Mecklenburg-Vorpommern</label>,<br>
                        <input type="checkbox" name="show[BRD_NI]" id="BRD_NI"<?php if(isset($_GET['show']['BRD_NI'])){echo  " checked  "; } ?>/> <label for="BRD_NI">die Feiertage in Niedersachsen</label>,<br>
                        <input type="checkbox" name="show[BRD_NW]" id="BRD_NW"<?php if(isset($_GET['show']['BRD_NW'])){echo  " checked  "; } ?>/> <label for="BRD_NW">die Feiertage in Nordrhein-Westfalen</label>,<br>
                        <input type="checkbox" name="show[BRD_RP]" id="BRD_RP"<?php if(isset($_GET['show']['BRD_RP'])){echo  " checked  "; } ?>/> <label for="BRD_RP">die Feiertage in Rheinland-Pfalz</label>,<br>
                        <input type="checkbox" name="show[BRD_SA]" id="BRD_SA"<?php if(isset($_GET['show']['BRD_SA'])){echo  " checked  "; } ?>/> <label for="BRD_SA">die Feiertage im Saarland</label>,<br>
                        <input type="checkbox" name="show[BRD_SH]" id="BRD_SH"<?php if(isset($_GET['show']['BRD_SH'])){echo  " checked  "; } ?>/> <label for="BRD_SH">die Feiertage in Schleswig-Holstein</label>,<br>
                        <input type="checkbox" name="show[BRD_SN]" id="BRD_SN"<?php if(isset($_GET['show']['BRD_SN'])){echo  " checked  "; } ?>/> <label for="BRD_SN">die Feiertage in Sachsen</label>,<br>
                        <input type="checkbox" name="show[BRD_TH]" id="BRD_TH"<?php if(isset($_GET['show']['BRD_TH'])){echo  " checked  "; } ?>/> <label for="BRD_TH">die Feiertage in Thüringen</label>,<br>
                        <input type="checkbox" name="show[BRD_THKAT]" id="BRD_THKAT"<?php if(isset($_GET['show']['BRD_THKAT'])){echo  " checked  "; } ?>/> <label for="BRD_THKAT">die Feiertage in Thüringen (nur einige katholische Gemeinden)</label>,<br>
                    </p>
                    <button>Ereignisse anzeigen</button>
                </form>
            </section>

            <section id="Ausgabe">
                <h2>Ergebnisse der <a href='#Formular'>Kalenderabfrage</a></h2>
<?php
if ( ! isset ( $_GET['show'] ) ) {
    $mustHave = false;
} else {
    $mustHave=array();
    if ( isset( $_GET['show'] ) ) {
        foreach ( array_keys( $_GET['show'] ) as $k ) {
            if ('' != trim( $k ) && '' != trim( $_GET['show'][$k] ) ) {
                $mustHave[] = $k;
            }
        }
    }
}
$o = new feiertage( $jahr );
$o -> setArrShowOnlyTypes( $mustHave );

#if (! isset($_GET['gMonat']) && ! isset($_GET['gJahr']) ) {
#    $sonne = new FtxSonne;
#    $sonne -> setOrt('Kassel');
#}
echo "<ul>";
for ( $i = $iStart; $i <= $iTage; $i++ ) {
    $dto = mktime( 0, 0, 0, $monat, $i, $jahr );
    list( $w, $y,$m, $d ) = explode( '-', date( 'w-Y-m-d', $dto ) );

    $ret = $o -> getDescriptionPerMD ($m, $d );
    if ( $ret ) {
        foreach ( $ret as $r ) {
            echo '                        <li><span class="datum">',$o->getWdName($w), ', ',  $d, '. ', $o->getMonthName($m), ' ', $o -> getJahr(), ':</span> ' , $r['name'], PHP_EOL;
            if ( count( $r['codes'] ) ) {
                echo '                    <ul>', PHP_EOL;
            }
            foreach ( $r['codes'] as $code ) {
                if ( $code ) {
                    echo '                        <li><span class="codes">', $code , ' : </span><span class="description">',  $o -> codes[$code], "<span></li>", PHP_EOL;
                }
            }
            if (count($r['codes'])) {
                echo '                        </ul>', PHP_EOL;
            }
            echo '                        </li>', PHP_EOL;
        }
    } else {
        if (! isset($_GET['gJahr']) and ! isset($_GET['gMonat']) ) {
            echo '                        <li><span class="datum">',$o->getWdName($w), ', ',  $d, '. ', $o->getMonthName($m), ' ', $o -> getJahr(), ':</span> ' , 'Kein Ereignis gespeichert.', "</li>", PHP_EOL;
        }
    }
    /*
    if (! isset($_GET['gMonat']) && ! isset($_GET['gJahr']) ) {
        $sonne -> setDate($d,$m,$jahr);
        echo '                    <ul>', PHP_EOL;
        echo '                      <li><strong>Sonnenaufgang in Kassel:</strong> ' . $sonne -> getSunrice('H:i') . ' </li>', PHP_EOL;
        echo '                      <li><strong>Sonnenuntergang:</strong> ' . $sonne -> getSunset('H:i') . '</li>', PHP_EOL;
        echo '                      <li>' . FtxMondPhase ($d, $m, $jahr) . '</li>', PHP_EOL;
        echo '                    </ul>', PHP_EOL;
    }
    */
}
?>

                    </ul>
                    <hr>
                    <ul>
                        <li><a href="#Formular">Zum Abfrageformular</a></li>
                    </ul>
                    <ul>
                        <li><strong>Systeminformationen:</strong><ul>
                        <li>Gebraut in <?=round( ( microtime( true ) - $startTime ) * 1000,0 );?> ms</li>
                        <li>Speicher: <?=round( memory_get_usage() / 1024 )?> KiloBytes (für Programm und <?=$o->countKalendarItems();?> Ereignisse) </li>
                        <li>Gebraut mit PHP, Version <?=phpversion ();?></li>
                        <li><a href="https://code.fastix.org/Projekte/PHP%3AFeiertage/">Quelltext und Projektbeschreibung</a></li>
                    </ul></li>
                </ul>
            </section>
        </div>
    </body>
</html>