ApplicationForm.php 2.47 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
<?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
 */

namespace GrEduLabs\ApplicationForm\Action;

use GrEduLabs\ApplicationForm\Service\ApplicationFormServiceInterface;
use GrEduLabs\Schools\Service\AssetServiceInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
use Zend\InputFilter\InputFilterInterface;

class ApplicationForm
{
    /**
     * @var Twig
     */
    protected $view;

    /**
     *
     * @var AssetServiceInterface
     */
    protected $assetsService;

    /**
     *
     * @var ApplicationFormServiceInterface
     */
    protected $appFormService;

    /**
     *
     * @var InputFilterInterface
     */
    protected $appFormInputFilter;


    public function __construct(
        Twig $view,
        AssetServiceInterface $assetsService,
        ApplicationFormServiceInterface $appFormService,
        InputFilterInterface $appFormInputFilter
    ) {
        $this->view               = $view;
        $this->assetsService      = $assetsService;
        $this->appFormService     = $appFormService;
        $this->appFormInputFilter = $appFormInputFilter;
    }

    public function __invoke(Request $req, Response $res)
    {
        $school = $req->getAttribute('school');
        if ($req->isPost()) {
            $this->appFormInputFilter->setData(array_merge($req->getParams(), [
                'school_id' => $school->id,
            ]));
            if ($this->appFormInputFilter->isValid()) {
                $data = $this->appFormInputFilter->getValues();
                $this->appFormService->submit($data);
            }
            var_dump($this->appFormInputFilter->getMessages());
            die('error');
            $this->view['messages'] = $this->appFormInputFilter->getMessages();
        }

        $res = $this->view->render($res, 'application_form/form.twig', [
            'type_choices' => array_map(function ($category) {
                return ['value' => $category['id'], 'label' => $category['name']];
            }, $this->assetsService->getAllItemCategories()),
            'apply_for_choices' => array_map(function ($choice) {
                return ['value' => $choice, 'label' => $choice];
            }, $this->appFormService->getApplyForChoices()),

        ]);

        return $res;
    }
}