diff --git a/src/Controller/ApiAnrInstancesController.php b/src/Controller/ApiAnrInstancesController.php index 14daee82..fcf97d04 100755 --- a/src/Controller/ApiAnrInstancesController.php +++ b/src/Controller/ApiAnrInstancesController.php @@ -7,13 +7,13 @@ namespace Monarc\FrontOffice\Controller; -use Laminas\Http\Response; use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler; use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait; use Monarc\Core\Validator\InputValidator\Instance\CreateInstanceDataInputValidator; use Monarc\Core\Validator\InputValidator\Instance\PatchInstanceDataInputValidator; use Monarc\Core\Validator\InputValidator\Instance\UpdateInstanceDataInputValidator; use Monarc\FrontOffice\Entity\Anr; +use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait; use Monarc\FrontOffice\Service\AnrInstanceRiskOpService; use Monarc\FrontOffice\Service\AnrInstanceRiskService; use Monarc\FrontOffice\Service\AnrInstanceService; @@ -21,6 +21,7 @@ class ApiAnrInstancesController extends AbstractRestfulControllerRequestHandler { use ControllerRequestResponseHandlerTrait; + use ExportResponseControllerTrait; public function __construct( private AnrInstanceService $anrInstanceService, @@ -50,13 +51,13 @@ public function get($id) $anr = $this->getRequest()->getAttribute('anr'); if ($this->params()->fromQuery('csv', false)) { - return $this->setCsvResponse( + return $this->prepareCsvDataResponse( $this->anrInstanceRiskOpService->getOperationalRisksInCsv($anr, (int)$id, $this->parseParams()) ); } if ($this->params()->fromQuery('csvInfoInst', false)) { - return $this->setCsvResponse( + return $this->prepareCsvDataResponse( $this->anrInstanceRiskService->getInstanceRisksInCsv($anr, (int)$id, $this->parseParams()) ); } @@ -142,13 +143,4 @@ private function parseParams(): array 'limit' => (int)$params->fromQuery('limit', 0), ]; } - - private function setCsvResponse(string $content): Response - { - $response = $this->getResponse(); - $response->getHeaders()->addHeaderLine('Content-Type', 'text/csv; charset=utf-8'); - $response->setContent($content); - - return $response; - } } diff --git a/src/Controller/ApiAnrRecommendationsRisksValidateController.php b/src/Controller/ApiAnrRecommendationsRisksValidateController.php index ba806cbd..cfe4d8b0 100755 --- a/src/Controller/ApiAnrRecommendationsRisksValidateController.php +++ b/src/Controller/ApiAnrRecommendationsRisksValidateController.php @@ -27,7 +27,7 @@ public function patch($id, $data) /** @var Anr $anr */ $anr = $this->getRequest()->getAttribute('anr'); - $this->anrRecommendationRiskService->validateFor($anr, $id, $data); + $this->anrRecommendationRiskService->validateFor($anr, (int)$id, $data); return $this->getSuccessfulJsonResponse(); } diff --git a/src/Controller/ApiAnrRisksController.php b/src/Controller/ApiAnrRisksController.php index 802d540b..707cc7a8 100755 --- a/src/Controller/ApiAnrRisksController.php +++ b/src/Controller/ApiAnrRisksController.php @@ -10,6 +10,7 @@ use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler; use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait; use Monarc\FrontOffice\Entity\Anr; +use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait; use Monarc\FrontOffice\Service\AnrInstanceRiskService; use Laminas\Http\Response; @@ -19,6 +20,7 @@ class ApiAnrRisksController extends AbstractRestfulControllerRequestHandler { use ControllerRequestResponseHandlerTrait; + use ExportResponseControllerTrait; public function __construct(private AnrInstanceRiskService $anrInstanceRiskService) { @@ -37,12 +39,9 @@ public function get($id) $id = $id === null ? null : (int)$id; if ($this->params()->fromQuery('csv', false)) { - /** @var Response $response */ - $response = $this->getResponse(); - $response->getHeaders()?->addHeaderLine('Content-Type', 'text/csv; charset=utf-8'); - $response->setContent($this->anrInstanceRiskService->getInstanceRisksInCsv($anr, (int)$id, $params)); - - return $response; + return $this->prepareCsvDataResponse( + $this->anrInstanceRiskService->getInstanceRisksInCsv($anr, $id, $params) + ); } $risks = $this->anrInstanceRiskService->getInstanceRisks($anr, $id, $params); diff --git a/src/Controller/ApiAnrRisksOpController.php b/src/Controller/ApiAnrRisksOpController.php index b4ec7ac2..8a164f61 100755 --- a/src/Controller/ApiAnrRisksOpController.php +++ b/src/Controller/ApiAnrRisksOpController.php @@ -7,10 +7,10 @@ namespace Monarc\FrontOffice\Controller; -use Laminas\Http\Response; use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler; use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait; use Monarc\FrontOffice\Entity\Anr; +use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait; use Monarc\FrontOffice\Service\AnrInstanceRiskOpService; /** @@ -19,6 +19,7 @@ class ApiAnrRisksOpController extends AbstractRestfulControllerRequestHandler { use ControllerRequestResponseHandlerTrait; + use ExportResponseControllerTrait; public function __construct(private AnrInstanceRiskOpService $anrInstanceRiskOpService) { @@ -37,14 +38,9 @@ public function get($id) $id = $id === null ? null : (int)$id; if ($this->params()->fromQuery('csv', false)) { - /** @var Response $response */ - $response = $this->getResponse(); - $response->getHeaders()?->addHeaderLine('Content-Type', 'text/csv; charset=utf-8'); - $response->setContent( + return $this->prepareCsvDataResponse( $this->anrInstanceRiskOpService->getOperationalRisksInCsv($anr, $id, $params) ); - - return $response; } $risks = $this->anrInstanceRiskOpService->getOperationalRisks($anr, $id, $params); diff --git a/src/Controller/ApiAnrScalesTypesController.php b/src/Controller/ApiAnrScalesTypesController.php index e498f95a..5b03107e 100755 --- a/src/Controller/ApiAnrScalesTypesController.php +++ b/src/Controller/ApiAnrScalesTypesController.php @@ -49,7 +49,7 @@ public function patch($id, $data) /** @var Anr $anr */ $anr = $this->getRequest()->getAttribute('anr'); - $this->scaleImpactTypeService->patch($anr, $id, $data); + $this->scaleImpactTypeService->patch($anr, (int)$id, $data); return $this->getSuccessfulJsonResponse(); } diff --git a/src/Export/Controller/Traits/ExportResponseControllerTrait.php b/src/Export/Controller/Traits/ExportResponseControllerTrait.php index 90441fa6..1165fd1b 100644 --- a/src/Export/Controller/Traits/ExportResponseControllerTrait.php +++ b/src/Export/Controller/Traits/ExportResponseControllerTrait.php @@ -31,4 +31,16 @@ private function prepareExportResponse(string $filename, string $output, bool $i 'Content-Disposition' => 'attachment; filename="' . $filename . $extension . '"', ]); } + + private function prepareCsvDataResponse(string $output): ResponseInterface + { + $stream = fopen('php://memory', 'rb+'); + fwrite($stream, $output); + rewind($stream); + + return new Response($stream, 200, [ + 'Content-Type' => 'text/csv; charset=utf-8', + 'Content-Length' => strlen($output), + ]); + } } diff --git a/src/Service/AnrInstanceRiskOpService.php b/src/Service/AnrInstanceRiskOpService.php index 3c52e7da..469b97f6 100644 --- a/src/Service/AnrInstanceRiskOpService.php +++ b/src/Service/AnrInstanceRiskOpService.php @@ -369,22 +369,22 @@ public function getOperationalRisksInCsv(Entity\Anr $anr, int $instanceId = null if ($anr->showRolfBrut()) { $translatedRiskValueDescription = $this->translateService->translate('Inherent risk', $anrLanguage); $tableHeaders['brutProb'] = $this->translateService->translate('Prob.', $anrLanguage) - . "(" . $translatedRiskValueDescription . ")"; + . '(' . $translatedRiskValueDescription . ')'; foreach ($operationalRiskScaleTypes as $operationalRiskScaleType) { $label = $operationalRiskScaleType->getLabel(); - $tableHeaders[$label . " (" . $translatedRiskValueDescription . ")"] = $label . " (" - . $translatedRiskValueDescription . ")"; + $tableHeaders[$label . ' (' . $translatedRiskValueDescription . ')'] = $label . ' (' + . $translatedRiskValueDescription . ')'; } $tableHeaders['cacheBrutRisk'] = $translatedRiskValueDescription; } $translatedNetRiskDescription = $this->translateService->translate('Net risk', $anrLanguage); - $tableHeaders['netProb'] = $this->translateService->translate('Prob.', $anrLanguage) . "(" - . $translatedNetRiskDescription . ")"; + $tableHeaders['netProb'] = $this->translateService->translate('Prob.', $anrLanguage) . '(' + . $translatedNetRiskDescription . ')'; foreach ($operationalRiskScaleTypes as $operationalRiskScaleType) { $label = $operationalRiskScaleType->getLabel(); - $tableHeaders[$label . " (" . $translatedNetRiskDescription . ")"] = $label . " (" - . $translatedNetRiskDescription . ")"; + $tableHeaders[$label . ' (' . $translatedNetRiskDescription . ')'] = $label . ' (' + . $translatedNetRiskDescription . ')'; } $tableHeaders['cacheNetRisk'] = $translatedNetRiskDescription; $tableHeaders['comment'] = $this->translateService->translate('Existing controls', $anrLanguage); @@ -426,12 +426,12 @@ public function getOperationalRisksInCsv(Entity\Anr $anr, int $instanceId = null $values[] = CoreEntity\InstanceRiskOpSuperClass::getAvailableMeasureTypes()[ $operationalInstanceRisk->getKindOfMeasure() ]; - $values[] = $operationalInstanceRisk->getCacheTargetedRisk() === -1 ? - $operationalInstanceRisk->getCacheNetRisk() : - $operationalInstanceRisk->getCacheTargetedRisk(); + $values[] = $operationalInstanceRisk->getCacheTargetedRisk() === -1 + ? $operationalInstanceRisk->getCacheNetRisk() + : $operationalInstanceRisk->getCacheTargetedRisk(); $values[] = $operationalInstanceRisk->getInstanceRiskOwner()?->getName(); $values[] = $operationalInstanceRisk->getContext(); - $values[] = $this->getCsvRecommendations($anr, $operationalInstanceRisk); + $values[] = $this->getCsvRecommendations($operationalInstanceRisk); $values[] = $this->getCsvMeasures($anrLanguage, $operationalInstanceRisk); @@ -473,12 +473,12 @@ public function createOperationalInstanceRiskScaleObject( return $operationalInstanceRiskScale; } - protected function getCsvRecommendations(Entity\Anr $anr, Entity\InstanceRiskOp $operationalInstanceRisk): string + protected function getCsvRecommendations(Entity\InstanceRiskOp $operationalInstanceRisk): string { $csvData = []; foreach ($operationalInstanceRisk->getRecommendationRisks() as $recommendationRisk) { $recommendation = $recommendationRisk->getRecommendation(); - $csvData[] = $recommendation->getCode() . " - " . $recommendation->getDescription(); + $csvData[] = $recommendation->getCode() . ' - ' . $recommendation->getDescription(); } return implode("\n", $csvData); @@ -487,9 +487,11 @@ protected function getCsvRecommendations(Entity\Anr $anr, Entity\InstanceRiskOp protected function getCsvMeasures(int $anrLanguage, Entity\InstanceRiskOp $operationalInstanceRisk): string { $csvData = []; - foreach ($operationalInstanceRisk->getRolfRisk()->getMeasures() as $measure) { - $csvData[] = "[" . $measure->getReferential()->getLabel($anrLanguage) . "] " . - $measure->getCode() . " - " . $measure->getLabel($anrLanguage); + if ($operationalInstanceRisk->getRolfRisk() !== null) { + foreach ($operationalInstanceRisk->getRolfRisk()->getMeasures() as $measure) { + $csvData[] = '[' . $measure->getReferential()->getLabel($anrLanguage) . '] ' + . $measure->getCode() . ' - ' . $measure->getLabel($anrLanguage); + } } return implode("\n", $csvData); diff --git a/src/Service/AnrInstanceRiskService.php b/src/Service/AnrInstanceRiskService.php index 345d1916..60b3a3a3 100644 --- a/src/Service/AnrInstanceRiskService.php +++ b/src/Service/AnrInstanceRiskService.php @@ -343,7 +343,7 @@ public function recalculateRiskRatesAndUpdateRecommendationsPositions(Entity\Ins $this->updateInstanceRiskRecommendationsPositions($instanceRisk); } - public function getInstanceRisksInCsv(Entity\Anr $anr, $instanceId = null, $params = []): string + public function getInstanceRisksInCsv(Entity\Anr $anr, int $instanceId = null, array $params = []): string { $languageIndex = $anr->getLanguage(); @@ -358,9 +358,9 @@ public function getInstanceRisksInCsv(Entity\Anr $anr, $instanceId = null, $para $this->translateService->translate('Vulnerability', $languageIndex), $this->translateService->translate('Existing controls', $languageIndex), $this->translateService->translate('Qualif.', $languageIndex), - $this->translateService->translate('Current risk', $languageIndex). " C", - $this->translateService->translate('Current risk', $languageIndex) . " I", - $this->translateService->translate('Current risk', $languageIndex) . " " + $this->translateService->translate('Current risk', $languageIndex). ' C', + $this->translateService->translate('Current risk', $languageIndex) . ' I', + $this->translateService->translate('Current risk', $languageIndex) . ' ' . $this->translateService->translate('A', $languageIndex), $this->translateService->translate('Treatment', $languageIndex), $this->translateService->translate('Residual risk', $languageIndex), @@ -383,14 +383,14 @@ public function getInstanceRisksInCsv(Entity\Anr $anr, $instanceId = null, $para $instance = $instanceRisk->getInstance(); $recommendationData = []; foreach ($instanceRisk->getRecommendationRisks() as $recommendationRisk) { - $recommendationData[] = $recommendationRisk->getRecommendation()->getCode() . " - " + $recommendationData[] = $recommendationRisk->getRecommendation()->getCode() . ' - ' . $recommendationRisk->getRecommendation()->getDescription(); } $measuresData = []; if ($instanceRisk->getAmv() !== null) { foreach ($instanceRisk->getAmv()->getMeasures() as $measure) { - $measuresData[] = "[" . $measure->getReferential()->getLabel($anr->getLanguage()) . "] " - . $measure->getCode() . " - " . $measure->getLabel($anr->getLanguage()); + $measuresData[] = '[' . $measure->getReferential()->getLabel($anr->getLanguage()) . '] ' + . $measure->getCode() . ' - ' . $measure->getLabel($anr->getLanguage()); } } diff --git a/src/Service/AnrRecommendationHistoryService.php b/src/Service/AnrRecommendationHistoryService.php index 0198110a..6ad1c9c4 100755 --- a/src/Service/AnrRecommendationHistoryService.php +++ b/src/Service/AnrRecommendationHistoryService.php @@ -96,7 +96,7 @@ public function createFromRecommendationRisk( ->setRiskInstance($recommendationRisk->getInstance()->getName($languageIndex)) ->setRiskInstanceContext($recommendationRisk->getInstance()->getHierarchyString()) ->setCacheCommentAfter($recommendationRisk->getCommentAfter()) - ->setCreator($this->connectedUser->getEmail()); + ->setCreator($this->connectedUser->getFirstname() . ' ' . $this->connectedUser->getLastname()); $instanceRisk = $recommendationRisk->getInstanceRisk(); $instanceRiskOp = $recommendationRisk->getInstanceRiskOp(); diff --git a/src/Service/AnrRecommendationRiskService.php b/src/Service/AnrRecommendationRiskService.php index 8b2f6dba..7c4e25ad 100755 --- a/src/Service/AnrRecommendationRiskService.php +++ b/src/Service/AnrRecommendationRiskService.php @@ -40,7 +40,7 @@ public function __construct( public function getList(FormattedInputParams $formattedInputParams): array { - $hasRecommendationFilter = $formattedInputParams->hasFilterFor('recommendation'); + $hasRecommendationFilter = $formattedInputParams->hasFilterFor('recommendation.uuid'); $recommendationRisksData = []; $globalObjectsUuids = []; /** @var Entity\RecommendationRisk $recommendationRisk */ @@ -533,9 +533,12 @@ private function getPreparedRecommendationRiskData( 'commentAfter' => $recommendationRisk->getCommentAfter(), ]; if ($extendedFormat) { - $recommendationRiskData['instance'] = array_merge([ - 'id' => $recommendationRisk->getInstance()->getId(), - ], $recommendationRisk->getInstance()->getNames()); + $instance = $recommendationRisk->getInstance(); + $recommendationRiskData['instance'] = array_merge(['id' => $instance->getId()], $instance->getNames()); + $recommendationRiskData['asset'] = array_merge([ + 'uuid' => $instance->getAsset()->getUuid(), + 'type' => $instance->getAsset()->getType(), + ], $instance->getAsset()->getLabels()); if ($recommendationRisk->getThreat() !== null && $recommendationRisk->getVulnerability() !== null) { $recommendationRiskData['threat'] = array_merge([ 'code' => $recommendationRisk->getThreat()->getCode(), diff --git a/src/Service/DeliverableGenerationService.php b/src/Service/DeliverableGenerationService.php index ea52300e..59488ed6 100644 --- a/src/Service/DeliverableGenerationService.php +++ b/src/Service/DeliverableGenerationService.php @@ -842,23 +842,24 @@ private function generateTrendAssessmentTable(): PhpWord\Element\Table // Fill in each row foreach ($questions as $question) { - $response = null; + $response = ''; if ($question['type'] === 1) { // Simple text $response = $question['response']; } else { // Choice, either simple or multiple if ($question['multichoice']) { - $responseIds = json_decode($question['response'], true); + $responseIds = empty($question['response']) ? [] : json_decode($question['response'], true); $responses = []; - foreach ($questionsChoices as $choice) { - if (!is_null($responseIds) && array_search($choice['id'], $responseIds) !== false) { - $responses[] = '- ' . $choice['label' . $this->currentLangAnrIndex]; + if (!empty($responseIds)) { + foreach ($questionsChoices as $choice) { + if (in_array($choice['id'], $responseIds, true)) { + $responses[] = '- ' . $choice['label' . $this->currentLangAnrIndex]; + } } + $response = implode("\n", $responses); } - - $response = implode("\n", $responses); } else { foreach ($questionsChoices as $choice) { if ($choice['id'] === $question['response']) { diff --git a/src/Service/OperationalRiskScaleService.php b/src/Service/OperationalRiskScaleService.php index ca9fbcaa..842299e4 100644 --- a/src/Service/OperationalRiskScaleService.php +++ b/src/Service/OperationalRiskScaleService.php @@ -97,6 +97,9 @@ public function createOperationalRiskScaleType(Entity\Anr $anr, array $data): En $operationalRiskScale = $this->operationalRiskScaleTable->findByAnrAndScaleId($anr, (int)$data['scaleId']); $operationalRiskScaleType = $this->getCreatedOperationalRiskScaleTypeObject($anr, $operationalRiskScale); + if (!empty($data['label'][$anr->getLanguageCode()])) { + $operationalRiskScaleType->setLabel($data['label'][$anr->getLanguageCode()]); + } // Process the scale comments. if (!empty($data['comments'])) {