*/ class EmailTemplate extends Record implements Di\FieldUtilAware { use Di\FieldUtilSetter; /** * @param array $params * @return array{ * subject: string, * body: string, * isHtml: bool, * attachmentsIds: string[], * attachmentsNames: \stdClass, * } */ public function parseTemplate( EmailTemplateEntity $emailTemplate, array $params = [], bool $copyAttachments = false, bool $skipAcl = false ): array { $paramsInternal = Params::create() ->withApplyAcl(!$skipAcl) ->withCopyAttachments($copyAttachments); $data = Data::create() ->withEmailAddress($params['emailAddress'] ?? null) ->withEntityHash($params['entityHash'] ?? []) ->withParent($params['parent'] ?? null) ->withParentId($params['parentId'] ?? null) ->withParentType($params['parentType'] ?? null) ->withRelatedId($params['relatedId'] ?? null) ->withRelatedType($params['relatedType'] ?? null) ->withUser($this->user); $result = $this->createProcessor()->process($emailTemplate, $paramsInternal, $data); /** @var array{ * subject: string, * body: string, * isHtml: bool, * attachmentsIds: string[], * attachmentsNames: \stdClass, * } */ return get_object_vars($result->getValueMap()); } /** * @param array $params * @return array * @throws \Espo\Core\Exceptions\ForbiddenSilent * @throws NotFound */ public function parse(string $id, array $params = [], bool $copyAttachments = false): array { /** @var EmailTemplateEntity|null */ $emailTemplate = $this->getEntity($id); if (empty($emailTemplate)) { throw new NotFound(); } return $this->parseTemplate($emailTemplate, $params, $copyAttachments); } /** * @param array $params */ public function getInsertFieldData(array $params): stdClass { $to = $params['to'] ?? null; $parentId = $params['parentId'] ?? null; $parentType = $params['parentType'] ?? null; $result = (object) []; $dataList = []; if ($parentId && $parentType) { $e = $this->entityManager->getEntity($parentType, $parentId); if ($e && $this->acl->check($e)) { $dataList[] = [ 'type' => 'parent', 'entity' => $e, ]; } } if ($to) { $e = $this->getEmailAddressRepository()->getEntityByAddress($to, null, ['Contact', 'Lead', 'Account']); if ($e && $e->getEntityType() !== 'User' && $this->acl->check($e)) { $dataList[] = [ 'type' => 'to', 'entity' => $e, ]; } } $fm = $this->fieldUtil; $formatter = $this->createFormatter(); foreach ($dataList as $item) { $type = $item['type']; $e = $item['entity']; $entityType = $e->getEntityType(); $recordService = $this->recordServiceContainer->get($entityType); $recordService->prepareEntityForOutput($e); $ignoreTypeList = ['image', 'file', 'map', 'wysiwyg', 'linkMultiple', 'attachmentMultiple', 'bool']; foreach ($fm->getEntityTypeFieldList($entityType) as $field) { $fieldType = $fm->getEntityTypeFieldParam($entityType, $field, 'type'); $fieldAttributeList = $fm->getAttributeList($entityType, $field); if ( $fm->getEntityTypeFieldParam($entityType, $field, 'disabled') || $fm->getEntityTypeFieldParam($entityType, $field, 'directAccessDisabled') || $fm->getEntityTypeFieldParam($entityType, $field, 'templatePlaceholderDisabled') || in_array($fieldType, $ignoreTypeList) ) { foreach ($fieldAttributeList as $a) { $e->clear($a); } } } $attributeList = $fm->getEntityTypeAttributeList($entityType); $values = (object) []; foreach ($attributeList as $a) { if (!$e->has($a)) { continue; } $value = $formatter->formatAttributeValue($e, $a); if ($value !== null && $value !== '') { $values->$a = $value; } } $result->$type = (object) [ 'entityType' => $e->getEntityType(), 'id' => $e->getId(), 'values' => $values, 'name' => $e->get('name'), ]; } return $result; } private function createProcessor(): Processor { return $this->injectableFactory->create(Processor::class); } private function createFormatter(): Formatter { return $this->injectableFactory->create(Formatter::class); } private function getEmailAddressRepository(): EmailAddressRepository { /** @var EmailAddressRepository */ return $this->entityManager->getRepository(EmailAddress::ENTITY_TYPE); } }