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