config = $config; $this->language = $language; $this->blueprintsService = app()->make(BlueprintsService::class); $canvasName = Str::studly(static::CANVAS_NAME).static::CANVAS_TYPE; $repoName = app()->getNamespace()."Domain\\$canvasName\\Repositories\\$canvasName"; $this->canvasRepo = app()->make($repoName); $this->canvasTypes = $this->canvasRepo->getCanvasTypes(); $this->statusLabels = $this->canvasRepo->getStatusLabels(); $this->relatesLabels = $this->canvasRepo->getRelatesLabels(); $this->dataLabels = $this->canvasRepo->getDataLabels(); } /** * Generates and serves an XML export of the canvas. * * @param array $params Request parameters */ #[RequiresPermission(BlueprintsPermissions::VIEW, entityScoped: true)] public function get(array $params): Response { $canvasId = (int) ($params['id'] ?? $_GET['id'] ?? 0); if ($canvasId <= 0 && session()->exists('current'.strtoupper(static::CANVAS_NAME).'Canvas')) { $canvasId = (int) session('current'.strtoupper(static::CANVAS_NAME).'Canvas'); } if ($canvasId <= 0) { return new Response; } $exportData = $this->export($canvasId); clearstatcache(); $response = new Response($exportData); $response->headers->set('Content-type', 'application/xml'); $response->headers->set('Content-Disposition', 'attachment; filename="'.static::CANVAS_NAME.static::CANVAS_TYPE.'-'.$canvasId.'.xml"'); $response->headers->set('Cache-Control', 'no-cache'); return $response; } /** * Generates XML data for the given canvas. * * @param int $id Canvas identifier * @return string XML data * * @throws BindingResolutionException * @throws Exception */ protected function export(int $id): string { // getBoard authorizes VIEW against the board's real project and returns false for a // missing/foreign/unauthorized board (export is reachable by arbitrary board id). $canvasAry = $this->blueprintsService->getBoard($id, static::CANVAS_NAME.static::CANVAS_TYPE); ! empty($canvasAry) || throw new Exception("Cannot find canvas with id '$id'"); $projectId = $canvasAry[0]['projectId']; $recordsAry = $this->blueprintsService->getBoardItems($id, static::CANVAS_NAME.static::CANVAS_TYPE, static::CANVAS_NAME.'canvasitem'); $projectService = app()->make(ProjectService::class); $projectAry = $projectService->getProject($projectId); ! empty($projectAry) || throw new Exception("Cannot retrieve project id '$projectId'"); $xml = ''.PHP_EOL.PHP_EOL; $xml .= $this->xmlExport(static::CANVAS_NAME.static::CANVAS_TYPE, $canvasAry[0]['title'], $recordsAry); return $xml; } /** * Generates XML markup for canvas data. * * @param string $canvasKey Encoded canvas name * @param string $canvasTitle Canvas title * @param array $recordsAry Array of canvas entry records * @param int $indent Indent level to use * @return string XML data */ protected function xmlExport(string $canvasKey, string $canvasTitle, array $recordsAry, int $indent = 0): string { $is = str_repeat(' ', 4 * $indent); $tab = str_repeat(' ', 4); $xml = $is.''.PHP_EOL; $xml .= $is.$tab.''.$canvasTitle.''.PHP_EOL; $xml .= $is.$tab.''.PHP_EOL; foreach ($this->canvasTypes as $key => $data) { $xml .= $is.$tab.$tab.''.PHP_EOL; foreach ($recordsAry as $record) { if ($record['box'] === $key) { $xml .= $is.$tab.$tab.$tab.''.PHP_EOL; $xml .= $is.$tab.$tab.$tab.$tab.''.($record['created'] ?? '').''.PHP_EOL; $xml .= $is.$tab.$tab.$tab.$tab.''.($record['modified'] ?? '').''.PHP_EOL; $xml .= $is.$tab.$tab.$tab.$tab.''.PHP_EOL; $xml .= $is.$tab.$tab.$tab.$tab.''.($record['description'] ?? '').''.PHP_EOL; $xml .= $is.$tab.$tab.$tab.$tab.''.PHP_EOL; $xml .= $is.$tab.$tab.$tab.$tab.''.PHP_EOL; $xml .= $is.$tab.$tab.$tab.$tab.''.($record['assumptions'] ?? '').''.PHP_EOL; $xml .= $is.$tab.$tab.$tab.$tab.''.($record['data'] ?? '').''.PHP_EOL; $xml .= $is.$tab.$tab.$tab.$tab.''.($record['conclusion'] ?? '').''.PHP_EOL; $xml .= $is.$tab.$tab.$tab.''.PHP_EOL; } } $xml .= $is.$tab.$tab.''.PHP_EOL; } $xml .= $is.$tab.''.PHP_EOL; $xml .= $is.''.PHP_EOL; return $xml; } }