Projekt

Obecné

Profil

Stáhnout (4.19 KB) Statistiky
| Větev: | Revize:
1
<?php
2

    
3
namespace App\Controller;
4

    
5
use App\Entity\DataSet;
6
use App\Form\DataSetType;
7
use App\Repository\IOpenDataManager;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Routing\Annotation\Route;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11

    
12
class HeatmapController extends AbstractController {
13
    /**
14
     * Map page controller.
15
     *
16
     * @param IOpenDataManager $manager connection to database autowired
17
     *
18
     * @Route("/heatmap", name="heatmap")
19
     */
20
    public function index(Request $request, IOpenDataManager $manager) {
21
        $dataSet = new DataSet();
22

    
23
        $form = $this->createForm(DataSetType::class, $dataSet);
24
        $form->handleRequest($request);
25

    
26
        $isSubmitted = $form->isSubmitted();
27
        if ($isSubmitted) {
28
            $dataSet = $form->getData();
29
            // Chceck whether the data is valid i.e. such a collection exists
30
            if (false == $manager->isCollectionAvailable($dataSet->getType(), $dataSet->getDate())) {
31
                // Not? Populate form with empty data
32
                $dataSet = new DataSet();
33
            }
34
            $form = $this->createForm(DataSetType::class, $dataSet);
35
        }
36

    
37
        return $this->render(
38
            'heatmap.html.twig',
39
            [
40
                'form' => $form->createView(),
41
                'submitted' => $isSubmitted,
42
                'data_to_display' => $dataSet,
43
            ]
44
        );
45
    }
46

    
47
    /**
48
     * Provides specific dataset information i.e. position, number and other informations depending on data type.
49
     *
50
     * @param IOpenDataManager $manager connection to database autowired
51
     * @param string           $name    dataset name
52
     * @param string           $date    dataset date in format YYYY-mm-dd
53
     * @param string           $time    dataset time, number between 0 - 23
54
     *
55
     * @return json with dataset information
56
     *
57
     * @Route("heatmap/opendata/{name}/{date}/{time}", name="opendata")
58
     */
59
    public function opendata(IOpenDataManager $manager, $name = 'NONE', $date = '2020-01-01', $time = '1') {
60
        return $this->json([
61
            'items' => $manager->getCollectionDataByName($name, $date, $time),
62
            'max' => $manager->getMaxCollectionNumberAtDay($name, $date),
63
        ]);
64
    }
65

    
66
    /**
67
     * Provides available datasets in given date.
68
     *
69
     * @param IOpenDataManager $manager connection to database autowired
70
     * @param string           $date    dataset date in format YYYY-mm-dd
71
     *
72
     * @return json with available datasets names
73
     *
74
     * @Route("heatmap/available/{date}", name="available")
75
     */
76
    public function availableDatasets(IOpenDataManager $manager, $date = '2020-01-01') {
77
        return $this->json($manager->getAvailableCollectionsByDay($date));
78
    }
79

    
80
    /**
81
     * Provides dates with at least one available dataset.
82
     *
83
     * @param IOpenDataManager $manager connection to database
84
     *
85
     * @return json with dates with at least one available dataset in format YYYY-mm-dd
86
     *
87
     * @Route("heatmap/dates", name="dates")
88
     */
89
    public function datesWithAvailableDatasets(IOpenDataManager $manager) {
90
        return $this->json($manager->getDatesWithAvailableCollection());
91
    }
92

    
93
    /**
94
     * Provides for dataset with given name all locations.
95
     *
96
     * @param IOpenDataManager $manager connection to database autowired
97
     * @param string           $name    dataset name
98
     *
99
     * @return json with all locations for dataset with given name as [x => lat, y => lng]
100
     *
101
     * @Route("heatmap/positions/{name}", name="positions")
102
     */
103
    public function dataSourcePoistions(IOpenDataManager $manager, $name = 'NONE') {
104
        return $this->json($manager->getDataSourcePositions($name));
105
    }
106

    
107
    /**
108
     * Provides last available date for each available dataset type.
109
     *
110
     * @param IOpenDataManager $manager connection to database autowired
111
     *
112
     * @return json with last available date for each available dataset type as [collection-type-name => YYYY-mm-dd]
113
     *
114
     * @Route("heatmap/last", name="last")
115
     */
116
    public function lastAvailableCollections(IOpenDataManager $manager) {
117
        return $this->json($manager->getLastAvailableCollections());
118
    }
119
}
(2-2/3)