busigbil /
MVC2025
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Controller; |
||||
| 4 | |||||
| 5 | use Amenadiel\JpGraph\Graph\Graph; |
||||
| 6 | use Amenadiel\JpGraph\Plot\Plot; |
||||
| 7 | use Amenadiel\JpGraph\Plot\BarPlot; |
||||
| 8 | use Amenadiel\JpGraph\Plot\LinePlot; |
||||
| 9 | use App\Entity\Goals\Descriptions; |
||||
| 10 | use App\Entity\Goals\Texts; |
||||
| 11 | use App\Repository\DescriptionsRepository; |
||||
| 12 | use App\Repository\TextsRepository; |
||||
| 13 | use App\Repository\TimeValueRepository; |
||||
| 14 | use Doctrine\Persistence\ManagerRegistry; |
||||
| 15 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||||
| 16 | use Symfony\Component\HttpFoundation\Request; |
||||
| 17 | use Symfony\Component\HttpFoundation\Response; |
||||
| 18 | use Symfony\Component\Routing\Attribute\Route; |
||||
| 19 | use Symfony\Component\Routing\RouterInterface; |
||||
| 20 | |||||
| 21 | class ProjectController extends AbstractController |
||||
| 22 | { |
||||
| 23 | #[Route("/back", name: "back")] |
||||
| 24 | public function back(): Response |
||||
| 25 | { |
||||
| 26 | return $this->redirectToRoute('home'); |
||||
| 27 | } |
||||
| 28 | |||||
| 29 | #[Route("/proj", name: "project", methods: ['GET'])] |
||||
| 30 | public function project( |
||||
| 31 | TextsRepository $textRepo |
||||
| 32 | ): Response |
||||
| 33 | { |
||||
| 34 | $texts = $textRepo->findAll(); |
||||
| 35 | |||||
| 36 | return $this->render( |
||||
| 37 | 'project/landing_page.html.twig', |
||||
| 38 | array('indicators' => $texts) |
||||
| 39 | ); |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | #[Route("/proj/indicator/{id}", name: "indicator", methods: ['GET'])] |
||||
| 43 | public function indicator( |
||||
| 44 | DescriptionsRepository $descriptionsRepo, |
||||
| 45 | TextsRepository $textRepo, |
||||
| 46 | TimeValueRepository $timeValueRepo, |
||||
|
0 ignored issues
–
show
|
|||||
| 47 | int $id |
||||
| 48 | ): Response |
||||
| 49 | { |
||||
| 50 | $indicator = $textRepo->find($id); |
||||
| 51 | |||||
| 52 | if (!$indicator) { |
||||
| 53 | throw new Exception("No indicator found for id"); |
||||
|
0 ignored issues
–
show
|
|||||
| 54 | }; |
||||
| 55 | |||||
| 56 | $description = $descriptionsRepo->findOneBy([ |
||||
| 57 | 'Target' => $indicator->getTarget() |
||||
| 58 | ]); |
||||
| 59 | |||||
| 60 | return $this->render( |
||||
| 61 | 'project/indicator.html.twig', |
||||
| 62 | [ |
||||
| 63 | 'indicator' => $indicator, |
||||
| 64 | 'description' => $description |
||||
| 65 | ] |
||||
| 66 | ); |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | #[Route("/proj/indicator/plot/{id}", name: "plot", methods: ['GET'])] |
||||
| 70 | public function plot( |
||||
| 71 | DescriptionsRepository $descriptionsRepo, |
||||
| 72 | TextsRepository $textRepo, |
||||
| 73 | TimeValueRepository $timeValueRepo, |
||||
| 74 | int $id |
||||
| 75 | ): Response |
||||
| 76 | { |
||||
| 77 | $indicator = $textRepo->find($id); |
||||
| 78 | |||||
| 79 | $description = $descriptionsRepo->findOneBy([ |
||||
|
0 ignored issues
–
show
|
|||||
| 80 | 'Target' => $indicator->getTarget() |
||||
| 81 | ]); |
||||
| 82 | |||||
| 83 | # find entity where Target in descriptions |
||||
| 84 | $timeValue = $timeValueRepo->findBy([ |
||||
| 85 | 'target' => $indicator->getTarget() |
||||
| 86 | ]); |
||||
| 87 | |||||
| 88 | # Add data to graph |
||||
| 89 | $dataY = []; |
||||
| 90 | $dataX = []; |
||||
| 91 | foreach ($timeValue as $val) { |
||||
| 92 | $dataY[] = floatval($val->getValue()); |
||||
| 93 | $dataX[] = intval($val->getYear()); |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | $graph = new Graph(400, 400); |
||||
| 97 | $graph->SetScale('textlin'); |
||||
| 98 | $graph->Set90AndMargin(50,50,10,10); |
||||
| 99 | $graph->yscale->SetAutoTicks(); |
||||
| 100 | $graph->xaxis->SetTickLabels($dataX); |
||||
| 101 | |||||
| 102 | $graph->xaxis->SetLabelMargin(10); |
||||
| 103 | $graph->xaxis->SetLabelAlign('right','center'); |
||||
| 104 | $graph->yaxis->Hide(); |
||||
| 105 | |||||
| 106 | $plot = new BarPlot($dataY); |
||||
| 107 | $plot->SetFillColor('#003148'); |
||||
| 108 | $plot->SetWidth(0.5); |
||||
| 109 | |||||
| 110 | $plot->value->Show(); |
||||
| 111 | $plot->value->SetColor('black','darkred'); |
||||
| 112 | |||||
| 113 | $plot->value->SetFormat('%.2f'); |
||||
| 114 | |||||
| 115 | $index = array_key_first($dataY); |
||||
| 116 | if ($dataY[$index] < 1) { |
||||
| 117 | $plot->value->SetFormat('%.3f'); |
||||
| 118 | } |
||||
| 119 | if ($dataY[$index] > 100) { |
||||
| 120 | $plot->value->SetFormat('%.0f'); |
||||
| 121 | } |
||||
| 122 | |||||
| 123 | $graph->Add($plot); |
||||
| 124 | |||||
| 125 | ob_start(); |
||||
| 126 | $graph->Stroke(); |
||||
| 127 | $imageData = ob_get_clean(); |
||||
| 128 | |||||
| 129 | $response = new Response($imageData); |
||||
| 130 | $response->headers->set('Content-Type', 'image/png'); |
||||
| 131 | |||||
| 132 | return $response; |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | #[Route("/proj/about", name: "about", methods: ['GET'])] |
||||
| 136 | public function about(): Response |
||||
| 137 | { |
||||
| 138 | return $this->render('project/about.html.twig'); |
||||
| 139 | } |
||||
| 140 | |||||
| 141 | #[Route("/proj/api", name: "api", methods: ['GET'])] |
||||
| 142 | public function api( |
||||
| 143 | RouterInterface $router, |
||||
| 144 | TextsRepository $textRepo |
||||
| 145 | ): Response |
||||
| 146 | { |
||||
| 147 | //Get all routes. |
||||
| 148 | $routes = $router->getRouteCollection()->all(); |
||||
| 149 | $apiArr = array(); |
||||
| 150 | |||||
| 151 | //Add route to array if starts with api. |
||||
| 152 | foreach ($routes as $routeName => $route) { |
||||
| 153 | |||||
| 154 | if (str_starts_with($route->getPath(), '/proj/api/')) { |
||||
| 155 | |||||
| 156 | $description = $route->getDefault('description') ?? 'Beskrivning saknas'; |
||||
| 157 | $target = $route->getDefault('target') ?? null; |
||||
| 158 | |||||
| 159 | $apiArr[] = [ |
||||
| 160 | 'header' => $route->getPath(), |
||||
| 161 | 'description' => $description, |
||||
| 162 | 'target' => $target, |
||||
| 163 | 'name' => $routeName |
||||
| 164 | ]; |
||||
| 165 | } |
||||
| 166 | } |
||||
| 167 | |||||
| 168 | //Get target for texts |
||||
| 169 | $texts = $textRepo->findAll(); |
||||
| 170 | $indicators = []; |
||||
| 171 | |||||
| 172 | foreach ($texts as $id => $text) { |
||||
| 173 | $indicators[$id] = $text; |
||||
| 174 | } |
||||
| 175 | |||||
| 176 | $data = [ |
||||
| 177 | 'routes' => $apiArr, |
||||
| 178 | 'indicators' => $indicators |
||||
| 179 | ]; |
||||
| 180 | return $this->render('project/api.html.twig', $data); |
||||
| 181 | } |
||||
| 182 | |||||
| 183 | #[Route("/proj/api/goal/{target}", name: "json-goal", methods: ['GET'], defaults: ['description' => 'Returnerar en JSON-struktur med beskrivningen av valt delmål.', 'target' => '15.4'])] |
||||
| 184 | public function jsonGoal( |
||||
| 185 | TextsRepository $textRepo, |
||||
| 186 | DescriptionsRepository $descriptionsRepo, |
||||
|
0 ignored issues
–
show
The parameter
$descriptionsRepo is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 187 | string $target |
||||
| 188 | ): Response |
||||
| 189 | { |
||||
| 190 | $text = $textRepo->findOneBy([ |
||||
| 191 | 'Target' => $target |
||||
| 192 | ]); |
||||
| 193 | |||||
| 194 | if (!$text) { |
||||
| 195 | throw new Exception("No indicator found for target"); |
||||
| 196 | }; |
||||
| 197 | |||||
| 198 | $response = $this->json($text); |
||||
| 199 | $response->setEncodingOptions( |
||||
| 200 | $response->getEncodingOptions() | JSON_PRETTY_PRINT |
||||
| 201 | ); |
||||
| 202 | return $response; |
||||
| 203 | } |
||||
| 204 | |||||
| 205 | #[Route("/proj/api/goals", name: "json-goals", methods: ['GET'], defaults: ['description' => 'Returnerar en JSON-struktur med beskrivning av alla delmål.'])] |
||||
| 206 | public function jsonGoals( |
||||
| 207 | TextsRepository $textRepo |
||||
| 208 | ): Response |
||||
| 209 | { |
||||
| 210 | $texts = $textRepo->findAll(); |
||||
| 211 | |||||
| 212 | if (empty($texts)) { |
||||
| 213 | throw $this->createNotFoundException( |
||||
| 214 | 'No indicators found' |
||||
| 215 | ); |
||||
| 216 | } |
||||
| 217 | |||||
| 218 | $response = $this->json($texts); |
||||
| 219 | $response->setEncodingOptions( |
||||
| 220 | $response->getEncodingOptions() | JSON_PRETTY_PRINT |
||||
| 221 | ); |
||||
| 222 | return $response; |
||||
| 223 | } |
||||
| 224 | |||||
| 225 | #[Route("/proj/api/alldata", name: "json-alldata", methods: ['GET'], defaults: ['description' => 'Returnerar en JSON-struktur med all tidseriedata för alla indikatorer.'])] |
||||
| 226 | public function jsonAllData( |
||||
| 227 | TimeValueRepository $timeValueRepo, |
||||
| 228 | ): Response |
||||
| 229 | { |
||||
| 230 | $timeData = $timeValueRepo->findAll(); |
||||
| 231 | |||||
| 232 | if (empty($timeData)) { |
||||
| 233 | throw $this->createNotFoundException( |
||||
| 234 | 'No time-data found' |
||||
| 235 | ); |
||||
| 236 | } |
||||
| 237 | |||||
| 238 | $response = $this->json($timeData); |
||||
| 239 | $response->setEncodingOptions( |
||||
| 240 | $response->getEncodingOptions() | JSON_PRETTY_PRINT |
||||
| 241 | ); |
||||
| 242 | return $response; |
||||
| 243 | } |
||||
| 244 | |||||
| 245 | #[Route("/proj/api/data/{target}", name: "json-data", methods: ['GET'], defaults: ['description' => 'Returnerar en JSON-struktur med tidseriedata för en specifik indikator.', 'target' => '15.1'])] |
||||
| 246 | public function jsonData( |
||||
| 247 | TimeValueRepository $timeValueRepo, |
||||
| 248 | string $target |
||||
| 249 | ): Response |
||||
| 250 | { |
||||
| 251 | |||||
| 252 | $timeValue = $timeValueRepo->findOneBy([ |
||||
| 253 | 'target' => $target |
||||
| 254 | ]); |
||||
| 255 | |||||
| 256 | if (empty($timeValue)) { |
||||
| 257 | throw $this->createNotFoundException( |
||||
| 258 | 'No data found' |
||||
| 259 | ); |
||||
| 260 | } |
||||
| 261 | |||||
| 262 | $response = $this->json($timeValue); |
||||
| 263 | $response->setEncodingOptions( |
||||
| 264 | $response->getEncodingOptions() | JSON_PRETTY_PRINT |
||||
| 265 | ); |
||||
| 266 | return $response; |
||||
| 267 | } |
||||
| 268 | |||||
| 269 | #[Route("/proj/api/update", name: "update", methods: ['POST'], defaults: ['description' => 'Textfältet för vald indikator uppdateras och returnerar en JSON-struktur med beskrivningen av indikatorn.'])] |
||||
| 270 | public function jsonUpdate( |
||||
| 271 | TextsRepository $textRepo, |
||||
| 272 | Request $request, |
||||
| 273 | ManagerRegistry $doctrine |
||||
| 274 | ): Response |
||||
| 275 | { |
||||
| 276 | $entityManager = $doctrine->getManager(); |
||||
| 277 | //Get the indicator selected in dropdown |
||||
| 278 | $id = $request->request->get('indicator'); |
||||
| 279 | $indicator = $textRepo->find($id); |
||||
| 280 | |||||
| 281 | if (!$indicator) { |
||||
| 282 | throw new Exception("No indicator found"); |
||||
| 283 | }; |
||||
| 284 | |||||
| 285 | //Set text to db |
||||
| 286 | $indicator->setText2((string) $request->request->get('text2')); |
||||
| 287 | |||||
| 288 | $entityManager->flush(); |
||||
| 289 | |||||
| 290 | $response = $this->json($indicator); |
||||
| 291 | $response->setEncodingOptions( |
||||
| 292 | $response->getEncodingOptions() | JSON_PRETTY_PRINT |
||||
| 293 | ); |
||||
| 294 | return $response; |
||||
| 295 | |||||
| 296 | } |
||||
| 297 | } |
||||
| 298 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.