<?php
/*
= retrieves events from db function =
Queries database for a specified period and dumps events into arrays
Input params: start date, end date (in 2010-04-28 format)
© Copyright 2010 LuxSoft - Luxembourg - www.LuxSoft.eu
This file is part of the LuxCal Web Calendar.
The LuxCal Web Calendar 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 3 of the License, or (at your option) any later version.
The LuxCal Web Calendar 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 the LuxCal
Web Calendar. If not, see <http://www.gnu.org/licenses/>.
*/
function retrieve($start,$end) {
global $sedit;
/* retrieve events between $start and $end */
$catFilter = ($_SESSION['cC'] > 0) ? "AND e.category_id = ".$_SESSION['cC'] : '';
$q0 =
"SELECT
d.s_date, #0
d.e_date, #1
DATE_FORMAT(d.s_time,'%H:%i'), #2
DATE_FORMAT(d.e_time,'%H:%i'), #3
d.r_type, #4
d.r_interval, #5
d.r_period, #6
d.r_until, #7
d.notify, #8
e.event_id, #9
e.title, #10
e.category_id, #11
e.venue, #12
e.user_id, #13
e.description, #14
e.private, #15
c.sequence, #16
c.color, #17
c.background, #18
u.user_name #19
FROM [db]events e
INNER JOIN [db]dates d ON d.event_id = e.event_id
INNER JOIN [db]categories c ON c.category_id = e.category_id
INNER JOIN [db]users u ON u.user_id = e.user_id
WHERE
d.status >= 0
".$catFilter;
//process non-recurring events
$q1 = $q0."
AND d.r_type = 0
AND ((d.s_date <= '$end') AND (IF(d.e_date != '9999-00-00', d.e_date, d.s_date) >= '$start') )
ORDER BY
d.s_date";
$query = dbquery($q1);
//echo "NR: ".mysql_num_rows($query)." "; //TEST
while ($row = mysql_fetch_row($query)) {
if (!$row[15] or ($row[13] == $_SESSION['uid']) or $sedit) { // private: only for current user + admin
$eEnd = ($row[1][0] != '9') ? $row[1] : $row[0];
processEvent(max($start,$row[0]), min($end,$eEnd), $row[0], $eEnd, $row);
}
}
//process recurring events
$q1 = $q0."
AND d.r_type > 0
AND d.s_date <= '$end'
AND d.r_until >= '$start'
ORDER BY
d.s_date";
$query = dbquery($q1);
//echo "R: ".mysql_num_rows($query)." "; //TEST
while ($row = mysql_fetch_row($query)) {
if (!$row[15] or ($row[13] == $_SESSION['uid']) or $sedit) { // private: only for current user + admin
$dDif = ($row[1][0] != '9') ? intval((strtotime($row[1]) - strtotime($row[0])) / 86400) : 0; // delta start date - end date
$eStart = $row[0];
if ($row[4] == 2) {
$nxtD = nextRdate($eStart,$row[4],$row[5],$row[6],0); // goto 1st occurence of xth <dayname> in current month
$eStart = ($nxtD < $eStart) ? nextRdate($eStart,$row[4],$row[5],$row[6],1) : $nxtD;
}
$eEnd = date("Y-m-d", mktime(12, 0, 0, substr($eStart,5,2), substr($eStart,8,2)+$dDif, substr($eStart,0,4)));
while ($eStart <= min($end, $row[7]) and $row[7] >= $start) {
if ($eEnd >= $start) { // hit
processEvent(max($start,$eStart), min($end,$eEnd), $eStart, $eEnd, $row);
}
$eStart = nextRdate($eStart,$row[4],$row[5],$row[6],1);
$eEnd = date("Y-m-d", mktime(12, 0, 0, substr($eStart,5,2), substr($eStart,8,2)+$dDif, substr($eStart,0,4)));
}
}
}
}
//
// Process (multi-day) events and save event data
//
function processEvent($from, $till, $eStart, $eEnd, &$row) {
global $evtList;
$sTs = mktime(12, 0, 0, substr($from,5,2), substr($from,8,2), substr($from,0,4));
$eTs = mktime(14, 0, 0, substr($till,5,2), substr($till,8,2), substr($till,0,4));
for($i=$sTs;$i<=$eTs;$i+=86400) { //increment 1 day
$curD = date("Y-m-d", $i);
$curdm = substr($curD,5);
if ($row[1][0] != '9' and $row[0] < $row[1]) { // multi-day event
//mde -> 0:no, 1:first, 2:middle,3:last day
$event['mde'] = ($curdm == substr($eStart,5)) ? 1 : (($curdm == substr($eEnd,5)) ? 3 : 2);
$event['sti'] = ($event['mde'] == 1) ? $row[2] : '00:00';
$event['eti'] = ($event['mde'] == 3) ? $row[3] : '23:59';
} else { // single day event
$event['mde'] = 0;
if (($row[2] == "00:00") and ($row[3] == "23:59")) {
$event['sti'] = $event['eti'] = ""; //all day: start/end time = ""
} else {
$event['sti'] = $row[2];
$event['eti'] = ($row[3][0] != '9') ? $row[3] : ""; // no end time = ""
}
}
$event['r_t'] = $row[4];
$event['r_i'] = $row[5];
$event['r_p'] = $row[6];
$event['r_u'] = $row[7];
$event['not'] = $row[8];
$event['eid'] = $row[9];
$event['tit'] = stripslashes($row[10]);
$event['cat'] = $row[11];
$event['ven'] = stripslashes($row[12]);
$event['uid'] = $row[13];
$event['des'] = stripslashes($row[14]);
$event['pri'] = $row[15];
$event['seq'] = str_pad($row[16],2,"0",STR_PAD_LEFT);
$event['col'] = $row[17] ? $row[17] : "000000";
$event['bco'] = $row[18] ? $row[18] : "FFFFFF";
$event['una'] = $row[19];
$evtList[$curD][] = $event;
}
}
//
// Compute next event start date
//
function nextRdate($curD, $rT, $rI, $rP, $i) {
if($rT == 1) { // repeat xth day, week, month, year
switch ($rP) { // period
case 1: // day
$nxtD = date("Y-m-d", mktime(12, 0, 0, substr($curD,5,2), substr($curD,8,2)+$rI, substr($curD,0,4)));
break;
case 2: // week
$nxtD = date("Y-m-d", (mktime(12, 0, 0, substr($curD,5,2), substr($curD,8,2), substr($curD,0,4)) + ($rI * 604800)));
break;
case 3: // month
$nxtD = date("Y-m-d", mktime(12, 0, 0, substr($curD,5,2)+$rI, substr($curD,8,2), substr($curD,0,4)));
break;
case 4: // year
$nxtD = date("Y-m-d", mktime(12, 0, 0, substr($curD,5,2), substr($curD,8,2), substr($curD,0,4)+$rI));
}
} else { // repeat on the xth <dayname> of the month
$day1Ts = mktime(12, 0, 0, substr($curD,5,2)+$i, 1, substr($curD,0,4));
$dowDif = $rP - date('N',$day1Ts); //day of week difference
$offset = $dowDif + 7 * $rI;
if ($dowDif >= 0) { $offset -= 7; }
if ($offset > date('t',$day1Ts)) { $offset -= 7; }
$nxtD = date("Y-m-d", $day1Ts + (86400 * $offset));
}
return $nxtD;
}
?>