From a930f571ba00121a7b4d4e2f012e244d27fd0feb Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 23 Apr 2014 14:37:30 +0300 Subject: [PATCH] create related stream --- application/Espo/Hooks/Common/Stream.php | 29 +++++++++-- application/Espo/Services/Stream.php | 18 +++++++ .../templates/stream/notes/create-related.tpl | 18 +++++++ .../src/views/stream/notes/create-related.js | 51 +++++++++++++++++++ 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 frontend/client/res/templates/stream/notes/create-related.tpl create mode 100644 frontend/client/src/views/stream/notes/create-related.js 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}} +
  • +{{/unless}} + +
    + + {{{createdBy}}} {{translate action category='relateActions'}} {{relatedTypeString}} {{entityName}} {{translate 'on' category='stream'}} + {{#if isUserStream}} {{parentTypeString}} {{{parent}}} {{else}} {{translate 'this' category='stream'}} {{parentTypeString}}{{/if}} + +
    + +
    + {{{createdAt}}} +
    + +{{#unless onlyContent}} +
  • +{{/unless}} diff --git a/frontend/client/src/views/stream/notes/create-related.js b/frontend/client/src/views/stream/notes/create-related.js new file mode 100644 index 0000000000..4edc8db402 --- /dev/null +++ b/frontend/client/src/views/stream/notes/create-related.js @@ -0,0 +1,51 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko + * Website: http://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + ************************************************************************/ + +Espo.define('Views.Stream.Notes.CreateRelated', 'Views.Stream.Note', function (Dep) { + + return Dep.extend({ + + template: 'stream.notes.create-related', + + data: function () { + return _.extend({ + entityType: this.entityType, + entityId: this.entityId, + entityName: this.entityName, + action: this.action, + relatedTypeString: this.translate(this.entityType, 'scopeNames').toLowerCase() + }, Dep.prototype.data.call(this)); + }, + + setup: function () { + if (this.model.get('data')) { + var data = JSON.parse(this.model.get('data')); + + this.entityType = data.entityType || null; + this.entityId = data.entityId || null; + this.entityName = data.entityName || null; + this.action = data.action || null; + + } + }, + }); +}); +