diff --git a/application/Espo/Hooks/Common/Stream.php b/application/Espo/Hooks/Common/Stream.php index b21ad3def0..4e341b60cd 100644 --- a/application/Espo/Hooks/Common/Stream.php +++ b/application/Espo/Hooks/Common/Stream.php @@ -73,6 +73,12 @@ class Stream extends \Espo\Core\Hooks\Base if ($this->checkHasStream($entity)) { $this->getStreamService()->unfollowAllUsersFromEntity($entity); } + $query = $this->getEntityManager()->getQuery(); + $sql = " + DELETE FROM `note` + WHERE related_id = ".$query->quote($entity->id)." AND related_type = ".$query->quote($entity->getEntityType()) ." + "; + $this->getEntityManager()->getPDO()->query($sql); } protected function handleCreateRelated(Entity $entity) diff --git a/application/Espo/Resources/metadata/entityDefs/Note.json b/application/Espo/Resources/metadata/entityDefs/Note.json index 260a2cf6ee..b31f517b3d 100644 --- a/application/Espo/Resources/metadata/entityDefs/Note.json +++ b/application/Espo/Resources/metadata/entityDefs/Note.json @@ -4,13 +4,20 @@ "type": "text" }, "data": { - "type": "jsonObject" + "type": "jsonObject", + "readOnly": true }, "type": { - "type": "varchar" + "type": "varchar", + "readOnly": true }, "parent": { - "type": "linkParent" + "type": "linkParent", + "readOnly": true + }, + "related": { + "type": "linkParent", + "readOnly": true }, "attachments": { "type": "linkMultiple", @@ -18,7 +25,8 @@ }, "number": { "type": "autoincrement", - "index": true + "index": true, + "readOnly": true }, "createdAt": { "type": "datetime", @@ -58,6 +66,9 @@ }, "superParent": { "type": "belongsToParent" + }, + "related": { + "type": "belongsToParent" } }, "collection": { diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index 84728e6f5d..07e4d5d962 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -279,6 +279,8 @@ class Stream extends \Espo\Core\Services\Base note.data AS 'data', note.parent_type AS 'parentType', note.parent_id AS 'parentId', + note.related_type AS 'relatedType', + note.related_id AS 'relatedId', note.created_at AS 'createdAt', note.created_by_id AS 'createdById', TRIM(CONCAT(createdBy.first_name, ' ', createdBy.last_name)) AS `createdByName` @@ -305,6 +307,8 @@ class Stream extends \Espo\Core\Services\Base note.data AS 'data', note.parent_type AS 'parentType', note.parent_id AS 'parentId', + note.related_type AS 'relatedType', + note.related_id AS 'relatedId', note.created_at AS 'createdAt', note.created_by_id AS 'createdById', TRIM(CONCAT(createdBy.first_name, ' ', createdBy.last_name)) AS `createdByName` @@ -357,6 +361,12 @@ class Stream extends \Espo\Core\Services\Base $e->set('parentName', $entity->get('name')); } } + if ($e->get('relatedId') && $e->get('relatedType')) { + $entity = $this->getEntityManager()->getEntity($e->get('relatedType'), $e->get('relatedId')); + if ($entity) { + $e->set('relatedName', $entity->get('name')); + } + } } if (count($collection) > $maxSize) { @@ -427,7 +437,12 @@ class Stream extends \Espo\Core\Services\Base $e->set('parentName', $parent->get('name')); } } - + } + if ($e->get('relatedId') && $e->get('relatedType')) { + $entity = $this->getEntityManager()->getEntity($e->get('relatedType'), $e->get('relatedId')); + if ($entity) { + $e->set('relatedName', $entity->get('name')); + } } } @@ -575,13 +590,13 @@ class Stream extends \Espo\Core\Services\Base $data['assignedUserName'] = $entity->get('assignedUserName'); } - $statusStyles = $this->getStatusStyles(); $statusFields = $this->getStatusFields(); if (!empty($statusFields[$entityType])) { $field = $statusFields[$entityType]; $value = $entity->get($field); if (!empty($value)) { + $statusStyles = $this->getStatusStyles(); $style = 'default'; if (!empty($statusStyles[$entityType]) && !empty($statusStyles[$entityType][$value])) { $style = $statusStyles[$entityType][$value]; @@ -597,26 +612,25 @@ class Stream extends \Espo\Core\Services\Base $this->getEntityManager()->saveEntity($note); } - public function noteCreateRelated(Entity $entity, $entityType, $id, $action = 'created') + public function noteCreateRelated(Entity $entity, $parentType, $parentId) { $note = $this->getEntityManager()->getEntity('Note'); + $entityType = $entity->getEntityType(); + $note->set('type', 'CreateRelated'); - $note->set('parentId', $id); - $note->set('parentType', $entityType); + $note->set('parentId', $parentId); + $note->set('parentType', $parentType); + $note->set(array( + 'relatedType' => $entityType, + 'relatedId' => $entity->id, + )); if ($entity->has('accountId') && $entity->get('accountId')) { $note->set('superParentId', $entity->get('accountId')); $note->set('superParentType', 'Account'); } - $note->set('data', array( - 'action' => $action, - 'entityType' => $entity->getEntityName(), - 'entityId' => $entity->id, - 'entityName' => $entity->get('name') - )); - $this->getEntityManager()->saveEntity($note); } diff --git a/frontend/client/res/templates/stream/notes/create-related.tpl b/frontend/client/res/templates/stream/notes/create-related.tpl index 94501925aa..192eb148df 100644 --- a/frontend/client/res/templates/stream/notes/create-related.tpl +++ b/frontend/client/res/templates/stream/notes/create-related.tpl @@ -13,6 +13,9 @@ {{{avatar}}}