Initial commit
This commit is contained in:
@@ -0,0 +1,260 @@
|
||||
<?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\Core\FieldProcessing\Relation;
|
||||
|
||||
use Espo\Core\ORM\Entity as CoreEntity;
|
||||
|
||||
use Espo\Core\FieldProcessing\Saver\Params;
|
||||
use Espo\Core\ORM\EntityManager;
|
||||
use Espo\Core\ORM\Repository\Option\SaveContext;
|
||||
use Espo\Core\ORM\Repository\Option\SaveOption;
|
||||
use Espo\ORM\Defs\Params\RelationParam;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Saves a link-multiple field or has-many relation set in a link stub attribute.
|
||||
* In case of a stab attribute, only processed for a new record.
|
||||
*/
|
||||
class LinkMultipleSaver
|
||||
{
|
||||
private const RELATE_OPTION = 'linkMultiple';
|
||||
|
||||
public function __construct(private EntityManager $entityManager)
|
||||
{}
|
||||
|
||||
public function process(CoreEntity $entity, string $name, Params $params): void
|
||||
{
|
||||
$idListAttribute = $name . 'Ids';
|
||||
$columnsAttribute = $name . 'Columns';
|
||||
|
||||
if (
|
||||
!$entity->isNew() &&
|
||||
!$entity->hasLinkMultipleField($name)
|
||||
) {
|
||||
$entity->clear($idListAttribute);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$isChanged = $entity->isAttributeChanged($idListAttribute) || $entity->isAttributeChanged($columnsAttribute);
|
||||
|
||||
if (!$isChanged) {
|
||||
return;
|
||||
}
|
||||
|
||||
$defs = $this->entityManager->getDefs()->getEntity($entity->getEntityType());
|
||||
|
||||
$skipCreate = $params->getOption('skipLinkMultipleCreate') ?? false;
|
||||
$skipRemove = $params->getOption('skipLinkMultipleRemove') ?? false;
|
||||
$skipUpdate = $params->getOption('skipLinkMultipleUpdate') ?? false;
|
||||
$skipHooks = $params->getOption('skipLinkMultipleHooks') ?? false;
|
||||
|
||||
if ($entity->isNew()) {
|
||||
$skipRemove = true;
|
||||
$skipUpdate = true;
|
||||
}
|
||||
|
||||
if ($entity->has($idListAttribute)) {
|
||||
$specifiedIdList = $entity->get($idListAttribute);
|
||||
} else if ($entity->has($columnsAttribute)) {
|
||||
$skipRemove = true;
|
||||
|
||||
$specifiedIdList = array_keys(
|
||||
get_object_vars(
|
||||
$entity->get($columnsAttribute) ?? (object) []
|
||||
)
|
||||
);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_array($specifiedIdList)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$toRemoveIdList = [];
|
||||
$existingIdList = [];
|
||||
$toUpdateIdList = [];
|
||||
$toCreateIdList = [];
|
||||
|
||||
$existingColumnsData = (object) [];
|
||||
|
||||
$columns = null;
|
||||
|
||||
if ($defs->hasField($name)) {
|
||||
$columns = $defs->getField($name)->getParam('columns');
|
||||
}
|
||||
|
||||
$allColumns = $columns;
|
||||
|
||||
if (is_array($columns)) {
|
||||
$additionalColumns = $defs->getRelation($name)->getParam(RelationParam::ADDITIONAL_COLUMNS) ?? [];
|
||||
|
||||
foreach ($columns as $column => $field) {
|
||||
if (!array_key_exists($column, $additionalColumns)) {
|
||||
unset($columns[$column]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$repository = $this->entityManager->getRDBRepository($entity->getEntityType());
|
||||
|
||||
$columnData = !empty($columns) ?
|
||||
$entity->get($columnsAttribute) :
|
||||
null;
|
||||
|
||||
if (!$skipRemove || !$skipUpdate) {
|
||||
$foreignEntityList = $repository->getRelation($entity, $name)->find();
|
||||
|
||||
foreach ($foreignEntityList as $foreignEntity) {
|
||||
$existingIdList[] = $foreignEntity->getId();
|
||||
|
||||
if (empty($allColumns)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = (object) [];
|
||||
|
||||
$foreignId = $foreignEntity->getId();
|
||||
|
||||
foreach ($allColumns as $columnName => $columnField) {
|
||||
$data->$columnName = $foreignEntity->get($columnField);
|
||||
}
|
||||
|
||||
$existingColumnsData->$foreignId = $data;
|
||||
|
||||
if (!$entity->isNew()) {
|
||||
$entity->setFetched($columnsAttribute, $existingColumnsData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$entity->isNew()) {
|
||||
if ($entity->has($idListAttribute) && !$entity->hasFetched($idListAttribute)) {
|
||||
$entity->setFetched($idListAttribute, $existingIdList);
|
||||
}
|
||||
|
||||
if ($entity->has($columnsAttribute) && !empty($allColumns)) {
|
||||
$entity->setFetched($columnsAttribute, $existingColumnsData);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($existingIdList as $id) {
|
||||
if (!in_array($id, $specifiedIdList)) {
|
||||
if (!$skipRemove) {
|
||||
$toRemoveIdList[] = $id;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($skipUpdate || empty($columns)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($columns as $columnName => $columnField) {
|
||||
if (!isset($columnData->$id) || !is_object($columnData->$id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
property_exists($columnData->$id, $columnName) &&
|
||||
(
|
||||
!property_exists($existingColumnsData->$id, $columnName) ||
|
||||
$columnData->$id->$columnName !== $existingColumnsData->$id->$columnName
|
||||
)
|
||||
) {
|
||||
$toUpdateIdList[] = $id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$skipCreate) {
|
||||
foreach ($specifiedIdList as $id) {
|
||||
if (!in_array($id, $existingIdList)) {
|
||||
$toCreateIdList[] = $id;
|
||||
}
|
||||
|
||||
if (!is_string($id)) {
|
||||
throw new RuntimeException("Non-string ID in link-multiple.");
|
||||
}
|
||||
|
||||
if ($id === '') {
|
||||
throw new RuntimeException("An entity ID value in link-multiple.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$saveContext = SaveContext::obtainFromRawOptions($params->getRawOptions());
|
||||
|
||||
foreach ($toCreateIdList as $id) {
|
||||
$data = null;
|
||||
|
||||
if (is_array($columns) && isset($columnData->$id)) {
|
||||
$data = (array) $columnData->$id;
|
||||
|
||||
foreach ($data as $column => $v) {
|
||||
if (!array_key_exists($column, $columns)) {
|
||||
unset($data[$column]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$repository->getRelation($entity, $name)->relateById($id, $data, [
|
||||
SaveOption::SKIP_HOOKS => $skipHooks,
|
||||
SaveOption::SILENT => $entity->isNew(),
|
||||
self::RELATE_OPTION => $entity->hasLinkMultipleField($name),
|
||||
SaveContext::NAME => $saveContext,
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($toRemoveIdList as $id) {
|
||||
$repository->getRelation($entity, $name)->unrelateById($id, [
|
||||
SaveOption::SKIP_HOOKS => $skipHooks,
|
||||
self::RELATE_OPTION => $entity->hasLinkMultipleField($name),
|
||||
SaveContext::NAME => $saveContext,
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($toUpdateIdList as $id) {
|
||||
$data = (array) $columnData->$id;
|
||||
|
||||
if (is_array($columns)) {
|
||||
foreach ($data as $column => $v) {
|
||||
if (!array_key_exists($column, $columns)) {
|
||||
unset($data[$column]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$repository->getRelation($entity, $name)->updateColumnsById($id, $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
357
application/Espo/Core/FieldProcessing/Relation/Saver.php
Normal file
357
application/Espo/Core/FieldProcessing/Relation/Saver.php
Normal file
@@ -0,0 +1,357 @@
|
||||
<?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\Core\FieldProcessing\Relation;
|
||||
|
||||
use Espo\Core\ORM\Defs\AttributeParam;
|
||||
use Espo\Core\ORM\Entity as CoreEntity;
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
use Espo\Core\FieldProcessing\Saver as SaverInterface;
|
||||
use Espo\Core\FieldProcessing\Saver\Params;
|
||||
use Espo\Core\ORM\EntityManager;
|
||||
use Espo\ORM\Name\Attribute;
|
||||
use Espo\ORM\Repository\Option\SaveOption;
|
||||
|
||||
/**
|
||||
* @implements SaverInterface<Entity>
|
||||
*/
|
||||
class Saver implements SaverInterface
|
||||
{
|
||||
private EntityManager $entityManager;
|
||||
private LinkMultipleSaver $linkMultipleSaver;
|
||||
|
||||
/** @var array<string, string[]> */
|
||||
private $manyRelationListMapCache = [];
|
||||
/** @var array<string, string[]> */
|
||||
private $hasOneRelationListMapCache = [];
|
||||
/** @var array<string, string[]> */
|
||||
private $belongsToHasOneRelationListMapCache = [];
|
||||
|
||||
public function __construct(
|
||||
EntityManager $entityManager,
|
||||
LinkMultipleSaver $linkMultipleSaver
|
||||
) {
|
||||
$this->entityManager = $entityManager;
|
||||
$this->linkMultipleSaver = $linkMultipleSaver;
|
||||
}
|
||||
|
||||
public function process(Entity $entity, Params $params): void
|
||||
{
|
||||
$this->processMany($entity, $params);
|
||||
$this->processHasOne($entity);
|
||||
$this->processBelongsToHasOne($entity);
|
||||
}
|
||||
|
||||
private function processMany(Entity $entity, Params $params): void
|
||||
{
|
||||
$entityType = $entity->getEntityType();
|
||||
|
||||
foreach ($this->getManyRelationList($entityType) as $name) {
|
||||
$this->processManyItem($entity, $name, $params);
|
||||
}
|
||||
}
|
||||
|
||||
private function processManyItem(Entity $entity, string $name, Params $params): void
|
||||
{
|
||||
$idsAttribute = $name . 'Ids';
|
||||
$columnsAttribute = $name . 'Columns';
|
||||
|
||||
if (!$entity->has($idsAttribute) && !$entity->has($columnsAttribute)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$entity instanceof CoreEntity) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->linkMultipleSaver->process($entity, $name, $params);
|
||||
}
|
||||
|
||||
private function processHasOne(Entity $entity): void
|
||||
{
|
||||
$entityType = $entity->getEntityType();
|
||||
|
||||
foreach ($this->getHasOneRelationList($entityType) as $name) {
|
||||
$this->processHasOneItem($entity, $name);
|
||||
}
|
||||
}
|
||||
|
||||
private function processHasOneItem(Entity $entity, string $name): void
|
||||
{
|
||||
$entityType = $entity->getEntityType();
|
||||
$idAttribute = $name . 'Id';
|
||||
|
||||
if (!$entity->has($idAttribute) || !$entity->isAttributeChanged($idAttribute)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var ?string $id */
|
||||
$id = $entity->get($idAttribute);
|
||||
|
||||
$defs = $this->entityManager->getDefs()->getEntity($entityType);
|
||||
$relationDefs = $defs->getRelation($name);
|
||||
|
||||
$foreignKey = $relationDefs->getForeignKey();
|
||||
$foreignEntityType = $relationDefs->getForeignEntityType();
|
||||
|
||||
$previous = $this->entityManager
|
||||
->getRDBRepository($foreignEntityType)
|
||||
->select([Attribute::ID])
|
||||
->where([$foreignKey => $entity->getId()])
|
||||
->findOne();
|
||||
|
||||
if (!$entity->isNew() && !$entity->hasFetched($idAttribute)) {
|
||||
$entity->setFetched($idAttribute, $previous ? $previous->getId() : null);
|
||||
}
|
||||
|
||||
if ($previous) {
|
||||
if (!$id) {
|
||||
$this->entityManager
|
||||
->getRelation($entity, $name)
|
||||
->unrelate($previous);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($previous->getId() === $id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->entityManager
|
||||
->getRelation($entity, $name)
|
||||
->relateById($id);
|
||||
}
|
||||
|
||||
private function processBelongsToHasOne(Entity $entity): void
|
||||
{
|
||||
$entityType = $entity->getEntityType();
|
||||
|
||||
foreach ($this->getBelongsToHasOneRelationList($entityType) as $name) {
|
||||
$this->processBelongsToHasOneItem($entity, $name);
|
||||
}
|
||||
}
|
||||
|
||||
private function processBelongsToHasOneItem(Entity $entity, string $name): void
|
||||
{
|
||||
$entityType = $entity->getEntityType();
|
||||
|
||||
$idAttribute = $name . 'Id';
|
||||
|
||||
if (!$entity->get($idAttribute)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$entity->isAttributeChanged($idAttribute)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$anotherEntity = $this->entityManager
|
||||
->getRDBRepository($entityType)
|
||||
->select([Attribute::ID])
|
||||
->where([
|
||||
$idAttribute => $entity->get($idAttribute),
|
||||
Attribute::ID . '!=' => $entity->getId(),
|
||||
])
|
||||
->findOne();
|
||||
|
||||
if (!$anotherEntity) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @noinspection PhpRedundantOptionalArgumentInspection */
|
||||
$anotherEntity->set($idAttribute, null);
|
||||
|
||||
$this->entityManager->saveEntity($anotherEntity, [
|
||||
SaveOption::SKIP_ALL => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function getManyRelationList(string $entityType): array
|
||||
{
|
||||
if (array_key_exists($entityType, $this->manyRelationListMapCache)) {
|
||||
return $this->manyRelationListMapCache[$entityType];
|
||||
}
|
||||
|
||||
$typeList = [
|
||||
Entity::HAS_MANY,
|
||||
Entity::MANY_MANY,
|
||||
Entity::HAS_CHILDREN,
|
||||
];
|
||||
|
||||
$defs = $this->entityManager->getDefs()->getEntity($entityType);
|
||||
|
||||
$list = [];
|
||||
|
||||
foreach ($defs->getRelationNameList() as $name) {
|
||||
$type = $defs->getRelation($name)->getType();
|
||||
|
||||
if (!in_array($type, $typeList)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$idsAttribute = $name . 'Ids';
|
||||
|
||||
if (!$defs->hasAttribute($idsAttribute)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attributeDefs = $defs->getAttribute($idsAttribute);
|
||||
|
||||
if (
|
||||
!$attributeDefs->getParam(AttributeParam::IS_LINK_MULTIPLE_ID_LIST) &&
|
||||
!$attributeDefs->getParam(AttributeParam::IS_LINK_STUB)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($defs->hasField($name) && $defs->getField($name)->getParam('noSave')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$list[] = $name;
|
||||
}
|
||||
|
||||
$this->manyRelationListMapCache[$entityType] = $list;
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function getHasOneRelationList(string $entityType): array
|
||||
{
|
||||
if (array_key_exists($entityType, $this->hasOneRelationListMapCache)) {
|
||||
return $this->hasOneRelationListMapCache[$entityType];
|
||||
}
|
||||
|
||||
$ormDefs = $this->entityManager->getDefs();
|
||||
|
||||
$defs = $ormDefs->getEntity($entityType);
|
||||
|
||||
$list = [];
|
||||
|
||||
foreach ($defs->getRelationNameList() as $name) {
|
||||
$relationDefs = $defs->getRelation($name);
|
||||
|
||||
$type = $relationDefs->getType();
|
||||
|
||||
if ($type !== Entity::HAS_ONE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$relationDefs->hasForeignEntityType()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$relationDefs->hasForeignKey()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$defs->hasAttribute($name . 'Id')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($defs->hasField($name) && $defs->getField($name)->getParam('noSave')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$list[] = $name;
|
||||
}
|
||||
|
||||
$this->hasOneRelationListMapCache[$entityType] = $list;
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function getBelongsToHasOneRelationList(string $entityType): array
|
||||
{
|
||||
if (array_key_exists($entityType, $this->belongsToHasOneRelationListMapCache)) {
|
||||
return $this->belongsToHasOneRelationListMapCache[$entityType];
|
||||
}
|
||||
|
||||
$ormDefs = $this->entityManager->getDefs();
|
||||
|
||||
$defs = $ormDefs->getEntity($entityType);
|
||||
|
||||
$list = [];
|
||||
|
||||
foreach ($defs->getRelationNameList() as $name) {
|
||||
$relationDefs = $defs->getRelation($name);
|
||||
|
||||
$type = $relationDefs->getType();
|
||||
|
||||
if ($type !== Entity::BELONGS_TO) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$relationDefs->hasForeignRelationName()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$relationDefs->hasForeignEntityType()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$defs->hasAttribute($name . 'Id')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$foreignEntityType = $relationDefs->getForeignEntityType();
|
||||
$foreignRelationName = $relationDefs->getForeignRelationName();
|
||||
|
||||
$foreignType = $ormDefs
|
||||
->getEntity($foreignEntityType)
|
||||
->tryGetRelation($foreignRelationName)
|
||||
?->getType();
|
||||
|
||||
if ($foreignType !== Entity::HAS_ONE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$list[] = $name;
|
||||
}
|
||||
|
||||
$this->belongsToHasOneRelationListMapCache[$entityType] = $list;
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user