public function loadEntriesAction()
{
$request = $this->getRequest();
$monthFrom = (int)$request->getPost('month_from');
$yearFrom = (int)$request->getPost('year_from');
$monthTo = (int)$request->getPost('month_to');
$yearTo = (int)$request->getPost('year_to');
$ownerId = (int)$request->getPost('ownerId');
$calendarType = $request->getPost('type');
$direction = $request->getPost('direction');
if (!in_array($direction, [self::DIRECTION_PREVIOUS, self::DIRECTION_NEXT, self::DIRECTION_BOTH], true))
{
$direction = null;
}
$activeSectionIds = is_array($request->getPost('active_sect'))
? $request->getPost('active_sect')
: [];
$additionalSectionIds = is_array($request->getPost('sup_sect'))
? $request->getPost('sup_sect')
: [];
$sections = [];
$limits = $this->getLimitDates($yearFrom, $monthFrom, $yearTo, $monthTo);
$connections = false;
$fetchTasks = false;
$sectionIdList = [];
foreach(array_unique(array_merge($activeSectionIds, $additionalSectionIds)) as $sectId)
{
if ($sectId === 'tasks')
{
$fetchTasks = true;
}
elseif ((int)$sectId > 0)
{
$sectionIdList[] = (int)$sectId;
}
}
if (!empty($sectionIdList))
{
$sect = CCalendarSect::GetList([
'arFilter' => [
'ID'=> $sectionIdList,
'ACTIVE' => 'Y'
],
'checkPermissions' => true
]);
foreach($sect as $section)
{
$sections[] = (int)$section['ID'];
}
}
$isBoundaryOfPastReached = false;
$isBoundaryOfFutureReached = false;
$entries = [];
if (!empty($sections))
{
$entries = $this->getEntries($sections, $limits);
if (
$direction === self::DIRECTION_BOTH
&& count($this->getShownEntries($entries)) < 5
)
{
$isBoundaryOfPastReached = true;
$isBoundaryOfFutureReached = true;
//Load all events
$limits = [
'from' => false,
'to' => false,
];
$entries = $this->getEntries($sections, $limits);
if (!empty($entries))
{
$earliestEvent = $this->getEarliestEvent($entries);
$timestamp = strtotime($earliestEvent['DATE_FROM']);
if($timestamp < strtotime("01.$monthFrom.$yearFrom"))
{
$yearFrom = (int)date('Y', $timestamp);
$monthFrom = (int)date('m', $timestamp);
}
$latestEvent = $this->getLatestEvent($entries);
$timestamp = strtotime($latestEvent['DATE_FROM']);
if($timestamp > strtotime("01.$monthTo.$yearTo"))
{
$yearTo = (int)date('Y', $timestamp);
$monthTo = (int)date('m', $timestamp);
[$yearTo, $monthTo] = $this->getValidYearAndMonth($yearTo, $monthTo + 1);
}
}
}
if (
($direction === self::DIRECTION_PREVIOUS)
&& !$this->hasArrayEntriesInMonth($entries, $yearFrom, $monthFrom)
)
{
//Load one month further
[$yearFrom, $monthFrom] = $this->getValidYearAndMonth($yearFrom, $monthFrom - 1);
$entries = $this->getEntries($sections, $this->getLimitDates($yearFrom, $monthFrom, $yearTo, $monthTo));
if (!$this->hasArrayEntriesInMonth($entries, $yearFrom, $monthFrom))
{
//Load half year further
[$yearFrom, $monthFrom] = $this->getValidYearAndMonth($yearFrom, $monthFrom - 5);
$limits = $this->getLimitDates($yearFrom, $monthFrom, $yearTo, $monthTo);
$entries = $this->getEntries($sections, $limits);
if (!$this->hasArrayEntriesInRange($entries, $yearFrom, $monthFrom, (int)$request->getPost('year_from'), (int)$request->getPost('month_from')))
{
$isBoundaryOfPastReached = true;
//Load all events
$limits['from'] = false;
$entries = $this->getEntries($sections, $limits);
if (!empty($entries))
{
$earliestEvent = $this->getEarliestEvent($entries);
$timestamp = strtotime($earliestEvent['DATE_FROM']);
$yearFrom = (int)date('Y', $timestamp);
$monthFrom = (int)date('m', $timestamp);
}
}
}
}
if (
($direction === self::DIRECTION_NEXT)
&& !$this->hasArrayEntriesInMonth($entries, $yearTo, $monthTo - 1)
)
{
//Load one month further
[$yearTo, $monthTo] = $this->getValidYearAndMonth($yearTo, $monthTo + 1);
$entries = $this->getEntries($sections, $this->getLimitDates($yearFrom, $monthFrom, $yearTo, $monthTo));
if (!$this->hasArrayEntriesInMonth($entries, $yearTo, $monthTo - 1))
{
//Load half year further
[$yearTo, $monthTo] = $this->getValidYearAndMonth($yearTo, $monthTo + 5);
$limits = $this->getLimitDates($yearFrom, $monthFrom, $yearTo, $monthTo);
$entries = $this->getEntries($sections, $limits);
if (!$this->hasArrayEntriesInRange($entries, (int)$request->getPost('year_to'), (int)$request->getPost('month_to') - 1, $yearTo, $monthTo - 1))
{
$isBoundaryOfFutureReached = true;
//Load all events
$limits['to'] = false;
$entries = $this->getEntries($sections, $limits);
if (!empty($entries))
{
$latestEvent = $this->getLatestEvent($entries);
$timestamp = strtotime($latestEvent['DATE_FROM']);
$yearTo = (int)date('Y', $timestamp);
$monthTo = (int)date('m', $timestamp);
[$yearTo, $monthTo] = $this->getValidYearAndMonth($yearTo, $monthTo + 1);
}
}
}
}
}
// **** GET TASKS ****
if ($fetchTasks)
{
$tasksEntries = CCalendar::getTaskList(
[
'type' => $calendarType,
'ownerId' => $ownerId,
]
);
if (!empty($tasksEntries))
{
$entries = array_merge($entries, $tasksEntries);
}
}
$response = [
'entries' => $entries,
'userIndex' => CCalendarEvent::getUserIndex(),
'isBoundaryOfPastReached' => $isBoundaryOfPastReached,
'isBoundaryOfFutureReached' => $isBoundaryOfFutureReached,
];
if (is_array($connections))
{
$response['connections'] = $connections;
}
if (
(int)$request->getPost('month_from') !== $monthFrom
|| (int)$request->getPost('year_from') !== $yearFrom
)
{
$response['newYearFrom'] = $yearFrom;
$response['newMonthFrom'] = $monthFrom;
}
if (
(int)$request->getPost('month_to') !== $monthTo
|| (int)$request->getPost('year_to') !== $yearTo
)
{
$response['newYearTo'] = $yearTo;
$response['newMonthTo'] = $monthTo;
}
return $response;
}