Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 132 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
OlzRolePageParams | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | n/a |
0 / 0 |
|||
OlzRolePage | |
0.00% |
0 / 132 |
|
0.00% |
0 / 1 |
342 | |
0.00% |
0 / 1 |
getHtml | |
0.00% |
0 / 132 |
|
0.00% |
0 / 1 |
342 |
1 | <?php |
2 | |
3 | namespace Olz\Roles\Components\OlzRolePage; |
4 | |
5 | use Olz\Components\Common\OlzComponent; |
6 | use Olz\Components\Page\OlzFooter\OlzFooter; |
7 | use Olz\Components\Page\OlzHeader\OlzHeader; |
8 | use Olz\Entity\Roles\Role; |
9 | use Olz\Users\Components\OlzUserInfoModal\OlzUserInfoModal; |
10 | use Olz\Utils\HttpParams; |
11 | |
12 | /** @extends HttpParams<array{von?: ?string}> */ |
13 | class OlzRolePageParams extends HttpParams { |
14 | } |
15 | |
16 | /** @extends OlzComponent<array<string, mixed>> */ |
17 | class OlzRolePage extends OlzComponent { |
18 | public function getHtml(mixed $args): string { |
19 | $this->httpUtils()->validateGetParams(OlzRolePageParams::class); |
20 | $is_member = $this->authUtils()->hasPermission('member'); |
21 | $entityManager = $this->dbUtils()->getEntityManager(); |
22 | $code_href = $this->envUtils()->getCodeHref(); |
23 | $role_repo = $entityManager->getRepository(Role::class); |
24 | $role_username = $args['ressort']; |
25 | $role_repo = $entityManager->getRepository(Role::class); |
26 | $role = $role_repo->findOneBy(['username' => $role_username, 'on_off' => 1]); |
27 | |
28 | if (!$role) { |
29 | $this->httpUtils()->dieWithHttpError(404); |
30 | throw new \Exception('should already have failed'); |
31 | } |
32 | |
33 | // TODO: Remove again, after all ressort descriptions have been updated. |
34 | // This is just temporary logic! |
35 | $no_robots = ($role->getGuide() === ''); |
36 | |
37 | $role_description = $role->getDescription(); |
38 | $end_of_first_line = strpos($role_description, "\n"); |
39 | $first_line = $end_of_first_line |
40 | ? substr($role_description, 0, $end_of_first_line) |
41 | : $role_description; |
42 | $description_html = $this->htmlUtils()->renderMarkdown($first_line); |
43 | $role_short_description = strip_tags($description_html); |
44 | |
45 | $role_id = $role->getId(); |
46 | $role_name = $role->getName(); |
47 | $role_description = $role->getDescription(); |
48 | $parent_role_id = $role->getParentRoleId(); |
49 | $parent_role = $role_repo->findOneBy(['id' => $parent_role_id]); |
50 | $can_have_child_roles = $role->getCanHaveChildRoles(); |
51 | |
52 | $parent_chain = []; |
53 | $parent = $role; |
54 | while ($parent) { |
55 | $parent_id = $parent->getParentRoleId(); |
56 | if ($parent_id) { |
57 | $parent = $role_repo->findOneBy(['id' => $parent_id]); |
58 | array_unshift($parent_chain, $parent); |
59 | } else { |
60 | $parent = null; |
61 | } |
62 | } |
63 | |
64 | $out = OlzHeader::render([ |
65 | 'back_link' => "{$code_href}verein", |
66 | 'title' => $role_name, |
67 | 'description' => "{$role_short_description} - Ressort {$role_name} der OL Zimmerberg.", |
68 | 'norobots' => $no_robots, |
69 | 'canonical_url' => "{$code_href}verein/{$role_username}", |
70 | ]); |
71 | |
72 | $out .= "<div class='content-full olz-role-page'>"; |
73 | $out .= "<nav aria-label='breadcrumb'>"; |
74 | $out .= "<ol class='breadcrumb'>"; |
75 | $out .= "<li class='breadcrumb-item'><a href='{$code_href}verein'>OL Zimmerberg</a></li>"; |
76 | foreach ($parent_chain as $breadcrumb) { |
77 | $username = $breadcrumb?->getUsername(); |
78 | $name = $breadcrumb?->getName(); |
79 | $out .= "<li class='breadcrumb-item'><a href='{$code_href}verein/{$username}'>{$name}</a></li>"; |
80 | } |
81 | $out .= "<li class='breadcrumb-item active' aria-current='page'>{$role_name}</li>"; |
82 | $out .= "</ol>"; |
83 | $out .= "</nav>"; |
84 | |
85 | $edit_admin = ''; |
86 | $add_membership_admin = ''; |
87 | $add_child_role_admin = ''; |
88 | $is_parent_superior = $this->authUtils()->hasRoleEditPermission($parent_role_id); |
89 | $is_parent_owner = $parent_role && $this->entityUtils()->canUpdateOlzEntity($parent_role, null, 'roles'); |
90 | $can_parent_edit = $is_parent_superior || $is_parent_owner; |
91 | $is_superior = $this->authUtils()->hasRoleEditPermission($role_id); |
92 | $is_owner = $this->entityUtils()->canUpdateOlzEntity($role, null, 'roles'); |
93 | $can_edit = $is_superior || $is_owner; |
94 | if ($can_edit) { |
95 | $json_id = json_encode($role_id); |
96 | $json_can_parent_edit = json_encode(boolval($can_parent_edit)); |
97 | $edit_admin = <<<ZZZZZZZZZZ |
98 | <div> |
99 | <button |
100 | id='edit-role-button' |
101 | class='btn btn-primary' |
102 | onclick='return olz.editRole({$json_id}, {$json_can_parent_edit})' |
103 | > |
104 | <img src='{$code_href}assets/icns/edit_white_16.svg' class='noborder' /> |
105 | Bearbeiten |
106 | </button> |
107 | </div> |
108 | ZZZZZZZZZZ; |
109 | $add_membership_admin = <<<ZZZZZZZZZZ |
110 | <div> |
111 | <button |
112 | id='add-role-user-button' |
113 | class='btn btn-primary' |
114 | onclick='return olz.addRoleUser({$json_id})' |
115 | > |
116 | <img src='{$code_href}assets/icns/new_white_16.svg' class='noborder' /> |
117 | Neuer Verantwortlicher |
118 | </button> |
119 | </div> |
120 | ZZZZZZZZZZ; |
121 | if ($can_have_child_roles) { |
122 | $add_child_role_admin = <<<ZZZZZZZZZZ |
123 | <div> |
124 | <button |
125 | id='add-sub-role-button' |
126 | class='btn btn-primary' |
127 | onclick='return olz.addChildRole({$json_id})' |
128 | > |
129 | <img src='{$code_href}assets/icns/new_white_16.svg' class='noborder' /> |
130 | Neues Unter-Ressort |
131 | </button> |
132 | </div> |
133 | ZZZZZZZZZZ; |
134 | } |
135 | } |
136 | |
137 | $out .= "<div>{$edit_admin}</div>"; |
138 | $description_html = $this->htmlUtils()->renderMarkdown($role->getDescription()); |
139 | $description_html = $role->replaceImagePaths($description_html); |
140 | $description_html = $role->replaceFilePaths($description_html); |
141 | $out .= $description_html; |
142 | |
143 | $assignees = $role->getUsers(); |
144 | $num_assignees = count($assignees); |
145 | $out .= "<br/><h2>Verantwortlich</h2>"; |
146 | if ($num_assignees === 0) { |
147 | $out .= "<p><i>Keine Ressort-Verantwortlichen</i></p>"; |
148 | $out .= $add_membership_admin; |
149 | } else { |
150 | $out .= "<div class='role-assignees'>"; |
151 | foreach ($assignees as $assignee) { |
152 | $out .= "<div class='assignee'>"; |
153 | if ($is_superior || $is_owner) { |
154 | $json_role_id = json_encode(intval($role_id)); |
155 | $json_user_id = json_encode(intval($assignee->getId())); |
156 | $out .= <<<ZZZZZZZZZZ |
157 | <button |
158 | id='delete-role-user-button' |
159 | class='btn btn-danger' |
160 | onclick='return olz.deleteRoleUser({$json_role_id}, {$json_user_id})' |
161 | > |
162 | <img src='{$code_href}assets/icns/delete_white_16.svg' class='noborder' /> |
163 | Entfernen |
164 | </button> |
165 | ZZZZZZZZZZ; |
166 | } |
167 | $out .= OlzUserInfoModal::render([ |
168 | 'user' => $assignee, |
169 | 'mode' => 'name_picture', |
170 | ]); |
171 | $out .= "</div>"; |
172 | } |
173 | $out .= $add_membership_admin; |
174 | $out .= "</div>"; |
175 | } |
176 | |
177 | $child_roles = $role_repo->findBy([ |
178 | 'parent_role' => $role_id, |
179 | 'on_off' => 1, |
180 | ], ['index_within_parent' => 'ASC']); |
181 | $num_child_roles = count($child_roles); |
182 | $out .= "<br/><h2>Unter-Ressorts</h2>"; |
183 | if ($num_child_roles === 0) { |
184 | $out .= "<p id='sub-roles'><i>Keine Unter-Ressorts</i></p>"; |
185 | } else { |
186 | $out .= "<ul id='sub-roles' class='no-style'>"; |
187 | foreach ($child_roles as $child_role) { |
188 | $child_role_name = $child_role->getName(); |
189 | $child_role_username = $child_role->getUsername(); |
190 | $out .= "<li><a href='{$code_href}verein/{$child_role_username}' class='linkint'><b>{$child_role_name}</b></a></li>"; |
191 | } |
192 | $out .= "</ul>"; |
193 | $out .= $add_child_role_admin; |
194 | } |
195 | |
196 | if ($is_member) { |
197 | $guide_html = $this->htmlUtils()->renderMarkdown($role->getGuide()); |
198 | $guide_html = $role->replaceImagePaths($guide_html); |
199 | $guide_html = $role->replaceFilePaths($guide_html); |
200 | $out .= "<br/><br/><h2>Aufgaben (nur für OLZ-Mitglieder sichtbar)</h2>"; |
201 | $out .= $guide_html; |
202 | } |
203 | |
204 | $out .= "</div>"; |
205 | $out .= OlzFooter::render(); |
206 | |
207 | return $out; |
208 | } |
209 | } |