organizations.php 9.7 KB

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