PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/application/Espo/Services/Notification.php

https://gitlab.com/johanlindberg/irvato-crm
PHP | 276 lines | 213 code | 36 blank | 27 comment | 41 complexity | ceffdc77a13839547c694c2ae0c2b44d 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\Services;
  30. use \Espo\Core\Exceptions\Forbidden;
  31. use \Espo\Core\Exceptions\NotFound;
  32. use Espo\ORM\Entity;
  33. use Espo\Core\Utils\Json;
  34. class Notification extends \Espo\Services\Record
  35. {
  36. protected function getEntityManager()
  37. {
  38. return $this->injections['entityManager'];
  39. }
  40. protected function getUser()
  41. {
  42. return $this->injections['user'];
  43. }
  44. protected function getMetadata()
  45. {
  46. return $this->injections['metadata'];
  47. }
  48. protected function getAclManager()
  49. {
  50. return $this->getInjection('aclManager');
  51. }
  52. public function notifyAboutMentionInPost($userId, $noteId)
  53. {
  54. $notification = $this->getEntityManager()->getEntity('Notification');
  55. $notification->set(array(
  56. 'type' => 'MentionInPost',
  57. 'data' => array('noteId' => $noteId),
  58. 'userId' => $userId,
  59. 'relatedId' => $noteId,
  60. 'relatedType' => 'Note'
  61. ));
  62. $this->getEntityManager()->saveEntity($notification);
  63. }
  64. public function notifyAboutNote(array $userIdList, \Espo\Entities\Note $note)
  65. {
  66. $data = array('noteId' => $note->id);
  67. $encodedData = Json::encode($data);
  68. $now = date('Y-m-d H:i:s');
  69. $pdo = $this->getEntityManager()->getPDO();
  70. $sql = "INSERT INTO `notification` (`id`, `data`, `type`, `user_id`, `created_at`, `related_id`, `related_type`, `related_parent_id`, `related_parent_type`) VALUES ";
  71. $arr = [];
  72. $userList = $this->getEntityManager()->getRepository('User')->where(array(
  73. 'isActive' => true,
  74. 'id' => $userIdList
  75. ))->find();
  76. foreach ($userList as $user) {
  77. $userId = $user->id;
  78. if (!$this->checkUserNoteAccess($user, $note)) continue;
  79. if ($note->get('createdById') === $user->id) continue;
  80. $id = uniqid();
  81. $arr[] = "(".$pdo->quote($id).", ".$pdo->quote($encodedData).", ".$pdo->quote('Note').", ".$pdo->quote($userId).", ".$pdo->quote($now).", ".$pdo->quote($note->id).", ".$pdo->quote('Note').", ".$pdo->quote($note->get('parentId')).", ".$pdo->quote($note->get('parentType')).")";
  82. }
  83. if (empty($arr)) {
  84. return;
  85. }
  86. $sql .= implode(", ", $arr);
  87. $pdo->query($sql);
  88. }
  89. public function checkUserNoteAccess(\Espo\Entities\User $user, \Espo\Entities\Note $note)
  90. {
  91. if ($user->get('isPortalUser')) {
  92. if ($note->get('relatedType')) {
  93. if ($note->get('relatedType') === 'Email' && $note->get('parentType') === 'Case') {
  94. return true;
  95. }
  96. return false;
  97. }
  98. return true;
  99. }
  100. if ($note->get('relatedType')) {
  101. if (!$this->getAclManager()->checkScope($user, $note->get('relatedType'))) {
  102. return false;
  103. }
  104. }
  105. if ($note->get('parentType')) {
  106. if (!$this->getAclManager()->checkScope($user, $note->get('parentType'))) {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. public function getNotReadCount($userId)
  113. {
  114. $whereClause = array(
  115. 'userId' => $userId,
  116. 'read' => 0
  117. );
  118. $ignoreScopeList = $this->getIgnoreScopeList();
  119. if (!empty($ignoreScopeList)) {
  120. $where = [];
  121. $where[] = array(
  122. 'OR' => array(
  123. 'relatedParentType' => null,
  124. 'relatedParentType!=' => $ignoreScopeList
  125. )
  126. );
  127. $whereClause[] = $where;
  128. }
  129. return $this->getEntityManager()->getRepository('Notification')->where($whereClause)->count();
  130. }
  131. public function markAllRead($userId)
  132. {
  133. $pdo = $this->getEntityManager()->getPDO();
  134. $sql = "UPDATE notification SET `read` = 1 WHERE user_id = ".$pdo->quote($userId)." AND `read` = 0";
  135. $pdo->prepare($sql)->execute();
  136. return true;
  137. }
  138. public function getList($userId, array $params = array())
  139. {
  140. $searchParams = array();
  141. $whereClause = array(
  142. 'userId' => $userId
  143. );
  144. if (!empty($params['after'])) {
  145. $whereClause['createdAt>'] = $params['after'];
  146. }
  147. $ignoreScopeList = $this->getIgnoreScopeList();
  148. if (!empty($ignoreScopeList)) {
  149. $where = [];
  150. $where[] = array(
  151. 'OR' => array(
  152. 'relatedParentType' => null,
  153. 'relatedParentType!=' => $ignoreScopeList
  154. )
  155. );
  156. $whereClause[] = $where;
  157. }
  158. $searchParams['whereClause'] = $whereClause;
  159. if (array_key_exists('offset', $params)) {
  160. $searchParams['offset'] = $params['offset'];
  161. }
  162. if (array_key_exists('maxSize', $params)) {
  163. $searchParams['limit'] = $params['maxSize'];
  164. }
  165. $searchParams['orderBy'] = 'createdAt';
  166. $searchParams['order'] = 'DESC';
  167. $collection = $this->getEntityManager()->getRepository('Notification')->find($searchParams);
  168. $count = $this->getEntityManager()->getRepository('Notification')->count($searchParams);
  169. $ids = array();
  170. foreach ($collection as $k => $entity) {
  171. $ids[] = $entity->id;
  172. $data = $entity->get('data');
  173. if (empty($data)) {
  174. continue;
  175. }
  176. switch ($entity->get('type')) {
  177. case 'Note':
  178. case 'MentionInPost':
  179. $note = $this->getEntityManager()->getEntity('Note', $data->noteId);
  180. if ($note) {
  181. if ($note->get('parentId') && $note->get('parentType')) {
  182. $parent = $this->getEntityManager()->getEntity($note->get('parentType'), $note->get('parentId'));
  183. if ($parent) {
  184. $note->set('parentName', $parent->get('name'));
  185. }
  186. } else {
  187. if (!$note->get('isGlobal')) {
  188. $targetType = $note->get('targetType');
  189. if (!$targetType || $targetType === 'users') {
  190. $note->loadLinkMultipleField('users');
  191. }
  192. if ($targetType !== 'users') {
  193. if (!$targetType || $targetType === 'teams') {
  194. $note->loadLinkMultipleField('teams');
  195. } else if ($targetType === 'portals') {
  196. $note->loadLinkMultipleField('portals');
  197. }
  198. }
  199. }
  200. }
  201. if ($note->get('relatedId') && $note->get('relatedType')) {
  202. $related = $this->getEntityManager()->getEntity($note->get('relatedType'), $note->get('relatedId'));
  203. if ($related) {
  204. $note->set('relatedName', $related->get('name'));
  205. }
  206. }
  207. $entity->set('noteData', $note->toArray());
  208. } else {
  209. unset($collection[$k]);
  210. $count--;
  211. $this->getEntityManager()->removeEntity($entity);
  212. }
  213. break;
  214. }
  215. }
  216. if (!empty($ids)) {
  217. $pdo = $this->getEntityManager()->getPDO();
  218. $sql = "UPDATE notification SET `read` = 1 WHERE id IN ('" . implode("', '", $ids) ."')";
  219. $s = $pdo->prepare($sql);
  220. $s->execute();
  221. }
  222. return array(
  223. 'total' => $count,
  224. 'collection' => $collection
  225. );
  226. }
  227. protected function getIgnoreScopeList()
  228. {
  229. $ignoreScopeList = [];
  230. $scopes = $this->getMetadata()->get('scopes', array());
  231. foreach ($scopes as $scope => $d) {
  232. if (empty($d['entity']) || !$d['entity']) continue;
  233. if (empty($d['object']) || !$d['object']) continue;
  234. if (!$this->getAcl()->checkScope($scope)) {
  235. $ignoreScopeList[] = $scope;
  236. }
  237. }
  238. return $ignoreScopeList;
  239. }
  240. }