<?
###################################################
#Copyright (C) 2002 Lihua Gao (hide@address.com)
#
#$Id: scheduler.php,v 1.1 2002/12/05 10:03:33 gaolihua Exp $
#
#This file is part of myTodos.
#
#myTodos 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.
#
#myTodos is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with myTodos; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#####################################################
?>
<?
/*******************************************************
if $userid is set the reports are based on this userid
if $todaystime is set today=todaystime
*******************************************************/
?>
<?
require_once("config.php");
include("sendmail.php");
include("header.php");
// check if today is a report day
function event2report($startdate, $enddate, $datestep, $datetype, $today, $force2report)
{
$today_wday=date('w', $today); // week day 0,1...=sonday, monday...
$today_mday=date('j', $today); // month day 1=1. day in a month
$today_yday=date('z', $today); // year day 0=1. day in a year
// should be calculated only once
$yToday=date("Y", $today);
$mToday=date("j", $today);
$wToday=date("w", $today);
$yStart=substr($startdate,0,4);
$mStart=substr($startdate,5,2);
$dStart=substr($startdate,8,2);
if ($datetype=='o') {
$secTotal=$today-mktime(0,0,0, $mStart, $dStart, $yStart);
$days=$secTotal/(24*3600);
//echo "once days=$days today=".date('Y-m-d H:i:s', $today)."<br>";
if ($days < 1.0) return 1; // today is due
if ($wToday==0 || strpos($force2report,"w")>0) { // on sunday
$secTotal=-($today-mktime(0,0,0, $mStart, $dStart, $yStart));
$days=$secTotal/(24*3600);
if ($days > 0 || $days<=7 ) return 1;
return 0;
}
if ($mToday==1 || strpos($force2report,"m")>0) { // on the 1. of a month
$months=($yToday-$yStart)*12+$mToday-$mStart;
if ($months == 0) return 1;
return 0;
}
return 0;
}
if ($datetype=='d') {
if ($datestep==1) return 1; // report
$secTotal=$today-mktime(0,0,0, $mStart, $dStart, $yStart);
$days=$secTotal/(24*3600);
if ($days % $datesep == 0) return 1;
return 0;
}
if ($datetype=='w' && ($wToday==0 || strpos($force2report,"w")>0)) { // on sunday
//echo "weekly\n";
if ($datestep==1) {
if ($startdate<$today+24*3600*7)
return 1; // report
else
return 0; // next report period (week)
}
$secTotal=$today-mktime(0,0,0, $mStart, $dStart, $yStart);
$days=$secTotal/(24*3600);
$weeks=$days/7;
if ($weeks % $datesep == 0) return 1;
return 0;
}
if ($datetype=='m' && ($mToday==1 || strpos($force2report,"m")>0)) { // on the 1. of a month
//if ($datestep==1) return 1; // report
//echo "monthly\n";
if ($datestep==1) {
if ($startdate<$today+24*3600*date("t",$today))
return 1; // report
else
return 0; // next report period (month)
}
$months=($yToday-$yStart)*12+$mToday-$mStart;
if ($months % $datesep == 0) return 1;
return 0;
}
if ($datetype=='y' && ($yToday==1 || strpos($force2report,"y")>0)) { // on the 1. of a year
//return 1; // always report
//echo "yearly\n";
if ($datestep==1) {
if ($startdate<$today+24*3600*date("z",mktime(0,0,0, 12,31,date("Y",$today))))
return 1; // report
else
return 0; // next report period (year)
}
}
return 0;
}
/*
$period1day: first day of the period (give by $mode)
$mode="today":
$mode=="week":
$mode=="month":
$mode=="year":
$mode=="comingweek":
$mode=="comingmonth":
$mode=="comingyear":
*/
function getDueDates($startdate, $enddate, $datestep, $datetype, $period1day, $mode)
{
if ($mode=="today") {
return date("Y-m-d H:i:s", $period1day);
}
//echo "<P>startdate:".$startdate."<br>";
$yStart=substr($startdate,0,4);
$mStart=substr($startdate,5,2);
$dStart=substr($startdate,8,2);
$startdate_time=mktime(0,0,0, $mStart, $dStart, $yStart);
//echo "<P>startdate_time:".date("Y-m-d H:i:s", $startdate_time)."<br>";
$yEnd=substr($enddate,0,4);
$mEnd=substr($enddate,5,2);
$dEnd=substr($enddate,8,2);
$enddate_time=mktime(0,0,0, $mEnd, $dEnd, $yEnd);
if ($enddate_time==-1) {
$enddate_time=mktime(0,0,0, 12, 31, 2037); // max. date
//echo "<P>new enddate_time:".$enddate_time."<br>";
}
//echo "<P>enddate_time:".date("Y-m-d H:i:s", $enddate_time)."<br>";
if ($mode=="week" or $mode=="comingweek") {
$period_lastday=$period1day+24*3600*6;
}
elseif ($mode=="month" or $mode=="comingmonth") {
$period_lastday=$period1day+24*3600*(date("t",$period1day)-1);
}
elseif ($mode=="year" or $mode=="comingyear") {
$period_lastday=$period1day+24*3600*date("z",mktime(0,0,0, 12,31,date("Y",$period1day)));
}
//echo "<P>period1day:".date("Y-m-d H:i:s", $period1day)."<br>";
//echo "<P>period_lastday:".date("Y-m-d H:i:s", $period_lastday)."<br>";
$enddate_time=$period_lastday;
$due_dates="";
// use $period1day for loop start
$aday=$startdate_time;
// invariant steps -> steps part one
if ($datetype=="d") {
$step_time=24*3600*$datestep;
}
elseif ($datetype=="w") {
$step_time=24*3600*7*$datestep;
}
else
$step_time=0; // once
$loop_cnt=0;
while ($aday<=$enddate_time){
// variant steps -> steps part two
if ($datetype=="m") {
$step_time=24*3600*date("t",$aday)*$datestep;
}
elseif ($datetype=="y") {
$step_time=24*3600*date("z",mktime(0,0,0, 12,31,date("Y",$aday)))*$datestep;
}
if ($aday>=$period1day and $aday<=$period_lastday) {
$due_dates.=date("Y-m-d H:i:s", $aday).",";
//echo "new due date added:".$due_dates."<br>\n";
}
//echo "checking date loop :".date("Y-m-d H:i:s", $aday)."<br>\n";
$aday+=$step_time;
if ($datetype=="o") break;
if ($loop_cnt++>54) {
$due_dates=substr($due_dates, 0, strlen($due_dates)-1);
$due_dates.="...(more then 54 due dates) ";
break;
}
}
$due_dates=substr($due_dates, 0, strlen($due_dates)-1);
return $due_dates;
}
?>
<table cellspacing="0" class="table3">
<tr class="rowtitle">
<td><h5> Scheduler output</h5></td>
</tr>
<!-- empty line -->
<tr><td> </td></tr>
<tr>
<td>
<?
$now=mktime(0,0,0, date("m",time()), date("d",time()), date("Y",time()));
if (isset($todaystime)) {
$now=$todaystime;
//echo "todaystime:".date("Y-m-d H:i:s", $todaystime)."<br>\n";
}
$today=date('Y-m-d', $now);
$today_wday=date('w', $now); // week day 0,1...=sonday, monday...
$today_mday=date('j', $now); // month day 1=1. day in a month
$today_yday=date('z', $now); // year day 0=1. day in a year
if ($mode=="today") $force2report="";
elseif ($mode=="week" or $mode=="comingweek") $force2report="w";
elseif ($mode=="month" or $mode=="comingmonth") $force2report="wm";
elseif ($mode=="year" or $mode=="comingyear") $force2report="wmy";
if (isset($userid))
$where_user=" tds_usr_id=usr_id and tds_usr_id=".$userid." and ";
else
$where_user=" tds_usr_id=usr_id and ";
$sql = "SELECT * FROM todos, users WHERE ".$where_user.
//"( (tds_startdate<='".$today."' and (tds_enddate is null or tds_enddate>='".$today."')) || tds_datetype='o')".
"( (tds_enddate is null or tds_enddate>='".$today."') || tds_datetype='o')".
" AND tds_state='a'".
" order by usr_email;";
//echo $sql."<P>\n";
$result = mysql_query($sql, $conn);
if ($mode=="today") $period=$today;
elseif ($mode=="week") $period="this week";
elseif ($mode=="month") $period="this month";
elseif ($mode=="year") $period="this year";
elseif ($mode=="comingweek") $period="this coming week up today";
elseif ($mode=="comingmonth") $period="this coming month up today";
elseif ($mode=="comingyear") $period="this coming year up today";
$mailtext_header="<b>You have remarked following events in mytodos for ".$period.":</b><br><br>\n\n";
$last_mailto="-";
$mail_cnt=0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$tds_id=$row["tds_id"];
$tds_createdate=$row["tds_createdate"];
$tds_owner=$row["usr_firstname"]." ".$row["usr_lastname"];
$tds_subject=$row["tds_subject"];
$tds_details=$row["tds_details"];
$tds_startdate=$row["tds_startdate"];
$tds_enddate=$row["tds_enddate"];
$tds_datetype=$row["tds_datetype"];
$tds_datestep=$row["tds_datestep"];
$tds_alertahead=$row["tds_alertahead"];
$usr_email=$row["usr_email"];
$tds_state=$row["tds_state"];
// if today is a due day for every date type
//if ( event2report($tds_startdate, $tds_enddate, $tds_datestep, $tds_datetype, time(), "_".$force2report)==1 )
if ( event2report($tds_startdate, $tds_enddate, $tds_datestep, $tds_datetype, $now, "_".$force2report)==1 )
{
// append mail text for the same email
if ($last_mailto!=$usr_email) {
$mailtext[$usr_email]=$mailtext_header;
$mailadr[$mail_cnt]=$usr_email;
$last_mailto=$usr_email;
$mail_cnt++;
}
$mailtext[$usr_email].="<I>subject: ".$tds_subject."</I><br>\n";
$mailtext[$usr_email].="created: ".$tds_createdate."<br>\n";
$mailtext[$usr_email].="created by: ".$tds_owner."<br>\n";
$mailtext[$usr_email].="details: ".$tds_details."<br>\n";
$mailtext[$usr_email].="date type: ".$tds_datetype."<br>\n";
$mailtext[$usr_email].="due date: ".
getDueDates($tds_startdate, $tds_enddate, $tds_datestep, $tds_datetype,
$now, $mode).
"<br>\n";
$mailtext[$usr_email].="<br><br>\n\n"; // separator
//echo implode(";",$row)."<P>\n";
}
}
//echo "mails: ".$mail_cnt."\n";
for ($i=0; $i<$mail_cnt; $i++){
$usr_email=$mailadr[$i];
echo $mailtext[$usr_email]."\n";
sendmail("mytodos report for ".$today,
$mailtext[$usr_email], $usr_email, "hide@address.com");
}
?>
</td>
</tr>
<!-- empty line -->
<tr><td> </td></tr>
<tr>
<td>
<?=$mail_cnt?> mail was send to you
</td>
</tr>
</table>
<?
include("footer.php");
?>