public static function CheckFields(&$arFields)
 {
     // Check dates
     //		$arFields['DT_FROM'] = CCalendar::CutZeroTime($arFields['DT_FROM']);
     //		$arFields['DT_TO'] = CCalendar::CutZeroTime($arFields['DT_TO']);
     $fromTs = CCalendar::Timestamp($arFields['DT_FROM']);
     $toTs = CCalendar::Timestamp($arFields['DT_TO']);
     if (!isset($arFields['DT_FROM_TS'])) {
         $arFields['DT_FROM_TS'] = $fromTs;
     }
     if (!isset($arFields['DT_TO_TS'])) {
         $arFields['DT_TO_TS'] = $toTs;
     }
     $h24 = 60 * 60 * 24;
     // 24 hours
     // Skip time
     if (isset($arFields['SKIP_TIME'])) {
         $arFields['DT_SKIP_TIME'] = $arFields['SKIP_TIME'] ? 'Y' : 'N';
         unset($arFields['SKIP_TIME']);
     } elseif (isset($arFields['DT_SKIP_TIME']) && $arFields['DT_SKIP_TIME'] != 'Y' && $arFields['DT_SKIP_TIME'] != 'N') {
         unset($arFields['DT_SKIP_TIME']);
     }
     // Event length in seconds
     if (!isset($arFields['DT_LENGTH'])) {
         //if($fromTs == $toTs && date('H:i', $fromTs) == '00:00') // One day
         if ($fromTs == $toTs && date('H:i', $fromTs) == '00:00' && $arFields['DT_SKIP_TIME'] == 'Y') {
             $arFields['DT_LENGTH'] = $h24;
         } else {
             $arFields['DT_LENGTH'] = intVal($toTs - $fromTs);
             if ($arFields['DT_SKIP_TIME'] == "Y") {
                 // We have dates without times
                 $arFields['DT_LENGTH'] += $h24;
             }
         }
     }
     if (!$arFields['VERSION']) {
         $arFields['VERSION'] = 1;
     }
     // Accessibility
     $arFields['ACCESSIBILITY'] = trim(strtolower($arFields['ACCESSIBILITY']));
     if (!in_array($arFields['ACCESSIBILITY'], array('busy', 'quest', 'free', 'absent'))) {
         $arFields['ACCESSIBILITY'] = 'busy';
     }
     // Importance
     $arFields['IMPORTANCE'] = trim(strtolower($arFields['IMPORTANCE']));
     if (!in_array($arFields['IMPORTANCE'], array('high', 'normal', 'low'))) {
         $arFields['IMPORTANCE'] = 'normal';
     }
     // Color
     $arFields['COLOR'] = CCalendar::Color($arFields['COLOR'], false);
     // Section
     if (!is_array($arFields['SECTIONS']) && intVal($arFields['SECTIONS']) > 0) {
         $arFields['SECTIONS'] = array(intVal($arFields['SECTIONS']));
     }
     // Check rrules
     if (is_array($arFields['RRULE']) && isset($arFields['RRULE']['FREQ']) && in_array($arFields['RRULE']['FREQ'], array('HOURLY', 'DAILY', 'MONTHLY', 'YEARLY', 'WEEKLY'))) {
         // Interval
         if (isset($arFields['RRULE']['INTERVAL']) && intVal($arFields['RRULE']['INTERVAL']) > 1) {
             $arFields['RRULE']['INTERVAL'] = intVal($arFields['RRULE']['INTERVAL']);
         }
         // Until date
         if ($arFields['RRULE']['UNTIL']) {
             if (!preg_match('/[^\\d]/', $arFields['RRULE']['UNTIL'])) {
                 $periodTs = $arFields['RRULE']['UNTIL'];
                 if ($periodTs > CCalendar::GetMaxTimestamp()) {
                     $periodTs = CCalendar::GetMaxTimestamp();
                 }
             } else {
                 $periodTs = CCalendar::Timestamp($arFields['RRULE']['UNTIL']);
             }
         } else {
             $periodTs = CCalendar::GetMaxTimestamp();
         }
         $arFields['RRULE']['UNTIL'] = $periodTs;
         $arFields['DT_TO'] = CCalendar::Date($periodTs);
         $arFields['DT_TO_TS'] = $periodTs;
         if (isset($arFields['RRULE']['BYDAY'])) {
             $BYDAY = array();
             $days = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
             $bydays = explode(',', $arFields['RRULE']['BYDAY']);
             foreach ($bydays as $day) {
                 $day = strtoupper($day);
                 if (in_array($day, $days)) {
                     $BYDAY[] = $day;
                 }
             }
             $arFields['RRULE']['BYDAY'] = implode(',', $BYDAY);
         }
         $arFields['RRULE'] = self::PackRRule($arFields['RRULE']);
     } else {
         $arFields['RRULE'] = '';
     }
     $arFields['EXRULE'] = "";
     $arFields['RDATE'] = "";
     $arFields['EXDATE'] = "";
     // Location
     if (!is_array($arFields['LOCATION'])) {
         $arFields['LOCATION'] = array("NEW" => is_string($arFields['LOCATION']) ? $arFields['LOCATION'] : "");
     }
     // Private
     $arFields['PRIVATE_EVENT'] = isset($arFields['PRIVATE_EVENT']) && $arFields['PRIVATE_EVENT'];
     return true;
 }
 public static function SectionUpdate($arParams = array(), $nav = null, $server = null)
 {
     $userId = CCalendar::GetCurUserId();
     $methodName = "calendar.section.update";
     if (isset($arParams['type'])) {
         $type = $arParams['type'];
     } else {
         throw new Exception(GetMessage('CAL_REST_PARAM_EXCEPTION', array('#REST_METHOD#' => $methodName, '#PARAM_NAME#' => 'type')));
     }
     if (isset($arParams['ownerId'])) {
         $ownerId = intval($arParams['ownerId']);
     } elseif ($type == 'user') {
         $ownerId = $userId;
     } else {
         throw new Exception(GetMessage('CAL_REST_PARAM_EXCEPTION', array('#REST_METHOD#' => $methodName, '#PARAM_NAME#' => 'ownerId')));
     }
     if (isset($arParams['id']) && intVal($arParams['id']) > 0) {
         $id = intVal($arParams['id']);
     } else {
         throw new Exception(GetMessage('CAL_REST_SECT_ID_EXCEPTION'));
     }
     if (!CCalendar::IsPersonal($type, $ownerId, $userId) && !CCalendarSect::CanDo('calendar_edit_section', $id, $userId)) {
         throw new Exception(GetMessage('CAL_REST_ACCESS_DENIED'));
     }
     $arFields = array('ID' => $id, 'CAL_TYPE' => $type, 'OWNER_ID' => $ownerId);
     if (isset($arParams['name']) && trim($arParams['name']) != '') {
         $arFields['NAME'] = trim($arParams['name']);
     }
     if (isset($arParams['description']) && trim($arParams['description']) != '') {
         $arFields['DESCRIPTION'] = trim($arParams['description']);
     }
     if (isset($arParams['color'])) {
         $arFields['COLOR'] = CCalendar::Color($arParams['color']);
     }
     if (isset($arParams['text_color'])) {
         $arFields['TEXT_COLOR'] = CCalendar::Color($arParams['text_color']);
     }
     if (isset($arParams['access']) && is_array($arParams['access'])) {
         $arFields['ACCESS'] = $arParams['access'];
     }
     $id = intVal(CCalendar::SaveSection(array('bAffectToDav' => false, 'arFields' => $arFields)));
     if (!$id) {
         throw new Exception(GetMessage('CAL_REST_SECTION_SAVE_ERROR'));
     }
     return $id;
 }
 public static function CreateDefault($Params = array())
 {
     if ($Params['type'] == 'user' || $Params['type'] == 'group') {
         $name = CCalendar::GetOwnerName($Params['type'], $Params['ownerId']);
     } else {
         $name = GetMessage('EC_DEF_SECT_GROUP_CAL');
     }
     // if ($Params['type'] == 'user')
     // $name = GetMessage('EC_DEF_SECT_USER_CAL');
     // else
     // $name = GetMessage('EC_DEF_SECT_GROUP_CAL');
     $arFields = array('CAL_TYPE' => $Params['type'], 'NAME' => $name, 'DESCRIPTION' => GetMessage('EC_DEF_SECT_DESC'), 'COLOR' => CCalendar::Color(), 'OWNER_ID' => $Params['ownerId'], 'IS_EXCHANGE' => 0, 'ACCESS' => CCalendarSect::GetDefaultAccess($Params['type'], $Params['ownerId']), 'PERM' => array('view_time' => true, 'view_title' => true, 'view_full' => true, 'add' => true, 'edit' => true, 'edit_section' => true, 'access' => true));
     $arFields['ID'] = self::Edit(array('arFields' => $arFields));
     if ($arFields['ID'] > 0) {
         return $arFields;
     }
     return false;
 }
 public static function CheckFields(&$arFields, $currentEvent = array(), $userId = false)
 {
     if (!isset($arFields['TIMESTAMP_X'])) {
         $arFields['TIMESTAMP_X'] = CCalendar::Date(mktime(), true, false);
     }
     if (!$userId) {
         $userId = CCalendar::GetCurUserId();
     }
     $bNew = !isset($arFields['ID']) || $arFields['ID'] <= 0;
     if (!isset($arFields['DATE_CREATE']) && $bNew) {
         $arFields['DATE_CREATE'] = $arFields['TIMESTAMP_X'];
     }
     // Skip time
     if (isset($arFields['SKIP_TIME'])) {
         $arFields['DT_SKIP_TIME'] = $arFields['SKIP_TIME'] ? 'Y' : 'N';
         unset($arFields['SKIP_TIME']);
     } elseif (isset($arFields['DT_SKIP_TIME']) && $arFields['DT_SKIP_TIME'] != 'Y' && $arFields['DT_SKIP_TIME'] != 'N') {
         unset($arFields['DT_SKIP_TIME']);
     }
     unset($arFields['DT_FROM']);
     unset($arFields['DT_TO']);
     $arFields['DT_SKIP_TIME'] = $arFields['DT_SKIP_TIME'] !== 'Y' ? 'N' : 'Y';
     $fromTs = CCalendar::Timestamp($arFields['DATE_FROM'], false, $arFields['DT_SKIP_TIME'] !== 'Y');
     $toTs = CCalendar::Timestamp($arFields['DATE_TO'], false, $arFields['DT_SKIP_TIME'] !== 'Y');
     if ($fromTs > $toTs) {
         $toTs = $fromTs;
     }
     $arFields['DATE_FROM'] = CCalendar::Date($fromTs);
     $arFields['DATE_TO'] = CCalendar::Date($toTs);
     if (!$fromTs) {
         $arFields['DATE_FROM'] = FormatDate("SHORT", time());
         $fromTs = CCalendar::Timestamp($arFields['DATE_FROM'], false, false);
         if (!$toTs) {
             $arFields['DATE_TO'] = $arFields['DATE_FROM'];
             $toTs = $fromTs;
             $arFields['DT_SKIP_TIME'] = 'Y';
         }
     } elseif (!$toTs) {
         $arFields['DATE_TO'] = $arFields['DATE_FROM'];
         $toTs = $fromTs;
     }
     if ($arFields['DT_SKIP_TIME'] !== 'Y') {
         $arFields['DT_SKIP_TIME'] = 'N';
         if (!isset($arFields['TZ_FROM']) && isset($currentEvent['TZ_FROM'])) {
             $arFields['TZ_FROM'] = $currentEvent['TZ_FROM'];
         }
         if (!isset($arFields['TZ_TO']) && isset($currentEvent['TZ_TO'])) {
             $arFields['TZ_TO'] = $currentEvent['TZ_TO'];
         }
         if (!isset($arFields['TZ_FROM']) && !isset($arFields['TZ_TO'])) {
             $userTimezoneOffsetUTC = CCalendar::GetCurrentOffsetUTC($userId);
             $userTimezoneName = CCalendar::GetUserTimezoneName($userId);
             if (!$userTimezoneName) {
                 $userTimezoneName = CCalendar::GetGoodTimezoneForOffset($userTimezoneOffsetUTC);
             }
             $arFields['TZ_FROM'] = $userTimezoneName;
             $arFields['TZ_TO'] = $userTimezoneName;
         }
         if (!isset($arFields['TZ_OFFSET_FROM'])) {
             $arFields['TZ_OFFSET_FROM'] = CCalendar::GetTimezoneOffset($arFields['TZ_FROM'], $fromTs);
         }
         if (!isset($arFields['TZ_OFFSET_TO'])) {
             $arFields['TZ_OFFSET_TO'] = CCalendar::GetTimezoneOffset($arFields['TZ_TO'], $toTs);
         }
     }
     if (!isset($arFields['TZ_OFFSET_FROM'])) {
         $arFields['TZ_OFFSET_FROM'] = 0;
     }
     if (!isset($arFields['TZ_OFFSET_TO'])) {
         $arFields['TZ_OFFSET_TO'] = 0;
     }
     if (!isset($arFields['DATE_FROM_TS_UTC'])) {
         $arFields['DATE_FROM_TS_UTC'] = $fromTs - $arFields['TZ_OFFSET_FROM'];
     }
     if (!isset($arFields['DATE_TO_TS_UTC'])) {
         $arFields['DATE_TO_TS_UTC'] = $toTs - $arFields['TZ_OFFSET_TO'];
     }
     $h24 = 60 * 60 * 24;
     // 24 hours
     if ($arFields['DT_SKIP_TIME'] == 'Y') {
         unset($arFields['TZ_FROM']);
         unset($arFields['TZ_TO']);
         unset($arFields['TZ_OFFSET_FROM']);
         unset($arFields['TZ_OFFSET_TO']);
     }
     // Event length in seconds
     if (!isset($arFields['DT_LENGTH']) || $arFields['DT_LENGTH'] == 0) {
         if ($fromTs == $toTs && date('H:i', $fromTs) == '00:00' && $arFields['DT_SKIP_TIME'] == 'Y') {
             $arFields['DT_LENGTH'] = $h24;
         } else {
             $arFields['DT_LENGTH'] = intVal($toTs - $fromTs);
             if ($arFields['DT_SKIP_TIME'] == "Y") {
                 // We have dates without times
                 $arFields['DT_LENGTH'] += $h24;
             }
         }
     }
     if (!$arFields['VERSION']) {
         $arFields['VERSION'] = 1;
     }
     // Accessibility
     $arFields['ACCESSIBILITY'] = trim(strtolower($arFields['ACCESSIBILITY']));
     if (!in_array($arFields['ACCESSIBILITY'], array('busy', 'quest', 'free', 'absent'))) {
         $arFields['ACCESSIBILITY'] = 'busy';
     }
     // Importance
     $arFields['IMPORTANCE'] = trim(strtolower($arFields['IMPORTANCE']));
     if (!in_array($arFields['IMPORTANCE'], array('high', 'normal', 'low'))) {
         $arFields['IMPORTANCE'] = 'normal';
     }
     // Color
     $arFields['COLOR'] = CCalendar::Color($arFields['COLOR'], false);
     // Section
     if (!is_array($arFields['SECTIONS']) && intVal($arFields['SECTIONS']) > 0) {
         $arFields['SECTIONS'] = array(intVal($arFields['SECTIONS']));
     }
     // Check rrules
     if (is_array($arFields['RRULE']) && isset($arFields['RRULE']['FREQ']) && in_array($arFields['RRULE']['FREQ'], array('HOURLY', 'DAILY', 'MONTHLY', 'YEARLY', 'WEEKLY'))) {
         // Interval
         if (isset($arFields['RRULE']['INTERVAL']) && intVal($arFields['RRULE']['INTERVAL']) > 1) {
             $arFields['RRULE']['INTERVAL'] = intVal($arFields['RRULE']['INTERVAL']);
         }
         // Until date
         $periodTs = CCalendar::Timestamp($arFields['RRULE']['UNTIL'], false, false);
         if (!$periodTs) {
             $arFields['RRULE']['UNTIL'] = CCalendar::GetMaxDate();
             $periodTs = CCalendar::Timestamp($arFields['RRULE']['UNTIL'], false, false);
         }
         $arFields['DATE_TO_TS_UTC'] = $periodTs;
         $arFields['RRULE']['UNTIL'] = CCalendar::Date($periodTs, false);
         if (isset($arFields['RRULE']['BYDAY'])) {
             if (is_array($arFields['RRULE']['BYDAY'])) {
                 $BYDAY = $arFields['RRULE']['BYDAY'];
             } else {
                 $BYDAY = array();
                 $days = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
                 $bydays = explode(',', $arFields['RRULE']['BYDAY']);
                 foreach ($bydays as $day) {
                     $day = strtoupper($day);
                     if (in_array($day, $days)) {
                         $BYDAY[] = $day;
                     }
                 }
             }
             $arFields['RRULE']['BYDAY'] = implode(',', $BYDAY);
         }
         unset($arFields['RRULE']['~UNTIL']);
         $arFields['RRULE'] = self::PackRRule($arFields['RRULE']);
     } else {
         $arFields['RRULE'] = '';
     }
     $arFields['EXRULE'] = "";
     $arFields['RDATE'] = "";
     $arFields['EXDATE'] = "";
     // Location
     if (!is_array($arFields['LOCATION'])) {
         $arFields['LOCATION'] = array("NEW" => is_string($arFields['LOCATION']) ? $arFields['LOCATION'] : "");
     }
     // Private
     $arFields['PRIVATE_EVENT'] = isset($arFields['PRIVATE_EVENT']) && $arFields['PRIVATE_EVENT'];
     return true;
 }