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'))); } } } }
public function beforeSave(Entity $entity) { if (!$entity->has('executeTime')) { $entity->set('executeTime', date('Y-m-d H:i:s')); } if (!$entity->has('attempts')) { $attempts = $this->getConfig()->get('cron.attempts', 0); $entity->set('attempts', $attempts); } }
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; }
public function afterSave(Entity $entity) { if ($this->getConfig()->get('assignmentEmailNotifications') && $entity->has('assignedUserId') && 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); } } }
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); } } }
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 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 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); }
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 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 noteStatus(Entity $entity, $field) { $note = $this->getEntityManager()->getEntity('Note'); $note->set('type', 'Status'); $note->set('parentId', $entity->id); $note->set('parentType', $entity->getEntityType()); if ($entity->has('accountId') && $entity->get('accountId')) { $note->set('superParentId', $entity->get('accountId')); $note->set('superParentType', 'Account'); } $style = 'default'; $entityType = $entity->getEntityType(); $value = $entity->get($field); $statusStyles = $this->getStatusStyles(); if (!empty($statusStyles[$entityType]) && !empty($statusStyles[$entityType][$value])) { $style = $statusStyles[$entityType][$value]; } $note->set('data', array('field' => $field, 'value' => $value, 'style' => $style)); $this->getEntityManager()->saveEntity($note); }
protected function handleSpecifiedRelations(Entity $entity) { $relationTypeList = [$entity::HAS_MANY, $entity::MANY_MANY, $entity::HAS_CHILDREN]; foreach ($entity->getRelations() as $name => $defs) { if (in_array($defs['type'], $relationTypeList)) { $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); } } } } } else { if ($defs['type'] === $entity::HAS_ONE) { if (empty($defs['entity']) || empty($defs['foreignKey'])) { return; } if ($this->getMetadata()->get("entityDefs." . $entity->getEntityType() . ".fields.{$name}.noSave")) { continue; } $foreignEntityType = $defs['entity']; $foreignKey = $defs['foreignKey']; $idFieldName = $name . 'Id'; $nameFieldName = $name . 'Name'; if (!$entity->has($idFieldName)) { return; } $where = array(); $where[$foreignKey] = $entity->id; $previousForeignEntity = $this->getEntityManager()->getRepository($foreignEntityType)->where($where)->findOne(); if ($previousForeignEntity) { $entity->setFetched($idFieldName, $previousForeignEntity->id); if ($previousForeignEntity->id !== $entity->get($idFieldName)) { $previousForeignEntity->set($foreignKey, null); $this->getEntityManager()->saveEntity($previousForeignEntity); } } else { $entity->setFetched($idFieldName, null); } if ($entity->get($idFieldName)) { $newForeignEntity = $this->getEntityManager()->getEntity($foreignEntityType, $entity->get($idFieldName)); if ($newForeignEntity) { $newForeignEntity->set($foreignKey, $entity->id); $this->getEntityManager()->saveEntity($newForeignEntity); } else { $entity->set($idFieldName, null); } } } } } }
protected function parseValue(Entity $entity, $field, $value, $params = array()) { $decimalMark = '.'; if (!empty($params['decimalMark'])) { $decimalMark = $params['decimalMark']; } $defaultCurrency = 'USD'; if (!empty($params['defaultCurrency'])) { $defaultCurrency = $params['defaultCurrency']; } $dateFormat = 'Y-m-d'; if (!empty($params['dateFormat'])) { if (!empty($this->dateFormatsMap[$params['dateFormat']])) { $dateFormat = $this->dateFormatsMap[$params['dateFormat']]; } } $timeFormat = 'H:i'; if (!empty($params['timeFormat'])) { if (!empty($this->timeFormatsMap[$params['timeFormat']])) { $timeFormat = $this->timeFormatsMap[$params['timeFormat']]; } } $fieldDefs = $entity->getFields(); if (!empty($fieldDefs[$field])) { $type = $fieldDefs[$field]['type']; switch ($type) { case Entity::DATE: $dt = \DateTime::createFromFormat($dateFormat, $value); if ($dt) { return $dt->format('Y-m-d'); } break; case Entity::DATETIME: $dt = \DateTime::createFromFormat($dateFormat . ' ' . $timeFormat, $value); if ($dt) { return $dt->format('Y-m-d H:i'); } break; case Entity::FLOAT: $currencyField = $field . 'Currency'; if ($entity->hasField($currencyField)) { if (!$entity->has($currencyField)) { $entity->set($currencyField, $defaultCurrency); } } $a = explode($decimalMark, $value); $a[0] = preg_replace('/[^A-Za-z0-9\\-]/', '', $a[0]); if (count($a) > 1) { return $a[0] . '.' . $a[1]; } else { return $a[0]; } break; } } return $value; }
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); } } } }
public function createQueue(Entity $massEmail, $isTest = false, $additionalTargetList = []) { if (!$isTest && $massEmail->get('status') !== 'Pending') { throw new Error("Mass Email '" . $massEmail->id . "' should be 'Pending'."); } if (!$isTest) { $existingQueueItemList = $this->getEntityManager()->getRepository('EmailQueueItem')->where(array('status' => ['Pending', 'Failed'], 'massEmailId' => $massEmail->id))->find(); foreach ($existingQueueItemList as $existingQueueItem) { $this->getEntityManager()->getMapper('RDB')->deleteFromDb('EmailQueueItem', $existingQueueItem->id); } } $targetHash = array(); $entityList = []; $pdo = $this->getEntityManager()->getPDO(); if (!$isTest) { if (!$massEmail->has('excludingTargetListsIds')) { $massEmail->loadLinkMultipleField('excludingTargetLists'); } $excludingTargetListIdList = $massEmail->get('excludingTargetListsIds'); $targetListCollection = $massEmail->get('targetLists'); foreach ($targetListCollection as $targetList) { $accountList = $targetList->get('accounts', array('additionalColumnsConditions' => array('optedOut' => false))); foreach ($accountList as $account) { $hashId = $account->getEntityType() . '-' . $account->id; if (!empty($targetHash[$hashId])) { continue; } $toExclude = false; foreach ($excludingTargetListIdList as $excludingTargetListId) { $sql = "\n SELECT id FROM account_target_list\n WHERE\n account_id = " . $pdo->quote($account->id) . " AND\n target_list_id = " . $pdo->quote($excludingTargetListId) . " AND\n deleted = 0\n "; $sth = $pdo->prepare($sql); $sth->execute(); if ($sth->fetch()) { $toExclude = true; break; } } if ($toExclude) { continue; } $entityList[] = $account; $targetHash[$hashId] = true; } $contactList = $targetList->get('contacts', array('additionalColumnsConditions' => array('optedOut' => false))); foreach ($contactList as $contact) { $hashId = $contact->getEntityType() . '-' . $contact->id; if (!empty($targetHash[$hashId])) { continue; } $toExclude = false; foreach ($excludingTargetListIdList as $excludingTargetListId) { $sql = "\n SELECT id FROM contact_target_list\n WHERE\n contact_id = " . $pdo->quote($contact->id) . " AND\n target_list_id = " . $pdo->quote($excludingTargetListId) . " AND\n deleted = 0\n "; $sth = $pdo->prepare($sql); $sth->execute(); if ($sth->fetch()) { $toExclude = true; break; } } if ($toExclude) { continue; } $entityList[] = $contact; $targetHash[$hashId] = true; } $leadList = $targetList->get('leads', array('additionalColumnsConditions' => array('optedOut' => false))); foreach ($leadList as $lead) { $hashId = $lead->getEntityType() . '-' . $lead->id; if (!empty($targetHash[$hashId])) { continue; } $toExclude = false; foreach ($excludingTargetListIdList as $excludingTargetListId) { $sql = "\n SELECT id FROM lead_target_list\n WHERE\n lead_id = " . $pdo->quote($lead->id) . " AND\n target_list_id = " . $pdo->quote($excludingTargetListId) . " AND\n deleted = 0\n "; $sth = $pdo->prepare($sql); $sth->execute(); if ($sth->fetch()) { $toExclude = true; break; } } if ($toExclude) { continue; } $entityList[] = $lead; $targetHash[$hashId] = true; } $userList = $targetList->get('users', array('additionalColumnsConditions' => array('optedOut' => false))); foreach ($userList as $user) { $hashId = $user->getEntityType() . '-' . $user->id; if (!empty($targetHash[$hashId])) { continue; } $toExclude = false; foreach ($excludingTargetListIdList as $excludingTargetListId) { $sql = "\n SELECT id FROM target_list_user\n WHERE\n user_id = " . $pdo->quote($user->id) . " AND\n target_list_id = " . $pdo->quote($excludingTargetListId) . " AND\n deleted = 0\n "; $sth = $pdo->prepare($sql); $sth->execute(); if ($sth->fetch()) { $toExclude = true; break; } } if ($toExclude) { continue; } $entityList[] = $user; $targetHash[$hashId] = true; } } } foreach ($additionalTargetList as $target) { $entityList[] = $target; } foreach ($entityList as $target) { $emailAddress = $target->get('emailAddress'); if (!$target->get('emailAddress')) { continue; } $emailAddressRecord = $this->getEntityManager()->getRepository('EmailAddress')->getByAddress($emailAddress); if ($emailAddressRecord) { if ($emailAddressRecord->get('invalid') || $emailAddressRecord->get('optOut')) { continue; } } $queueItem = $this->getEntityManager()->getEntity('EmailQueueItem'); $queueItem->set(array('massEmailId' => $massEmail->id, 'status' => 'Pending', 'targetId' => $target->id, 'targetType' => $target->getEntityType(), 'isTest' => $isTest)); $this->getEntityManager()->saveEntity($queueItem); } if (!$isTest) { $massEmail->set('status', 'In Process'); if (empty($entityList)) { $massEmail->set('status', 'Complete'); } $this->getEntityManager()->saveEntity($massEmail); } }
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); } }
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 storeEntityPhoneNumber(Entity $entity) { $phone = trim($entity->get('phoneNumber')); $phoneNumberData = null; if ($entity->has('phoneNumberData')) { $phoneNumberData = $entity->get('phoneNumberData'); } $pdo = $this->getEntityManager()->getPDO(); if ($phoneNumberData !== null && is_array($phoneNumberData)) { $previousPhoneNumberData = array(); if (!$entity->isNew()) { $previousPhoneNumberData = $this->getPhoneNumberData($entity); } $hash = array(); foreach ($phoneNumberData as $row) { $key = $row->phoneNumber; if (!empty($key)) { $hash[$key] = array('primary' => $row->primary ? true : false, 'type' => $row->type); } } $hashPrev = array(); foreach ($previousPhoneNumberData as $row) { $key = $row->phoneNumber; if (!empty($key)) { $hashPrev[$key] = array('primary' => $row->primary ? true : false, 'type' => $row->type); } } $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]['type'] != $hashPrev[$key]['type']; 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 $number) { $phoneNumber = $this->getByNumber($number); if ($phoneNumber) { $query = "\n DELETE FROM entity_phone_number\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n phone_number_id = " . $pdo->quote($phoneNumber->id) . "\n "; $sth = $pdo->prepare($query); $sth->execute(); } } foreach ($toUpdate as $number) { $phoneNumber = $this->getByNumber($number); if ($phoneNumber) { $phoneNumber->set(array('type' => $hash[$number]['type'])); $this->save($phoneNumber); } } foreach ($toCreate as $number) { $phoneNumber = $this->getByNumber($number); if (!$phoneNumber) { $phoneNumber = $this->get(); $phoneNumber->set(array('name' => $number, 'type' => $hash[$number]['type'])); $this->save($phoneNumber); } else { if ($phoneNumber->get('type') != $hash[$number]['type']) { $phoneNumber->set(array('type' => $hash[$number]['type'])); $this->save($phoneNumber); } } $query = "\n INSERT entity_phone_number\n (entity_id, entity_type, phone_number_id, `primary`)\n VALUES\n (\n " . $pdo->quote($entity->id) . ",\n " . $pdo->quote($entity->getEntityName()) . ",\n " . $pdo->quote($phoneNumber->id) . ",\n " . $pdo->quote((int) ($number === $primary)) . "\n )\n "; $sth = $pdo->prepare($query); $sth->execute(); } if ($primary) { $phoneNumber = $this->getByNumber($primary); if ($phoneNumber) { $query = "\n UPDATE entity_phone_number\n SET `primary` = 0\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n `primary` = 1 AND\n deleted = 0\n "; $sth = $pdo->prepare($query); $sth->execute(); $query = "\n UPDATE entity_phone_number\n SET `primary` = 1\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n phone_number_id = " . $pdo->quote($phoneNumber->id) . " AND \n deleted = 0\n "; $sth = $pdo->prepare($query); $sth->execute(); } } } else { $entityRepository = $this->getEntityManager()->getRepository($entity->getEntityName()); if (!empty($phone)) { if ($phone != $entity->getFetched('phoneNumber')) { $phoneNumberNew = $this->where(array('name' => $phone))->findOne(); $isNewPhoneNumber = false; if (!$phoneNumberNew) { $phoneNumberNew = $this->get(); $phoneNumberNew->set('name', $phone); $defaultType = $this->getEntityManager()->getEspoMetadata()->get('entityDefs.' . $entity->getEntityName() . '.fields.phoneNumber.defaultType'); $phoneNumberNew->set('type', $defaultType); $this->save($phoneNumberNew); $isNewPhoneNumber = true; } $phoneOld = $entity->getFetched('phoneNumber'); if (!empty($phoneOld)) { $phoneNumberOld = $this->getByNumber($phoneOld); $entityRepository->unrelate($entity, 'phoneNumbers', $phoneNumberOld); } $entityRepository->relate($entity, 'phoneNumbers', $phoneNumberNew); $query = "\n UPDATE entity_phone_number\n SET `primary` = 1\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n phone_number_id = " . $pdo->quote($phoneNumberNew->id) . "\n "; $sth = $pdo->prepare($query); $sth->execute(); } } else { $phoneOld = $entity->getFetched('phoneNumber'); if (!empty($phoneOld)) { $phoneNumberOld = $this->getByNumber($phoneOld); $entityRepository->unrelate($entity, 'phoneNumbers', $phoneNumberOld); } } } }
public function checkInTeam(User $user, Entity $entity) { $userTeamIds = $user->get('teamsIds'); if (!$entity->hasRelation('teams') || !$entity->hasField('teamsIds')) { return false; } if (!$entity->has('teamsIds')) { $entity->loadLinkMultipleField('teams'); } $teamIds = $entity->get('teamsIds'); if (empty($teamIds)) { return false; } foreach ($userTeamIds as $id) { if (in_array($id, $teamIds)) { return true; } } return false; }
protected function beforeSave(Entity $entity, array $options) { $eaRepositoty = $this->getEntityManager()->getRepository('EmailAddress'); if ($entity->has('attachmentsIds')) { $attachmentsIds = $entity->get('attachmentsIds'); if (!empty($attachmentsIds)) { $entity->set('hasAttachment', true); } } if ($entity->has('from') || $entity->has('to') || $entity->has('cc') || $entity->has('bcc') || $entity->has('replyTo')) { if (!$entity->has('usersIds')) { $entity->loadLinkMultipleField('users'); } if ($entity->has('from')) { $from = trim($entity->get('from')); if (!empty($from)) { $ids = $eaRepositoty->getIds(array($from)); if (!empty($ids)) { $entity->set('fromEmailAddressId', $ids[0]); $this->setUsersIdsByEmailAddressId($entity, $ids[0]); } } else { $entity->set('fromEmailAddressId', null); } } if ($entity->has('to')) { $this->prepareAddressess($entity, 'to'); } if ($entity->has('cc')) { $this->prepareAddressess($entity, 'cc'); } if ($entity->has('bcc')) { $this->prepareAddressess($entity, 'bcc'); } if ($entity->has('replyTo')) { $this->prepareAddressess($entity, 'replyTo'); } $usersIds = $entity->get('usersIds'); $assignedUserId = $entity->get('assignedUserId'); if (!empty($assignedUserId) && !in_array($assignedUserId, $usersIds)) { $usersIds[] = $assignedUserId; } $entity->set('usersIds', $usersIds); } parent::beforeSave($entity, $options); $parentId = $entity->get('parentId'); $parentType = $entity->get('parentType'); if (!empty($parentId) || !empty($parentType)) { $parent = $this->getEntityManager()->getEntity($parentType, $parentId); if (!empty($parent)) { if ($parent->getEntityType() == 'Account') { $accountId = $parent->id; } else { if ($parent->has('accountId')) { $accountId = $parent->get('accountId'); } } if (!empty($accountId)) { $account = $this->getEntityManager()->getEntity('Account', $accountId); if ($account) { $entity->set('accountId', $accountId); $entity->set('accountName', $account->get('name')); } } } } }
public function checkIsOwner(User $user, Entity $entity) { if ($entity->hasAttribute('createdById')) { if ($entity->has('createdById')) { if ($user->id === $entity->get('createdById')) { return true; } } } return false; }
public function noteAssign(Entity $entity) { $note = $this->getEntityManager()->getEntity('Note'); $note->set('type', 'Assign'); $note->set('parentId', $entity->id); $note->set('parentType', $entity->getEntityName()); if (!$entity->has('assignedUserName')) { $this->loadAssignedUserName($entity); } $note->set('data', json_encode(array('assignedUserId' => $entity->get('assignedUserId'), 'assignedUserName' => $entity->get('assignedUserName')))); $this->getEntityManager()->saveEntity($note); }