bootstrap.php 5.44 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * 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
 */
10 11 12
use Slim\Http\Request;
use Slim\Http\Response;
use RedBeanPHP\R;
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

return function (Slim\App $app) {
    $container = $app->getContainer();
    $events    = $container['events'];

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

    $events('on', 'app.services', function ($container) {
        $container[GrEduLabs\ReceiveEquip\Service\ReceiveEquipServiceInterface::class] = function ($c) {
            return new GrEduLabs\ReceiveEquip\Service\ReceiveEquipService($c['logger']);
        };

        $container[GrEduLabs\ReceiveEquip\InputFilter\ReceiveEquip::class] = function ($c) {
            return new GrEduLabs\ReceiveEquip\InputFilter\ReceiveEquip(
                $c->get(GrEduLabs\ReceiveEquip\InputFilter\ReceiveEquipItemCollection::class)
            );
        };

        $container[GrEduLabs\ReceiveEquip\InputFilter\ReceiveEquipItem::class] = function ($c) {
            return new GrEduLabs\ReceiveEquip\InputFilter\ReceiveEquipItem(
            );
        };

        $container[GrEduLabs\ReceiveEquip\InputFilter\ReceiveEquipItemCollection::class] = function ($c) {
            return new GrEduLabs\ReceiveEquip\InputFilter\ReceiveEquipItemCollection(
                $c->get(GrEduLabs\ReceiveEquip\InputFilter\ReceiveEquipItem::class)
            );
        };

        $container[GrEduLabs\ReceiveEquip\Action\ReceiveEquip::class] = function ($c) {
            $settings = $c->get('settings');
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
            return new GrEduLabs\ReceiveEquip\Action\ReceiveEquip(
                $c->get('view'),
                $c->get(GrEduLabs\ReceiveEquip\Service\ReceiveEquipServiceInterface::class),
                $c->get(GrEduLabs\ReceiveEquip\InputFilter\ReceiveEquip::class),
                $c->get('authentication_service'),
                $c->get('router')->pathFor('receive_equip.submit_success'),
                $c
            );
        };

        $container[GrEduLabs\ReceiveEquip\Action\SubmitSuccess::class] = function ($c) {
            return new GrEduLabs\ReceiveEquip\Action\SubmitSuccess(
                $c->get('view'),
                $c->get('router')->pathFor('receive_equip')
            );
        };

        $container[GrEduLabs\ReceiveEquip\Action\ReceiveEquipPdf::class] = function ($c) {
            return new GrEduLabs\ReceiveEquip\Action\ReceiveEquipPdf(
                $c->get(GrEduLabs\ReceiveEquip\Service\ReceiveEquipServiceInterface::class),
                $c->get('view')
            );
        };

        $container[GrEduLabs\ReceiveEquip\Acl\Assertion\CanSubmit::class] = function ($c) {
            return new GrEduLabs\ReceiveEquip\Acl\Assertion\CanSubmit(
                $c->get('authentication_service'),
                $c->get(GrEduLabs\ReceiveEquip\Service\ReceiveEquipServiceInterface::class)
            );
        };
77
    });
78

79 80 81
    $events('on', 'app.services', function ($container) {
        $container[GrEduLabs\ReceiveEquip\Middleware\HandleEmptyPosts::class] = function ($c) {
            return new GrEduLabs\ReceiveEquip\Middleware\HandleEmptyPosts(
82 83
                $c->get('view'),
                $c->get(GrEduLabs\ReceiveEquip\Service\ReceiveEquipServiceInterface::class),
84
                $c['flash'],
85 86 87
                $c
            );
        };
88
    }, -100000);
89 90 91 92 93 94 95 96

    $events('on', 'app.bootstrap', function ($app, $container) {
        $container['view']->getEnvironment()->getLoader()->prependPath(__DIR__ . '/templates');

        $app->group('/receive-equip', function () {
            $this->map(['get', 'post'], '', GrEduLabs\ReceiveEquip\Action\ReceiveEquip::class)
                ->add(GrEduLabs\Application\Middleware\AddCsrfToView::class)
                ->add('csrf')
97
                ->add(GrEduLabs\ReceiveEquip\Middleware\HandleEmptyPosts::class)
98 99 100 101 102 103
                ->setName('receive_equip');
            $this->get('/submit-success', GrEduLabs\ReceiveEquip\Action\SubmitSuccess::class)
                ->setName('receive_equip.submit_success');
            $this->get('/report', GrEduLabs\ReceiveEquip\Action\ReceiveEquipPdf::class)
                ->setName('receive_equip.report');
        })->add(GrEduLabs\Schools\Middleware\FetchSchoolFromIdentity::class);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

        $app->get('/receive-equip/receive-doc/{fn}', function (Request $req, Response $res) use ($container) {
            $route = $req->getAttribute('route');
            $fn = $route->getArgument('fn');
/*            $container["logger"]->info(sprintf('filename = %s  url=%s', $fn, path_for('receive_equip.receive_doc', [
    'fn' => form.values.received_document,])
)); */

            $file = $container['settings']['receive_equip']['file_upload_path'] . "/" . $fn;
            $response = $res->withHeader('Content-Description', 'File Transfer')
                ->withHeader('Content-Type', 'application/octet-stream')
                ->withHeader('Content-Disposition', 'attachment;filename="' . basename($file) . '"')
                ->withHeader('Expires', '0')
                ->withHeader('Cache-Control', 'must-revalidate')
                ->withHeader('Content-Length', filesize($file));

            readfile($file);

            return $response;
        })->setName('receive_equip.receive_doc');

125 126
    });
};