public static function Edit($Params = array())
 {
     global $DB, $CACHE_MANAGER;
     $arFields = $Params['arFields'];
     // Get current user id
     $userId = isset($Params['userId']) && intVal($Params['userId']) > 0 ? intVal($Params['userId']) : CCalendar::GetCurUserId();
     if (!$userId && isset($arFields['CREATED_BY'])) {
         $userId = intVal($arFields['CREATED_BY']);
     }
     $path = !empty($Params['path']) ? $Params['path'] : CCalendar::GetPath($arFields['CAL_TYPE'], $arFields['OWNER_ID'], true);
     if ($userId < 0) {
         return false;
     }
     if (!self::CheckFields($arFields)) {
         return false;
     }
     if ($arFields['CAL_TYPE'] == 'user') {
         $CACHE_MANAGER->ClearByTag('calendar_user_' . $arFields['OWNER_ID']);
     }
     $bNew = !isset($arFields['ID']) || $arFields['ID'] <= 0;
     $arFields['TIMESTAMP_X'] = CCalendar::Date(mktime(), true, false);
     if ($bNew) {
         if (!isset($arFields['CREATED_BY'])) {
             $arFields['CREATED_BY'] = $userId;
         }
         if (!isset($arFields['DATE_CREATE'])) {
             $arFields['DATE_CREATE'] = $arFields['TIMESTAMP_X'];
         }
     }
     $attendees = is_array($arFields['ATTENDEES']) ? $arFields['ATTENDEES'] : array();
     if (!isset($arFields['OWNER_ID']) || !$arFields['OWNER_ID']) {
         $arFields['OWNER_ID'] = 0;
     }
     if (!isset($arFields['LOCATION']['OLD']) && !$bNew) {
         // Select meeting info about event
         if (isset($Params['currentEvent'])) {
             $oldEvent = $Params['currentEvent'];
         } else {
             $oldEvent = CCalendarEvent::GetById($arFields['ID']);
         }
         if ($oldEvent) {
             $arFields['LOCATION']['OLD'] = $oldEvent['LOCATION'];
         }
     }
     $offset = CCalendar::GetOffset();
     $arFields['LOCATION'] = CCalendar::SetLocation($arFields['LOCATION']['OLD'], $arFields['LOCATION']['NEW'], array('dateFrom' => CCalendar::Date($arFields['DT_FROM_TS'] + $offset), 'dateTo' => CCalendar::Date($arFields['DT_TO_TS'] + $offset), 'name' => $arFields['NAME'], 'persons' => count($attendees), 'attendees' => $attendees, 'bRecreateReserveMeetings' => $arFields['LOCATION']['RE_RESERVE'] !== 'N'));
     $bSendInvitations = false;
     if (!isset($arFields['IS_MEETING']) && isset($arFields['ATTENDEES']) && is_array($arFields['ATTENDEES']) && empty($arFields['ATTENDEES'])) {
         $arFields['IS_MEETING'] = false;
     }
     $attendeesCodes = array();
     if ($arFields['IS_MEETING'] && is_array($arFields['MEETING'])) {
         if (!empty($arFields['ATTENDEES_CODES'])) {
             $attendeesCodes = $arFields['ATTENDEES_CODES'];
             $arFields['ATTENDEES_CODES'] = implode(',', $arFields['ATTENDEES_CODES']);
         }
         // Organizer
         $bSendInvitations = $Params['bSendInvitations'] !== false;
         $arFields['~MEETING'] = array('HOST_NAME' => $arFields['MEETING']['HOST_NAME'], 'TEXT' => $arFields['MEETING']['TEXT'], 'OPEN' => $arFields['MEETING']['OPEN'], 'NOTIFY' => $arFields['MEETING']['NOTIFY'], 'REINVITE' => $arFields['MEETING']['REINVITE']);
         $arFields['MEETING'] = serialize($arFields['~MEETING']);
     }
     $arReminders = array();
     if ($arFields['REMIND'] && is_array($arFields['REMIND'])) {
         foreach ($arFields['REMIND'] as $remind) {
             if (in_array($remind['type'], array('min', 'hour', 'day'))) {
                 $arReminders[] = array('type' => $remind['type'], 'count' => floatVal($remind['count']));
             }
         }
     }
     $arFields['REMIND'] = count($arReminders) > 0 ? serialize($arReminders) : '';
     $AllFields = self::GetFields();
     $dbFields = array();
     foreach ($arFields as $field => $val) {
         if (isset($AllFields[$field]) && $field != "ID") {
             $dbFields[$field] = $arFields[$field];
         }
     }
     CTimeZone::Disable();
     if ($bNew) {
         $ID = CDatabase::Add("b_calendar_event", $dbFields, array('DESCRIPTION', 'MEETING', 'RDATE', 'EXDATE'));
     } else {
         $ID = $arFields['ID'];
         $strUpdate = $DB->PrepareUpdate("b_calendar_event", $dbFields);
         $strSql = "UPDATE b_calendar_event SET " . $strUpdate . " WHERE ID=" . IntVal($arFields['ID']);
         $DB->QueryBind($strSql, array('DESCRIPTION' => $arFields['DESCRIPTION'], 'MEETING' => $arFields['MEETING'], 'RDATE' => $arFields['RDATE'], 'EXDATE' => $arFields['EXDATE']));
     }
     CTimeZone::Enable();
     if ($bNew && !isset($dbFields['DAV_XML_ID'])) {
         $strSql = "UPDATE b_calendar_event SET " . $DB->PrepareUpdate("b_calendar_event", array('DAV_XML_ID' => $ID)) . " WHERE ID=" . IntVal($ID);
         $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
     }
     // Clean links
     // Del link from table
     if (!$bNew) {
         $arAffectedSections = CCalendarEvent::GetCurrentSectionIds($ID);
         $DB->Query("DELETE FROM b_calendar_event_sect WHERE EVENT_ID=" . IntVal($ID), false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__);
     } else {
         $arAffectedSections = array();
     }
     $strSections = "0";
     foreach ($arFields['SECTIONS'] as $sect) {
         if (IntVal($sect) > 0) {
             $strSections .= "," . IntVal($sect);
             $arAffectedSections[] = IntVal($sect);
         }
     }
     if (count($arAffectedSections) > 0) {
         CCalendarSect::UpdateModificationLabel($arAffectedSections);
     }
     // We don't have any section for this event
     // and we have to create default one.
     if ($strSections == "0") {
         $defCalendar = CCalendarSect::CreateDefault(array('type' => CCalendar::GetType(), 'ownerId' => CCalendar::GetOwnerId()));
         $strSections .= "," . IntVal($defCalendar['ID']);
     }
     // Add links
     $strSql = "INSERT INTO b_calendar_event_sect(EVENT_ID, SECT_ID) " . "SELECT " . intVal($ID) . ", ID " . "FROM b_calendar_section " . "WHERE ID in (" . $strSections . ")";
     $DB->Query($strSql, false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__);
     $bPull = CModule::IncludeModule("pull");
     if ($arFields['IS_MEETING']) {
         if (isset($arFields['ATTENDEES'])) {
             self::InviteAttendees($ID, $arFields, $arFields['ATTENDEES'], is_array($Params['attendeesStatuses']) ? $Params['attendeesStatuses'] : array(), $bSendInvitations, $userId);
             if ($bPull) {
                 // TODO: CACHE IT!
                 $attendees = self::GetAttendees($ID);
                 $attendees = $attendees[$ID];
                 foreach ($attendees as $user) {
                     CPullStack::AddByUser($user['USER_ID'], array('module_id' => 'calendar', 'command' => 'event_update', 'params' => array('EVENT' => CCalendarEvent::OnPullPrepareArFields($arFields), 'ATTENDEES' => $attendees, 'NEW' => $bNew ? 'Y' : 'N')));
                 }
             }
         }
     } else {
         if ($bPull) {
             CPullStack::AddByUser($userId, array('module_id' => 'calendar', 'command' => 'event_update', 'params' => array('EVENT' => CCalendarEvent::OnPullPrepareArFields($arFields), 'ATTENDEES' => array(), 'NEW' => $bNew ? 'Y' : 'N')));
         }
     }
     // Clean old reminders and add new reminders
     self::UpdateReminders(array('id' => $ID, 'reminders' => $arReminders, 'arFields' => $arFields, 'userId' => $userId, 'path' => $path, 'bNew' => $bNew));
     if ($arFields['CAL_TYPE'] == 'user' && $arFields['IS_MEETING'] && !empty($attendeesCodes)) {
         CCalendarLiveFeed::OnEditCalendarEventEntry($ID, $arFields, $attendeesCodes);
     }
     CCalendar::ClearCache('event_list');
     return $ID;
 }
 public static function Edit($Params = array())
 {
     global $DB, $CACHE_MANAGER;
     $arFields = $Params['arFields'];
     $arAffectedSections = array();
     $result = false;
     // Get current user id
     $userId = isset($Params['userId']) && intVal($Params['userId']) > 0 ? intVal($Params['userId']) : CCalendar::GetCurUserId();
     if (!$userId && isset($arFields['CREATED_BY'])) {
         $userId = intVal($arFields['CREATED_BY']);
     }
     $path = !empty($Params['path']) ? $Params['path'] : CCalendar::GetPath($arFields['CAL_TYPE'], $arFields['OWNER_ID'], true);
     $bNew = !isset($arFields['ID']) || $arFields['ID'] <= 0;
     $arFields['TIMESTAMP_X'] = CCalendar::Date(mktime(), true, false);
     if ($bNew) {
         if (!isset($arFields['CREATED_BY'])) {
             $arFields['CREATED_BY'] = $arFields['IS_MEETING'] && $arFields['CAL_TYPE'] == 'user' && $arFields['OWNER_ID'] ? $arFields['OWNER_ID'] : $userId;
         }
         if (!isset($arFields['DATE_CREATE'])) {
             $arFields['DATE_CREATE'] = $arFields['TIMESTAMP_X'];
         }
     }
     if (!isset($arFields['OWNER_ID']) || !$arFields['OWNER_ID']) {
         $arFields['OWNER_ID'] = 0;
     }
     // Current event
     $currentEvent = array();
     if (!$bNew) {
         if (isset($Params['currentEvent'])) {
             $currentEvent = $Params['currentEvent'];
         } else {
             $currentEvent = CCalendarEvent::GetById($arFields['ID']);
         }
         if (!isset($arFields['LOCATION']['OLD']) && $currentEvent) {
             $arFields['LOCATION']['OLD'] = $currentEvent['LOCATION'];
         }
         if ($currentEvent['IS_MEETING'] && !isset($arFields['ATTENDEES']) && $currentEvent['PARENT_ID'] == $currentEvent['ID'] && $arFields['IS_MEETING']) {
             $arFields['ATTENDEES'] = array();
             $attendees = self::GetAttendees($currentEvent['PARENT_ID']);
             if ($attendees[$currentEvent['PARENT_ID']]) {
                 for ($i = 0, $l = count($attendees[$currentEvent['PARENT_ID']]); $i < $l; $i++) {
                     $arFields['ATTENDEES'][] = $attendees[$currentEvent['PARENT_ID']][$i]['USER_ID'];
                 }
             }
         }
         if (($currentEvent['IS_MEETING'] || $arFields['IS_MEETING']) && $currentEvent['PARENT_ID']) {
             $arFields['PARENT_ID'] = $currentEvent['PARENT_ID'];
         }
     }
     if ($userId > 0 && self::CheckFields($arFields, $currentEvent, $userId)) {
         if ($arFields['CAL_TYPE'] == 'user') {
             $CACHE_MANAGER->ClearByTag('calendar_user_' . $arFields['OWNER_ID']);
         }
         $attendees = is_array($arFields['ATTENDEES']) ? $arFields['ATTENDEES'] : array();
         if (!$arFields['PARENT_ID']) {
             $fromTs = $arFields['DATE_FROM_TS_UTC'];
             $toTs = $arFields['DATE_TO_TS_UTC'];
             if ($arFields['DT_SKIP_TIME'] == "Y") {
                 //$toTs += CCalendar::GetDayLen();
             } else {
                 $fromTs += date('Z', $arFields['DATE_FROM_TS_UTC']);
                 $toTs += date('Z', $arFields['DATE_TO_TS_UTC']);
             }
             $arFields['LOCATION'] = CCalendar::SetLocation($arFields['LOCATION']['OLD'], $arFields['LOCATION']['NEW'], array('dateFrom' => CCalendar::Date($fromTs, $arFields['DT_SKIP_TIME'] !== "Y"), 'dateTo' => CCalendar::Date($toTs, $arFields['DT_SKIP_TIME'] !== "Y"), 'name' => $arFields['NAME'], 'persons' => count($attendees), 'attendees' => $attendees, 'bRecreateReserveMeetings' => $arFields['LOCATION']['RE_RESERVE'] !== 'N'));
         } else {
             $arFields['LOCATION'] = CCalendar::GetTextLocation($arFields['LOCATION']['NEW']);
         }
         $bSendInvitations = $Params['bSendInvitations'] !== false;
         if (!isset($arFields['IS_MEETING']) && isset($arFields['ATTENDEES']) && is_array($arFields['ATTENDEES']) && empty($arFields['ATTENDEES'])) {
             $arFields['IS_MEETING'] = false;
         }
         $attendeesCodes = array();
         if ($arFields['IS_MEETING'] && is_array($arFields['MEETING'])) {
             if (!empty($arFields['ATTENDEES_CODES'])) {
                 $attendeesCodes = $arFields['ATTENDEES_CODES'];
                 $arFields['ATTENDEES_CODES'] = implode(',', $arFields['ATTENDEES_CODES']);
             }
             $arFields['~MEETING'] = array('HOST_NAME' => $arFields['MEETING']['HOST_NAME'], 'TEXT' => $arFields['MEETING']['TEXT'], 'OPEN' => $arFields['MEETING']['OPEN'], 'NOTIFY' => $arFields['MEETING']['NOTIFY'], 'REINVITE' => $arFields['MEETING']['REINVITE']);
             $arFields['MEETING'] = serialize($arFields['~MEETING']);
             if (!isset($arFields['MEETING_STATUS'])) {
                 $arFields['MEETING_STATUS'] = 'H';
             }
         }
         $arReminders = array();
         if (is_array($arFields['REMIND'])) {
             foreach ($arFields['REMIND'] as $remind) {
                 if (in_array($remind['type'], array('min', 'hour', 'day'))) {
                     $arReminders[] = array('type' => $remind['type'], 'count' => floatVal($remind['count']));
                 }
             }
         } elseif ($currentEvent['REMIND']) {
             $arReminders = $currentEvent['REMIND'];
         }
         $arFields['REMIND'] = count($arReminders) > 0 ? serialize($arReminders) : '';
         $AllFields = self::GetFields();
         $dbFields = array();
         foreach ($arFields as $field => $val) {
             if (isset($AllFields[$field]) && $field != "ID") {
                 $dbFields[$field] = $arFields[$field];
             }
         }
         CTimeZone::Disable();
         if ($bNew) {
             $eventId = CDatabase::Add("b_calendar_event", $dbFields, array('DESCRIPTION', 'MEETING', 'RDATE', 'EXDATE'));
         } else {
             $eventId = $arFields['ID'];
             $strUpdate = $DB->PrepareUpdate("b_calendar_event", $dbFields);
             $strSql = "UPDATE b_calendar_event SET " . $strUpdate . " WHERE ID=" . IntVal($eventId);
             $DB->QueryBind($strSql, array('DESCRIPTION' => $arFields['DESCRIPTION'], 'MEETING' => $arFields['MEETING'], 'RDATE' => $arFields['RDATE'], 'EXDATE' => $arFields['EXDATE']));
         }
         CTimeZone::Enable();
         if ($bNew && !isset($dbFields['DAV_XML_ID'])) {
             $strSql = "UPDATE b_calendar_event SET " . $DB->PrepareUpdate("b_calendar_event", array('DAV_XML_ID' => $eventId)) . " WHERE ID=" . IntVal($eventId);
             $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
         }
         // *** Check and update section links ***
         $sectionId = is_array($arFields['SECTIONS']) && $arFields['SECTIONS'][0] ? intVal($arFields['SECTIONS'][0]) : false;
         if ($sectionId && CCalendarSect::GetById($sectionId, false)) {
             if (!$bNew) {
                 $arAffectedSections[] = $currentEvent['SECT_ID'];
             }
             self::ConnectEventToSection($eventId, $sectionId);
         } else {
             // It's new event we have to find section where to put it automatically
             if ($bNew) {
                 if ($arFields['IS_MEETING'] && $arFields['PARENT_ID'] && $arFields['CAL_TYPE'] == 'user') {
                     $sectionId = CCalendar::GetMeetingSection($arFields['OWNER_ID']);
                 } else {
                     $sectionId = CCalendarSect::GetLastUsedSection($arFields['CAL_TYPE'], $arFields['OWNER_ID'], $userId);
                 }
                 if ($sectionId) {
                     $res = CCalendarSect::GetList(array('arFilter' => array('CAL_TYPE' => $arFields['CAL_TYPE'], 'OWNER_ID' => $arFields['OWNER_ID'], 'ID' => $sectionId)));
                     if (!$res || !$res[0]) {
                         $sectionId = false;
                     }
                 } else {
                     $sectionId = false;
                 }
                 if (!$sectionId) {
                     $sectRes = CCalendarSect::GetSectionForOwner($arFields['CAL_TYPE'], $arFields['OWNER_ID'], true);
                     $sectionId = $sectRes['sectionId'];
                 }
                 self::ConnectEventToSection($eventId, $sectionId);
             } else {
                 // It's existing event, we take it's section to update modification lables (no db changes in b_calendar_event_sect)
                 $sectionId = $currentEvent['SECT_ID'];
             }
         }
         $arAffectedSections[] = $sectionId;
         if (count($arAffectedSections) > 0) {
             CCalendarSect::UpdateModificationLabel($arAffectedSections);
         }
         $bPull = CModule::IncludeModule("pull");
         if ($arFields['IS_MEETING'] || !$bNew && $currentEvent['IS_MEETING']) {
             if (!$arFields['PARENT_ID']) {
                 $DB->Query("UPDATE b_calendar_event SET " . $DB->PrepareUpdate("b_calendar_event", array("PARENT_ID" => $eventId)) . " WHERE ID=" . intVal($eventId), false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__);
             }
             if (!$arFields['PARENT_ID'] || $arFields['PARENT_ID'] == $eventId) {
                 self::CreateChildEvents($eventId, $arFields, $Params, $userId);
             }
             if (!$arFields['PARENT_ID']) {
                 $arFields['PARENT_ID'] = intVal($eventId);
             }
         } else {
             if ($bNew && !$arFields['PARENT_ID'] || !$bNew && !$currentEvent['PARENT_ID']) {
                 $DB->Query("UPDATE b_calendar_event SET " . $DB->PrepareUpdate("b_calendar_event", array("PARENT_ID" => $eventId)) . " WHERE ID=" . intVal($eventId), false, "FILE: " . __FILE__ . "<br> LINE: " . __LINE__);
                 if (!$arFields['PARENT_ID']) {
                     $arFields['PARENT_ID'] = intVal($eventId);
                 }
             }
             if ($bPull) {
                 $curUserId = $userId;
                 if ($arFields['PARENT_ID'] && $arFields['PARENT_ID'] !== $arFields['ID']) {
                     $curUserId = $arFields['OWNER_ID'];
                 }
                 CPullStack::AddByUser($curUserId, array('module_id' => 'calendar', 'command' => 'event_update', 'params' => array('EVENT' => CCalendarEvent::OnPullPrepareArFields($arFields), 'ATTENDEES' => array(), 'NEW' => $bNew ? 'Y' : 'N')));
             }
         }
         // Clean old reminders and add new reminders
         self::UpdateReminders(array('id' => $eventId, 'reminders' => $arReminders, 'arFields' => $arFields, 'userId' => $userId, 'path' => $path, 'bNew' => $bNew));
         // Send invitations and notivications
         if ($arFields['IS_MEETING']) {
             if ($bSendInvitations) {
                 if ($arFields['PARENT_ID'] != $eventId) {
                     $CACHE_MANAGER->ClearByTag('calendar_user_' . $arFields['OWNER_ID']);
                     $fromTo = CCalendarEvent::GetEventFromToForUser($arFields, $arFields['OWNER_ID']);
                     CCalendar::SendMessage(array('mode' => 'invite', 'name' => $arFields['NAME'], "from" => $fromTo['DATE_FROM'], "to" => $fromTo['DATE_TO'], "location" => CCalendar::GetTextLocation($arFields["LOCATION"]), "meetingText" => $arFields["~MEETING"]["TEXT"], "guestId" => $arFields['OWNER_ID'], "eventId" => $arFields['PARENT_ID'], "userId" => $userId));
                 }
             } else {
                 if ($arFields['PARENT_ID'] != $eventId && $arFields['MEETING_STATUS'] == "Y") {
                     $CACHE_MANAGER->ClearByTag('calendar_user_' . $arFields['OWNER_ID']);
                     $fromTo = CCalendarEvent::GetEventFromToForUser($arFields, $arFields['OWNER_ID']);
                     CCalendar::SendMessage(array('mode' => 'change_notify', 'name' => $arFields['NAME'], "from" => $fromTo['DATE_FROM'], "to" => $fromTo['DATE_TO'], "location" => CCalendar::GetTextLocation($arFields["LOCATION"]), "meetingText" => $arFields["~MEETING"]["TEXT"], "guestId" => $arFields['OWNER_ID'], "eventId" => $arFields['PARENT_ID'], "userId" => $userId));
                 }
             }
         }
         if ($arFields['CAL_TYPE'] == 'user' && $arFields['IS_MEETING'] && !empty($attendeesCodes) && $arFields['PARENT_ID'] == $eventId) {
             CCalendarLiveFeed::OnEditCalendarEventEntry($eventId, $arFields, $attendeesCodes);
         }
         CCalendar::ClearCache('event_list');
         $result = $eventId;
     }
     return $result;
 }