<?php

// calendar_view_day.php - PHProjekt Version 5.0
// copyright  ©  2000-2005 Albrecht Guenther  ag@phprojekt.com
// www.phprojekt.com
// Authors: Albrecht Guenther, $Author: paolo $
// $Id: calendar_view_day.php,v 1.44 2005/06/06 15:10:26 paolo Exp $

// show single user day view

// check whether the lib has been included - authentication!
if (!defined('lib_included')) die('Please use calendar.php!');


$view_env = calendar_get_view_env();

$tages_anfang = ($settings['tagesanfang']) ? $settings['tagesanfang'] : PHPR_DAY_START;
$tages_ende   = ($settings['tagesende'])   ? $settings['tagesende']   : PHPR_DAY_END;

for ($i=0; $i<(60/$time_step * ($tages_ende - $tages_anfang)); $i++) {
    $htmtab[$i] = '';
    $repeat[$i] = 1;
    $isset[$i]  = 0;
}

// for special events..
$special = array( 'all_day' => array(), 'outside' => array() );

// get the (probably filtered) events of the day
$res = calendar_get_day_events($view_env);

foreach ($res as $row) {

    $special_day = false;
    // collect "all day" events
    if ($row['anfang'] == '----' && $row['ende'] == '----') {
        $special_day = 'all_day';
    }
    // collect events outside the visible time range
    else if ($row['ende'] <= $tages_anfang.'00' || $row['anfang'] >= $tages_ende.'00') {
        $special_day = 'outside';
    }
    if ($special_day !== false) {
        $alt_title = calendar_get_alt_title_tag($row);
        if ($view_env['can_edit']) {
            $special[$special_day][] = '<a href="./calendar.php?ID='.$row['ID'].'&amp;mode=forms&amp;view='.$view.
                                       $view_env['af'].$sid.'" title="'.$alt_title.'">'.calendar_get_event_text($row).'</a>';
        }
        else {
            $special[$special_day][] = '<span title="'.$alt_title.'">'.calendar_get_event_text($row).'</span>';
        }
        continue;
    }

    $ha = substr($row['anfang'], 0, 2);
    $ma = substr($row['anfang'], 2, 2);
    $he = substr($row['ende'], 0, 2);
    $me = substr($row['ende'], 2, 2);

    if ($ha < $tages_anfang) {
        $ha = 0;
        $ma = 0;
    }
    else {
        $ha -= $tages_anfang;
    }
    if ($he < $tages_anfang) {
        $he = $tages_anfang;
        $me = 0;
    }
    if ($he == $tages_ende) {
        $me = 0;
    }
    if ($he > $tages_ende) {
        $he = $tages_ende;
        $me = 0;
    }
    $he -= $tages_anfang;
    $i1 = (int) floor(($ha*60+$ma)/$time_step);
    $i2 = (int) floor(($he*60+$me-1)/$time_step);

    $alt_title = calendar_get_alt_title_tag($row);
    if ($view_env['can_edit']) {
        $text = '<a href="./calendar.php?ID='.$row['ID'].'&amp;mode=forms&amp;view='.$view.$view_env['af'].$sid.
                '" class="calendar_day_event" title="'.$alt_title.'">'.calendar_get_event_text($row).'</a>';
    }
    else {
        $text = '<span class="calendar_day_event" title="'.$alt_title.'">'.calendar_get_event_text($row).'</span>';
    }

    $repeat[$i1]   = 0;
    $repeat[$i2+1] = 0;
    for ($i = $i1; $i <= $i2; $i++) {
        if ($isset[$i]) $htmtab[$i] .= '<br />'.$text;
        else            $htmtab[$i]  = $text;
        $isset[$i] = ($row['status'] == '1') ? 5 : $row['partstat'] + 1;
    }
}

for ($i=0; $i<(60 / $time_step *($tages_ende - $tages_anfang)); $i++) {
    if ($isset[$i] == 0) {
        $repeat[$i] = 0;
    }
}

$output = '
<div class="inner_content">
'.calendar_view_prevnext_header('d', $view_env).'
<table cellspacing="0" cellpadding="0" class="calendar_table" width="100%" border="0">
';

// get holidays
$holidays = calendar_get_holidays(mktime(0,0,0, $month, $day, $year));
if (count($holidays)) {
    foreach ($holidays as $a_holiday) {
        $class = ($a_holiday['class']) ? ' class="'.$a_holiday['class'].'"' : '';
        $output .= '
    <tr>
        <td colspan="2"'.$class.'>'.$a_holiday['name'].'</td>
    </tr>
';
    }
}

