'Termin', 2 => 'IBN Xinon', 3 => 'IBN ESTMK', 4 => 'Snopp', 5 => 'Störung', 6 => 'Support Gespräch', 7 => 'IBN SBIDI', ); public static $standardCalendarColors = [ '#b3d5f3', '#d0e2c0', '#bac2d4', '#c6d4c0', '#e3e3e3', '#acdce5', '#cdb6c0', '#aae0e1', '#d9d9d9', '#d9e5d5', '#c8a7d5', '#aad8d0', '#dcadc3', '#d9e8e5', '#e4d8d5', '#b4dcc5', '#b9d1e2', '#a3c1c2', '#cdd2b9', '#e3c8ce', '#aae2c5', '#c7d0cc', '#d0c0da', '#dbdfc8', '#b4e3d7', '#c3e4e3', '#c5c4e3', '#d0b7e5', '#b6e98c', '#e3d1de', '#dbbeb3', '#e9aebc', '#b5cae7', '#dec2d4', '#aebbd5', '#e2baaf', '#b6ebd5', '#a9c4bb', '#d7d2c5', '#d7e3d4', '#b3d5f3', '#d0e2c0', '#bac2d4', '#c6d4c0', '#e3e3e3', '#acdce5', '#cdb6c0', '#aae0e1', '#d9d9d9', '#d9e5d5', '#c8a7d5', '#aad8d0', '#dcadc3', '#d9e8e5', '#e4d8d5', '#b4dcc5', '#b9d1e2', '#a3c1c2', '#cdd2b9', '#e3c8ce', '#aae2c5', '#c7d0cc', '#d0c0da', '#dbdfc8', '#b4e3d7', '#c3e4e3', '#c5c4e3', '#d0b7e5', '#b6e98c', '#e3d1de', '#dbbeb3', '#e9aebc', '#b5cae7', '#dec2d4', '#aebbd5', '#e2baaf', '#b6ebd5', '#a9c4bb', '#d7d2c5', '#d7e3d4' ]; public static $eventCategories = array( 2 => 'Gelbe Kategorie', //Xinon IBN 3 => 'Grüne Kategorie', //ESTMK IBN 4 => 'Lila Kategorie', //Snopp IBN 5 => 'Rote Kategorie', //Störung 6 => 'Rote Kategorie', //Störung 7 => 'Orange Kategorie', //Störung 98 => 'Gru00fcne Kategorie', //ESTMK IBN 99 => 'Gr\u00fcne Kategorie', //ESTMK IBN ); public static $specialCalendarColors = array(997 => '#bd0000', 998 => '#8000A3', 999 => '#08769b'); public static function convertToSummertime($timestamp) { $date = new DateTime(); $date->setTimestamp($timestamp); $date->setTimezone(new DateTimeZone('Europe/Berlin')); if ($date->format('I') == 1) { $cest = 7200; } else { $cest = 3600; } $unixtimestamp = $date->getTimestamp() + $cest; return date('Y-m-d H:i:s', $unixtimestamp); } public static function convertMillisecondsToTimestamp($milliseconds) { $timestamp = $milliseconds / 1000; $date = new DateTime(); $date->setTimestamp($timestamp); $date->setTimezone(new DateTimeZone('Europe/Berlin')); if ($date->format('I') == 1) { $offset = 7200; } else { $offset = 3600; } return $timestamp - $offset; } public static function replace_unicode_sequences($string) { $unicode_map = [ '\u00f6' => 'ö', 'u00f6' => 'ö', '\u00fc' => 'ü', 'u00fc' => 'ü', '\u00e4' => 'ä', 'u00e4' => 'ä', '\u00df' => 'ß', 'u00df' => 'ß', '\u00e9' => 'é', '\u00e0' => 'à', '\u00f3' => 'ó', ]; foreach ($unicode_map as $unicode => $char) { $string = str_replace($unicode, $char, $string); } return $string; } public static function dbKalender() { if (!TT_CALENDAR_ENABLE) { $json['errormessage'] = "Calendar is is disabled."; $json['success'] = false; $json = json_encode($json); echo trim($json); die(); } $db = FronkDB::singleton(TT_CALENDAR_HOSTNAME, TT_CALENDAR_USERNAME, TT_CALENDAR_PASSWORD, TT_CALENDAR_DATABASE); return $db; } public static function searchCalendarEvents($r) { $dbcal = self::dbKalender(); $termstring = trim($r->term); $calendars = $r->calendars; $visibleCalendarTypes = $r->visibleCalendarTypes; $termExplode = explode(" ", $termstring); $where = ""; foreach ($termExplode as $term) { $where .= " AND (name LIKE '%" . $term . "%' OR location LIKE '%" . $term . "%') "; } $where .= " AND calendar_id IN (" . implode(",", $calendars) . ")"; if ($visibleCalendarTypes) { $where .= " AND event_type IN (" . implode(",", $visibleCalendarTypes) . ")"; } if ($r->visibleCancellation === "0") { $where .= " AND ((canceld = '0' AND `cal_events`.name NOT LIKE 'Abgesagt:%' AND `cal_events`.name NOT LIKE 'Absage%' AND `cal_events`.name NOT LIKE 'Canceled%') OR event_type = '1')"; } $res = $dbcal->select("cal_events", "id, uuid, calendar_id, user_id, start_time, end_time, timezone, all_day_event, name, description, location, repeat_end_time, reminder, ctime, mtime, muser_id, busy, status, resource_event_id, private, rrule, background, files_folder_id, read_only, category_id, exception_for_event_id, recurrence_id, is_organizer,event_type", "1=1 $where ORDER BY name"); $rows = array(); while ($data = $dbcal->fetch_array($res)) { if ($data['private'] != 1 || $data['calendar_id'] == $r->cal_id) { if ($data['location']) { $searchName = $data['name'] . " @" . $data['location']; } else { $searchName = $data['name']; } $id = $data['id'] . ";" . date("Y-m-d", $data['start_time']); $rows[] = array( 'id' => $id, 'text' => $searchName . " (" . date("Y-m-d", $data['start_time']) . ")" ); } } $json['incomplete_results'] = false; $json['total_count'] = count($rows); $json['items'] = $rows; $json = json_encode($json); echo trim($json); die(); } public static function getCalendarEvents($me, $id = 0, $r = 0, $returnvalues = false) { $rrulefreq = array('daily' => 'DAILY', 'weekly' => 'WEEKLY', 'relativeMonthly' => 'MONTHLY', 'yearly' => 'YEARLY', 'absoluteMonthly' => 'absoluteMonthly'); $calendar = self::search(array("user_id" => $me)); $standardCalendarColors = CalendarModel::$standardCalendarColors; $calendarColors = json_decode($calendar[0]->colors, true); $calenderRights = json_decode($calendar[0]->rights, true); $colorCounter = 0; foreach ($calenderRights as $key => $value) { if (!$calendarColors[$key]['bgcolor']) { $calendarColors[$key]['bgcolor'] = $standardCalendarColors[$colorCounter];; $calendarColors[$key]['txtcolor'] = '#000'; $colorCounter++; } } $dbcal = self::dbKalender(); $req = $dbcal->select("cal_events_attachments", "id,cal_events_id,name,size,contentType", ""); $oldcalevent = ""; while ($data = $dbcal->fetch_array($req)) { if ($oldcalevent != $data['cal_events_id']) { $counter = 0; } $attachments[$data['cal_events_id']]['attachments'][$counter]['name'] = $data['name']; $attachments[$data['cal_events_id']]['attachments'][$counter]['id'] = $data['id']; $attachments[$data['cal_events_id']]['attachments'][$counter]['size'] = $data['size']; $attachments[$data['cal_events_id']]['attachments'][$counter]['contentType'] = $data['contentType']; $oldcalevent = $data['cal_events_id']; $counter++; } if ($id != 0) { $where = " AND `cal_events`.id='" . $id . "'"; } else { $where = ""; } $visibleCalendars = $r->visibleCalendars; $visibleCalendarTypes = $r->visibleCalendarTypes; if ($visibleCalendars) { $where .= " AND calendar_id IN (" . implode(",", $visibleCalendars) . ")"; $whereTimeRecording = " AND `Calendar`.`go_calendar_id` IN (" . implode(",", $visibleCalendars) . ")"; } if ($visibleCalendarTypes) { $where .= " AND event_type IN (" . implode(",", $visibleCalendarTypes) . ")"; } if ($r->visibleCancellation === "0") { $where .= " AND ((canceld = '0' AND `cal_events`.name NOT LIKE 'Abgesagt:%' AND `cal_events`.name NOT LIKE 'Absage%' AND `cal_events`.name NOT LIKE 'Canceled%') OR event_type = '1')"; } $sql = "SELECT `cal_events`.id,`cal_events`.categories, uuid, calendar_id, `cal_events`.user_id, start_time, end_time, timezone, all_day_event, `cal_events`.name,`cal_calendars`.name calendar_name, description, location, repeat_end_time, reminder, ctime,cname, mtime,mname, muser_id, busy, status, resource_event_id, private, rrule, `cal_events`.background, `cal_events`.files_folder_id, read_only, category_id, exception_for_event_id, recurrence_id, is_organizer,event_type,busy,recurrence,rrule_events,canceld FROM cal_events INNER JOIN `cal_calendars` ON (`cal_calendars`.`id`=`cal_events`.`calendar_id`) WHERE 1=1 $where ORDER BY start_time DESC"; $res = $dbcal->query($sql); if ($dbcal->num_rows($res)) { while ($data = $dbcal->fetch_array($res)) { if ($data['categories']) { $categories = json_decode($data['categories'], true); } else { $categories = []; } unset($byweekday); $rrule = false; if ($attachments[$data['uuid']]) { $attachment = 1; $attachmentLinks = json_encode($attachments[$data['uuid']]['attachments']); } else { $attachment = 0; $attachmentLinks = ""; } if (!empty($categories) && in_array("Abwesenheit", $categories)) { continue; } if ($data['all_day_event'] == 1) { if (in_array("Feiertag", $categories)) { continue; } $starttime = date("Y-m-d", $data['start_time']); $endtime = date("Y-m-d", $data['end_time']); } else { $starttime = date("Y-m-d H:i", $data['start_time']); $endtime = date("Y-m-d H:i", $data['end_time']); } if (!empty($fetch['location'])) { $name = $data['name'] . " @ " . $data['location']; } else { $name = $data['name']; } if ($data['recurrence']) { $recurrence = json_decode($data['recurrence'], true); $rrule_events = json_decode($data['rrule_events'], true); foreach ($rrule_events as $key => $value) { if ($r->visibleCancellation === "0" && (str_starts_with(trim($value['subject']), "Abgesagt:") || str_starts_with(trim($value['subject']), "Abgesage:") || str_starts_with(trim($value['subject']), "Canceled:"))) { unset($rrule_events[$key]); continue; } $rrule_events[$key]['start'] = self::convertToSummertime(strtotime($value['start'])); $rrule_events[$key]['end'] = self::convertToSummertime(strtotime($value['end'])); } if ($rrulefreq[$recurrence['pattern']['type']]) { unset ($byweekday); $freq = $rrulefreq[$recurrence['pattern']['type']]; if (isset($recurrence['pattern']['daysOfWeek'])) { foreach ($recurrence['pattern']['daysOfWeek'] as $value) { $byweekday[] = strtolower(substr($value, 0, 2)); } } $duration = ($data['end_time'] - $data['start_time']) * 1000; $until = $recurrence['range']['endDate']; if ($until == "0001-01-01") { $until = strtotime("+3 year", time()); $until = date("Y-m-d", $until); } else { $until = date("Y-m-d", strtotime($recurrence['range']['endDate']) + 86400); } $rrule = [ 'freq' => $freq, 'interval' => $recurrence['pattern']['interval'], 'byweekday' => $byweekday, 'dtstart' => date("Y-m-d\TH:i", $data['start_time']), 'until' => $until ]; if ($freq == "MONTHLY") { $bystpos = array("first" => 1, "second" => 2, "third" => 3, "fourth" => 4, "last" => -1); $rrule['bysetpos'] = $bystpos[$recurrence['pattern']['index']]; } } } else { $rrule = false; $rrule_events = false; } if ($calendarColors[$data['calendar_id']]['bgcolor']) { $bgcolor = $calendarColors[$data['calendar_id']]['bgcolor']; $txtcolor = $calendarColors[$data['calendar_id']]['txtcolor']; } else { $bgcolor = $standardCalendarColors[$colorCounter]; $txtcolor = "#000"; $colorCounter++; } $location = $data['location']; $eventtype = $data['event_type']; $description = $data['description']; if ($calenderRights[$data['calendar_id']]) { $rights = $calenderRights[$data['calendar_id']]; $CalendarUsers[$data['calendar_id']] = $data['calendar_name']; if ($data['private'] == 1 && $calendar[0]->go_calendar_id != $data['calendar_id']) { $name = "Privat"; $attachment = 0; $attachmentLinks = ""; $location = ""; $eventtype = "1"; $description = ""; } $rows[] = array( 'id' => array('id' => $data['id']), 'cstart' => array('cstart' => $starttime), 'cend' => array('cend' => $endtime), 'ccategory' => array('ccategory' => $name), 'category' => array('category' => $name), 'calendar_id' => array('calendar_id' => $data['calendar_id']), 'bgColor' => array('bgColor' => $bgcolor), 'txtColor' => array('txtColor' => $txtcolor), 'rights' => array('rights' => $rights, 'order' => $rights), 'location' => array('location' => $location), 'busy' => array('busy' => $data['busy']), 'privateflag' => array('privateflag' => $data['private']), 'allDay' => array('allDay' => $data['all_day_event']), 'rrule' => array('rrule' => $rrule), 'rrule_events' => array('rrule_events' => $rrule_events), 'duration' => array('duration' => $duration), 'event_type' => array('event_type' => $eventtype), 'description' => array('description' => ($description)), 'attachment' => array('attachment' => $attachment), 'attachments' => array('attachments' => $attachmentLinks), 'calendar_name' => array('calendar_name' => $data['calendar_name']), 'ctime' => array('ctime' => date("d.m.Y H:i", $data['ctime'])), 'cname' => array('cname' => $data['cname']), 'mtime' => array('mtime' => date("d.m.Y H:i", $data['mtime'])), 'mname' => array('mname' => $data['mname']), 'isorganizer' => array('isorganizer' => $data['is_organizer']), 'busy' => array('busy' => $data['busy']), 'canceld' => array('canceld' => $data['canceld']), 'timerecording' => array('timerecording' => 0), ); } } if ($id == 0) { $db = FronkDB::singleton(); $sql = "SELECT `Timerecording`.`id`, `Timerecording`.`start` start_time, `Timerecording`.`end` end_time,`Calendar`.`go_calendar_id` calendar_id,`TimerecordingCategory`.`name`,`TimerecordingCategory`.`id` CategoryId,`Timerecording`.`create` ctime,`Timerecording`.`edit` mtime FROM `Timerecording` INNER JOIN `TimerecordingCategory` ON `TimerecordingCategory`.`id`=`Timerecording`.`timerecordingCategory_id` INNER JOIN `Calendar` ON `Calendar`.`user_id`=`Timerecording`.`user_id` WHERE `TimerecordingCategory`.`hourday`!='1' AND `TimerecordingCategory`.`hourday`!='7' AND `TimerecordingCategory`.`hourday`!='5' $whereTimeRecording"; $res = $db->query($sql); if ($db->num_rows($res)) { while ($data = $db->fetch_array($res)) { if ($calendarColors[$data['calendar_id']]['bgcolor']) { $bgcolor = $calendarColors[$data['calendar_id']]['bgcolor']; $txtcolor = $calendarColors[$data['calendar_id']]['txtcolor']; } else { $bgcolor = $standardCalendarColors[$colorCounter]; $txtcolor = "#000"; $colorCounter++; } if ($data['CategoryId'] != 11) { $starttime = date("Y-m-d", $data['start_time']); $endtime = date("Y-m-d", $data['end_time']); if ($starttime != $endtime) { $endtime = $data['end_time'] + 86400; $endtime = date("Y-m-d", $endtime); } } else { $starttime = date("Y-m-d H:i", $data['start_time']); $endtime = date("Y-m-d H:i", $data['end_time']); } $rows[] = array( 'id' => array('id' => "9999" . $data['id']), 'category' => array('category' => $data['name']), 'ccategory' => array('ccategory' => $data['name']), 'cstart' => array('cstart' => $starttime), 'cend' => array('cend' => $endtime), 'calendar_id' => array('calendar_id' => $data['calendar_id']), 'ctime' => array('ctime' => date("d.m.Y H:i", $data['ctime'])), 'cname' => array('cname' => $CalendarUsers[$data['calendar_id']]), 'mtime' => array('mtime' => date("d.m.Y H:i", $data['mtime'])), 'mname' => array('mname' => $CalendarUsers[$data['calendar_id']]), 'description' => array('description' => ""), 'bgColor' => array('bgColor' => $bgcolor), 'txtColor' => array('txtColor' => $txtcolor), 'timerecording' => array('timerecording' => 1), 'calendar_name' => array('calendar_name' => $CalendarUsers[$data['calendar_id']]), ); } } } $json['success'] = true; $json['data'] = $rows; } else { $json['success'] = true; $json['errormessage'] = "No data found."; } $json = json_encode($json); if ($id == 0 && $returnvalues == false) { echo trim($json); die(); } else { return $json; } } public static function getCalendarEventAttachment($id) { $return = array(); $dbcal = self::dbKalender(); $req = $dbcal->select("cal_events_attachments", "id, cal_events_id,name,contentType,content,size", "1=1 AND id='" . $id . "' ORDER BY id"); if ($dbcal->num_rows($req)) { $data = $dbcal->fetch_array($req); $return = $data; } return $return; } public static function getCalendarEventAttachmenttmp($id, $name) { $return = array(); $dbcal = self::dbKalender(); $req = $dbcal->select("tmp_cal_events_attachments", "id,name,contentType,content,size", "1=1 AND newkey='" . $id . "' AND name='" . $name . "' ORDER BY id"); if ($dbcal->num_rows($req)) { $data = $dbcal->fetch_array($req); $return = $data; } return $return; } public static function getCalendarEventsReminder() { $dbcal = self::dbKalender(); $timecalcend = time() + 86400; $timecalcstart = $timecalcend - 3600; $sql = "SELECT `id`,`name`,`customer_info_send`,`start_time`,`end_time`,`event_type` FROM `cal_events` WHERE `customer_info_reminder`= 1 AND `customer_info_send` IS NOT NULL AND `event_type` > 1 AND `start_time` BETWEEN $timecalcstart AND $timecalcend AND is_organizer=1"; $res = $dbcal->query($sql); while ($data = $dbcal->fetch_array($res)) { $rows[] = array( 'id' => $data['id'], 'name' => $data['name'], 'customer_info_send' => $data['customer_info_send'], 'start_time' => $data['start_time'], 'end_time' => $data['end_time'], 'event_type' => $data['event_type'] ); } return ($rows); } public static function getCalendarEvent($id) { $calendar = self::search(array("user_id" => $me)); $dbcal = self::dbKalender(); $Calendars = self::getAll(); foreach ($Calendars as $key => $value) { $Allcalendar[$value->user->name] = $key; } $Allcalendar['Daniel Whoknows'] = 2; $Allcalendar['Stefan Plaschg'] = 26; $res = $dbcal->select("cal_events", "id, uuid, calendar_id, user_id, start_time, end_time, timezone, all_day_event, name, description, location, repeat_end_time, reminder, ctime, mtime, muser_id, busy, status, resource_event_id, private, rrule, background, files_folder_id, read_only, category_id, exception_for_event_id, recurrence_id, recurrence, is_organizer,event_type,customer,customer_info,customer_info_send,customer_info_reminder,busy,attendees,organizer,is_organizer,accepted,canceld", "1=1 AND id='" . $id . "' ORDER BY id"); if ($dbcal->num_rows($res)) { $data = $dbcal->fetch_array($res); if ($data['recurrence']) { $recurrence = self::generateDataRecurrence(json_decode($data['recurrence'], true)); } else { $recurrence = false; } $attachment = 0; if ($data['all_day_event'] == 1) { $starttime = date("Y-m-d", $data['start_time']); $endtime = date("Y-m-d", $data['end_time']); } else { $starttime = date("Y-m-d H:i", $data['start_time']); $endtime = date("Y-m-d H:i", $data['end_time']); } if (!empty($fetch['location'])) { $name = $data['name'] . " @ " . $data['location']; } else { $name = $data['name']; } $req = $dbcal->select("cal_events_attachments", "id, cal_events_id,name,contentType,size", "1=1 AND cal_events_id='" . $data['uuid'] . "' ORDER BY id"); $counter = 0; $contentType['application/pdf'] = 'fa-file-pdf'; $contentType['application/vnd.openxmlformats-officedocument.wordprocessingml.document'] = 'fa-file-doc'; $contentType['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'] = 'fa-file-xls'; $contentType['application/octet-stream'] = 'fa-file-csv'; $contentType['text/csv'] = 'fa-file-csv'; $contentType['application/vnd.openxmlformats-officedocument.presentationml.presentation'] = 'fa-file-ppt'; $contentType['application/zip'] = 'fa-file-zip'; $contentType['application/x-zip-compressed'] = 'fa-file-zip'; $contentType['application/x-rar-compressed'] = 'fa-file-archive'; $contentType['application/x-7z-compressed'] = 'fa-file-archive'; $contentType['application/x-tar'] = 'fa-file-archive'; $contentType['application/x-gzip'] = 'fa-file-archive'; $contentType['application/x-bzip2'] = 'fa-file-archive'; $contentType['text/xml'] = 'fa-file-xml'; $contentType['application/xml'] = 'fa-file-xml'; $contentType['audio/mpeg'] = 'fa-file-mp3'; $contentType['application/x-7z-compressed'] = 'fa-file-archive'; $contentType['image/png'] = 'fa-file-png'; $contentType['image/jpeg'] = 'fa-file-jpg'; while ($dataAttachments = $dbcal->fetch_array($req)) { $attachment = 1; $attachments[$counter]['name'] = $dataAttachments['name']; $attachments[$counter]['contentType'] = $dataAttachments['contentType']; $attachments[$counter]['size'] = $dataAttachments['size']; $attachments[$counter]['id'] = $dataAttachments['id']; if ($contentType[$dataAttachments['contentType']]) { $attachments[$counter]['icon'] = $contentType[$dataAttachments['contentType']]; } else { $attachments[$counter]['icon'] = 'fa-file'; } $counter++; } $attendees = json_decode($data['attendees'], true); $organizer = json_decode($data['organizer'], true); if ($attendees) { foreach ($attendees as $key => $value) { if ($key == "required" || $key == "optional") { foreach ($value as $attendeekey => $attendee) { $attendee['name'] = self::replace_unicode_sequences($attendee['name']); if ($attendee['email'] == $organizer['email']) { } elseif ($Allcalendar[$attendee['name']] && $Allcalendar[$attendee['name']] == $data['calendar_id']) { } else if ($Allcalendar[$attendee['name']]) { $AttendeeArray[] = $Allcalendar[$attendee['name']]; } else { $AttendeeArray[] = $attendee['email']; } } } } } if (!$data['accepted'] && $data['busy'] == 1) { $data['accepted']['ok'] = 1; $data['accepted'] = json_encode($data['accepted']); } $rows = array( 'id' => array('id' => $data['id'], 'order' => $data['id']), 'cstart' => array('cstart' => $starttime, 'order' => $data['ctime']), 'cend' => array('cend' => $endtime, 'order' => $data['mtime']), 'ccategory' => array('ccategory' => $name, 'order' => $data['name']), 'category' => array('category' => $name, 'order' => $data['name']), 'calendar_id' => array('calendar_id' => $data['calendar_id'], 'order' => $data['calendar_id']), 'location' => array('location' => $data['location'], 'order' => $data['location']), 'description' => array('description' => $data['description'], 'order' => $data['description']), 'reminder' => array('reminder' => $data['reminder'], 'order' => $data['reminder']), 'busy' => array('busy' => $data['busy'], 'order' => $data['busy']), 'type' => array('type' => $data['event_type'], 'order' => $data['event_type']), 'customer' => array('customer' => $data['customer']), 'customer_info' => array('customer_info' => $data['customer_info']), 'customer_info_send' => array('customer_info_send' => $data['customer_info_send']), 'customer_info_reminder' => array('customer_info_reminder' => $data['customer_info_reminder']), 'privateflag' => array('privateflag' => $data['private'], 'order' => $data['private']), 'isorganizer' => array('isorganizer' => $data['is_organizer']), 'canceld' => array('canceld' => $data['canceld']), 'attendees' => array('attendees' => json_encode($AttendeeArray)), 'organizer' => array('organizer' => self::replace_unicode_sequences($data['organizer'])), 'accepted' => array('accepted' => $data['accepted']), 'recurrence' => array('recurrence' => $recurrence), 'attachment' => array('attachment' => $attachment, 'order' => $attachment), 'attachments' => array('attachments' => $attachments, 'order' => $attachments) ); $json['success'] = true; $json['data'] = $rows; } else { $json['success'] = false; $json['errormessage'] = "No data found."; } $json = json_encode($json); echo trim($json); die(); } public static function insertMicrosoftCalendarEvent($request) { $db = self::dbKalender(); $json_data = json_decode($request, true); $data = []; $data['ms_event_id'] = $json_data['value'][0]['resourceData']['id']; $msUserIdExplode = explode("/", $json_data['value'][0]['resource']); $data['ms_user_id'] = trim($msUserIdExplode[1]); $Calendar = self::search(array("microsoft_id" => $data['ms_user_id'])); $data['go_calendar_id'] = $Calendar[0]->go_calendar_id; $data['data'] = $request; $data['source'] = 'ms'; if ($json_data['value'][0]['changeType'] == 'created') $data['type'] = 'c'; else if ($json_data['value'][0]['changeType'] == 'updated') $data['type'] = 'u'; else if ($json_data['value'][0]['changeType'] == 'deleted') $data['type'] = 'd'; $data['status'] = 'p'; $data['edit'] = time(); $data['create'] = time(); $data['edit_by'] = 89; $data['create_by'] = 89; $db->insert("TheTool_CalendarQueue", $data); return ""; die(); } public static function updateCalendarEvent($r, $me) { $description = ($r->description); $attachments = ($r->attachments); $location = ($r->location); $title = ($r->title); $start = CalendarModel::convertMillisecondsToTimestamp($r->start); $end = CalendarModel::convertMillisecondsToTimestamp($r->end); if ($title) { $start = strtotime($r->start); $end = strtotime($r->end); } $originalend = $end; $allday = ($r->allday); if ($allday) { $start = $start + 7200; $originalend = $end + 7200; $end = $end + 7200 + 86400; } $reminder = ($r->reminder); $newkey = ($r->newkey); $id = ($r->id); $type = ($r->type); $busy = ($r->busy); $users = ($r->users); $attendees = ($r->attendees); $attendeesOriginal = $attendees; if (is_string($attendees)) { $attendees = json_decode($attendees, true); } $privateflag = ($r->privateflag); foreach ($users as $key => $value) { $user_id = $value; } date_default_timezone_set('Europe/Berlin'); header('Content-Type: application/json'); $db = self::dbKalender(); $sql = "SELECT `cal_events`.id,microsoft_id,uuid,ms_user_id,categories FROM `cal_events` INNER JOIN `cal_calendars` ON (`cal_calendars`.`id`=`cal_events`.`calendar_id`) WHERE `cal_events`.id = '" . $id . "' LIMIT 1"; $res = $db->query($sql); if ($db->num_rows($res)) { $result = $db->fetch_object($res); $microsoft_id = $result->microsoft_id; $cal_events_id = $result->uuid; $microsoft_user_id = $result->ms_user_id; $categories = json_decode($result->categories, true); } $updateArray['start_time'] = $start; $updateArray['end_time'] = $end; $updateArray['mtime'] = time(); $updateArray['mname'] = $me->name; if ($title) { $updateArray['name'] = $title; } if ($description) $updateArray['description'] = $description; if ($location) $updateArray['location'] = $location; if (isset($allday)) $updateArray['all_day_event'] = $allday; if (isset($privateflag)) $updateArray['private'] = $privateflag; if (isset($reminder)) { if ($reminder == 'NULL') $updateArray['reminder'] = NULL; else $updateArray['reminder'] = $reminder; } if ($type) { $updateArray['event_type'] = $type; $calEventCategories = self::$eventCategories; foreach ($calEventCategories as $key => $value) { if ($categories) { $arraykey = array_search($value, $categories); } else { $arraykey = false; } if (!empty($arraykey || $arraykey === 0)) { unset($categories[$arraykey]); } } if ($type > 1) { $categories[] = $calEventCategories[$type]; $categories = array_values($categories); } if ($categories) { $updateArray['categories'] = json_encode($categories); } else $updateArray['categories'] = NULL; } if (isset($busy)) $updateArray['busy'] = $busy; if ($type == 2 || $type == 3 || $type == 4 || $type == 7) { if ($r->customer) $updateArray['customer'] = $r->customer; else $updateArray['customer'] = NULL; if (($r->customer_info_type)) { $customer_info_type = $r->customer_info_type; } if (($r->customer_info_text)) { $customer_info_text = $r->customer_info_text; } if (($r->customer_info_type_text)) { $customer_info_type_text = $r->customer_info_type_text; } if ($r->customer_info_reminder_check || $r->customer_info_reminder_check == 0) { $updateArray['customer_info_reminder'] = $r->customer_info_reminder_check; } $customerJson = array('customer_info_type' => $customer_info_type, 'customer_info_text' => $customer_info_text, 'customer_info_type_text' => $customer_info_type_text); $updateArray['customer_info'] = json_encode($customerJson); if ($r->customer_info_check) { $customerJson['sendby'] = $me->name; $customerJson['sendtime'] = time(); $updateArray['customer_info_send'] = json_encode($customerJson); } } if ($type == 1) { $updateArray['customer'] = NULL; $updateArray['customer_info'] = NULL; } $db->update("cal_events", $updateArray, "id = '" . $id . "'"); // error_log("=== NACH ERSTEM UPDATE ==="); // error_log("Suche nach linked events für Event ID: " . $id); if ($newkey) { $res = $db->select("cal_events_attachments", "id,cal_events_id", "1=1 AND cal_events_id = '" . $cal_events_id . "'"); if ($db->num_rows($res)) { while ($dataAttachment = $db->fetch_array($res)) { $toolattachments[$dataAttachment['id']] = 1; } } if ($attachments) { foreach ($attachments as $key => $value) { unset($toolattachments[$value]); } } if (!empty($toolattachments)) { foreach ($toolattachments as $key => $value) { $db->delete("cal_events_attachments", "id = '" . $key . "'"); } } $res = $db->select("tmp_cal_events_attachments", "id,name,contentType,content,size,create_timestamp,edit_timestamp", "1=1 AND newkey='" . $newkey . "' ORDER BY id"); if ($db->num_rows($res)) { while ($data = $db->fetch_array($res)) { $tmpid = $data['id']; unset($data['id']); $data['cal_events_id'] = $cal_events_id; $db->insert("cal_events_attachments", $data); $db->delete("tmp_cal_events_attachments", "id = '" . $tmpid . "'"); } } $currentBg = $db->select("cal_events", "background", "id = '" . $id . "' LIMIT 1"); if ($db->num_rows($currentBg)) { $bgData = $db->fetch_array($currentBg); if ($bgData['background']=='EBF1E2') { $pupdateArray['background'] = "EBF1E3"; } else { $pupdateArray['background'] = "EBF1E2"; } $db->update("cal_events", $pupdateArray , "id = '" . $id . "'"); } $updateArray['attachments'] = $attachments; $updateArray['end_time'] = $originalend; } if ($attendees) $updateArray['attendees'] = $attendeesOriginal; $json_data = json_encode($updateArray); if (!empty($microsoft_id) && !empty($microsoft_user_id)) { $data = []; $data['ms_user_id'] = $microsoft_user_id; $data['ms_event_id'] = $microsoft_id; $data['data'] = $json_data; $data['type'] = 'u'; $data['status'] = 'p'; $data['edit'] = time(); $data['create'] = time(); $data['edit_by'] = 89; $data['create_by'] = 89; $db->insert("TheTool_CalendarQueue", $data); } $resMain = $db->select("cal_events", 'linked_event_uid,is_organizer', "id = '" . $id . "' LIMIT 1"); // error_log("=== MAIN EVENT CHECK ==="); // error_log("num_rows: " . $db->num_rows($resMain)); if ($db->num_rows($resMain)) { // error_log("DEBUG: Vor fetch_object"); $mainEvent = $db->fetch_object($resMain); // error_log("DEBUG: Nach fetch_object"); if ($mainEvent && $mainEvent->is_organizer == 1 && empty($mainEvent->linked_event_uid)) { $new_linked_uid = 'LINKED-' . strtoupper(uniqid()) . '-' . time(); $db->update("cal_events", array('linked_event_uid' => $new_linked_uid), "id = '" . $id . "'"); $mainEvent->linked_event_uid = $new_linked_uid; // error_log("DEBUG: Alte Event - linked_event_uid erstellt: " . $new_linked_uid); } if (!$mainEvent) { // error_log("ERROR: fetch_object returned false/null!"); } else { // error_log("DEBUG: mainEvent Object: " . print_r($mainEvent, true)); // error_log("mainEvent->is_organizer: " . ($mainEvent->is_organizer ?? 'NULL')); // error_log("mainEvent->linked_event_uid: " . ($mainEvent->linked_event_uid ?? 'NULL')); if (isset($mainEvent->is_organizer) && isset($mainEvent->linked_event_uid)) { // error_log("Bedingung erfüllt? " . (($mainEvent->is_organizer == 1 && !empty($mainEvent->linked_event_uid)) ? "JA" : "NEIN")); } else { // error_log("ERROR: is_organizer oder linked_event_uid nicht gesetzt!"); } } if ($mainEvent->is_organizer == 1 && ($mainEvent->linked_event_uid)) { $dbCore = self::dbKalender(); $sqlLinked = "SELECT ce.id, ce.calendar_id, cc.ms_user_id microsoft_id FROM cal_events ce LEFT JOIN cal_calendars cc ON ce.calendar_id = cc.id WHERE ce.linked_event_uid = '" . $mainEvent->linked_event_uid . "' AND ce.id != '" . $id . "' AND ce.is_organizer = 0"; $resLinked = $db->query($sqlLinked); while ($linkedEvent = $db->fetch_object($resLinked)) { if (empty($linkedEvent->microsoft_id)) { $linkedUpdateArray = array(); if (isset($updateArray['start_time'])) $linkedUpdateArray['start_time'] = $updateArray['start_time']; if (isset($updateArray['end_time'])) $linkedUpdateArray['end_time'] = $end; if (isset($updateArray['name'])) $linkedUpdateArray['name'] = $updateArray['name']; if (isset($updateArray['description'])) $linkedUpdateArray['description'] = $updateArray['description']; if (isset($updateArray['location'])) $linkedUpdateArray['location'] = $updateArray['location']; if (isset($updateArray['all_day_event'])) $linkedUpdateArray['all_day_event'] = $updateArray['all_day_event']; if (isset($updateArray['private'])) $linkedUpdateArray['private'] = $updateArray['private']; if (isset($updateArray['reminder'])) $linkedUpdateArray['reminder'] = $updateArray['reminder']; if (isset($updateArray['busy'])) $linkedUpdateArray['busy'] = 2; if (isset($updateArray['event_type'])) $linkedUpdateArray['event_type'] = $updateArray['event_type']; if (isset($updateArray['categories'])) $linkedUpdateArray['categories'] = $updateArray['categories']; if (isset($updateArray['customer'])) $linkedUpdateArray['customer'] = $updateArray['customer']; if (isset($updateArray['customer_info'])) $linkedUpdateArray['customer_info'] = $updateArray['customer_info']; if (isset($updateArray['customer_info_send'])) $linkedUpdateArray['customer_info_send'] = $updateArray['customer_info_send']; if (isset($updateArray['customer_info_reminder'])) $linkedUpdateArray['customer_info_reminder'] = $updateArray['customer_info_reminder']; if ($attendees) { $attendeesForLinkedEvent = array(); $attendeesForLinkedEvent['required'] = array(); $organizerCalendar = self::search(array("go_calendar_id" => $user_id)); if ($organizerCalendar && count($organizerCalendar) > 0) { $orgName = ''; $orgEmail = ''; if ($organizerCalendar[0]->user_id) { $sql = "SELECT `cal_calendars`.`id`, `name`,`email` FROM `cal_calendars` INNER JOIN `core_user` ON `cal_calendars`.`user_id`=`core_user`.`id` WHERE `cal_calendars`.id = '" . $user_id . "' LIMIT 1"; $resOrgUser = $dbCore->query($sql); if ($dbCore->num_rows($resOrgUser)) { $orgUserData = $dbCore->fetch_object($resOrgUser); $orgName = $orgUserData->name; $orgEmail = $orgUserData->email; } } elseif ($organizerCalendar[0]->calendar_name) { $orgName = $organizerCalendar[0]->calendar_name; $orgEmail = $organizerCalendar[0]->calendar_email ?: ''; } $attendeesForLinkedEvent['required'][] = array( 'name' => $orgName, 'email' => $orgEmail, 'response' => 'none', 'time' => '0001-01-01 00:00:00.000000' ); } foreach ($attendees as $att) { if (is_numeric($att) && intval($att) != $linkedEvent->calendar_id) { $attCal = self::search(array("go_calendar_id" => intval($att))); if ($attCal && count($attCal) > 0) { $attName = ''; $attEmail = ''; if ($attCal[0]->user_id) { $sql = "SELECT `cal_calendars`.`id`, `name`,`email` FROM `cal_calendars` INNER JOIN `core_user` ON `cal_calendars`.`user_id`=`core_user`.`id` WHERE `cal_calendars`.id = '" . intval($att) . "' LIMIT 1"; $resAttUser = $dbCore->query($sql); if ($dbCore->num_rows($resAttUser)) { $attUserData = $dbCore->fetch_object($resAttUser); $attName = $attUserData->name; $attEmail = $attUserData->email; } } elseif ($attCal[0]->calendar_name) { $attName = $attCal[0]->calendar_name; $attEmail = $attCal[0]->calendar_email ?: ''; } if ($attName && $attEmail) { $attendeesForLinkedEvent['required'][] = array( 'name' => $attName, 'email' => $attEmail, 'response' => 'none', 'time' => '0001-01-01 00:00:00.000000' ); } } } elseif (!is_numeric($att)) { $attendeesForLinkedEvent['required'][] = array( 'name' => $att, 'email' => $att, 'response' => 'none', 'time' => '0001-01-01 00:00:00.000000' ); } } $linkedUpdateArray['attendees'] = json_encode($attendeesForLinkedEvent); } $linkedUpdateArray['mtime'] = time(); $linkedUpdateArray['mname'] = $me->name; if (!empty($linkedUpdateArray)) { usleep(200000); $db->update("cal_events", $linkedUpdateArray, "id = '" . $linkedEvent->id . "'"); } } } if ($attendees) { $currentAttendeeCalendarIds = array(); $sqlLinkedForAttendees = "SELECT ce.id, ce.calendar_id, cc.ms_user_id microsoft_id FROM cal_events ce LEFT JOIN cal_calendars cc ON ce.calendar_id = cc.id WHERE ce.linked_event_uid = '" . $mainEvent->linked_event_uid . "' AND ce.id != '" . $id . "' AND ce.is_organizer = 0"; $resCurrentAttendeesQuery = $db->query($sqlLinkedForAttendees); while ($currAtt = $db->fetch_object($resCurrentAttendeesQuery)) { if (empty($currAtt->microsoft_id)) { $currentAttendeeCalendarIds[] = $currAtt->calendar_id; } } foreach ($attendees as $newAttendee) { $newAttendeeCalId = null; if (is_numeric($newAttendee) && !filter_var($newAttendee, FILTER_VALIDATE_EMAIL)) { $newAttendeeCalId = intval($newAttendee); } else { $dbCheckEmail = FronkDB::singleton(); $resCheckEmail = $dbCheckEmail->query("SELECT go_calendar_id FROM Calendar WHERE calendar_email = '" . $newAttendee . "' LIMIT 1"); if ($dbCheckEmail->num_rows($resCheckEmail)) { $emailCalData = $dbCheckEmail->fetch_object($resCheckEmail); $newAttendeeCalId = intval($emailCalData->go_calendar_id); } } // error_log("=== NEUER TEILNEHMER CHECK ==="); // error_log("newAttendee Wert: " . print_r($newAttendee, true)); // error_log("newAttendeeCalId: " . ($newAttendeeCalId ?: 'NULL')); // error_log("currentAttendeeCalendarIds: " . print_r($currentAttendeeCalendarIds, true)); // error_log("Ist in currentAttendeeCalendarIds? " . (in_array($newAttendeeCalId, $currentAttendeeCalendarIds) ? "JA" : "NEIN")); // if ($newAttendeeCalId) { if (!in_array($newAttendeeCalId, $currentAttendeeCalendarIds)) { $newAttCal = self::search(array("go_calendar_id" => $newAttendeeCalId)); if (count($newAttCal) > 0) { $resCheckNewAttMs = $db->select("cal_calendars", "ms_user_id", "id = '" . $newAttendeeCalId . "' LIMIT 1"); $hasMsId = false; if ($db->num_rows($resCheckNewAttMs)) { $newAttMsData = $db->fetch_object($resCheckNewAttMs); $hasMsId = !empty($newAttMsData->ms_user_id); } } if ($newAttCal && count($newAttCal) > 0 && !$hasMsId) { $organizerCalendar = self::search(array("go_calendar_id" => $user_id)); $organizerName = ''; $organizerEmail = ''; if ($organizerCalendar && count($organizerCalendar) > 0) { if ($organizerCalendar[0]->user_id) { $sql = "SELECT `cal_calendars`.`id`, `name`,`email` FROM `cal_calendars` INNER JOIN `core_user` ON `cal_calendars`.`user_id`=`core_user`.`id` WHERE `cal_calendars`.id = '" . $user_id . "' LIMIT 1"; error_log('new Calendar resOrgUser2: '.$sql); $resOrgUser2 = $dbCore->query($sql); if ($dbCore->num_rows($resOrgUser2)) { $orgUserData2 = $dbCore->fetch_object($resOrgUser2); $organizerName = $orgUserData2->name; $organizerEmail = $orgUserData2->email; } } elseif ($organizerCalendar[0]->calendar_name) { $organizerName = $organizerCalendar[0]->calendar_name; $organizerEmail = $organizerCalendar[0]->calendar_email ?: ''; } } $newOrganizerJson = json_encode(array( 'name' => $organizerName, 'email' => $organizerEmail )); $newAttendeesForEvent = array(); $newAttendeesForEvent['required'] = array(); $newAttendeesForEvent['required'][] = array( 'name' => $organizerName, 'email' => $organizerEmail, 'response' => 'none', 'time' => '0001-01-01 00:00:00.000000' ); foreach ($attendees as $otherAtt) { if ($otherAtt != $newAttendee) { if (is_numeric($otherAtt) && !filter_var($otherAtt, FILTER_VALIDATE_EMAIL)) { $otherCal = self::search(array("go_calendar_id" => intval($otherAtt))); if ($otherCal && count($otherCal) > 0) { $otherName = ''; $otherEmail = ''; if ($otherCal[0]->user_id) { $sql = "SELECT `cal_calendars`.`id`, `name`,`email` FROM `cal_calendars` INNER JOIN `core_user` ON `cal_calendars`.`user_id`=`core_user`.`id` WHERE `cal_calendars`.id = '" . intval($otherAtt) . "' LIMIT 1"; error_log('new Calendar resOtherUser: '.$sql); $resOtherUser = $dbCore->query($sql); if ($dbCore->num_rows($resOtherUser)) { $otherUserData = $dbCore->fetch_object($resOtherUser); $otherName = $otherUserData->name; $otherEmail = $otherUserData->email; } } elseif ($otherCal[0]->calendar_name) { $otherName = $otherCal[0]->calendar_name; $otherEmail = $otherCal[0]->calendar_email ?: ''; } if ($otherName && $otherEmail) { $newAttendeesForEvent['required'][] = array( 'name' => $otherName, 'email' => $otherEmail, 'response' => 'none', 'time' => '0001-01-01 00:00:00.000000' ); } } } else { $newAttendeesForEvent['required'][] = array( 'name' => $otherAtt, 'email' => $otherAtt, 'response' => 'none', 'time' => '0001-01-01 00:00:00.000000' ); } } } $newAttendeesJson = json_encode($newAttendeesForEvent); $newAttendeeDataarray = array( "start_time" => $updateArray['start_time'], 'end_time' => $end, 'name' => $updateArray['name'], 'description' => $updateArray['description'] ?? '', 'location' => $updateArray['location'] ?? '', 'calendar_id' => $newAttendeeCalId, 'uuid' => "a5eb79b3-fca7-5378-a09e-" . rand(100000000000, 999999999999), 'user_id' => 1, 'timezone' => 'Europe/Amsterdam', 'all_day_event' => $updateArray['all_day_event'] ?? 0, 'repeat_end_time' => 0, 'reminder' => $updateArray['reminder'] ?? NULL, 'ctime' => time(), 'cname' => $me->name, 'mtime' => time(), 'mname' => $me->name, 'busy' => 2, 'status' => 'CONFIRMED', 'resource_event_id' => 0, 'private' => $updateArray['private'] ?? 0, 'rrule' => '', 'background' => 'EBF1E2', 'files_folder_id' => 0, 'read_only' => 0, 'categories' => $updateArray['categories'] ?? NULL, 'exception_for_event_id' => 0, 'recurrence_id' => 0, 'is_organizer' => 0, 'organizer' => $newOrganizerJson, 'attendees' => $newAttendeesJson, 'linked_event_uid' => $mainEvent->linked_event_uid, 'event_type' => $updateArray['event_type'] ?? 1, 'customer' => $updateArray['customer'] ?? NULL, 'customer_info' => $updateArray['customer_info'] ?? NULL, 'customer_info_send' => $updateArray['customer_info_send'] ?? NULL, 'customer_info_reminder' => $updateArray['customer_info_reminder'] ?? 0 ); usleep(200000); $db->insert("cal_events", $newAttendeeDataarray); } } } } $newAttendeeCalendarIds = array(); foreach ($attendees as $att) { if (is_numeric($att)) { $newAttendeeCalendarIds[] = intval($att); } else { $dbCheck = FronkDB::singleton(); $resEmailCheck = $dbCheck->query("SELECT go_calendar_id FROM Calendar WHERE calendar_email = '" . $att . "' LIMIT 1"); if ($dbCheck->num_rows($resEmailCheck)) { $emailData = $dbCheck->fetch_object($resEmailCheck); $resCheckMs = $db->select("cal_calendars", "ms_user_id", "id = '" . $emailData->go_calendar_id . "' LIMIT 1"); if ($db->num_rows($resCheckMs)) { $msData = $db->fetch_object($resCheckMs); $emailHasMsId = !empty($msData->ms_user_id); if (!$emailHasMsId) { $newAttendeeCalendarIds[] = intval($emailData->go_calendar_id); } } else { } } } } error_log("DEBUG: newAttendeeCalendarIds nach Email-Check: " . print_r($newAttendeeCalendarIds, true)); foreach ($currentAttendeeCalendarIds as $oldCalId) { if (!in_array($oldCalId, $newAttendeeCalendarIds)) { $db->delete("cal_events", "linked_event_uid = '" . $mainEvent->linked_event_uid . "' AND calendar_id = '" . $oldCalId . "' AND is_organizer = 0"); } else { } } } } } } public static function updateCalendarEventState($r, $me) { $id = ($r->id); $users = ($r->users); $eventAction = $r->eventAction; if ($eventAction == "accept") { $process = 'ae'; $updateArray['busy'] = 1; } else if ($eventAction == "decline") { $process = "ce"; } else { } foreach ($users as $key => $value) { $user_id = $value; } $db = self::dbKalender(); $sql = "SELECT `cal_events`.id,microsoft_id,uuid,ms_user_id FROM `cal_events` INNER JOIN `cal_calendars` ON (`cal_calendars`.`id`=`cal_events`.`calendar_id`) WHERE `cal_events`.id = '" . $id . "' LIMIT 1"; $res = $db->query($sql); if ($db->num_rows($res)) { $result = $db->fetch_object($res); $microsoft_id = $result->microsoft_id; $cal_events_id = $result->uuid; $microsoft_user_id = $result->ms_user_id; } $acceptJson = array('time' => time(), 'user' => $me->name); $updateArray['accepted'] = json_encode($acceptJson); $db->update("cal_events", $updateArray, "id = '" . $id . "'"); $json_data = json_encode($updateArray); if (!empty($microsoft_id) && !empty($microsoft_user_id)) { $data = []; $data['ms_user_id'] = $microsoft_user_id; $data['ms_event_id'] = $microsoft_id; $data['data'] = $json_data; $data['type'] = $process; $data['status'] = 'p'; $data['edit'] = time(); $data['create'] = time(); $data['edit_by'] = 89; $data['create_by'] = 89; $db->insert("TheTool_CalendarQueue", $data); } if ($eventAction == "decline") { $resCheckDecline = $db->select("cal_events", "linked_event_uid, calendar_id", "id = '" . $id . "' LIMIT 1"); if ($db->num_rows($resCheckDecline)) { $declineEvent = $db->fetch_object($resCheckDecline); $resCheckDeclineMs = $db->select("cal_calendars", "ms_user_id", "id = '" . $declineEvent->calendar_id . "' LIMIT 1"); if ($db->num_rows($resCheckDeclineMs)) { $declineMsData = $db->fetch_object($resCheckDeclineMs); $declineHasMsId = !empty($declineMsData->ms_user_id); if (!$declineHasMsId && !empty($declineEvent->linked_event_uid)) { error_log("DEBUG: Teilnehmer ohne MS-ID lehnt ab - Lösche Event ID: " . $id); $db->delete("cal_events", "id = '" . $id . "' AND linked_event_uid = '" . $declineEvent->linked_event_uid . "' AND is_organizer = 0"); } } } } } public static function insertCalendarEvent($r, $me, $rrules = "") { $description = ($r->description); $attachments = ($r->attachments); $location = ($r->location); $title = ($r->title); $start = strtotime($r->start); $end = strtotime($r->end); $originalend = $end; $allday = ($r->allday); $reminder = ($r->reminder); $newkey = ($r->newkey); $type = ($r->type); $busy = ($r->busy); $users = ($r->users); $customer_info_reminder_check = 0; $attendees = ($r->attendees); $privateflag = ($r->privateflag); date_default_timezone_set('Europe/Berlin'); header('Content-Type: application/json'); foreach ($users as $key => $value) { $user_id = $value; } $db = self::dbKalender(); $res = $db->select("cal_calendars", "id,ms_user_id", "id = '" . $user_id . "' LIMIT 1"); if ($db->num_rows($res)) { $result = $db->fetch_object($res); $microsoft_user_id = $result->ms_user_id; } if (!$allday) { $allday = 0; } else { $start = $start + 7200; $originalend = $end + 7200; $end = $end + 7200 + 86400; } if ($reminder == 'NULL') { $reminder = NULL; } $calEventCategories = self::$eventCategories; if ($type > 1) { $categories[] = $calEventCategories[$type]; } if ($categories) { $categories = json_encode($categories); } else $categories = NULL; if ($type == 2 || $type == 3 || $type == 4 || $type == 7) { $customer = $r->customer; $customer_info_type = $r->customer_info_type; $customer_info_text = $r->customer_info_text; $customer_info_type_text = $r->customer_info_type_text; $customerJson = array('customer_info_type' => $customer_info_type, 'customer_info_text' => $customer_info_text, 'customer_info_type_text' => $customer_info_type_text); $customer_info = json_encode($customerJson); if ($r->customer_info_reminder_check) { $customer_info_reminder_check = $r->customer_info_reminder_check; } if ($r->customer_info_check) { $customerJson['sendby'] = $me->name; $customerJson['sendtime'] = time(); $customer_info_send = json_encode($customerJson); } } else { $customer = NULL; $customer_info = NULL; $customer_info_send = NULL; } $dataarray = array("start_time" => $start, 'end_time' => $end, 'name' => $title, 'description' => $description, 'location' => $location, 'calendar_id' => $user_id, 'uuid' => "a5eb79b3-fca7-5378-a09e-" . rand(100000000000, 999999999999), 'user_id' => 1, 'timezone' => 'Europe/Amsterdam', 'all_day_event' => $allday, 'repeat_end_time' => 0, 'reminder' => $reminder, 'ctime' => time(), 'cname' => $me->name, 'mtime' => time(), 'mname' => $me->name, 'user_id' => 1, 'busy' => $busy, 'status' => 'CONFIRMED', 'resource_event_id' => 0, 'private' => $privateflag, 'rrule' => '', 'background' => 'EBF1E2', 'files_folder_id' => 0, 'read_only' => 0, 'categories' => $categories, 'exception_for_event_id' => 0, 'recurrence_id' => 0, 'is_organizer' => 1, 'event_type' => $type, 'customer' => $customer, 'customer_info' => $customer_info, 'customer_info_send' => $customer_info_send, 'customer_info_reminder' => $customer_info_reminder_check); $db->insert("cal_events", $dataarray); $event_id = $dataarray['uuid']; $shared_linked_uid = 'LINKED-' . strtoupper(uniqid()) . '-' . time(); $db->update("cal_events", array('linked_event_uid' => $shared_linked_uid), "uuid = '" . $event_id . "'"); if ($newkey) { $res = $db->select("tmp_cal_events_attachments", "id,name,contentType,content,size,create_timestamp,edit_timestamp", "1=1 AND newkey='" . $newkey . "' ORDER BY id"); if ($db->num_rows($res)) { while ($data = $db->fetch_array($res)) { $tmpid = $data['id']; unset($data['id']); $data['cal_events_id'] = $event_id; $db->insert("cal_events_attachments", $data); $db->delete("tmp_cal_events_attachments", "id = '" . $tmpid . "'"); } } } if ($attendees) $dataarray['attendees'] = $attendees; $dataarray['attachments'] = $attachments; if ($rrules) { $dataarray['rrule'] = $rrules; } $dataarray['end_time'] = $originalend; $json_data = json_encode($dataarray); if (!empty($microsoft_user_id)) { $data = []; $data['ms_user_id'] = $microsoft_user_id; $data['event_id'] = $event_id; $data['data'] = $json_data; $data['type'] = 'c'; $data['status'] = 'p'; $data['edit'] = time(); $data['create'] = time(); $data['edit_by'] = 89; $data['create_by'] = 89; $db->insert("TheTool_CalendarQueue", $data); } if ($attendees) { $dbCore =self::dbKalender();; $organizerCalendar = self::search(array("go_calendar_id" => $user_id)); $organizerName = ''; $organizerEmail = ''; if ($organizerCalendar && count($organizerCalendar) > 0) { if ($organizerCalendar[0]->user_id) { $sql = "SELECT `cal_calendars`.`id`, `name`,`email` FROM `cal_calendars` INNER JOIN `core_user` ON `cal_calendars`.`user_id`=`core_user`.`id` WHERE `cal_calendars`.id = '" . $user_id . "' LIMIT 1"; error_log($sql); $resOrgUser = $db->query($sql); if ($dbCore->num_rows($resOrgUser)) { $orgUserData = $dbCore->fetch_object($resOrgUser); $organizerName = $orgUserData->name; $organizerEmail = $orgUserData->email; } } elseif ($organizerCalendar[0]->calendar_name) { $organizerName = $organizerCalendar[0]->calendar_name; $organizerEmail = $organizerCalendar[0]->calendar_email ?: ''; } } $organizerJson = json_encode(array( 'name' => $organizerName, 'email' => $organizerEmail )); foreach ($attendees as $attendeeValue) { if (is_numeric($attendeeValue) && !filter_var($attendeeValue, FILTER_VALIDATE_EMAIL)) { $attendeeCalendarId = intval($attendeeValue); $attendeeCalendar = self::search(array("go_calendar_id" => $attendeeCalendarId)); if ($attendeeCalendar && count($attendeeCalendar) > 0) { $resCheckAttMs = $db->select("cal_calendars", "ms_user_id", "id = '" . $attendeeCalendarId . "' LIMIT 1"); $attendeeHasMicrosoftId = false; if ($db->num_rows($resCheckAttMs)) { $attMsData = $db->fetch_object($resCheckAttMs); $attendeeHasMicrosoftId = !empty($attMsData->ms_user_id); } if (!$attendeeHasMicrosoftId) { $attendeeName = ''; $attendeeEmail = ''; if ($attendeeCalendar[0]->user_id) { $sql = "SELECT `cal_calendars`.`id`, `name`,`email` FROM `cal_calendars` INNER JOIN `core_user` ON `cal_calendars`.`user_id`=`core_user`.`id` WHERE `cal_calendars`.id = '" . $attendeeCalendarId . "' LIMIT 1"; error_log('Debug attendee '.$sql); $resAttUser = $db->query($sql); if ($dbCore->num_rows($resAttUser)) { $attUserData = $dbCore->fetch_object($resAttUser); $attendeeName = $attUserData->name; $attendeeEmail = $attUserData->email; } } elseif ($attendeeCalendar[0]->calendar_name) { $attendeeName = $attendeeCalendar[0]->calendar_name; $attendeeEmail = $attendeeCalendar[0]->calendar_email ?: ''; } $attendeesForEvent = array(); $attendeesForEvent['required'] = array(); $attendeesForEvent['required'][] = array( 'name' => $organizerName, 'email' => $organizerEmail, 'response' => 'none', 'time' => '0001-01-01 00:00:00.000000' ); $attendeesForEvent = array(); $attendeesForEvent['required'] = array(); $attendeesForEvent['required'][] = array( 'name' => $organizerName, 'email' => $organizerEmail, 'response' => 'none', 'time' => '0001-01-01 00:00:00.000000' ); foreach ($attendees as $otherAttendee) { if ($otherAttendee != $attendeeValue) { if (is_numeric($otherAttendee) && !filter_var($otherAttendee, FILTER_VALIDATE_EMAIL)) { $resCheckOtherMs = $db->select("cal_calendars", "ms_user_id", "id = '" . intval($otherAttendee) . "' LIMIT 1"); $otherHasMicrosoftId = false; if ($db->num_rows($resCheckOtherMs)) { $otherMsData = $db->fetch_object($resCheckOtherMs); $otherHasMicrosoftId = !empty($otherMsData->ms_user_id); } $otherCalendar = self::search(array("go_calendar_id" => intval($otherAttendee))); if ($otherCalendar && count($otherCalendar) > 0) { $otherName = ''; $otherEmail = ''; if ($otherCalendar[0]->user_id) { $sql = "SELECT `cal_calendars`.`id`, `name`,`email` FROM `cal_calendars` INNER JOIN `core_user` ON `cal_calendars`.`user_id`=`core_user`.`id` WHERE `cal_calendars`.id = '" . intval($otherAttendee) . "' LIMIT 1"; $resOtherUser = $db->query($sql); if ($dbCore->num_rows($resOtherUser)) { $otherUserData = $dbCore->fetch_object($resOtherUser); $otherName = $otherUserData->name; $otherEmail = $otherUserData->email; } } elseif ($otherCalendar[0]->calendar_name) { $otherName = $otherCalendar[0]->calendar_name; $otherEmail = $otherCalendar[0]->calendar_email ?: ''; } if ($otherName && $otherEmail) { $attendeesForEvent['required'][] = array( 'name' => $otherName, 'email' => $otherEmail, 'response' => 'none', 'time' => '0001-01-01 00:00:00.000000' ); } } } else { $attendeesForEvent['required'][] = array( 'name' => $otherAttendee, 'email' => $otherAttendee, 'response' => 'none', 'time' => '0001-01-01 00:00:00.000000' ); } } } $attendeesJson = json_encode($attendeesForEvent); $attendeeDataarray = array( "start_time" => $start, 'end_time' => $end, 'name' => $title, 'description' => $description, 'location' => $location, 'calendar_id' => $attendeeCalendarId, 'uuid' => "a5eb79b3-fca7-5378-a09e-" . rand(100000000000, 999999999999), 'user_id' => 1, 'timezone' => 'Europe/Amsterdam', 'all_day_event' => $allday, 'repeat_end_time' => 0, 'reminder' => $reminder, 'ctime' => time(), 'cname' => $me->name, 'mtime' => time(), 'mname' => $me->name, 'busy' => 2, 'status' => 'CONFIRMED', 'resource_event_id' => 0, 'private' => $privateflag, 'rrule' => '', 'background' => 'EBF1E2', 'files_folder_id' => 0, 'read_only' => 0, 'categories' => $categories, 'exception_for_event_id' => 0, 'recurrence_id' => 0, 'is_organizer' => 0, 'organizer' => $organizerJson, 'attendees' => $attendeesJson, 'linked_event_uid' => $shared_linked_uid, 'event_type' => $type, 'customer' => $customer, 'customer_info' => $customer_info, 'customer_info_send' => $customer_info_send, 'customer_info_reminder' => $customer_info_reminder_check ); $db->insert("cal_events", $attendeeDataarray); } } } } } if ($r->delete_old_entry) { CalendarModel::deleteCalendarOldEvent($r->delete_old_entry); die(); } } public static function insertCalendarEventAttachmentTemp($name, $contantType, $content, $size, $newkey) { $db = self::dbKalender(); $data = []; $data['name'] = $name; $data['contentType'] = $contantType; $data['content'] = $content; $data['size'] = $size; $data['newkey'] = $newkey; $data['create_timestamp'] = time(); $data['edit_timestamp'] = time(); $db->insert("tmp_cal_events_attachments", $data); // echo "insert_id: " . $db->insert_id; return $db->insert_id; } public static function deleteCalendarEventAttachmentTemp($newkey, $name) { $db = self::dbKalender(); $db->delete("tmp_cal_events_attachments", "newkey = '" . $newkey . "' AND name = '" . $name . "'"); } public static function deleteCalendarEvent($r) { $id = $r->id; $users = $r->users; foreach ($users as $key => $value) { $user_id = $value; } date_default_timezone_set('Europe/Berlin'); header('Content-Type: application/json'); $db = self::dbKalender(); $res = $db->select("cal_calendars", "id,ms_user_id", "id = '" . $user_id . "' LIMIT 1"); if ($db->num_rows($res)) { $result = $db->fetch_object($res); $microsoft_user_id = $result->ms_user_id; } $res = $db->select("cal_events", 'id,microsoft_id', "id = '" . $id . "' LIMIT 1"); if ($db->num_rows($res)) { $result = $db->fetch_object($res); if (!empty($result->microsoft_id) && !empty($microsoft_user_id)) { $data = []; $data['ms_user_id'] = $microsoft_user_id; $data['ms_event_id'] = $result->microsoft_id; $data['type'] = 'd'; $data['status'] = 'p'; $data['edit'] = time(); $data['create'] = time(); $data['edit_by'] = 89; $data['create_by'] = 89; $db->insert("TheTool_CalendarQueue", $data); } } $db->delete("cal_events", "id = '" . $id . "'"); die(); } public static function deleteCalendarOldEvent($id) { date_default_timezone_set('Europe/Berlin'); header('Content-Type: application/json'); $db = self::dbKalender(); $res = $db->select("cal_events", 'id,microsoft_id,user_id,calendar_id,microsoft_ical_uid', "id = '" . $id . "' LIMIT 1"); if ($db->num_rows($res)) { $result = $db->fetch_object($res); $resical = $db->select("cal_events", 'id,microsoft_id,user_id,calendar_id,microsoft_ical_uid', "microsoft_ical_uid = '" . $result->microsoft_ical_uid . "'"); while ($resultical = $db->fetch_object($resical)) { $rescal = $db->select("cal_calendars", "id,ms_user_id", "id = '" . $resultical->calendar_id . "' LIMIT 1"); if ($db->num_rows($res)) { $resultcal = $db->fetch_object($rescal); $microsoft_user_id = $resultcal->ms_user_id; } if (!empty($microsoft_user_id)) { $data = []; $data['ms_user_id'] = $microsoft_user_id; $data['ms_event_id'] = $resultical->microsoft_id; $data['type'] = 'd'; $data['status'] = 'p'; $data['edit'] = time(); $data['create'] = time(); $data['edit_by'] = 89; $data['create_by'] = 89; $db->insert("TheTool_CalendarQueue", $data); } $db->delete("cal_events", "id = '" . $resical->id . "'"); } } die(); } public static function cancelCalendarEvent($r, $me) { $id = $r->id; $users = $r->users; foreach ($users as $key => $value) { $user_id = $value; } date_default_timezone_set('Europe/Berlin'); header('Content-Type: application/json'); $db = self::dbKalender(); $res = $db->select("cal_calendars", "id,ms_user_id", "id = '" . $user_id . "' LIMIT 1"); if ($db->num_rows($res)) { $result = $db->fetch_object($res); $microsoft_user_id = $result->ms_user_id; } $updateArray = []; $res = $db->select("cal_events", 'id,microsoft_id,name,microsoft_ical_uid,linked_event_uid,start_time,end_time', "id = '" . $id . "' LIMIT 1"); if ($db->num_rows($res)) { $result = $db->fetch_object($res); if (($r->customer_info_type)) { $customer_info_type = $r->customer_info_type; } if (($r->customer_info_text)) { $customer_info_text = $r->customer_info_text; } if (($r->customer_info_type_text)) { $customer_info_type_text = $r->customer_info_type_text; } $customerJson = array('customer_info_type' => $customer_info_type, 'customer_info_text' => $customer_info_text, 'customer_info_type_text' => $customer_info_type_text); $updateArray['customer_info'] = json_encode($customerJson); if ($r->customer_info_check) { $customerJson['sendby'] = $me->name; $customerJson['sendtime'] = time(); $updateArray['customer_info_send'] = json_encode($customerJson); } $newname = "Absage: " . $result->name; $microsoft_id = $result->microsoft_id; $updateArray['name'] = $newname; $updateArray['busy'] = '0'; $updateArray['start_time'] = $result->start_time; $updateArray['end_time'] = $result->end_time; $updateArray['customer_info_reminder'] = 0; $updateArray['canceld'] = 1; $db->update("cal_events", $updateArray, "id = '" . $id . "'"); $json_data = json_encode($updateArray); if (!empty($microsoft_id) && !empty($microsoft_user_id)) { $data = []; $data['ms_user_id'] = $microsoft_user_id; $data['ms_event_id'] = $microsoft_id; $data['data'] = $json_data; $data['type'] = 'u'; $data['status'] = 'p'; $data['edit'] = time(); $data['create'] = time(); $data['edit_by'] = 89; $data['create_by'] = 89; $db->insert("TheTool_CalendarQueue", $data); } $res = $db->select("cal_events", 'id,microsoft_id,name,microsoft_ical_uid,start_time,end_time,calendar_id', "id != '" . $id . "' AND (microsoft_ical_uid = '" . $result->microsoft_ical_uid . "' || linked_event_uid = '" .$result->linked_event_uid."') "); while ($row = $db->fetch_object($res)) { $updateArray = []; $updateArray['name'] = $newname; $updateArray['busy'] = '0'; $updateArray['start_time'] = $row->start_time; $updateArray['end_time'] = $row->end_time; $updateArray['customer_info_reminder'] = 0; $updateArray['canceld'] = 1; $updateArray['customer_info'] = json_encode($customerJson); if ($r->customer_info_check) { $customerJson['sendby'] = $me->name; $customerJson['sendtime'] = time(); $updateArray['customer_info_send'] = json_encode($customerJson); } $db->update("cal_events", $updateArray, "id = '" . $row->id . "'"); $json_data = json_encode($updateArray); $rescal = $db->select("cal_calendars", "id,ms_user_id", "id = '" . $row->calendar_id . "' LIMIT 1"); $resultcal = $db->fetch_object($rescal); if (!empty($row->microsoft_id) && !empty($resultcal->ms_user_id)) { $data = []; $data['ms_user_id'] = $resultcal->ms_user_id; $data['ms_event_id'] = $row->microsoft_id; $data['data'] = $json_data; $data['type'] = 'u'; $data['status'] = 'p'; $data['edit'] = time(); $data['create'] = time(); $data['edit_by'] = 89; $data['create_by'] = 89; $db->insert("TheTool_CalendarQueue", $data); } } } die(); } public static function generateDataRecurrence($graph) { $data = []; switch ($graph['pattern']['type']) { case 'daily': $data['rrule_frequency'] = 'DAILY'; break; case 'weekly': $data['rrule_frequency'] = 'WEEKLY'; if (!empty($graph['pattern']['daysOfWeek'])) { $data['rrule_byweekday'] = array_map(function ($day) { return self::convertGraphToDay($day); }, $graph['pattern']['daysOfWeek']); } break; case 'absoluteMonthly': $data['rrule_frequency'] = 'MONTHLY'; $data['monthly_type'] = 'BYMONTHDAY'; $data['rrule_bymonthday'] = intval($graph['pattern']['dayOfMonth']); break; case 'relativeMonthly': $data['rrule_frequency'] = 'MONTHLY'; $data['monthly_type'] = 'BYSETPOS'; $indexMapping = [ 'first' => 1, 'second' => 2, 'third' => 3, 'fourth' => 4, 'last' => -1 ]; $index = isset($graph['pattern']['index']) ? $graph['pattern']['index'] : 'first'; $data['rrule_setpos'] = isset($indexMapping[$index]) ? $indexMapping[$index] : 1; if (!empty($graph['pattern']['daysOfWeek'])) { $data['rrule_bynweekday'] = self::convertGraphToDay($graph['pattern']['daysOfWeek'][0]); } break; case 'absoluteYearly': $data['rrule_frequency'] = 'YEARLY'; break; default: $data['rrule_frequency'] = 'DAILY'; } if (!empty($graph['range']['startDate'])) { $data['startDate'] = $graph['range']['startDate']; } if (!empty($graph['range']['type'])) { switch ($graph['range']['type']) { case 'endDate': $data['rrule_until'] = $graph['range']['endDate']; break; case 'numbered': $data['rrule_count'] = intval($graph['range']['numberOfOccurrences']); break; case 'noEnd': break; } } return $data; } public static function convertGraphToDay($graphDay) { $mapping = [ 'MONDAY' => 'MO', 'TUESDAY' => 'TU', 'WEDNESDAY' => 'WE', 'THURSDAY' => 'TH', 'FRIDAY' => 'FR', 'SATURDAY' => 'SA', 'SUNDAY' => 'SU' ]; $graphDay = strtoupper($graphDay); return isset($mapping[$graphDay]) ? $mapping[$graphDay] : $graphDay; } public static function create(array $data) { $model = new Calendar(); foreach ($data as $field => $value) { if (property_exists(get_called_class(), $field)) { if (substr($field, 0, 5) == "vlan_" && !$value) { $model->$field = null; continue; } $model->$field = $value; } } $me = mfValuecache::singleton()->get("me"); if (!$me) { $me = new User(); $me->loadMe(); mfValuecache::singleton()->set("me", $me); } if ($model->create_by === null) { $model->create_by = $me->id; } if ($model->edit_by === null) { $model->edit_by = $me->id; } return $model; } public static function getOne($id) { if (!is_numeric($id) || !$id) { throw new Exception("Invalid number", 400); } $item = []; $db = FronkDB::singleton(); $res = $db->select("Calendar", "*", "id=$id LIMIT 1"); if ($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new Calendar($data); } return $item; } public static function getAll($key = 'go_calendar_id') { $items = []; $db = FronkDB::singleton(); $res = $db->select("Calendar", "*", "1=1"); if ($db->num_rows($res)) { while ($data = $db->fetch_object($res)) { $items[$data->{$key}] = new Calendar($data); } } return $items; } public static function getAllIndex() { $items = []; $db = FronkDB::singleton(); $res = $db->select("Calendar", "*", "1=1"); if ($db->num_rows($res)) { while ($data = $db->fetch_object($res)) { $items[] = new Calendar($data); } } return $items; } public static function getAddressSbidi($search) { $sql = "SELECT Preorder.id,`firstname`, `lastname`, `street`, `housenumber`, `block`, `stiege`, `stock`, `tuer`, `zip`, `city`, `phone`, `email` FROM Preorder INNER JOIN Preordercampaign ON Preorder.preordercampaign_id=Preordercampaign.id INNER JOIN Network ON Network.id=Preordercampaign.network_id WHERE Network.owner_id='9633'"; if ($search) { $sql .= " AND (firstname LIKE '%$search%' OR lastname LIKE '%$search%' OR street LIKE '%$search%' OR housenumber LIKE '%$search%' OR block LIKE '%$search%' OR stiege LIKE '%$search%' OR stock LIKE '%$search%' OR tuer LIKE '%$search%' OR zip LIKE '%$search%' OR city LIKE '%$search%')"; } $sql .= " ORDER BY lastname"; $db = FronkDB::singleton(); $res = $db->query($sql); if ($db->num_rows($res)) { while ($data = $db->fetch_object($res)) { $items[] = $data; } } return $items; } public static function getFirst() { $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $res = $db->select("Calendar", "*", "$where "); if ($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new Calendar($data); if ($item->id) { return $item; } else { return null; } } return null; } public static function search($filter) { $items = []; $db = FronkDB::singleton(); $where = self::getSqlFilter($filter); $res = $db->select("Calendar", "*", "$where"); if ($db->num_rows($res)) { while ($data = $db->fetch_object($res)) { $items[] = new Calendar($data); } } return $items; } private static function getSqlFilter($filter) { $where = "1=1 "; //var_dump($filter);exit; if (array_key_exists("user_id", $filter)) { $user_id = $filter['user_id']; if (is_numeric($user_id)) { $where .= " AND user_id=$user_id"; } } if (array_key_exists("microsoft_id", $filter)) { $microsoft_id = $filter['microsoft_id']; $where .= " AND microsoft_id='$microsoft_id'"; } if (array_key_exists("checkSubscriptions", $filter)) { $where .= " AND microsoft_id!='' AND microsoft_id IS NOT NULL AND active=1"; } //var_dump($filter, $where);exit; return $where; } }