diff --git a/application/Espo/Hooks/Common/Stream.php b/application/Espo/Hooks/Common/Stream.php index f3299b8bf8..393ba90193 100644 --- a/application/Espo/Hooks/Common/Stream.php +++ b/application/Espo/Hooks/Common/Stream.php @@ -59,6 +59,24 @@ class Stream extends \Espo\Core\Hooks\Base } } + protected function handleCreateRelated(Entity $entity) + { + $relationDefs = $entity->getRelations(); + + foreach ($relationDefs as $relation => $defs) { + if ($defs['type'] == 'belongsTo') { + $field = $relation . 'Id'; + $scope = $defs['entity']; + if ($entity->has($field)) { + $entityId = $entity->get($field); + if (!empty($entityId) && $this->getMetadata()->get("scopes.{$scope}.stream")) { + $this->getStreamService()->noteCreateRelated($entity, $scope, $entityId); + } + } + } + } + } + public function afterSave(Entity $entity) { $entityName = $entity->getEntityName(); @@ -76,7 +94,8 @@ class Stream extends \Espo\Core\Hooks\Base if (!empty($assignedUserId) && $createdById != $assignedUserId) { $this->getStreamService()->followEntity($entity, $assignedUserId); } - $this->getStreamService()->noteCreate($entity); + $this->getStreamService()->noteCreate($entity); + } else { if ($entity->isFieldChanged('assignedUserId')) { $assignedUserId = $entity->get('assignedUserId'); @@ -94,9 +113,13 @@ class Stream extends \Espo\Core\Hooks\Base $this->getStreamService()->noteStatus($entity, $field); } } - } + } - } + } + + /*if (!$entity->isFetched() && $this->getMetadata()->get("scopes.{$entityName}.tab")) { + $this->handleCreateRelated($entity); + }*/ } protected function getStreamService() diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index b84a1d372f..68f7d58134 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -319,6 +319,24 @@ class Stream extends \Espo\Core\Services\Base $this->getEntityManager()->saveEntity($note); } + public function noteCreateRelated(Entity $entity, $entityType, $id, $action = 'created') + { + $note = $this->getEntityManager()->getEntity('Note'); + + $note->set('type', 'CreateRelated'); + $note->set('parentId', $id); + $note->set('parentType', $entityType); + + $note->set('data', json_encode(array( + 'action' => $action, + 'entityType' => $entity->getEntityName(), + 'entityId' => $entity->id, + 'entityName' => $entity->get('name') + ))); + + $this->getEntityManager()->saveEntity($note); + } + public function noteAssign(Entity $entity) { $note = $this->getEntityManager()->getEntity('Note'); diff --git a/frontend/client/res/templates/stream/notes/create-related.tpl b/frontend/client/res/templates/stream/notes/create-related.tpl new file mode 100644 index 0000000000..9bb7835610 --- /dev/null +++ b/frontend/client/res/templates/stream/notes/create-related.tpl @@ -0,0 +1,18 @@ +{{#unless onlyContent}} +