示例#1
0
 public static function GetList($Params = array())
 {
     global $DB, $USER_FIELD_MANAGER;
     $getUF = $Params['getUserfields'] !== false;
     $checkPermissions = $Params['checkPermissions'] !== false;
     $bCache = CCalendar::CacheTime() > 0;
     $bCache = false;
     $Params['setDefaultLimit'] = $Params['setDefaultLimit'] === true;
     $userId = isset($Params['userId']) ? intVal($Params['userId']) : CCalendar::GetCurUserId();
     CTimeZone::Disable();
     if ($bCache) {
         $cache = new CPHPCache();
         if ($checkPermissions) {
             $cacheId = 'event_list_' . md5(serialize(array($Params, CCalendar::GetCurUserId())));
         } else {
             $cacheId = 'event_list_' . md5(serialize(array($Params)));
         }
         $cacheId .= CCalendar::GetOffset();
         $cachePath = CCalendar::CachePath() . 'event_list';
         if ($cache->InitCache(CCalendar::CacheTime(), $cacheId, $cachePath)) {
             $res = $cache->GetVars();
             $arResult = $res["arResult"];
             $arAttendees = $res["arAttendees"];
         }
     }
     if (!$bCache || !isset($arResult)) {
         $arFilter = $Params['arFilter'];
         if ($getUF) {
             $obUserFieldsSql = new CUserTypeSQL();
             $obUserFieldsSql->SetEntity("CALENDAR_EVENT", "CE.ID");
             $obUserFieldsSql->SetSelect(array("UF_*"));
             $obUserFieldsSql->SetFilter($arFilter);
         }
         $fetchMeetings = $Params['fetchMeetings'];
         $Params['fetchAttendees'] = $Params['fetchAttendees'] !== false;
         $skipDeclined = $Params['skipDeclined'] === true;
         if ($Params['setDefaultLimit'] !== false) {
             if (!isset($arFilter["FROM_LIMIT"])) {
                 // default 3 month back
                 $arFilter["FROM_LIMIT"] = CCalendar::Date(time() - 31 * 3 * 24 * 3600, false);
             }
             if (!isset($arFilter["TO_LIMIT"])) {
                 // default one year into the future
                 $arFilter["TO_LIMIT"] = CCalendar::Date(time() + 365 * 24 * 3600, false);
             }
         }
         $arOrder = isset($Params['arOrder']) ? $Params['arOrder'] : array('SORT' => 'asc');
         $arFields = self::GetFields();
         if (!isset($arFilter["DELETED"])) {
             $arFilter["DELETED"] = "N";
         }
         $ownerId = isset($arFilter['OWNER_ID']) ? $arFilter['OWNER_ID'] : CCalendar::GetOwnerId();
         if ($fetchMeetings) {
             // We fetch all events for user where it was attented
             $strInvIds = "";
             $arUserMeeting = CCalendarEvent::GetAttendeesList(array('userKey' => $ownerId), $strInvIds);
         }
         $arSqlSearch = array();
         if (is_array($arFilter)) {
             $filter_keys = array_keys($arFilter);
             for ($i = 0, $l = count($filter_keys); $i < $l; $i++) {
                 $n = strtoupper($filter_keys[$i]);
                 $val = $arFilter[$filter_keys[$i]];
                 if (is_string($val) && strlen($val) <= 0 || strval($val) == "NOT_REF") {
                     continue;
                 }
                 if ($n == 'FROM_LIMIT') {
                     if (strtoupper($DB->type) == "MYSQL") {
                         $arSqlSearch[] = "CE.DT_TO>=FROM_UNIXTIME('" . MkDateTime(FmtDate($val, "D.M.Y"), "d.m.Y") . "')";
                     } elseif (strtoupper($DB->type) == "MSSQL") {
                         $arSqlSearch[] = "CE.DT_TO>=" . $DB->CharToDateFunction($val, "SHORT");
                     } elseif (strtoupper($DB->type) == "ORACLE") {
                         $arSqlSearch[] = "CE.DT_TO>=TO_DATE('" . FmtDate($val, "D.M.Y") . " 00:00:00','dd.mm.yyyy hh24:mi:ss')";
                     }
                 } elseif ($n == 'TO_LIMIT') {
                     if (strtoupper($DB->type) == "MYSQL") {
                         $arSqlSearch[] = "CE.DT_FROM<=FROM_UNIXTIME('" . MkDateTime(FmtDate($val, "D.M.Y") . " 23:59:59", "d.m.Y H:i:s") . "')";
                     } elseif (strtoupper($DB->type) == "MSSQL") {
                         $arSqlSearch[] = "CE.DT_FROM<=dateadd(day, 1, " . $DB->CharToDateFunction($val, "SHORT") . ")";
                     } elseif (strtoupper($DB->type) == "ORACLE") {
                         $arSqlSearch[] = "CE.DT_FROM<=TO_DATE('" . FmtDate($val, "D.M.Y") . " 23:59:59','dd.mm.yyyy hh24:mi:ss')";
                     }
                 } elseif ($n == 'TIMESTAMP_X') {
                 } elseif ($n == 'OWNER_ID' && intVal($val) > 0) {
                     $q = "CE.OWNER_ID=" . intVal($val);
                     if ($fetchMeetings && $strInvIds != "") {
                         if ($q != "") {
                             $q .= ' OR ';
                         }
                         $q .= 'CE.ID in (' . $strInvIds . ')';
                     }
                     $arSqlSearch[] = $q;
                 } elseif ($n == 'SECTION') {
                     if (!is_array($val)) {
                         $val = array($val);
                     }
                     $q = "";
                     if (is_array($val)) {
                         $sval = '';
                         foreach ($val as $sectid) {
                             if (intVal($sectid) > 0) {
                                 $sval .= intVal($sectid) . ',';
                             }
                         }
                         $sval = trim($sval, ' ,');
                         if ($sval != '') {
                             $q = 'CES.SECT_ID in (' . $sval . ')';
                         }
                     }
                     if ($fetchMeetings && $strInvIds != "") {
                         if ($q != "") {
                             $q .= ' OR ';
                         }
                         $q .= 'CE.ID in (' . $strInvIds . ')';
                     }
                     if ($q != "") {
                         $arSqlSearch[] = $q;
                     }
                 } elseif ($n == 'DAV_XML_ID' && is_array($val)) {
                     array_walk($val, array($DB, 'ForSql'));
                     $arSqlSearch[] = 'CE.DAV_XML_ID IN (\'' . implode('\',\'', $val) . '\')';
                 } elseif (isset($arFields[$n])) {
                     $arSqlSearch[] = GetFilterQuery($arFields[$n]["FIELD_NAME"], $val, 'N');
                 }
             }
         }
         if ($getUF) {
             $r = $obUserFieldsSql->GetFilter();
             if (strlen($r) > 0) {
                 $arSqlSearch[] = "(" . $r . ")";
             }
         }
         $strSqlSearch = GetFilterSqlSearch($arSqlSearch);
         $strOrderBy = '';
         foreach ($arOrder as $by => $order) {
             if (isset($arFields[strtoupper($by)])) {
                 $strOrderBy .= $arFields[strtoupper($by)]["FIELD_NAME"] . ' ' . (strtolower($order) == 'desc' ? 'desc' . (strtoupper($DB->type) == "ORACLE" ? " NULLS LAST" : "") : 'asc' . (strtoupper($DB->type) == "ORACLE" ? " NULLS FIRST" : "")) . ',';
             }
         }
         if (strlen($strOrderBy) > 0) {
             $strOrderBy = "ORDER BY " . rtrim($strOrderBy, ",");
         }
         $selectList = "";
         foreach ($arFields as $field) {
             $selectList .= $field['FIELD_NAME'] . ", ";
         }
         $strSql = "\n\t\t\t\tSELECT " . $selectList . "CES.SECT_ID, CES.REL\n\t\t\t\t\t" . ($getUF ? $obUserFieldsSql->GetSelect() : '') . "\n\t\t\t\tFROM\n\t\t\t\t\tb_calendar_event CE\n\t\t\t\tINNER JOIN b_calendar_event_sect CES ON (CE.ID=CES.EVENT_ID)\n\t\t\t\t" . ($getUF ? $obUserFieldsSql->GetJoin("CE.ID") : '') . "\n\t\t\t\tWHERE\n\t\t\t\t\t{$strSqlSearch}\n\t\t\t\t{$strOrderBy}";
         $res = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
         if ($getUF) {
             $res->SetUserFields($USER_FIELD_MANAGER->GetUserFields("CALENDAR_EVENT"));
         }
         $arResult = array();
         $arMeetingIds = array();
         $arEvents = array();
         $bIntranet = CCalendar::IsIntranetEnabled();
         while ($event = $res->Fetch()) {
             $event['IS_MEETING'] = intVal($event['IS_MEETING']) > 0;
             $arEvents[] = $event;
             if ($bIntranet && $event['IS_MEETING']) {
                 $arMeetingIds[] = $event['ID'];
             }
         }
         if ($Params['fetchAttendees'] && count($arMeetingIds) > 0) {
             $arAttendees = self::GetAttendees($arMeetingIds);
         } else {
             $arAttendees = array();
         }
         foreach ($arEvents as $event) {
             $event["ACCESSIBILITY"] = trim($event["ACCESSIBILITY"]);
             if ($bIntranet && $event['IS_MEETING']) {
                 if (isset($event['MEETING']) && $event['MEETING'] != "") {
                     $event['MEETING'] = unserialize($event['MEETING']);
                     if (!is_array($event['MEETING'])) {
                         $event['MEETING'] = array();
                     }
                 }
                 if ($arUserMeeting[$event['ID']]) {
                     $status = $arUserMeeting[$event['ID']]['STATUS'];
                     if ($skipDeclined && $status == "N") {
                         continue;
                     }
                     if ($status == "Y" || $userId == $ownerId) {
                         $event['USER_MEETING'] = array('ATTENDEE_ID' => $ownerId, 'ACCESSIBILITY' => $arUserMeeting[$event['ID']]['ACCESSIBILITY'], 'COLOR' => $arUserMeeting[$event['ID']]['COLOR'], 'TEXT_COLOR' => $arUserMeeting[$event['ID']]['TEXT_COLOR'], 'DESCRIPTION' => $arUserMeeting[$event['ID']]['DESCRIPTION'], 'STATUS' => $status, 'REMIND' => array());
                         if (isset($arUserMeeting[$event['ID']]['REMIND']) && $arUserMeeting[$event['ID']]['REMIND'] != "") {
                             $event['USER_MEETING']['REMIND'] = unserialize($arUserMeeting[$event['ID']]['REMIND']);
                             if (!is_array($event['USER_MEETING']['REMIND'])) {
                                 $event['USER_MEETING']['REMIND'] = array();
                             }
                         }
                     } else {
                         if (is_array($arFilter['SECTION']) && !in_array($event['SECT_ID'], $arFilter['SECTION'])) {
                             continue;
                         }
                     }
                 }
             }
             if (isset($event['REMIND']) && $event['REMIND'] != "") {
                 $event['REMIND'] = unserialize($event['REMIND']);
                 if (!is_array($event['REMIND'])) {
                     $event['REMIND'] = array();
                 }
             }
             if ($bIntranet && $event['IS_MEETING'] && isset($arAttendees[$event['ID']]) && count($arAttendees[$event['ID']]) > 0) {
                 $event['~ATTENDEES'] = $arAttendees[$event['ID']];
             }
             $checkPermissionsForEvent = $userId != $event['CREATED_BY'];
             // It's creator
             // It's event in user's calendar
             if ($checkPermissionsForEvent && $event['CAL_TYPE'] == 'user' && $userId == $event['OWNER_ID']) {
                 $checkPermissionsForEvent = false;
             }
             if ($checkPermissionsForEvent && $event['IS_MEETING'] && $event['USER_MEETING'] && $event['USER_MEETING']['ATTENDEE_ID'] == $userId) {
                 $checkPermissionsForEvent = false;
             }
             if ($checkPermissionsForEvent && $event['IS_MEETING'] && is_array($event['~ATTENDEES'])) {
                 foreach ($event['~ATTENDEES'] as $att) {
                     if ($att['USER_ID'] == $userId) {
                         $checkPermissionsForEvent = false;
                         break;
                     }
                 }
             }
             if ($checkPermissions && $checkPermissionsForEvent) {
                 $event = self::ApplyAccessRestrictions($event, $userId);
             }
             if ($event === false) {
                 continue;
             }
             $event = self::PreHandleEvent($event);
             if ($Params['parseRecursion'] && self::CheckRecurcion($event)) {
                 self::ParseRecursion($arResult, $event, array('fromLimit' => $arFilter["FROM_LIMIT"], 'toLimit' => $arFilter["TO_LIMIT"], 'instanceCount' => isset($Params['maxInstanceCount']) ? $Params['maxInstanceCount'] : false, 'preciseLimits' => isset($Params['preciseLimits']) ? $Params['preciseLimits'] : false));
             } else {
                 self::HandleEvent($arResult, $event);
             }
         }
         if ($bCache) {
             $cache->StartDataCache(CCalendar::CacheTime(), $cacheId, $cachePath);
             $cache->EndDataCache(array("arResult" => $arResult, "arAttendees" => $arAttendees));
         }
     }
     CTimeZone::Enable();
     //self::$lastAttendeesList = $arAttendees;
     if (!is_array(self::$lastAttendeesList)) {
         self::$lastAttendeesList = $arAttendees;
     } elseif (is_array($arAttendees)) {
         foreach ($arAttendees as $eventId => $att) {
             self::$lastAttendeesList[$eventId] = $att;
         }
     }
     return $arResult;
 }