diff --git a/application/Espo/Modules/Crm/Services/Activities.php b/application/Espo/Modules/Crm/Services/Activities.php index 14595a8eab..064d928bcd 100644 --- a/application/Espo/Modules/Crm/Services/Activities.php +++ b/application/Espo/Modules/Crm/Services/Activities.php @@ -187,7 +187,7 @@ class Activities implements */ protected function getActivitiesUserCallQuery(UserEntity $entity, array $statusList = []): Select { - $seed = $this->entityManager->getEntity('Call'); + $seed = $this->entityManager->getNewEntity('Call'); $builder = $this->selectBuilderFactory ->create() @@ -325,7 +325,7 @@ class Activities implements return $this->$methodName($entity, $statusList); } - $seed = $this->entityManager->getEntity($targetEntityType); + $seed = $this->entityManager->getNewEntity($targetEntityType); $baseBuilder = $this->selectBuilderFactory ->create() @@ -1070,7 +1070,7 @@ class Activities implements 'createdAt', ]; - $seed = $this->entityManager->getEntity('Meeting'); + $seed = $this->entityManager->getNewEntity('Meeting'); $additionalAttributeList = $this->metadata->get( ['app', 'calendar', 'additionalAttributeList'] @@ -1131,7 +1131,7 @@ class Activities implements 'createdAt', ]; - $seed = $this->entityManager->getEntity('Call'); + $seed = $this->entityManager->getNewEntity('Call'); $additionalAttributeList = $this->metadata->get( ['app', 'calendar', 'additionalAttributeList'] @@ -1192,7 +1192,7 @@ class Activities implements 'createdAt', ]; - $seed = $this->entityManager->getEntity('Task'); + $seed = $this->entityManager->getNewEntity('Task'); $additionalAttributeList = $this->metadata->get( ['app', 'calendar', 'additionalAttributeList'] @@ -1263,7 +1263,7 @@ class Activities implements $builder->withStrictAccessControl(); } - $seed = $this->entityManager->getEntity($scope); + $seed = $this->entityManager->getNewEntity($scope); $select = [ ['"' . $scope . '"', 'scope'], @@ -1406,7 +1406,7 @@ class Activities implements */ protected function getActivitiesBaseQuery(Entity $entity, string $scope, array $statusList = []): Select { - $seed = $this->entityManager->getEntity($scope); + $seed = $this->entityManager->getNewEntity($scope); $builder = $this->selectBuilderFactory ->create() @@ -1642,7 +1642,7 @@ class Activities implements } if ($this->acl->get('userPermission') === 'team') { - $userTeamIdList = $this->user->getLinkMultipleIdList('teams'); + $userTeamIdList = $this->user->getLinkMultipleIdList('teams') ?? []; foreach ($teamIdList as $teamId) { if (!in_array($teamId, $userTeamIdList)) { @@ -2022,6 +2022,10 @@ class Activities implements $user = $this->entityManager->getEntity('User', $userId); + if (!$user) { + throw new NotFound(); + } + $this->accessCheck($user); if (!$entityTypeList) { @@ -2108,6 +2112,12 @@ class Activities implements foreach ($rows as $row) { $entity = $this->entityManager->getEntity($row['entityType'], $row['id']); + if (!$entity) { + $entity = $this->entityManager->getNewEntity($row['entityType']); + + $entity->set('id', $row['id']); + } + $entityData = $entity->getValueMap(); $entityData->_scope = $entity->getEntityType(); diff --git a/application/Espo/Modules/Crm/Services/Campaign.php b/application/Espo/Modules/Crm/Services/Campaign.php index 2b32a84e60..53a50a90f9 100644 --- a/application/Espo/Modules/Crm/Services/Campaign.php +++ b/application/Espo/Modules/Crm/Services/Campaign.php @@ -83,7 +83,7 @@ class Campaign extends Record implements $actionDate = date('Y-m-d H:i:s'); } - $logRecord = $this->entityManager->getEntity('CampaignLogRecord'); + $logRecord = $this->entityManager->getNewEntity('CampaignLogRecord'); $logRecord->set([ 'campaignId' => $campaignId, @@ -111,7 +111,7 @@ class Campaign extends Record implements $actionDate = date('Y-m-d H:i:s'); } - $logRecord = $this->entityManager->getEntity('CampaignLogRecord'); + $logRecord = $this->entityManager->getNewEntity('CampaignLogRecord'); $logRecord->set([ 'campaignId' => $campaignId, @@ -162,7 +162,7 @@ class Campaign extends Record implements $actionDate = date('Y-m-d H:i:s'); } - $logRecord = $this->entityManager->getEntity('CampaignLogRecord'); + $logRecord = $this->entityManager->getNewEntity('CampaignLogRecord'); $logRecord->set([ 'campaignId' => $campaignId, @@ -214,7 +214,7 @@ class Campaign extends Record implements $emailAddress = $target->get('emailAddress'); } - $logRecord = $this->entityManager->getEntity('CampaignLogRecord'); + $logRecord = $this->entityManager->getNewEntity('CampaignLogRecord'); $logRecord->set([ 'campaignId' => $campaignId, @@ -257,7 +257,7 @@ class Campaign extends Record implements $actionDate = date('Y-m-d H:i:s'); } - $logRecord = $this->entityManager->getEntity('CampaignLogRecord'); + $logRecord = $this->entityManager->getNewEntity('CampaignLogRecord'); $logRecord->set([ 'campaignId' => $campaignId, @@ -305,7 +305,7 @@ class Campaign extends Record implements $massEmail = $this->entityManager->getEntity('MassEmail', $queueItem->get('massEmailId')); if ($massEmail && $massEmail->hasId()) { - $logRecord = $this->entityManager->getEntity('CampaignLogRecord'); + $logRecord = $this->entityManager->getNewEntity('CampaignLogRecord'); $logRecord->set([ 'campaignId' => $campaignId, @@ -357,7 +357,7 @@ class Campaign extends Record implements $actionDate = date('Y-m-d H:i:s'); } - $logRecord = $this->entityManager->getEntity('CampaignLogRecord'); + $logRecord = $this->entityManager->getNewEntity('CampaignLogRecord'); $logRecord->set([ 'campaignId' => $campaignId, @@ -419,7 +419,7 @@ class Campaign extends Record implements $campaign->loadLinkMultipleField('targetLists'); $campaign->loadLinkMultipleField('excludingTargetLists'); - if (count($campaign->getLinkMultipleIdList('targetLists')) === 0) { + if (count($campaign->getLinkMultipleIdList('targetLists') ?? []) === 0) { throw new Error("Could not mail merge campaign w/o any specified target list."); } diff --git a/application/Espo/Modules/Crm/Services/CaseObj.php b/application/Espo/Modules/Crm/Services/CaseObj.php index 8f6bd94dff..5df1d76b9c 100644 --- a/application/Espo/Modules/Crm/Services/CaseObj.php +++ b/application/Espo/Modules/Crm/Services/CaseObj.php @@ -201,7 +201,7 @@ class CaseObj extends Record */ protected function getContactEmailAddressList(CaseEntity $entity): array { - $contactIdList = $entity->getLinkMultipleIdList('contacts'); + $contactIdList = $entity->getLinkMultipleIdList('contacts') ?? []; if (!count($contactIdList)) { return []; diff --git a/application/Espo/Modules/Crm/Services/KnowledgeBaseArticle.php b/application/Espo/Modules/Crm/Services/KnowledgeBaseArticle.php index 1f0088df38..58e5db0d0e 100644 --- a/application/Espo/Modules/Crm/Services/KnowledgeBaseArticle.php +++ b/application/Espo/Modules/Crm/Services/KnowledgeBaseArticle.php @@ -67,7 +67,6 @@ class KnowledgeBaseArticle extends Record implements throw new BadRequest(); } - /** @var KnowledgeBaseArticleEntity|null $entity */ $entity = $this->entityManager->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { @@ -87,7 +86,8 @@ class KnowledgeBaseArticle extends Record implements $source = $this->entityManager->getEntity('Attachment', $attachmentId); if ($source) { - $attachment = $this->entityManager->getEntity('Attachment'); + $attachment = $this->entityManager->getNewEntity('Attachment'); + $attachment->set('role', 'Attachment'); $attachment->set('type', $source->get('type')); $attachment->set('size', $source->get('size')); @@ -129,6 +129,8 @@ class KnowledgeBaseArticle extends Record implements */ public function moveUp(string $id, ?array $where = null): void { + assert($this->entityType !== null); + $entity = $this->entityManager->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { @@ -191,6 +193,8 @@ class KnowledgeBaseArticle extends Record implements */ public function moveDown(string $id, ?array $where = null): void { + assert($this->entityType !== null); + $entity = $this->entityManager->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { @@ -253,6 +257,8 @@ class KnowledgeBaseArticle extends Record implements */ public function moveToTop(string $id, ?array $where = null): void { + assert($this->entityType !== null); + $entity = $this->entityManager->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { @@ -312,6 +318,8 @@ class KnowledgeBaseArticle extends Record implements */ public function moveToBottom(string $id, ?array $where = null): void { + assert($this->entityType !== null); + $entity = $this->entityManager->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { diff --git a/application/Espo/Modules/Crm/Services/Lead.php b/application/Espo/Modules/Crm/Services/Lead.php index c8ed39d066..005ca3f45e 100644 --- a/application/Espo/Modules/Crm/Services/Lead.php +++ b/application/Espo/Modules/Crm/Services/Lead.php @@ -66,22 +66,22 @@ class Lead extends Record implements protected function afterCreateEntity(Entity $entity, $data) { if (!empty($data->emailId)) { - $email = $this->getEntityManager()->getEntity('Email', $data->emailId); + $email = $this->entityManager->getEntity('Email', $data->emailId); - if ($email && !$email->get('parentId') && $this->getAcl()->check($email)) { + if ($email && !$email->get('parentId') && $this->acl->check($email)) { $email->set([ 'parentType' => 'Lead', 'parentId' => $entity->getId(), ]); - $this->getEntityManager()->saveEntity($email); + $this->entityManager->saveEntity($email); } } if ($entity->get('campaignId')) { - $campaign = $this->getEntityManager()->getEntity('Campaign', $entity->get('campaignId')); + $campaign = $this->entityManager->getEntity('Campaign', $entity->get('campaignId')); if ($campaign) { - $log = $this->getEntityManager()->getEntity('CampaignLogRecord'); + $log = $this->entityManager->getNewEntity('CampaignLogRecord'); $log->set([ 'action' => 'Lead Created', @@ -91,7 +91,7 @@ class Lead extends Record implements 'campaignId' => $campaign->getId(), ]); - $this->getEntityManager()->saveEntity($log); + $this->entityManager->saveEntity($log); } } } @@ -105,7 +105,7 @@ class Lead extends Record implements /** @var LeadEntity */ $lead = $this->getEntity($id); - if (!$this->getAcl()->check($lead, 'read')) { + if (!$this->acl->check($lead, 'read')) { throw new Forbidden(); } @@ -120,7 +120,7 @@ class Lead extends Record implements $convertFieldsDefs = $this->metadata->get('entityDefs.Lead.convertFields', []); foreach ($entityList as $entityType) { - if (!$this->getAcl()->checkScope($entityType, 'edit')) { + if (!$this->acl->checkScope($entityType, 'edit')) { continue; } @@ -217,7 +217,7 @@ class Lead extends Record implements $leadAttribute = $leadAttributeList[$i] ?? null; - if (!$lead->has($leadAttribute)) { + if (!$leadAttribute || !$lead->has($leadAttribute)) { continue; } @@ -241,19 +241,20 @@ class Lead extends Record implements $additionalData = $additionalData ?? (object) []; - if (!$this->getAcl()->check($lead, 'edit')) { + if (!$this->acl->check($lead, 'edit')) { throw new Forbidden(); } $duplicateList = []; $duplicateCheck = !($additionalData->skipDuplicateCheck ?? false); - $entityManager = $this->getEntityManager(); + $entityManager = $this->entityManager; $skipSave = false; if (!empty($recordsData->Account)) { - $account = $entityManager->getEntity('Account'); + $account = $entityManager->getNewEntity('Account'); + $account->set(get_object_vars($recordsData->Account)); if ($duplicateCheck) { @@ -281,7 +282,8 @@ class Lead extends Record implements } if (!empty($recordsData->Contact)) { - $contact = $entityManager->getEntity('Contact'); + $contact = $entityManager->getNewEntity('Contact'); + $contact->set(get_object_vars($recordsData->Contact)); if (isset($account)) { @@ -315,7 +317,8 @@ class Lead extends Record implements } if (!empty($recordsData->Opportunity)) { - $opportunity = $entityManager->getEntity('Opportunity'); + $opportunity = $entityManager->getNewEntity('Opportunity'); + $opportunity->set(get_object_vars($recordsData->Opportunity)); if (isset($account)) { diff --git a/application/Espo/Modules/Crm/Services/Meeting.php b/application/Espo/Modules/Crm/Services/Meeting.php index 9a7b156450..096d8d0992 100644 --- a/application/Espo/Modules/Crm/Services/Meeting.php +++ b/application/Espo/Modules/Crm/Services/Meeting.php @@ -209,11 +209,14 @@ class Meeting extends Record implements */ public function massSetHeld(array $ids): bool { + assert($this->entityType !== null); + foreach ($ids as $id) { $entity = $this->getEntityManager()->getEntity($this->entityType, $id); if ($entity && $this->getAcl()->check($entity, 'edit')) { $entity->set('status', 'Held'); + $this->getEntityManager()->saveEntity($entity); } } @@ -226,6 +229,8 @@ class Meeting extends Record implements */ public function massSetNotHeld(array $ids): bool { + assert($this->entityType !== null); + foreach ($ids as $id) { $entity = $this->getEntityManager()->getEntity($this->entityType, $id); diff --git a/application/Espo/Modules/Crm/Services/Opportunity.php b/application/Espo/Modules/Crm/Services/Opportunity.php index 888bd93465..67ef898e89 100644 --- a/application/Espo/Modules/Crm/Services/Opportunity.php +++ b/application/Espo/Modules/Crm/Services/Opportunity.php @@ -321,6 +321,9 @@ class Opportunity extends Record list($dateFrom, $dateTo) = $this->getDateRangeByFilter($dateFilter); } + /** @var string $dateFrom */ + /** @var string $dateTo */ + $queryBuilder = $this->selectBuilderFactory ->create() ->from(OpportunityEntity::ENTITY_TYPE) @@ -645,7 +648,7 @@ class Opportunity extends Record */ protected function getContactEmailAddressList(OpportunityEntity $entity): array { - $contactIdList = $entity->getLinkMultipleIdList('contacts'); + $contactIdList = $entity->getLinkMultipleIdList('contacts') ?? []; if (!count($contactIdList)) { return []; diff --git a/application/Espo/Modules/Crm/Services/Target.php b/application/Espo/Modules/Crm/Services/Target.php index 1c6eefddd1..dce3dce72a 100644 --- a/application/Espo/Modules/Crm/Services/Target.php +++ b/application/Espo/Modules/Crm/Services/Target.php @@ -30,6 +30,7 @@ namespace Espo\Modules\Crm\Services; use Espo\Core\Exceptions\Forbidden; +use Espo\Core\Exceptions\NotFound; use Espo\ORM\Entity; @@ -42,16 +43,23 @@ class Target extends \Espo\Services\Record public function convert(string $id): Entity { $entityManager = $this->getEntityManager(); + $target = $this->getEntity($id); + if (!$target) { + throw new NotFound(); + } + if (!$this->getAcl()->check($target, 'delete')) { throw new Forbidden(); } + if (!$this->getAcl()->check('Lead', 'read')) { throw new Forbidden(); } - $lead = $entityManager->getEntity('Lead'); + $lead = $entityManager->getNewEntity('Lead'); + $lead->set($target->getValueMap()); $entityManager->removeEntity($target); diff --git a/application/Espo/Modules/Crm/Services/TargetList.php b/application/Espo/Modules/Crm/Services/TargetList.php index b08773f1c6..99fa49a959 100644 --- a/application/Espo/Modules/Crm/Services/TargetList.php +++ b/application/Espo/Modules/Crm/Services/TargetList.php @@ -358,7 +358,7 @@ class TargetList extends Record implements ->create(); while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { - $itemEntity = $this->entityManager->getEntity($row['entityType']); + $itemEntity = $this->entityManager->getNewEntity($row['entityType']); $itemEntity->set($row); $itemEntity->setAsFetched();