From 3f2ffb851f3c6bfab6cb06d714f4fc2fc4ca43cb Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 8 Feb 2024 10:15:17 +0200 Subject: [PATCH] ref --- application/Espo/Entities/Note.php | 36 +++ application/Espo/Tools/Stream/Service.php | 276 ++++++++++------------ 2 files changed, 165 insertions(+), 147 deletions(-) diff --git a/application/Espo/Entities/Note.php b/application/Espo/Entities/Note.php index 321aa73184..4f67950ce3 100644 --- a/application/Espo/Entities/Note.php +++ b/application/Espo/Entities/Note.php @@ -29,6 +29,7 @@ namespace Espo\Entities; +use Espo\Core\Field\LinkParent; use Espo\Core\ORM\Entity; use Espo\Core\Field\DateTime; @@ -216,6 +217,41 @@ class Note extends Entity return $this->get('createdById'); } + public function setType(string $type): self + { + $this->set('type', $type); + + return $this; + } + + public function setParent(LinkParent $parent): self + { + $this->setValueObject('parent', $parent); + + return $this; + } + + public function setRelated(LinkParent $related): self + { + $this->setValueObject('related', $related); + + return $this; + } + + public function setSuperParent(LinkParent $superParent): self + { + $this->setValueObject('superParent', $superParent); + + return $this; + } + + public function setPost(?string $post): self + { + $this->set('post', $post); + + return $this; + } + public function loadAdditionalFields(): void { if ( diff --git a/application/Espo/Tools/Stream/Service.php b/application/Espo/Tools/Stream/Service.php index 7e771692f1..dc5b62ffe2 100644 --- a/application/Espo/Tools/Stream/Service.php +++ b/application/Espo/Tools/Stream/Service.php @@ -29,7 +29,8 @@ namespace Espo\Tools\Stream; - +use Espo\Core\Field\LinkParent; +use Espo\Core\ORM\Repository\Option\SaveOption; use Espo\Core\ORM\Type\FieldType; use Espo\Entities\Subscription; use Espo\Modules\Crm\Entities\Account; @@ -142,12 +143,17 @@ class Service return $this->statusStyles; } + private function getStatusField(string $entityType): ?string + { + return $this->getStatusFields()[$entityType] ?? null; + } + /** * @return array */ private function getStatusFields(): array { - if (is_null($this->statusFields)) { + if ($this->statusFields === null) { $this->statusFields = []; /** @var array> $scopes */ @@ -466,26 +472,22 @@ class Service return; } - /** @var Note $note */ - $note = $this->entityManager->getNewEntity(Note::ENTITY_TYPE); + $note = $this->getNewNote(); - $note->set('type', Note::TYPE_EMAIL_RECEIVED); - $note->set('parentId', $entity->getId()); - $note->set('parentType', $entityType); - $note->set('relatedId', $email->getId()); - $note->set('relatedType', Email::ENTITY_TYPE); + $note->setType(Note::TYPE_EMAIL_RECEIVED); + $note->setParent(LinkParent::createFromEntity($entity)); + $note->setRelated(LinkParent::create(Email::ENTITY_TYPE, $email->getId())); $this->processNoteTeamsUsers($note, $email); - if ($email->get('accountId')) { - $note->set('superParentId', $email->get('accountId')); - $note->set('superParentType', Account::ENTITY_TYPE); + if ($email->getAccount()) { + $note->setSuperParent(LinkParent::create(Account::ENTITY_TYPE, $email->getAccount()->getId())); } $withContent = in_array($entityType, $this->config->get('streamEmailWithContentEntityTypeList', [])); if ($withContent) { - $note->set('post', $email->getBodyPlain()); + $note->setPost($email->getBodyPlain()); } $data = []; @@ -519,28 +521,22 @@ class Service { $entityType = $entity->getEntityType(); - /** @var Note $note */ - $note = $this->entityManager->getNewEntity(Note::ENTITY_TYPE); + $note = $this->getNewNote(); - $note->set('type', Note::TYPE_EMAIL_SENT); - $note->set('parentId', $entity->getId()); - $note->set('parentType', $entityType); - $note->set('relatedId', $email->getId()); - $note->set('relatedType', Email::ENTITY_TYPE); + $note->setType(Note::TYPE_EMAIL_SENT); + $note->setParent(LinkParent::createFromEntity($entity)); + $note->setRelated(LinkParent::create(Email::ENTITY_TYPE, $email->getId())); $this->processNoteTeamsUsers($note, $email); - $accountLink = $email->getAccount(); - - if ($accountLink) { - $note->set('superParentId', $accountLink->getId()); - $note->set('superParentType', Account::ENTITY_TYPE); + if ($email->getAccount()) { + $note->setSuperParent(LinkParent::create(Account::ENTITY_TYPE, $email->getAccount()->getId())); } $withContent = in_array($entityType, $this->config->get('streamEmailWithContentEntityTypeList', [])); if ($withContent) { - $note->set('post', $email->getBodyPlain()); + $note->setPost($email->getBodyPlain()); } $data = []; @@ -585,20 +581,12 @@ class Service { $entityType = $entity->getEntityType(); - /** @var Note $note */ - $note = $this->entityManager->getNewEntity(Note::ENTITY_TYPE); + $note = $this->getNewNote(); - $note->set('type', Note::TYPE_CREATE); - $note->set('parentId', $entity->getId()); - $note->set('parentType', $entityType); + $note->setType(Note::TYPE_CREATE); + $note->setParent(LinkParent::createFromEntity($entity)); - if ($entity->has('accountId') && $entity->get('accountId')) { - $note->set('superParentId', $entity->get('accountId')); - $note->set('superParentType', Account::ENTITY_TYPE); - - // only if it has super parent - $this->processNoteTeamsUsers($note, $entity); - } + $this->setSuperParent($entity, $note, true); $data = []; @@ -611,13 +599,12 @@ class Service $data['assignedUserName'] = $entity->get('assignedUserName'); } - $statusFields = $this->getStatusFields(); + $field = $this->getStatusField($entityType); - if (!empty($statusFields[$entityType])) { - $field = $statusFields[$entityType]; + if ($field) { $value = $entity->get($field); - if (!empty($value)) { + if ($value) { $data['statusValue'] = $value; $data['statusField'] = $field; $data['statusStyle'] = $this->getStatusStyle($entityType, $field, $value); @@ -626,13 +613,13 @@ class Service $note->set('data', (object) $data); - $o = []; + $noteOptions = []; - if (!empty($options['createdById'])) { - $o['createdById'] = $options['createdById']; + if (!empty($options[SaveOption::CREATED_BY_ID])) { + $noteOptions[SaveOption::CREATED_BY_ID] = $options[SaveOption::CREATED_BY_ID]; } - $this->entityManager->saveEntity($note, $o); + $this->entityManager->saveEntity($note, $noteOptions); } /** @@ -673,33 +660,23 @@ class Service array $options = [] ): void { - /** @var Note $note */ - $note = $this->entityManager->getNewEntity(Note::ENTITY_TYPE); + $note = $this->getNewNote(); - $entityType = $entity->getEntityType(); - - $note->set('type', Note::TYPE_CREATE_RELATED); - $note->set('parentId', $parentId); - $note->set('parentType', $parentType); - $note->set([ - 'relatedType' => $entityType, - 'relatedId' => $entity->getId(), - ]); + $note->setType(Note::TYPE_CREATE_RELATED); + $note->setParent(LinkParent::create($parentType, $parentId)); + $note->setRelated(LinkParent::createFromEntity($entity)); $this->processNoteTeamsUsers($note, $entity); - if ($entity->has('accountId') && $entity->get('accountId')) { - $note->set('superParentId', $entity->get('accountId')); - $note->set('superParentType', Account::ENTITY_TYPE); + $this->setSuperParent($entity, $note, false); + + $noteOptions = []; + + if (!empty($options[SaveOption::CREATED_BY_ID])) { + $noteOptions[SaveOption::CREATED_BY_ID] = $options[SaveOption::CREATED_BY_ID]; } - $o = []; - - if (!empty($options['createdById'])) { - $o['createdById'] = $options['createdById']; - } - - $this->entityManager->saveEntity($note, $o); + $this->entityManager->saveEntity($note, $noteOptions); } /** @@ -725,26 +702,21 @@ class Service return; } - /** @var Note $note */ - $note = $this->entityManager->getNewEntity(Note::ENTITY_TYPE); + $note = $this->getNewNote(); - $note->set([ - 'type' => Note::TYPE_RELATE, - 'parentId' => $parentId, - 'parentType' => $parentType, - 'relatedType' => $entityType, - 'relatedId' => $entity->getId(), - ]); + $note->setType(Note::TYPE_RELATE); + $note->setParent(LinkParent::create($parentType, $parentId)); + $note->setRelated(LinkParent::createFromEntity($entity)); $this->processNoteTeamsUsers($note, $entity); - $o = []; + $noteOptions = []; - if (!empty($options['createdById'])) { - $o['createdById'] = $options['createdById']; + if (!empty($options[SaveOption::CREATED_BY_ID])) { + $noteOptions[SaveOption::CREATED_BY_ID] = $options[SaveOption::CREATED_BY_ID]; } - $this->entityManager->saveEntity($note, $o); + $this->entityManager->saveEntity($note, $noteOptions); } /** @@ -770,26 +742,21 @@ class Service return; } - /** @var Note $note */ - $note = $this->entityManager->getNewEntity(Note::ENTITY_TYPE); + $note = $this->getNewNote(); - $note->set([ - 'type' => Note::TYPE_UNRELATE, - 'parentId' => $parentId, - 'parentType' => $parentType, - 'relatedType' => $entityType, - 'relatedId' => $entity->getId(), - ]); + $note->setType(Note::TYPE_UNRELATE); + $note->setParent(LinkParent::create($parentType, $parentId)); + $note->setRelated(LinkParent::createFromEntity($entity)); $this->processNoteTeamsUsers($note, $entity); - $o = []; + $noteOptions = []; - if (!empty($options['modifiedById'])) { - $o['createdById'] = $options['modifiedById']; + if (!empty($options[SaveOption::MODIFIED_BY_ID])) { + $noteOptions[SaveOption::CREATED_BY_ID] = $options[SaveOption::MODIFIED_BY_ID]; } - $this->entityManager->saveEntity($note, $o); + $this->entityManager->saveEntity($note, $noteOptions); } /** @@ -797,20 +764,12 @@ class Service */ public function noteAssign(Entity $entity, array $options = []): void { - /** @var Note $note */ - $note = $this->entityManager->getNewEntity(Note::ENTITY_TYPE); + $note = $this->getNewNote(); - $note->set('type', Note::TYPE_ASSIGN); - $note->set('parentId', $entity->getId()); - $note->set('parentType', $entity->getEntityType()); + $note->setType(Note::TYPE_ASSIGN); + $note->setParent(LinkParent::createFromEntity($entity)); - if ($entity->has('accountId') && $entity->get('accountId')) { - $note->set('superParentId', $entity->get('accountId')); - $note->set('superParentType', Account::ENTITY_TYPE); - - // only if it has super parent - $this->processNoteTeamsUsers($note, $entity); - } + $this->setSuperParent($entity, $note, true); if ($entity->get('assignedUserId')) { if (!$entity->has('assignedUserName')) { @@ -827,17 +786,17 @@ class Service ]); } - $o = []; + $noteOptions = []; - if (!empty($options['createdById'])) { - $o['createdById'] = $options['createdById']; + if (!empty($options[SaveOption::CREATED_BY_ID])) { + $noteOptions[SaveOption::CREATED_BY_ID] = $options[SaveOption::CREATED_BY_ID]; } - if (!empty($options['modifiedById'])) { - $o['createdById'] = $options['modifiedById']; + if (!empty($options[SaveOption::MODIFIED_BY_ID])) { + $noteOptions[SaveOption::CREATED_BY_ID] = SaveOption::MODIFIED_BY_ID; } - $this->entityManager->saveEntity($note, $o); + $this->entityManager->saveEntity($note, $noteOptions); } /** @@ -845,19 +804,12 @@ class Service */ public function noteStatus(Entity $entity, string $field, array $options = []): void { - /** @var Note $note */ - $note = $this->entityManager->getNewEntity(Note::ENTITY_TYPE); + $note = $this->getNewNote(); - $note->set('type', Note::TYPE_STATUS); - $note->set('parentId', $entity->getId()); - $note->set('parentType', $entity->getEntityType()); + $note->setType(Note::TYPE_STATUS); + $note->setParent(LinkParent::createFromEntity($entity)); - if ($entity->has('accountId') && $entity->get('accountId')) { - $note->set('superParentId', $entity->get('accountId')); - $note->set('superParentType', Account::ENTITY_TYPE); - - $this->processNoteTeamsUsers($note, $entity); - } + $this->setSuperParent($entity, $note, true); $entityType = $entity->getEntityType(); @@ -871,17 +823,17 @@ class Service 'style' => $style, ]); - $o = []; + $noteOptions = []; - if (!empty($options['createdById'])) { - $o['createdById'] = $options['createdById']; + if (!empty($options[SaveOption::CREATED_BY_ID])) { + $noteOptions[SaveOption::CREATED_BY_ID] = $options[SaveOption::CREATED_BY_ID]; } - if (!empty($options['modifiedById'])) { - $o['createdById'] = $options['modifiedById']; + if (!empty($options[SaveOption::MODIFIED_BY_ID])) { + $noteOptions[SaveOption::CREATED_BY_ID] = SaveOption::MODIFIED_BY_ID; } - $this->entityManager->saveEntity($note, $o); + $this->entityManager->saveEntity($note, $noteOptions); } /** @@ -898,8 +850,6 @@ class Service { $entityType = $entity->getEntityType(); - $statusFields = $this->getStatusFields(); - if (array_key_exists($entityType, $this->auditedFieldsCache)) { return $this->auditedFieldsCache[$entityType]; } @@ -907,6 +857,8 @@ class Service /** @var array> $fields */ $fields = $this->metadata->get(['entityDefs', $entityType, 'fields']); + $hasStream = (bool) $this->metadata->get("scopes.$entityType.stream"); + $auditedFields = []; foreach ($fields as $field => $defs) { @@ -914,7 +866,7 @@ class Service continue; } - if (!empty($statusFields[$entityType]) && $statusFields[$entityType] === $field) { + if ($hasStream && $this->getStatusField($entityType) === $field) { continue; } @@ -925,15 +877,11 @@ class Service continue; } - $auditedFields[$field] = []; - - $auditedFields[$field]['actualList'] = - $this->fieldUtil->getActualAttributeList($entityType, $field); - - $auditedFields[$field]['notActualList'] = - $this->fieldUtil->getNotActualAttributeList($entityType, $field); - - $auditedFields[$field]['fieldType'] = $type; + $auditedFields[$field] = [ + 'actualList' => $this->fieldUtil->getActualAttributeList($entityType, $field), + 'notActualList' => $this->fieldUtil->getNotActualAttributeList($entityType, $field), + 'fieldType' => $type, + ]; } $this->auditedFieldsCache[$entityType] = $auditedFields; @@ -997,8 +945,12 @@ class Service $wasParentType = $was[$field . 'Type']; $wasParentId = $was[$field . 'Id']; - if ($wasParentType && $wasParentId && $this->entityManager->hasRepository($wasParentType)) { - $wasParent = $this->entityManager->getEntity($wasParentType, $wasParentId); + if ( + $wasParentType && + $wasParentId && + $this->entityManager->hasRepository($wasParentType) + ) { + $wasParent = $this->entityManager->getEntityById($wasParentType, $wasParentId); if ($wasParent) { $was[$field . 'Name'] = $wasParent->get('name'); @@ -1011,12 +963,10 @@ class Service return; } - /** @var Note $note */ - $note = $this->entityManager->getNewEntity(Note::ENTITY_TYPE); + $note = $this->getNewNote(); - $note->set('type', Note::TYPE_UPDATE); - $note->set('parentId', $entity->getId()); - $note->set('parentType', $entity->getEntityType()); + $note->setType(Note::TYPE_UPDATE); + $note->setParent(LinkParent::createFromEntity($entity)); $note->set('data', [ 'fields' => $updatedFieldList, @@ -1428,4 +1378,36 @@ class Service /** @var EmailAddressRepository */ return $this->entityManager->getRepository(EmailAddress::ENTITY_TYPE); } + + private function setSuperParent(Entity $entity, Note $note, bool $processTeamsUsers): void + { + $accountId = $entity->get('accountId'); + + if (!$accountId) { + return; + } + + $entityDefs = $this->entityManager + ->getDefs() + ->getEntity($entity->getEntityType()); + + $foreignEntityType = $entityDefs->tryGetRelation('account')?->tryGetForeignEntityType(); + + if ($foreignEntityType !== Account::ENTITY_TYPE) { + return; + } + + $note->setSuperParent(LinkParent::create(Account::ENTITY_TYPE, $accountId)); + + if ($processTeamsUsers) { + // only if it has super parent + $this->processNoteTeamsUsers($note, $entity); + } + } + + private function getNewNote(): Note + { + /** @var Note */ + return $this->entityManager->getNewEntity(Note::ENTITY_TYPE); + } }