<?php // $Revision: 1.3.2.1 $
/* vim: set expandtab ts=4 sw=4 sts=4: */

/**
 * $Id: send_reminder,v 1.3.2.1 2007/11/22 07:52:57 luiswang Exp $
 * 
 * Copyright (c) 2007 by the netOffice Dwins developers
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

$checkSession = false;
require_once("../includes/library.php");
$today = date('Y-m-d');
$year1 = date('Y');
$month1 = date('m');
$day1 = date('d');
$hour1 = date('H');
$min1 = date('i');
$timestamp1 = mktime($hour1, $min1, 0, $month1, $day1, $year1);
$dateM = date('Y-m-d', mktime($hour1, $min1, 0, $month1, $day1+14, $year1));
$weekDay0 = intval(strftime("%w", mktime(12, 12, 12, $month1, $day1, $year1)) + 1);

// personal calendar reminders
$tmpquery = "WHERE cal.project = '0' AND cal.date_start<='$dateM' AND ((cal.recurring = '0' AND cal.date_start >= '$today') OR cal.recurring = '1') AND cal.reminder = '1' ORDER BY cal.owner";
$calendarList = new request();
$calendarList->openCalendar($tmpquery);
$comptCalendarList = count($calendarList->cal_id);
$mem_id = '';

for ($i=0; $i<$comptCalendarList; $i++) {
    if ($mem_id == '' || $mem_id != $calendarList->cal_owner[$i]) {
        $mem_id = $calendarList->cal_owner[$i];
        unset($eventList);
    }

    if ($calendarList->cal_recurring[$i] == '0' || ($calendarList->cal_recurring[$i] == '1' && $calendarList->cal_date_start[$i] >= $today)) {
        $year1 = substr($calendarList->cal_date_start[$i], 0, 4);
        $month1 = substr($calendarList->cal_date_start[$i], 5, 2);
        $day1 = substr($calendarList->cal_date_start[$i], 8, 2);
        $hour1 = substr($calendarList->cal_time_start[$i], 0, 2);
        $min1 = substr($calendarList->cal_time_start[$i], 3, 2);
        $timestamp0 = mktime($hour1, $min1, 0, $month1, $day1, $year1);
        $timestamp2 = $timestamp0 - $calendarList->cal_reminder_time1[$i] * 60;

        if ($calendarList->cal_reminder_time2[$i] > 0) {
            $timestamp3 = $timestamp0 - $calendarList->cal_reminder_time2[$i] * 60;
        } else {
            $timestamp3 = -1;
        }
    } else {
        $weekDay1 = $calendarList->cal_recur_day[$i];

        if ($weekDay1 >= $weekDay0) {
            $timestamp4 = mktime(12,0,0,date("m",$timestamp1),date("d",$timestamp1)+$weekDay1-$weekDay0,date("Y",$timestamp1));
        } else {
            $timestamp4 = mktime(12,0,0,date("m",$timestamp1),date("d",$timestamp1)+$weekDay1+7-$weekDay0,date("Y",$timestamp1));
        }

        $timestamp2 = $timestamp4 - $calendarList->cal_reminder_time1[$i] * 60;

        if ($cal_reminder_time2[$i] > 0) {
            $timestamp3 = $timestamp4 - $calendarList->cal_reminder_time2[$i] * 60;
        } else {
            $timestamp3 = -1;
        }
    }

    if ($timestamp1 == $timestamp2 || $timestamp1 == $timestamp3) {
        $eventList['name'][] = $calendarList->cal_shortname[$i];
        $eventList['subject'][] = $calendarList->cal_subject[$i];
        $eventList['date_start'][] = $calendarList->cal_date_start[$i];
        $eventList['date_end'][] = $calendarList->cal_date_end[$i];
    }

    if (($i == $comptCalendarList-1 || $mem_id != $calendarList->cal_owner[$i+1]) && (count($eventList['name']) > 0)) {
        $mail = new notification();
        $mail->getUserinfo('1', "from");
        $mail->getUserinfo($mem_id, "to");
        $mail->partSubject = lang_strings("send_reminder1");
        $mail->partMessage = lang_strings("send_reminder2");
        $mail->openBody();
        $mail->openResults();
        $mail->openRow();
        $mail->cellRow(lang_strings('shortname'));
        $mail->cellRow(lang_strings('subject'));
        $mail->cellRow(lang_strings('date_start'));
        $mail->cellRow(lang_strings('date_end'));
        $mail->closeRow();

        for ($j=0; $j<count($eventList['name']); $j++) {
            $mail->openRow();
            $mail->cellRow($eventList['name'][$j]);
            $mail->cellRow($eventList['subject'][$j]);
            $mail->cellRow($eventList['date_start'][$j]);
            $mail->cellRow($eventList['date_end'][$j]);
            $mail->closeRow();
        }

        $mail->closeResults();
        $mail->closeBody();
        $subject = $mail->partSubject;
        $mail->Subject = $subject;
        $mail->Send();
    }
}

// project calendar reminders
$tmpquery = "WHERE cal.project <> '0' AND cal.date_start<='$dateM' AND ((cal.recurring = '0' AND cal.date_start >= '$today') OR cal.recurring = '1') AND cal.reminder = '1' ORDER BY cal.project";
$calendarList = new request();
$calendarList->openCalendar($tmpquery);
$comptCalendarList = count($calendarList->cal_id);
$project_id = '';

for ($i=0; $i<$comptCalendarList; $i++) {
    if ($project_id == '' || $project_id != $calendarList->cal_project[$i]) {
        $project_id = $calendarList->cal_project[$i];
        unset($eventList);
    }

    if ($calendarList->cal_recurring[$i] == '0' || ($calendarList->cal_recurring[$i] == '1' && $calendarList->cal_date_start[$i] >= $today)) {
        $year1 = substr($calendarList->cal_date_start[$i], 0, 4);
        $month1 = substr($calendarList->cal_date_start[$i], 5, 2);
        $day1 = substr($calendarList->cal_date_start[$i], 8, 2);
        $hour1 = substr($calendarList->cal_time_start[$i], 0, 2);
        $min1 = substr($calendarList->cal_time_start[$i], 3, 2);
        $timestamp0 = mktime($hour1, $min1, 0, $month1, $day1, $year1);
        $timestamp2 = $timestamp0 - $calendarList->cal_reminder_time1[$i] * 60;

        if ($calendarList->cal_reminder_time2[$i] > 0) {
            $timestamp3 = $timestamp0 - $calendarList->cal_reminder_time2[$i] * 60;
        } else {
            $timestamp3 = -1;
        }
    } else {
        $weekDay1 = $calendarList->cal_recur_day[$i];

        if ($weekDay1 >= $weekDay0) {
            $timestamp4 = mktime(12,0,0,date("m",$timestamp1),date("d",$timestamp1)+$weekDay1-$weekDay0,date("Y",$timestamp1));
        } else {
            $timestamp4 = mktime(12,0,0,date("m",$timestamp1),date("d",$timestamp1)+$weekDay1+7-$weekDay0,date("Y",$timestamp1));
        }

        $timestamp2 = $timestamp4 - $calendarList->cal_reminder_time1[$i] * 60;

        if ($cal_reminder_time2[$i] > 0) {
            $timestamp3 = $timestamp4 - $calendarList->cal_reminder_time2[$i] * 60;
        } else {
            $timestamp3 = -1;
        }
    }

    if ($timestamp1 == $timestamp2 || $timestamp1 == $timestamp3) {
        $eventList['name'][] = $calendarList->cal_shortname[$i];
        $eventList['subject'][] = $calendarList->cal_subject[$i];
        $eventList['date_start'][] = $calendarList->cal_date_start[$i];
        $eventList['date_end'][] = $calendarList->cal_date_end[$i];
    }

    if (($i == $comptCalendarList-1 || $project_id != $calendarList->cal_project[$i+1]) && (count($eventList) > 0)) {
        $tmpquery = " WHERE pro.id='$project_id'";
        $projectDetail = new request();
        $projectDetail->openProjects($tmpquery);
        $tmpquery = " WHERE att.project='$project_id'";
        $attendantList = new request();
        $attendantList->openAttendants($tmpquery);
        $comptAttendantList = count($attendantList->att_id);
        $mail = new notification();
        $mail->getUserinfo('1', "from");

        for ($j=0; $j<$comptAttendantList; $j++) {
            $mail->getUserinfo($attendantList->att_mem_id[$j], "to");
        }

        $mail->partSubject = lang_strings("send_reminder3");
        $mail->partMessage = lang_strings("send_reminder4") . $projectDetail->pro_name[0] . lang_strings("send_reminder5");
        $mail->openBody();
        $mail->openResults();
        $mail->openRow();
        $mail->cellRow(lang_strings('shortname'));
        $mail->cellRow(lang_strings('subject'));
        $mail->cellRow(lang_strings('date_start'));
        $mail->cellRow(lang_strings('date_end'));
        $mail->closeRow();

        for ($j=0; $j<count($eventList['name']); $j++) {
            $mail->openRow();
            $mail->cellRow($eventList['name'][$j]);
            $mail->cellRow($eventList['subject'][$j]);
            $mail->cellRow($eventList['date_start'][$j]);
            $mail->cellRow($eventList['date_end'][$j]);
            $mail->closeRow();
        }

        $mail->closeResults();
        $mail->closeBody();
        $subject = $mail->partSubject;
        $mail->Subject = $subject;
        $mail->Send();
    }
}

// meeting reminders
$tmpquery = "WHERE mee.date<='$dateM' AND mee.date >= '$today' AND mee.reminder = '1'";
$meetingList = new request();
$meetingList->openMeetings($tmpquery);
$comptMeetingList = count($meetingList->mee_id);

for ($i = 0;$i < $comptMeetingList;$i++) {
    $year1 = substr($meetingList->mee_date[$i], 0, 4);
    $month1 = substr($meetingList->mee_date[$i], 5, 2);
    $day1 = substr($meetingList->mee_date[$i], 8, 2);
    $hour1 = substr($meetingList->mee_start_time[$i], 0, 2);
    $min1 = substr($meetingList->mee_start_time[$i], 3, 2);
    $timestamp0 = mktime($hour1, $min1, 0, $month1, $day1, $year1);
    $timestamp2 = $timestamp0 - $meetingList->mee_reminder_time1[$i] * 60;

    if ($meetingList->mee_reminder_time2[$i] > 0) {
        $timestamp3 = $timestamp0 - $meetingList->mee_reminder_time2[$i] * 60;
    } else {
        $timestamp3 = -1;
    }

    if ($timestamp1 != $timestamp2 && $timestamp1 != $timestamp3) continue;

    $tmpquery = " WHERE att.meeting='" . $meetingList->mee_id[$i] . "'";
    $attendantList = new request();
    $attendantList->openAttendants($tmpquery);
    $comptAttendantList = count($attendantList->att_id);

    if ($comptAttendantList == 0) continue;

    $mail = new notification();
    $mail->getUserinfo('1', "from");

    for ($j = 0;$j < $comptAttendantList;$j++) {
        $mail->getUserinfo($attendantList->att_mem_id[$j], "to");
    }

    $mail->partSubject = lang_strings("send_reminder6");
    $mail->partMessage = lang_strings("send_reminder6");
    $mail->openBody();
    $mail->openContent();
    $mail->contentRow(lang_strings('name'), $meetingList->mee_name[$i]);
    $mail->contentRow(lang_strings('me_agenda'), $meetingList->mee_agenda[$i]);
    $mail->contentRow(lang_strings('date'), $meetingList->mee_date[$i]);
    $mail->contentRow(lang_strings('start_time'), $meetingList->mee_start_time[$i]);
    $mail->contentRow(lang_strings('end_time'), $meetingList->mee_end_time[$i]);
    $mail->closeContent();
    $mail->closeBody();
    $subject = $mail->partSubject;
    $mail->Subject = $subject;
    $mail->Send();
}

?>
