entityManager = $entityManager; $this->attachmentAccessChecker = $attachmentAccessChecker; $this->serviceContainer = $serviceContainer; $this->selectBuilderFactory = $selectBuilderFactory; $this->acl = $acl; } /** * Copy article attachments for re-using (e.g. in an email). * * @return Attachment[] * @throws NotFound * @throws Forbidden */ public function copyAttachments(string $id, FieldData $fieldData): array { /** @var ?KnowledgeBaseArticle $entity */ $entity = $this->serviceContainer ->get(KnowledgeBaseArticle::ENTITY_TYPE) ->getEntity($id); if (!$entity) { throw new NotFound(); } $this->attachmentAccessChecker->check($fieldData); $list = []; foreach ($entity->getAttachmentIdList() as $attachmentId) { $attachment = $this->copyAttachment($attachmentId, $fieldData); if ($attachment) { $list[] = $attachment; } } return $list; } private function copyAttachment(string $attachmentId, FieldData $fieldData): ?Attachment { /** @var ?Attachment $attachment */ $attachment = $this->entityManager ->getRDBRepositoryByClass(Attachment::class) ->getById($attachmentId); if (!$attachment) { return null; } $copied = $this->getAttachmentRepository()->getCopiedAttachment($attachment); $copied->set('parentType', $fieldData->getParentType()); $copied->set('relatedType', $fieldData->getRelatedType()); $copied->setTargetField($fieldData->getField()); $copied->setRole(Attachment::ROLE_ATTACHMENT); $this->getAttachmentRepository()->save($copied); return $copied; } private function getAttachmentRepository(): AttachmentRepository { /** @var AttachmentRepository */ return $this->entityManager->getRepositoryByClass(Attachment::class); } /** * @throws NotFound * @throws Forbidden * @throws Error */ public function moveUp(string $id, ?WhereItem $where = null): void { /** @var ?KnowledgeBaseArticle $entity */ $entity = $this->entityManager->getEntityById(KnowledgeBaseArticle::ENTITY_TYPE, $id); if (!$entity) { throw new NotFound(); } if (!$this->acl->checkEntityEdit($entity)) { throw new Forbidden(); } $currentIndex = $entity->getOrder(); if (!is_int($currentIndex)) { throw new Error(); } $params = SearchParams::create(); if ($where) { $params = $params->withWhere($where); } $query = $this->selectBuilderFactory ->create() ->from(KnowledgeBaseArticle::ENTITY_TYPE) ->withStrictAccessControl() ->withSearchParams($params) ->buildQueryBuilder() ->where([ 'order<' => $currentIndex, ]) ->order([ ['order', 'DESC'], ]) ->build(); /** @var ?KnowledgeBaseArticle $previousEntity */ $previousEntity = $this->entityManager ->getRDBRepositoryByClass(KnowledgeBaseArticle::class) ->clone($query) ->findOne(); if (!$previousEntity) { return; } $entity->set('order', $previousEntity->getOrder()); $previousEntity->set('order', $currentIndex); $this->entityManager->saveEntity($entity); $this->entityManager->saveEntity($previousEntity); } /** * @throws NotFound * @throws Forbidden * @throws Error */ public function moveDown(string $id, ?WhereItem $where = null): void { /** @var ?KnowledgeBaseArticle $entity */ $entity = $this->entityManager->getEntityById(KnowledgeBaseArticle::ENTITY_TYPE, $id); if (!$entity) { throw new NotFound(); } if (!$this->acl->checkEntityEdit($entity)) { throw new Forbidden(); } $currentIndex = $entity->getOrder(); if (!is_int($currentIndex)) { throw new Error(); } $params = SearchParams::create(); if ($where) { $params = $params->withWhere($where); } $query = $this->selectBuilderFactory ->create() ->from(KnowledgeBaseArticle::ENTITY_TYPE) ->withStrictAccessControl() ->withSearchParams($params) ->buildQueryBuilder() ->where([ 'order>' => $currentIndex, ]) ->order([ ['order', 'ASC'], ]) ->build(); /** @var ?KnowledgeBaseArticle $nextEntity */ $nextEntity = $this->entityManager ->getRDBRepositoryByClass(KnowledgeBaseArticle::class) ->clone($query) ->findOne(); if (!$nextEntity) { return; } $entity->set('order', $nextEntity->getOrder()); $nextEntity->set('order', $currentIndex); $this->entityManager->saveEntity($entity); $this->entityManager->saveEntity($nextEntity); } /** * @throws NotFound * @throws Forbidden * @throws Error */ public function moveToTop(string $id, ?WhereItem $where = null): void { /** @var ?KnowledgeBaseArticle $entity */ $entity = $this->entityManager->getEntityById(KnowledgeBaseArticle::ENTITY_TYPE, $id); if (!$entity) { throw new NotFound(); } if (!$this->acl->checkEntityEdit($entity)) { throw new Forbidden(); } $currentIndex = $entity->getOrder(); if (!is_int($currentIndex)) { throw new Error(); } $params = SearchParams::create(); if ($where) { $params = $params->withWhere($where); } $query = $this->selectBuilderFactory ->create() ->from(KnowledgeBaseArticle::ENTITY_TYPE) ->withStrictAccessControl() ->withSearchParams($params) ->buildQueryBuilder() ->where([ 'order<' => $currentIndex, ]) ->order([ ['order', 'ASC'], ]) ->build(); /** @var ?KnowledgeBaseArticle $previousEntity */ $previousEntity = $this->entityManager ->getRDBRepositoryByClass(KnowledgeBaseArticle::class) ->clone($query) ->findOne(); if (!$previousEntity) { return; } $entity->set('order', $previousEntity->getOrder() - 1); $this->entityManager->saveEntity($entity); } /** * @throws NotFound * @throws Forbidden * @throws Error */ public function moveToBottom(string $id, ?WhereItem $where = null): void { /** @var ?KnowledgeBaseArticle $entity */ $entity = $this->entityManager->getEntityById(KnowledgeBaseArticle::ENTITY_TYPE, $id); if (!$entity) { throw new NotFound(); } if (!$this->acl->checkEntityEdit($entity)) { throw new Forbidden(); } $currentIndex = $entity->getOrder(); if (!is_int($currentIndex)) { throw new Error(); } $params = SearchParams::create(); if ($where) { $params = $params->withWhere($where); } $query = $this->selectBuilderFactory ->create() ->from(KnowledgeBaseArticle::ENTITY_TYPE) ->withStrictAccessControl() ->withSearchParams($params) ->buildQueryBuilder() ->where([ 'order>' => $currentIndex, ]) ->order([ ['order', 'DESC'], ]) ->build(); /** @var ?KnowledgeBaseArticle $nextEntity */ $nextEntity = $this->entityManager ->getRDBRepositoryByClass(KnowledgeBaseArticle::class) ->clone($query) ->findOne(); if (!$nextEntity) { return; } $entity->set('order', $nextEntity->getOrder() + 1); $this->entityManager->saveEntity($entity); } }