Initial commit

This commit is contained in:
root
2026-01-19 17:44:46 +01:00
commit 823af8b11d
8721 changed files with 1130846 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Activities\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Record\SearchParamsFetcher;
use Espo\Modules\Crm\Tools\Activities\FetchParams as ActivitiesFetchParams;
use Espo\Modules\Crm\Tools\Activities\Service as Service;
/**
* Activities related to a record.
*/
class Get implements Action
{
public function __construct(
private SearchParamsFetcher $searchParamsFetcher,
private Service $service,
private Acl $acl
) {}
public function process(Request $request): Response
{
if (!$this->acl->check('Activities')) {
throw new Forbidden();
}
$parentType = $request->getRouteParam('parentType');
$id = $request->getRouteParam('id');
$type = $request->getRouteParam('type');
if (
!$parentType ||
!$id ||
!in_array($type, ['activities', 'history'])
) {
throw new BadRequest();
}
$searchParams = $this->searchParamsFetcher->fetch($request);
$offset = $searchParams->getOffset();
$maxSize = $searchParams->getMaxSize();
$targetEntityType = $request->getQueryParam('entityType');
$fetchParams = new ActivitiesFetchParams($maxSize, $offset, $targetEntityType);
$result = $type === 'history' ?
$this->service->getHistory($parentType, $id, $fetchParams) :
$this->service->getActivities($parentType, $id, $fetchParams);
return ResponseComposer::json($result->toApiOutput());
}
}

View File

@@ -0,0 +1,91 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Activities\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Modules\Crm\Tools\Activities\ComposeEmailService;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
/**
* @noinspection PhpUnused
*/
class GetComposeAddressList implements Action
{
public function __construct(
private Acl $acl,
private EntityManager $entityManager,
private ComposeEmailService $service
) {}
public function process(Request $request): Response
{
$parentType = $request->getRouteParam('parentType');
$id = $request->getRouteParam('id');
if (!$parentType || !$id) {
throw new BadRequest();
}
$entity = $this->fetchEntity($parentType, $id);
$list = $this->service->getEmailAddressList($entity);
return ResponseComposer::json(
array_map(fn ($item) => $item->getValueMap(), $list)
);
}
/**
* @throws Forbidden
* @throws NotFound
*/
private function fetchEntity(string $parentType, string $id): Entity
{
$entity = $this->entityManager->getEntityById($parentType, $id);
if (!$entity) {
throw new NotFound();
}
if (!$this->acl->checkEntityRead($entity)) {
throw new Forbidden();
}
return $entity;
}
}

View File

@@ -0,0 +1,86 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Activities\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Record\SearchParamsFetcher;
use Espo\Modules\Crm\Tools\Activities\Service as Service;
/**
* Activities of specific entity type related to a record.
*/
class GetListTyped implements Action
{
public function __construct(
private SearchParamsFetcher $searchParamsFetcher,
private Service $service
) {}
public function process(Request $request): Response
{
$parentType = $request->getRouteParam('parentType');
$id = $request->getRouteParam('id');
$type = $request->getRouteParam('type');
$targetType = $request->getRouteParam('targetType');
if (
!$parentType ||
!$id ||
!$type ||
!$targetType
) {
throw new BadRequest();
}
if ($type === 'activities') {
$isHistory = false;
} else if ($type === 'history') {
$isHistory = true;
} else {
throw new BadRequest("Bad type.");
}
$searchParams = $this->searchParamsFetcher->fetch($request);
$result = $this->service->findActivitiesEntityType(
$parentType,
$id,
$targetType,
$isHistory,
$searchParams
);
return ResponseComposer::json($result->toApiOutput());
}
}

View File

