From c415ce677df5a310fa8473ee32099deb3a04ac36 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 21 Nov 2019 11:09:57 +0200 Subject: [PATCH] stream noteRelate --- application/Espo/Hooks/Common/Stream.php | 39 ++---------------------- application/Espo/Services/Stream.php | 33 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/application/Espo/Hooks/Common/Stream.php b/application/Espo/Hooks/Common/Stream.php index 885f2a4681..24ace0e74d 100644 --- a/application/Espo/Hooks/Common/Stream.php +++ b/application/Espo/Hooks/Common/Stream.php @@ -371,48 +371,13 @@ class Stream extends \Espo\Core\Hooks\Base if ( $this->getMetadata()->get(['entityDefs', $entityType, 'links', $link, 'audited']) - ) { - $n = $this->getEntityManager()->getRepository('Note')->where(array( - 'type' => 'Relate', - 'parentId' => $entity->id, - 'parentType' => $entityType, - 'relatedId' => $foreignEntity->id, - 'relatedType' => $foreignEntity->getEntityType() - ))->findOne(); - if (!$n) { - $note = $this->getEntityManager()->getEntity('Note'); - $note->set(array( - 'type' => 'Relate', - 'parentId' => $entity->id, - 'parentType' => $entityType, - 'relatedId' => $foreignEntity->id, - 'relatedType' => $foreignEntity->getEntityType() - )); - $this->getEntityManager()->saveEntity($note); - } + $this->getStreamService()->noteRelate($foreignEntity, $entityType, $entity->id); } $foreignLink = $entity->getRelationParam($link, 'foreign'); if ($this->getMetadata()->get(['entityDefs', $foreignEntity->getEntityType(), 'links', $foreignLink, 'audited'])) { - $n = $this->getEntityManager()->getRepository('Note')->where(array( - 'type' => 'Relate', - 'parentId' => $foreignEntity->id, - 'parentType' => $foreignEntity->getEntityType(), - 'relatedId' => $entity->id, - 'relatedType' => $entityType - ))->findOne(); - if (!$n) { - $note = $this->getEntityManager()->getEntity('Note'); - $note->set(array( - 'type' => 'Relate', - 'parentId' => $foreignEntity->id, - 'parentType' => $foreignEntity->getEntityType(), - 'relatedId' => $entity->id, - 'relatedType' => $entityType - )); - $this->getEntityManager()->saveEntity($note); - } + $this->getStreamService()->noteRelate($entity, $foreignEntity->getEntityType(), $foreignEntity->id); } } } diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index c2c7836686..9dc347bce5 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -1321,6 +1321,39 @@ class Stream extends \Espo\Core\Services\Base $this->getEntityManager()->saveEntity($note, $o); } + public function noteRelate(Entity $entity, $parentType, $parentId, array $options = []) + { + $entityType = $entity->getEntityType(); + + $existing = $this->getEntityManager()->getRepository('Note')->select(['id'])->where([ + 'type' => 'Relate', + 'parentId' => $parentId, + 'parentType' => $parentType, + 'relatedId' => $entity->id, + 'relatedType' => $entityType, + ])->findOne(); + if ($existing) return false; + + $note = $this->getEntityManager()->getEntity('Note'); + + $note->set([ + 'type' => 'Relate', + 'parentId' => $parentId, + 'parentType' => $parentType, + 'relatedType' => $entityType, + 'relatedId' => $entity->id, + ]); + + $this->processNoteTeamsUsers($note, $entity); + + $o = []; + if (!empty($options['createdById'])) { + $o['createdById'] = $options['createdById']; + } + + $this->getEntityManager()->saveEntity($note, $o); + } + public function noteAssign(Entity $entity, array $options = []) { $note = $this->getEntityManager()->getEntity('Note');