From f69093cc008a34d62cb8ab70eaf34428d1212f06 Mon Sep 17 00:00:00 2001 From: vincent Date: Sat, 4 Jan 2025 14:11:42 +0100 Subject: [PATCH] Ajoute les badges --- src/Controller/BadgeController.php | 134 +++++++++++++++++++++++++ templates/partials/_project-metadata.html.twig | 3 + 2 files changed, 137 insertions(+) create mode 100644 src/Controller/BadgeController.php diff --git a/src/Controller/BadgeController.php b/src/Controller/BadgeController.php new file mode 100644 index 0000000..ddf5c5a --- /dev/null +++ b/src/Controller/BadgeController.php @@ -0,0 +1,134 @@ +getRepository(Project::class)->findOneBySlug($projectSlug); + if (!$project) { + throw new NotFoundHttpException('Project not found'); + } + + $stats = $taskLifecycleManager->getProjectStats($project); + if (!isset($stats[$status])) { + throw new NotFoundHttpException('Status not found'); + } + + $stats = $stats[$status]; + + $response = new StreamedResponse(); + + $response->headers->set('Content-Type', 'image/svg+xml'); + $response->headers->set('Cache-Control', 'public, max-age=300'); + + $response->setCallback(function () use ($stats): void { + $colors = [ + 'primary' => '#0d6efd', // $blue; + 'secondary' => '#6c757d', // $gray-600; + 'success' => '#198754', // $green; + 'info' => '#0dcaf0', // $cyan; + 'warning' => '#ffc107', // $yellow; + 'danger' => '#dc3545', // $red; + ]; + + $xml = new \DOMDocument(); + + $left = $stats['title']; + $leftColor = $colors['secondary']; + $right = sprintf(' % 3.0f%%', $stats['percentage']); + $rightColor = $colors[$stats['color']]; + + $charWidth = 7; + $fontSize = 11; + $totalHeight = 20; + $radius = ceil($totalHeight / $charWidth); + $verticalMargin = round(($totalHeight - $fontSize) / 2); + $horizontalMargin = round($fontSize / 3); + $totalWidth = round(strlen($left.$right) * $charWidth + 2 * $horizontalMargin); + $middle = floor(strlen($left) * $charWidth); + + $svg = $xml->createElement('svg'); + $svg->setAttributeNS('http://www.w3.org/2000/xmlns/','xmlns', 'http://www.w3.org/2000/svg'); + $svg->setAttribute('width', $totalWidth); + $svg->setAttribute('height', $totalHeight); + + $defs = $xml->createElement('defs'); + + $gradientId = 'a'; + $gradient = $xml->createElement('linearGradient'); + $gradient->setAttribute('id', $gradientId); + + $stop = $xml->createElement('stop'); + $stop->setAttribute('offset', ($middle * 100 / $totalWidth).'%'); + $stop->setAttribute('stop-color', $leftColor); + $gradient->appendChild($stop); + + $stop = $xml->createElement('stop'); + $stop->setAttribute('offset', ($middle * 100 / $totalWidth).'%'); + $stop->setAttribute('stop-color', $rightColor); + $gradient->appendChild($stop); + + $defs->appendChild($gradient); + + $svg->appendChild($defs); + + $rect = $xml->createElement('rect'); + $rect->setAttribute('width', $totalWidth); + $rect->setAttribute('height', $totalHeight); + $rect->setAttribute('rx', $radius); + $rect->setAttribute('style', sprintf('fill:url(#%s)', $gradientId)); + + $svg->appendChild($rect); + + $fontG = $xml->createElement('g'); + $fontG->setAttribute('font-family', implode(',', ['DejaVu Sans', 'Verdana', 'Geneva', 'sans-serif'])); + $fontG->setAttribute('font-size', $fontSize); + + $leftText = $xml->createElement('text', $left); + $leftText->setAttribute('x', $horizontalMargin); + $leftText->setAttribute('y', $totalHeight - $verticalMargin); + $leftText->setAttribute('fill', '#000'); + $fontG->appendChild($leftText); + + $leftText = $xml->createElement('text', $left); + $leftText->setAttribute('x', $horizontalMargin); + $leftText->setAttribute('y', $totalHeight - $verticalMargin - 1); + $leftText->setAttribute('fill', '#fff'); + $fontG->appendChild($leftText); + + $rightText = $xml->createElement('text', $right); + $rightText->setAttribute('x', $totalWidth - $horizontalMargin); + $rightText->setAttribute('y', $totalHeight - $verticalMargin); + $rightText->setAttribute('style', 'text-anchor:end;fill:#000'); + $fontG->appendChild($rightText); + + $rightText = $xml->createElement('text', $right); + $rightText->setAttribute('x', $totalWidth - $horizontalMargin); + $rightText->setAttribute('y', $totalHeight - $verticalMargin - 1); + $rightText->setAttribute('style', 'text-anchor:end;fill:#fff'); + $fontG->appendChild($rightText); + + $svg->appendChild($fontG); + + $xml->appendChild($svg); + + echo $xml->saveXML(); + }); + return $response; + } + +} diff --git a/templates/partials/_project-metadata.html.twig b/templates/partials/_project-metadata.html.twig index 6addd82..9dbc403 100644 --- a/templates/partials/_project-metadata.html.twig +++ b/templates/partials/_project-metadata.html.twig @@ -10,3 +10,6 @@ {{ project.createdAt|format_datetime('short', 'short', locale='fr') }} + + +