function check_for_conflicts($dates, $duration, $hour, $minute, $participants, $login, $id) { global $single_user_login, $single_user; global $repeated_events, $limit_appts, $limit_appts_number; if (!count($dates)) { return false; } $evtcnt = array(); $sql = "SELECT distinct webcal_entry_user.cal_login, webcal_entry.cal_time," . "webcal_entry.cal_duration, webcal_entry.cal_name, " . "webcal_entry.cal_id, webcal_entry.cal_ext_for_id, " . "webcal_entry.cal_access, " . "webcal_entry_user.cal_status, webcal_entry.cal_date " . "FROM webcal_entry, webcal_entry_user " . "WHERE webcal_entry.cal_id = webcal_entry_user.cal_id " . "AND ("; for ($x = 0; $x < count($dates); $x++) { if ($x != 0) { $sql .= " OR "; } $sql .= "webcal_entry.cal_date = " . date("Ymd", $dates[$x]); } $sql .= ") AND webcal_entry.cal_time >= 0 " . "AND webcal_entry_user.cal_status IN ('A','W') AND ( "; if ($single_user == "Y") { $participants[0] = $single_user_login; } else { if (strlen($participants[0]) == 0) { // likely called from a form with 1 user $participants[0] = $login; } } for ($i = 0; $i < count($participants); $i++) { if ($i > 0) { $sql .= " OR "; } $sql .= " webcal_entry_user.cal_login = '******'"; } $sql .= " )"; // make sure we don't get something past the end date of the // event we are saving. //echo "SQL: $sql<P>"; $conflicts = ""; $res = dbi_query($sql); $found = array(); $count = 0; if ($res) { $time1 = sprintf("%d%02d00", $hour, $minute); $duration1 = sprintf("%d", $duration); while ($row = dbi_fetch_row($res)) { //Add to an array to see if it has been found already for the next part. $found[$count++] = $row[4]; // see if either event overlaps one another if ($row[4] != $id && (empty($row[5]) || $row[5] != $id)) { $time2 = $row[1]; $duration2 = $row[2]; $cntkey = $user . "-" . $row[8]; $evtcnt[$cntkey]++; $over_limit = 0; if ($limit_appts == "Y" && $limit_appts_number > 0 && $evtcnt[$cntkey] >= $limit_appts_number) { $over_limit = 1; } if ($over_limit || times_overlap($time1, $duration1, $time2, $duration2)) { $conflicts .= "<LI>"; if ($single_user == "Y") { $conflicts .= "{$row['0']}: "; } if ($row[6] == 'R' && $row[0] != $login) { $conflicts .= "(" . translate("Private") . ")"; } else { $conflicts .= "<A HREF=\"view_entry.php?id={$row['4']}"; if ($user != $login) { $conflicts .= "&user={$user}"; } $conflicts .= "\">{$row['3']}</A>"; } if ($duration2 == 24 * 60) { $conflicts .= " (" . translate("All day event") . ")"; } else { $conflicts .= " (" . display_time($time2); if ($duration2 > 0) { $conflicts .= "-" . display_time(add_duration($time2, $duration2)); } $conflicts .= ")"; } $conflicts .= " on " . date_to_str($row[8]); if ($over_limit) { $tmp = translate("exceeds limit of XXX events per day"); $tmp = str_replace("XXX", $limit_appts_number, $tmp); $conflicts .= " (" . $tmp . ")"; } } } } dbi_free_result($res); } else { echo translate("Database error") . ": " . dbi_error(); exit; } //echo "<br>hello"; for ($q = 0; $q < count($participants); $q++) { $time1 = sprintf("%d%02d00", $hour, $minute); $duration1 = sprintf("%d", $duration); //This date filter is not necessary for functional reasons, but it eliminates some of the //events that couldn't possibly match. This could be made much more complex to put more //of the searching work onto the database server, or it could be dropped all together to put //the searching work onto the client. $date_filter = "AND (webcal_entry.cal_date <= " . date("Ymd", $dates[count($dates) - 1]); $date_filter .= " AND (webcal_entry_repeats.cal_end IS NULL OR webcal_entry_repeats.cal_end >= " . date("Ymd", $dates[0]) . "))"; //Read repeated events for the participants only once for a participant for //for performance reasons. $repeated_events = query_events($participants[$q], true, $date_filter); //for ($dd=0; $dd<count($repeated_events); $dd++) { // echo $repeated_events[$dd]['cal_id'] . "<BR>"; //} for ($i = 0; $i < count($dates); $i++) { $dateYmd = date("Ymd", $dates[$i]); $list = get_repeating_entries($participants[$q], $dateYmd); $thisyear = substr($dateYmd, 0, 4); $thismonth = substr($dateYmd, 4, 2); for ($j = 0; $j < count($list); $j++) { //okay we've narrowed it down to a day, now I just gotta check the time... //I hope this is right... $row = $list[$j]; if ($row['cal_id'] != $id && $row['cal_ext_for_id'] != $id) { $time2 = $row['cal_time']; $duration2 = $row['cal_duration']; if (times_overlap($time1, $duration1, $time2, $duration2)) { $conflicts .= "<LI>"; if ($single_user != "Y") { $conflicts .= $row['cal_login'] . ": "; } if ($row['cal_access'] == 'R' && $row['cal_login'] != $login) { $conflicts .= "(" . translate("Private") . ")"; } else { $conflicts .= "<A HREF=\"view_entry.php?id=" . $row['cal_id']; if ($user != $login) { $conflicts .= "&user={$user}"; } $conflicts .= "\">" . $row['cal_name'] . "</A>"; } $conflicts .= " (" . display_time($time2); if ($duration2 > 0) { $conflicts .= "-" . display_time(add_duration($time2, $duration2)); } $conflicts .= ")"; $conflicts .= " on " . date("l, F j, Y", $dates[$i]); } } } } } return $conflicts; }
function check_for_conflicts($dates, $duration, $eventstart, $participants, $login, $id) { global $LIMIT_APPTS, $LIMIT_APPTS_NUMBER, $repeated_events, $single_user, $single_user_login, $jumpdate; $datecnt = count($dates); if (!$datecnt) { return false; } $conflicts = ''; $count = 0; $evtcnt = $found = $query_params = array(); $partcnt = count($participants); $hour = gmdate('H', $eventstart); $minute = gmdate('i', $eventstart); $allDayStr = translate('All day event'); $confidentialStr = translate('Confidential'); $exceedsStr = translate('exceeds limit of XXX events per day'); $onStr = translate('on'); $privateStr = translate('Private'); $sql = 'SELECT DISTINCT( weu.cal_login ), we.cal_time, we.cal_duration, we.cal_name, we.cal_id, we.cal_access, weu.cal_status, we.cal_date FROM webcal_entry we, webcal_entry_user weu WHERE we.cal_id = weu.cal_id AND ( '; for ($i = 0; $i < $datecnt; $i++) { $sql .= ($i != 0 ? ' OR ' : '') . 'we.cal_date = ' . gmdate('Ymd', $dates[$i]); } $sql .= ' ) AND we.cal_time >= 0 AND weu.cal_status IN ( \'A\',\'W\' ) AND ( '; if ($single_user == 'Y') { $participants[0] = $single_user_login; } else { if (strlen($participants[0]) == 0) { // Likely called from a form with 1 user. $participants[0] = $login; } } for ($i = 0; $i < $partcnt; $i++) { $sql .= ($i > 0 ? ' OR ' : '') . 'weu.cal_login = ?'; $query_params[] = $participants[$i]; } // Make sure we don't get something past the end date of the event we're saving. $res = dbi_execute($sql . ' )', $query_params); if ($res) { $duration1 = sprintf("%d", $duration); $time1 = sprintf("%d%02d00", $hour, $minute); while ($row = dbi_fetch_row($res)) { // Add to an array to see if it has been found already for the next part. $found[$count++] = $row[4]; // See if events overlaps one another. if ($row[4] != $id) { $cntkey = $row[0] . '-' . $row[7]; $duration2 = $row[2]; $time2 = sprintf("%06d", $row[1]); if (empty($evtcnt[$cntkey])) { $evtcnt[$cntkey] = 0; } else { $evtcnt[$cntkey]++; } $over_limit = $LIMIT_APPTS == 'Y' && $LIMIT_APPTS_NUMBER > 0 && $evtcnt[$cntkey] >= $LIMIT_APPTS_NUMBER ? 1 : 0; if ($over_limit || times_overlap($time1, $duration1, $time2, $duration2)) { $conflicts .= ' <li>'; if ($single_user != 'Y') { user_load_variables($row[0], 'conflict_'); $conflicts .= $GLOBALS['conflict_fullname'] . ': '; } $conflicts .= ($row[5] == 'C' && $row[0] != $login && !$is_assistant && !$is_nonuser_admin ? '(' . $confidentialStr . ')' : ($row[5] == 'R' && $row[0] != $login ? '( ' . $privateStr . ')' : '<a href="view_entry.php?id=' . $row[4] . ($row[0] != $login ? '&user='******'') . '">' . $row[3] . '</a>')) . ($duration2 == 1440 && $time2 == 0 ? ' (' . $allDayStr . ')' : ' (' . display_time($row[7] . $time2) . ($duration2 > 0 ? '-' . display_time($row[7] . add_duration($time2, $duration2)) : '') . ')') . ' ' . $onStr . ' ' . date_to_str(date('Ymd', date_to_epoch($row[7] . sprintf("%06d", $row[1])))) . ($over_limit ? ' (' . str_replace('XXX', $LIMIT_APPTS_NUMBER, $exceedsStr) . ')' : '') . '</li>'; } } } dbi_free_result($res); } else { db_error(true); } for ($q = 0; $q < $partcnt; $q++) { // Read repeated events only once for a participant for performance reasons. $jumpdate = gmdate('Ymd', $dates[count($dates) - 1]); $repeated_events = query_events($participants[$q], true, 'AND ( we.cal_date <= ' . $jumpdate . ' AND ( wer.cal_end IS NULL OR wer.cal_end >= ' . gmdate('Ymd', $dates[0]) . ' ) )'); for ($i = 0; $i < $datecnt; $i++) { $dateYmd = gmdate('Ymd', $dates[$i]); $list = get_repeating_entries($participants[$q], $dateYmd); for ($j = 0, $listcnt = count($list); $j < $listcnt; $j++) { // OK we've narrowed it down to a day, now I just gotta check the time... // I hope this is right... $row = $list[$j]; if ($row->getID() != $id && !in_array($row->getID(), $found) && ($row->getExtForID() == '' || $row->getExtForID() != $id)) { $time2 = sprintf("%06d", $row->getTime()); $duration2 = $row->getDuration(); if (times_overlap($time1, $duration1, $time2, $duration2)) { $conflicts .= ' <li>'; if ($single_user != 'Y') { user_load_variables($row->getLogin(), 'conflict_'); $conflicts .= $GLOBALS['conflict_fullname'] . ': '; } $conflicts .= ($row->getAccess() == 'C' && $row->getLogin() != $login && !$is_assistant && !$is_nonuser_admin ? '(' . $confidentialStr . ')' : ($row->getAccess() == 'R' && $row->getLogin() != $login ? '(' . $privateStr . ')' : '<a href="view_entry.php?id=' . $row->getID() . (!empty($user) && $user != $login ? '&user='******'') . '">' . $row->getName() . '</a>')) . ' (' . display_time($row->getDate() . $time2) . ($duration2 > 0 ? '-' . display_time($row->getDate() . add_duration($time2, $duration2)) : '') . ')' . ' ' . $onStr . ' ' . date_to_str($dateYmd) . '</li>'; } } } } } return $conflicts; }