@@ -0,0 +1,106 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Activities\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Record\SearchParamsFetcher;
use Espo\Entities\User;
use Espo\Modules\Crm\Tools\Activities\Upcoming\Params;
use Espo\Modules\Crm\Tools\Activities\UpcomingService;
/**
* Upcoming activities.
*
* @noinspection PhpUnused
*/
class GetUpcoming implements Action
{
public function __construct(
private User $user,
private SearchParamsFetcher $searchParamsFetcher,
private UpcomingService $service
) {}
public function process(Request $request): Response
{
$userId = $request->getQueryParam('userId') ?? $this->user->getId();
$params = $this->fetchParams($request);
$result = $this->service->get($userId, $params);
return ResponseComposer::json($result->toApiOutput());
}
/**
* @throws BadRequest
* @throws Forbidden
*/
private function fetchParams(Request $request): Params
{
$entityTypeList = $this->fetchEntityTypeList($request);
$futureDays = $request->hasQueryParam('futureDays') ? intval($request->getQueryParam('futureDays')) : null;
$searchParams = $this->searchParamsFetcher->fetch($request);
return new Params(
offset: $searchParams->getOffset(),
maxSize: $searchParams->getMaxSize(),
futureDays: $futureDays,
entityTypeList: $entityTypeList,
includeShared: $request->getQueryParam('includeShared') === 'true',
);
}
/**
* @return ?string[]
* @throws BadRequest
*/
private function fetchEntityTypeList(Request $request): ?array
{
$entityTypeList = $request->getQueryParams()['entityTypeList'] ?? null;
if (!is_array($entityTypeList) && $entityTypeList !== null) {
throw new BadRequest("Bad entityTypeList.");
}
foreach ($entityTypeList ?? [] as $it) {
if (!is_string($it)) {
throw new BadRequest("Bad item in entityTypeList.");
}
}
return $entityTypeList;
}
}

View File

@@ -0,0 +1,174 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Activities;
use Espo\Core\Acl;
use Espo\Core\Field\EmailAddress;
use Espo\Core\Templates\Entities\Company;
use Espo\Core\Templates\Entities\Person;
use Espo\Core\Utils\Metadata;
use Espo\Modules\Crm\Entities\Account;
use Espo\Modules\Crm\Entities\Contact;
use Espo\Modules\Crm\Entities\Lead;
use Espo\ORM\Defs\RelationDefs;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Type\RelationType;
use Espo\Tools\Email\EmailAddressEntityPair;
class ComposeEmailService
{
public function __construct(
private EntityManager $entityManager,
private Metadata $metadata,
private Acl $acl
) {}
/**
* @return EmailAddressEntityPair[]
*/
public function getEmailAddressList(Entity $entity): array
{
$relations = $this->getRelations($entity);
foreach ($relations as $relation) {
$address = $this->getPersonEmailAddress($entity, $relation->getName());
if ($address) {
return [$address];
}
}
return [];
}
private function getPersonEmailAddress(Entity $entity, string $link): ?EmailAddressEntityPair
{
$foreignEntity = $this->entityManager
->getRDBRepository($entity->getEntityType())
->getRelation($entity, $link)
->findOne();
if (!$foreignEntity) {
return null;
}
if (!$this->acl->checkEntityRead($foreignEntity)) {
return null;
}
if (!$this->acl->checkField($foreignEntity->getEntityType(), 'emailAddress')) {
return null;
}
/** @var ?string $address */
$address = $foreignEntity->get('emailAddress');
if (!$address) {
return null;
}
$emailAddress = EmailAddress::create($address);
return new EmailAddressEntityPair($emailAddress, $foreignEntity);
}
/**
* @return RelationDefs[]
*/
private function getRelations(Entity $entity): array
{
$relations = $this->entityManager
->getDefs()
->getEntity($entity->getEntityType())
->getRelationList();
$targetRelations = [];
foreach ($relations as $relation) {
if (
$relation->getType() !== RelationType::BELONGS_TO &&
$relation->getType() !== RelationType::HAS_ONE
) {
continue;
}
$foreignEntityType = $relation->getForeignEntityType();
if (
$foreignEntityType !== Account::ENTITY_TYPE &&
$foreignEntityType !== Contact::ENTITY_TYPE &&
$foreignEntityType !== Lead::ENTITY_TYPE &&
$this->metadata->get("scopes.$foreignEntityType.type") !== Person::TEMPLATE_TYPE &&
$this->metadata->get("scopes.$foreignEntityType.type") !== Company::TEMPLATE_TYPE
) {
continue;
}
$targetRelations[] = $relation;
}
return $this->sortRelations($targetRelations);
}
/**
* @param RelationDefs[] $targetRelations
* @return RelationDefs[]
*/
private function sortRelations(array $targetRelations): array
{
usort($targetRelations, function (RelationDefs $a, RelationDefs $b) {
$entityTypeList = [
Account::ENTITY_TYPE,
Contact::ENTITY_TYPE,
Lead::ENTITY_TYPE,
];
$index1 = array_search($a->getForeignEntityType(), $entityTypeList);
$index2 = array_search($b->getForeignEntityType(), $entityTypeList);
if ($index1 !== false && $index2 === false) {
return -1;
}
if ($index1 === false && $index2 !== false) {
return 1;
}
if ($index1 !== false && $index2 !== false && $index1 !== $index2) {
return $index1 - $index2;
}
return 0;
});
return $targetRelations;
}
}

View File

