aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Institutes/InstitutesIndex.php
blob: d31cbe8866cb961a450f2dcaebf8d6c862172e60 (plain)
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
<?php

namespace JsonApi\Routes\Institutes;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use JsonApi\JsonApiController;
use JsonApi\Schemas\Institute as InstituteSchema;

class InstitutesIndex extends JsonApiController
{
    protected $allowedIncludePaths = [
        InstituteSchema::REL_FACULTY,
        InstituteSchema::REL_STATUS_GROUPS,
        InstituteSchema::REL_SUB_INSTITUTES,
        InstituteSchema::REL_COURSES_OF_STUDY,
    ];

    protected $allowedFilteringParameters = ['is-faculty'];

    protected $allowedPagingParameters = ['offset', 'limit'];

    public function __invoke(Request $request, Response $response, $args)
    {
        [$offset, $limit] = $this->getOffsetAndLimit();

        $filters = $this->getFilters();

        if (!isset($filters['is-faculty'])) {
            $condition = '1';
        } elseif ($filters['is-faculty']) {
            $condition = 'fakultaets_id = Institut_id';
        } else {
            $condition = 'fakultaets_id != Institut_id';
        }

        $institutes = \Institute::findBySql("{$condition} ORDER BY Name LIMIT ? OFFSET ?", [$limit, $offset]);
        $total = \Institute::countBySql($condition);

        return $this->getPaginatedContentResponse($institutes, $total);
    }

    private function getFilters()
    {
        $filtering = $this->getQueryParameters()->getFilteringParameters() ?? [];

        $filters = [];

        if (isset($filtering['is-faculty'])) {
            $filters['is-faculty'] = (bool) $filtering['is-faculty'];
        }

        return $filters;
    }
}