EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
free busy refactoring
Yuri Kuznetsov5cce21643b28
diff --git a/application/Espo/Modules/Crm/Classes/FormulaFunctions/ExtGroup/CalendarGroup/UserIsBusyType.php b/application/Espo/Modules/Crm/Classes/FormulaFunctions/ExtGroup/CalendarGroup/UserIsBusyType.phpindex f77a1e4d5d..97de199bde 100644--- a/application/Espo/Modules/Crm/Classes/FormulaFunctions/ExtGroup/CalendarGroup/UserIsBusyType.php+++ b/application/Espo/Modules/Crm/Classes/FormulaFunctions/ExtGroup/CalendarGroup/UserIsBusyType.php@@ -34,15 +34,23 @@ use Espo\Core\Formula\EvaluatedArgumentList;use Espo\Core\Formula\Exceptions\BadArgumentType;use Espo\Core\Formula\Exceptions\TooFewArguments;use Espo\Core\Formula\Func;-use Espo\Modules\Crm\Tools\Calendar\FetchParams;+use Espo\Entities\User;+use Espo\Modules\Crm\Tools\Calendar\FreeBusy\FetchParams;+use Espo\Modules\Crm\Tools\Calendar\FreeBusy\Service;use Espo\Modules\Crm\Tools\Calendar\Items\Event;-use Espo\Modules\Crm\Tools\Calendar\Service;+use Espo\ORM\EntityManager;use Exception;use RuntimeException;+/**+ * @noinspection PhpUnused+ */class UserIsBusyType implements Func{- public function __construct(private Service $service) {}+ public function __construct(+ private Service $service,+ private EntityManager $entityManager,+ ) {}public function process(EvaluatedArgumentList $arguments): bool{@@ -76,17 +84,26 @@ class UserIsBusyType implements Functhrow BadArgumentType::create(5, 'string');}- $params = FetchParams::create(DateTime::fromString($from), DateTime::fromString($to))- ->withSkipAcl();-$ignoreList = [];if ($entityType && $id) {$ignoreList[] = (new Event(null, null, $entityType, []))->withId($id);}+ $user = $this->entityManager->getRDBRepositoryByClass(User::class)->getById($userId);++ if (!$user) {+ throw new RuntimeException("User $userId not found.");+ }++ $busyParams = new FetchParams(+ from: DateTime::fromString($from),+ to: DateTime::fromString($to),+ ignoreEventList: $ignoreList,+ );+try {- $ranges = $this->service->fetchBusyRanges($userId, $params, $ignoreList);+ $ranges = $this->service->fetchRanges($user, $busyParams);} catch (Exception $e) {throw new RuntimeException($e->getMessage());}diff --git a/application/Espo/Modules/Crm/Tools/Calendar/FetchParams.php b/application/Espo/Modules/Crm/Tools/Calendar/FetchParams.phpindex 09abe5bb38..83e090ab22 100644--- a/application/Espo/Modules/Crm/Tools/Calendar/FetchParams.php+++ b/application/Espo/Modules/Crm/Tools/Calendar/FetchParams.php@@ -53,6 +53,22 @@ class FetchParamsreturn new self($from, $to);}+ public function withFrom(DateTime $from): self+ {+ $obj = clone $this;+ $obj->from = $from;++ return $obj;+ }++ public function withTo(DateTime $to): self+ {+ $obj = clone $this;+ $obj->to = $to;++ return $obj;+ }+public function withIsAgenda(bool $isAgenda = true): self{$obj = clone $this;diff --git a/application/Espo/Modules/Crm/Tools/Calendar/FreeBusy/FetchParams.php b/application/Espo/Modules/Crm/Tools/Calendar/FreeBusy/FetchParams.phpnew file mode 100644index 0000000000..a8a55fc0fb--- /dev/null+++ b/application/Espo/Modules/Crm/Tools/Calendar/FreeBusy/FetchParams.php@@ -0,0 +1,47 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Modules\Crm\Tools\Calendar\FreeBusy;++use Espo\Core\Field\DateTime;+use Espo\Modules\Crm\Tools\Calendar\Items\Event;++readonly class FetchParams+{+ /**+ * @param bool $accessCheck To user apply access check.+ * @param Event[] $ignoreEventList Events not to be included in a result.+ */+ public function __construct(+ public DateTime $from,+ public DateTime $to,+ public bool $accessCheck = false,+ public array $ignoreEventList = [],+ ) {}+}diff --git a/application/Espo/Modules/Crm/Tools/Calendar/FreeBusy/Service.php b/application/Espo/Modules/Crm/Tools/Calendar/FreeBusy/Service.phpnew file mode 100644index 0000000000..d3018db365--- /dev/null+++ b/application/Espo/Modules/Crm/Tools/Calendar/FreeBusy/Service.php@@ -0,0 +1,61 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Modules\Crm\Tools\Calendar\FreeBusy;++use Espo\Core\Exceptions\Forbidden;+use Espo\Core\Exceptions\NotFound;+use Espo\Entities\User;+use Espo\Modules\Crm\Tools\Calendar\FetchParams as CalendarFetchParams;+use Espo\Modules\Crm\Tools\Calendar\Items\BusyRange;+use Espo\Modules\Crm\Tools\Calendar\Service as CalendarService;++/**+ * @since 9.0.0+ */+class Service+{+ public function __construct(+ private CalendarService $service,+ ) {}++ /**+ * Fetch busy-ranges for user. Access is not checked by default.+ *+ * @return BusyRange[]+ * @throws NotFound+ * @throws Forbidden+ */+ public function fetchRanges(User $user, FetchParams $params): array+ {+ $fetchParams = CalendarFetchParams::create($params->from, $params->to);++ return $this->service->fetchBusyRanges($user, $params, $fetchParams);+ }+}diff --git a/application/Espo/Modules/Crm/Tools/Calendar/Service.php b/application/Espo/Modules/Crm/Tools/Calendar/Service.phpindex 4e9a4fb8d8..09b21d19b2 100644--- a/application/Espo/Modules/Crm/Tools/Calendar/Service.php+++ b/application/Espo/Modules/Crm/Tools/Calendar/Service.php@@ -49,6 +49,7 @@ use Espo\Entities\User;use Espo\Modules\Crm\Entities\Call;use Espo\Modules\Crm\Entities\Meeting;use Espo\Modules\Crm\Entities\Task;+use Espo\Modules\Crm\Tools\Calendar\FreeBusy\FetchParams as FetchBusyParams;use Espo\Modules\Crm\Tools\Calendar\Items\BusyRange;use Espo\Modules\Crm\Tools\Calendar\Items\Event;use Espo\Modules\Crm\Tools\Calendar\Items\NonWorkingRange;@@ -93,21 +94,34 @@ class Service* @throws Forbidden*/public function fetch(string $userId, FetchParams $fetchParams): array+ {+ $user = $this->entityManager->getRDBRepositoryByClass(User::class)->getById($userId);++ if (!$user) {+ throw new NotFound();+ }++ return $this->fetchInternal($user, $fetchParams);++ }++ /**+ * Fetch events and ranges.+ *+ * @return (Event|NonWorkingRange|WorkingRange)[]+ * @throws Forbidden+ */+ private function fetchInternal(User $user, FetchParams $fetchParams, bool $skipAccessCheck = false): array{$from = $fetchParams->getFrom()->toString();$to = $fetchParams->getTo()->toString();$scopeList = $fetchParams->getScopeList();$skipAcl = $fetchParams->skipAcl();- /** @var ?User $user */- $user = $this->entityManager->getEntityById(User::ENTITY_TYPE, $userId);-- if (!$user) {- throw new NotFound();+ if (!$skipAccessCheck) {+ $this->accessCheck($user);}- $this->accessCheck($user);-$calendarEntityList = $this->config->get('calendarEntityList', []);if (is_null($scopeList)) {@@ -139,7 +153,7 @@ class Service}$subItem = [- $this->getCalendarQuery($scope, $userId, $from, $to, $skipAcl)+ $this->getCalendarQuery($scope, $user->getId(), $from, $to, $skipAcl)];$queryList = array_merge($queryList, $subItem);@@ -569,13 +583,22 @@ class Serviceforeach ($userIdList as $userId) {try {- $eventList = $this->fetch($userId, $itemFetchParams);+ $user = $this->entityManager->getRDBRepositoryByClass(User::class)->getById($userId);++ if (!$user) {+ throw new NotFound("User $userId not found.");+ }- $busyRangeList = $this->fetchBusyRanges(- $userId,- $fetchParams->withScopeList($brScopeList),- array_filter($eventList, fn (Item $item) => $item instanceof Event)+ $eventList = $this->fetchInternal($user, $itemFetchParams);++ $busyParams = new FetchBusyParams(+ from: $fetchParams->getFrom(),+ to: $fetchParams->getTo(),+ accessCheck: true,+ ignoreEventList: array_filter($eventList, fn (Item $item) => $item instanceof Event),);++ $busyRangeList = $this->fetchBusyRanges($user, $busyParams, $fetchParams->withScopeList($brScopeList));} catch (Exception $e) {if ($e instanceof Forbidden) {continue;@@ -615,7 +638,7 @@ class Service$userIdList = [];$userList = $this->entityManager- ->getRDBRepository(User::ENTITY_TYPE)+ ->getRDBRepositoryByClass(User::class)->select([Attribute::ID, 'name'])->leftJoin(Field::TEAMS)->where([@@ -759,21 +782,25 @@ class Service}/**- * @param Event[] $ignoreEventList+ * @internal Use FreeBusyService instead.+ ** @return BusyRange[]* @throws NotFound* @throws Forbidden*/- public function fetchBusyRanges(string $userId, FetchParams $fetchParams, array $ignoreEventList = []): array+ public function fetchBusyRanges(User $user, FetchBusyParams $params, FetchParams $fetchParams): array{$rangeList = [];- /** @noinspection PhpRedundantOptionalArgumentInspection */- $eventList = $this->fetch($userId, $fetchParams->withSkipAcl(true));+ $fetchParams = $fetchParams+ ->withFrom($params->from)+ ->withTo($params->to);++ $eventList = $this->fetchInternal($user, $fetchParams->withSkipAcl(), !$params->accessCheck);$ignoreHash = (object) [];- foreach ($ignoreEventList as $event) {+ foreach ($params->ignoreEventList as $event) {$id = $event->getId();if ($id) {@@ -977,9 +1004,7 @@ class Service->withScopeList($scopeList);foreach ($userIdList as $userId) {- $user = $this->entityManager- ->getRDBRepositoryByClass(User::class)- ->getById($userId);+ $user = $this->entityManager->getRDBRepositoryByClass(User::class)->getById($userId);if (!$user) {continue;@@ -997,10 +1022,16 @@ class Service) :[];+ $busyParams = new FetchBusyParams(+ from: $fetchParams->getFrom(),+ to: $fetchParams->getTo(),+ accessCheck: true,+ ignoreEventList: $ignoreList,+ );+try {$busyRangeList = $toReturn ?- $this->fetchBusyRanges($userId, $fetchParams, $ignoreList) :- [];+ $this->fetchBusyRanges($user, $busyParams, $fetchParams) : [];} catch (Exception $e) {if ($e instanceof Forbidden) {continue;