Beispiel #1
0
 public static function ModifyEvent($calendarId, $arFields)
 {
     list($sectionId, $entityType, $entityId) = $calendarId;
     $userId = $entityType == 'user' ? $entityId : 0;
     self::$silentErrorMode = true;
     CCalendar::SetOffset(false, CCalendar::GetOffset($userId));
     $entityType = strtolower($entityType);
     $eventId = isset($arFields["ID"]) && intval($arFields["ID"]) > 0 ? intval($arFields["ID"]) : 0;
     $arNewFields = array("DAV_XML_ID" => $arFields['XML_ID'], "CAL_DAV_LABEL" => isset($arFields['PROPERTY_BXDAVCD_LABEL']) && strlen($arFields['PROPERTY_BXDAVCD_LABEL']) > 0 ? $arFields['PROPERTY_BXDAVCD_LABEL'] : '', "DAV_EXCH_LABEL" => isset($arFields['PROPERTY_BXDAVEX_LABEL']) && strlen($arFields['PROPERTY_BXDAVEX_LABEL']) > 0 ? $arFields['PROPERTY_BXDAVEX_LABEL'] : '', "ID" => $eventId, 'NAME' => $arFields["NAME"] ? $arFields["NAME"] : GetMessage('EC_NONAME_EVENT'), 'CAL_TYPE' => $entityType, 'OWNER_ID' => $entityId, 'DESCRIPTION' => isset($arFields['DESCRIPTION']) ? $arFields['DESCRIPTION'] : '', 'SECTIONS' => $sectionId, 'ACCESSIBILITY' => isset($arFields['ACCESSIBILITY']) ? $arFields['ACCESSIBILITY'] : 'busy', 'IMPORTANCE' => isset($arFields['IMPORTANCE']) ? $arFields['IMPORTANCE'] : 'normal', "REMIND" => is_array($arFields['REMIND']) ? $arFields['REMIND'] : array(), "RRULE" => is_array($arFields['RRULE']) ? is_array($arFields['RRULE']) : array(), "VERSION" => isset($arFields['VERSION']) ? intVal($arFields['VERSION']) : 1, "PRIVATE_EVENT" => !!$arFields['PRIVATE_EVENT']);
     $bServerTime = false;
     if (isset($arFields['DT_FROM_TS'], $arFields['DT_TO_TS'])) {
         $arNewFields["DT_FROM_TS"] = $arFields['DT_FROM_TS'];
         $arNewFields["DT_TO_TS"] = $arFields['DT_TO_TS'];
         $arNewFields["SKIP_TIME"] = $arFields['SKIP_TIME'];
         $bServerTime = true;
     } elseif (isset($arFields['ACTIVE_FROM'], $arFields['ACTIVE_TO'])) {
         $arNewFields["DT_FROM"] = $arFields['ACTIVE_FROM'];
         $arNewFields["DT_TO"] = $arFields['ACTIVE_TO'];
     }
     if (!empty($arFields['PROPERTY_REMIND_SETTINGS'])) {
         $ar = explode("_", $arFields["PROPERTY_REMIND_SETTINGS"]);
         if (count($ar) == 2) {
             $arNewFields["REMIND"][] = array('type' => $ar[1], 'count' => floatVal($ar[0]));
         }
     }
     if (!empty($arFields['PROPERTY_ACCESSIBILITY'])) {
         $arNewFields["ACCESSIBILITY"] = $arFields['PROPERTY_ACCESSIBILITY'];
     }
     if (!empty($arFields['PROPERTY_IMPORTANCE'])) {
         $arNewFields["IMPORTANCE"] = $arFields['PROPERTY_IMPORTANCE'];
     }
     if (!empty($arFields['PROPERTY_LOCATION'])) {
         $arNewFields["LOCATION"] = CCalendar::UnParseTextLocation($arFields['PROPERTY_LOCATION']);
     }
     if (!empty($arFields['DETAIL_TEXT'])) {
         $arNewFields["DESCRIPTION"] = $arFields['DETAIL_TEXT'];
     }
     $arNewFields["DESCRIPTION"] = CCalendar::ClearExchangeHtml($arNewFields["DESCRIPTION"]);
     if (isset($arFields["PROPERTY_PERIOD_TYPE"]) && in_array($arFields["PROPERTY_PERIOD_TYPE"], array("DAILY", "WEEKLY", "MONTHLY", "YEARLY"))) {
         $arNewFields['RRULE']['FREQ'] = $arFields["PROPERTY_PERIOD_TYPE"];
         $arNewFields['RRULE']['INTERVAL'] = $arFields["PROPERTY_PERIOD_COUNT"];
         if (!isset($arNewFields['DT_LENGTH']) && !empty($arFields['PROPERTY_EVENT_LENGTH'])) {
             $arNewFields['DT_LENGTH'] = intval($arFields['PROPERTY_EVENT_LENGTH']);
         } else {
             $arNewFields['DT_LENGTH'] = $arFields['DT_TO_TS'] - $arFields['DT_FROM_TS'];
             //$arNewFields['DT_LENGTH'] = self::GetDayLen();
         }
         if ($arNewFields['RRULE']['FREQ'] == "WEEKLY" && !empty($arFields['PROPERTY_PERIOD_ADDITIONAL'])) {
             $arNewFields['RRULE']['BYDAY'] = array();
             $bydays = explode(',', $arFields['PROPERTY_PERIOD_ADDITIONAL']);
             foreach ($bydays as $day) {
                 $day = CCalendar::WeekDayByInd($day, false);
                 if ($day !== false) {
                     $arNewFields['RRULE']['BYDAY'][] = $day;
                 }
             }
             $arNewFields['RRULE']['BYDAY'] = implode(',', $arNewFields['RRULE']['BYDAY']);
         }
         if (isset($arFields['ACTIVE_TO'])) {
             $arNewFields['RRULE']['UNTIL'] = $arFields['ACTIVE_TO'];
         } else {
             $arNewFields['RRULE']['UNTIL'] = $arFields['DT_TO_TS'];
         }
     }
     $eventId = CCalendar::SaveEvent(array('arFields' => $arNewFields, 'userId' => $userId, 'bAffectToDav' => false, 'bSilentAccessMeeting' => true, 'bServerTime' => $bServerTime));
     self::$silentErrorMode = false;
     return $eventId;
 }
