Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
21.05% covered (danger)
21.05%
16 / 76
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzFaqDetailParams
n/a
0 / 0
n/a
0 / 0
0
n/a
0 / 0
OlzFaqDetail
21.05% covered (danger)
21.05%
16 / 76
40.00% covered (danger)
40.00%
2 / 5
70.54
0.00% covered (danger)
0.00%
0 / 1
 hasAccess
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 searchSqlWhenHasAccess
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 getPageTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPageDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHtmlWhenHasAccess
0.00% covered (danger)
0.00%
0 / 58
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 searchSqlWhenHasAccess(array $terms): string|array|null {
26        $code_href = $this->envUtils()->getCodeHref();
27        $where = implode(' AND ', array_map(function ($term) {
28            return <<<ZZZZZZZZZZ
29                (
30                    ident LIKE '%{$term}%'
31                    OR question LIKE '%{$term}%'
32                    OR answer LIKE '%{$term}%'
33                )
34                ZZZZZZZZZZ;
35        }, $terms));
36        return <<<ZZZZZZZZZZ
37            SELECT
38                CONCAT('{$code_href}fragen_und_antworten/', ident) AS link,
39                '{$code_href}assets/icns/question_mark_20.svg' AS icon,
40                NULL AS date,
41                CONCAT('Frage: ', question) AS title,
42                CONCAT(IFNULL(ident, ''), ' Antwort: ', IFNULL(answer, '')) AS text,
43                1.0 AS time_relevance
44            FROM questions
45            WHERE
46                on_off = '1'
47                AND {$where}
48            ZZZZZZZZZZ;
49    }
50
51    public function getPageTitle(): string {
52        return "";
53    }
54
55    public function getPageDescription(): string {
56        return "";
57    }
58
59    public function getHtmlWhenHasAccess(mixed $args): string {
60        $this->httpUtils()->validateGetParams(OlzFaqDetailParams::class);
61        $code_href = $this->envUtils()->getCodeHref();
62        $entityManager = $this->dbUtils()->getEntityManager();
63        $ident = $args['ident'] ?? null;
64
65        $question_repo = $entityManager->getRepository(Question::class);
66        $answered_question = $question_repo->findOneBy(['ident' => $ident]);
67        if (!$answered_question) {
68            $this->httpUtils()->dieWithHttpError(404);
69            throw new \Exception('should already have failed');
70        }
71        $is_active = $answered_question->getOnOff();
72        if (!$is_active && !$this->authUtils()->hasPermission('faq')) {
73            $this->httpUtils()->dieWithHttpError(404);
74            throw new \Exception('should already have failed');
75        }
76
77        $question = $answered_question->getQuestion();
78        $answer = $answered_question->getAnswer() ?? '';
79        $answer_html = $this->htmlUtils()->renderMarkdown($answer);
80        $answer_html = $answered_question->replaceImagePaths($answer_html);
81        $answer_html = $answered_question->replaceFilePaths($answer_html);
82
83        $description = strip_tags($answer_html);
84        $out = OlzHeader::render([
85            'back_link' => "{$code_href}fragen_und_antworten",
86            'title' => "{$question} - Fragen & Antworten",
87            'description' => $description,
88            'canonical_url' => "{$code_href}fragen_und_antworten/{$ident}",
89        ]);
90
91        $edit_admin = '';
92        $can_edit = $this->authUtils()->hasPermission('faq');
93        if ($can_edit) {
94            $id = $answered_question->getId();
95            $json_id = json_encode($id);
96            $edit_admin = <<<ZZZZZZZZZZ
97                <div>
98                    <button
99                        id='edit-question-button'
100                        class='btn btn-primary'
101                        onclick='return olz.editQuestion({$json_id})'
102                    >
103                        <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' />
104                        Bearbeiten
105                    </button>
106                </div>
107                ZZZZZZZZZZ;
108        }
109
110        $owner_role = $answered_question->getOwnerRole();
111        $role_repo = $entityManager->getRepository(Role::class);
112        $responsible_role = $owner_role ?? $role_repo->getPredefinedRole(PredefinedRole::Nachwuchs);
113        $responsible_title = 'Ansprechperson';
114        if ($owner_role) {
115            $responsible_title = OlzRoleInfoModal::render(['role' => $owner_role]);
116        }
117        $responsible_assignees = $responsible_role?->getUsers() ?? [];
118        $responsible_out = '';
119        foreach ($responsible_assignees as $responsible_assignee) {
120            $responsible_out .= OlzUserInfoModal::render([
121                'user' => $responsible_assignee,
122                'mode' => 'name_picture',
123            ]);
124        }
125
126        $out .= <<<ZZZZZZZZZZ
127            <div class='content-right optional'>
128                <h3>{$responsible_title}</h3>
129                <div style='padding:0px 10px 0px 10px; text-align:center;'>
130                    {$responsible_out}
131                </div>
132            </div>
133            <div class='content-middle'>
134                {$edit_admin}
135                <h1>{$question}</h1>
136                <div>{$answer_html}</div>
137            </div>
138            ZZZZZZZZZZ;
139
140        $out .= OlzFooter::render();
141        return $out;
142    }
143}