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 getDuplicateWhereClause(Entity $entity) { $data = array('OR' => array(array('firstName' => $entity->get('firstName'), 'lastName' => $entity->get('lastName')))); if ($entity->get('emailAddress')) { $data['OR'][] = array('emailAddress' => $entity->get('emailAddress')); } return $data; }
public function process(Entity $entity) { if ($entity->has('assignedUserId') && $entity->get('assignedUserId')) { $assignedUserId = $entity->get('assignedUserId'); if ($assignedUserId != $this->getUser()->id && $entity->isFieldChanged('assignedUserId')) { $notification = $this->getEntityManager()->getEntity('Notification'); $notification->set(array('type' => 'Assign', 'userId' => $assignedUserId, 'data' => array('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 getIscContents(Entity $entity) { $user = $entity->get('assignedUser'); $who = ''; $email = ''; if ($user) { $who = $user->get('name'); $email = $user->get('emailAddress'); } $ics = new Ics('//EspoCRM//EspoCRM Calendar//EN', array('startDate' => strtotime($entity->get('dateStart')), 'endDate' => strtotime($entity->get('dateEnd')), 'uid' => $entity->id, 'summary' => $entity->get('name'), 'who' => $who, 'email' => $email, 'description' => $entity->get('description'))); return $ics->get(); }
public function checkIsOwner(User $user, Entity $entity) { if ($entity->has('assignedUserId')) { if ($user->id === $entity->get('assignedUserId')) { return true; } } if ($user->id === $entity->get('createdById')) { return true; } return false; }
public function checkIsOwner(User $user, Entity $entity) { if ($entity->has('parentId') && $entity->has('parentType')) { $parentType = $entity->get('parentType'); $parentId = $entity->get('parentId'); if (!$parentType || !$parentId) { return; } $parent = $this->getEntityManager()->getEntity($parentType, $parentId); if ($parent && $parent->has('assignedUserId') && $parent->get('assignedUserId') === $user->id) { return true; } } return; }
protected function afterRemove(Entity $entity, array $options = array()) { if ($entity->get('fileId')) { $attachment = $this->getEntityManager()->getEntity('Attachment', $entity->get('fileId')); if ($attachment) { $this->getEntityManager()->removeEntity($attachment); } } $pdo = $this->getEntityManager()->getPDO(); $sql = "DELETE FROM import_entity WHERE import_id = :importId"; $sth = $pdo->prepare($sql); $sth->bindValue(':importId', $entity->id); $sth->execute(); parent::afterRemove($entity, $options); }
public function sendInvitations(Entity $entity) { $invitationManager = $this->getInvitationManager(); $emailHash = array(); $users = $entity->get('users'); foreach ($users as $user) { if ($user->get('emailAddress') && !array_key_exists($user->get('emailAddress'), $emailHash)) { $invitationManager->sendInvitation($entity, $user, 'users'); $emailHash[$user->get('emailAddress')] = true; } } $contacts = $entity->get('contacts'); foreach ($contacts as $contact) { if ($contact->get('emailAddress') && !array_key_exists($contact->get('emailAddress'), $emailHash)) { $invitationManager->sendInvitation($entity, $contact, 'contacts'); $emailHash[$user->get('emailAddress')] = true; } } $leads = $entity->get('leads'); foreach ($leads as $lead) { if ($lead->get('emailAddress') && !array_key_exists($lead->get('emailAddress'), $emailHash)) { $invitationManager->sendInvitation($entity, $lead, 'leads'); $emailHash[$user->get('emailAddress')] = true; } } return true; }
protected function handleCreateRelated(Entity $entity) { $linkDefs = $this->getMetadata()->get("entityDefs." . $entity->getEntityName() . ".links", array()); $scopeNotifiedList = array(); foreach ($linkDefs as $link => $defs) { if ($defs['type'] == 'belongsTo') { if (empty($defs['foreign']) || empty($defs['entity'])) { continue; } $foreign = $defs['foreign']; $scope = $defs['entity']; $entityId = $entity->get($link . 'Id'); if (!empty($scope) && !empty($entityId)) { if (in_array($scope, $scopeNotifiedList) || !$this->isLinkObservableInStream($scope, $foreign)) { continue; } $this->getStreamService()->noteCreateRelated($entity, $scope, $entityId); $scopeNotifiedList[] = $scope; } } else { if ($defs['type'] == 'belongsToParent') { $foreign = $defs['foreign']; if (empty($defs['foreign'])) { continue; } $scope = $entity->get($link . 'Type'); $entityId = $entity->get($link . 'Id'); if (!empty($scope) && !empty($entityId)) { if (in_array($scope, $scopeNotifiedList) || !$this->isLinkObservableInStream($scope, $foreign)) { continue; } $this->getStreamService()->noteCreateRelated($entity, $scope, $entityId); $scopeNotifiedList[] = $scope; } } else { if ($defs['type'] == 'hasMany') { if (empty($defs['foreign']) || empty($defs['entity'])) { continue; } $foreign = $defs['foreign']; $scope = $defs['entity']; $entityIds = $entity->get($link . 'Ids'); if (!empty($scope) && is_array($entityIds) && !empty($entityIds)) { if (in_array($scope, $scopeNotifiedList) || !$this->isLinkObservableInStream($scope, $foreign)) { continue; } $entityId = $entityIds[0]; $this->getStreamService()->noteCreateRelated($entity, $scope, $entityId); $scopeNotifiedList[] = $scope; } } } } } }
protected function storeEntity(Entity $entity) { $assignedUserId = $entity->get('assignedUserId'); if ($assignedUserId && $entity->has('usersIds')) { $usersIds = $entity->get('usersIds'); if (!is_array($usersIds)) { $usersIds = array(); } if (!in_array($assignedUserId, $usersIds)) { $usersIds[] = $assignedUserId; $entity->set('usersIds', $usersIds); $hash = $entity->get('usersNames'); if ($hash instanceof \stdClass) { $hash->assignedUserId = $entity->get('assignedUserName'); $entity->set('usersNames', $hash); } } } return parent::storeEntity($entity); }
public function afterSave(Entity $entity) { if ($this->getConfig()->get('assignmentEmailNotifications') && in_array($entity->getEntityName(), $this->getConfig()->get('assignmentEmailNotificationsEntityList', array()))) { $userId = $entity->get('assignedUserId'); if (!empty($userId) && $userId != $this->getUser()->id && $entity->isFieldChanged('assignedUserId')) { $job = $this->getEntityManager()->getEntity('Job'); $job->set(array('serviceName' => 'EmailNotification', 'method' => 'notifyAboutAssignmentJob', 'data' => json_encode(array('userId' => $userId, 'assignerUserId' => $this->getUser()->id, 'entityId' => $entity->id, 'entityType' => $entity->getEntityName())), 'executeTime' => date('Y-m-d H:i:s'))); $this->getEntityManager()->saveEntity($job); } } }
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); } } }
public function afterSave(Entity $entity) { if ($entity->isNew()) { $parentType = $entity->get('parentType'); $parentId = $entity->get('parentId'); $superParentType = $entity->get('superParentType'); $superParentTypeId = $entity->get('superParentTypeId'); $userIdList = []; if ($parentType && $parentId) { $userIdList = array_merge($userIdList, $this->getSubscriberIdList($parentType, $parentId)); } if ($superParentType && $superParentTypeId) { $userIdList = array_merge($userIdList, $this->getSubscriberIdList($superParentType, $superParentTypeId)); } //$userIdList = array_merge($userIdList, $this->getMentionedUserIdList($entity)); $userIdList = array_unique($userIdList); if (!empty($userIdList)) { $this->getNotificationService()->notifyAboutNote($userIdList, $entity); } } }
public function checkInTeam(User $user, Entity $entity) { if ($entity->has('campaignId')) { $campaignId = $entity->get('campaignId'); if (!$campaignId) { return; } $campaign = $this->getEntityManager()->getEntity('Campaign', $campaignId); if ($campaign && $this->getAclManager()->getImplementation('Campaign')->checkInTeam($user, $campaign)) { return true; } } return; }
protected function beforeSave(Entity $entity) { if ($entity->isNew()) { $userName = $entity->get('userName'); if (empty($userName)) { throw new Error(); } $user = $this->where(array('userName' => $userName))->findOne(); if ($user) { throw new Error(); } } else { if ($entity->isFieldChanged('userName')) { $userName = $entity->get('userName'); if (empty($userName)) { throw new Error(); } $user = $this->where(array('userName' => $userName, 'id!=' => $entity->id))->findOne(); if ($user) { throw new Error(); } } } }
public function afterSave(Entity $entity) { if (!$entity->isFetched()) { $parentType = $entity->get('parentType'); $parentId = $entity->get('parentId'); if ($parentType && $parentId) { $pdo = $this->getEntityManager()->getPDO(); $sql = "\n\t\t\t\t\tSELECT user_id AS userId \n\t\t\t\t\tFROM subscription\n\t\t\t\t\tWHERE entity_id = " . $pdo->quote($parentId) . " AND entity_type = " . $pdo->quote($parentType); $sth = $pdo->prepare($sql); $sth->execute(); $userIdList = array(); while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { if ($this->getUser()->id != $row['userId']) { $userIdList[] = $row['userId']; } } if (!empty($userIdList)) { $job = $this->getEntityManager()->getEntity('Job'); $job->set(array('serviceName' => 'Notification', 'method' => 'notifyAboutNoteFromJob', 'data' => json_encode(array('userIdList' => $userIdList, 'noteId' => $entity->id)), 'executeTime' => date('Y-m-d H:i:s'))); $this->getEntityManager()->saveEntity($job); } } } }
public function sendInvitations(Entity $entity) { $invitationManager = $this->getInvitationManager(); $users = $entity->get('users'); foreach ($users as $user) { $invitationManager->sendInvitation($entity, $user, 'users'); } $contacts = $entity->get('contacts'); foreach ($contacts as $contact) { $invitationManager->sendInvitation($entity, $contact, 'contacts'); } $leads = $entity->get('leads'); foreach ($leads as $lead) { $invitationManager->sendInvitation($entity, $lead, 'leads'); } return true; }
protected function beforeSave(Entity $entity) { $eaRepositoty = $this->getEntityManager()->getRepository('EmailAddress'); $from = trim($entity->get('from')); if (!empty($from)) { $ids = $eaRepositoty->getIds(array($from)); if (!empty($ids)) { $entity->set('fromEmailAddressId', $ids[0]); } } else { $entity->set('fromEmailAddressId', null); } $this->prepareAddressess($entity, 'to'); $this->prepareAddressess($entity, 'cc'); $this->prepareAddressess($entity, 'bcc'); parent::beforeSave($entity); $parentId = $entity->get('parentId'); $parentType = $entity->get('parentType'); if (!empty($parentId) || !empty($parentType)) { $parent = $this->getEntityManager()->getEntity($parentType, $parentId); if (!empty($parent)) { if ($parent->getEntityName() == 'Account') { $accountId = $parent->id; } else { if ($parent->has('accountId')) { $accountId = $parent->get('accountId'); } } if (!empty($accountId)) { $entity->set('accountId', $accountId); } } } else { // TODO find account by from address } }
protected function parseText($type, Entity $entity, $text) { $fieldList = array_keys($entity->getFields()); $fieldList[] = $id; foreach ($fieldList as $field) { $value = $entity->get($field); if (is_object($value)) { continue; } if ($entity->fields[$field]['type'] == 'date') { $value = $this->getDateTime()->convertSystemDateToGlobal($value); } else { if ($entity->fields[$field]['type'] == 'datetime') { $value = $this->getDateTime()->convertSystemDateTimeToGlobal($value); } } $text = str_replace('{' . $type . '.' . $field . '}', $value, $text); } return $text; }
public function loadBccField(Entity $entity) { $entity->loadLinkMultipleField('bccEmailAddresses'); $names = $entity->get('bccEmailAddressesNames'); if (!empty($names)) { $arr = array(); foreach ($names as $id => $address) { $arr[] = $address; } $entity->set('bcc', implode(';', $arr)); } }
protected function getFieldFromEntityForExport(Entity $entity, $field) { $defs = $entity->getFields(); if (!empty($defs[$field]) && !empty($defs[$field]['type'])) { $type = $defs[$field]['type']; switch ($type) { case 'jsonArray': $value = $entity->get($field); if (is_array($value)) { return implode(',', $value); } else { return null; } break; case 'password': return null; break; } } return $entity->get($field); }
protected function handleSpecifiedRelations(Entity $entity) { $relationTypes = array($entity::HAS_MANY, $entity::MANY_MANY, $entity::HAS_CHILDREN); foreach ($entity->getRelations() as $name => $defs) { if (in_array($defs['type'], $relationTypes)) { $fieldName = $name . 'Ids'; $columnsFieldsName = $name . 'Columns'; if ($entity->has($fieldName) || $entity->has($columnsFieldsName)) { if ($this->getMetadata()->get("entityDefs." . $entity->getEntityType() . ".fields.{$name}.noSave")) { continue; } if ($entity->has($fieldName)) { $specifiedIds = $entity->get($fieldName); } else { $specifiedIds = array(); foreach ($entity->get($columnsFieldsName) as $id => $d) { $specifiedIds[] = $id; } } if (is_array($specifiedIds)) { $toRemoveIds = array(); $existingIds = array(); $toUpdateIds = array(); $existingColumnsData = new \stdClass(); $defs = array(); $columns = $this->getMetadata()->get("entityDefs." . $entity->getEntityType() . ".fields.{$name}.columns"); if (!empty($columns)) { $columnData = $entity->get($columnsFieldsName); $defs['additionalColumns'] = $columns; } $foreignCollection = $entity->get($name, $defs); if ($foreignCollection) { foreach ($foreignCollection as $foreignEntity) { $existingIds[] = $foreignEntity->id; if (!empty($columns)) { $data = new \stdClass(); foreach ($columns as $columnName => $columnField) { $foreignId = $foreignEntity->id; $data->{$columnName} = $foreignEntity->get($columnField); } $existingColumnsData->{$foreignId} = $data; $entity->setFetched($columnsFieldsName, $existingColumnsData); } } } if ($entity->has($fieldName)) { $entity->setFetched($fieldName, $existingIds); } if ($entity->has($columnsFieldsName) && !empty($columns)) { $entity->setFetched($columnsFieldsName, $existingColumnsData); } foreach ($existingIds as $id) { if (!in_array($id, $specifiedIds)) { $toRemoveIds[] = $id; } else { if (!empty($columns)) { foreach ($columns as $columnName => $columnField) { if ($columnData->{$id}->{$columnName} != $existingColumnsData->{$id}->{$columnName}) { $toUpdateIds[] = $id; } } } } } foreach ($specifiedIds as $id) { if (!in_array($id, $existingIds)) { $data = null; if (!empty($columns) && isset($columnData->{$id})) { $data = $columnData->{$id}; } $this->relate($entity, $name, $id, $data); } } foreach ($toRemoveIds as $id) { $this->unrelate($entity, $name, $id); } if (!empty($columns)) { foreach ($toUpdateIds as $id) { $data = $columnData->{$id}; $this->updateRelation($entity, $name, $id, $data); } } } } } } }
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); } }
protected function getDuplicateWhereClause(Entity $entity) { return array('name' => $entity->get('name')); }
protected function findDuplicate(Entity $email) { if ($email->get('messageId')) { $duplicate = $this->getEntityManager()->getRepository('Email')->where(array('messageId' => $email->get('messageId')))->findOne(); if ($duplicate) { return $duplicate; } } }
public function checkIsOwner(User $user, Entity $entity) { if ($entity->hasAttribute('assignedUserId')) { if ($entity->has('assignedUserId')) { if ($user->id === $entity->get('assignedUserId')) { return true; } } } else { if ($entity->hasAttribute('createdById')) { if ($entity->has('createdById')) { if ($user->id === $entity->get('createdById')) { return true; } } } } if ($entity->hasAttribute('assignedUsersIds') && $entity->hasRelation('assignedUsers')) { if ($entity->hasLinkMultipleId('assignedUsers', $user->id)) { return true; } } return false; }
public function storeEntityEmailAddress(Entity $entity) { $email = trim($entity->get('emailAddress')); $emailAddressData = null; if ($entity->has('emailAddressData')) { $emailAddressData = $entity->get('emailAddressData'); } $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; if (!empty($key)) { $hash[$key] = array('primary' => $row->primary ? true : false, 'optOut' => $row->optOut ? true : false, 'invalid' => $row->invalid ? true : false); } } $hashPrev = array(); foreach ($previousEmailAddressData as $row) { $key = $row->emailAddress; if (!empty($key)) { $hashPrev[$key] = array('primary' => $row->primary ? true : false, 'optOut' => $row->optOut ? true : false, 'invalid' => $row->invalid ? true : false); } } $primary = false; $toCreate = 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']; if ($hash[$key]['primary']) { if ($hash[$key]['primary'] == $hashPrev[$key]['primary']) { $primary = false; } } } if ($new) { $toCreate[] = $key; } if ($changed) { $toUpdate[] = $key; } } foreach ($hashPrev as $key => $data) { if (!array_key_exists($key, $hash)) { $toRemove[] = $key; } } foreach ($toRemove as $address) { $emailAddress = $this->getByAddress($address); if ($emailAddress) { $query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `deleted` = 1, `primary` = 0\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\temail_address_id = " . $pdo->quote($emailAddress->id) . "\n\t\t\t\t\t\t"; $sth = $pdo->prepare($query); $sth->execute(); } } foreach ($toUpdate as $address) { $emailAddress = $this->getByAddress($address); if ($emailAddress) { $emailAddress->set(array('optOut' => $hash[$address]['optOut'], 'invalid' => $hash[$address]['invalid'])); $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']) { $emailAddress->set(array('optOut' => $hash[$address]['optOut'], 'invalid' => $hash[$address]['invalid'])); $this->save($emailAddress); } } $query = "\n\t\t\t\t\t\tINSERT entity_email_address \n\t\t\t\t\t\t\t(entity_id, entity_type, email_address_id, `primary`)\n\t\t\t\t\t\t\tVALUES\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t" . $pdo->quote($entity->id) . ",\n\t\t\t\t\t\t\t\t" . $pdo->quote($entity->getEntityName()) . ",\n\t\t\t\t\t\t\t\t" . $pdo->quote($emailAddress->id) . ",\n\t\t\t\t\t\t\t\t" . $pdo->quote($address === $primary) . "\n\t\t\t\t\t\t\t)\n\t\t\t\t\t"; $sth = $pdo->prepare($query); $sth->execute(); } if ($primary) { $emailAddress = $this->getByAddress($primary); if ($emailAddress) { $query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `primary` = 0\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\t`primary` = 1 AND \n\t\t\t\t\t\t\t\tdeleted = 0\n\t\t\t\t\t\t"; $sth = $pdo->prepare($query); $sth->execute(); $query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `primary` = 1\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\temail_address_id = " . $pdo->quote($emailAddress->id) . " AND \n\t\t\t\t\t\t\t\tdeleted = 0\n\t\t\t\t\t\t"; $sth = $pdo->prepare($query); $sth->execute(); } } } else { $entityRepository = $this->getEntityManager()->getRepository($entity->getEntityName()); if (!empty($email)) { if ($email != $entity->getFetched('emailAddress')) { $emailAddressNew = $this->where(array('lower' => strtolower($email)))->findOne(); $isNewEmailAddress = false; if (!$emailAddressNew) { $emailAddressNew = $this->get(); $emailAddressNew->set('name', $email); $this->save($emailAddressNew); $isNewEmailAddress = true; } $emailOld = $entity->getFetched('emailAddress'); if (!empty($emailOld)) { $emailAddressOld = $this->getByAddress($emailOld); $entityRepository->unrelate($entity, 'emailAddresses', $emailAddressOld); } $entityRepository->relate($entity, 'emailAddresses', $emailAddressNew); $query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `primary` = 1\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\temail_address_id = " . $pdo->quote($emailAddressNew->id) . "\n\t\t\t\t\t\t"; $sth = $pdo->prepare($query); $sth->execute(); } } else { $emailOld = $entity->getFetched('emailAddress'); if (!empty($emailOld)) { $emailAddressOld = $this->getByAddress($emailOld); $entityRepository->unrelate($entity, 'emailAddresses', $emailAddressOld); } } } }
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 isPermittedTeams(Entity $entity) { $assignmentPermission = $this->getAcl()->get('assignmentPermission'); if (empty($assignmentPermission) || $assignmentPermission === true || !in_array($assignmentPermission, ['team', 'no'])) { return true; } if (!$entity->hasAttribute('teamsIds')) { return true; } $teamIds = $entity->get('teamsIds'); if (empty($teamIds)) { return true; } $newIds = []; if (!$entity->isNew()) { $existingIds = []; foreach ($entity->get('teams') as $team) { $existingIds[] = $team->id; } foreach ($teamIds as $id) { if (!in_array($id, $existingIds)) { $newIds[] = $id; } } } else { $newIds = $teamIds; } if (empty($newIds)) { return true; } $userTeamIds = $this->getUser()->get('teamsIds'); foreach ($newIds as $id) { if (!in_array($id, $userTeamIds)) { return false; } } return true; }
protected function sendQueueItem(Entity $queueItem, Entity $massEmail, Entity $emailTemplate, $attachmetList = [], $campaign = null, $isTest = false) { $target = $this->getEntityManager()->getEntity($queueItem->get('targetType'), $queueItem->get('targetId')); if (!$target || !$target->id || !$target->get('emailAddress')) { $queueItem->set('status', 'Failed'); $this->getEntityManager()->saveEntity($queueItem); return; } $emailAddress = $target->get('emailAddress'); if (!$emailAddress) { $queueItem->set('status', 'Failed'); $this->getEntityManager()->saveEntity($queueItem); return false; } $emailAddressRecord = $this->getEntityManager()->getRepository('EmailAddress')->getByAddress($emailAddress); if ($emailAddressRecord) { if ($emailAddressRecord->get('invalid') || $emailAddressRecord->get('optOut')) { $queueItem->set('status', 'Failed'); $this->getEntityManager()->saveEntity($queueItem); return false; } } $trackingUrlList = []; if ($campaign) { $trackingUrlList = $campaign->get('trackingUrls'); } $email = $this->getPreparedEmail($queueItem, $massEmail, $emailTemplate, $target, $trackingUrlList); $params = array(); if ($massEmail->get('fromName')) { $params['fromName'] = $massEmail->get('fromName'); } if ($massEmail->get('replyToName')) { $params['replyToName'] = $massEmail->get('replyToName'); } try { $attemptCount = $queueItem->get('attemptCount'); $attemptCount++; $queueItem->set('attemptCount', $attemptCount); $message = new \Zend\Mail\Message(); $header = new \Espo\Core\Mail\Mail\Header\XQueueItemId(); $header->setId($queueItem->id); $message->getHeaders()->addHeader($header); $this->getMailSender()->useGlobal()->send($email, $params, $message, $attachmetList); $emailObject = $emailTemplate; if ($massEmail->get('storeSentEmails')) { $this->getEntityManager()->saveEntity($email); $emailObject = $email; } $queueItem->set('emailAddress', $target->get('emailAddress')); $queueItem->set('status', 'Sent'); $queueItem->set('sentAt', date('Y-m-d H:i:s')); $this->getEntityManager()->saveEntity($queueItem); if ($campaign) { $this->getCampaignService()->logSent($campaign->id, $queueItem->id, $target, $emailObject, $target->get('emailAddress')); } } catch (\Exception $e) { if ($queueItem->get('attemptCount') >= self::MAX_ATTEMPT_COUNT) { $queueItem->set('status', 'Failed'); } $this->getEntityManager()->saveEntity($queueItem); $GLOBALS['log']->error('MassEmail#sendQueueItem: [' . $e->getCode() . '] ' . $e->getMessage()); return false; } return true; }