SchoolApplicationForm.php 4.27 KB
Newer Older
1 2 3 4
<?php

/**
 * gredu_labs.
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * @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\Middleware;

use GrEduLabs\ApplicationForm\Service\ApplicationFormServiceInterface;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;


class SchoolApplicationForm
{
    /**
     *
     * @var Twig
     */
    protected $view;
27
    protected $container;
28 29 30 31 32 33 34

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

35
    public function __construct(Twig $view, ApplicationFormServiceInterface $appFormService, $container)
36 37 38
    {
        $this->view           = $view;
        $this->appFormService = $appFormService;
39
        $this->container = $container;
40 41 42 43 44 45 46 47
    }

    public function __invoke(Request $req, Response $res, callable $next)
    {
        $school = $req->getAttribute('school');

        $appForm = $this->appFormService->findSchoolApplicationForm($school->id);
        if ($appForm) {
48 49 50 51 52 53 54 55 56
            $settings = $this->container->get('settings');
            $currentVersion = $settings['application_form']['itemcategory']['currentversion'];
            // get the existing (db) application form version
            $items_version = $currentVersion;
            if (isset($appForm['items']) && \count($appForm['items']) > 0) {
                $items_version = array_values($appForm['items'])[0]['version'];
            }

            $appForm['items'] = array_reduce($appForm['items'], function ($aggr, $item) use ($currentVersion, $items_version) {
57 58 59
                $category = $item['itemcategory_id'];
                if (!isset($aggr[$category])) {
                    $aggr[$category] = [
60 61 62 63
                        'category'      => $item['itemcategory'],
                        'count'         => 0,
                        'countAcquired' => 0,
                        'available'     => 'LATEST'
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

                    /**
                     * 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.
                     */
                    if ($currentVersion != $items_version &&
                        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'] == $currentVersion &&
                        isset($this->container['settings']['application_form']['itemcategory']['map']['items'])) {

                        if (isset($this->container['settings']['application_form']['itemcategory']['map']['items'][$item['itemcategory_id']]) &&
                            intval($this->container['settings']['application_form']['itemcategory']['map']['items'][$item['itemcategory_id']]) > 0) {
                            $aggr[$category]['available'] = "MIGRATE";
                        } else {
                            $aggr[$category]['available'] = "UNAVAILABLE";
                        }
                    } elseif ($currentVersion != $items_version &&
                        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'] != $currentVersion)) {
                        $aggr[$category]['available'] = "UNAVAILABLE";
                    }
90 91
                }
                $aggr[$category]['count'] += $item['qty'];
92
                $aggr[$category]['countAcquired'] += $item['qtyacquired'];
93 94 95 96 97 98 99 100 101 102

                return $aggr;
            }, []);
        }

        $this->view['appForm'] = $appForm;

        return $next($req, $res);
    }
}