Projekt

Obecné

Profil

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

    
3
namespace App\Controller;
4

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

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

    
26
        $form = $formBuilder->getForm();
27
        $form->submit($request->query->all());
28
        $isSubmitted = $form->isSubmitted();
29
        if ($isSubmitted) {
30
            $dataSet = $form->getData();
31
            //check availability
32
            $notExist = false;
33
            if ($dataSet->getType() && count($dataSet->getType()) > 0) {
34
                foreach ($dataSet->getType() as $value) {
35
                    if (!$manager->isCollectionAvailable($value, $dataSet->getDate())) {
36
                        $notExist = true;
37
                        break;
38
                    }
39
                }
40
            } else {
41
                $notExist = true;
42
            }
43
            if ($notExist) {
44
                // Not? Populate form with empty data
45
                $formBuilder = $datasetFormBuilder->getBuilder();
46
                $form = $formBuilder->getForm();
47
            }
48
        }
49
        //ziskej barvy o datasetech a namapuj je na jejich jmena
50

    
51
        return $this->render(
52
            'heatmap.html.twig',
53
            [
54
                'form' => $form->createView(),
55
                'submitted' => $isSubmitted,
56
                'data_to_display' => $dataSet,
57
                'dataset_colors' => Utils::prepareDatasetsColors($manager->getAvailableCollections()),
58
                'formated_current_time' => $dataSet->getFormattedTime(),
59
                'current_time' => $dataSet->getTime(),
60
            ]
61
        );
62
    }
63

    
64
    /**
65
     * Provides specific dataset information i.e. position, number and other informations depending on data type.
66
     *
67
     * @param IOpenDataManager $manager connection to database autowired
68
     * @param string           $name    dataset name
69
     * @param string           $date    dataset date in format YYYY-mm-dd
70
     * @param string           $time    dataset time, number between 0 - 23
71
     *
72
     * @return json with dataset information
73
     *
74
     * @Route("heatmap/opendata/{name}/{date}/{time}", name="opendata")
75
     */
76
    public function opendata(IOpenDataManager $manager, $name = 'NONE', $date = '2020-01-01', $time = '1') {
77
        return $this->json([
78
            'items' => $manager->getCollectionDataByName($name, $date, $time),
79
            'max' => $manager->getMaxCollectionNumberAtDay($name, $date),
80
        ]);
81
    }
82

    
83
    /**
84
     * Provides available datasets in given date.
85
     *
86
     * @param IOpenDataManager $manager connection to database autowired
87
     * @param string           $date    dataset date in format YYYY-mm-dd
88
     *
89
     * @return json with available datasets names
90
     *
91
     * @Route("heatmap/available/{date}", name="available")
92
     */
93
    public function availableDatasets(IOpenDataManager $manager, $date = '2020-01-01') {
94
        return $this->json($manager->getAvailableCollectionsByDay($date));
95
    }
96

    
97
    /**
98
     * Provides dates with at least one available dataset.
99
     *
100
     * @param IOpenDataManager $manager connection to database
101
     *
102
     * @return json with dates with at least one available dataset in format YYYY-mm-dd
103
     *
104
     * @Route("heatmap/dates", name="dates")
105
     */
106
    public function datesWithAvailableDatasets(IOpenDataManager $manager) {
107
        return $this->json($manager->getDatesWithAvailableCollection());
108
    }
109

    
110
    /**
111
     * Provides for dataset with given name all locations.
112
     *
113
     * @param IOpenDataManager $manager connection to database autowired
114
     * @param string           $name    dataset name
115
     *
116
     * @return json with all locations for dataset with given name as [x => lat, y => lng]
117
     *
118
     * @Route("heatmap/positions/{name}", name="positions")
119
     */
120
    public function dataSourcePoistions(IOpenDataManager $manager, $name = 'NONE') {
121
        return $this->json($manager->getDataSourcePositions($name));
122
    }
123

    
124
    /**
125
     * Provides last available date for each available dataset type.
126
     *
127
     * @param IOpenDataManager $manager connection to database autowired
128
     *
129
     * @return json with last available date for each available dataset type as [collection-type-name => YYYY-mm-dd]
130
     *
131
     * @Route("heatmap/last", name="last")
132
     */
133
    public function lastAvailableCollections(IOpenDataManager $manager) {
134
        return $this->json($manager->getLastAvailableCollections());
135
    }
136
}
(2-2/3)