protected static function SaveCalendarEvent(&$arFields) { $responsibleID = isset($arFields['RESPONSIBLE_ID']) ? intval($arFields['RESPONSIBLE_ID']) : 0; $typeID = isset($arFields['TYPE_ID']) ? intval($arFields['TYPE_ID']) : CCrmActivityType::Undefined; if (!($responsibleID > 0 && ($typeID === CCrmActivityType::Call || $typeID === CCrmActivityType::Meeting))) { return false; } if (!(IsModuleInstalled('calendar') && CModule::IncludeModule('calendar'))) { return false; } $arCalEventFields = array('CAL_TYPE' => 'user', 'OWNER_ID' => $responsibleID, 'NAME' => isset($arFields['SUBJECT']) ? $arFields['SUBJECT'] : '', 'DT_FROM' => isset($arFields['START_TIME']) ? $arFields['START_TIME'] : '', 'DT_TO' => isset($arFields['END_TIME']) ? $arFields['END_TIME'] : '', 'IMPORTANCE' => CCrmActivityPriority::ToCalendarEventImportance(isset($arFields['PRIORITY']) ? intval($arFields['PRIORITY']) : CCrmActivityPriority::Low), 'DESCRIPTION' => isset($arFields['DESCRIPTION']) ? $arFields['DESCRIPTION'] : ''); $associatedEntityID = isset($arFields['ASSOCIATED_ENTITY_ID']) ? intval($arFields['ASSOCIATED_ENTITY_ID']) : 0; if ($associatedEntityID > 0) { $arCalEventFields['ID'] = $associatedEntityID; $arPresentEventFields = CCalendarEvent::GetById($associatedEntityID, false); if (is_array($arPresentEventFields)) { if (isset($arPresentEventFields['RRULE']) && $arPresentEventFields['RRULE'] != '') { $arCalEventFields['RRULE'] = CCalendarEvent::ParseRRULE($arPresentEventFields['RRULE']); } if (isset($arPresentEventFields['DT_LENGTH'])) { $arCalEventFields['DT_LENGTH'] = $arPresentEventFields['DT_LENGTH']; } } } if (isset($arFields['NOTIFY_TYPE']) && $arFields['NOTIFY_TYPE'] != CCrmActivityNotifyType::None) { $arCalEventFields['REMIND'] = array(array('type' => CCrmActivityNotifyType::ToCalendarEventRemind($arFields['NOTIFY_TYPE']), 'count' => isset($arFields['NOTIFY_VALUE']) ? intval($arFields['NOTIFY_VALUE']) : 15)); } self::$IGNORE_CALENDAR_EVENTS = true; // We must initialize CCalendar! $calendar = new CCalendar(); $calendar->Init(array('type' => 'user', 'userId' => $responsibleID, 'ownerId' => $responsibleID)); $result = $calendar->SaveEvent(array('arFields' => $arCalEventFields, 'userId' => $responsibleID, 'autoDetectSection' => true, 'autoCreateSection' => true)); $eventID = intval($result); $ownerID = intval($arFields['OWNER_ID']); $ownerTypeID = intval($arFields['OWNER_TYPE_ID']); $arBindings = isset($arFields['BINDINGS']) ? $arFields['BINDINGS'] : array(); if (empty($arBindings) && $ownerID > 0 && $ownerTypeID > 0) { $arBindings[] = array('OWNER_TYPE_ID' => $ownerTypeID, 'OWNER_ID' => $ownerID); } if ($eventID > 0 && !empty($arBindings)) { $arUserFields = array(); foreach ($arBindings as &$arBinding) { $arUserFields[] = CUserTypeCrm::GetShortEntityType(CCrmOwnerType::ResolveName($arBinding['OWNER_TYPE_ID'])) . '_' . $arBinding['OWNER_ID']; } unset($arBinding); CCalendarEvent::UpdateUserFields($eventID, array('UF_CRM_CAL_EVENT' => $arUserFields)); } self::$IGNORE_CALENDAR_EVENTS = false; return $result; }