bootstrap.php 30.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?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;
10
use GrEduLabs\UniversityForm\Service\UniversityFormService;
11 12 13 14 15 16 17 18 19 20 21

/**
 * 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();
22
    $events = $container['events'];
23 24 25

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

26 27 28 29 30 31 32
        $c['csv_export_protected_exports'] = function ($c) {
            return [
                'volunteer_teachers',
                'volunteer_institutions',
            ];
        };

33 34 35 36 37
        $c['csv_export_config'] = function ($c) {

            return [
                'schools' => [
                    'data_callback' => 'csv_export_schools',
38
                    'headers' => [
39 40 41 42 43 44 45 46 47 48 49
                        'Κωδικός μονάδας',
                        'Ονομασία',
                        'Τύπος μονάδας',
                        'Περιφερειακή ενότητα',
                        'Διεύθυνση εκπαίδευσης',
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'Βαθμίδα εκπαίδευσης',
                    ],
                ],
                'labs' => [
                    'data_callback' => 'csv_export_labs',
50
                    'headers' => [
51 52 53
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
54
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
55 56 57 58 59 60 61 62 63 64 65 66 67 68
                        'Ονομασία χώρου',
                        'Τύπος χώρου',
                        'Ειδικότητα υπευθύνου',
                        'Νέος χώρος',
                        'Επιφάνεια (m^2)',
                        'Σύνδεση στο δίκτυο',
                        'Διαθέτει server',
                        'Μαθήματα',
                        'Χρήση στα πλαίσια μαθημάτων',
                        'Χρήση για δραστηριότητες εκτός εκπαιδευτικού προγράμματος',
                    ],
                ],
                'assets' => [
                    'data_callback' => 'csv_export_assets',
69
                    'headers' => [
70 71 72 73 74 75 76
                        'Είδος',
                        'Πλήθος ',
                        'Έτος κτήσης',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
77
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
78 79
                        'Σχόλια - Παρατηρήσεις',
                    ],
80
                ],
81
                'software' => [
82 83 84 85 86
                    'data_callback' => 'csv_export_software',
                    'headers' => [
                        'Τύπος',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
87
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
88 89 90 91 92 93
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Ονομασία',
                        'Κατασκευαστής',
                        'URL',
                    ],
94 95 96
                ],
                'appforms' => [
                    'data_callback' => 'csv_export_appforms',
97
                    'headers' => [
98 99 100
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
101
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
102 103 104 105
                        'Ημερομηνία υποβολής',
                        'Σχόλια - Παρατηρήσεις',
                    ],
                ],
106
                'appnewforms' => [
107 108 109 110 111
                    'data_callback' => 'csv_export_appnewforms',
                    'headers' => [
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
112
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
113 114 115 116
                        'Ημερομηνία υποβολής',
                        'Σχόλια - Παρατηρήσεις',
                    ],
                ],
117 118
                'appforms_items' => [
                    'data_callback' => 'csv_export_appforms_items',
119
                    'headers' => [
120 121 122
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
123
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
124 125 126 127 128 129 130 131 132
                        'Ημερομηνία υποβολής',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Νέος χώρος',
                        'Είδος',
                        'Πλήθος ',
                        'Αιτιολογία χρήσης',
                    ],
                ],
133 134 135 136 137 138
                'newapplication' => [
                    'data_callback' => 'csv_export_newapplication',
                    'headers' => [
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
139 140 141 142 143
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'Ημερομηνία υποβολής',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Νέος χώρος',
144 145
                        'Είδος',
                        'Πλήθος Αιτουμένων',
146
                        'Πλήθος Υπαρχόντων που λειτουργούν',
147
                        'Αιτιολογία χρήσης',
148 149
                    ],
                ],
150 151 152 153 154 155 156
                'volunteer_teachers' => [
                    'data_callback' => 'csv_export_volunteer_teachers',
                    'headers' => [
                        'ID',
                        'Όνομα',
                        'Επώνυμο',
                        'Ειδικότητα',
157
                        'Ειδικότητα (λεκτικό)',
158 159 160 161 162 163
                        'Αριθμός Μητρώου',
                        'Τηλέφωνο',
                        'Email',
                        'Σχολείο που υπηρετεί',
                        'Τηλέφωνο Σχολείου',
                        'Σχόλια/Παρατηρήσεις',
164 165 166
                        // 'Σχετική δράση που συμμετείχε',
                        // 'URL δράσης που συμμετείχε',
                        'Περιγραφή (ακολουθούν ανά στήλη)',
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
                        'Έργο 1',
                        'Έργο 2',
                        'Έργο 3',
                        'Έργο 4',
                        'Έργο 5',
                        'Έργο 6',
                        'Έργο 7',
                        'Έργο 8',
                        'Έργο 9',
                        'Έργο 10',
                    ],
                ],
                'volunteer_institutions' => [
                    'data_callback' => 'csv_export_volunteer_institutions',
                    'headers' => [
                        'ID',
                        'Ίδρυμα',
                        'Σχολή',
                        'Τμήμα',
                        'Ερευνητικό Κέντρο',
                        'Ινστιτούτο',
                        'Άλλο',
                        'Υπεύθυνος Επικοινωνίας',
                        'Τηλέφωνο',
                        'Email',
                        'Σχόλια/Παρατηρήσεις',
                        'Έργα',
                        'Περιγραφή (ακολουθούν ανά στήλη)',
                        'Έργο 1',
                        'Έργο 2',
                        'Έργο 3',
                        'Έργο 4',
                        'Έργο 5',
                        'Έργο 6',
                        'Έργο 7',
                        'Έργο 8',
                        'Έργο 9',
                        'Έργο 10',
                    ],
                ],
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
            ];
        };

        $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;
            };
        };

244 245 246
        $c['csv_export_volunteer_teachers'] = function ($c) {

            return function () {
247 248 249 250 251 252 253 254 255 256 257
                $sql = 'SELECT `volunteerteachers`.`id`, '
                    . '`volunteerteachers`.`name`, '
                    . '`volunteerteachers`.`surname`, '
                    . '`volunteerteachers`.`eidikothta`, '
                    . '`branch`.`name` AS `eidikothta_name`, '
                    . '`volunteerteachers`.`arithmitroou`, '
                    . '`volunteerteachers`.`telef`, '
                    . '`volunteerteachers`.`email`, '
                    . '`volunteerteachers`.`school`, '
                    . '`volunteerteachers`.`schooltelef`, '
                    . '`volunteerteachers`.`comments`, '
258 259
                    // . '`volunteerteachers`.`projecttitle`, '
                    // . '`volunteerteachers`.`projecturl`, '
260 261 262
                    . '`volunteerteachers`.`projectdescription` '
                    . ' FROM `volunteerteachers` JOIN `branch` ON (`volunteerteachers`.`eidikothta` = `branch`.`id`) '
//                    . ' FROM `volunteerteachers` '
263 264 265 266 267
                    . ' ORDER BY id ';

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

                $volunteer_teachers = array_map(function ($row) {
268 269 270 271
                    $row['projectdescription'] = UniversityFormService::recomposeProjects($row['projectdescription'], false);
                    if (($projects = preg_split('/' . UniversityFormService::PLAIN_TEXT_SEPARATOR . '/msUu', $row['projectdescription'])) !== false) {
                        foreach ($projects as $i => $proj) {
                            $row["project{$i}"] = $proj;
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
                        }
                    }
                    return $row;
                }, $volunteer_teachers);

                return $volunteer_teachers;
            };
        };

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

            return function () {
                $sql = 'SELECT `id`, '
                    . '`idrima`, '
                    . '`sxolh`, '
                    . '`tmhma`, '
                    . '`ereunitiko`, '
                    . '`institute`, '
                    . '`other`,'
                    . '`person`, '
                    . '`telef`, '
                    . '`email`, '
                    . '`comments`, '
                    . '`erga`, '
                    . '`projectdescription` '
                    . ' FROM `univ` '
                    . ' ORDER BY id';

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

                $volunteer_institutions = array_map(function ($row) {
303 304 305 306
                    $row['projectdescription'] = UniversityFormService::recomposeProjects($row['projectdescription'], false);
                    if (($projects = preg_split('/' . UniversityFormService::PLAIN_TEXT_SEPARATOR . '/msUu', $row['projectdescription'])) !== false) {
                        foreach ($projects as $i => $proj) {
                            $row["project{$i}"] = $proj;
307 308 309 310 311 312 313 314 315
                        }
                    }
                    return $row;
                }, $volunteer_institutions);

                return $volunteer_institutions;
            };
        };

316 317 318 319
        $c['csv_export_schools'] = function ($c) {

            return function () {
                $sql = 'SELECT school.registry_no AS registry_no, '
320 321 322 323 324 325 326 327 328 329 330 331 332 333
                    . ' 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';
334 335 336 337 338 339 340 341 342 343
                $schools = R::getAll($sql);

                return $schools;
            };
        };

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

            return function () {
                $sql = 'SELECT lab.id AS id, '
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' regioneduadmin.name AS region_edu_admin, '
                    . ' 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 eduadmin ON school.eduadmin_id = eduadmin.id '
                    . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.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 '
                    . ' GROUP BY lab.id '
                    . ' ORDER BY school_name ';
368 369 370 371 372 373 374 375 376 377 378

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

                return $labs;
            };
        };

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

            return function () {
                $sql = 'SELECT TRIM(itemcategory.name) AS category, '
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
                    . ' 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, '
                    . ' regioneduadmin.name AS region_edu_admin, '
                    . ' 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 eduadmin ON school.eduadmin_id = eduadmin.id '
                    . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.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';
396 397 398 399 400 401 402

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

                return $assets;
            };
        };

403
        $c['csv_export_software'] = function ($c) {
404 405

            return function () {
406
                $sql = 'SELECT softwarecategory.name AS name, '
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' regioneduadmin.name AS region_edu_admin, '
                    . ' 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 eduadmin ON school.eduadmin_id = eduadmin.id '
                    . ' LEFT JOIN regioneduadmin ON eduadmin.regioneduadmin_id = regioneduadmin.id '
                    . ' LEFT JOIN lab ON software.lab_id = lab.id '
                    . ' LEFT JOIN labtype ON lab.labtype_id = labtype.id '
                    . ' ORDER BY school_name ';
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

                $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) {

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

                $appFormIdsSql = 'SELECT id FROM applicationform WHERE (submitted)IN (SELECT MAX( submitted )FROM applicationform '
445 446 447 448 449 450 451
                    . ' 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)';
452 453 454 455 456 457 458 459 460 461 462 463



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

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

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


464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
                $sql = 'SELECT applicationform.id AS id, '
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' 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 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 ';
480

481
                $appForms = R::getAll($sql, $appFormIds);
482 483 484 485 486

                return $appForms;
            };
        };

487
        $c['csv_export_appnewforms'] = function ($c) {
488

489
            return function () use ($c) {
490
                $appFormIdsSql = 'SELECT id FROM applicationform WHERE (submitted) IN( SELECT MAX(submitted) FROM applicationform GROUP BY school_id)';
491 492 493

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

494 495 496
                if (empty($appFormIds)) {
                    return [];
                }
497
                $in = implode(',', array_fill(0, count($appFormIds), '?'));
498 499 500 501

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

502
                $sql = 'SELECT applicationform.id AS id, '
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' 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 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 . ')'
                    . ' GROUP BY school.id ';
519 520 521 522 523 524

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

                return $appForms;
            };
        };
525

526 527
        $c['csv_export_appforms_items'] = function ($c) {

528
            return function () use ($c) {
529

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

                $appFormIdsSql = 'SELECT id FROM applicationform WHERE (submitted)IN (SELECT MAX( submitted )FROM applicationform '
534 535 536 537 538 539 540
                    . ' 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)';
541 542 543 544 545 546 547 548 549

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

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

                $in = implode(',', array_fill(0, count($appFormIds), '?'));
                $sql = 'SELECT applicationform.id AS id, '
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' 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 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 . ') ';
569

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

572
                return $appForms;
573 574
            };
        };
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

        $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, '
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
                    . ' school.registry_no AS school_registry_no, '
                    . ' school.name AS school_name, '
                    . ' 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 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 . ')';
616 617 618 619 620 621 622


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

                return $appForms;
            };
        };
623 624 625 626 627 628
    });

    $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'];
629 630 631 632 633 634 635 636 637 638

            $role = call_user_func($c['current_role']);
            if (in_array($type, $c['csv_export_protected_exports'])) {
                if ($role !== 'admin') {
                    $res = $res->withStatus(403);
                    $res->getBody()->write('Forbidden. The information requested in not available.');
                    return $res;
                }
            }

639 640
            try {
                $config = $c['csv_export_config'];
641

642 643 644 645 646 647
                if (!array_key_exists($type, $config)) {
                    return $res->withStatus(404);
                }

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

649 650 651 652 653 654 655 656
                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()]);
657

658 659
                return $res->withRedirect($router->pathFor('index'));
            }
660 661 662 663 664 665 666 667 668 669
            $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');
670 671 672
        })->setName('export.csv');
    });
};