From 300198c9fa144c367d4f526d416b65f8b2bbd69e Mon Sep 17 00:00:00 2001 From: yuri Date: Thu, 22 Sep 2016 11:24:21 +0300 Subject: [PATCH] duplicate with links --- .../Espo/Modules/Crm/Services/TargetList.php | 2 ++ application/Espo/Services/Record.php | 30 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/application/Espo/Modules/Crm/Services/TargetList.php b/application/Espo/Modules/Crm/Services/TargetList.php index 3e397dd151..480bbee5f9 100644 --- a/application/Espo/Modules/Crm/Services/TargetList.php +++ b/application/Espo/Modules/Crm/Services/TargetList.php @@ -37,6 +37,8 @@ class TargetList extends \Espo\Services\Record { protected $noEditAccessRequiredLinkList = ['accounts', 'contacts', 'leads', 'users']; + protected $duplicatingLinkList = ['accounts', 'contacts', 'leads', 'users']; + public function loadAdditionalFields(Entity $entity) { parent::loadAdditionalFields($entity); diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index d8133d1b83..c7e8834125 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -81,6 +81,8 @@ class Record extends \Espo\Core\Services\Base protected $checkForDuplicatesInUpdate = false; + protected $duplicatingLinkList = []; + const FOLLOWERS_LIMIT = 4; public function __construct() @@ -542,6 +544,7 @@ class Record extends \Espo\Core\Services\Base if ($this->storeEntity($entity)) { $this->afterCreate($entity, $data); + $this->afterCreateProcessDuplicating($entity, $data); $this->prepareEntityForOutput($entity); return $entity; } @@ -549,7 +552,6 @@ class Record extends \Espo\Core\Services\Base throw new Error(); } - public function updateEntity($id, $data) { unset($data['deleted']); @@ -1501,7 +1503,33 @@ class Record extends \Espo\Core\Services\Base } } + $attributes['_duplicatingEntityId'] = $id; + return $attributes; } + + protected function afterCreateProcessDuplicating(Entity $entity, $data) + { + if (isset($data['_duplicatingEntityId'])) { + $this->duplicateLinks($entity, $data['_duplicatingEntityId']); + } + } + + protected function duplicateLinks(Entity $entity, $duplicatingEntityId) + { + if (!$duplicatingEntityId) return; + $duplicatingEntity = $this->getEntityManager()->getEntity($entity->getEntityType(), $duplicatingEntityId); + if (!$duplicatingEntity) return; + if (!$this->getAcl()->check($duplicatingEntity, 'read')) return; + + $repository = $this->getRepository(); + + foreach ($this->duplicatingLinkList as $link) { + $linkedList = $repository->findRelated($duplicatingEntity, $link); + foreach ($linkedList as $linked) { + $repository->relate($entity, $link, $linked); + } + } + } }