diff --git a/application/Espo/Core/SelectManagers/Base.php b/application/Espo/Core/SelectManagers/Base.php index 3948cfa81e..d32e1da184 100644 --- a/application/Espo/Core/SelectManagers/Base.php +++ b/application/Espo/Core/SelectManagers/Base.php @@ -313,6 +313,26 @@ class Base } } + public function manageAccess(&$result) + { + if (empty($result)) { + $result = array(); + } + if (empty($result['joins'])) { + $result['joins'] = []; + } + if (empty($result['leftJoins'])) { + $result['leftJoins'] = []; + } + if (empty($result['whereClause'])) { + $result['whereClause'] = array(); + } + if (empty($result['customJoin'])) { + $result['customJoin'] = []; + } + $this->access($result); + } + protected function access(&$result) { if ($this->acl->checkReadOnlyOwn($this->entityType)) { diff --git a/application/Espo/Resources/i18n/en_US/InboundEmail.json b/application/Espo/Resources/i18n/en_US/InboundEmail.json index 0848c4300f..985de1b120 100644 --- a/application/Espo/Resources/i18n/en_US/InboundEmail.json +++ b/application/Espo/Resources/i18n/en_US/InboundEmail.json @@ -1,6 +1,7 @@ { "fields": { "name": "Name", + "emailAddress": "Email Address", "team": "Team", "status": "Status", "assignToUser": "Assign to User", diff --git a/application/Espo/Resources/layouts/InboundEmail/detail.json b/application/Espo/Resources/layouts/InboundEmail/detail.json index 629a3ac1bd..5ffa7153ac 100644 --- a/application/Espo/Resources/layouts/InboundEmail/detail.json +++ b/application/Espo/Resources/layouts/InboundEmail/detail.json @@ -3,13 +3,17 @@ "label":"Main", "rows":[ [ - {"name":"name"}, + {"name":"emailAddress"}, {"name":"status"} ], [ - {"name":"team"}, + {"name":"name"}, {"name":"replyToAddress"} ], + [ + {"name":"team"}, + false + ], [ {"name":"assignToUser"}, false diff --git a/application/Espo/Resources/metadata/entityDefs/InboundEmail.json b/application/Espo/Resources/metadata/entityDefs/InboundEmail.json index c7c91b0311..c4f7bf0322 100644 --- a/application/Espo/Resources/metadata/entityDefs/InboundEmail.json +++ b/application/Espo/Resources/metadata/entityDefs/InboundEmail.json @@ -4,6 +4,12 @@ "type": "varchar", "required": true }, + "emailAddress": { + "type": "varchar", + "required": true, + "maxLength": 100, + "view": "EmailAccount.Fields.EmailAddress" + }, "status": { "type": "enum", "options": ["Active", "Inactive"] diff --git a/application/Espo/Services/EmailAddress.php b/application/Espo/Services/EmailAddress.php index 81ffc70303..a1b00d0c7d 100644 --- a/application/Espo/Services/EmailAddress.php +++ b/application/Espo/Services/EmailAddress.php @@ -28,19 +28,34 @@ use \Espo\Core\Exceptions\Error; class EmailAddress extends Record { - private function findInAddressBookByEntityType($where, $limit, $entityType, &$result) + + protected function findInAddressBookByEntityType($query, $limit, $entityType, &$result) { - $service = $this->getServiceFactory()->create($entityType); + $whereClause = array( + 'OR' => array( + array( + 'name*' => $query . '%' + ), + array( + 'emailAddress*' => $query . '%' + ) + ), + array( + 'emailAddress!=' => null + ) + ); - $r = $service->findEntities(array( - 'where' => $where, - 'maxSize' => $limit, - 'sortBy' => 'name' - )); + $searchParams = array( + 'whereClause' => $whereClause, + 'orderBy' => 'name', + 'limit' => $limit + ); - foreach ($r['collection'] as $entity) { - $entity->loadLinkMultipleField('emailAddress'); + $this->getSelectManagerFactory()->create($entityType)->manageAccess($searchParams); + $collection = $this->getEntityManager()->getRepository($entityType)->find($searchParams); + + foreach ($collection as $entity) { $emailAddress = $entity->get('emailAddress'); $result[] = array( @@ -50,8 +65,7 @@ class EmailAddress extends Record 'entityId' => $entity->id ); - $c = $service->getEntity($entity->id); - $emailAddressData = $c->get('emailAddressData'); + $emailAddressData = $this->getEntityManager()->getRepository('EmailAddress')->getEmailAddressData($entity); foreach ($emailAddressData as $d) { if ($emailAddress != $d->emailAddress) { $emailAddress = $d->emailAddress; @@ -67,37 +81,38 @@ class EmailAddress extends Record } } + protected function findInInboundEmail($query, $limit, &$result) + { + $pdo = $this->getEntityManager()->getPDO(); + $qu = $this->getEntityManager()->getQuery()->createSelectQuery('InboundEmail', [ + 'select' => ['id', 'name', 'emailAddress'], + 'whereClause' => [ + 'emailAddress*' => $query . '%' + ], + 'orderBy' => 'name', + ]); + + $sth = $pdo->prepare($qu); + $sth->execute(); + while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { + $result[] = [ + 'emailAddress' => $row['emailAddress'], + 'entityName' => $row['name'], + 'entityType' => 'InboundEmail', + 'entityId' => $row['id'] + ]; + } + } public function searchInAddressBook($query, $limit) { - $result = array(); + $result = []; - $where = array( - array( - 'type' => 'or', - 'value' => array( - array( - 'type' => 'like', - 'field' => 'name', - 'value' => $query . '%' - ), - array( - 'type' => 'like', - 'field' => 'emailAddress', - 'value' => $query . '%' - ) - ) - ), - array( - 'type' => 'notEquals', - 'field' => 'emailAddress', - 'value' => null - ) - ); - - $this->findInAddressBookByEntityType($where, $limit, 'Contact', $result); - $this->findInAddressBookByEntityType($where, $limit, 'Lead', $result); - $this->findInAddressBookByEntityType($where, $limit, 'User', $result); + $this->findInAddressBookByEntityType($query, $limit, 'Contact', $result); + $this->findInAddressBookByEntityType($query, $limit, 'Lead', $result); + $this->findInAddressBookByEntityType($query, $limit, 'User', $result); + $this->findInAddressBookByEntityType($query, $limit, 'Account', $result); + $this->findInInboundEmail($query, $limit, $result); $final = array();