Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 159
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
OlzRoleDetailParams
n/a
0 / 0
n/a
0 / 0
0
n/a
0 / 0
OlzRoleDetail
0.00% covered (danger)
0.00%
0 / 159
0.00% covered (danger)
0.00%
0 / 5
600
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 / 17
0.00% covered (danger)
0.00%
0 / 1
2
 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 / 139
0.00% covered (danger)
0.00%
0 / 1
420
1<?php
2
3namespace Olz\Roles\Components\OlzRoleDetail;
4
5use Olz\Common\Components\OlzFooter\OlzFooter;
6use Olz\Common\Components\OlzHeader\OlzHeader;
7use Olz\Common\Components\OlzRootComponent;
8use Olz\Common\Utils\HttpParams;
9use Olz\Entity\Roles\Role;
10use Olz\Users\Components\OlzUserInfoModal\OlzUserInfoModal;
11
12/** @extends HttpParams<array{von?: ?string}> */
13class OlzRoleDetailParams extends HttpParams {
14}
15
16/** @extends OlzRootComponent<array<string, mixed>> */
17class OlzRoleDetail extends OlzRootComponent {
18    public function hasAccess(): bool {
19        return true;
20    }
21
22    public function searchSqlWhenHasAccess(array $terms): string|array|null {
23        $code_href = $this->envUtils()->getCodeHref();
24        $where = implode(' AND ', array_map(function ($term) {
25            return <<<ZZZZZZZZZZ
26                (
27                    r.username LIKE '%{$term}%'
28                    OR r.old_username LIKE '%{$term}%'
29                    OR r.name LIKE '%{$term}%'
30                    OR r.description LIKE '%{$term}%'
31                    OR EXISTS(
32                        SELECT id
33                        FROM users u
34                            JOIN users_roles ur ON (ur.role_id = r.id AND ur.user_id = u.id)
35                        WHERE u.first_name LIKE '%{$term}%' OR u.last_name LIKE '%{$term}%'
36                    )
37                )
38                ZZZZZZZZZZ;
39        }, $terms));
40        return <<<ZZZZZZZZZZ
41            SELECT
42                CONCAT('{$code_href}verein/', r.username) AS link,
43                '{$code_href}assets/icns/link_role_16.svg' AS icon,
44                NULL AS date,
45                CONCAT('Ressort: ', IF(rpp.name IS NULL, '', CONCAT(rpp.name, ' &gt; ')), IF(rp.name IS NULL, '', CONCAT(rp.name, ' &gt; ')), r.name) AS title,
46                CONCAT(IFNULL(r.username, ''), ' ', IFNULL(r.old_username, ''), ' ', IFNULL(r.description, ''), ' Verantwortlich: ', (
47                    SELECT GROUP_CONCAT(CONCAT(u.first_name, ' ', u.last_name) SEPARATOR ', ')
48                    FROM
49                        users_roles ur
50                        JOIN users u ON (u.id = ur.user_id)
51                    WHERE ur.role_id = r.id
52                    GROUP BY r.id
53                )) AS text,
54                1.0 AS time_relevance
55            FROM roles r
56                LEFT JOIN roles rp ON (rp.id = r.parent_role)
57                LEFT JOIN roles rpp ON (rpp.id = rp.parent_role)
58            WHERE
59                r.deleted_at IS NULL
60                AND {$where}
61            ZZZZZZZZZZ;
62    }
63
64    public function getPageTitle(): string {
65        return "";
66    }
67
68    public function getPageDescription(): string {
69        return "";
70    }
71
72    public function getHtmlWhenHasAccess(mixed $args): string {
73        $this->httpUtils()->validateGetParams(OlzRoleDetailParams::class);
74        $is_member = $this->authUtils()->hasPermission('roles')
75            || ($this->authUtils()->getCurrentUser()?->isMember() ?? false);
76        $entityManager = $this->dbUtils()->getEntityManager();
77        $code_href = $this->envUtils()->getCodeHref();
78        $role_username = $args['ressort'];
79        $role_repo = $entityManager->getRepository(Role::class);
80        $role = $role_repo->findOneBy(['username' => $role_username, 'deleted_at' => null]);
81        if (!$role) {
82            $old_role = $role_repo->findOneBy(['old_username' => $role_username, 'deleted_at' => null]);
83            if ($old_role) {
84                $redirect_url = "{$code_href}verein/{$old_role->getUsername()}";
85                $this->httpUtils()->redirect($redirect_url, ['status' => 308]);
86                throw new \Exception('should already have terminated');
87            }
88            $this->httpUtils()->dieWithHttpError(404);
89            throw new \Exception('should already have terminated');
90        }
91
92        // TODO: Remove again, after all ressort descriptions have been updated.
93        // This is just temporary logic!
94        $no_robots = ($role->getGuide() === '');
95
96        $role_description = $role->getDescription();
97        $end_of_first_line = strpos($role_description, "\n");
98        $first_line = $end_of_first_line
99            ? substr($role_description, 0, $end_of_first_line)
100            : $role_description;
101        $description_html = $this->htmlUtils()->renderMarkdown($first_line);
102        $role_short_description = strip_tags($description_html);
103
104        $role_id = $role->getId();
105        $role_name = $role->getName();
106        $role_description = $role->getDescription();
107        $parent_role_id = $role->getParentRoleId();
108        $parent_role = $role_repo->findOneBy(['id' => $parent_role_id]);
109        $can_have_child_roles = $role->getCanHaveChildRoles();
110
111        $parent_chain = [];
112        $parent = $role;
113        while ($parent) {
114            $parent_id = $parent->getParentRoleId();
115            if ($parent_id) {
116                $parent = $role_repo->findOneBy(['id' => $parent_id]);
117                array_unshift($parent_chain, $parent);
118            } else {
119                $parent = null;
120            }
121        }
122
123        $out = OlzHeader::render([
124            'back_link' => "{$code_href}verein",
125            'title' => $role_name,
126            'description' => "{$role_short_description} - Ressort {$role_name} der OL Zimmerberg.",
127            'norobots' => $no_robots,
128            'canonical_url' => "{$code_href}verein/{$role_username}",
129        ]);
130
131        $out .= "<div class='content-full olz-role-page'>";
132        $out .= "<nav aria-label='breadcrumb'>";
133        $out .= "<ol class='breadcrumb'>";
134        $out .= "<li class='breadcrumb-item'><a href='{$code_href}verein'>OL Zimmerberg</a></li>";
135        foreach ($parent_chain as $breadcrumb) {
136            $username = $breadcrumb?->getUsername();
137            $name = $breadcrumb?->getName();
138            $out .= "<li class='breadcrumb-item'><a href='{$code_href}verein/{$username}'>{$name}</a></li>";
139        }
140        $out .= "<li class='breadcrumb-item active' aria-current='page'>{$role_name}</li>";
141        $out .= "</ol>";
142        $out .= "</nav>";
143
144        $edit_admin = '';
145        $add_membership_admin = '';
146        $add_child_role_admin = '';
147        $is_parent_superior = $this->authUtils()->hasRoleEditPermission($parent_role_id);
148        $is_parent_owner = $parent_role && $this->entityUtils()->canUpdateOlzEntity($parent_role, null, 'roles');
149        $can_parent_edit = $is_parent_superior || $is_parent_owner;
150        $is_superior = $this->authUtils()->hasRoleEditPermission($role_id);
151        $is_owner = $this->entityUtils()->canUpdateOlzEntity($role, null, 'roles');
152        $can_edit = $is_superior || $is_owner;
153        if ($can_edit) {
154            $json_id = json_encode($role_id);
155            $json_can_parent_edit = json_encode(boolval($can_parent_edit));
156            $edit_admin = <<<ZZZZZZZZZZ
157                <div>
158                    <button
159                        id='edit-role-button'
160                        class='btn btn-auth'
161                        onclick='return olz.editRole({$json_id}, {$json_can_parent_edit})'
162                    >
163                        <img src='{$code_href}assets/icns/edit_16.svg' class='noborder' />
164                        Bearbeiten
165                    </button>
166                </div>
167                ZZZZZZZZZZ;
168            $add_membership_admin = <<<ZZZZZZZZZZ
169                <div>
170                    <button
171                        id='add-role-user-button'
172                        class='btn btn-sm btn-auth'
173                        onclick='return olz.addRoleUser({$json_id})'
174                    >
175                        <img src='{$code_href}assets/icns/new_16.svg' class='noborder' />
176                        Neuer Verantwortlicher
177                    </button>
178                </div>
179                ZZZZZZZZZZ;
180            if ($can_have_child_roles) {
181                $add_child_role_admin = <<<ZZZZZZZZZZ
182                    <div>
183                        <button
184                            id='add-sub-role-button'
185                            class='btn btn-sm btn-auth'
186                            onclick='return olz.addChildRole({$json_id})'
187                        >
188                            <img src='{$code_href}assets/icns/new_16.svg' class='noborder' />
189                            Neues Unter-Ressort
190                        </button>
191                    </div>
192                    ZZZZZZZZZZ;
193            }
194        }
195
196        $out .= "<div>{$edit_admin}</div>";
197        $description_html = $this->htmlUtils()->renderMarkdown($role->getDescription());
198        $description_html = $role->replaceImagePaths($description_html);
199        $description_html = $role->replaceFilePaths($description_html);
200        $out .= $description_html;
201
202        $assignees = $role->getUsers();
203        $num_assignees = count($assignees);
204        $out .= "<br/><h2>Verantwortlich</h2>";
205        if ($num_assignees === 0) {
206            $out .= "<p><i>Keine Ressort-Verantwortlichen</i></p>";
207            $out .= $add_membership_admin;
208        } else {
209            $out .= "<div class='role-assignees'>";
210            foreach ($assignees as $assignee) {
211                $out .= "<div class='assignee'>";
212                if ($is_superior || $is_owner) {
213                    $json_role_id = json_encode(intval($role_id));
214                    $json_user_id = json_encode(intval($assignee->getId()));
215                    $out .= <<<ZZZZZZZZZZ
216                            <button
217                                id='delete-role-user-button'
218                                class='btn btn-sm btn-auth'
219                                onclick='return olz.deleteRoleUser({$json_role_id}, {$json_user_id})'
220                            >
221                                <img src='{$code_href}assets/icns/delete_16.svg' class='noborder' />
222                            </button>
223                        ZZZZZZZZZZ;
224                }
225                $out .= OlzUserInfoModal::render([
226                    'user' => $assignee,
227                    'mode' => 'name_picture',
228                ]);
229                $out .= "</div>";
230            }
231            $out .= $add_membership_admin;
232            $out .= "</div>";
233        }
234
235        $child_roles = $role_repo->findBy([
236            'parent_role' => $role_id,
237            'deleted_at' => null,
238        ], ['position_within_parent' => 'ASC']);
239        $num_child_roles = count($child_roles);
240        $out .= "<br/><h2>Unter-Ressorts</h2>";
241        if ($num_child_roles === 0) {
242            $out .= "<p id='sub-roles'><i>Keine Unter-Ressorts</i></p>";
243        } else {
244            $out .= "<ul id='sub-roles' class='no-style'>";
245            foreach ($child_roles as $child_role) {
246                $child_role_name = $child_role->getName();
247                $child_role_username = $child_role->getUsername();
248                $out .= "<li><a href='{$code_href}verein/{$child_role_username}' class='linkint'><b>{$child_role_name}</b></a></li>";
249            }
250            $out .= "</ul>";
251            $out .= $add_child_role_admin;
252        }
253
254        if ($is_member) {
255            $guide_html = $this->htmlUtils()->renderMarkdown($role->getGuide());
256            $guide_html = $role->replaceImagePaths($guide_html);
257            $guide_html = $role->replaceFilePaths($guide_html);
258            $out .= <<<ZZZZZZZZZZ
259                <br/>
260                <br/>
261                <div class='auth-section'>
262                    <h2>Aufgaben (nur für OLZ-Mitglieder sichtbar)</h2>
263                    {$guide_html}
264                </div>
265                ZZZZZZZZZZ;
266        }
267
268        $out .= "</div>";
269        $out .= OlzFooter::render();
270
271        return $out;
272    }
273}