<? require_once("../diary/util.php"); ?>
<?
function getevents($dbdate1, $dbdate2, $sqlWhere) {
global $myDb, $myCompId, $daysToView, $linkedItemsSeen, $currentDate, $diaryevent, $HTTP_SESSION_VARS;
global $eventtype, $viewFor, $diaryType, $diaryContent, $starttime, $endtime, $diaryId, $personName, $notTime, $i, $currentDay, $currentMonth, $currentYear, $userId, $diaryMinutes, $smartdb, $dayToLookAt, $prodcatcolour;
// do non-recurring events first!
if(getApplicationObject("webofficemulviewbook") == "yes") {
// Long left-joined inner query is to get colour from the shop_categories table - todo this may be put into the diary table
$mySQL = "SELECT diary.*, colour, department, firstname, lastname, eventtype, users.isDeleted AS userIsDeleted FROM diary LEFT JOIN (select shop_products.id AS productId, colour from shop_products, shop_products_categories, shop_categories where shop_products.id=shop_products_categories.prodId and shop_products_categories.categoryid=shop_categories.id) XX ON diary.productid=XX.productId LEFT JOIN users ON diary.userid=users.id WHERE diary.isDeleted=0 AND ".$sqlWhere." starttime < '".$dbdate1."' AND endtime > '".$dbdate2."' AND recurring=-1 AND diary.companyId=".$myCompId." ORDER BY starttime";
} elseif($HTTP_SESSION_VARS["is-smart"] != "true") {
$mySQL = "SELECT diary.*, department, firstname, lastname, eventtype, users.isDeleted AS userIsDeleted FROM diary LEFT JOIN users ON diary.userid=users.id WHERE diary.isDeleted=0 AND ".$sqlWhere." starttime < '".$dbdate1."' AND endtime > '".$dbdate2."' AND recurring=-1 AND diary.companyId=".$myCompId." ORDER BY starttime";
} else {
$smartdbs = array("ballinger", "smart", "clarendons");
$mySQL = "";
foreach($smartdbs as $smartdb) {
$mySQL.= ($smartdb == "ballinger" ? "" : " UNION ")."SELECT '".$smartdb."' AS smartdb, diary.*, department, firstname, lastname, eventtype, users.isDeleted AS userIsDeleted FROM ".$smartdb.".diary LEFT JOIN ".$smartdb.".users ON ".$smartdb.".diary.userid=".$smartdb.".users.id WHERE ".$smartdb.".diary.isDeleted=0 AND ".$sqlWhere." starttime < '".$dbdate1."' AND endtime > '".$dbdate2."' AND recurring=-1 AND diary.companyId=".$myCompId;
}
$mySQL.= " ORDER BY starttime";
}
$rows = gendb_query($mySQL, $myDb) or die(gendb_error());
while ($row = gendb_fetch_assoc($rows)) {
$userIsDeleted = $row["userIsDeleted"];
if($userIsDeleted == 1) continue;
$diaryId = $row["id"];
$userId = $row["userid"];
$starttime = strtotime($row["starttime"]);
$endtime = strtotime($row["endtime"]);
$diaryevent = $row["event"];
$notTime = $row["notificationTime"];
$smartdb = $row["smartdb"];
$linkedId = $row["linkedId"];
$eventtype = $row["eventtype"];
if($row["colour"] != "") {
$prodcatcolour = "#".substr("000000".dechex($row["colour"]),-6);
if($eventtype == 990) { // completed booking
// old code make it darker (but still same colour)
$red = hexdec(substr($prodcatcolour, 1, 2));
$green = hexdec(substr($prodcatcolour, 3, 2));
$blue = hexdec(substr($prodcatcolour, 5, 2));
$red = (int) ($red / 2);
$green = (int) ($green / 2);
$blue = (int) ($blue / 2);
$red = substr("00".dechex($red), -2);
$green = substr("00".dechex($green), -2);
$blue = substr("00".dechex($blue), -2);
$prodcatcolour = "#".$red.$green.$blue;
// just make it black
$red = "00";
$green = "00";
$blue = "00";
$prodcatcolour = "#".$red.$green.$blue;
} elseif($eventtype == 987) { // no-show booking
$red = "70";
$green = "70";
$blue = "70";
$prodcatcolour = "#".$red.$green.$blue;
}
} else {
$prodcatcolour = "";
}
if($linkedId > 0) {
$personName = "Multiple people";
} elseif($userId == 0) {
$personName = "All";
} else {
$personName = $row["firstname"]." ".$row["lastname"];
}
if($linkedId == 0 || ($linkedId > 0 && $linkedItemsSeen[$linkedId] == "")) {
if($daysToView == 1) {
updateDiaryContent($currentDate, $starttime, $endtime, $diaryevent, $diaryId, $personName, $eventtype, true, $notTime);
} else {
updateDiaryContent($currentDate, $starttime, $endtime, $diaryevent, $diaryId, $personName, $eventtype, false, $notTime);
}
}
if($linkedId > 0) {
$linkedItemsSeen[$linkedId] = "1";
}
}
gendb_free_result($rows);
// recurring events now! - NOTE THEY WILL NOT SPAN DAYS!!
if($HTTP_SESSION_VARS["is-smart"] != "true") {
$mySQL = "SELECT diary.*, department, firstname, lastname, eventtype, users.isDeleted AS userIsDeleted FROM diary LEFT JOIN users ON diary.userid=users.id WHERE diary.isDeleted=0 AND ".$sqlWhere." starttime < '".$dbdate1."' AND recurring!=-1 AND diary.companyId=".$myCompId." ORDER BY starttime";
} else {
$smartdbs = array("ballinger", "smart", "clarendons");
$mySQL = "";
foreach($smartdbs as $smartdb) {
$mySQL.= ($smartdb == "ballinger" ? "" : " UNION ")."SELECT '".$smartdb."' AS smartdb, diary.*, department, firstname, lastname, eventtype, users.isDeleted AS userIsDeleted FROM ".$smartdb.".diary LEFT JOIN ".$smartdb.".users ON ".$smartdb.".diary.userid=".$smartdb.".users.id WHERE ".$smartdb.".diary.isDeleted=0 AND ".$sqlWhere." starttime < '".$dbdate1."' AND recurring!=-1 AND diary.companyId=".$myCompId;
}
$mySQL.= " ORDER BY starttime";
}
$rows = gendb_query($mySQL, $myDb) or die(gendb_error());
while ($row = gendb_fetch_assoc($rows)) {
$userIsDeleted = $row["userIsDeleted"];
if($userIsDeleted == 1) continue;
$diaryId = $row["id"];
$userId = $row["userid"];
$starttime = strtotime($row["starttime"]);
$endtime = strtotime($row["endtime"]);
$diaryevent = $row["event"];
$notTime = $row["notificationTime"];
$recurring = $row["recurring"];
$smartdb = $row["smartdb"];
$linkedId = $row["linkedId"];
if($linkedId > 0) {
$personName = "Multiple people";
} elseif($userId == 0) {
$personName = "All";
} else {
$personName = $row["firstname"]." ".$row["lastname"];
}
$eventtype = $row["eventtype"];
$numToLoopFrom = 7;
if($daysToView == 31) $numToLoopFrom = getDaysInMonth($currentMonth, $currentYear) - 1;
if($linkedId == 0 || ($linkedId > 0 && $linkedItemsSeen[$linkedId] == "")) {
if($recurring == 3) { //daily
for($i = 0; $i < 31; $i++) {
$dayToLookAt = DateAdd("d", $i + 1, $currentDate);
if($dayToLookAt>$starttime) {
addDayFromRecurring();
}
}
} elseif($recurring == 2) { //monthly
// TODO PUT A COMMENT HERE! - AND BELOW!!!... and make it work :(
if(date("d", $starttime) > getDaysInMonth($currentMonth, $currentYear)) {
$starttime = DateAdd("d", getDaysInMonth($currentMonth, $currentYear)-date("d", $starttime), $starttime);
}
for($i = $numToLoopFrom; $i >= 0; $i--) {
$dayToLookAt = DateAdd("d", $i, $currentDate);
if(date("d",$dayToLookAt) == date("d", $starttime)) {
addDayFromRecurring();
break;
}
}
} elseif($recurring == 5) { //quarterly
// TODO PUT A COMMENT HERE! - AND BELOW!!!... and make it work :(
if(date("d", $starttime) > getDaysInMonth($currentMonth, $currentYear)) {
$starttime = DateAdd("d", getDaysInMonth($currentMonth, $currentYear)-date("d", $starttime), $starttime);
}
for($i = $numToLoopFrom; $i >= 0; $i--) {
$dayToLookAt = DateAdd("d", $i, $currentDate);
if((date("d",$dayToLookAt) == date("d", $starttime)) && (date("n", $dayToLookAt) % 3 == date("n", $starttime) % 3)) {
addDayFromRecurring();
break;
}
}
} elseif($recurring == 1) { //weekly
for($i = 0; $i < $numToLoopFrom; $i++) {
$dayToLookAt = DateAdd("d", $i, $currentDate);
if(date("w",$dayToLookAt) == date("w", $starttime)) {
addDayFromRecurring();
// if we're not in weekly view, and we've found a weekly one, may as well stop!
if($daysToView != 31) break;
}
}
} elseif($recurring == 4) { //annually
for($i = 0; $i < $numToLoopFrom; $i++) {
$dayToLookAt = DateAdd("d", $i, $currentDate);
if((date("d",$dayToLookAt) == date("d", $starttime)) && (date("m",$dayToLookAt) == date("m", $starttime))) {
addDayFromRecurring();
break;
}
}
}
}
if($linkedId > 0) {
$linkedItemsSeen[$linkedId] = "1";
}
}
gendb_free_result($rows);
}
?>