예제 #1
0
 public static function ReminderAgent($eventId = 0, $userId = 0, $viewPath = '', $calendarType = '', $ownerId = 0)
 {
     if ($eventId > 0 && $userId > 0 && $calendarType != '') {
         if (!CModule::IncludeModule("im")) {
             return false;
         }
         $skipReminding = false;
         global $USER;
         // Create tmp user
         if ($bTmpUser = !$USER || !is_object($USER)) {
             $USER = new CUser();
         }
         // We have to use this to set timezone offset to local user's timezone
         self::SetOffset(false, self::GetOffset($userId));
         $arEvents = CCalendarEvent::GetList(array('arFilter' => array("ID" => $eventId, "DELETED" => "N", "FROM_LIMIT" => CCalendar::Date(time() - 3600, false), "TO_LIMIT" => CCalendar::Date(CCalendar::GetMaxTimestamp(), false)), 'parseRecursion' => true, 'maxInstanceCount' => 2, 'preciseLimits' => true, 'fetchAttendees' => true, 'checkPermissions' => false, 'setDefaultLimit' => false));
         if ($arEvents && is_array($arEvents[0])) {
             $Event = $arEvents[0];
         }
         if ($Event && $Event['IS_MEETING'] && is_array($Event['~ATTENDEES'])) {
             foreach ($Event['~ATTENDEES'] as $attendee) {
                 // If current user is an attendee but his status is 'N' we don't take care about reminding
                 if ($attendee['USER_ID'] == $userId && $attendee['STATUS'] == 'N') {
                     $skipReminding = true;
                     break;
                 }
             }
         }
         if ($Event && $Event['DELETED'] != 'Y' && !$skipReminding) {
             // Get Calendar Info
             $Section = CCalendarSect::GetById($Event['SECT_ID'], false);
             if ($Section) {
                 $arNotifyFields = array('FROM_USER_ID' => $userId, 'TO_USER_ID' => $userId, 'NOTIFY_TYPE' => IM_NOTIFY_SYSTEM, 'NOTIFY_MODULE' => "calendar", 'NOTIFY_EVENT' => "reminder", 'NOTIFY_TAG' => "CALENDAR|INVITE|" . $eventId . "|" . $userId . "|REMINDER", 'NOTIFY_SUB_TAG' => "CALENDAR|INVITE|" . $eventId);
                 $arNotifyFields['MESSAGE'] = GetMessage('EC_EVENT_REMINDER', array('#EVENT_NAME#' => $Event["NAME"], '#DATE_FROM#' => CCalendar::CutZeroTime($Event["DT_FROM"])));
                 $sectionName = $Section['NAME'];
                 $ownerName = CCalendar::GetOwnerName($calendarType, $ownerId);
                 if ($calendarType == 'user' && $ownerId == $userId) {
                     $arNotifyFields['MESSAGE'] .= ' ' . GetMessage('EC_EVENT_REMINDER_IN_PERSONAL', array('#CALENDAR_NAME#' => $sectionName));
                 } else {
                     if ($calendarType == 'user') {
                         $arNotifyFields['MESSAGE'] .= ' ' . GetMessage('EC_EVENT_REMINDER_IN_USER', array('#CALENDAR_NAME#' => $sectionName, '#USER_NAME#' => $ownerName));
                     } else {
                         if ($calendarType == 'group') {
                             $arNotifyFields['MESSAGE'] .= ' ' . GetMessage('EC_EVENT_REMINDER_IN_GROUP', array('#CALENDAR_NAME#' => $sectionName, '#GROUP_NAME#' => $ownerName));
                         } else {
                             $arNotifyFields['MESSAGE'] .= ' ' . GetMessage('EC_EVENT_REMINDER_IN_COMMON', array('#CALENDAR_NAME#' => $sectionName, '#IBLOCK_NAME#' => $ownerName));
                         }
                     }
                 }
                 if ($viewPath != '') {
                     $arNotifyFields['MESSAGE'] .= "\n" . GetMessage('EC_EVENT_REMINDER_DETAIL', array('#URL_VIEW#' => $viewPath));
                 }
                 CIMNotify::Add($arNotifyFields);
                 foreach (GetModuleEvents("calendar", "OnRemindEvent", true) as $arEvent) {
                     ExecuteModuleEventEx($arEvent, array(array('eventId' => $eventId, 'userId' => $userId, 'viewPath' => $viewPath, 'calType' => $calendarType, 'ownerId' => $ownerId)));
                 }
                 if (CCalendarEvent::CheckRecurcion($Event) && ($nextEvent = $arEvents[1])) {
                     $remAgentParams = array('eventId' => $eventId, 'userId' => $userId, 'viewPath' => $viewPath, 'calendarType' => $calendarType, 'ownerId' => $ownerId);
                     // 1. clean reminders
                     CCalendar::RemoveAgent($remAgentParams);
                     // 2. Set new reminders
                     $startTs = $nextEvent['DT_FROM_TS'];
                     $reminder = $nextEvent['REMIND'][0];
                     if ($reminder) {
                         $delta = intVal($reminder['count']) * 60;
                         //Minute
                         if ($reminder['type'] == 'hour') {
                             $delta = $delta * 60;
                         } elseif ($reminder['type'] == 'day') {
                             $delta = $delta * 60 * 24;
                         }
                         //Day
                         if ($startTs - $delta >= time() - 60 * 10) {
                             // Inaccuracy - 10 min
                             CCalendar::AddAgent(CCalendar::Date($startTs - $delta), $remAgentParams);
                         }
                     }
                 }
             }
         }
         self::$offset = null;
         if (isset($bTmpUser) && $bTmpUser) {
             unset($USER);
         }
     }
 }