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,80 @@
<?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\Tools\Import\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\Entities\Import;
use Espo\Tools\Import\Params as ImportParams;
use Espo\Tools\Import\Service;
/**
* Creates imports.
*/
class Post implements Action
{
public function __construct(private Service $service, private Acl $acl) {}
public function process(Request $request): Response
{
if (!$this->acl->checkScope(Import::ENTITY_TYPE)) {
throw new Forbidden();
}
$data = $request->getParsedBody();
$entityType = $data->entityType ?? null;
$attributeList = $data->attributeList ?? null;
$attachmentId = $data->attachmentId ?? null;
if (!is_array($attributeList)) {
throw new BadRequest("No `attributeList`.");
}
if (!$attachmentId) {
throw new BadRequest("No `attachmentId`.");
}
if (!$entityType) {
throw new BadRequest("No `entityType`.");
}
$params = ImportParams::fromRaw($data);
$result = $this->service->import($entityType, $attributeList, $attachmentId, $params);
return ResponseComposer::json($result->getValueMap());
}
}

View File

@@ -0,0 +1,65 @@
<?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\Tools\Import\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\Entities\Import;
use Espo\Tools\Import\Service;
/**
* Exports import errors.
*/
class PostExportErrors implements Action
{
public function __construct(private Service $service, private Acl $acl) {}
public function process(Request $request): Response
{
if (!$this->acl->checkScope(Import::ENTITY_TYPE)) {
throw new Forbidden();
}
$id = $request->getRouteParam('id');
if (!$id) {
throw new BadRequest();
}
$attachmentId = $this->service->exportErrors($id);
return ResponseComposer::json(['attachmentId' => $attachmentId]);
}
}

View File

@@ -0,0 +1,60 @@
<?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\Tools\Import\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\Forbidden;
use Espo\Entities\Import;
use Espo\Tools\Import\Service;
/**
* Uploads CSV import files.
*/
class PostFile implements Action
{
public function __construct(private Service $service, private Acl $acl) {}
public function process(Request $request): Response
{
if (!$this->acl->checkScope(Import::ENTITY_TYPE)) {
throw new Forbidden();
}
$contents = $request->getBodyContents() ?? '';
$attachmentId = $this->service->uploadFile($contents);
return ResponseComposer::json(['attachmentId' => $attachmentId]);
}
}

View File

@@ -0,0 +1,65 @@
<?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\Tools\Import\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\Entities\Import;
use Espo\Tools\Import\Service;
/**
* Removes imported duplicates.
*/
class PostRemoveDuplicates implements Action
{
public function __construct(private Service $service, private Acl $acl) {}
public function process(Request $request): Response
{
if (!$this->acl->checkScope(Import::ENTITY_TYPE)) {
throw new Forbidden();
}
$id = $request->getRouteParam('id');
if (!$id) {
throw new BadRequest();
}
$this->service->removeDuplicates($id);
return ResponseComposer::json(true);
}
}

View File

@@ -0,0 +1,65 @@
<?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\Tools\Import\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\Entities\Import;
use Espo\Tools\Import\Service;
/**
* Reverts imports.
*/
class PostRevert implements Action
{
public function __construct(private Service $service, private Acl $acl) {}
public function process(Request $request): Response
{
if (!$this->acl->checkScope(Import::ENTITY_TYPE)) {
throw new Forbidden();
}
$id = $request->getRouteParam('id');
if (!$id) {
throw new BadRequest();
}
$this->service->revert($id);
return ResponseComposer::json(true);
}
}

View File

