diff --git a/application/Espo/Core/SelectManagers/Base.php b/application/Espo/Core/SelectManagers/Base.php index 2aa8cd5cf1..ba0d30c7ea 100644 --- a/application/Espo/Core/SelectManagers/Base.php +++ b/application/Espo/Core/SelectManagers/Base.php @@ -52,6 +52,11 @@ class Base $this->metadata = $metadata; } + protected function getUser() + { + return $this->user; + } + public function setEntityName($entityName) { $this->entityName = $entityName; @@ -112,12 +117,10 @@ class Base if (!empty($p)) { $params['where'][] = $p; } + $this->boolFilter($filter, $result); } } else if ($item['type'] == 'textFilter' && !empty($item['value'])) { if (!empty($item['value'])) { - if (empty($result['whereClause'])) { - $result['whereClause'] = array(); - } $fieldDefs = $this->getSeed()->getFields(); $fieldList = $this->getTextFilterFields(); $d = array(); @@ -191,17 +194,13 @@ class Base } - $result['whereClause'] = $where; + $result['whereClause'] = array_merge($result['whereClause'], $where); } } protected function q($params, &$result) { if (!empty($params['q'])) { - if (empty($result['whereClause'])) { - $result['whereClause'] = array(); - } - $fieldDefs = $this->getSeed()->getFields(); $value = $params['q']; @@ -229,33 +228,34 @@ class Base protected function access(&$result) { if ($this->acl->checkReadOnlyOwn($this->entityName)) { - - if (!array_key_exists('whereClause', $result)) { - $result['whereClause'] = array(); - } - $result['whereClause']['assignedUserId'] = $this->user->id; + $this->accessOnlyOwn($result); } if (!$this->user->isAdmin() && $this->acl->checkReadOnlyTeam($this->entityName)) { - if (!array_key_exists('whereClause', $result)) { - $result['whereClause'] = array(); - } - $result['distinct'] = true; - if (!array_key_exists('joins', $result)) { - $result['joins'] = array(); - } - if (!in_array('teams', $result['joins'])) { - $result['leftJoins'][] = 'teams'; - } - - $result['whereClause'][] = array( - 'OR' => array( - 'Team.id' => $this->user->get('teamsIds'), - 'assignedUserId' => $this->user->id - ) - ); + $this->accessOnlyTeam($result); } } + protected function accessOnlyOwn(&$result) + { + $result['whereClause'][] = array( + 'assignedUserId' => $this->getUser()->id + ); + } + + protected function accessOnlyTeam(&$result) + { + $result['distinct'] = true; + if (!in_array('teams', $result['joins'])) { + $result['leftJoins'][] = 'teams'; + } + $result['whereClause'][] = array( + 'OR' => array( + 'Team.id' => $this->user->get('teamsIds'), + 'assignedUserId' => $this->getUser()->id + ) + ); + } + public function getAclParams() { $result = array(); @@ -265,7 +265,11 @@ class Base public function getSelectParams(array $params, $withAcl = false) { - $result = array(); + $result = array( + 'joins' => array(), + 'leftJoins' => array(), + 'whereClause' => array() + ); $this->order($params, $result); $this->limit($params, $result); @@ -411,6 +415,14 @@ class Base return $part; } + protected function boolFilter($filterName, &$result) + { + $method = 'boolFilter' . ucfirst($filterName); + if (method_exists($this, $method)) { + $this->$method($result); + } + } + protected function getBoolFilterWhere($filterName) { $method = 'getBoolFilterWhere' . ucfirst($filterName); @@ -419,12 +431,10 @@ class Base } } - protected function getBoolFilterWhereOnlyMy() + protected function boolFilterOnlyMy(&$result) { - return array( - 'type' => 'equals', - 'field' => 'assignedUserId', - 'value' => $this->user->id, + $result['whereClause'][] = array( + 'assignedUserId' => $this->getUser()->id ); } } diff --git a/application/Espo/Modules/Crm/SelectManagers/Call.php b/application/Espo/Modules/Crm/SelectManagers/Call.php index 81334184b2..2abae97a29 100644 --- a/application/Espo/Modules/Crm/SelectManagers/Call.php +++ b/application/Espo/Modules/Crm/SelectManagers/Call.php @@ -18,19 +18,19 @@ * * You should have received a copy of the GNU General Public License * along with EspoCRM. If not, see http://www.gnu.org/licenses/. - ************************************************************************/ + ************************************************************************/ namespace Espo\Modules\Crm\SelectManagers; class Call extends \Espo\Core\SelectManagers\Base { - - protected function getBoolFilterWhereOnlyMy() - { - return array( - 'type' => 'linkedWith', - 'field' => 'users', - 'value' => array($this->user->id) + protected function boolFilterOnlyMy(&$result) + { + if (!in_array('users', $result['joins'])) { + $result['joins'][] = 'users'; + } + $result['whereClause'][] = array( + 'user.id' => $this->getUser()->id ); } } diff --git a/application/Espo/Modules/Crm/SelectManagers/Meeting.php b/application/Espo/Modules/Crm/SelectManagers/Meeting.php index 8b24a4bab4..447d281850 100644 --- a/application/Espo/Modules/Crm/SelectManagers/Meeting.php +++ b/application/Espo/Modules/Crm/SelectManagers/Meeting.php @@ -18,19 +18,19 @@ * * You should have received a copy of the GNU General Public License * along with EspoCRM. If not, see http://www.gnu.org/licenses/. - ************************************************************************/ + ************************************************************************/ namespace Espo\Modules\Crm\SelectManagers; class Meeting extends \Espo\Core\SelectManagers\Base { - - protected function getBoolFilterWhereOnlyMy() - { - return array( - 'type' => 'linkedWith', - 'field' => 'users', - 'value' => array($this->user->id) + protected function boolFilterOnlyMy(&$result) + { + if (!in_array('users', $result['joins'])) { + $result['joins'][] = 'users'; + } + $result['whereClause'][] = array( + 'user.id' => $this->getUser()->id ); } } diff --git a/application/Espo/Modules/Crm/SelectManagers/Task.php b/application/Espo/Modules/Crm/SelectManagers/Task.php index 6addc0ef7b..340117e883 100644 --- a/application/Espo/Modules/Crm/SelectManagers/Task.php +++ b/application/Espo/Modules/Crm/SelectManagers/Task.php @@ -24,22 +24,17 @@ namespace Espo\Modules\Crm\SelectManagers; class Task extends \Espo\Core\SelectManagers\Base { - - protected function getBoolFilterWhereActive() - { - return array( - 'type' => 'notIn', - 'field' => 'status', - 'value' => array('Completed', 'Canceled') + protected function boolFilterActive(&$result) + { + $result['whereClause'][] = array( + 'status!=' => array('Completed', 'Canceled') ); } - - protected function getBoolFilterWhereInactive() - { - return array( - 'type' => 'in', - 'field' => 'status', - 'value' => array('Completed', 'Canceled') + + protected function boolFilterInative(&$result) + { + $result['whereClause'][] = array( + 'status' => array('Completed', 'Canceled') ); } } diff --git a/application/Espo/ORM/DB/Query/Base.php b/application/Espo/ORM/DB/Query/Base.php index 2e8564a0fc..3b82a69679 100644 --- a/application/Espo/ORM/DB/Query/Base.php +++ b/application/Espo/ORM/DB/Query/Base.php @@ -173,10 +173,12 @@ class Base } if (empty($params['aggregation'])) { - return $this->composeSelectQuery($this->toDb($entity->getEntityName()), $selectPart, $joinsPart, $wherePart, $orderPart, $params['offset'], $params['limit'], $params['distinct'], null, $groupByPart); + $sql = $this->composeSelectQuery($this->toDb($entity->getEntityName()), $selectPart, $joinsPart, $wherePart, $orderPart, $params['offset'], $params['limit'], $params['distinct'], null, $groupByPart); } else { - return $this->composeSelectQuery($this->toDb($entity->getEntityName()), $selectPart, $joinsPart, $wherePart, null, null, null, false, $params['aggregation']); + $sql = $this->composeSelectQuery($this->toDb($entity->getEntityName()), $selectPart, $joinsPart, $wherePart, null, null, null, false, $params['aggregation']); } + + return $sql; } protected function getFunctionPart($function, $part, $entityName, $distinct = false) diff --git a/application/Espo/Repositories/Email.php b/application/Espo/Repositories/Email.php index 211bc0df63..0ecd650253 100644 --- a/application/Espo/Repositories/Email.php +++ b/application/Espo/Repositories/Email.php @@ -18,7 +18,7 @@ * * You should have received a copy of the GNU General Public License * along with EspoCRM. If not, see http://www.gnu.org/licenses/. - ************************************************************************/ + ************************************************************************/ namespace Espo\Repositories; @@ -29,39 +29,69 @@ class Email extends \Espo\Core\ORM\Repositories\RDB protected function prepareAddressess(Entity $entity, $type) { $eaRepositoty = $this->getEntityManager()->getRepository('EmailAddress'); - + $address = $entity->get($type); $ids = array(); if (!empty($address) || !filter_var($address, FILTER_VALIDATE_EMAIL)) { $arr = array_map(function ($e) { return trim($e); }, explode(';', $address)); - + $ids = $eaRepositoty->getIds($arr); + foreach ($ids as $id) { + $this->setUsersIdsByEmailAddressId($entity, $id); + } } $entity->set($type . 'EmailAddressesIds', $ids); } - + + protected function setUsersIdsByEmailAddressId(Entity $entity, $emailAddressId) + { + $user = $this->getEntityManager()->getRepository('EmailAddress')->getEntityByAddressId($emailAddressId, 'User'); + if ($user) { + $usersIds = $entity->get('usersIds'); + if (empty($usersIds)) { + $usersIds = array(); + } + if (!in_array($user->id, $usersIds)) { + $usersIds[] = $user->id; + } + $entity->set('usersIds', $usersIds); + } + } + protected function beforeSave(Entity $entity) { $eaRepositoty = $this->getEntityManager()->getRepository('EmailAddress'); - + + $entity->set('usersIds', array()); + $from = trim($entity->get('from')); if (!empty($from)) { $ids = $eaRepositoty->getIds(array($from)); if (!empty($ids)) { $entity->set('fromEmailAddressId', $ids[0]); + $this->setUsersIdsByEmailAddressId($entity, $ids[0]); } } else { $entity->set('fromEmailAddressId', null); } - + $this->prepareAddressess($entity, 'to'); $this->prepareAddressess($entity, 'cc'); $this->prepareAddressess($entity, 'bcc'); - + + $usersIds = $entity->get('usersIds'); + $assignedUserId = $entity->get('assignedUserId'); + + if (!empty($assignedUserId) && !in_array($assignedUserId, $usersIds)) { + $usersIds[] = $assignedUserId; + } + $entity->set('usersIds', $usersIds); + parent::beforeSave($entity); - + + $parentId = $entity->get('parentId'); $parentType = $entity->get('parentType'); if (!empty($parentId) || !empty($parentType)) { @@ -80,7 +110,7 @@ class Email extends \Espo\Core\ORM\Repositories\RDB // TODO find account by from address } } - + protected function beforeRemove(Entity $entity) { parent::beforeRemove($entity); diff --git a/application/Espo/Repositories/EmailAddress.php b/application/Espo/Repositories/EmailAddress.php index 5317e1485b..0b26092b53 100644 --- a/application/Espo/Repositories/EmailAddress.php +++ b/application/Espo/Repositories/EmailAddress.php @@ -18,7 +18,7 @@ * * You should have received a copy of the GNU General Public License * along with EspoCRM. If not, see http://www.gnu.org/licenses/. - ************************************************************************/ + ************************************************************************/ namespace Espo\Repositories; @@ -27,8 +27,8 @@ use Espo\ORM\Entity; class EmailAddress extends \Espo\Core\ORM\Repositories\RDB { public function getIds($arr = array()) - { - $ids = array(); + { + $ids = array(); if (!empty($arr)) { $a = array_map(function ($item) { return strtolower($item); @@ -58,20 +58,20 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB } return $ids; } - + public function getEmailAddressData(Entity $entity) { $data = array(); - - $pdo = $this->getEntityManager()->getPDO(); + + $pdo = $this->getEntityManager()->getPDO(); $sql = " - SELECT email_address.name, email_address.invalid, email_address.opt_out AS optOut, entity_email_address.primary + SELECT email_address.name, email_address.invalid, email_address.opt_out AS optOut, entity_email_address.primary FROM entity_email_address JOIN email_address ON email_address.id = entity_email_address.email_address_id AND email_address.deleted = 0 - WHERE - entity_email_address.entity_id = ".$pdo->quote($entity->id)." AND - entity_email_address.entity_type = ".$pdo->quote($entity->getEntityName())." AND - entity_email_address.deleted = 0 + WHERE + entity_email_address.entity_id = ".$pdo->quote($entity->id)." AND + entity_email_address.entity_type = ".$pdo->quote($entity->getEntityName())." AND + entity_email_address.deleted = 0 ORDER BY entity_email_address.primary DESC "; $sth = $pdo->prepare($sql); @@ -82,27 +82,56 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB $obj->emailAddress = $row['name']; $obj->primary = ($row['primary'] == '1') ? true : false; $obj->optOut = ($row['optOut'] == '1') ? true : false; - $obj->invalid = ($row['invalid'] == '1') ? true : false; + $obj->invalid = ($row['invalid'] == '1') ? true : false; $data[] = $obj; } } - + return $data; } - + public function getByAddress($address) { return $this->where(array('lower' => strtolower($address)))->findOne(); } - - public function getEntityByAddress($address) + + public function getEntityByAddressId($emailAddressId, $entityType = null) { - $pdo = $this->getEntityManager()->getPDO(); + $pdo = $this->getEntityManager()->getPDO(); + $sql = " + SELECT entity_email_address.entity_type AS 'entityType', entity_email_address.entity_id AS 'entityId' + FROM entity_email_address + WHERE + entity_email_address.email_address_id = ".$pdo->quote($emailAddressId)." AND + entity_email_address.deleted = 0 + ORDER BY entity_email_address.primary DESC, FIELD(entity_email_address.entity_type, 'User', 'Contact', 'Lead', 'Account') + "; + + $sth = $pdo->prepare($sql); + $sth->execute(); + while ($row = $sth->fetch()) { + if (!empty($row['entityType']) && !empty($row['entityId'])) { + if (!empty($entityType)) { + if ($entityType != $row['entityType']) { + continue; + } + } + $entity = $this->getEntityManager()->getEntity($row['entityType'], $row['entityId']); + if ($entity) { + return $entity; + } + } + } + } + + public function getEntityByAddress($address, $entityType = null) + { + $pdo = $this->getEntityManager()->getPDO(); $sql = " SELECT entity_email_address.entity_type AS 'entityType', entity_email_address.entity_id AS 'entityId' FROM entity_email_address JOIN email_address ON email_address.id = entity_email_address.email_address_id AND email_address.deleted = 0 - WHERE + WHERE email_address.lower = ".$pdo->quote(strtolower($address))." AND entity_email_address.deleted = 0 ORDER BY entity_email_address.primary DESC, FIELD(entity_email_address.entity_type, 'User', 'Contact', 'Lead', 'Account') @@ -112,6 +141,11 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB $sth->execute(); while ($row = $sth->fetch()) { if (!empty($row['entityType']) && !empty($row['entityId'])) { + if (!empty($entityType)) { + if ($entityType != $row['entityType']) { + continue; + } + } $entity = $this->getEntityManager()->getEntity($row['entityType'], $row['entityId']); if ($entity) { return $entity; @@ -119,24 +153,24 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB } } } - + public function storeEntityEmailAddress(Entity $entity) { $email = trim($entity->get('emailAddress')); $emailAddressData = null; - + if ($entity->has('emailAddressData')) { $emailAddressData = $entity->get('emailAddressData'); } - - $pdo = $this->getEntityManager()->getPDO(); - + + $pdo = $this->getEntityManager()->getPDO(); + if ($emailAddressData !== null && is_array($emailAddressData)) { $previousEmailAddressData = array(); if (!$entity->isNew()) { $previousEmailAddressData = $this->getEmailAddressData($entity); } - + $hash = array(); foreach ($emailAddressData as $row) { $key = $row->emailAddress; @@ -144,11 +178,11 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB $hash[$key] = array( 'primary' => $row->primary ? true : false, 'optOut' => $row->optOut ? true : false, - 'invalid' => $row->invalid ? true : false, + 'invalid' => $row->invalid ? true : false, ); } } - + $hashPrev = array(); foreach ($previousEmailAddressData as $row) { $key = $row->emailAddress; @@ -156,25 +190,25 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB $hashPrev[$key] = array( 'primary' => $row->primary ? true : false, 'optOut' => $row->optOut ? true : false, - 'invalid' => $row->invalid ? true : false, + 'invalid' => $row->invalid ? true : false, ); } - } - - $primary = false; + } + + $primary = false; $toCreate = array(); - $toUpdate = array(); + $toUpdate = array(); $toRemove = array(); - + foreach ($hash as $key => $data) { $new = true; $changed = false; - + if ($hash[$key]['primary']) { $primary = $key; } - + if (array_key_exists($key, $hashPrev)) { $new = false; $changed = $hash[$key]['optOut'] != $hashPrev[$key]['optOut'] || $hash[$key]['invalid'] != $hashPrev[$key]['invalid']; @@ -182,23 +216,23 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB if ($hash[$key]['primary'] == $hashPrev[$key]['primary']) { $primary = false; } - } + } } - + if ($new) { $toCreate[] = $key; - } + } if ($changed) { $toUpdate[] = $key; - } + } } - - foreach ($hashPrev as $key => $data) { + + foreach ($hashPrev as $key => $data) { if (!array_key_exists($key, $hash)) { $toRemove[] = $key; } } - + foreach ($toRemove as $address) { $emailAddress = $this->getByAddress($address); if ($emailAddress) { @@ -211,10 +245,10 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB email_address_id = ".$pdo->quote($emailAddress->id)." "; $sth = $pdo->prepare($query); - $sth->execute(); + $sth->execute(); } } - + foreach ($toUpdate as $address) { $emailAddress = $this->getByAddress($address); if ($emailAddress) { @@ -225,17 +259,17 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB $this->save($emailAddress); } } - + foreach ($toCreate as $address) { $emailAddress = $this->getByAddress($address); if (!$emailAddress) { $emailAddress = $this->get(); - + $emailAddress->set(array( 'name' => $address, 'optOut' => $hash[$address]['optOut'], 'invalid' => $hash[$address]['invalid'], - )); + )); $this->save($emailAddress); } else { if ($emailAddress->get('optOut') != $hash[$address]['optOut'] || $emailAddress->get('invalid') != $hash[$address]['invalid']) { @@ -246,9 +280,9 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB $this->save($emailAddress); } } - + $query = " - INSERT entity_email_address + INSERT entity_email_address (entity_id, entity_type, email_address_id, `primary`) VALUES ( @@ -261,7 +295,7 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB $sth = $pdo->prepare($query); $sth->execute(); } - + if ($primary) { $emailAddress = $this->getByAddress($primary); if ($emailAddress) { @@ -271,27 +305,26 @@ class EmailAddress extends \Espo\Core\ORM\Repositories\RDB WHERE entity_id = ".$pdo->quote($entity->id)." AND entity_type = ".$pdo->quote($entity->getEntityName())." AND - `primary` = 1 AND + `primary` = 1 AND deleted = 0 "; $sth = $pdo->prepare($query); $sth->execute(); - + $query = " UPDATE entity_email_address SET `primary` = 1 WHERE entity_id = ".$pdo->quote($entity->id)." AND entity_type = ".$pdo->quote($entity->getEntityName())." AND - email_address_id = ".$pdo->quote($emailAddress->id)." AND + email_address_id = ".$pdo->quote($emailAddress->id)." AND deleted = 0 "; $sth = $pdo->prepare($query); $sth->execute(); } - } - - + } + } else { $entityRepository = $this->getEntityManager()->getRepository($entity->getEntityName()); if (!empty($email)) { diff --git a/application/Espo/Resources/metadata/entityDefs/Email.json b/application/Espo/Resources/metadata/entityDefs/Email.json index 81ae59f9c7..44471e8c1a 100644 --- a/application/Espo/Resources/metadata/entityDefs/Email.json +++ b/application/Espo/Resources/metadata/entityDefs/Email.json @@ -131,6 +131,9 @@ }, "teams": { "type": "linkMultiple" + }, + "users": { + "type": "linkMultiple" } }, "links": { @@ -151,6 +154,11 @@ "entity": "Team", "relationName": "EntityTeam" }, + "users": { + "type": "hasMany", + "entity": "User", + "foreign": "emails" + }, "attachments": { "type": "hasChildren", "entity": "Attachment", @@ -176,7 +184,7 @@ "additionalColumns": { "addressType": { "type": "varchar", - "len": "4" + "len": "4" } } }, @@ -190,7 +198,7 @@ "additionalColumns": { "addressType": { "type": "varchar", - "len": "4" + "len": "4" } } }, diff --git a/application/Espo/Resources/metadata/entityDefs/User.json b/application/Espo/Resources/metadata/entityDefs/User.json index 5785db355c..f7a103409a 100644 --- a/application/Espo/Resources/metadata/entityDefs/User.json +++ b/application/Espo/Resources/metadata/entityDefs/User.json @@ -119,6 +119,11 @@ "type": "hasMany", "entity": "Call", "foreign": "users" + }, + "emails": { + "type": "hasMany", + "entity": "Email", + "foreign": "users" } }, "collection": { diff --git a/application/Espo/SelectManagers/Email.php b/application/Espo/SelectManagers/Email.php new file mode 100644 index 0000000000..0f8ea3632c --- /dev/null +++ b/application/Espo/SelectManagers/Email.php @@ -0,0 +1,42 @@ + $this->getUser()->id + ); + } + + protected function accessOnlyOwn(&$result) + { + $this->boolFilterOnlyMy($result); + } +} + diff --git a/application/Espo/SelectManagers/EmailAccount.php b/application/Espo/SelectManagers/EmailAccount.php index 5167a7c48c..746553e8b2 100644 --- a/application/Espo/SelectManagers/EmailAccount.php +++ b/application/Espo/SelectManagers/EmailAccount.php @@ -26,11 +26,10 @@ class EmailAccount extends \Espo\Core\SelectManagers\Base { protected function access(&$result) { - if (!array_key_exists('whereClause', $result)) { - $result['whereClause'] = array(); - } if (!$this->user->isAdmin()) { - $result['whereClause']['assignedUserId'] = $this->user->id; + $result['whereClause'][] = array( + 'assignedUserId' => $this->user->id + ); } } } diff --git a/application/Espo/Services/Email.php b/application/Espo/Services/Email.php index 13879dffbc..d079920545 100644 --- a/application/Espo/Services/Email.php +++ b/application/Espo/Services/Email.php @@ -28,7 +28,6 @@ use \Espo\Core\Exceptions\Error; class Email extends Record { - protected function init() { $this->dependencies[] = 'mailSender'; @@ -36,7 +35,9 @@ class Email extends Record $this->dependencies[] = 'fileManager'; $this->dependencies[] = 'crypt'; } - + + protected $fetchEntityBeforeUpdate = true; + protected function getFileManager() { return $this->getInjection('fileManager'); @@ -50,7 +51,7 @@ class Email extends Record protected function getPreferences() { return $this->injections['preferences']; - } + } protected function getCrypt() { @@ -69,7 +70,7 @@ class Email extends Record if (array_key_exists('password', $smtpParams)) { $smtpParams['password'] = $this->getCrypt()->decrypt($smtpParams['password']); } - + if ($smtpParams) { $smtpParams['fromName'] = $this->getUser()->get('name'); $emailSender->useSmtp($smtpParams); @@ -130,7 +131,7 @@ class Email extends Record } $entity->set('bcc', implode(';', $arr)); } - + $this->loadNameHash($entity); $this->loadAttachmentsTypes($entity); @@ -155,28 +156,28 @@ class Email extends Record $entity->set('attachmentsTypes', $types); } - + public function loadNameHash(Entity $entity) { $addressList = array(); if ($entity->get('from')) { $addressList[] = $entity->get('from'); } - + $arr = explode(';', $entity->get('to')); foreach ($arr as $address) { if (!in_array($address, $addressList)) { $addressList[] = $address; } } - + $arr = explode(';', $entity->get('cc')); foreach ($arr as $address) { if (!in_array($address, $addressList)) { $addressList[] = $address; } } - + $nameHash = array(); $typeHash = array(); $idHash = array(); @@ -188,65 +189,65 @@ class Email extends Record $idHash[$address] = $p->id; } } - + $entity->set('nameHash', $nameHash); $entity->set('typeHash', $typeHash); $entity->set('idHash', $idHash); } - + public function findEntities($params) { $searchByEmailAddress = false; if (!empty($params['where']) && is_array($params['where'])) { foreach ($params['where'] as $i => $p) { if (!empty($p['field']) && $p['field'] == 'emailAddress') { - $searchByEmailAddress = true; + $searchByEmailAddress = true; $emailAddress = $this->getEntityManager()->getRepository('EmailAddress')->where(array( 'lower' => strtolower($p['value']) ))->findOne(); - unset($params['where'][$i]); + unset($params['where'][$i]); $emailAddressId = null; if ($emailAddress) { $emailAddressId = $emailAddress->id; } } - + } } - + $selectParams = $this->getSelectManager($this->entityName)->getSelectParams($params, true); - + if ($searchByEmailAddress) { if ($emailAddressId) { $pdo = $this->getEntityManager()->getPDO(); - + $selectParams['distinct'] = true; - + $selectParams['customJoin'] = " - LEFT JOIN email_email_address - ON - email_email_address.email_id = email.id AND + LEFT JOIN email_email_address + ON + email_email_address.email_id = email.id AND email_email_address.deleted = 0 "; - $selectParams['customWhere'] = " + $selectParams['customWhere'] = " AND ( - email.from_email_address_id = ".$pdo->quote($emailAddressId)." OR + email.from_email_address_id = ".$pdo->quote($emailAddressId)." OR email_email_address.email_address_id = ".$pdo->quote($emailAddressId)." ) "; } else { $selectParams['customWhere'] = ' AND 0'; } - - } - - $collection = $this->getRepository()->find($selectParams); - + + } + + $collection = $this->getRepository()->find($selectParams); + foreach ($collection as $e) { $this->loadParentNameFields($e); } - + return array( 'total' => $this->getRepository()->count($selectParams), 'collection' => $collection, @@ -257,21 +258,21 @@ class Email extends Record { return $this->getCopiedAttachments($emailId, $parentType, $parentId); } - + public function getCopiedAttachments($id, $parentType = null, $parentId = null) { - $ids = array(); + $ids = array(); $names = new \stdClass(); - + if (!empty($id)) { $email = $this->getEntityManager()->getEntity('Email', $id); if ($email && $this->getAcl()->check($email, 'read')) { $email->loadLinkMultipleField('attachments'); $attachmentsIds = $email->get('attachmentsIds'); - + foreach ($attachmentsIds as $attachmentId) { $source = $this->getEntityManager()->getEntity('Attachment', $attachmentId); - if ($source) { + if ($source) { $attachment = $this->getEntityManager()->getEntity('Attachment'); $attachment->set('role', 'Attachment'); $attachment->set('type', $source->get('type')); @@ -283,9 +284,9 @@ class Email extends Record $attachment->set('parentType', $parentType); $attachment->set('parentId', $parentId); } - + $this->getEntityManager()->saveEntity($attachment); - + $contents = $this->getFileManager()->getContents('data/upload/' . $source->id); if (!empty($contents)) { $this->getFileManager()->putContents('data/upload/' . $attachment->id, $contents); @@ -293,29 +294,29 @@ class Email extends Record $names->{$attachment->id} = $attachment->get('name'); } } - } + } } } - + return array( 'ids' => $ids, 'names' => $names ); } - + public function sendTestEmail($data) { $email = $this->getEntityManager()->getEntity('Email'); - + $email->set(array( 'subject' => 'EspoCRM: Test Email', 'isHtml' => false, - 'to' => $data['emailAddress'] - )); - + 'to' => $data['emailAddress'] + )); + $emailSender = $this->getMailSender(); $emailSender->useSmtp($data)->send($email); - + return true; } } diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index ac74d18603..54c55a5a3b 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -47,6 +47,8 @@ class Record extends \Espo\Core\Services\Base 'preferences' ); + protected $fetchEntityBeforeUpdate = false; + protected $entityName; private $streamService; @@ -330,7 +332,11 @@ class Record extends \Espo\Core\Services\Base $this->filterInput($data); $this->handleInput($data); - $entity = $this->getRepository()->get($id); + if ($this->fetchEntityBeforeUpdate) { + $entity = $this->getEntity($id); + } else { + $entity = $this->getRepository()->get($id); + } if (!$entity) { throw new NotFound();