Beispiel #2
0
 public static function ParseRecursion(&$res, $event, $Params = array())
 {
     $event['DT_LENGTH'] = intVal($event['DT_LENGTH']);
     // length in seconds
     $length = $event['DT_LENGTH'];
     $rrule = self::ParseRRULE($event['RRULE']);
     $h24 = CCalendar::GetDayLen();
     $instanceCount = $Params['instanceCount'] && $Params['instanceCount'] > 0 ? $Params['instanceCount'] : false;
     $preciseLimits = $Params['preciseLimits'];
     if ($length < 0) {
         // Protection from infinite recursion
         $length = $h24;
     }
     // Time boundaries
     if (isset($Params['fromLimitTs'])) {
         $limitFromTS = intVal($Params['fromLimitTs']);
     } else {
         $limitFromTS = CCalendar::Timestamp($Params['fromLimit']);
     }
     if (isset($Params['toLimitTs'])) {
         $limitToTS = intVal($Params['toLimitTs']);
     } else {
         $limitToTS = CCalendar::Timestamp($Params['toLimit']);
     }
     $limitFromTSReal = $limitFromTS;
     $evFromTS = isset($event['DT_FROM_TS']) ? $event['DT_FROM_TS'] : CCalendar::Timestamp($event['DT_FROM']);
     $evToTS = isset($event['DT_TO_TS']) ? $event['DT_TO_TS'] : CCalendar::Timestamp($event['DT_TO']);
     if ($limitFromTS < $evFromTS) {
         $limitFromTS = $evFromTS;
     }
     if ($limitToTS > $evToTS) {
         $limitToTS = $evToTS;
     }
     $fromTS = $evFromTS;
     $offset = CCalendar::GetOffset();
     if ($event['DT_SKIP_TIME'] == 'N' && $offset != 0) {
         $fromTS += $offset;
     }
     $event['~DT_FROM'] = $event['DT_FROM'];
     $event['~DT_FROM_TS'] = $event['DT_FROM_TS'];
     if ($event['DT_SKIP_TIME'] == 'Y') {
         if ($event['DT_LENGTH'] == $h24) {
             $event['~DT_TO'] = $event['DT_FROM'];
             $event['~DT_TO_TS'] = $event['DT_FROM_TS'];
         } else {
             $event['~DT_TO_TS'] = mktime(date("H", $evFromTS), date("i", $evFromTS), date("s", $evFromTS) + $event['DT_LENGTH'] - $h24, date("m", $evFromTS), date("d", $evFromTS), date("Y", $evFromTS));
             $event['~DT_TO'] = CCalendar::Date($event['~DT_TO_TS']);
         }
     } else {
         $event['~DT_TO_TS'] = mktime(date("H", $evFromTS), date("i", $evFromTS), date("s", $evFromTS) + $event['DT_LENGTH'], date("m", $evFromTS), date("d", $evFromTS), date("Y", $evFromTS));
         $event['~DT_TO'] = CCalendar::Date($event['~DT_TO_TS']);
     }
     $hour = date("H", $fromTS);
     $min = date("i", $fromTS);
     $sec = date("s", $fromTS);
     $orig_d = date("d", $fromTS);
     $orig_m = date("m", $fromTS);
     $orig_y = date("Y", $fromTS);
     $count = 0;
     $realCount = 0;
     $dispCount = 0;
     while (true) {
         $d = date("d", $fromTS);
         $m = date("m", $fromTS);
         $y = date("Y", $fromTS);
         $toTS = mktime($hour, $min, $sec + $length, $m, $d, $y);
         if (!$fromTS || $fromTS < $evFromTS - CCalendar::GetDayLen() || $rrule['COUNT'] > 0 && $count >= $rrule['COUNT'] || !$rrule['COUNT'] && $fromTS >= $limitToTS || $instanceCount && $dispCount >= $instanceCount) {
             break;
         }
         if ($rrule['FREQ'] == 'WEEKLY') {
             $weekDay = CCalendar::WeekDayByInd(date("w", $fromTS));
             if ($rrule['BYDAY'][$weekDay]) {
                 if ($preciseLimits && $fromTS > $limitFromTSReal || !$preciseLimits && $fromTS > $limitFromTS - $h24) {
                     if ($event['DT_SKIP_TIME'] == 'N' && $offset != 0) {
                         $fromTS -= $offset;
                         $toTS -= $offset;
                     }
                     self::HandleREvent($res, $event, array('DT_FROM_TS' => $fromTS, 'DT_TO_TS' => $toTS, 'RINDEX' => $count, 'RRULE' => $rrule));
                     $dispCount++;
                 }
                 $realCount++;
             }
             if (isset($weekDay) && $weekDay == 'SU') {
                 $delta = ($rrule['INTERVAL'] - 1) * 7 + 1;
             } else {
                 $delta = 1;
             }
             $fromTS = mktime($hour, $min, $sec, $m, $d + $delta, $y);
         } else {
             if ($preciseLimits && $fromTS > $limitFromTSReal || !$preciseLimits && $fromTS > $limitFromTS - $h24) {
                 if ($event['DT_SKIP_TIME'] == 'N' && $offset != 0) {
                     $fromTS -= $offset;
                     $toTS -= $offset;
                 }
                 self::HandleREvent($res, $event, array('DT_FROM_TS' => $fromTS, 'DT_TO_TS' => $toTS, 'RINDEX' => $count, 'RRULE' => $rrule));
                 $dispCount++;
             }
             $realCount++;
             switch ($rrule['FREQ']) {
                 //					case 'HOURLY':
                 //						$fromTS = mktime($hour + $rrule['INTERVAL'], $min, $sec, $m, $d, $y);
                 //						break;
                 case 'DAILY':
                     $fromTS = mktime($hour, $min, $sec, $m, $d + $rrule['INTERVAL'], $y);
                     break;
                 case 'MONTHLY':
                     $durOffset = $realCount * $rrule['INTERVAL'];
                     $day = $orig_d;
                     $month = $orig_m + $durOffset;
                     $year = $orig_y;
                     if ($month > 12) {
                         $delta_y = floor($month / 12);
                         $delta_m = $month - $delta_y * 12;
                         $month = $delta_m;
                         $year = $orig_y + $delta_y;
                     }
                     // 1. Check only for 29-31 dates. 2.We are out of range in this month
                     if ($orig_d > 28 && $orig_d > date("t", mktime($hour, $min, $sec, $month, 1, $year))) {
                         $month++;
                         $day = 1;
                     }
                     $fromTS = mktime($hour, $min, $sec, $month, $day, $year);
                     break;
                 case 'YEARLY':
                     $fromTS = mktime($hour, $min, $sec, $orig_m, $orig_d, $y + $rrule['INTERVAL']);
                     break;
             }
         }
         $count++;
     }
 }
 public static function ConvertEntity($ownerType, $ownerId, $sectionId, $iblockId, $createdBy)
 {
     $eventsCount = 0;
     $sectCount = 0;
     $bs = new CIBlockSection();
     $ent_id = "IBLOCK_" . $iblockId . "_SECTION";
     $result = array('eventsCount' => 0, 'sectCount' => 0);
     $bSetAccessFromCalendar = true;
     $Access = array('G2' => CCalendar::GetAccessTasksByName('calendar_section', 'calendar_denied'));
     // CONVERT ACCESS:
     if ($ownerType == 'user') {
         if (!CSocNetFeatures::IsActiveFeature(SONET_ENTITY_USER, $ownerId, "calendar")) {
             return $result;
         }
         // Read
         $read = CSocNetFeaturesPerms::GetOperationPerm(SONET_ENTITY_USER, $ownerId, "calendar", 'view');
         $taskId = CCalendar::GetAccessTasksByName('calendar_section', 'calendar_view');
         if ($read == 'A') {
             // All users
             $Access['G2'] = $taskId;
         } elseif ($read == 'C') {
             // All autorized
             $Access['AU'] = $taskId;
         } elseif ($read == 'M' || $read == 'E') {
             // Friends
             $Access['SU' . $ownerId . '_F'] = $taskId;
         } elseif ($bSetAccessFromCalendar) {
             $bSetAccessFromCalendar = false;
         }
         // Write - will override read access
         $write = CSocNetFeaturesPerms::GetOperationPerm(SONET_ENTITY_USER, $ownerId, "calendar", 'write');
         $taskId = CCalendar::GetAccessTasksByName('calendar_section', 'calendar_edit');
         if ($write == 'A') {
             // All users
             $Access['G2'] = $taskId;
         } elseif ($write == 'C') {
             // All autorized
             $Access['AU'] = $taskId;
         } elseif ($write == 'M' || $write == 'E') {
             // Friends
             $Access['SU' . $ownerId . '_F'] = $taskId;
         }
     } elseif ($ownerType == 'group') {
         if (!CSocNetFeatures::IsActiveFeature(SONET_ENTITY_GROUP, $ownerId, "calendar")) {
             return $result;
         }
         // Read
         $read = CSocNetFeaturesPerms::GetOperationPerm(SONET_ENTITY_GROUP, $ownerId, "calendar", 'view');
         $taskId = CCalendar::GetAccessTasksByName('calendar_section', 'calendar_view');
         if ($read == 'A') {
             // Group owner
             $Access['SG' . $ownerId . '_A'] = $taskId;
         } elseif ($read == 'E') {
             // Group moderator
             $Access['SG' . $ownerId . '_E'] = $taskId;
         } elseif ($read == 'K') {
             // User
             $Access['SG' . $ownerId . '_K'] = $taskId;
         } elseif ($read == 'L') {
             // Authorized
             $Access['AU'] = $taskId;
         } elseif ($read == 'N') {
             // Authorized
             $Access['G2'] = $taskId;
         }
         // Write - will override read access
         $write = CSocNetFeaturesPerms::GetOperationPerm(SONET_ENTITY_GROUP, $ownerId, "calendar", 'write');
         $taskId = CCalendar::GetAccessTasksByName('calendar_section', 'calendar_edit');
         if ($write == 'A') {
             // Group owner
             $Access['SG' . $ownerId . '_A'] = $taskId;
         } elseif ($write == 'E') {
             // Group moderator
             $Access['SG' . $ownerId . '_E'] = $taskId;
         } elseif ($write == 'K') {
             // User
             $Access['SG' . $ownerId . '_K'] = $taskId;
         } elseif ($write == 'L') {
             // Authorized
             $Access['AU'] = $taskId;
         } elseif ($write == 'N') {
             // Authorized
             $Access['G2'] = $taskId;
         }
     } else {
         $arGroupPerm = CIBlock::GetGroupPermissions($iblockId);
         $taskByLetter = array('D' => CCalendar::GetAccessTasksByName('calendar_section', 'calendar_denied'), 'R' => CCalendar::GetAccessTasksByName('calendar_section', 'calendar_view'), 'W' => CCalendar::GetAccessTasksByName('calendar_section', 'calendar_edit'), 'X' => CCalendar::GetAccessTasksByName('calendar_section', 'calendar_access'));
         foreach ($arGroupPerm as $groupId => $letter) {
             $Access['G' . $groupId] = $taskByLetter[$letter];
         }
     }
     // 1. Fetch sections
     $arUserSections = CEventCalendar::GetCalendarList(array($iblockId, $sectionId, 0, $ownerType));
     $calendarIndex = array();
     foreach ($arUserSections as $section) {
         $arUF = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFields($ent_id, $section['ID']);
         if (isset($arUF["UF_CAL_CONVERTED"]) && strlen($arUF["UF_CAL_CONVERTED"]['VALUE']) > 0) {
             continue;
         }
         $SectionAccess = array();
         if ($bSetAccessFromCalendar && $ownerType == 'user') {
             if ($section['PRIVATE_STATUS'] == 'private') {
                 $deniedTask = CCalendar::GetAccessTasksByName('calendar_section', 'calendar_denied');
                 $SectionAccess['G2'] = $deniedTask;
             } elseif ($section['PRIVATE_STATUS'] == 'time') {
                 $viewTimeTask = CCalendar::GetAccessTasksByName('calendar_section', 'calendar_view_time');
                 $SectionAccess['G2'] = $viewTimeTask;
             } elseif ($section['PRIVATE_STATUS'] == 'title') {
                 $viewTitleTask = CCalendar::GetAccessTasksByName('calendar_section', 'calendar_view_title');
                 $SectionAccess['G2'] = $viewTitleTask;
             } else {
                 $SectionAccess = $Access;
                 // nested from common access settings from socnet
             }
         } else {
             $SectionAccess = $Access;
             // G2 => denied
         }
         $new_sect_id = CCalendarSect::Edit(array('arFields' => array("CAL_TYPE" => $ownerType, "NAME" => $section['NAME'], "OWNER_ID" => $ownerId, "CREATED_BY" => $createdBy, "DESCRIPTION" => $section['DESCRIPTION'], "COLOR" => $section["COLOR"], "ACCESS" => $SectionAccess)));
         // Set converted property
         $bs->Update($section['ID'], array('UF_CAL_CONVERTED' => 1));
         $calendarIndex[$section['ID']] = $new_sect_id;
         $sectCount++;
     }
     // 2. Create events
     $arEvents = CEventCalendar::GetCalendarEventsList(array($iblockId, $sectionId, 0), array());
     foreach ($arEvents as $event) {
         if (!isset($calendarIndex[$event['IBLOCK_SECTION_ID']]) || $event['PROPERTY_PARENT'] > 0) {
             continue;
         }
         $arFields = array("CAL_TYPE" => $ownerType, "OWNER_ID" => $ownerId, "CREATED_BY" => $event["CREATED_BY"], "DT_FROM" => $event['ACTIVE_FROM'], "DT_TO" => $event['ACTIVE_TO'], 'NAME' => htmlspecialcharsback($event['NAME']), 'DESCRIPTION' => CCalendar::ParseHTMLToBB(htmlspecialcharsback($event['DETAIL_TEXT'])), 'SECTIONS' => array($calendarIndex[$event['IBLOCK_SECTION_ID']]), 'ACCESSIBILITY' => $event['PROPERTY_ACCESSIBILITY'], 'IMPORTANCE' => $event['PROPERTY_IMPORTANCE'], 'PRIVATE_EVENT' => $event['PROPERTY_PRIVATE'] && $event['PROPERTY_PRIVATE'] == 'true' ? '1' : '', 'RRULE' => array(), 'LOCATION' => array('NEW' => $event['PROPERTY_LOCATION'], 'RE_RESERVE' => 'N'), "REMIND" => array(), "IS_MEETING" => $event['PROPERTY_IS_MEETING'] == 'Y');
         if (!empty($event['PROPERTY_REMIND_SETTINGS'])) {
             $ar = explode("_", $event["PROPERTY_REMIND_SETTINGS"]);
             if (count($ar) == 2) {
                 $arFields["REMIND"][] = array('type' => $ar[1], 'count' => floatVal($ar[0]));
             }
         }
         if (isset($event["PROPERTY_PERIOD_TYPE"]) && in_array($event["PROPERTY_PERIOD_TYPE"], array("DAILY", "WEEKLY", "MONTHLY", "YEARLY"))) {
             $arFields['RRULE']['FREQ'] = $event["PROPERTY_PERIOD_TYPE"];
             $arFields['RRULE']['INTERVAL'] = $event["PROPERTY_PERIOD_COUNT"];
             if (!empty($event['PROPERTY_EVENT_LENGTH'])) {
                 $arFields['DT_LENGTH'] = intval($event['PROPERTY_EVENT_LENGTH']);
             }
             if (!$arFields['DT_LENGTH']) {
                 $arFields['DT_LENGTH'] = 86400;
             }
             if ($arFields['RRULE']['FREQ'] == "WEEKLY" && !empty($event['PROPERTY_PERIOD_ADDITIONAL'])) {
                 $arFields['RRULE']['BYDAY'] = array();
                 $bydays = explode(',', $event['PROPERTY_PERIOD_ADDITIONAL']);
                 foreach ($bydays as $day) {
                     $day = CCalendar::WeekDayByInd($day);
                     if ($day !== false) {
                         $arFields['RRULE']['BYDAY'][] = $day;
                     }
                 }
                 $arFields['RRULE']['BYDAY'] = implode(',', $arFields['RRULE']['BYDAY']);
             }
             $arFields['RRULE']['UNTIL'] = $event['ACTIVE_TO'];
         }
         if ($arFields['IS_MEETING']) {
             if ($event['PROPERTY_PARENT'] > 0) {
                 continue;
             }
             $host = intVal($event['CREATED_BY']);
             $arFields['ATTENDEES'] = array();
             if ($event['PROPERTY_HOST_IS_ABSENT'] == 'N') {
                 $arFields['ATTENDEES'][] = $host;
             }
             $arGuests = CECEvent::GetGuests(self::$userIblockId, $event['ID']);
             $attendeesStatuses = array();
             foreach ($arGuests as $userId => $ev) {
                 $attendeesStatuses[$userId] = $ev['PROPERTY_VALUES']['CONFIRMED'];
                 $arFields['ATTENDEES'][] = $userId;
             }
             $arFields['MEETING_HOST'] = $host;
             $arFields['MEETING'] = array('HOST_NAME' => CCalendar::GetUserName($host), 'TEXT' => is_array($event['PROPERTY_MEETING_TEXT']) && is_string($event['PROPERTY_MEETING_TEXT']['TEXT']) ? trim($event['PROPERTY_MEETING_TEXT']['TEXT']) : '', 'OPEN' => false, 'NOTIFY' => false, 'REINVITE' => false);
         }
         $new_event_id = CCalendar::SaveEvent(array('arFields' => $arFields, 'bAffectToDav' => false, 'attendeesStatuses' => $attendeesStatuses, 'bSendInvitations' => false));
         $eventsCount++;
     }
     // 3. Set userfield
     $bs->Update($sectionId, array('UF_CAL_CONVERTED' => 1));
     return array('eventsCount' => $eventsCount, 'sectCount' => $sectCount);
 }