// show "all day" events
if (count($special['all_day']) > 0) {
    foreach ($special['all_day'] as $row) {
        $output .= '
    <tr>
        <td colspan="2">'.$row.'</td>
    </tr>
';
    }
}

$output .= '
    <tr>
        <!-- begin first column -->
        <td valign="top" width="50%" class="calendar_table">
            <table cellspacing="1" cellpadding="0" class="calendar_table">
';

$i  = 0;
$a1 = $tages_anfang;
$a2 = 0;

$first  = true;
$reihe2 = false;

while ($a1 < $tages_ende) {
    $a1 = substr('0'.$a1, -2);
    $a2 = substr('0'.$a2, -2);

    if ((($a1+$a2/60) >= (($tages_ende - $tages_anfang)/2 + $tages_anfang)) && !$reihe2) {
        $output .= '
            </table>
        </td>
        <!-- end first column -->

        <!-- begin second column -->
        <td valign="top" width="50%" class="calendar_table">
            <table class="calendar_table" cellpadding="0" cellspacing="1">
';
        $first  = true;
        $reihe2 = true;
    }

    $a3 = $a2 + $time_step - 1;
    $tanfang = $a1.$a2;
    $tende = $a1.$a3;
    $a4 = $a2 + $time_step;
    $r = 1;
    while ($repeat[$i+$r]) {
        $r++;
    }
    $date_help_var = '&nbsp;'.$a1.':'.$a2.'&nbsp;';
    //$timelink = '<a href="javascript:time(\''.$a1.$a2.'\')" title="'.$date_help_var.'">'.$date_help_var.'</a>';
    $timelink = $date_help_var;

/* TODO: remove this; I think this is obsolete now..
    if (($a1 + 1) == $tages_ende && $a4 == 60) {
        $date_help_var = '&nbsp;'.($a1 + 1).':00&nbsp;';
        //$timelink .= '<br /><a href="javascript:time(\''.($a1+1).'00\')" title="'.$date_help_var.'">'.$date_help_var.'</a>';
        $timelink .= '<br />'.$date_help_var;
    }
*/

    if ($first) {
        $output .= '
                <tr>
                    <td class="calendar_table">'.$timelink.'</td>
                    <td width="99%"'.($r>1 ? ' rowspan="'.$r.'"' : '').' class="calendar_day '.$cal_class[$isset[$i]].'">
                        '.$htmtab[$i].'
                    </td>
                </tr>
';
    }
    else {
        $output .= '
                <tr>
                    <td class="calendar_table">'.$timelink.'</td>
                </tr>
';
    }

    $first = false;
    $r--;
    if ($r == 0) $first = true;
    $i++;
    $a2 = $a4;
    if ($a2 == 60) {
        $a1 += 1;
        $a2 = 0;
    }
}

$output .= '
            </table>
        </td>
        <!-- end second column -->
    </tr>
</table>
';
// show events outside the visible time range
if (count($special['outside']) > 0) {
    $output .= "<br />\n".__('Further events').":<br />\n";
    foreach ($special['outside'] as $row) {
        $output .= $row."<br />\n";
    }
}
$output .= '
<br /><br />
</div>
';

echo $output;


// get the (probably filtered) events of the day
function calendar_get_day_events($view_env) {
    $ret = array();
    if (!$view_env['is_visible']) return $ret;

    $query = "SELECT ID, von, an, event, anfang, ende, visi, partstat, status
                FROM ".DB_PREFIX."termine
               WHERE datum = '".calendar_get_ymd()."'
                 AND an = '".$view_env['which_user']."'
                 AND visi IN ('".implode("','", $view_env['visi'])."')
            ORDER BY anfang, event";
    $res = db_query($query) or db_die();
    while ($row = db_fetch_row($res)) {
        $event = array( 'ID'       => $row[0]
                       ,'von'      => $row[1]
                       ,'an'       => $row[2]
                       ,'event'    => stripslashes($row[3])
                       ,'anfang'   => $row[4]
                       ,'ende'     => $row[5]
                       ,'visi'     => $row[6]
                       ,'partstat' => $row[7]
                       ,'status'   => $row[8]
                      );
        // neutralize 'event' text if only visibility is available
        if (!$view_env['is_readable'] ||
            !calendar_event_is_readable($view_env['which_user'], $event['visi'])) {
            $event['event'] = __('Event');
        }
        $ret[] = $event;
    }
    return $ret;
}

?>
