ApplicationForm.php 7.88 KB
Newer Older
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
1
<?php
2

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * 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;
16
use GrEduLabs\Schools\Service\LabServiceInterface;
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
17 18 19
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
20
use Zend\Authentication\AuthenticationServiceInterface;
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
21 22
use Zend\InputFilter\InputFilterInterface;

23 24
class ApplicationForm {

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
25 26 27 28 29 30 31 32 33 34 35
    /**
     * @var Twig
     */
    protected $view;

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

36 37 38 39 40 41
    /**
     *
     * @var LabServiceInterface
     */
    protected $labService;

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
42 43 44 45 46 47 48 49 50 51 52 53
    /**
     *
     * @var ApplicationFormServiceInterface
     */
    protected $appFormService;

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

54 55 56 57 58 59
    /**
     *
     * @var AuthenticationServiceInterface
     */
    protected $authService;

60 61 62 63 64 65 66 67 68 69 70 71
    /**
     *
     * @var int The version of the application form to handle
     */
    protected $version;

    /**
     *
     * @var type SLIM application container 
     */
    protected $container;

72 73 74 75 76
    /**
     *
     * @var string
     */
    protected $successUrl;
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
77 78

    public function __construct(
79
    Twig $view, AssetServiceInterface $assetsService, LabServiceInterface $labService, ApplicationFormServiceInterface $appFormService, InputFilterInterface $appFormInputFilter, AuthenticationServiceInterface $authService, $successUrl, $version, $container
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
80
    ) {
81 82 83 84
        $this->view = $view;
        $this->assetsService = $assetsService;
        $this->labService = $labService;
        $this->appFormService = $appFormService;
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
85
        $this->appFormInputFilter = $appFormInputFilter;
86 87 88 89
        $this->authService = $authService;
        $this->successUrl = $successUrl;
        $this->version = $version;
        $this->container = $container;
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
90 91
    }

92
    public function __invoke(Request $req, Response $res) {
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
93
        $school = $req->getAttribute('school');
94

Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
95
        if ($req->isPost()) {
96 97 98 99 100
            $reqParams = $req->getParams();
            array_splice($reqParams['items'], 0, 0);
            $this->appFormInputFilter->setData(array_merge($reqParams, [
                'school_id' => $school->id,
                'submitted_by' => $this->authService->getIdentity()->mail,
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
101
            ]));
102 103
            $isValid = $this->appFormInputFilter->isValid();
            if ($isValid) {
104 105
                $data = $this->appFormInputFilter->getValues();
                $appForm = $this->appFormService->submit($data);
106
                $_SESSION['applicationForm']['appForm'] = $appForm;
107
                $res = $res->withRedirect($this->successUrl);
108 109

                return $res;
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
110
            }
111 112

            $this->view['form'] = [
113 114
                'is_valid' => $isValid,
                'values' => $this->appFormInputFilter->getValues(),
115
                'raw_values' => $this->appFormInputFilter->getRawValues(),
116
                'messages' => $this->appFormInputFilter->getMessages(),
117
            ];
Vassilis Kanellopoulos's avatar
Vassilis Kanellopoulos committed
118 119
        }

120
        $loadForm = (bool) $req->getParam('load', false);
121 122
        $this->view['choose'] = !$loadForm && !$req->isPost();
        if (!$req->isPost() && $loadForm) {
123
            // take care of new options in applications and migrate existing ones
124
            if (null !== ($appForm = $this->appFormService->findSchoolApplicationForm($school->id))) {
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
                /**
                 * Do mapping of old items to new only if items do exist (old form) 
                 * and the map is available at the app settings.
                 * TODO: Only one version migrations are supported. If the old items are
                 * two or more versions older, they will not be handled.
                 */
                // get the existing (db) application form version
                $items_version = $this->version;
                if (isset($appForm['items']) && \count($appForm['items']) > 0) {
                    $items_version = array_values($appForm['items'])[0]['version'];
                }

                if ($this->version != $items_version &&
                        isset($appForm['items']) &&
                        isset($this->container['settings']['application_form']['itemcategory']['map']) &&
                        $this->container['settings']['application_form']['itemcategory']['map']['fromversion'] == $items_version &&
                        $this->container['settings']['application_form']['itemcategory']['map']['toversion'] == $this->version &&
                        isset($this->container['settings']['application_form']['itemcategory']['map']['items'])) {
                    // if map exists for this version, use it
                    $items_map = $this->container['settings']['application_form']['itemcategory']['map']['items'];
                    $appForm['items'] = array_map(function ($item) use ($items_map) {
                        $migrate_values = [];
                        if (isset($items_map[$item['itemcategory_id']]) &&
                                intval($items_map[$item['itemcategory_id']]) > 0) {
                            $migrate_values = [
                                'itemcategory_prev' => $item['itemcategory_id'],
                                'itemcategory_id_prev' => $item['itemcategory_id'],
                                'itemcategory_id' => intval($items_map[$item['itemcategory_id']]),
                            ];
                        } else {
                            $migrate_values = [
                                'itemcategory_prev' => '',
                                'itemcategory_id_prev' => -1,
                            ];
                        }
                        $migrate_values['prev_form_load'] = true;
                        return array_merge($item, $migrate_values);
                    }, $appForm['items']);
                } elseif ($this->version != $items_version &&
                        isset($appForm['items']) &&
                        isset($this->container['settings']['application_form']['itemcategory']['map']) &&
                        ($this->container['settings']['application_form']['itemcategory']['map']['fromversion'] != $items_version ||
                        $this->container['settings']['application_form']['itemcategory']['map']['toversion'] != $this->version)) {
                    // if map does not exist for this version, notify user
                    $items_map = $this->container['settings']['application_form']['itemcategory']['map']['items'];
                    $appForm['items'] = array_map(function ($item) use ($items_map) {
                        $migrate_values = [
                            'itemcategory_prev' => '',
                            'itemcategory_id_prev' => -2,
                            'prev_form_load' => true
                        ];
                        return array_merge($item, $migrate_values);
                    }, $appForm['items']);
                }

180 181 182 183 184
                $this->view['form'] = [
                    'values' => $appForm,
                ];
            }
        }
185
        $labs = $this->labService->getLabsBySchoolId($school->id);
186
        $res = $this->view->render($res, 'application_form/form.twig', [
187
            'lab_choices' => array_map(function ($lab) {
188 189 190 191 192 193 194 195 196 197 198 199
                        return ['value' => $lab['id'], 'label' => $lab['name']];
                    }, $labs),
                    'type_choices' => array_map(function ($category) {
                        return ['value' => $category['id'], 'label' => $category['name']];
                    }, $this->assetsService->getAllItemCategories($this->version)),
                ]);

                return $res;
            }

        }