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