bootstrap.php 14.7 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
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
<?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();
    $events    = $container['events'];

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

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

            return [
                'schools' => [
                    'data_callback' => 'csv_export_schools',
                    'headers'       => [
                        'Κωδικός μονάδας',
                        'Ονομασία',
                        'Τύπος μονάδας',
                        'Περιφερειακή ενότητα',
                        'Διεύθυνση εκπαίδευσης',
                        'Περιφερειακή διεύθυνση εκπαίδευσης',
                        'Βαθμίδα εκπαίδευσης',
                    ],
                ],
                'labs' => [
                    'data_callback' => 'csv_export_labs',
                    'headers'       => [
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'Ονομασία χώρου',
                        'Τύπος χώρου',
                        'Ειδικότητα υπευθύνου',
                        'Νέος χώρος',
                        'Επιφάνεια (m^2)',
                        'Σύνδεση στο δίκτυο',
                        'Διαθέτει server',
                        'Μαθήματα',
                        'Χρήση στα πλαίσια μαθημάτων',
                        'Χρήση για δραστηριότητες εκτός εκπαιδευτικού προγράμματος',
                    ],
                ],
                'assets' => [
                    'data_callback' => 'csv_export_assets',
                    'headers'       => [
                        'Είδος',
                        'Πλήθος ',
                        'Έτος κτήσης',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'Σχόλια - Παρατηρήσεις',
                    ],
                ],
                'appforms' => [
                    'data_callback' => 'csv_export_appforms',
                    'headers'       => [
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'Ημερομηνία υποβολής',
                        'Σχόλια - Παρατηρήσεις',
                    ],
                ],
                'appforms_items' => [
                    'data_callback' => 'csv_export_appforms_items',
                    'headers'       => [
                        'ID',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'Ημερομηνία υποβολής',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Νέος χώρος',
                        'Είδος',
                        'Πλήθος ',
                        'Αιτιολογία χρήσης',
                    ],
                ],
97
98
99
100
101
102
103
104
105
106
107
108
109
                'software' => [
                    'data_callback' => 'csv_export_software',
                    'headers'       => [
                        'Τύπος',
                        'Κωδικός σχολείου',
                        'Ονομασία σχολείου',
                        'ID χώρου',
                        'Τύπος χώρου',
                        'Ονομασία',
                        'Κατασκευαστής',
                        'URL',
                    ],
                ],
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
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
            ];
        };

        $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, '
                     . ' 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';
                $schools = R::getAll($sql);

                return $schools;
            };
        };

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

            return function () {
                $sql = '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 '
                     . ' GROUP BY lab.id '
                     . ' ORDER BY school_name ' ;

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

                return $labs;
            };
        };

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

            return function () {
                $sql = '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';

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

                return $assets;
            };
        };

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

            return function () {
                $sql = 'SELECT applicationform.id AS id, '
                     . ' school.registry_no AS school_registry_no, '
                     . ' school.name AS school_name, '
                     . ' FROM_UNIXTIME(applicationform.submitted) AS submitted, '
                     . ' TRIM(applicationform.comments) AS comments '
                     . ' FROM applicationform '
                     . ' LEFT JOIN school ON applicationform.school_id = school.id '
                     . ' GROUP BY school.id '
                     . ' HAVING MAX(applicationform.submitted)';

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

                return $appForms;
            };
        };

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

            return function () {
                $appFormIdsSql = 'SELECT applicationform.id '
                    . ' FROM applicationform '
                    . ' GROUP BY school_id '
                    . ' HAVING MAX(applicationform.submitted)';

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

                $in       = implode(',', array_fill(0, count($appFormIds), '?'));
                $sql = 'SELECT applicationform.id AS id, '
                     . ' school.registry_no AS school_registry_no, '
                     . ' school.name AS school_name, '
                     . ' 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 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 . ') ';

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

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

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

            return function () {
                $sql = '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 ';

                $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;
            };
        };
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
313
314
315
316
317
318
    });

    $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'];
319
320
            try {
                $config = $c['csv_export_config'];
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
321

322
323
324
325
326
327
                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
328

329
330
331
332
333
334
335
336
                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
337

338
339
                return $res->withRedirect($router->pathFor('index'));
            }
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
340
341
342
        })->setName('export.csv');
    });
};