forked from LibreHealthIO/lh-ehr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar_events.inc.php
110 lines (97 loc) · 4.29 KB
/
calendar_events.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
// Get a result set of squad events for the given squad, player and day.
// This is only useful for sports teams.
//
function getSquadEvents($date, $squad, $plid) {
return sqlStatement("SELECT e.pc_eid, e.pc_hometext, " .
"e.pc_eventDate, e.pc_endDate, e.pc_startTime, " .
"e.pc_duration, e.pc_recurrtype, e.pc_recurrspec, " .
"p.pid, p.minutes, p.fitness_related " .
"FROM libreehr_postcalendar_events AS e " .
"JOIN libreehr_postcalendar_categories AS c ON " .
"c.pc_catdesc LIKE 'Squad=$squad' AND c.pc_catid = e.pc_catid " .
"LEFT JOIN player_event AS p ON " .
"p.pid = '$plid' AND p.date = '$date' AND p.pc_eid = e.pc_eid " .
"WHERE ((e.pc_endDate >= '$date' AND e.pc_eventDate <= '$date') OR " .
"(e.pc_endDate = '0000-00-00' AND e.pc_eventDate = '$date')) " .
"ORDER BY e.pc_startTime, e.pc_eid");
}
// Determine if the specified event applies to the specified date (YYYY-MM-DD).
//
function eventMatchesDay($row, $date) {
$time1 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2), substr($date, 0, 4));
$time2 = $time1 + (24 * 60 * 60);
$thistime = strtotime($row['pc_eventDate'] . " 00:00:00");
if ($row['pc_recurrtype']) {
preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
$repeattype = $matches[1];
preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
$repeatfreq = $matches[1];
if ($row['pc_recurrtype'] == 2) {
// Repeat type is 2 so frequency comes from event_repeat_on_freq.
preg_match('/"event_repeat_on_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
$repeatfreq = $matches[1];
}
if (! $repeatfreq) $repeatfreq = 1;
preg_match('/"event_repeat_on_num";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
$my_repeat_on_num = $matches[1];
preg_match('/"event_repeat_on_day";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
$my_repeat_on_day = $matches[1];
$endtime = strtotime($row['pc_endDate'] . " 00:00:00") + (24 * 60 * 60);
if ($endtime > $time2) $endtime = $time2;
// Shortcut for events that repeat every day.
if ($repeattype == 0 && $repeatfreq == 1)
return ($thistime < $time2 && $endtime >= $time1);
$repeatix = 0;
while ($thistime < $endtime) {
if ($repeatix == 0 && $thistime >= $time1) return true;
if (++$repeatix >= $repeatfreq) $repeatix = 0;
$adate = getdate($thistime);
if ($row['pc_recurrtype'] == 2) {
// Need to skip to nth or last weekday of the next month.
$adate['mon'] += 1;
if ($adate['mon'] > 12) {
$adate['year'] += 1;
$adate['mon'] -= 12;
}
if ($my_repeat_on_num < 5) { // not last
$adate['mday'] = 1;
$dow = jddayofweek(cal_to_jd(CAL_GREGORIAN, $adate['mon'], $adate['mday'], $adate['year']));
if ($dow > $my_repeat_on_day) $dow -= 7;
$adate['mday'] += ($my_repeat_on_num - 1) * 7 + $my_repeat_on_day - $dow;
}
else { // last weekday of month
$adate['mday'] = cal_days_in_month(CAL_GREGORIAN, $adate['mon'], $adate['year']);
$dow = jddayofweek(cal_to_jd(CAL_GREGORIAN, $adate['mon'], $adate['mday'], $adate['year']));
if ($dow < $my_repeat_on_day) $dow += 7;
$adate['mday'] += $my_repeat_on_day - $dow;
}
} // end recurrtype 2
else { // recurrtype 1
if ($repeattype == 0) { // daily
$adate['mday'] += 1;
} else if ($repeattype == 1) { // weekly
$adate['mday'] += 7;
} else if ($repeattype == 2) { // monthly
$adate['mon'] += 1;
} else if ($repeattype == 3) { // yearly
$adate['year'] += 1;
} else if ($repeattype == 4) { // work days
if ($adate['wday'] == 5) // if friday, skip to monday
$adate['mday'] += 3;
else if ($adate['wday'] == 6) // saturday should not happen
$adate['mday'] += 2;
else
$adate['mday'] += 1;
} else {
die("Invalid repeat type '$repeattype'");
}
} // end recurrtype 1
$thistime = mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']);
}
} else { // not recurring
return ($thistime >= $time1 && $thistime < $time2);
}
return false; // repeating event did not match
}
?>