Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzFaqDetailParams
n/a
0 / 0
n/a
0 / 0
0
n/a
0 / 0
OlzFaqDetail
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 hasAccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchResults
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 getHtmlWhenHasAccess
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace Olz\Faq\Components\OlzFaqDetail;
4
5use Olz\Components\Common\OlzRootComponent;
6use Olz\Components\Page\OlzFooter\OlzFooter;
7use Olz\Components\Page\OlzHeader\OlzHeader;
8use Olz\Entity\Faq\Question;
9use Olz\Entity\Roles\Role;
10use Olz\Repository\Roles\PredefinedRole;
11use Olz\Roles\Components\OlzRoleInfoModal\OlzRoleInfoModal;
12use Olz\Users\Components\OlzUserInfoModal\OlzUserInfoModal;
13use Olz\Utils\HttpParams;
14
15/** @extends HttpParams<array{von?: ?string}> */
16class OlzFaqDetailParams extends HttpParams {
17}
18
19/** @extends OlzRootComponent<array<string, mixed>> */
20class OlzFaqDetail extends OlzRootComponent {
21    public function hasAccess(): bool {
22        return true;
23    }
24
25    public function getSearchTitle(): string {
26        return 'Fragen & Antworten';
27    }
28
29    public function getSearchResults(array $terms): array {
30        $results = [];
31        $code_href = $this->envUtils()->getCodeHref();
32        $question_repo = $this->entityManager()->getRepository(Question::class);
33        $questions = $question_repo->search($terms);
34        foreach ($questions as $question) {
35            $ident = $question->getIdent();
36            $results[] = $this->searchUtils()->getScoredSearchResult([
37                'link' => "{$code_href}fragen_und_antworten/{$ident}",
38                'icon' => "{$code_href}assets/icns/question_mark_20.svg",
39                'date' => null,
40                'title' => $question->getQuestion() ?: '?',
41                'text' => strip_tags("{$question->getIdent()} {$question->getAnswer()}") ?: null,
42            ], $terms);
43        }
44        return $results;
45    }
46
47    public function getHtmlWhenHasAccess(mixed $args): string {
48        $this->httpUtils()->validateGetParams(OlzFaqDetailParams::class);
49        $code_href = $this->envUtils()->getCodeHref();
50        $entityManager = $this->dbUtils()->getEntityManager();
51        $ident = $args['ident'] ?? null;
52
53        $question_repo = $entityManager->getRepository(Question::class);
54        $answered_question = $question_repo->findOneBy(['ident' => $ident]);
55        if (!$answered_question) {
56            $this->httpUtils()->dieWithHttpError(404);
57            throw new \Exception('should already have failed');
58        }
59        $is_active = $answered_question->getOnOff();
60        if (!$is_active && !$this->authUtils()->hasPermission('faq')) {
61            $this->httpUtils()->dieWithHttpError(404);
62            throw new \Exception('should already have failed');
63        }
64
65        $question = $answered_question->getQuestion();
66        $out = OlzHeader::render([
67            'back_link' => "{$code_href}fragen_und_antworten",
68            'title' => "{$question} - Fragen & Antworten",
69            'description' => "Antworten auf die wichtigsten Fragen rund um den OL, die OL Zimmerberg und diese Website.",
70            'canonical_url' => "{$code_href}fragen_und_antworten/{$ident}",
71        ]);
72
73        $answer = $answered_question->getAnswer() ?? '';
74        $answer_html = $this->htmlUtils()->renderMarkdown($answer);
75        $answer_html = $answered_question->replaceImagePaths($answer_html);
76        $answer_html = $answered_question->replaceFilePaths($answer_html);
77
78        $edit_admin = '';
79        $can_edit = $this->authUtils()->hasPermission('faq');
80        if ($can_edit) {
81            $id = $answered_question->getId();
82            $json_id = json_encode($id);
83            $edit_admin = <<<ZZZZZZZZZZ
84                <div>
85                    <button
86                        id='edit-question-button'
87                        class='btn btn-primary'
88                        onclick='return olz.editQuestion({$json_id})'
89                    >
90                        <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' />
91                        Bearbeiten
92                    </button>
93                </div>
94                ZZZZZZZZZZ;
95        }
96
97        $owner_role = $answered_question->getOwnerRole();
98        $role_repo = $entityManager->getRepository(Role::class);
99        $responsible_role = $owner_role ?? $role_repo->getPredefinedRole(PredefinedRole::Nachwuchs);
100        $responsible_title = 'Ansprechperson';
101        if ($owner_role) {
102            $responsible_title = OlzRoleInfoModal::render(['role' => $owner_role]);
103        }
104        $responsible_assignees = $responsible_role?->getUsers() ?? [];
105        $responsible_out = '';
106        foreach ($responsible_assignees as $responsible_assignee) {
107            $responsible_out .= OlzUserInfoModal::render([
108                'user' => $responsible_assignee,
109                'mode' => 'name_picture',
110            ]);
111        }
112
113        $out .= <<<ZZZZZZZZZZ
114            <div class='content-right optional'>
115                <h3>{$responsible_title}</h3>
116                <div style='padding:0px 10px 0px 10px; text-align:center;'>
117                    {$responsible_out}
118                </div>
119            </div>
120            <div class='content-middle'>
121                {$edit_admin}
122                <h1>{$question}</h1>
123                <div>{$answer_html}</div>
124            </div>
125            ZZZZZZZZZZ;
126
127        $out .= OlzFooter::render();
128        return $out;
129    }
130}