protected function handleAfterSaveAccounts(Entity $entity, array $options) { $accountIdChanged = $entity->has('accountId') && $entity->get('accountId') != $entity->getFetched('accountId'); $titleChanged = $entity->has('title') && $entity->get('title') != $entity->getFetched('title'); if ($accountIdChanged) { $accountId = $entity->get('accountId'); if (empty($accountId)) { $this->unrelate($entity, 'accounts', $entity->getFetched('accountId')); return; } } if ($titleChanged) { if (empty($accountId)) { $accountId = $entity->getFetched('accountId'); if (empty($accountId)) { return; } } } if ($accountIdChanged || $titleChanged) { $pdo = $this->getEntityManager()->getPDO(); $sql = "\n SELECT id, role FROM account_contact\n WHERE\n account_id = " . $pdo->quote($accountId) . " AND\n contact_id = " . $pdo->quote($entity->id) . " AND\n deleted = 0\n "; $sth = $pdo->prepare($sql); $sth->execute(); if ($row = $sth->fetch()) { if ($titleChanged && $entity->get('title') != $row['role']) { $this->updateRelation($entity, 'accounts', $accountId, array('role' => $entity->get('title'))); } } else { if ($accountIdChanged) { $this->relate($entity, 'accounts', $accountId, array('role' => $entity->get('title'))); } } } }
protected function handleAfterSaveContacts(Entity $entity, array $options) { $contactIdChanged = $entity->has('contactId') && $entity->get('contactId') != $entity->getFetched('contactId'); if ($contactIdChanged) { $contactId = $entity->get('contactId'); if (empty($contactId)) { $this->unrelate($entity, 'contacts', $entity->getFetched('contactId')); return; } } if ($contactIdChanged) { $pdo = $this->getEntityManager()->getPDO(); $sql = "\n SELECT id FROM case_contact\n WHERE\n contact_id = " . $pdo->quote($contactId) . " AND\n case_id = " . $pdo->quote($entity->id) . " AND\n deleted = 0\n "; $sth = $pdo->prepare($sql); $sth->execute(); if (!$sth->fetch()) { $this->relate($entity, 'contacts', $contactId); } } }
protected function storeAutoFollowEntityTypeList(Entity $entity) { $id = $entity->id; $isChanged = false; $was = $entity->getFetched('autoFollowEntityTypeList'); $became = $entity->get('autoFollowEntityTypeList'); if (!is_array($was)) { $was = []; } if (!is_array($became)) { $became = []; } if ($was == $became) { return; } $pdo = $this->getEntityManger()->getPDO(); $sql = "DELETE FROM autofollow WHERE user_id = " . $pdo->quote($id) . ""; $pdo->query($sql); $scopes = $this->getMetadata()->get('scopes'); foreach ($became as $entityType) { if (isset($scopes[$entityType]) && !empty($scopes[$entityType]['stream'])) { $sql = "\n INSERT INTO autofollow (user_id, entity_type)\n VALUES (" . $pdo->quote($id) . ", " . $pdo->quote($entityType) . ")\n "; $pdo->query($sql); } } }
public function process(Entity $entity) { if ($entity->get('status') !== 'Archived' && $entity->get('status') !== 'Sent') { return; } if ($entity->get('isJustSent')) { $previousUserIdList = []; } else { $previousUserIdList = $entity->getFetched('usersIds'); if (!is_array($previousUserIdList)) { $previousUserIdList = []; } } $emailUserIdList = $entity->get('usersIds'); if (is_null($emailUserIdList) || !is_array($emailUserIdList)) { return; } $userIdList = []; foreach ($emailUserIdList as $userId) { if (!in_array($userId, $userIdList) && !in_array($userId, $previousUserIdList) && $userId != $this->getUser()->id) { $userIdList[] = $userId; } } $data = array('emailId' => $entity->id, 'emailName' => $entity->get('name')); if (!$entity->has('from')) { $this->getEntityManager()->getRepository('Email')->loadFromField($entity); } $from = $entity->get('from'); if ($from) { $person = $this->getEntityManager()->getRepository('EmailAddress')->getEntityByAddress($from, null, ['User', 'Contact', 'Lead']); if ($person) { $data['personEntityType'] = $person->getEntityType(); $data['personEntityName'] = $person->get('name'); $data['personEntityId'] = $person->id; } } $userIdFrom = null; if ($person && $person->getEntityType() == 'User') { $userIdFrom = $person->id; } if (empty($data['personEntityId'])) { $data['fromString'] = \Espo\Services\Email::parseFromName($entity->get('fromString')); if (empty($data['fromString']) && $from) { $data['fromString'] = $from; } } $parent = null; if ($entity->get('parentId') && $entity->get('parentType')) { $parent = $this->getEntityManager()->getEntity($entity->get('parentType'), $entity->get('parentId')); } $account = null; if ($entity->get('accountId')) { $account = $this->getEntityManager()->getEntity('Account', $entity->get('accountId')); } foreach ($userIdList as $userId) { if ($userIdFrom == $userId) { continue; } if ($entity->get('status') == 'Archived') { if ($parent) { if ($this->getStreamService()->checkIsFollowed($parent, $userId)) { continue; } } if ($account) { if ($this->getStreamService()->checkIsFollowed($account, $userId)) { continue; } } } $notification = $this->getEntityManager()->getEntity('Notification'); $notification->set(array('type' => 'EmailReceived', 'userId' => $userId, 'data' => $data)); $this->getEntityManager()->saveEntity($notification); } }