PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/application/Espo/Repositories/Email.php

https://gitlab.com/johanlindberg/irvato-crm
PHP | 273 lines | 214 code | 32 blank | 27 comment | 63 complexity | c02b0b36586128747519627c43521d1d MD5 | raw file
  1. <?php
  2. /************************************************************************
  3. * This file is part of EspoCRM.
  4. *
  5. * EspoCRM - Open Source CRM application.
  6. * Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
  7. * Website: http://www.espocrm.com
  8. *
  9. * EspoCRM is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * EspoCRM is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
  21. *
  22. * The interactive user interfaces in modified source and object code versions
  23. * of this program must display Appropriate Legal Notices, as required under
  24. * Section 5 of the GNU General Public License version 3.
  25. *
  26. * In accordance with Section 7(b) of the GNU General Public License version 3,
  27. * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
  28. ************************************************************************/
  29. namespace Espo\Repositories;
  30. use Espo\ORM\Entity;
  31. class Email extends \Espo\Core\ORM\Repositories\RDB
  32. {
  33. protected function init()
  34. {
  35. parent::init();
  36. $this->addDependency('emailFilterManager');
  37. }
  38. protected function prepareAddressess(Entity $entity, $type, $addAssignedUser = false)
  39. {
  40. if (!$entity->has($type)) {
  41. return;
  42. }
  43. $eaRepositoty = $this->getEntityManager()->getRepository('EmailAddress');
  44. $address = $entity->get($type);
  45. $idList = [];
  46. if (!empty($address) || !filter_var($address, FILTER_VALIDATE_EMAIL)) {
  47. $arr = array_map(function ($e) {
  48. return trim($e);
  49. }, explode(';', $address));
  50. $idList = $eaRepositoty->getIdListFormAddressList($arr);
  51. foreach ($idList as $id) {
  52. $this->addUserByEmailAddressId($entity, $id, $addAssignedUser);
  53. }
  54. }
  55. $entity->setLinkMultipleIdList($type . 'EmailAddresses', $idList);
  56. }
  57. protected function addUserByEmailAddressId(Entity $entity, $emailAddressId, $addAssignedUser = false)
  58. {
  59. $user = $this->getEntityManager()->getRepository('EmailAddress')->getEntityByAddressId($emailAddressId, 'User');
  60. if ($user) {
  61. $entity->addLinkMultipleId('users', $user->id);
  62. if ($addAssignedUser) {
  63. $entity->addLinkMultipleId('assignedUsers', $user->id);
  64. }
  65. }
  66. }
  67. public function loadFromField(Entity $entity)
  68. {
  69. if ($entity->get('fromEmailAddressName')) {
  70. $entity->set('from', $entity->get('fromEmailAddressName'));
  71. }
  72. }
  73. public function loadNameHash(Entity $entity, array $fieldList = ['from', 'to', 'cc'])
  74. {
  75. $addressList = array();
  76. if (in_array('from', $fieldList) && $entity->get('from')) {
  77. $addressList[] = $entity->get('from');
  78. }
  79. if (in_array('to', $fieldList)) {
  80. $arr = explode(';', $entity->get('to'));
  81. foreach ($arr as $address) {
  82. if (!in_array($address, $addressList)) {
  83. $addressList[] = $address;
  84. }
  85. }
  86. }
  87. if (in_array('cc', $fieldList)) {
  88. $arr = explode(';', $entity->get('cc'));
  89. foreach ($arr as $address) {
  90. if (!in_array($address, $addressList)) {
  91. $addressList[] = $address;
  92. }
  93. }
  94. }
  95. $nameHash = (object) [];
  96. $typeHash = (object) [];
  97. $idHash = (object) [];
  98. foreach ($addressList as $address) {
  99. $p = $this->getEntityManager()->getRepository('EmailAddress')->getEntityByAddress($address);
  100. if ($p) {
  101. $nameHash->$address = $p->get('name');
  102. $typeHash->$address = $p->getEntityName();
  103. $idHash->$address = $p->id;
  104. }
  105. }
  106. $entity->set('nameHash', $nameHash);
  107. $entity->set('typeHash', $typeHash);
  108. $entity->set('idHash', $idHash);
  109. }
  110. protected function beforeSave(Entity $entity, array $options = array())
  111. {
  112. $eaRepositoty = $this->getEntityManager()->getRepository('EmailAddress');
  113. if ($entity->has('attachmentsIds')) {
  114. $attachmentsIds = $entity->get('attachmentsIds');
  115. if (!empty($attachmentsIds)) {
  116. $entity->set('hasAttachment', true);
  117. }
  118. }
  119. if (!$entity->get('sentById')) {
  120. if ($entity->get('from')) {
  121. $from = trim($entity->get('from'));
  122. $user = $this->getEntityManager()->getRepository('EmailAddress')->getEntityByAddressId($emailAddressId, 'User');
  123. }
  124. }
  125. if ($entity->has('from') || $entity->has('to') || $entity->has('cc') || $entity->has('bcc') || $entity->has('replyTo')) {
  126. if (!$entity->has('usersIds')) {
  127. $entity->loadLinkMultipleField('users');
  128. }
  129. if ($entity->has('from')) {
  130. $from = trim($entity->get('from'));
  131. if (!empty($from)) {
  132. $ids = $eaRepositoty->getIds(array($from));
  133. if (!empty($ids)) {
  134. $entity->set('fromEmailAddressId', $ids[0]);
  135. $this->addUserByEmailAddressId($entity, $ids[0], true);
  136. $entity->setLinkMultipleColumn('users', 'isRead', $ids[0], true);
  137. if (!$entity->get('sentById')) {
  138. $user = $this->getEntityManager()->getRepository('EmailAddress')->getEntityByAddressId($entity->get('fromEmailAddressId'), 'User');
  139. if ($user) {
  140. $entity->set('sentById', $user->id);
  141. }
  142. }
  143. }
  144. } else {
  145. $entity->set('fromEmailAddressId', null);
  146. }
  147. }
  148. if ($entity->has('to')) {
  149. $this->prepareAddressess($entity, 'to', true);
  150. }
  151. if ($entity->has('cc')) {
  152. $this->prepareAddressess($entity, 'cc');
  153. }
  154. if ($entity->has('bcc')) {
  155. $this->prepareAddressess($entity, 'bcc');
  156. }
  157. if ($entity->has('replyTo')) {
  158. $this->prepareAddressess($entity, 'replyTo');
  159. }
  160. $assignedUserId = $entity->get('assignedUserId');
  161. if ($assignedUserId) {
  162. $entity->addLinkMultipleId('users', $assignedUserId);
  163. }
  164. }
  165. parent::beforeSave($entity, $options);
  166. $parentId = $entity->get('parentId');
  167. $parentType = $entity->get('parentType');
  168. if (!empty($parentId) || !empty($parentType)) {
  169. $parent = $this->getEntityManager()->getEntity($parentType, $parentId);
  170. if (!empty($parent)) {
  171. if ($parent->getEntityType() == 'Account') {
  172. $accountId = $parent->id;
  173. } else if ($parent->has('accountId')) {
  174. $accountId = $parent->get('accountId');
  175. }
  176. if (!empty($accountId)) {
  177. $account = $this->getEntityManager()->getEntity('Account', $accountId);
  178. if ($account) {
  179. $entity->set('accountId', $accountId);
  180. $entity->set('accountName', $account->get('name'));
  181. }
  182. }
  183. }
  184. }
  185. if ($entity->get('isBeingImported')) {
  186. foreach ($entity->getLinkMultipleIdList('users') as $userId) {
  187. $filter = $this->getEmailFilterManager()->getMatchingFilter($entity, $userId);
  188. if ($filter) {
  189. $action = $filter->get('action');
  190. if ($action === 'Skip') {
  191. $entity->setLinkMultipleColumn('users', 'inTrash', $userId, true);
  192. } else if ($action === 'Move to Folder') {
  193. $folderId = $filter->get('emailFolderId');
  194. if ($folderId) {
  195. $entity->setLinkMultipleColumn('users', 'folderId', $userId, $folderId);
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. protected function afterSave(Entity $entity, array $options = array())
  203. {
  204. parent::afterSave($entity, $options);
  205. if (!$entity->isNew()) {
  206. if ($entity->get('parentType') && $entity->get('parentId') && $entity->isFieldChanged('parentId')) {
  207. $replyList = $this->findRelated($entity, 'replies');
  208. foreach ($replyList as $reply) {
  209. if ($reply->id === $entity->id) continue;
  210. if (!$reply->get('parentId')) {
  211. $reply->set(array(
  212. 'parentId' => $entity->get('parentId'),
  213. 'parentType' => $entity->get('parentType'),
  214. ));
  215. $this->getEntityManager()->saveEntity($reply);
  216. }
  217. }
  218. }
  219. }
  220. if (
  221. ($entity->get('status') === 'Archived' || $entity->get('status') === 'Sent')
  222. &&
  223. ($entity->isAttributeChanged('status') || $entity->isNew())
  224. ) {
  225. if ($entity->get('repliedId')) {
  226. $replied = $this->getEntityManager()->getEntity('Email', $entity->get('repliedId'));
  227. if ($replied && $replied->id !== $entity->id && !$replied->get('isReplied')) {
  228. $replied->set('isReplied', true);
  229. $this->getEntityManager()->saveEntity($replied, array('silent' => true));
  230. }
  231. }
  232. }
  233. if ($entity->get('isBeingImportered')) {
  234. $entity->set('isBeingImportered', false);
  235. }
  236. }
  237. protected function getEmailFilterManager()
  238. {
  239. return $this->getInjection('emailFilterManager');
  240. }
  241. }