@@ -0,0 +1,74 @@
<?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\Tools\Import\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\Entities\Import;
use Espo\Tools\Import\Service;
/**
* Un-marks specific records as duplicates.
*/
class PostUnmarkDuplicates implements Action
{
public function __construct(private Service $service, private Acl $acl) {}
public function process(Request $request): Response
{
if (!$this->acl->checkScope(Import::ENTITY_TYPE)) {
throw new Forbidden();
}
$id = $request->getRouteParam('id');
if (!$id) {
throw new BadRequest();
}
$data = $request->getParsedBody();
$entityType = $data->entityType ?? null;
$entityId = $data->entityId ?? null;
if (!$entityType || !$entityId) {
throw new BadRequest("No `entityType` or `entityId`.");
}
$this->service->unmarkAsDuplicate($id, $entityType, $entityId);
return ResponseComposer::json(true);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,43 @@
<?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\Tools\Import;
use Espo\Core\InjectableFactory;
class ImportFactory
{
public function __construct(private InjectableFactory $injectableFactory)
{}
public function create(): Import
{
return $this->injectableFactory->create(Import::class);
}
}

View File

@@ -0,0 +1,85 @@
<?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\Tools\Import\Jobs;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Job\Job;
use Espo\Core\Job\Job\Data;
use Espo\Core\Exceptions\Error;
use Espo\Tools\Import\ImportFactory;
use Espo\Tools\Import\Params as ImportParams;
use Espo\ORM\EntityManager;
use Espo\Entities\User;
class RunIdle implements Job
{
public function __construct(
private ImportFactory $factory,
private EntityManager $entityManager
) {}
/**
* @throws Forbidden
* @throws Error
*/
public function run(Data $data): void
{
$raw = $data->getRaw();
$entityType = $raw->entityType;
$attachmentId = $raw->attachmentId;
$importId = $raw->importId;
$importAttributeList = $raw->importAttributeList;
$userId = $raw->userId;
$params = ImportParams::fromRaw($raw->params);
/** @var ?User $user */
$user = $this->entityManager->getEntityById(User::ENTITY_TYPE, $userId);
if (!$user) {
throw new Error("Import: User not found.");
}
if (!$user->isActive()) {
throw new Error("Import: User is not active.");
}
$this->factory
->create()
->setEntityType($entityType)
->setAttributeList($importAttributeList)
->setAttachmentId($attachmentId)
->setParams($params)
->setId($importId)
->setUser($user)
->run();
}
}

View File

@@ -0,0 +1,392 @@
<?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\Tools\Import;
use Espo\Core\Utils\ObjectUtil;
use stdClass;
use TypeError;
/**
* Immutable.
*/
class Params
{
public const ACTION_CREATE = 'create';
public const ACTION_CREATE_AND_UPDATE = 'createAndUpdate';
public const ACTION_UPDATE = 'update';
private ?string $action = null;
private ?string $delimiter = null;
private ?string $textQualifier = null;
private ?string $personNameFormat = null;
private bool $idleMode = false;
private bool $manualMode = false;
private bool $silentMode = false;
private bool $headerRow = false;
private bool $skipDuplicateChecking = false;
private bool $startFromLastIndex = false;
/**
* @var int[]
*/
private $updateBy = [];
private stdClass $defaultValues;
private ?string $dateFormat = null;
private ?string $timeFormat = null;
private ?string $currency = null;
private ?string $timezone = null;
private ?string $decimalMark = null;
private ?string $phoneNumberCountry = null;
private function __construct()
{
$this->defaultValues = (object) [];
}
public function getAction(): ?string
{
return $this->action;
}
public function getDelimiter(): ?string
{
return $this->delimiter;
}
public function getTextQualifier(): ?string
{
return $this->textQualifier;
}
public function getPersonNameFormat(): ?string
{
return $this->personNameFormat;
}
public function getPhoneNumberCountry(): ?string
{
return $this->phoneNumberCountry;
}
public function isIdleMode(): bool
{
return $this->idleMode;
}
public function isManualMode(): bool
{
return $this->manualMode;
}
public function isSilentMode(): bool
{
return $this->silentMode;
}
public function headerRow(): bool
{
return $this->headerRow;
}
public function skipDuplicateChecking(): bool
{
return $this->skipDuplicateChecking;
}
public function startFromLastIndex(): bool
{
return $this->startFromLastIndex;
}
/**
* @return int[]
*/
public function getUpdateBy(): array
{
return $this->updateBy;
}
public function getDefaultValues(): stdClass
{
return ObjectUtil::clone($this->defaultValues);
}
public function getDateFormat(): ?string
{
return $this->dateFormat;
}
public function getTimeFormat(): ?string
{
return $this->timeFormat;
}
public function getCurrency(): ?string
{
return $this->currency;
}
public function getTimezone(): ?string
{
return $this->timezone;
}
public function getDecimalMark(): ?string
{
return $this->decimalMark;
}
public static function create(): self
{
return new self();
}
public function withAction(?string $action): self
{
$obj = clone $this;
$obj->action = $action;
return $obj;
}
public function withDelimiter(?string $delimiter): self
{
$obj = clone $this;
$obj->delimiter = $delimiter;
return $obj;
}
public function withTextQualifier(?string $textQualifier): self
{
$obj = clone $this;
$obj->textQualifier = $textQualifier;
return $obj;
}
public function withPersonNameFormat(?string $personNameFormat): self
{
$obj = clone $this;
$obj->personNameFormat = $personNameFormat;
return $obj;
}
public function withPhoneNumberCountry(?string $phoneNumberCountry): self
{
$obj = clone $this;
$obj->phoneNumberCountry = $phoneNumberCountry;
return $obj;
}
public function withIdleMode(bool $idleMode = true): self
{
$obj = clone $this;
$obj->idleMode = $idleMode;
return $obj;
}
public function withManualMode(bool $manualMode = true): self
{
$obj = clone $this;
$obj->manualMode = $manualMode;
return $obj;
}
public function withSilentMode(bool $silentMode = true): self
{
$obj = clone $this;
$obj->silentMode = $silentMode;
return $obj;
}
public function withHeaderRow(bool $headerRow = true): self
{
$obj = clone $this;
$obj->headerRow = $headerRow;
return $obj;
}
public function withSkipDuplicateChecking(bool $skipDuplicateChecking = true): self
{
$obj = clone $this;
$obj->skipDuplicateChecking = $skipDuplicateChecking;
return $obj;
}
public function withStartFromLastIndex(bool $startFromLastIndex = true): self
{
$obj = clone $this;
$obj->startFromLastIndex = $startFromLastIndex;
return $obj;
}
/**
* @param int[] $updateBy
*/
public function withUpdateBy(array $updateBy): self
{
$obj = clone $this;
$obj->updateBy = $updateBy;
return $obj;
}
/**
* @param stdClass|array<string, mixed>|null $defaultValues
*/
public function withDefaultValues($defaultValues): self
{
if (is_array($defaultValues)) {
$defaultValues = (object) $defaultValues;
}
if (is_null($defaultValues)) {
$defaultValues = (object) [];
}
/** @var stdClass|scalar $defaultValues */
if (!is_object($defaultValues)) {
throw new TypeError();
}
$obj = clone $this;
$obj->defaultValues = $defaultValues;
return $obj;
}
public function withDateFormat(?string $dateFormat): self
{
$obj = clone $this;
$obj->dateFormat = $dateFormat;
return $obj;
}
public function withTimeFormat(?string $timeFormat): self
{
$obj = clone $this;
$obj->timeFormat = $timeFormat;
return $obj;
}
public function withCurrency(?string $currency): self
{
$obj = clone $this;
$obj->currency = $currency;
return $obj;
}
public function withTimezone(?string $timezone): self
{
$obj = clone $this;
$obj->timezone = $timezone;
return $obj;
}
public function withDecimalMark(?string $decimalMark): self
{
$obj = clone $this;
$obj->decimalMark = $decimalMark;
return $obj;
}
/**
* @param stdClass|array<string, mixed>|null $params
*/
public static function fromRaw($params): self
{
if ($params === null) {
$params = (object) [];
}
$raw = (object) $params;
return self::create()
->withAction($raw->action ?? null)
->withCurrency($raw->currency ?? null)
->withDateFormat($raw->dateFormat ?? null)
->withDecimalMark($raw->decimalMark ?? null)
->withDefaultValues($raw->defaultValues ?? null)
->withDelimiter($raw->delimiter ?? null)
->withHeaderRow($raw->headerRow ?? false)
->withIdleMode($raw->idleMode ?? false)
->withManualMode($raw->manualMode ?? false)
->withPersonNameFormat($raw->personNameFormat ?? null)
->withPhoneNumberCountry($raw->phoneNumberCountry ?? null)
->withSilentMode($raw->silentMode ?? false)
->withSkipDuplicateChecking($raw->skipDuplicateChecking ?? false)
->withStartFromLastIndex($raw->startFromLastIndex ?? false)
->withTextQualifier($raw->textQualifier ?? null)
->withTimeFormat($raw->timeFormat ?? false)
->withTimezone($raw->timezone ?? null)
->withUpdateBy($raw->updateBy ?? []);
}
/**
* @return array<string, mixed>
*/
public function getRaw(): array
{
return [
'action' => $this->action,
'currency' => $this->currency,
'dateFormat' => $this->dateFormat,
'decimalMark' => $this->decimalMark,
'defaultValues' => $this->defaultValues,
'delimiter' => $this->delimiter,
'headerRow' => $this->headerRow,
'idleMode' => $this->idleMode,
'manualMode' => $this->manualMode,
'personNameFormat' => $this->personNameFormat,
'silentMode' => $this->silentMode,
'skipDuplicateChecking' => $this->skipDuplicateChecking,
'startFromLastIndex' => $this->startFromLastIndex,
'textQualifier' => $this->textQualifier,
'phoneNumberCountry' => $this->phoneNumberCountry,
'timeFormat' => $this->timeFormat,
'timezone' => $this->timezone,
'updateBy' => $this->updateBy,
];
}
}

View File

@@ -0,0 +1,138 @@
<?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\Tools\Import;
use stdClass;
/**
* Immutable.
*/
class Result
{
private ?string $id = null;
private int $countCreated = 0;
private int $countUpdated = 0;
private int $countError = 0;
private int $countDuplicate = 0;
private bool $manualMode = false;
public function getId(): ?string
{
return $this->id;
}
public function getCountCreated(): int
{
return $this->countCreated;
}
public function getCountUpdated(): int
{
return $this->countUpdated;
}
public function getCountError(): int
{
return $this->countError;
}
public function getCountDuplicate(): int
{
return $this->countDuplicate;
}
public function isManualMode(): bool
{
return $this->manualMode;
}
public static function create(): self
{
return new self();
}
public function withId(?string $id): self
{
$obj = clone $this;
$obj->id = $id;
return $obj;
}
public function withCountCreated(int $value): self
{
$obj = clone $this;
$obj->countCreated = $value;
return $obj;
}
public function withCountUpdated(int $value): self
{
$obj = clone $this;
$obj->countUpdated = $value;
return $obj;
}
public function withCountError(int $value): self
{
$obj = clone $this;
$obj->countError = $value;
return $obj;
}
public function withCountDuplicate(int $value): self
{
$obj = clone $this;
$obj->countDuplicate = $value;
return $obj;
}
public function withManualMode(bool $manualMode = true): self
{
$obj = clone $this;
$obj->manualMode = $manualMode;
return $obj;
}
public function getValueMap(): stdClass
{
return (object) [
'id' => $this->id,
'countCreated' => $this->countCreated,
'countUpdated' => $this->countUpdated,
'manualMode' => $this->manualMode,
];
}
}

View File

@@ -0,0 +1,548 @@
<?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\Tools\Import;
use Espo\Core\Name\Field;
use Espo\ORM\Name\Attribute;
use Exception;
use GuzzleHttp\Psr7\Utils as Psr7Utils;
use Espo\Core\Record\ActionHistory\Action;
use Espo\ORM\Entity;
use Espo\ORM\Query\DeleteBuilder;
use Espo\ORM\Type\RelationType;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\Entities\ImportEntity as ImportEntityEntity;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\FileStorage\Manager as FileStorageManager;
use Espo\Core\Record\ServiceContainer;
use Espo\Core\Acl;
use Espo\Core\Acl\Table;
use Espo\Entities\ImportError;
use Espo\ORM\Collection;
use Espo\ORM\EntityManager;
use Espo\Entities\Import as ImportEntity;
use Espo\Entities\Attachment;
use DateTime;
use SplFileObject;
use RuntimeException;
class Service
{
private const REVERT_PERMANENTLY_REMOVE_PERIOD_DAYS = 2;
public function __construct(
private ImportFactory $factory,
private ServiceContainer $recordServiceContainer,
private EntityManager $entityManager,
private Acl $acl,
private FileStorageManager $fileStorageManager
) {}
/**
* @param string[] $attributeList
* @param string $attachmentId
* @throws Forbidden
* @throws Error
*/
public function import(
string $entityType,
array $attributeList,
string $attachmentId,
Params $params
): Result {
if (!$this->acl->checkScope(ImportEntity::ENTITY_TYPE)) {
throw new Forbidden("No access to Import scope.");
}
if (!$this->acl->check($entityType, Table::ACTION_CREATE)) {
throw new Forbidden("No create access for '$entityType'.");
}
$result = $this->factory
->create()
->setEntityType($entityType)
->setAttributeList($attributeList)
->setAttachmentId($attachmentId)
->setParams($params)
->run();
$id = $result->getId();
if ($id) {
$import = $this->entityManager->getEntityById(ImportEntity::ENTITY_TYPE, $id);
if ($import) {
$this->recordServiceContainer
->get(ImportEntity::ENTITY_TYPE)
->processActionHistoryRecord(Action::CREATE, $import);
}
}
return $result;
}
/**
* @throws Forbidden
* @throws Error
*/
public function importContentsWithParamsId(string $contents, string $importParamsId): Result
{
if (!$contents) {
throw new Error("Contents is empty.");
}
/** @var ?ImportEntity $source */
$source = $this->entityManager->getEntityById(ImportEntity::ENTITY_TYPE, $importParamsId);
if (!$source) {
throw new Error("Import '$importParamsId' not found.");
}
$entityType = $source->getTargetEntityType();
$attributeList = $source->getTargetAttributeList() ?? [];
if (!$entityType) {
throw new Error("No entity-type.");
}
$params = $source->getParams()
->withIdleMode(false)
->withManualMode(false);
$attachmentId = $this->uploadFile($contents);
return $this->import($entityType, $attributeList, $attachmentId, $params);
}
/**
* @throws Forbidden
* @throws Error
* @throws NotFound
*/
public function importById(string $id, bool $startFromLastIndex = false, bool $forceResume = false): Result
{
/** @var ?ImportEntity $import */
$import = $this->entityManager->getEntityById(ImportEntity::ENTITY_TYPE, $id);
if (!$import) {
throw new NotFound("Import '$id' not found.");
}
$status = $import->getStatus();
if ($status !== ImportEntity::STATUS_STANDBY) {
if (!in_array($status, [ImportEntity::STATUS_IN_PROCESS, ImportEntity::STATUS_FAILED])) {
throw new Forbidden("Can't run import with '$status' status.");
}
if (!$forceResume) {
throw new Forbidden("Import has '$status' status. Use -r flag to force resume.");
}
}
$entityType = $import->getTargetEntityType();
$attributeList = $import->getTargetAttributeList() ?? [];
if (!$entityType) {
throw new Error("No entity-type.");
}
$params = $import->getParams()
->withStartFromLastIndex($startFromLastIndex);
$attachmentId = $import->getFileId();
if (!$attachmentId) {
throw new Error("No file-id.");
}
return $this->factory
->create()
->setEntityType($entityType)
->setAttributeList($attributeList)
->setAttachmentId($attachmentId)
->setParams($params)
->setId($id)
->run();
}
/**
* @throws Forbidden
* @throws NotFound
*/
public function revert(string $id): void
{
if (!$this->acl->checkScope(ImportEntity::ENTITY_TYPE)) {
throw new Forbidden("No access to Import scope.");
}
$import = $this->entityManager->getEntityById(ImportEntity::ENTITY_TYPE, $id);
if (!$import) {
throw new NotFound("Could not find import record.");
}
if (!$this->acl->checkEntityDelete($import)) {
throw new Forbidden("No access to import record.");
}
$importEntityList = $this->entityManager
->getRDBRepository(ImportEntityEntity::ENTITY_TYPE)
->sth()
->where([
'importId' => $import->getId(),
'isImported' => true,
])
->find();
$removeFromDb = false;
$createdAt = $import->get(Field::CREATED_AT);
if ($createdAt) {
$dtNow = new DateTime();
try {
$createdAtDt = new DateTime($createdAt);
} catch (Exception $e) {
throw new RuntimeException($e->getMessage());
}
$dayDiff = ($dtNow->getTimestamp() - $createdAtDt->getTimestamp()) / 60 / 60 / 24;
if ($dayDiff < self::REVERT_PERMANENTLY_REMOVE_PERIOD_DAYS) {
$removeFromDb = true;
}
}
foreach ($importEntityList as $importEntity) {
$entityType = $importEntity->get('entityType');
$entityId = $importEntity->get('entityId');
if (!$entityType || !$entityId) {
continue;
}
if (!$this->entityManager->hasRepository($entityType)) {
continue;
}
$entity = $this->entityManager
->getRDBRepository($entityType)
->select([Attribute::ID])
->where([Attribute::ID => $entityId])
->findOne();
if (!$entity) {
continue;
}
if ($removeFromDb) {
$this->deleteRelations($entity);
}
$this->entityManager->removeEntity($entity, [
SaveOption::NO_STREAM => true,
SaveOption::NO_NOTIFICATIONS => true,
SaveOption::SILENT => true,
SaveOption::IMPORT => true,
]);
if ($removeFromDb) {
$this->entityManager
->getRDBRepository($entityType)
->deleteFromDb($entityId);
}
}
$this->entityManager->removeEntity($import);
$this->recordServiceContainer
->get(ImportEntity::ENTITY_TYPE)
->processActionHistoryRecord(Action::DELETE, $import);
}
private function deleteRelations(Entity $entity): void
{
$relationDefsList = $this->entityManager
->getDefs()
->getEntity($entity->getEntityType())
->getRelationList();
foreach ($relationDefsList as $relationDefs) {
if ($relationDefs->getType() !== RelationType::MANY_MANY) {
continue;
}
$middleEntityType = ucfirst($relationDefs->getRelationshipName());
$midKey = $relationDefs->getMidKey();
$where = [$midKey => $entity->getId()];
foreach ($relationDefs->getConditions() as $key => $value) {
$where[$key] = $value;
}
$deleteQuery = DeleteBuilder::create()
->from($middleEntityType)
->where($where)
->build();
$this->entityManager
->getQueryExecutor()
->execute($deleteQuery);
}
}
/**
* @return string Attachment ID.
* @throws Forbidden
*/
public function uploadFile(string $contents): string
{
if (!$this->acl->checkScope(ImportEntity::ENTITY_TYPE)) {
throw new Forbidden("No access to Import scope.");
}
$attachment = $this->entityManager->getRDBRepositoryByClass(Attachment::class)->getNew();
$attachment->setType('text/csv');
$attachment->setRole('Import File');
$attachment->setName('import-file.csv');
$attachment->setContents($contents);
$this->entityManager->saveEntity($attachment);
return $attachment->getId();
}
/**
* @throws Forbidden
* @throws NotFound
*/
public function removeDuplicates(string $id): void
{
if (!$this->acl->checkScope(ImportEntity::ENTITY_TYPE)) {
throw new Forbidden("No access to Import scope.");
}
$import = $this->entityManager->getEntityById(ImportEntity::ENTITY_TYPE, $id);
if (!$import) {
throw new NotFound("Import '$id' not found.");
}
if (!$this->acl->checkEntityDelete($import)) {
throw new Forbidden("No delete access.");
}
$importEntityList = $this->entityManager
->getRDBRepository(ImportEntityEntity::ENTITY_TYPE)
->sth()
->where([
'importId' => $import->getId(),
'isDuplicate' => true,
])
->find();
foreach ($importEntityList as $importEntity) {
$entityType = $importEntity->get('entityType');
$entityId = $importEntity->get('entityId');
if (!$entityType || !$entityId) {
continue;
}
if (!$this->entityManager->hasRepository($entityType)) {
continue;
}
$entity = $this->entityManager
->getRDBRepository($entityType)
->select([Attribute::ID])
->where([Attribute::ID => $entityId])
->findOne();
if (!$entity) {
continue;
}
$this->deleteRelations($entity);
$this->entityManager->removeEntity($entity, [
SaveOption::NO_STREAM => true,
SaveOption::NO_NOTIFICATIONS => true,
SaveOption::SILENT => true,
SaveOption::IMPORT => true,
]);
$this->entityManager
->getRDBRepository($entityType)
->deleteFromDb($entityId);
}
}
/**
* @throws NotFound
* @throws Forbidden
*/
public function unmarkAsDuplicate(string $importId, string $entityType, string $entityId): void
{
if (!$this->acl->checkScope(ImportEntity::ENTITY_TYPE)) {
throw new Forbidden("No access to Import scope.");
}
$entity = $this->entityManager
->getRDBRepository(ImportEntityEntity::ENTITY_TYPE)
->where([
'importId' => $importId,
'entityType' => $entityType,
'entityId' => $entityId,
])
->findOne();
if (!$entity) {
throw new NotFound();
}
$entity->set('isDuplicate', false);
$this->entityManager->saveEntity($entity);
}
/**
* @param string $importId An import ID.
* @return ?string An attachment ID.
* @throws NotFound
*/
public function exportErrors(string $importId): ?string
{
$import = $this->entityManager
->getRepositoryByClass(ImportEntity::class)
->getById($importId);
if (!$import) {
throw new NotFound();
}
$count = $this->entityManager
->getRDBRepositoryByClass(ImportEntity::class)
->getRelation($import, 'errors')
->count();
if ($count === 0) {
return null;
}
$importAttachmentId = $import->getFileId();
if (!$importAttachmentId) {
throw new RuntimeException("No import file ID.");
}
$importAttachment = $this->entityManager
->getRepositoryByClass(Attachment::class)
->getById($importAttachmentId);
if (!$importAttachment) {
throw new RuntimeException("No import attachment.");
}
$filePath = $this->fileStorageManager->getLocalFilePath($importAttachment);
$file = new SplFileObject($filePath);
$resource = fopen('php://temp', 'w+');
if ($resource === false) {
throw new RuntimeException("Could not open temp.");
}
$stream = Psr7Utils::streamFor($resource);
/** @var Collection<ImportError> $errorList */
$errorList = $this->entityManager
->getRDBRepositoryByClass(ImportEntity::class)
->getRelation($import, 'errors')
->sth()
->select(['exportRowIndex', 'rowIndex'])
->order('rowIndex')
->find();
if ($import->getParams()->headerRow()) {
$file->seek(0);
/** @var string|false $line */
$line = $file->current();
if ($line === false) {
throw new RuntimeException();
}
$stream->write($line);
}
foreach ($errorList as $error) {
$file->seek($error->getRowIndex());
/** @var string|false $line */
$line = $file->current();
if ($line === false) {
throw new RuntimeException();
}
$stream->write($line);
}
$name = 'Errors_' . substr($importAttachment->getName() ?? '', 0, -4) . '.csv';
$attachment = $this->entityManager->getRepositoryByClass(Attachment::class)->getNew();
$attachment->setRole(Attachment::ROLE_EXPORT_FILE);
$attachment->setType('text/csv');
$attachment->setName($name);
$attachment->setSize($stream->getSize());
$this->entityManager->saveEntity($attachment);
$this->fileStorageManager->putStream($attachment, $stream);
fclose($resource);
return $attachment->getId();
}
}