From 3bd73f16a8a8d917b0d88f44fd0aeb598e84cb0d Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sat, 17 Dec 2022 10:39:04 +0200 Subject: [PATCH] ref --- application/Espo/Core/Htmlizer/Htmlizer.php | 121 ++++++++++++-------- 1 file changed, 74 insertions(+), 47 deletions(-) diff --git a/application/Espo/Core/Htmlizer/Htmlizer.php b/application/Espo/Core/Htmlizer/Htmlizer.php index 11419df35b..ad8d994c17 100644 --- a/application/Espo/Core/Htmlizer/Htmlizer.php +++ b/application/Espo/Core/Htmlizer/Htmlizer.php @@ -61,9 +61,9 @@ use const JSON_PRESERVE_ZERO_FRACTION; */ class Htmlizer { - /** - * @phpstan-ignore-next-line - */ + private const LINK_LIMIT = 100; + + /** @phpstan-ignore-next-line */ private $fileManager; private $dateTime; private $number; @@ -228,8 +228,8 @@ class Htmlizer } /** - * @param ?array $additionalData - * @return array + * @param ?array $additionalData + * @return array */ private function getDataFromEntity( Entity $entity, @@ -270,52 +270,17 @@ class Htmlizer ); } - $relationList = $entity->getRelationList(); - - if (!$skipLinks && $level === 0 && $this->entityManager && $entity->hasId()) { - foreach ($relationList as $relation) { - $collection = null; - - $orderData = $this->getRelationOrder($entity->getEntityType(), $relation); - - if ($entity instanceof CoreEntity && $entity->hasLinkMultipleField($relation)) { - $collection = $this->entityManager - ->getRDBRepository($entity->getEntityType()) - ->getRelation($entity, $relation) - ->order($orderData) - ->find(); - } - else if ( - $template && - in_array( - $entity->getRelationType($relation), - ['hasMany', 'manyMany', 'hasChildren'] - ) && - mb_stripos($template, '{{#each '.$relation.'}}') !== false - ) { - $limit = 100; - - if ($this->config) { - $limit = $this->config->get('htmlizerLinkLimit') ?? $limit; - } - - $collection = $this->entityManager - ->getRDBRepository($entity->getEntityType()) - ->getRelation($entity, $relation) - ->limit(0, $limit) - ->order($orderData) - ->find(); - } - - if ($collection) { - $data[$relation] = $collection; - } - } + if ( + !$skipLinks && + $level === 0 && + $this->entityManager && + $entity->hasId() + ) { + $this->loadRelatedCollections($entity, $template, $data); } foreach ($data as $key => $value) { if ($value instanceof Collection) { - $skipAttributeList[] = $key; /** @var iterable $collection */ @@ -488,6 +453,68 @@ class Htmlizer return $data; } + /** + * @param array $data + */ + private function loadRelatedCollections(Entity $entity, ?string $template, array &$data): void + { + foreach ($entity->getRelationList() as $relation) { + $collection = $this->loadRelatedCollection($entity, $relation, $template); + + if ($collection) { + $data[$relation] = $collection; + } + } + } + + /** + * @return ?Collection + */ + private function loadRelatedCollection(Entity $entity, string $relation, ?string $template): ?Collection + { + assert($this->entityManager !== null); + + $limit = $this->config ? + $this->config->get('htmlizerLinkLimit', self::LINK_LIMIT) : + self::LINK_LIMIT; + + $orderData = $this->getRelationOrder($entity->getEntityType(), $relation); + + if ( + $entity instanceof CoreEntity && + $entity->hasLinkMultipleField($relation) + ) { + return $this->entityManager + ->getRDBRepository($entity->getEntityType()) + ->getRelation($entity, $relation) + ->limit(0, $limit) + ->order($orderData) + ->find(); + } + + if ( + $template && + in_array( + $entity->getRelationType($relation), + [ + Entity::HAS_MANY, + Entity::MANY_MANY, + Entity::HAS_CHILDREN, + ] + ) && + mb_stripos($template, '{{#each ' . $relation . '}}') !== false + ) { + return $this->entityManager + ->getRDBRepository($entity->getEntityType()) + ->getRelation($entity, $relation) + ->limit(0, $limit) + ->order($orderData) + ->find(); + } + + return null; + } + /** * @return array */