bootstrap.php 35.6 KB
Newer Older
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
1 2 3 4 5 6
<?php

use Slim\App;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
7
use GrEduLabs\OpenData\Action;
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
8 9 10 11 12 13 14 15 16 17 18

/**
 * gredu_labs.
 *
 * @link https://github.com/eellak/gredu_labs for the canonical source repository
 *
 * @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
 * @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
 */
return function (App $app) {
    $container = $app->getContainer();
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    $events = $container['events'];

    $filter_enabled_queries = ['schools', 'applications', 'application_items', 'new_applications', 'new_application_items', 'approved', 'labs'];
    $raw_csv_export_providers = ['assets', 'software'];

    $events('on', 'app.autoload', function ($autoloader) {
        $autoloader->addPsr4('GrEduLabs\\OpenData\\', __DIR__ . '/src/');
    });

    /**
     * Data queries specifications.
     */
    $events('on', 'app.services', function ($container) {
        $container['data_retrieve_query_specs'] = function ($container) {
            return [
                'schools' => [
                    'query' => 'SELECT school.registry_no AS registry_no, '
                    . ' school.name AS school_name, '
                    . ' schooltype.name as school_type, '
                    . ' prefecture.name AS prefecture, '
                    . ' school.municipality AS municipality, '
                    . ' eduadmin.name AS eduadmin, '
                    . ' regioneduadmin.name AS region_edu_admin, '
                    . ' educationlevel.name AS education_level '
                    . ' FROM school '
                    . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                    . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
                    . ' LEFT JOIN educationlevel ON school.educationlevel_id = educationlevel.id '
                    . ' LEFT JOIN schooltype ON school.schooltype_id = schooltype.id '
                    . ' LEFT JOIN prefecture ON school.prefecture_id = prefecture.id '
                    . ' GROUP BY school.id '
                    . ' ORDER BY school_name',
                    'headers' => [
                        'registry_no' => 'Κωδικός μονάδας',
                        'school_name' => 'Ονομασία',
                        'school_type' => 'Τύπος μονάδας',
                        'prefecture' => 'Περιφερειακή ενότητα',
                        'municipality' => 'Δήμος',
                        'eduadmin' => 'Διεύθυνση εκπαίδευσης',
                        'region_edu_admin' => 'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'education_level' => 'Βαθμίδα εκπαίδευσης',
                    ],
                ],
                'labs' => [
                    'query' => 'SELECT lab.id AS id, '
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' TRIM(lab.name) AS name, '
                    . ' TRIM(labtype.name) AS type, '
                    . ' branch.name AS responsible_branch, '
                    . ' IF(lab.is_new = 1, "ΝΑΙ", "ΟΧΙ") AS is_new, '
                    . ' lab.area AS area, '
                    . ' lab.has_network AS has_network, '
                    . ' lab.has_server AS has_server, '
                    . ' GROUP_CONCAT(lesson.name SEPARATOR ", ") AS lessons, '
                    . ' TRIM(lab.use_in_program) AS use_in_program, '
                    . ' TRIM(lab.use_ext_program) AS use_ext_program '
                    . ' FROM lab '
                    . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                    . ' LEFT JOIN school ON lab.school_id = school.id '
                    . ' LEFT JOIN lab_lesson ON lab_lesson.lab_id = lab.id '
                    . ' LEFT JOIN lesson ON lab_lesson.lesson_id = lesson.id '
                    . ' LEFT JOIN teacher ON lab.responsible_id = teacher.id '
                    . ' LEFT JOIN branch ON branch.id = teacher.branch_id '
                    . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                    . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
                    . ' LEFT JOIN prefecture ON school.prefecture_id = prefecture.id '
                    . ' GROUP BY lab.id '
                    . ' ORDER BY school_name ',
                    'headers' => [
                        'id' => 'ID',
                        'school_registry_no' => 'Κωδικός σχολείου',
                        'school_name' => 'Ονομασία σχολείου',
                        'name' => 'Ονομασία χώρου',
                        'type' => 'Τύπος χώρου',
                        'responsible_branch' => 'Ειδικότητα υπευθύνου',
                        'is_new' => 'Νέος χώρος',
                        'area' => 'Επιφάνεια (m^2)',
                        'has_network' => 'Σύνδεση στο δίκτυο',
                        'has_server' => 'Διαθέτει server',
                        'lessons' => 'Μαθήματα',
                        'use_in_program' => 'Χρήση στα πλαίσια μαθημάτων',
                        'use_ext_program' => 'Χρήση για δραστηριότητες εκτός εκπαιδευτικού προγράμματος',
                    ],
                ],
                'assets' => [
                    'query' => 'SELECT TRIM(itemcategory.name) AS category, '
                    . ' schoolasset.qty AS qty, '
                    . ' schoolasset.acquisition_year AS acquisition_year, '
                    . ' lab.id AS lab_id, '
                    . ' TRIM(labtype.name) AS lab_type, '
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' schoolasset.comments AS comments '
                    . ' FROM schoolasset '
                    . ' LEFT JOIN itemcategory ON schoolasset.itemcategory_id = itemcategory.id '
                    . ' LEFT JOIN school ON schoolasset.school_id = school.id '
                    . ' LEFT JOIN lab ON schoolasset.lab_id = lab.id '
                    . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                    . ' GROUP BY schoolasset.id '
                    . ' ORDER BY lab.id',
                    'headers' => [
                        'category' => 'Είδος',
                        'qty' => 'Πλήθος ',
                        'acquisition_year' => 'Έτος κτήσης',
                        'lab_id' => 'ID χώρου',
                        'lab_type' => 'Τύπος χώρου',
                        'school_registry_no' => 'Κωδικός σχολείου',
                        'school_name' => 'Ονομασία σχολείου',
                        'comments' => 'Σχόλια - Παρατηρήσεις',
                    ],
                ],
                'software' => [
                    'query' => 'SELECT softwarecategory.name AS name, '
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' lab.id AS lab_id, '
                    . ' TRIM(labtype.name) AS lab_type, '
                    . ' TRIM(software.title) AS title, '
                    . ' TRIM(software.vendor) AS vendor, '
                    . ' TRIM(software.url) AS url '
                    . ' FROM software '
                    . ' LEFT JOIN softwarecategory ON software.softwarecategory_id = softwarecategory.id '
                    . ' LEFT JOIN school ON software.school_id = school.id '
                    . ' LEFT JOIN lab ON software.lab_id = lab.id '
                    . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                    . ' ORDER BY school_name ',
                    'data_post_handle_callback' => function ($data) {
                        return array_map(function ($row) {
                                $row['url'] = strtolower($row['url']);
                                $row['url'] = str_replace('\\', '/', $row['url']);
                                $row['url'] = urldecode($row['url']);
                                return $row;
                            }, $data);
                    },
                    'headers' => [
                        'name' => 'Τύπος',
                        'school_registry_no' => 'Κωδικός σχολείου',
                        'school_name' => 'Ονομασία σχολείου',
                        'lab_id' => 'ID χώρου',
                        'lab_type' => 'Τύπος χώρου',
                        'title' => 'Ονομασία',
                        'vendor' => 'Κατασκευαστής',
                        'url' => 'URL',
                    ],
                ],
                'applications' => [
                    'query' => 'SELECT applicationform.id AS id, '
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' eduadmin.name AS eduadmin, '
                    . ' regioneduadmin.name AS region_edu_admin, '
                    . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
                    . ' TRIM(applicationform.comments) AS comments'
                    . ' FROM applicationformitem '
                    . ' LEFT JOIN applicationform ON applicationformitem.applicationform_id = applicationform.id '
                    . ' LEFT JOIN school ON applicationform.school_id = school.id '
                    . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                    . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
                    . ' LEFT JOIN prefecture ON school.prefecture_id = prefecture.id '
                    . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id '
                    . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id '
                    . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                    . ' WHERE applicationform.id IN '
                    . '('
                    . 'SELECT id FROM applicationform WHERE (submitted)IN (SELECT MAX( submitted )FROM applicationform '
                    . ' LEFT JOIN applicationformitem ON applicationformitem.applicationform_id = applicationform.id '
                    . ' LEFT JOIN school ON applicationform.school_id = school.id'
                    . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id'
                    . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id'
                    . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id'
                    . ' WHERE itemcategory.groupflag NOT IN ( :version )'
                    . ' GROUP BY school.id)'
                    . ')'
                    . ' GROUP BY school.id ',
                    'headers' => [
                        'id' => 'ID',
                        'school_registry_no' => 'Κωδικός σχολείου',
                        'school_name' => 'Ονομασία σχολείου',
                        'eduadmin' => 'Διεύθυνση εκπαίδευσης',
                        'region_edu_admin' => 'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'submitted' => 'Ημερομηνία υποβολής',
                        'comments' => 'Σχόλια - Παρατηρήσεις',
                    ],
                    'params_callback' => function ($container) {
                    $settings = $container->get('settings');
                    return [
                        ':version' => $settings['application_form']['itemcategory']['currentversion'],
                    ];
                },
                ],
                'new_applications' => [
                    'query' =>
                    'SELECT applicationform.id AS id, '
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' eduadmin.name AS eduadmin, '
                    . ' regioneduadmin.name AS region_edu_admin, '
                    . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
                    . ' TRIM(applicationform.comments) AS comments'
                    . ' FROM applicationformitem '
                    . ' LEFT JOIN applicationform ON applicationformitem.applicationform_id = applicationform.id '
                    . ' LEFT JOIN school ON applicationform.school_id = school.id '
                    . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                    . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
                    . ' LEFT JOIN prefecture ON school.prefecture_id = prefecture.id '
                    . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id '
                    . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id '
                    . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                    . ' WHERE applicationform.id IN '
                    . '('
                    . 'SELECT id FROM applicationform WHERE (submitted) IN( SELECT MAX(submitted) FROM applicationform GROUP BY school_id)'
                    . ')'
                    . ' AND itemcategory.groupflag IN ( :version )'
                    . ' GROUP BY school.id ',
                    'headers' => [
                        'id' => 'ID',
                        'school_registry_no' => 'Κωδικός σχολείου',
                        'school_name' => 'Ονομασία σχολείου',
                        'eduadmin' => 'Διεύθυνση εκπαίδευσης',
                        'region_edu_admin' => 'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'submitted' => 'Ημερομηνία υποβολής',
                        'comments' => 'Σχόλια - Παρατηρήσεις',
                    ],
                    'params_callback' => function ($container) {
                    $settings = $container->get('settings');
                    return [
                        'version' => $settings['application_form']['itemcategory']['currentversion'],
                    ];
                },
                ],
                'application_items' => [
                    'query' =>
                    'SELECT applicationform.id AS id, '
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' eduadmin.name AS eduadmin, '
                    . ' regioneduadmin.name AS region_edu_admin, '
                    . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
                    . ' lab.id AS lab_id, '
                    . ' TRIM(labtype.name) AS lab_type, '
                    . ' IF(lab.is_new = 1, "ΝΑΙ", "ΟΧΙ") AS is_new, '
                    . ' TRIM(itemcategory.name) AS category, '
                    . ' applicationformitem.qty AS qty, '
                    . ' TRIM(applicationformitem.reasons) AS reasons '
                    . ' FROM applicationformitem '
                    . ' LEFT JOIN applicationform ON applicationformitem.applicationform_id = applicationform.id '
                    . ' LEFT JOIN school ON applicationform.school_id = school.id '
                    . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                    . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
                    . ' LEFT JOIN prefecture ON school.prefecture_id = prefecture.id '
                    . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id '
                    . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id '
                    . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                    . ' WHERE applicationform.id IN '
                    . '('
                    . 'SELECT id FROM applicationform WHERE (submitted)IN (SELECT MAX( submitted )FROM applicationform '
                    . ' LEFT JOIN applicationformitem ON applicationformitem.applicationform_id = applicationform.id '
                    . ' LEFT JOIN school ON applicationform.school_id = school.id'
                    . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id'
                    . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id'
                    . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id'
                    . ' WHERE itemcategory.groupflag NOT IN ( :version )'
                    . ' GROUP BY school.id)'
                    . ') '
                    ,
                    'headers' => [
                        'id' => 'ID αίτησης',
                        'school_registry_no' => 'Κωδικός σχολείου',
                        'school_name' => 'Ονομασία σχολείου',
                        'eduadmin' => 'Διεύθυνση εκπαίδευσης',
                        'region_edu_admin' => 'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'submitted' => 'Ημερομηνία υποβολής',
                        'lab_id' => 'ID χώρου',
                        'lab_type' => 'Τύπος χώρου',
                        'is_new' => 'Νέος χώρος',
                        'category' => 'Είδος',
                        'qty' => 'Πλήθος ',
                        'reasons' => 'Αιτιολογία χρήσης',
                    ],
                    'params_callback' => function ($container) {
                    $settings = $container->get('settings');
                    return [
                        'version' => $settings['application_form']['itemcategory']['currentversion'],
                    ];
                },
                ],
                'new_application_items' => [
                    'query' => 'SELECT applicationform.id AS id, '
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' eduadmin.name AS eduadmin, '
                    . ' regioneduadmin.name AS region_edu_admin, '
                    . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
                    . ' lab.id AS lab_id, '
                    . ' TRIM(labtype.name) AS lab_type, '
                    . ' IF(lab.is_new = 1, "ΝΑΙ", "ΟΧΙ") AS is_new, '
                    . ' TRIM(itemcategory.name) AS category, '
                    . ' applicationformitem.qty AS qty, '
                    . ' applicationformitem.qtyacquired AS qtyacquired, '
                    . ' TRIM(applicationformitem.reasons) AS reasons '
                    . ' FROM applicationformitem '
                    . ' LEFT JOIN applicationform ON applicationformitem.applicationform_id = applicationform.id '
                    . ' LEFT JOIN school ON applicationform.school_id = school.id '
                    . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                    . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
                    . ' LEFT JOIN prefecture ON school.prefecture_id = prefecture.id '
                    . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id '
                    . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id '
                    . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                    . ' WHERE applicationform.id IN '
                    . '('
                    . 'SELECT id FROM applicationform WHERE (submitted) IN( SELECT MAX(submitted) FROM applicationform GROUP BY school_id)'
                    . ')'
                    . ' AND itemcategory.groupflag IN ( :version )',
                    'headers' => [
                        'id' => 'ID',
                        'school_registry_no' => 'Κωδικός σχολείου',
                        'school_name' => 'Ονομασία σχολείου',
                        'eduadmin' => 'Διεύθυνση εκπαίδευσης',
                        'region_edu_admin' => 'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'submitted' => 'Ημερομηνία υποβολής',
                        'lab_id' => 'ID χώρου',
                        'lab_type' => 'Τύπος χώρου',
                        'is_new' => 'Νέος χώρος',
                        'category' => 'Είδος',
                        'qtyacquired' => 'Πλήθος Υπαρχόντων που λειτουργούν',
                        'qty' => 'Πλήθος Αιτουμένων',
                        'reasons' => 'Αιτιολογία χρήσης',
                    ],
                    'params_callback' => function ($container) {
                    $settings = $container->get('settings');
                    return [
                        'version' => $settings['application_form']['itemcategory']['currentversion'],
                    ];
                },
                ],
                'approved' => [
                    'query' => 'SELECT school.registry_no AS school_registry_no, '
                    . 'school.name AS school_name,'
                    . 'regioneduadmin.name AS regionedu_name,'
                    . 'eduadmin.name AS eduadmin_name '
                    . 'FROM applicationform '
                    . 'JOIN school ON applicationform.school_id=school.id '
                    . 'JOIN eduadmin ON school.eduadmin_id=eduadmin.id '
                    . 'JOIN regioneduadmin ON eduadmin.regioneduadmin_id=regioneduadmin.id '
                    . ' LEFT JOIN prefecture ON school.prefecture_id = prefecture.id '
                    . 'WHERE 0=1 ' // 'WHERE applicationform.approved=1 '
                    . 'ORDER BY regioneduadmin.name, eduadmin.name, school.name',
                    'headers' => [
                        'school_registry_no' => 'Κωδικός σχολείου',
                        'school_name' => 'Ονομασία σχολείου',
                        'regionedu_name' => 'Περιφέρεια',
                        'eduadmin_name' => 'Διεύθυνση',
                    ],
                ],
                'eduadminunits' => [
                    'query' => 'SELECT id, name '
                    . 'FROM eduadmin '
                    . 'ORDER BY name ',
                    'headers' => [
                        'id' => 'Κωδικός',
                        'name' => 'Ονομασία',
                    ],
                ],
                'regioneduadminunits' => [
                    'query' => 'SELECT id, name '
                    . 'FROM regioneduadmin '
                    . 'ORDER BY name ',
                    'headers' => [
                        'id' => 'Κωδικός',
                        'name' => 'Ονομασία',
                    ],
                ],
                'educationlevels' => [
                    'query' => 'SELECT id, name '
                    . 'FROM educationlevel '
                    . 'ORDER BY name ',
                    'headers' => [
                        'id' => 'Κωδικός',
                        'name' => 'Ονομασία',
                    ],
                ],
                'municipalities' => [
                    'query' => 'SELECT distinct municipality '
                    . 'FROM school '
                    . 'ORDER BY municipality ',
                    'headers' => [
                        'municipality' => 'Ονομασία δήμου',
                    ],
                ],
            ];
        };
    });

    /**
     * Adds api routes to acl 
     */
    $events('on', 'app.services', function ($container) use ($filter_enabled_queries, $raw_csv_export_providers) {
        $data_retrieve_query_types = array_keys($container['data_retrieve_query_specs']);
        $acl = $container['settings']['acl'];

        // custom endpoints 
        $acl['guards']['routes'] = array_merge($acl['guards']['routes'], [
            ['/open-data/api', ['guest', 'user'], ['get']],
            ['/open-data/api/index', ['guest', 'user'], ['get']],
            ['/open-data/api/prefectures', ['guest', 'user'], ['get']],
            ['/open-data/api/prefecture/{name}', ['guest', 'user'], ['get']],
            ['/open-data/api/itemcategorynames', ['guest', 'user'], ['get']],
        ]);

        // endpoints for page-enabled results
        foreach ($data_retrieve_query_types as $data_retrieve_query_type) {
            $acl['guards']['routes'][] = ["/open-data/api/{$data_retrieve_query_type}", ['guest', 'user'], ['get']];
        }
        // endpoints for filter enabled results
        foreach ($filter_enabled_queries as $spec_key) {
            $acl['guards']['routes'][] = ["/open-data/api/{$spec_key}/eduadmin/{eduadmin}", ['guest', 'user'], ['get']];
            $acl['guards']['routes'][] = ["/open-data/api/{$spec_key}/regioneduadmin/{regioneduadmin}", ['guest', 'user'], ['get']];
            $acl['guards']['routes'][] = ["/open-data/api/{$spec_key}/prefecture/{prefecture}", ['guest', 'user'], ['get']];
            $acl['guards']['routes'][] = ["/open-data/api/{$spec_key}/municipality/{municipality}", ['guest', 'user'], ['get']];
        }

        // endpoints for results by custom or combined filters 
        $acl['guards']['routes'] = array_merge($acl['guards']['routes'], [
            ["/open-data/api/schools/education_level/{education_level}", ['guest', 'user'], ['get']],
            ["/open-data/api/schools/prefecture/{prefecture}/education_level/{education_level}", ['guest', 'user'], ['get']],
            ["/open-data/api/school/{registry_no:[0-9]+}/application_items", ['guest', 'user'], ['get']],
            ["/open-data/api/school/{registry_no:[0-9]+}/new_application_items", ['guest', 'user'], ['get']],
            ["/open-data/api/school/{registry_no:[0-9]+}/labs", ['guest', 'user'], ['get']],
        ]);

        $container['settings']->set('acl', $acl);
    });

    $events('on', 'app.services', function ($container) use ($filter_enabled_queries, $raw_csv_export_providers) {
        $specs = $container['data_retrieve_query_specs'];
        $data_retrieve_query_types = array_keys($specs);

        // root api page handler 
        $container[GrEduLabs\OpenData\Service\IndexProvider::class] = function ($c) {
            $settings = $c->get('settings');
            return new GrEduLabs\OpenData\Service\IndexProvider((string) $settings['api_doc_url'], $c['router']);
        };
        $container[GrEduLabs\OpenData\Action\Index::class] = function ($c) {
            return new GrEduLabs\OpenData\Action\Index(
                $c, $c->get(GrEduLabs\OpenData\Service\IndexProvider::class), true
            );
        };
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
468

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
        // prefectures endpoint handler 
        $container[GrEduLabs\OpenData\Service\PrefecturesProvider::class] = function ($c) {
            return new GrEduLabs\OpenData\Service\PrefecturesProvider();
        };
        $container[GrEduLabs\OpenData\Action\Prefectures::class] = function ($c) {
            return new GrEduLabs\OpenData\Action\Prefectures(
                $c, $c->get(GrEduLabs\OpenData\Service\PrefecturesProvider::class), true
            );
        };

        // item categories endpoint handler 
        $container[GrEduLabs\OpenData\Service\ItemCategoryNamesProvider::class] = function ($c) {
            return new GrEduLabs\OpenData\Service\ItemCategoryNamesProvider();
        };
        $container[GrEduLabs\OpenData\Action\ItemCategoryNames::class] = function ($c) {
            return new GrEduLabs\OpenData\Action\ItemCategoryNames(
                $c, $c->get(GrEduLabs\OpenData\Service\ItemCategoryNamesProvider::class), false
            );
        };

        // unified paged interface 
        foreach ($data_retrieve_query_types as $data_retrieve_query_type) {
            $spec = $specs[$data_retrieve_query_type];
            $container["{$data_retrieve_query_type}_provider"] = function ($c) use ($spec) {
                $dataProvider = new GrEduLabs\OpenData\Service\RedBeanQueryPagedDataProvider();
                $dataProvider->setPagesize(20);
                $dataProvider->setPage(1);
                $params = (isset($spec['params_callback']) ? $spec['params_callback']($c) : []);
                $dataProvider->setQuery($spec['query'], $params);
                if (isset($spec['data_post_handle_callback'])) {
                    $dataProvider->setDataPostHandleCallback($spec['data_post_handle_callback']);
                }
                if (isset($spec['headers'])) {
                    $dataProvider->setLabels($spec['headers']);
                }
                return $dataProvider;
            };
            $container["{$data_retrieve_query_type}_action"] = function ($c) use ($data_retrieve_query_type) {
                return new GrEduLabs\OpenData\Action\PagedApiAction(
                    $c, $c->get("{$data_retrieve_query_type}_provider"), true
                );
            };
        }

        // custom /schools{/<prefecture>} /schools{/education_level} ??
        $container[GrEduLabs\OpenData\Action\SchoolsFilteredAction::class . "_provider"] = function ($c) use ($specs) {
            $dataProvider = new GrEduLabs\OpenData\Service\RedBeanFilteredQueryPagedDataProvider();
            $dataProvider->setLabels($specs['schools']['headers']);
            $dataProvider->setQuery($specs['schools']['query']);
            return $dataProvider;
        };
        $container[GrEduLabs\OpenData\Action\SchoolsFilteredAction::class] = function ($c) {
            return new GrEduLabs\OpenData\Action\SchoolsFilteredAction(
                $c, $c->get(GrEduLabs\OpenData\Action\SchoolsFilteredAction::class . "_provider"), true
            );
        };

        // eduadmin, regioneduadmin and prefecture filter enabled actions 
        foreach ($filter_enabled_queries as $spec_key) {
            $container["MunicipalityPrefectureEduadminFiltered_{$spec_key}_provider"] = function ($c) use ($specs, $spec_key) {
                $spec = $specs[$spec_key];
                $dataProvider = new GrEduLabs\OpenData\Service\RedBeanFilteredQueryPagedDataProvider();
                $dataProvider->setLabels($spec['headers']);
                $dataProvider->setQuery($spec['query'], isset($spec['params_callback']) ? $spec['params_callback']($c) : []);
                return $dataProvider;
            };
            $container["{$spec_key}_filtered_action"] = function ($c) use ($spec_key) {
                return new GrEduLabs\OpenData\Action\MunicipalityPrefectureEduadminFilteredPagedApiAction(
                    $c, $c->get("MunicipalityPrefectureEduadminFiltered_{$spec_key}_provider"), true
                );
            };
        }

        // application items by school 
        $container["application_items_of_school_filtered_action"] = function ($c) {
            return new GrEduLabs\OpenData\Action\RegistryEduadminFilteredPagedApiAction(
                $c, $c->get("MunicipalityPrefectureEduadminFiltered_application_items_provider"), true
            );
        };
        $container["new_application_items_of_school_filtered_action"] = function ($c) {
            return new GrEduLabs\OpenData\Action\RegistryEduadminFilteredPagedApiAction(
                $c, $c->get("MunicipalityPrefectureEduadminFiltered_new_application_items_provider"), true
            );
        };
        $container["labs_of_school_filtered_action"] = function ($c) {
            return new GrEduLabs\OpenData\Action\RegistryEduadminFilteredPagedApiAction(
                $c, $c->get("MunicipalityPrefectureEduadminFiltered_labs_provider"), true
            );
        };
    });

    $events('on', 'app.bootstrap', function (App $app, Container $c) use ($filter_enabled_queries, $raw_csv_export_providers) {
        $data_retrieve_query_types = array_keys($c['data_retrieve_query_specs']);
        $router = $c['router'];
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
563 564 565 566 567 568 569

        $app->get('/open-data', function (Request $req, Response $res) use ($c) {
            $view = $c->get('view');
            $view->getEnvironment()->getLoader()->prependPath(__DIR__ . '/templates');

            return $view->render($res, 'open_data/index.twig');
        })->setName('open_data');
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

        /**
         * Define api routes. Each route is handled by a devoted class.
         */
        $app->group('/open-data/api', function () use ($data_retrieve_query_types, $filter_enabled_queries, $raw_csv_export_providers, $router) {
            $this->get('', function (Request $request, Response $response) use ($router) {
                    return $response->withStatus(302)->withHeader('Location', $router->pathFor('open_data.api.index'));
                })
                ->setName('open_data.api');
            $this->get('/index', Action\Index::class)
                ->setName('open_data.api.index');

            $this->get('/prefectures', Action\Prefectures::class)
                ->setName('open_data.api.prefectures');
            $this->get('/prefecture/{name}', Action\Prefectures::class)
                ->setName('open_data.api.prefecture');

            $this->get('/itemcategorynames', Action\ItemCategoryNames::class)
                ->setName('open_data.api.itemcategorynames');

            // page enabled actions 
            foreach ($data_retrieve_query_types as $data_retrieve_query_type) {
                $this->get("/{$data_retrieve_query_type}", "{$data_retrieve_query_type}_action")
                    ->setName("open_data.api.{$data_retrieve_query_type}");
            }

            // custom filter enabled actions for schools 
            // moved to generic handler
//            $this->get('/schools/prefecture/{prefecture}', Action\SchoolsFilteredAction::class)
//                ->setName('open_data.api.schools.prefecture');
            $this->get('/schools/education_level/{education_level}', Action\SchoolsFilteredAction::class)
                ->setName('open_data.api.schools.education_level');
            $this->get('/schools/prefecture/{prefecture}/education_level/{education_level}', Action\SchoolsFilteredAction::class)
                ->setName('open_data.api.schools.prefecture_education_level');

            // eduadmin, regioneduadmin, prefecture and municipality filter enabled actions 
            foreach ($filter_enabled_queries as $spec_key) {
                $this->get("/{$spec_key}/eduadmin/{eduadmin}", "{$spec_key}_filtered_action")
                    ->setName("open_data.api.{$spec_key}.eduadmin");
                $this->get("/{$spec_key}/regioneduadmin/{regioneduadmin}", "{$spec_key}_filtered_action")
                    ->setName("open_data.api.{$spec_key}.regioneduadmin");
                $this->get("/{$spec_key}/prefecture/{prefecture}", "{$spec_key}_filtered_action")
                    ->setName("open_data.api.{$spec_key}.prefecture");
                $this->get("/{$spec_key}/municipality/{municipality}", "{$spec_key}_filtered_action")
                    ->setName("open_data.api.{$spec_key}.municipality");
            }

            // application items by school 
            $this->get("/school/{registry_no:[0-9]+}/application_items", "application_items_of_school_filtered_action")
                ->setName("open_data.api.school.application_items");
            $this->get("/school/{registry_no:[0-9]+}/new_application_items", "new_application_items_of_school_filtered_action")
                ->setName("open_data.api.school.new_application_items");
            $this->get("/school/{registry_no:[0-9]+}/labs", "labs_of_school_filtered_action")
                ->setName("open_data.api.school.labs");
        });
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
625 626
    });
};