addDependencyList([ 'fileStorageManager' ]); } protected function getFileStorageManager() { return $this->fileStorageManager; } public function getCopiedAttachments(string $id, ?string $parentType = null, ?string $parentId = null): stdClass { $ids = []; $names = (object) []; if (empty($id)) { throw new BadRequest(); } /** @var KnowledgeBaseArticleEntity|null $entity */ $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { throw new NotFound(); } if (!$this->getAcl()->checkEntity($entity, 'read')) { throw new Forbidden(); } $entity->loadLinkMultipleField('attachments'); $attachmentsIds = $entity->get('attachmentsIds'); foreach ($attachmentsIds as $attachmentId) { /** @var Attachment|null $source */ $source = $this->getEntityManager()->getEntity('Attachment', $attachmentId); if ($source) { $attachment = $this->getEntityManager()->getEntity('Attachment'); $attachment->set('role', 'Attachment'); $attachment->set('type', $source->get('type')); $attachment->set('size', $source->get('size')); $attachment->set('global', $source->get('global')); $attachment->set('name', $source->get('name')); $attachment->set('sourceId', $source->getSourceId()); $attachment->set('storage', $source->get('storage')); if (!empty($parentType) && !empty($parentId)) { $attachment->set('parentType', $parentType); $attachment->set('parentId', $parentId); } if ($this->getFileStorageManager()->exists($source)) { $this->getEntityManager()->saveEntity($attachment); $contents = $this->getFileStorageManager()->getContents($source) ?? ''; $this->getFileStorageManager()->putContents($attachment, $contents); $ids[] = $attachment->getId(); $names->{$attachment->getId()} = $attachment->get('name'); } } } return (object) [ 'ids' => $ids, 'names' => $names, ]; } public function moveUp(string $id, ?array $where = null) { $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { throw new NotFound(); } if (!$this->getAcl()->check($entity, 'edit')) { throw new Forbidden(); } $currentIndex = $entity->get('order'); if (!is_int($currentIndex)) { throw new Error(); } if (!$where) { $where = []; } $params = [ 'where' => $where, ]; $query = $this->selectBuilderFactory ->create() ->from($this->entityType) ->withStrictAccessControl() ->withSearchParams(SearchParams::fromRaw($params)) ->buildQueryBuilder() ->where([ 'order<' => $currentIndex, ]) ->order([ ['order', 'DESC'], ]) ->build(); $previousEntity = $this->getRepository() ->clone($query) ->findOne(); if (!$previousEntity) { return; } $entity->set('order', $previousEntity->get('order')); $previousEntity->set('order', $currentIndex); $this->getEntityManager()->saveEntity($entity); $this->getEntityManager()->saveEntity($previousEntity); } public function moveDown(string $id, ?array $where = null) { $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { throw new NotFound(); } if (!$this->getAcl()->check($entity, 'edit')) { throw new Forbidden(); } $currentIndex = $entity->get('order'); if (!is_int($currentIndex)) { throw new Error(); } if (!$where) { $where = []; } $params = [ 'where' => $where, ]; $query = $this->selectBuilderFactory ->create() ->from($this->entityType) ->withStrictAccessControl() ->withSearchParams(SearchParams::fromRaw($params)) ->buildQueryBuilder() ->where([ 'order>' => $currentIndex, ]) ->order([ ['order', 'ASC'], ]) ->build(); $nextEntity = $this->getRepository() ->clone($query) ->findOne(); if (!$nextEntity) { return; } $entity->set('order', $nextEntity->get('order')); $nextEntity->set('order', $currentIndex); $this->getEntityManager()->saveEntity($entity); $this->getEntityManager()->saveEntity($nextEntity); } public function moveToTop(string $id, ?array $where = null) { $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { throw new NotFound(); } if (!$this->getAcl()->check($entity, 'edit')) { throw new Forbidden(); } $currentIndex = $entity->get('order'); if (!is_int($currentIndex)) { throw new Error(); } if (!$where) { $where = []; } $params = [ 'where' => $where, ]; $query = $this->selectBuilderFactory ->create() ->from($this->entityType) ->withStrictAccessControl() ->withSearchParams(SearchParams::fromRaw($params)) ->buildQueryBuilder() ->where([ 'order<' => $currentIndex, ]) ->order([ ['order', 'ASC'], ]) ->build(); $previousEntity = $this->getRepository() ->clone($query) ->findOne(); if (!$previousEntity) { return; } $entity->set('order', $previousEntity->get('order') - 1); $this->getEntityManager()->saveEntity($entity); } public function moveToBottom(string $id, ?array $where = null) { $entity = $this->getEntityManager()->getEntity('KnowledgeBaseArticle', $id); if (!$entity) { throw new NotFound(); } if (!$this->getAcl()->check($entity, 'edit')) { throw new Forbidden(); } $currentIndex = $entity->get('order'); if (!is_int($currentIndex)) { throw new Error(); } if (!$where) { $where = []; } $params = [ 'where' => $where, ]; $query = $this->selectBuilderFactory ->create() ->from($this->entityType) ->withStrictAccessControl() ->withSearchParams(SearchParams::fromRaw($params)) ->buildQueryBuilder() ->where([ 'order>' => $currentIndex, ]) ->order([ ['order', 'DESC'], ]) ->build(); $nextEntity = $this->getRepository() ->clone($query) ->findOne(); if (!$nextEntity) { return; } $entity->set('order', $nextEntity->get('order') + 1); $this->getEntityManager()->saveEntity($entity); } }