organizations.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. require_once __DIR__ . "/../config.php";
  3. require_once __DIR__ . "/../includes/functions.php";
  4. if (empty($_SESSION["admin_logged_in"])) {
  5. header("Location: login.php");
  6. exit();
  7. }
  8. $pageTitle = "Organisationen verwalten";
  9. $message = "";
  10. $messageType = "";
  11. $organizations = getOrganizations(false);
  12. if ($_SERVER["REQUEST_METHOD"] === "POST") {
  13. // Validate CSRF token
  14. if (!validateCsrfToken($_POST["csrf_token"] ?? "")) {
  15. $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  16. $messageType = "error";
  17. } else {
  18. if (isset($_POST["add_organization"])) {
  19. $label = normalizeOrganizationLabel($_POST["label"] ?? "");
  20. $sortOrder = (int) ($_POST["sort_order"] ?? 0);
  21. $active = isset($_POST["active"]);
  22. if (!isValidOrganizationLabel($label)) {
  23. $message =
  24. "Bitte einen Organisationsnamen mit maximal 120 Zeichen eingeben.";
  25. $messageType = "error";
  26. } else {
  27. $orgId = generateOrganizationIdFromLabel(
  28. $label,
  29. $organizations,
  30. );
  31. $organizations[] = [
  32. "id" => $orgId,
  33. "label" => $label,
  34. "sort_order" => $sortOrder,
  35. "active" => $active,
  36. ];
  37. saveOrganizations($organizations);
  38. logAccess("Admin added organization", [
  39. "org_id" => $orgId,
  40. "label" => $label,
  41. ]);
  42. $message = "Organisation wurde angelegt.";
  43. $messageType = "success";
  44. }
  45. }
  46. if (isset($_POST["update_organization"])) {
  47. $organizationId = normalizeOrganizationId(
  48. $_POST["organization_id"] ?? "",
  49. );
  50. $label = normalizeOrganizationLabel($_POST["label"] ?? "");
  51. $sortOrder = (int) ($_POST["sort_order"] ?? 0);
  52. $active = isset($_POST["active"]);
  53. $updated = false;
  54. if (!isValidOrganizationLabel($label)) {
  55. $message =
  56. "Bitte einen Organisationsnamen mit maximal 120 Zeichen eingeben.";
  57. $messageType = "error";
  58. } else {
  59. foreach ($organizations as &$organization) {
  60. if ($organization["id"] !== $organizationId) {
  61. continue;
  62. }
  63. $organization["label"] = $label;
  64. $organization["sort_order"] = $sortOrder;
  65. $organization["active"] = $active;
  66. $updated = true;
  67. break;
  68. }
  69. unset($organization);
  70. if ($updated) {
  71. saveOrganizations($organizations);
  72. logAccess("Admin updated organization", [
  73. "org_id" => $organizationId,
  74. "label" => $label,
  75. ]);
  76. $message = "Organisation wurde aktualisiert.";
  77. $messageType = "success";
  78. } else {
  79. $message = "Organisation nicht gefunden.";
  80. $messageType = "error";
  81. }
  82. }
  83. }
  84. if (isset($_POST["delete_organization"])) {
  85. $organizationId = normalizeOrganizationId(
  86. $_POST["organization_id"] ?? "",
  87. );
  88. $orgLabel = "";
  89. $found = false;
  90. foreach ($organizations as $organization) {
  91. if ($organization["id"] === $organizationId) {
  92. $orgLabel = $organization["label"];
  93. break;
  94. }
  95. }
  96. $organizations = array_values(
  97. array_filter($organizations, function ($organization) use (
  98. $organizationId,
  99. ) {
  100. return $organization["id"] !== $organizationId;
  101. }),
  102. );
  103. saveOrganizations($organizations);
  104. logAccess("Admin deleted organization", [
  105. "org_id" => $organizationId,
  106. "label" => $orgLabel,
  107. ]);
  108. $message = "Organisation wurde gelöscht.";
  109. $messageType = "success";
  110. }
  111. $organizations = getOrganizations(false);
  112. }
  113. }
  114. $editingOrganization = isset($_GET["edit"])
  115. ? getOrganizationById($_GET["edit"])
  116. : null;
  117. $bodyClass = "admin-page";
  118. include __DIR__ . "/../includes/header.php";
  119. ?>
  120. <div class="admin-header">
  121. <h2>Organisationen verwalten</h2>
  122. <div>
  123. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  124. </div>
  125. </div>
  126. <?php if ($message !== ""): ?>
  127. <div class="alert alert-<?php echo escape($messageType); ?>">
  128. <?php echo escape($message); ?>
  129. </div>
  130. <?php endif; ?>
  131. <div class="panel panel-lg">
  132. <h3><?php echo $editingOrganization
  133. ? "Organisation bearbeiten"
  134. : "Neue Organisation"; ?></h3>
  135. <form method="POST">
  136. <?php echo csrfField(); ?>
  137. <?php if ($editingOrganization): ?>
  138. <input type="hidden" name="organization_id" value="<?php echo escape(
  139. $editingOrganization["id"],
  140. ); ?>">
  141. <?php endif; ?>
  142. <div class="form-group">
  143. <label for="label">Name *</label>
  144. <input type="text" id="label" name="label" required maxlength="120" value="<?php echo escape(
  145. $editingOrganization["label"] ?? "",
  146. ); ?>">
  147. </div>
  148. <div class="form-group">
  149. <label for="sort_order">Sortierung</label>
  150. <input type="number" id="sort_order" name="sort_order" value="<?php echo escape(
  151. (string) ($editingOrganization["sort_order"] ?? 0),
  152. ); ?>">
  153. </div>
  154. <div class="form-group">
  155. <label>
  156. <input type="checkbox" name="active" value="1" <?php echo !isset(
  157. $editingOrganization["active"],
  158. ) || !empty($editingOrganization["active"])
  159. ? "checked"
  160. : ""; ?>>
  161. Organisation ist auswählbar
  162. </label>
  163. </div>
  164. <button type="submit" name="<?php echo $editingOrganization
  165. ? "update_organization"
  166. : "add_organization"; ?>" class="btn">
  167. <?php echo $editingOrganization
  168. ? "Speichern"
  169. : "Organisation anlegen"; ?>
  170. </button>
  171. <?php if ($editingOrganization): ?>
  172. <a href="organizations.php" class="btn btn-secondary">Abbrechen</a>
  173. <?php endif; ?>
  174. </form>
  175. </div>
  176. <div class="panel">
  177. <h3>Organisationen</h3>
  178. <div class="table-responsive">
  179. <table class="responsive-table">
  180. <thead>
  181. <tr>
  182. <th>Name</th>
  183. <th>ID</th>
  184. <th>Sortierung</th>
  185. <th>Status</th>
  186. <th>Aktionen</th>
  187. </tr>
  188. </thead>
  189. <tbody>
  190. <?php foreach ($organizations as $organization): ?>
  191. <tr>
  192. <td data-label="Name"><?php echo escape(
  193. $organization["label"],
  194. ); ?></td>
  195. <td data-label="ID"><code><?php echo escape(
  196. $organization["id"],
  197. ); ?></code></td>
  198. <td data-label="Sortierung"><?php echo (int) $organization[
  199. "sort_order"
  200. ]; ?></td>
  201. <td data-label="Status">
  202. <span class="status <?php echo !empty(
  203. $organization["active"]
  204. )
  205. ? "status-open"
  206. : "status-cancelled"; ?>">
  207. <?php echo !empty($organization["active"])
  208. ? "Aktiv"
  209. : "Inaktiv"; ?>
  210. </span>
  211. </td>
  212. <td data-label="Aktionen">
  213. <a href="organizations.php?edit=<?php echo urlencode(
  214. $organization["id"],
  215. ); ?>" class="btn btn-small">Bearbeiten</a>
  216. <form method="POST" class="inline-form" onsubmit="return confirm('Organisation wirklich löschen?');">
  217. <?php echo csrfField(); ?>
  218. <input type="hidden" name="organization_id" value="<?php echo escape(
  219. $organization["id"],
  220. ); ?>">
  221. <button type="submit" name="delete_organization" class="btn btn-secondary btn-small">Löschen</button>
  222. </form>
  223. </td>
  224. </tr>
  225. <?php endforeach; ?>
  226. </tbody>
  227. </table>
  228. </div>
  229. </div>
  230. <?php include __DIR__ . "/../includes/footer.php"; ?>