init(); } protected function init() { } protected function addDependencyList(array $list) { foreach ($list as $item) { $this->addDependency($item); } } protected function addDependency($name) { $this->dependencyList[] = $name; } public function setEntityType(string $entityType) { $this->entityType = $entityType; } public function getDependencyList() { return $this->dependencyList; } protected function getInjection($name) { return $this->injections[$name] ?? $this->$name ?? null; } public function inject($name, $object) { $this->injections[$name] = $object; } protected function getEntityManager() { return $this->getInjection('entityManager'); } protected function getUser() { return $this->getInjection('user'); } public function process(Entity $entity, array $options = []) { if ($entity->hasLinkMultipleField('assignedUsers')) { $userIdList = $entity->getLinkMultipleIdList('assignedUsers'); $fetchedAssignedUserIdList = $entity->getFetched('assignedUsersIds'); if (!is_array($fetchedAssignedUserIdList)) { $fetchedAssignedUserIdList = []; } foreach ($userIdList as $userId) { if (in_array($userId, $fetchedAssignedUserIdList)) continue; $this->processForUser($entity, $userId); } } else { if (!$entity->get('assignedUserId')) return; if (!$entity->isAttributeChanged('assignedUserId')) return; $assignedUserId = $entity->get('assignedUserId'); $this->processForUser($entity, $assignedUserId); } } protected function processForUser(Entity $entity, $assignedUserId) { if (!$this->isNotificationsEnabledForUser($assignedUserId)) return; if ($entity->hasAttribute('createdById') && $entity->hasAttribute('modifiedById')) { if ($entity->isNew()) { $isNotSelfAssignment = $assignedUserId !== $entity->get('createdById'); } else { $isNotSelfAssignment = $assignedUserId !== $entity->get('modifiedById'); } } else { $isNotSelfAssignment = $assignedUserId !== $this->getUser()->id; } if (!$isNotSelfAssignment) return; $notification = $this->getEntityManager()->getEntity('Notification'); $notification->set([ 'type' => 'Assign', 'userId' => $assignedUserId, 'data' => [ 'entityType' => $entity->getEntityType(), 'entityId' => $entity->id, 'entityName' => $entity->get('name'), 'isNew' => $entity->isNew(), 'userId' => $this->getUser()->id, 'userName' => $this->getUser()->get('name'), ] ]); $this->getEntityManager()->saveEntity($notification); } protected function isNotificationsEnabledForUser(string $userId) { if (!array_key_exists($userId, $this->userIdEnabledMap)) { $preferences = $this->getEntityManager()->getEntity('Preferences', $userId); $isEnabled = false; if ($preferences) { $isEnabled = true; $ignoreList = $preferences->get('assignmentNotificationsIgnoreEntityTypeList') ?? []; if (in_array($this->entityType, $ignoreList)) { $isEnabled = false; } } $this->userIdEnabledMap[$userId] = $isEnabled; } return $this->userIdEnabledMap[$userId]; } }