canvasRepo = $canvasRepo; $this->goalService = $goalService; } /** * get - generate and return the goal canvas as an XML file. * * @param array $params Request parameters */ #[RequiresPermission(GoalcanvasPermissions::VIEW, entityScoped: true)] public function get(array $params): Response { if (isset($params['id']) && $params['id'] !== '') { $canvasId = (int) $params['id']; } elseif (session()->exists(self::SESSION_KEY)) { $canvasId = (int) session(self::SESSION_KEY); } else { return new Response('', 204); } // getSingleCanvas authorizes VIEW against the board's real project and returns false for // a missing/foreign/unauthorized board (export is reachable by arbitrary board id). $canvas = $this->goalService->getSingleCanvas($canvasId); if (! $canvas) { return new Response('Canvas not found', 404); } $records = $this->goalService->getCanvasItemsById($canvasId); $canvasTypes = $this->canvasRepo->getCanvasTypes(); // The Goalcanvas repo's getSingleCanvas returns a single row (not array-of-rows). $exportData = $this->buildXml($canvas['title'] ?? '', $records, $canvasTypes); clearstatcache(); $response = new Response($exportData); $response->headers->set('Content-type', 'application/xml'); $response->headers->set('Content-Disposition', 'attachment; filename="'.self::CANVAS_TYPE.'-'.$canvasId.'.xml"'); $response->headers->set('Cache-Control', 'no-cache'); return $response; } /** * buildXml - render the canvas board and its items as XML. * * @param string $canvasTitle Board title * @param array> $records Canvas item records * @param array> $canvasTypes Translated box definitions */ private function buildXml(string $canvasTitle, array $records, array $canvasTypes): string { $tab = str_repeat(' ', 4); $xml = ''.PHP_EOL.PHP_EOL; $xml .= ''.PHP_EOL; $xml .= $tab.''.$canvasTitle.''.PHP_EOL; $xml .= $tab.''.PHP_EOL; foreach ($canvasTypes as $key => $data) { $xml .= $tab.$tab.''.PHP_EOL; foreach ($records as $record) { if ($record['box'] === $key) { $xml .= $tab.$tab.$tab.''.PHP_EOL; $xml .= $tab.$tab.$tab.$tab.''.($record['created'] ?? '').''.PHP_EOL; $xml .= $tab.$tab.$tab.$tab.''.($record['modified'] ?? '').''.PHP_EOL; $xml .= $tab.$tab.$tab.$tab.''.PHP_EOL; $xml .= $tab.$tab.$tab.$tab.''.($record['description'] ?? '').''.PHP_EOL; $xml .= $tab.$tab.$tab.$tab.''.PHP_EOL; $xml .= $tab.$tab.$tab.$tab.''.PHP_EOL; $xml .= $tab.$tab.$tab.$tab.''.($record['assumptions'] ?? '').''.PHP_EOL; $xml .= $tab.$tab.$tab.$tab.''.($record['data'] ?? '').''.PHP_EOL; $xml .= $tab.$tab.$tab.$tab.''.($record['conclusion'] ?? '').''.PHP_EOL; $xml .= $tab.$tab.$tab.''.PHP_EOL; } } $xml .= $tab.$tab.''.PHP_EOL; } $xml .= $tab.''.PHP_EOL; $xml .= ''.PHP_EOL; return $xml; } }