insurance allocation report clean up
[openemr.git] / library / calendar_events.inc.php
blob4dcf74ee072985a783144fe233ad6e81aea666cd
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)
7 return sqlStatement("SELECT e.pc_eid, e.pc_hometext, " .
8 "e.pc_eventDate, e.pc_endDate, e.pc_startTime, " .
9 "e.pc_duration, e.pc_recurrtype, e.pc_recurrspec, " .
10 "p.pid, p.minutes, p.fitness_related " .
11 "FROM openemr_postcalendar_events AS e " .
12 "JOIN openemr_postcalendar_categories AS c ON " .
13 "c.pc_catdesc LIKE 'Squad=$squad' AND c.pc_catid = e.pc_catid " .
14 "LEFT JOIN player_event AS p ON " .
15 "p.pid = '$plid' AND p.date = '$date' AND p.pc_eid = e.pc_eid " .
16 "WHERE ((e.pc_endDate >= '$date' AND e.pc_eventDate <= '$date') OR " .
17 "(e.pc_endDate = '0000-00-00' AND e.pc_eventDate = '$date')) " .
18 "ORDER BY e.pc_startTime, e.pc_eid");
21 // Determine if the specified event applies to the specified date (YYYY-MM-DD).
23 function eventMatchesDay($row, $date)
25 $time1 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2), substr($date, 0, 4));
26 $time2 = $time1 + (24 * 60 * 60);
28 $thistime = strtotime($row['pc_eventDate'] . " 00:00:00");
29 if ($row['pc_recurrtype']) {
30 preg_match('/"event_repeat_freq_type";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
31 $repeattype = $matches[1];
33 preg_match('/"event_repeat_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
34 $repeatfreq = $matches[1];
35 if ($row['pc_recurrtype'] == 2) {
36 // Repeat type is 2 so frequency comes from event_repeat_on_freq.
37 preg_match('/"event_repeat_on_freq";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
38 $repeatfreq = $matches[1];
41 if (! $repeatfreq) {
42 $repeatfreq = 1;
45 preg_match('/"event_repeat_on_num";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
46 $my_repeat_on_num = $matches[1];
48 preg_match('/"event_repeat_on_day";s:1:"(\d)"/', $row['pc_recurrspec'], $matches);
49 $my_repeat_on_day = $matches[1];
51 $endtime = strtotime($row['pc_endDate'] . " 00:00:00") + (24 * 60 * 60);
52 if ($endtime > $time2) {
53 $endtime = $time2;
56 // Shortcut for events that repeat every day.
57 if ($repeattype == 0 && $repeatfreq == 1) {
58 return ($thistime < $time2 && $endtime >= $time1);
61 $repeatix = 0;
62 while ($thistime < $endtime) {
63 if ($repeatix == 0 && $thistime >= $time1) {
64 return true;
67 if (++$repeatix >= $repeatfreq) {
68 $repeatix = 0;
71 $adate = getdate($thistime);
73 if ($row['pc_recurrtype'] == 2) {
74 // Need to skip to nth or last weekday of the next month.
75 $adate['mon'] += 1;
76 if ($adate['mon'] > 12) {
77 $adate['year'] += 1;
78 $adate['mon'] -= 12;
81 if ($my_repeat_on_num < 5) { // not last
82 $adate['mday'] = 1;
83 $dow = jddayofweek(cal_to_jd(CAL_GREGORIAN, $adate['mon'], $adate['mday'], $adate['year']));
84 if ($dow > $my_repeat_on_day) {
85 $dow -= 7;
88 $adate['mday'] += ($my_repeat_on_num - 1) * 7 + $my_repeat_on_day - $dow;
89 } else { // last weekday of month
90 $adate['mday'] = cal_days_in_month(CAL_GREGORIAN, $adate['mon'], $adate['year']);
91 $dow = jddayofweek(cal_to_jd(CAL_GREGORIAN, $adate['mon'], $adate['mday'], $adate['year']));
92 if ($dow < $my_repeat_on_day) {
93 $dow += 7;
96 $adate['mday'] += $my_repeat_on_day - $dow;
98 } // end recurrtype 2
100 else { // recurrtype 1
101 if ($repeattype == 0) { // daily
102 $adate['mday'] += 1;
103 } else if ($repeattype == 1) { // weekly
104 $adate['mday'] += 7;
105 } else if ($repeattype == 2) { // monthly
106 $adate['mon'] += 1;
107 } else if ($repeattype == 3) { // yearly
108 $adate['year'] += 1;
109 } else if ($repeattype == 4) { // work days
110 if ($adate['wday'] == 5) { // if friday, skip to monday
111 $adate['mday'] += 3;
112 } else if ($adate['wday'] == 6) { // saturday should not happen
113 $adate['mday'] += 2;
114 } else {
115 $adate['mday'] += 1;
117 } else {
118 die("Invalid repeat type '$repeattype'");
120 } // end recurrtype 1
122 $thistime = mktime(0, 0, 0, $adate['mon'], $adate['mday'], $adate['year']);
124 } else { // not recurring
125 return ($thistime >= $time1 && $thistime < $time2);
128 return false; // repeating event did not match