Initial commit
This commit is contained in:
39
application/Espo/Tools/MassUpdate/Action.php
Normal file
39
application/Espo/Tools/MassUpdate/Action.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?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\MassUpdate;
|
||||
|
||||
class Action
|
||||
{
|
||||
public const UPDATE = 'update';
|
||||
|
||||
public const ADD = 'add';
|
||||
|
||||
public const REMOVE = 'remove';
|
||||
}
|
||||
166
application/Espo/Tools/MassUpdate/Data.php
Normal file
166
application/Espo/Tools/MassUpdate/Data.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?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\MassUpdate;
|
||||
|
||||
use Espo\Core\MassAction\Data as ActionData;
|
||||
use Espo\Core\Utils\ObjectUtil;
|
||||
|
||||
use RuntimeException;
|
||||
use stdClass;
|
||||
|
||||
class Data
|
||||
{
|
||||
private stdClass $values;
|
||||
|
||||
/**
|
||||
* @var array<string, Action::*>
|
||||
*/
|
||||
private array $actions;
|
||||
|
||||
/**
|
||||
* @param array<string, Action::*> $actions
|
||||
*/
|
||||
private function __construct(stdClass $values, array $actions)
|
||||
{
|
||||
$this->values = $values;
|
||||
$this->actions = $actions;
|
||||
}
|
||||
|
||||
public function has(string $attribute): bool
|
||||
{
|
||||
return property_exists($this->values, $attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAttributeList(): array
|
||||
{
|
||||
return array_keys(get_object_vars($this->values));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue(string $attribute)
|
||||
{
|
||||
return $this->getValues()->$attribute ?? null;
|
||||
}
|
||||
|
||||
public function getValues(): stdClass
|
||||
{
|
||||
return ObjectUtil::clone($this->values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Action::*|null
|
||||
*/
|
||||
public function getAction(string $attribute): ?string
|
||||
{
|
||||
if (!$this->has($attribute)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->actions[$attribute] ?? Action::UPDATE;
|
||||
}
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self((object) [], []);
|
||||
}
|
||||
|
||||
public static function fromMassActionData(ActionData $data): self
|
||||
{
|
||||
$values = $data->get('values');
|
||||
$rawActions = $data->get('actions');
|
||||
|
||||
// Backward compatibility.
|
||||
if (!$data->has('values')) {
|
||||
return new self($data->getRaw(), []);
|
||||
}
|
||||
|
||||
if (!$values instanceof stdClass) {
|
||||
throw new RuntimeException("No `values` in mass-action data.");
|
||||
}
|
||||
|
||||
if ($rawActions !== null && !$rawActions instanceof stdClass) {
|
||||
throw new RuntimeException("Bad `actions` in mass-action data.");
|
||||
}
|
||||
|
||||
if ($rawActions === null) {
|
||||
$rawActions = (object) [];
|
||||
}
|
||||
|
||||
return new self($values, get_object_vars($rawActions));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param Action::*|null $action If NULL, the current action will be used. If no current, then 'update'.
|
||||
*/
|
||||
public function with(string $attribute, $value, ?string $action = null): self
|
||||
{
|
||||
if ($action === null) {
|
||||
$action = $this->getAction($attribute) ?? Action::UPDATE;
|
||||
}
|
||||
|
||||
$values = $this->getValues();
|
||||
$actions = $this->actions;
|
||||
|
||||
$values->$attribute = $value;
|
||||
$actions[$attribute] = $action;
|
||||
|
||||
return new self($values, $actions);
|
||||
}
|
||||
|
||||
public function without(string $attribute): self
|
||||
{
|
||||
$values = $this->getValues();
|
||||
$actions = $this->actions;
|
||||
|
||||
unset($values->$attribute);
|
||||
unset($actions[$attribute]);
|
||||
|
||||
return new self($values, $actions);
|
||||
}
|
||||
|
||||
public function toMassActionData(): ActionData
|
||||
{
|
||||
return ActionData::fromRaw((object) [
|
||||
'values' => $this->getValues(),
|
||||
'actions' => (object) $this->actions,
|
||||
]);
|
||||
}
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$this->values = ObjectUtil::clone($this->values);
|
||||
}
|
||||
}
|
||||
85
application/Espo/Tools/MassUpdate/MassUpdate.php
Normal file
85
application/Espo/Tools/MassUpdate/MassUpdate.php
Normal 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\MassUpdate;
|
||||
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Exceptions\NotFound;
|
||||
use Espo\Core\MassAction\Params;
|
||||
use Espo\Core\MassAction\Result;
|
||||
use Espo\Core\MassAction\MassActionFactory;
|
||||
|
||||
use Espo\Core\Utils\SystemUser;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\Entities\User;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Entry point for the mass-update tool.
|
||||
*/
|
||||
class MassUpdate
|
||||
{
|
||||
private const ACTION = 'massUpdate';
|
||||
|
||||
public function __construct(
|
||||
private MassActionFactory $massActionFactory,
|
||||
private EntityManager $entityManager
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Process.
|
||||
*
|
||||
* @param ?User $user Under what user to perform mass-update. If not specified, the system user will be used.
|
||||
* Access control is applied for the user.
|
||||
* @throws NotFound
|
||||
* @throws Forbidden
|
||||
* @throws BadRequest
|
||||
*/
|
||||
public function process(Params $params, Data $data, ?User $user = null): Result
|
||||
{
|
||||
$entityType = $params->getEntityType();
|
||||
|
||||
if (!$user) {
|
||||
$user = $this->entityManager
|
||||
->getRDBRepositoryByClass(User::class)
|
||||
->where(['userName' => SystemUser::NAME])
|
||||
->findOne();
|
||||
}
|
||||
|
||||
if (!$user) {
|
||||
throw new RuntimeException("No user.");
|
||||
}
|
||||
|
||||
$action = $this->massActionFactory->createForUser(self::ACTION, $entityType, $user);
|
||||
|
||||
return $action->process($params, $data->toMassActionData());
|
||||
}
|
||||
}
|
||||
375
application/Espo/Tools/MassUpdate/Processor.php
Normal file
375
application/Espo/Tools/MassUpdate/Processor.php
Normal file
@@ -0,0 +1,375 @@
|
||||
<?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\MassUpdate;
|
||||
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
use Espo\Core\FieldProcessing\LinkMultiple\ListLoader as LinkMultipleLoader;
|
||||
use Espo\Core\FieldProcessing\Loader\Params as LoaderParams;
|
||||
use Espo\Core\MassAction\QueryBuilder;
|
||||
use Espo\Core\MassAction\Params;
|
||||
use Espo\Core\MassAction\Result;
|
||||
use Espo\Core\Acl;
|
||||
use Espo\Core\Acl\Table;
|
||||
use Espo\Core\ORM\Repository\Option\SaveOption;
|
||||
use Espo\Core\Record\Access\LinkCheck;
|
||||
use Espo\Core\Record\ActionHistory\Action as RecordAction;
|
||||
use Espo\Core\Record\ServiceFactory;
|
||||
use Espo\Core\Record\Service;
|
||||
use Espo\Core\Utils\FieldUtil;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Utils\Log;
|
||||
use Espo\Core\Utils\ObjectUtil;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Repositories\Attachment as AttachmentRepository;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Entities\Attachment;
|
||||
use Espo\Core\ORM\Type\FieldType;
|
||||
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
use stdClass;
|
||||
|
||||
class Processor
|
||||
{
|
||||
public function __construct(
|
||||
private ValueMapPreparator $valueMapPreparator,
|
||||
private QueryBuilder $queryBuilder,
|
||||
private Acl $acl,
|
||||
private ServiceFactory $serviceFactory,
|
||||
private EntityManager $entityManager,
|
||||
private FieldUtil $fieldUtil,
|
||||
private User $user,
|
||||
private LinkCheck $linkCheck,
|
||||
private LinkMultipleLoader $linkMultipleLoader,
|
||||
private Log $log,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws BadRequest
|
||||
* @throws Forbidden
|
||||
*/
|
||||
public function process(Params $params, Data $data): Result
|
||||
{
|
||||
$entityType = $params->getEntityType();
|
||||
|
||||
if (!$this->acl->check($entityType, Table::ACTION_EDIT)) {
|
||||
throw new Forbidden("No edit access for '$entityType'.");
|
||||
}
|
||||
|
||||
if ($this->acl->getPermissionLevel(Acl\Permission::MASS_UPDATE) !== Table::LEVEL_YES) {
|
||||
throw new Forbidden("No mass-update permission.");
|
||||
}
|
||||
|
||||
$service = $this->serviceFactory->createForUser($entityType, $this->user);
|
||||
|
||||
$filteredData = $this->filterData($entityType, $data, $service);
|
||||
|
||||
if ($filteredData->getAttributeList() === []) {
|
||||
return new Result(0, []);
|
||||
}
|
||||
|
||||
$copyFieldList = $this->detectFieldToCopyList($entityType, $filteredData);
|
||||
|
||||
$query = $this->queryBuilder->build($params);
|
||||
|
||||
$collection = $this->entityManager
|
||||
->getRDBRepository($entityType)
|
||||
->clone($query)
|
||||
->sth()
|
||||
->find();
|
||||
|
||||
$ids = [];
|
||||
$count = 0;
|
||||
|
||||
foreach ($collection as $i => $entity) {
|
||||
$itemResult = $this->processEntity(
|
||||
entity: $entity,
|
||||
data: $filteredData,
|
||||
i: $i,
|
||||
fieldToCopyList: $copyFieldList,
|
||||
service: $service,
|
||||
);
|
||||
|
||||
if (!$itemResult) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ids[] = $entity->getId();
|
||||
$count++;
|
||||
}
|
||||
|
||||
return new Result($count, $ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Service<Entity> $service
|
||||
*/
|
||||
private function filterData(string $entityType, Data $data, Service $service): Data
|
||||
{
|
||||
$filteredData = $data;
|
||||
|
||||
$values = $data->getValues();
|
||||
|
||||
$service->filterUpdateInput($values);
|
||||
$service->sanitizeInput($values);
|
||||
$this->filterDisabledFields($entityType, $values);
|
||||
|
||||
foreach ($data->getAttributeList() as $attribute) {
|
||||
if (!property_exists($values, $attribute)) {
|
||||
$filteredData = $filteredData->without($attribute);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$action = $filteredData->getAction($attribute) ?? Action::UPDATE;
|
||||
$value = $values->$attribute;
|
||||
|
||||
$filteredData = $filteredData->with($attribute, $value, $action);
|
||||
}
|
||||
|
||||
return $filteredData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $fieldToCopyList
|
||||
* @param Service<Entity> $service
|
||||
*/
|
||||
private function processEntity(
|
||||
Entity $entity,
|
||||
Data $data,
|
||||
int $i,
|
||||
array $fieldToCopyList,
|
||||
Service $service
|
||||
): bool {
|
||||
|
||||
$service->loadAdditionalFields($entity);
|
||||
|
||||
if (!$this->acl->check($entity, Table::ACTION_EDIT)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$values = $this->prepareItemValueMap($entity, $data, $i, $fieldToCopyList);
|
||||
|
||||
$service->filterInputReadOnlySaved($entity, $values);
|
||||
|
||||
if (count(get_object_vars($values)) === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$entity->set($values);
|
||||
|
||||
try {
|
||||
$service->processValidation($entity, $values);
|
||||
} catch (Exception) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$service->checkAssignment($entity)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->linkCheck->processFields($entity);
|
||||
} catch (Forbidden) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->entityManager->saveEntity($entity, [
|
||||
SaveOption::MASS_UPDATE => true,
|
||||
SaveOption::MODIFIED_BY_ID => $this->user->getId(),
|
||||
'skipStreamNotesAcl' => true,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
$this->log->info("Mass update save exception. Record: {id}.", [
|
||||
'exception' => $e,
|
||||
'id' => $entity->getId(),
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$service->processActionHistoryRecord(RecordAction::UPDATE, $entity);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $copyFieldList
|
||||
*/
|
||||
private function prepareItemValueMap(Entity $entity, Data $data, int $i, array $copyFieldList): stdClass
|
||||
{
|
||||
$dataModified = $this->copy($entity->getEntityType(), $data, $i, $copyFieldList);
|
||||
|
||||
$values = $this->valueMapPreparator->prepare($entity, $dataModified);
|
||||
|
||||
return ObjectUtil::clone($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $copyFieldList
|
||||
*/
|
||||
private function copy(string $entityType, Data $data, int $i, array $copyFieldList): Data
|
||||
{
|
||||
if (!count($copyFieldList)) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ($i === 0) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
foreach ($copyFieldList as $field) {
|
||||
$type = $this->fieldUtil->getEntityTypeFieldParam($entityType, $field, 'type');
|
||||
|
||||
if ($type === FieldType::FILE || $type === FieldType::IMAGE) {
|
||||
$data = $this->copyFileField($field, $data);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($type === FieldType::ATTACHMENT_MULTIPLE) {
|
||||
$data = $this->copyAttachmentMultipleField($field, $data);
|
||||
|
||||
//continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function copyFileField(string $field, Data $data): Data
|
||||
{
|
||||
$attribute = $field . 'Id';
|
||||
|
||||
$id = $data->getValue($attribute);
|
||||
|
||||
if (!$id) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$attachment = $this->entityManager->getRDBRepositoryByClass(Attachment::class)->getById($id);
|
||||
|
||||
if (!$attachment) {
|
||||
return $data->with($attribute, null);
|
||||
}
|
||||
|
||||
/** @var AttachmentRepository $attachmentRepository */
|
||||
$attachmentRepository = $this->entityManager->getRepository(Attachment::ENTITY_TYPE);
|
||||
|
||||
$copiedAttachment = $attachmentRepository->getCopiedAttachment($attachment);
|
||||
|
||||
return $data->with($attribute, $copiedAttachment->getId());
|
||||
}
|
||||
|
||||
private function copyAttachmentMultipleField(string $field, Data $data): Data
|
||||
{
|
||||
$attribute = $field . 'Ids';
|
||||
|
||||
$ids = $data->getValue($attribute) ?? [];
|
||||
|
||||
if (!is_array($ids)) {
|
||||
throw new RuntimeException("Bad link-multiple-ids value.");
|
||||
}
|
||||
|
||||
if (!count($ids)) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
/** @var AttachmentRepository $attachmentRepository */
|
||||
$attachmentRepository = $this->entityManager->getRepository(Attachment::ENTITY_TYPE);
|
||||
|
||||
$copiedIds = [];
|
||||
|
||||
foreach ($ids as $id) {
|
||||
$attachment = $this->entityManager->getRDBRepositoryByClass(Attachment::class)->getById($id);
|
||||
|
||||
if (!$attachment) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$copiedIds[] = $attachmentRepository
|
||||
->getCopiedAttachment($attachment)
|
||||
->getId();
|
||||
}
|
||||
|
||||
return $data->with($attribute, $copiedIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function detectFieldToCopyList(string $entityType, Data $data): array
|
||||
{
|
||||
$resultFieldList = [];
|
||||
|
||||
$fieldList = array_merge(
|
||||
$this->fieldUtil->getFieldByTypeList($entityType, FieldType::FILE),
|
||||
$this->fieldUtil->getFieldByTypeList($entityType, FieldType::IMAGE),
|
||||
$this->fieldUtil->getFieldByTypeList($entityType, FieldType::ATTACHMENT_MULTIPLE)
|
||||
);
|
||||
|
||||
foreach ($fieldList as $field) {
|
||||
$actualAttributeList = $this->fieldUtil->getActualAttributeList($entityType, $field);
|
||||
|
||||
$met = false;
|
||||
|
||||
foreach ($actualAttributeList as $attribute) {
|
||||
if ($data->getValue($attribute)) {
|
||||
$met = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($met) {
|
||||
$resultFieldList[] = $field;
|
||||
}
|
||||
}
|
||||
|
||||
return $resultFieldList;
|
||||
}
|
||||
|
||||
private function filterDisabledFields(string $entityType, stdClass $values): void
|
||||
{
|
||||
$fieldDefsList = array_filter(
|
||||
$this->entityManager
|
||||
->getDefs()
|
||||
->getEntity($entityType)
|
||||
->getFieldList(),
|
||||
fn ($it) => $it->getParam('massUpdateDisabled')
|
||||
);
|
||||
|
||||
foreach ($fieldDefsList as $fieldDefs) {
|
||||
foreach ($this->fieldUtil->getActualAttributeList($entityType, $fieldDefs->getName()) as $attribute) {
|
||||
unset($values->$attribute);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
250
application/Espo/Tools/MassUpdate/ValueMapPreparator.php
Normal file
250
application/Espo/Tools/MassUpdate/ValueMapPreparator.php
Normal file
@@ -0,0 +1,250 @@
|
||||
<?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\MassUpdate;
|
||||
|
||||
use Espo\Core\ORM\Defs\AttributeParam;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Defs as OrmDefs;
|
||||
|
||||
use Espo\Core\ORM\Entity as CoreEntity;
|
||||
use Espo\Core\Utils\ObjectUtil;
|
||||
|
||||
use stdClass;
|
||||
|
||||
class ValueMapPreparator
|
||||
{
|
||||
private OrmDefs $ormDefs;
|
||||
|
||||
public function __construct(OrmDefs $ormDefs)
|
||||
{
|
||||
$this->ormDefs = $ormDefs;
|
||||
}
|
||||
|
||||
public function prepare(Entity $entity, Data $data): stdClass
|
||||
{
|
||||
$map = (object) [];
|
||||
|
||||
$this->loadAdditionalFields($entity, $data);
|
||||
|
||||
foreach ($data->getAttributeList() as $attribute) {
|
||||
if ($data->getAction($attribute) === Action::UPDATE) {
|
||||
$map->$attribute = $data->getValue($attribute);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($data->getValue($attribute) === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$entity->has($attribute)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($data->getAction($attribute) === Action::ADD) {
|
||||
$map->$attribute = $this->prepareItemAdd($entity->get($attribute), $data->getValue($attribute));
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($data->getAction($attribute) === Action::REMOVE) {
|
||||
$map->$attribute = $this->prepareItemRemove($entity->get($attribute), $data->getValue($attribute));
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
private function loadAdditionalFields(Entity $entity, Data $data): void
|
||||
{
|
||||
if (!$entity instanceof CoreEntity) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($data->getAttributeList() as $attribute) {
|
||||
if ($entity->has($attribute)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
$entity->getAttributeParam($attribute, AttributeParam::IS_LINK_MULTIPLE_ID_LIST) &&
|
||||
$entity->getAttributeParam($attribute, 'relation')
|
||||
) {
|
||||
$field = $entity->getAttributeParam($attribute, 'relation');
|
||||
|
||||
$entity->loadLinkMultipleField($field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $set
|
||||
* @param mixed $ch
|
||||
* @return mixed
|
||||
*/
|
||||
private function prepareItemAdd($set, $ch)
|
||||
{
|
||||
if ($set === null && $ch === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_array($set) || is_array($ch)) {
|
||||
$set = $set ?? [];
|
||||
$ch = $ch ?? [];
|
||||
|
||||
if (!is_array($set) || !is_array($ch)) {
|
||||
return $set;
|
||||
}
|
||||
|
||||
return $this->prepareItemAddArray($set, $ch);
|
||||
}
|
||||
|
||||
if ($set instanceof stdClass || $ch instanceof stdClass) {
|
||||
$set = $set ?? (object) [];
|
||||
$ch = $ch ?? (object) [];
|
||||
|
||||
if (!$set instanceof stdClass || !$ch instanceof stdClass) {
|
||||
return $set;
|
||||
}
|
||||
|
||||
return $this->prepareItemAddObject($set, $ch);
|
||||
}
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $set
|
||||
* @param mixed[] $ch
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function prepareItemAddArray(array $set, array $ch): array
|
||||
{
|
||||
if ($ch === []) {
|
||||
return $set;
|
||||
}
|
||||
|
||||
$result = $set;
|
||||
|
||||
foreach ($ch as $value) {
|
||||
if (in_array($value, $result)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result[] = $value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function prepareItemAddObject(stdClass $set, stdClass $ch): stdClass
|
||||
{
|
||||
$result = ObjectUtil::clone($set);
|
||||
|
||||
foreach (get_object_vars($ch) as $key => $value) {
|
||||
$result->$key = $value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $set
|
||||
* @param mixed $ch
|
||||
* @return mixed
|
||||
*/
|
||||
private function prepareItemRemove($set, $ch)
|
||||
{
|
||||
if ($set === null && $ch === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_array($set) || is_array($ch)) {
|
||||
$set = $set ?? [];
|
||||
$ch = $ch ?? [];
|
||||
|
||||
if (!is_array($set) || !is_array($ch)) {
|
||||
return $set;
|
||||
}
|
||||
|
||||
return $this->prepareItemRemoveArray($set, $ch);
|
||||
}
|
||||
|
||||
if ($set instanceof stdClass || $ch instanceof stdClass) {
|
||||
$set = $set ?? (object) [];
|
||||
$ch = $ch ?? (object) [];
|
||||
|
||||
if (!$set instanceof stdClass || !$ch instanceof stdClass) {
|
||||
return $set;
|
||||
}
|
||||
|
||||
return $this->prepareItemRemoveObject($set, $ch);
|
||||
}
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $set
|
||||
* @param mixed[] $ch
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function prepareItemRemoveArray(array $set, array $ch): array
|
||||
{
|
||||
if ($ch === []) {
|
||||
return $set;
|
||||
}
|
||||
|
||||
$result = $set;
|
||||
|
||||
foreach ($result as $i => $value) {
|
||||
if (!in_array($value, $ch)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($result[$i]);
|
||||
}
|
||||
|
||||
return array_values($result);
|
||||
}
|
||||
|
||||
private function prepareItemRemoveObject(stdClass $set, stdClass $ch): stdClass
|
||||
{
|
||||
$result = ObjectUtil::clone($set);
|
||||
|
||||
foreach (array_keys(get_object_vars($ch)) as $key) {
|
||||
unset($result->$key);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user