organizations.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. if (isset($_POST['add_organization'])) {
  14. $label = normalizeOrganizationLabel($_POST['label'] ?? '');
  15. $sortOrder = (int) ($_POST['sort_order'] ?? 0);
  16. $active = isset($_POST['active']);
  17. if (!isValidOrganizationLabel($label)) {
  18. $message = 'Bitte einen Organisationsnamen mit maximal 120 Zeichen eingeben.';
  19. $messageType = 'error';
  20. } else {
  21. $organizations[] = [
  22. 'id' => generateOrganizationIdFromLabel($label, $organizations),
  23. 'label' => $label,
  24. 'sort_order' => $sortOrder,
  25. 'active' => $active,
  26. ];
  27. saveOrganizations($organizations);
  28. $message = 'Organisation wurde angelegt.';
  29. $messageType = 'success';
  30. }
  31. }
  32. if (isset($_POST['update_organization'])) {
  33. $organizationId = normalizeOrganizationId($_POST['organization_id'] ?? '');
  34. $label = normalizeOrganizationLabel($_POST['label'] ?? '');
  35. $sortOrder = (int) ($_POST['sort_order'] ?? 0);
  36. $active = isset($_POST['active']);
  37. $updated = false;
  38. if (!isValidOrganizationLabel($label)) {
  39. $message = 'Bitte einen Organisationsnamen mit maximal 120 Zeichen eingeben.';
  40. $messageType = 'error';
  41. } else {
  42. foreach ($organizations as &$organization) {
  43. if ($organization['id'] !== $organizationId) {
  44. continue;
  45. }
  46. $organization['label'] = $label;
  47. $organization['sort_order'] = $sortOrder;
  48. $organization['active'] = $active;
  49. $updated = true;
  50. break;
  51. }
  52. unset($organization);
  53. if ($updated) {
  54. saveOrganizations($organizations);
  55. $message = 'Organisation wurde aktualisiert.';
  56. $messageType = 'success';
  57. } else {
  58. $message = 'Organisation nicht gefunden.';
  59. $messageType = 'error';
  60. }
  61. }
  62. }
  63. if (isset($_POST['delete_organization'])) {
  64. $organizationId = normalizeOrganizationId($_POST['organization_id'] ?? '');
  65. $organizations = array_values(array_filter($organizations, function ($organization) use ($organizationId) {
  66. return $organization['id'] !== $organizationId;
  67. }));
  68. saveOrganizations($organizations);
  69. $message = 'Organisation wurde gelöscht.';
  70. $messageType = 'success';
  71. }
  72. $organizations = getOrganizations(false);
  73. }
  74. $editingOrganization = isset($_GET['edit']) ? getOrganizationById($_GET['edit']) : null;
  75. $bodyClass = 'admin-page';
  76. include __DIR__ . '/../includes/header.php';
  77. ?>
  78. <div class="admin-header">
  79. <h2>Organisationen verwalten</h2>
  80. <div>
  81. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  82. </div>
  83. </div>
  84. <?php if ($message !== ''): ?>
  85. <div class="alert alert-<?php echo escape($messageType); ?>">
  86. <?php echo escape($message); ?>
  87. </div>
  88. <?php endif; ?>
  89. <div class="panel" style="padding: 2rem;">
  90. <h3><?php echo $editingOrganization ? 'Organisation bearbeiten' : 'Neue Organisation'; ?></h3>
  91. <form method="POST">
  92. <?php if ($editingOrganization): ?>
  93. <input type="hidden" name="organization_id" value="<?php echo escape($editingOrganization['id']); ?>">
  94. <?php endif; ?>
  95. <div class="form-group">
  96. <label for="label">Name *</label>
  97. <input type="text" id="label" name="label" required maxlength="120" value="<?php echo escape($editingOrganization['label'] ?? ''); ?>">
  98. </div>
  99. <div class="form-group">
  100. <label for="sort_order">Sortierung</label>
  101. <input type="number" id="sort_order" name="sort_order" value="<?php echo escape((string) ($editingOrganization['sort_order'] ?? 0)); ?>">
  102. </div>
  103. <div class="form-group">
  104. <label>
  105. <input type="checkbox" name="active" value="1" <?php echo (!isset($editingOrganization['active']) || !empty($editingOrganization['active'])) ? 'checked' : ''; ?>>
  106. Organisation ist auswählbar
  107. </label>
  108. </div>
  109. <button type="submit" name="<?php echo $editingOrganization ? 'update_organization' : 'add_organization'; ?>" class="btn">
  110. <?php echo $editingOrganization ? 'Speichern' : 'Organisation anlegen'; ?>
  111. </button>
  112. <?php if ($editingOrganization): ?>
  113. <a href="organizations.php" class="btn btn-secondary">Abbrechen</a>
  114. <?php endif; ?>
  115. </form>
  116. </div>
  117. <div class="panel">
  118. <h3>Organisationen</h3>
  119. <div class="table-responsive">
  120. <table class="responsive-table">
  121. <thead>
  122. <tr>
  123. <th>Name</th>
  124. <th>ID</th>
  125. <th>Sortierung</th>
  126. <th>Status</th>
  127. <th>Aktionen</th>
  128. </tr>
  129. </thead>
  130. <tbody>
  131. <?php foreach ($organizations as $organization): ?>
  132. <tr>
  133. <td data-label="Name"><?php echo escape($organization['label']); ?></td>
  134. <td data-label="ID"><code><?php echo escape($organization['id']); ?></code></td>
  135. <td data-label="Sortierung"><?php echo (int) $organization['sort_order']; ?></td>
  136. <td data-label="Status">
  137. <span class="status <?php echo !empty($organization['active']) ? 'status-open' : 'status-cancelled'; ?>">
  138. <?php echo !empty($organization['active']) ? 'Aktiv' : 'Inaktiv'; ?>
  139. </span>
  140. </td>
  141. <td data-label="Aktionen">
  142. <a href="organizations.php?edit=<?php echo urlencode($organization['id']); ?>" class="btn btn-small">Bearbeiten</a>
  143. <form method="POST" style="display: inline;" onsubmit="return confirm('Organisation wirklich löschen?');">
  144. <input type="hidden" name="organization_id" value="<?php echo escape($organization['id']); ?>">
  145. <button type="submit" name="delete_organization" class="btn btn-secondary btn-small">Löschen</button>
  146. </form>
  147. </td>
  148. </tr>
  149. <?php endforeach; ?>
  150. </tbody>
  151. </table>
  152. </div>
  153. </div>
  154. <?php include __DIR__ . '/../includes/footer.php'; ?>