bootstrap.php 24.9 KB
Newer Older
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php

use League\Csv\Reader;
use League\Csv\Writer;
use RedBeanPHP\R;
use Slim\App;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;

/**
 * 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();
21
    $events = $container['events'];
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
22 23 24 25 26 27 28 29

    $events('on', 'app.services', function (Container $c) {

        $c['csv_export_config'] = function ($c) {

            return [
                'schools' => [
                    'data_callback' => 'csv_export_schools',
30
                    'headers' => [
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
31 32 33 34 35 36 37 38 39 40 41
                        'Κωδικός μονάδας',
                        'Ονομασία',
                        'Τύπος μονάδας',
                        'Περιφερειακή ενότητα',
                        'Διεύθυνση εκπαίδευσης',
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'Βαθμίδα εκπαίδευσης',
                    ],
                ],
                'labs' => [
                    'data_callback' => 'csv_export_labs',
42
                    'headers' => [
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
43 44 45
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
46
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60
                        'Ονομασία χώρου',
                        'Τύπος χώρου',
                        'Ειδικότητα υπευθύνου',
                        'Νέος χώρος',
                        'Επιφάνεια (m^2)',
                        'Σύνδεση στο δίκτυο',
                        'Διαθέτει server',
                        'Μαθήματα',
                        'Χρήση στα πλαίσια μαθημάτων',
                        'Χρήση για δραστηριότητες εκτός εκπαιδευτικού προγράμματος',
                    ],
                ],
                'assets' => [
                    'data_callback' => 'csv_export_assets',
61
                    'headers' => [
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
62 63 64 65 66 67 68
                        'Είδος',
                        'Πλήθος ',
                        'Έτος κτήσης',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
69
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
70 71
                        'Σχόλια - Παρατηρήσεις',
                    ],
72 73 74 75 76 77 78
                ],
                 'software' => [
                    'data_callback' => 'csv_export_software',
                    'headers' => [
                        'Τύπος',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
79
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
80 81 82 83 84 85
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Ονομασία',
                        'Κατασκευαστής',
                        'URL',
                    ],
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
86 87 88
                ],
                'appforms' => [
                    'data_callback' => 'csv_export_appforms',
89
                    'headers' => [
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
90 91 92
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
93
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
94 95 96 97
                        'Ημερομηνία υποβολής',
                        'Σχόλια - Παρατηρήσεις',
                    ],
                ],
98 99 100 101 102 103 104
             
 				'appnewforms' => [
                    'data_callback' => 'csv_export_appnewforms',
                    'headers' => [
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
105
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
106 107 108 109 110 111
                        'Ημερομηνία υποβολής',
                        'Σχόλια - Παρατηρήσεις',
                    ],
                ],
             

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
112 113
                'appforms_items' => [
                    'data_callback' => 'csv_export_appforms_items',
114
                    'headers' => [
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
115 116 117
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
118
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
119 120 121 122 123 124 125 126 127
                        'Ημερομηνία υποβολής',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Νέος χώρος',
                        'Είδος',
                        'Πλήθος ',
                        'Αιτιολογία χρήσης',
                    ],
                ],
128 129 130 131 132 133
                'newapplication' => [
                    'data_callback' => 'csv_export_newapplication',
                    'headers' => [
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
134 135 136 137 138
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'Ημερομηνία υποβολής',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Νέος χώρος',
139 140
                        'Είδος',
                        'Πλήθος Αιτουμένων',
141
                        'Πλήθος Υπαρχόντων που λειτουργούν',
142
                        'Αιτιολογία χρήσης',
143 144
                    ],
                ],
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
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
            ];
        };

        $c['csv_export_csv_response'] = function ($c) {

            return function ($res, callable $dataCallback, array $headers, $filename) use ($c) {
                $csvFactory = $c['csv_export_csv_factory'];
                $csv = $csvFactory($dataCallback(), $headers);
                $res = $res->withHeader('Content-Type', 'application/octet-stream');
                $res = $res->withHeader('Content-Transfer-Encoding', 'binary');
                $res = $res->withHeader('Content-Disposition', 'attachment; filename=' . $filename);
                $res->write($csv);

                return $res;
            };
        };

        $c['csv_export_csv_factory'] = function ($c) {

            return function (array $data, array $headers = null) {
                $csv = Writer::createFromFileObject(new SplTempFileObject());
                $csv->setOutputBOM(Reader::BOM_UTF8);
                $csv->addFormatter(function ($row) {
                    return array_map(function ($value) {
                        return str_replace(["\n", "\r"], ' ', $value);
                    }, $row);
                });
                if (null !== $headers) {
                    $csv->insertOne($headers);
                }

                $csv->insertAll($data);

                return $csv;
            };
        };

        $c['csv_export_schools'] = function ($c) {

            return function () {
                $sql = 'SELECT school.registry_no AS registry_no, '
186 187 188 189 190 191 192 193 194 195 196 197 198 199
                        . ' school.name AS school_name, '
                        . ' schooltype.name as school_type, '
                        . ' prefecture.name AS prefecture, '
                        . ' 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';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
200 201 202 203 204 205 206 207 208 209
                $schools = R::getAll($sql);

                return $schools;
            };
        };

        $c['csv_export_labs'] = function ($c) {

            return function () {
                $sql = 'SELECT lab.id AS id, '
210 211
                        . ' school.registry_no AS school_registry_no, '
                        . ' school.name AS school_name, '
212
                        . ' regioneduadmin.name AS region_edu_admin, '
213 214 215 216 217 218 219 220 221 222 223 224 225
                        . ' 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 '
226 227
                        . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                        . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
228 229 230 231 232 233
                        . ' 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 '
                        . ' GROUP BY lab.id '
                        . ' ORDER BY school_name ';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
234 235 236 237 238 239 240 241 242 243 244

                $labs = R::getAll($sql);

                return $labs;
            };
        };

        $c['csv_export_assets'] = function ($c) {

            return function () {
                $sql = 'SELECT TRIM(itemcategory.name) AS category, '
245 246 247 248 249 250
                        . ' 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, '
251
                        . ' regioneduadmin.name AS region_edu_admin, '
252 253 254 255
                        . ' schoolasset.comments AS comments '
                        . ' FROM schoolasset '
                        . ' LEFT JOIN itemcategory ON schoolasset.itemcategory_id = itemcategory.id '
                        . ' LEFT JOIN school ON schoolasset.school_id = school.id '
256 257
                        . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                        . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
258 259 260 261
                        . ' 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';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
262 263 264 265 266 267 268

                $assets = R::getAll($sql);

                return $assets;
            };
        };

269
        $c['csv_export_software'] = function ($c) {
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
270 271

            return function () {
272
                $sql = 'SELECT softwarecategory.name AS name, '
273
                        . ' school.registry_no AS school_registry_no, '
274
                        . ' school.name AS school_name, '
275
                        . ' regioneduadmin.name AS region_edu_admin, '
276 277 278 279 280 281 282 283
                        . ' 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 '
284 285
                        . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                        . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
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
                        . ' LEFT JOIN lab ON software.lab_id = lab.id '
                        . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                        . ' ORDER BY school_name ';

                $software = R::getAll($sql);

                $software = array_map(function ($row) {
                    $row['url'] = strtolower($row['url']);
                    $row['url'] = str_replace('\\', '/', $row['url']);
                    $row['url'] = urldecode($row['url']);

                    return $row;
                }, $software);

                return $software;
            };
        };

        $c['csv_export_appforms'] = function ($c) {

        	 return function () use ($c) {
				$settings = $c->get('settings');
                $version = $settings['application_form']['itemcategory']['currentversion'];

                $appFormIdsSql = '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)';



                $appFormIds = R::getCol($appFormIdsSql);

                if (empty($appFormIds)) {
                    return [];
                }

                $in = implode(',', array_fill(0, count($appFormIds), '?'));


				 $sql = 'SELECT applicationform.id AS id, '
                       . ' school.registry_no AS school_registry_no, '
332
                        . ' school.name AS school_name, '
333
                        . ' regioneduadmin.name AS region_edu_admin, '
334
                        . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
335 336 337
                        . ' TRIM(applicationform.comments) AS comments' 
                        . ' FROM applicationformitem '
                        . ' LEFT JOIN applicationform ON applicationformitem.applicationform_id = applicationform.id '
338
                        . ' LEFT JOIN school ON applicationform.school_id = school.id '
339 340
                        . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                        . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
341 342 343 344 345
                        . ' 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(' . $in . ')'
                          . ' GROUP BY school.id ';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
346

347
                $appForms = R::getAll($sql, $appFormIds);
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
348 349

                return $appForms;
350

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
351 352 353
            };
        };

354
        $c['csv_export_appnewforms'] = function ($c) {
355

356
            return function () use ($c) {
357
                $appFormIdsSql = 'SELECT id FROM applicationform WHERE (submitted) IN( SELECT MAX(submitted) FROM applicationform GROUP BY school_id)';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
358 359 360

                $appFormIds = R::getCol($appFormIdsSql);

361 362 363
                if (empty($appFormIds)) {
                    return [];
                }
364
                $in = implode(',', array_fill(0, count($appFormIds), '?'));
365 366 367 368

                $settings = $c->get('settings');
                $version = $settings['application_form']['itemcategory']['currentversion'];

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
369
                $sql = 'SELECT applicationform.id AS id, '
370
                       . ' school.registry_no AS school_registry_no, '
371
                        . ' school.name AS school_name, '
372
                        . ' regioneduadmin.name AS region_edu_admin, '
373
                        . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
374
                        . ' TRIM(applicationform.comments) AS comments' 
375 376 377
                        . ' FROM applicationformitem '
                        . ' LEFT JOIN applicationform ON applicationformitem.applicationform_id = applicationform.id '
                        . ' LEFT JOIN school ON applicationform.school_id = school.id '
378 379
                        . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                        . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
380 381 382
                        . ' 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 '
383 384 385
                        . ' WHERE applicationform.id IN(' . $in . ')'
                        . ' AND itemcategory.groupflag IN(' . $version . ')'
                        . ' GROUP BY school.id ';
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
386 387 388 389 390 391

                $appForms = R::getAll($sql, $appFormIds);

                return $appForms;
            };
        };
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
        $c['csv_export_appforms_items'] = function ($c) {

            return function () use ($c){

            	$settings = $c->get('settings');
                $version = $settings['application_form']['itemcategory']['currentversion'];

                $appFormIdsSql = '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)';

                $appFormIds = R::getCol($appFormIdsSql);

                if (empty($appFormIds)) {
                    return [];
                }

                $in = implode(',', array_fill(0, count($appFormIds), '?'));
                $sql = 'SELECT applicationform.id AS id, '
417 418
                        . ' school.registry_no AS school_registry_no, '
                        . ' school.name AS school_name, '
419
                        . ' regioneduadmin.name AS region_edu_admin, '
420
                        . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
421 422
                        . ' lab.id AS lab_id, '
                        . ' TRIM(labtype.name) AS lab_type, '
423 424 425 426 427 428 429
                        . ' 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 '
430 431
                        . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                        . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
432 433
                        . ' LEFT JOIN itemcategory ON applicationformitem.itemcategory_id = itemcategory.id '
                        . ' LEFT JOIN lab ON applicationformitem.lab_id = lab.id '
434
                        . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
435
                        . ' WHERE applicationform.id IN(' . $in . ') ';
436

437
                $appForms = R::getAll($sql, $appFormIds);
438

439
                return $appForms;
440 441
            };
        };
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

        $c['csv_export_newapplication'] = function ($c) {

            return function () use ($c) {

                $appFormIdsSql = 'SELECT id FROM applicationform WHERE (submitted) IN( SELECT MAX(submitted) FROM applicationform GROUP BY school_id)';

                $appFormIds = R::getCol($appFormIdsSql);


                if (empty($appFormIds)) {
                    return [];
                }
                $in = implode(',', array_fill(0, count($appFormIds), '?'));


                $settings = $c->get('settings');
                $version = $settings['application_form']['itemcategory']['currentversion'];

                $sql = 'SELECT applicationform.id AS id, '
                        . ' school.registry_no AS school_registry_no, '
                        . ' school.name AS school_name, '
464 465 466 467 468
                        . ' 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, '
469 470 471 472 473 474 475
                        . ' 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 '
476 477
                        . ' LEFT JOIN eduadmin ON school.eduadmin_id = eduadmin.id '
                        . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
478 479 480 481 482 483 484 485 486 487 488 489
                        . ' 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(' . $in . ')'
                        . ' AND itemcategory.groupflag IN(' . $version . ')';


                $appForms = R::getAll($sql, $appFormIds);

                return $appForms;
            };
        };
490

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
491 492 493 494 495 496
    });

    $events('on', 'app.bootstrap', function (App $app, Container $c) {

        $app->get('/export/csv/edulabs_{type}.csv', function (Request $req, Response $res, array $args) use ($c) {
            $type = $args['type'];
497 498
            try {
                $config = $c['csv_export_config'];
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
499

500 501 502 503 504 505
                if (!array_key_exists($type, $config)) {
                    return $res->withStatus(404);
                }

                $typeConfig = $config[$type];
                $csvResponse = $c['csv_export_csv_response'];
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
506

507 508 509 510 511 512 513 514
                return $csvResponse($res, $c[$typeConfig['data_callback']], $typeConfig['headers'], 'edulabs_' . $type . '.csv');
            } catch (Exception $ex) {
                $flash = $c['flash'];
                $log = $c['logger'];
                $router = $c['router'];
                $flash->addMessage('warning', 'Προέκυψε κάποιο σφάλμα. Δοκιμάστε αργότερα');
                $log->error(sprintf('Problem downloading %s file. Exception message %s', $type, $ex->getMessage()));
                $log->debug('Exception Trace', ['trace' => $ex->getTraceAsString()]);
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
515

516 517
                return $res->withRedirect($router->pathFor('index'));
            }
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
518 519 520 521 522 523 524 525 526 527
            $config = $c['csv_export_config'];

            if (!array_key_exists($type, $config)) {
                return $res->withStatus(404);
            }

            $typeConfig = $config[$type];
            $csvResponse = $c['csv_export_csv_response'];

            return $csvResponse($res, $c[$typeConfig['data_callback']], $typeConfig['headers'], 'edulabs_' . $type . '.csv');
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
528 529 530
        })->setName('export.csv');
    });
};