@@ -0,0 +1,62 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Activities;
class FetchParams
{
private ?int $maxSize;
private ?int $offset;
private ?string $entityType;
public function __construct(
?int $maxSize,
?int $offset,
?string $entityType
) {
$this->maxSize = $maxSize;
$this->offset = $offset;
$this->entityType = $entityType;
}
public function getMaxSize(): ?int
{
return $this->maxSize;
}
public function getOffset(): ?int
{
return $this->offset;
}
public function getEntityType(): ?string
{
return $this->entityType;
}
}

View File

@@ -0,0 +1,162 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Activities;
use DateInterval;
use DateTime;
use Espo\Core\Name\Field;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Espo\Entities\User;
use Espo\Modules\Crm\Entities\Meeting;
use Espo\Modules\Crm\Entities\Reminder;
use Espo\Modules\Crm\Entities\Task;
use Espo\ORM\EntityManager;
use Espo\ORM\Name\Attribute;
use Espo\Tools\PopupNotification\Item;
use Espo\Tools\PopupNotification\Provider;
use Exception;
class PopupNotificationsProvider implements Provider
{
private const REMINDER_PAST_HOURS = 24;
private Config $config;
private EntityManager $entityManager;
public function __construct(
Config $config,
EntityManager $entityManager
) {
$this->config = $config;
$this->entityManager = $entityManager;
}
/**
* @return Item[]
* @throws Exception
*/
public function get(User $user): array
{
$userId = $user->getId();
$dt = new DateTime();
$pastHours = $this->config->get('reminderPastHours', self::REMINDER_PAST_HOURS);
$now = $dt->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);
$nowShifted = $dt
->sub(new DateInterval('PT' . $pastHours . 'H'))
->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);
/** @var iterable<Reminder> $reminderCollection */
$reminderCollection = $this->entityManager
->getRDBRepositoryByClass(Reminder::class)
->select([
'id',
'entityType',
'entityId',
])
->where([
'type' => Reminder::TYPE_POPUP,
'userId' => $userId,
'remindAt<=' => $now,
'startAt>' => $nowShifted,
])
->find();
$resultList = [];
foreach ($reminderCollection as $reminder) {
$reminderId = $reminder->getId();
$entityType = $reminder->getTargetEntityType();
$entityId = $reminder->getTargetEntityId();
if (!$entityId || !$entityType) {
continue;
}
$entity = $this->entityManager->getEntityById($entityType, $entityId);
if (!$entity) {
continue;
}
$data = null;
if (
$entity instanceof CoreEntity &&
$entity->hasLinkMultipleField('users') &&
$entity->hasAttribute('usersColumns')
) {
$status = $entity->getLinkMultipleColumn('users', 'status', $userId);
if ($status === Meeting::ATTENDEE_STATUS_DECLINED) {
$this->removeReminder($reminderId);
continue;
}
}
$dateField = $entityType === Task::ENTITY_TYPE ?
'dateEnd' :
'dateStart';
$data = (object) [
'id' => $entity->getId(),
'entityType' => $entityType,
'name' => $entity->get(Field::NAME),
'dateField' => $dateField,
'attributes' => (object) [
$dateField => $entity->get($dateField),
$dateField . 'Date' => $entity->get($dateField . 'Date'),
],
];
$resultList[] = new Item($reminderId, $data);
}
return $resultList;
}
private function removeReminder(string $id): void
{
$deleteQuery = $this->entityManager
->getQueryBuilder()
->delete()
->from(Reminder::ENTITY_TYPE)
->where([Attribute::ID => $id])
->build();
$this->entityManager->getQueryExecutor()->execute($deleteQuery);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,44 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Activities\Upcoming;
class Params
{
/**
* @param ?string[] $entityTypeList
*/
public function __construct(
readonly public ?int $offset = null,
readonly public ?int $maxSize = null,
readonly public ?int $futureDays = null,
readonly public ?array $entityTypeList = null,
readonly public bool $includeShared = false,
) {}
}

View File

@@ -0,0 +1,410 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* 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\Activities;
use Espo\Core\Acl;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Name\Field;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Core\Record\ServiceContainer;
use Espo\Core\Select\Bool\Filters\OnlyMy;
use Espo\Core\Select\Bool\Filters\Shared;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Espo\Core\Utils\Metadata;
use Espo\Entities\Preferences;
use Espo\Entities\User;
use Espo\Modules\Crm\Classes\Select\Meeting\PrimaryFilters\Planned;
use Espo\Modules\Crm\Classes\Select\Task\PrimaryFilters\Actual;
use Espo\Modules\Crm\Entities\Task;
use Espo\Modules\Crm\Tools\Activities\Upcoming\Params;
use Espo\ORM\EntityCollection;
use Espo\ORM\EntityManager;
use Espo\ORM\Entity;
use Espo\ORM\Query\Select;
use Espo\Core\Acl\Table;
use Espo\Core\Record\Collection as RecordCollection;
use Espo\Core\Select\Where\Item as WhereItem;
use Espo\Core\Select\Where\ConverterFactory as WhereConverterFactory;
use Espo\Core\Select\SelectBuilderFactory;
use Espo\ORM\Query\SelectBuilder;
use Exception;
use PDO;
use DateTime;
use RuntimeException;
class UpcomingService
{
private const UPCOMING_ACTIVITIES_FUTURE_DAYS = 1;
private const UPCOMING_ACTIVITIES_TASK_FUTURE_DAYS = 7;
public function __construct(
private WhereConverterFactory $whereConverterFactory,
private SelectBuilderFactory $selectBuilderFactory,
private Config $config,
private Metadata $metadata,
private Acl $acl,
private EntityManager $entityManager,
private ServiceContainer $serviceContainer,
private Config\ApplicationConfig $applicationConfig,
) {}
/**
* Get upcoming activities.
*
* @return RecordCollection<Entity>
* @throws Forbidden
* @throws NotFound
* @throws BadRequest
*/
public function get(string $userId, Params $params): RecordCollection
{
/** @var ?User $user */
$user = $this->entityManager->getEntityById(User::ENTITY_TYPE, $userId);
if (!$user) {
throw new NotFound();
}
$this->accessCheck($user);
$entityTypeList = $params->entityTypeList ?? $this->config->get('activitiesEntityList', []);
$futureDays = $params->futureDays ??
$this->config->get('activitiesUpcomingFutureDays', self::UPCOMING_ACTIVITIES_FUTURE_DAYS);
$queryList = [];
foreach ($entityTypeList as $entityType) {
if (
!$this->metadata->get(['scopes', $entityType, 'activity']) &&
$entityType !== Task::ENTITY_TYPE
) {
continue;
}
if (
!$this->acl->checkScope($entityType, Table::ACTION_READ) ||
!$this->metadata->get(['entityDefs', $entityType, 'fields', 'dateStart']) ||
!$this->metadata->get(['entityDefs', $entityType, 'fields', 'dateEnd'])
) {
continue;
}
$queryList[] = $this->getEntityTypeQuery($entityType, $user, $futureDays, $params->includeShared);
}
if ($queryList === []) {
return RecordCollection::create(new EntityCollection(), 0);
}
$builder = $this->entityManager
->getQueryBuilder()
->union();
foreach ($queryList as $query) {
$builder->query($query);
}
$unionCountQuery = $builder->build();
$countQuery = $this->entityManager->getQueryBuilder()
->select()
->fromQuery($unionCountQuery, 'c')
->select('COUNT:(c.id)', 'count')
->build();
$countSth = $this->entityManager->getQueryExecutor()->execute($countQuery);
$row = $countSth->fetch(PDO::FETCH_ASSOC);
$totalCount = $row['count'];
$offset = $params->offset ?? 0;
$maxSize = $params->maxSize ?? 0;
$unionQuery = $builder
->order('dateEndIsNull')
->order('order')
->order('dateStart')
->order('dateEnd')
->order('name')
->limit($offset, $maxSize)
->build();
$sth = $this->entityManager->getQueryExecutor()->execute($unionQuery);
$rows = $sth->fetchAll(PDO::FETCH_ASSOC) ?: [];
$collection = new EntityCollection();
foreach ($rows as $row) {
/** @var string $itemEntityType */
$itemEntityType = $row['entityType'];
/** @var string $itemId */
$itemId = $row['id'];
$entity = $this->entityManager->getEntityById($itemEntityType, $itemId);
if (!$entity) {
// @todo Revise.
$entity = $this->entityManager->getNewEntity($itemEntityType);
$entity->set('id', $itemId);
}
if (
$entity instanceof CoreEntity &&
$entity->hasLinkParentField(Field::PARENT)
) {
$entity->loadParentNameField(Field::PARENT);
}
$this->serviceContainer->get($itemEntityType)->prepareEntityForOutput($entity);
$collection->append($entity);
}
/** @var RecordCollection<Entity> */
return RecordCollection::create($collection, $totalCount);
}
/**
* @throws Forbidden
* @throws BadRequest
*/
private function getEntityTypeQuery(string $entityType, User $user, int $futureDays, bool $includeShared): Select
{
try {
$beforeString = (new DateTime())->modify('+' . $futureDays . ' days')
->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);
} catch (Exception $e) {
throw new RuntimeException($e->getMessage(), 0, $e);
}
$builder = $this->selectBuilderFactory
->create()
->from($entityType)
->forUser($user)
->withBoolFilter(OnlyMy::NAME)
->withStrictAccessControl();
if ($includeShared && $this->metadata->get("scopes.$entityType.collaborators")) {
$builder->withBoolFilter(Shared::NAME);
}
$orderField = 'dateStart';
$primaryFilter = Planned::NAME;
if ($entityType === Task::ENTITY_TYPE) {
$orderField = 'dateEnd';
$primaryFilter = Actual::NAME;
}
$builder->withPrimaryFilter($primaryFilter);
$queryBuilder = $builder->buildQueryBuilder();
$this->apply($entityType, $user, $queryBuilder, $beforeString);
$queryBuilder->select([
'id',
'name',
'dateStart',
'dateEnd',
['"' . $entityType . '"', 'entityType'],
['IS_NULL:(dateEnd)', 'dateEndIsNull'],
[$orderField, 'order'],
]);
return $queryBuilder->build();
}
private function getUserTimeZone(User $user): string
{
$preferences = $this->entityManager->getEntityById(Preferences::ENTITY_TYPE, $user->getId());
if ($preferences) {
$timeZone = $preferences->get('timeZone');
if ($timeZone) {
return $timeZone;
}
}
return $this->applicationConfig->getTimeZone();
}
/**
* @throws Forbidden
*/
private function accessCheck(Entity $entity): void
{
if ($entity instanceof User) {
if (!$this->acl->checkUserPermission($entity, Acl\Permission::USER_CALENDAR)) {
throw new Forbidden();
}
return;
}
if (!$this->acl->check($entity, Table::ACTION_READ)) {
throw new Forbidden();
}
}
/**
* @throws BadRequest
*/
private function applyTask(
User $user,
SelectBuilder $queryBuilder,
string $beforeString
): void {
$converter = $this->whereConverterFactory->create(Task::ENTITY_TYPE, $user);
$timeZone = $this->getUserTimeZone($user);
$upcomingTaskFutureDays = $this->config->get(
'activitiesUpcomingTaskFutureDays',
self::UPCOMING_ACTIVITIES_TASK_FUTURE_DAYS
);
$taskBeforeString = (new DateTime())
->modify('+' . $upcomingTaskFutureDays . ' days')
->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);
$queryBuilder->where([
'OR' => [
[
'dateStart' => null,
'OR' => [
'dateEnd' => null,
$converter->convert(
$queryBuilder,
WhereItem::fromRaw([
'type' => 'before',
'attribute' => 'dateEnd',
'value' => $taskBeforeString,
])
)->getRaw()
]
],
[
'dateStart!=' => null,
'OR' => [
$converter->convert(
$queryBuilder,
WhereItem::fromRaw([
'type' => 'past',
'attribute' => 'dateStart',
'dateTime' => true,
'timeZone' => $timeZone,
])
)->getRaw(),
$converter->convert(
$queryBuilder,
WhereItem::fromRaw([
'type' => 'today',
'attribute' => 'dateStart',
'dateTime' => true,
'timeZone' => $timeZone,
])
)->getRaw(),
$converter->convert(
$queryBuilder,
WhereItem::fromRaw([
'type' => 'before',
'attribute' => 'dateStart',
'value' => $beforeString,
])
)->getRaw(),
]
],
],
]);
}
/**
* @throws BadRequest
*/
private function apply(
string $entityType,
User $user,
SelectBuilder $queryBuilder,
string $beforeString
): void {
if ($entityType === Task::ENTITY_TYPE) {
$this->applyTask($user, $queryBuilder, $beforeString);
return;
}
$converter = $this->whereConverterFactory->create($entityType, $user);
$timeZone = $this->getUserTimeZone($user);
$queryBuilder->where([
'OR' => [
$converter->convert(
$queryBuilder,
WhereItem::fromRaw([
'type' => 'today',
'attribute' => 'dateStart',
'dateTime' => true,
'timeZone' => $timeZone,
])
)->getRaw(),
[
$converter->convert(
$queryBuilder,
WhereItem::fromRaw([
'type' => 'future',
'attribute' => 'dateEnd',
'dateTime' => true,
'timeZone' => $timeZone,
])
)->getRaw(),
$converter->convert(
$queryBuilder,
WhereItem::fromRaw([
'type' => 'before',
'attribute' => 'dateStart',
'value' => $beforeString,
])
)->getRaw(),
],
],
]);
}
}