Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 3
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 / 74
0.00% covered (danger)
0.00%
0 / 3
90
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
 searchSqlWhenHasAccess
0.00% covered (danger)
0.00%
0 / 15
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 getHtmlWhenHasAccess(mixed $args): string {
52        $this->httpUtils()->validateGetParams(OlzFaqDetailParams::class);
53        $code_href = $this->envUtils()->getCodeHref();
54        $entityManager = $this->dbUtils()->getEntityManager();
55        $ident = $args['ident'] ?? null;
56
57        $question_repo = $entityManager->getRepository(Question::class);
58        $answered_question = $question_repo->findOneBy(['ident' => $ident]);
59        if (!$answered_question) {
60            $this->httpUtils()->dieWithHttpError(404);
61            throw new \Exception('should already have failed');
62        }
63        $is_active = $answered_question->getOnOff();
64        if (!$is_active && !$this->authUtils()->hasPermission('faq')) {
65            $this->httpUtils()->dieWithHttpError(404);
66            throw new \Exception('should already have failed');
67        }
68
69        $question = $answered_question->getQuestion();
70        $answer = $answered_question->getAnswer() ?? '';
71        $answer_html = $this->htmlUtils()->renderMarkdown($answer);
72        $answer_html = $answered_question->replaceImagePaths($answer_html);
73        $answer_html = $answered_question->replaceFilePaths($answer_html);
74
75        $description = strip_tags($answer_html);
76        $out = OlzHeader::render([
77            'back_link' => "{$code_href}fragen_und_antworten",
78            'title' => "{$question} - Fragen & Antworten",
79            'description' => $description,
80            'canonical_url' => "{$code_href}fragen_und_antworten/{$ident}",
81        ]);
82
83        $edit_admin = '';
84        $can_edit = $this->authUtils()->hasPermission('faq');
85        if ($can_edit) {
86            $id = $answered_question->getId();
87            $json_id = json_encode($id);
88            $edit_admin = <<<ZZZZZZZZZZ
89                <div>
90                    <button
91                        id='edit-question-button'
92                        class='btn btn-primary'
93                        onclick='return olz.editQuestion({$json_id})'
94                    >
95                        <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' />
96                        Bearbeiten
97                    </button>
98                </div>
99                ZZZZZZZZZZ;
100        }
101
102        $owner_role = $answered_question->getOwnerRole();
103        $role_repo = $entityManager->getRepository(Role::class);
104        $responsible_role = $owner_role ?? $role_repo->getPredefinedRole(PredefinedRole::Nachwuchs);
105        $responsible_title = 'Ansprechperson';
106        if ($owner_role) {
107            $responsible_title = OlzRoleInfoModal::render(['role' => $owner_role]);
108        }
109        $responsible_assignees = $responsible_role?->getUsers() ?? [];
110        $responsible_out = '';
111        foreach ($responsible_assignees as $responsible_assignee) {
112            $responsible_out .= OlzUserInfoModal::render([
113                'user' => $responsible_assignee,
114                'mode' => 'name_picture',
115            ]);
116        }
117
118        $out .= <<<ZZZZZZZZZZ
119            <div class='content-right optional'>
120                <h3>{$responsible_title}</h3>
121                <div style='padding:0px 10px 0px 10px; text-align:center;'>
122                    {$responsible_out}
123                </div>
124            </div>
125            <div class='content-middle'>
126                {$edit_admin}
127                <h1>{$question}</h1>
128                <div>{$answer_html}</div>
129            </div>
130            ZZZZZZZZZZ;
131
132        $out .= OlzFooter::render();
133        return $out;
134    }
135}