<?php
#############################################################
# Project:      bCounter                                    #
# File:         weeks.php                                   #
# Decription:   week statistics
#                                                           #
# Copyright (c) Alexander Bondar (http://bondar.de/)        #
#############################################################

    $strTitle .= ' | Wochenstatistiken';
    $bShowInfo = false;

    $strDateToday = date("d.m.Y");

    $arrSums = array(
        "mo" => 0,
        "tu" => 0,
        "we" => 0,
        "th" => 0,
        "fr" => 0,
        "sa" => 0,
        "su" => 0,
        "sum" => 0,
        "weeks" => 0
    );

    $iFirstWeek = mysql_result( $bCounterDB->query( "SELECT MIN(week) FROM ".$bCounter_dbPrefix."visits_per_day" ), 0, 0);
    $iLastWeek  = mysql_result( $bCounterDB->query( "SELECT MAX(week) FROM ".$bCounter_dbPrefix."visits_per_day" ), 0, 0);

    $strSQLstring = "SELECT *, (mo + tu + we + th + fr + sa + su) AS summe FROM ".$bCounter_dbPrefix."visits_per_day ORDER BY week DESC";
    $SQLresult = $bCounterDB->query($strSQLstring);

    $strSQLstring = "SELECT
        MIN(mo) AS min_mo, MAX(mo) AS max_mo, AVG(mo) AS avg_mo,
        MIN(tu) AS min_tu, MAX(tu) AS max_tu, AVG(tu) AS avg_tu,
        MIN(we) AS min_we, MAX(we) AS max_we, AVG(we) AS avg_we,
        MIN(th) AS min_th, MAX(th) AS max_th, AVG(th) AS avg_th,
        MIN(fr) AS min_fr, MAX(fr) AS max_fr, AVG(fr) AS avg_fr,
        MIN(sa) AS min_sa, MAX(sa) AS max_sa, AVG(sa) AS avg_sa,
        MIN(su) AS min_su, MAX(su) AS max_su, AVG(su) AS avg_su,
        MIN(mo + tu + we + th + fr + sa + su) AS min_sum, MAX(mo + tu + we + th + fr + sa + su) AS max_sum
        FROM ".$bCounter_dbPrefix."visits_per_day
        WHERE week NOT IN ( $iFirstWeek, $iLastWeek );
        ";
    $SQLresult_minmax = $bCounterDB->query($strSQLstring);

    $iGlobalMin = min(
        mysql_result($SQLresult_minmax, 0, "min_mo"),
        mysql_result($SQLresult_minmax, 0, "min_tu"),
        mysql_result($SQLresult_minmax, 0, "min_we"),
        mysql_result($SQLresult_minmax, 0, "min_th"),
        mysql_result($SQLresult_minmax, 0, "min_fr"),
        mysql_result($SQLresult_minmax, 0, "min_sa"),
        mysql_result($SQLresult_minmax, 0, "min_su")
    );


    $iGlobalMax = max(
        mysql_result($SQLresult_minmax, 0, "max_mo"),
        mysql_result($SQLresult_minmax, 0, "max_tu"),
        mysql_result($SQLresult_minmax, 0, "max_we"),
        mysql_result($SQLresult_minmax, 0, "max_th"),
        mysql_result($SQLresult_minmax, 0, "max_fr"),
        mysql_result($SQLresult_minmax, 0, "max_sa"),
        mysql_result($SQLresult_minmax, 0, "max_su")
    );

    $strDivBody .= '<h2>&Uuml;bersicht</h2>'."\n";
    $strDivBody .= '<table>'."\n";
    $strDivBody .= '<tr><th>Jahr, Wo.</th><th>Mo</th><th>Di</th><th>Mi</th><th>Do</th><th>Fr</th><th>Sa</th><th>So</th><th>&sum;</th><th>&#8960;/Tag</th></tr>'."\n";

    $iGlobalMinSum = mysql_result($SQLresult_minmax, 0, "min_sum");
    $iGlobalMaxSum = mysql_result($SQLresult_minmax, 0, "max_sum");

    function get_CSSclass_weeks_td ( $iVisitors, $iMin = 0, $iMax = 0 ) {
        global $iGlobalMin, $iGlobalMax;
        if ( $iMin == 0 && $iMax == 0 ) {
            $iMin = $iGlobalMin;
            $iMax = $iGlobalMax;
        }

        $iTmp = zahl( 100*( $iVisitors / max( $iMax - $iMin, 1 ) ), 0 );
        if ( $iTmp > 80 ) {
            return "bcounter-visitors-upto100percent";
        }elseif ( $iTmp > 60 ) {
            return "bcounter-visitors-upto80percent";
        }elseif ( $iTmp > 40 ) {
            return "bcounter-visitors-upto60percent";
        }elseif ( $iTmp > 20 ) {
            return "bcounter-visitors-upto40percent";
        }else {
            return "bcounter-visitors-upto20percent";
        }
    }


    for ( $i = 0; $i < mysql_num_rows($SQLresult); $i++ ) {
        $strYearWeek = mysql_result($SQLresult, $i, "week");
        $iYear = substr($strYearWeek, 0, 4)+0;
        $iWeek = substr($strYearWeek, 4, 2)+0;
        $strDivBody .= '<tr>'."\n";


        $strDivBody .= '<td>'.$iYear.', '.$iWeek.'.</td>'."\n";
        $strDivBody .= '<td class="right';
        if ( $strDateToday == bCounter_getDateFromYearWeekDay($iYear, $iWeek, 1) ) {
            $strDivBody .= ' today';
        }
        $strDivBody .= ' '.get_CSSclass_weeks_td( mysql_result($SQLresult, $i, "mo") );
        $strDivBody .= '" title="Montag, '.bCounter_getDateFromYearWeekDay($iYear, $iWeek, 1).'">';
        $strDivBody .= zahl(mysql_result($SQLresult, $i, "mo")).'</td>'."\n";


        $strDivBody .= '<td class="right';
        if ( $strDateToday == bCounter_getDateFromYearWeekDay($iYear, $iWeek, 2) ) {
            $strDivBody .= ' today';
        }
        $strDivBody .= ' '.get_CSSclass_weeks_td( mysql_result($SQLresult, $i, "tu") );
        $strDivBody .= '" title="Dienstag, '.bCounter_getDateFromYearWeekDay($iYear, $iWeek, 2).'">';
        $strDivBody .= zahl(mysql_result($SQLresult, $i, "tu")).'</td>'."\n";


        $strDivBody .= '<td class="right';
        if ( $strDateToday == bCounter_getDateFromYearWeekDay($iYear, $iWeek, 3) ) {
            $strDivBody .= ' today';
        }
        $strDivBody .= ' '.get_CSSclass_weeks_td( mysql_result($SQLresult, $i, "we") );
        $strDivBody .= '" title="Mittwoch, '.bCounter_getDateFromYearWeekDay($iYear, $iWeek, 3).'">';
        $strDivBody .= zahl(mysql_result($SQLresult, $i, "we")).'</td>'."\n";


        $strDivBody .= '<td class="right';
        if ( $strDateToday == bCounter_getDateFromYearWeekDay($iYear, $iWeek, 4) ) {
            $strDivBody .= ' today';
        }
        $strDivBody .= ' '.get_CSSclass_weeks_td( mysql_result($SQLresult, $i, "th") );
        $strDivBody .= '" title="Donnerstag, '.bCounter_getDateFromYearWeekDay($iYear, $iWeek, 4).'">';
        $strDivBody .= zahl(mysql_result($SQLresult, $i, "th")).'</td>'."\n";


        $strDivBody .= '<td class="right';
        if ( $strDateToday == bCounter_getDateFromYearWeekDay($iYear, $iWeek, 5) ) {
            $strDivBody .= ' today';
        }
        $strDivBody .= ' '.get_CSSclass_weeks_td( mysql_result($SQLresult, $i, "fr") );
        $strDivBody .= '" title="Freitag, '.bCounter_getDateFromYearWeekDay($iYear, $iWeek, 5).'">';
        $strDivBody .= zahl(mysql_result($SQLresult, $i, "fr")).'</td>'."\n";


        $strDivBody .= '<td class="right';
        if ( $strDateToday == bCounter_getDateFromYearWeekDay($iYear, $iWeek, 6) ) {
            $strDivBody .= ' today';
        }
        $strDivBody .= ' '.get_CSSclass_weeks_td( mysql_result($SQLresult, $i, "sa") );
        $strDivBody .= '" title="Samstag, '.bCounter_getDateFromYearWeekDay($iYear, $iWeek, 6).'">';
        $strDivBody .= zahl(mysql_result($SQLresult, $i, "sa")).'</td>'."\n";


        $strDivBody .= '<td class="right';
        if ( $strDateToday == bCounter_getDateFromYearWeekDay($iYear, $iWeek, 7) ) {
            $strDivBody .= ' today';
        }
        $strDivBody .= ' '.get_CSSclass_weeks_td( mysql_result($SQLresult, $i, "su") );
        $strDivBody .= '" title="Sonntag, '.bCounter_getDateFromYearWeekDay($iYear, $iWeek, 7).'">';
        $strDivBody .= zahl(mysql_result($SQLresult, $i, "su")).'</td>'."\n";


        $strDivBody .= '<td class="right';
        /*
        if ( mysql_result($SQLresult_minmax, 0, "max_sum") == mysql_result($SQLresult, $i, "summe") ) {
            $strDivBody .= ' green';
        }elseif ( mysql_result($SQLresult_minmax, 0, "min_sum") == mysql_result($SQLresult, $i, "summe") ) {
            $strDivBody .= ' red';
        }
        */

        $strDivBody .= ' '.get_CSSclass_weeks_td( mysql_result($SQLresult, $i, "summe"), $iGlobalMinSum, $iGlobalMaxSum ).'">'.zahl(mysql_result($SQLresult, $i, "summe")).'</td>'."\n";
        if (
            ($i != 0 || mysql_result($SQLresult, $i, "su") > 0 )
            &&
            ( $i != (mysql_num_rows($SQLresult)-1) || mysql_result($SQLresult, $i, "mo") > 0 )
        ) {
            $strDivBody .= '<td class="right '.get_CSSclass_weeks_td( mysql_result($SQLresult, $i, "summe"), $iGlobalMinSum, $iGlobalMaxSum ).'">'.zahl(mysql_result($SQLresult, $i, "summe")/7, 1).'</td>'."\n";
            
            $arrSums["mo"] += mysql_result($SQLresult, $i, "mo");
            $arrSums["tu"] += mysql_result($SQLresult, $i, "tu");
            $arrSums["we"] += mysql_result($SQLresult, $i, "we");
            $arrSums["th"] += mysql_result($SQLresult, $i, "th");
            $arrSums["fr"] += mysql_result($SQLresult, $i, "fr");
            $arrSums["sa"] += mysql_result($SQLresult, $i, "sa");
            $arrSums["su"] += mysql_result($SQLresult, $i, "su");
            $arrSums["sum"] += mysql_result($SQLresult, $i, "summe");
            $arrSums["weeks"] += 1;
        }else{
            $strDivBody .= '<td class="center">&#8213;*</td>';
            $bShowInfo = true;
        }
        $strDivBody .= '</tr>'."\n";
    }

    $strDivBody .= '<tr>'."\n";
    $strDivBody .= '<td colspan="10">&nbsp;</td>'."\n";
    $strDivBody .= '</tr>'."\n";


    $strDivBody .= '<tr>'."\n";
    $strDivBody .= '<td class="center">&sum;</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl($arrSums["mo"]).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl($arrSums["tu"]).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl($arrSums["we"]).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl($arrSums["th"]).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl($arrSums["fr"]).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl($arrSums["sa"]).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl($arrSums["su"]).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl($arrSums["sum"]).'</td>'."\n";
    $strDivBody .= '<td class="right">';
    if ( $arrSums["weeks"] > 0 ) {
        $strDivBody .= zahl($arrSums["sum"]/($arrSums["weeks"]*7), 1);
    }else{
        $strDivBody .= 0;
    }
    $strDivBody .= '</td>'."\n";
    $strDivBody .= '</tr>'."\n";

    $strDivBody .= '<tr>'."\n";
    $strDivBody .= '<td class="center">&#8960;</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "avg_mo"), 1).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "avg_tu"), 1).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "avg_we"), 1).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "avg_th"), 1).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "avg_fr"), 1).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "avg_sa"), 1).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "avg_su"), 1).'</td>'."\n";
    $strDivBody .= '<td class="center">&#8213;</td>'."\n";
    $strDivBody .= '<td class="center">&#8213;</td>'."\n";
    $strDivBody .= '</tr>'."\n";

    $strDivBody .= '<tr>'."\n";
    $strDivBody .= '<td class="center bcounter-visitors-upto100percent">Maxima</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "max_mo")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "max_tu")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "max_we")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "max_th")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "max_fr")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "max_sa")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "max_su")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "max_sum")).'</td>'."\n";
    $strDivBody .= '<td class="center">&#8213;</td>'."\n";
    $strDivBody .= '</tr>'."\n";

    $strDivBody .= '<tr>'."\n";
    $strDivBody .= '<td class="center bcounter-visitors-upto20percent">Minima</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "min_mo")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "min_tu")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "min_we")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "min_th")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "min_fr")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "min_sa")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "min_su")).'</td>'."\n";
    $strDivBody .= '<td class="right">'.zahl(mysql_result($SQLresult_minmax, 0, "min_sum")).'</td>'."\n";
    $strDivBody .= '<td class="center">&#8213;</td>'."\n";
    $strDivBody .= '</tr>'."\n";

    $strDivBody .= '</table>'."\n";

    if ( $bShowInfo ) {
        $strDivBody .= '<p>*: Diese Woche wird bei der Berechnung nicht ber&uuml;cksichtigt,<br />da Daten nicht f&uuml;r die volle Woche vorhanden sind.</p>'."\n";
        $strDivBody .= '<p>Insgesamt wurden '.$arrSums["weeks"].' Wochen ber&uuml;cksichtigt.</p>'."\n";
    }
?>