is(["preorderfront"]) && !$me->is("preorderreadonly")): ?>
+
$preorder->id])?>">
$preorder->id, "filter" => $filter])?>" class="text-danger" onclick="if(!confirm('Vorbestellung wirklich löschen?')) return false;" title="Vorbestellung Löschen">
@@ -2011,4 +2012,7 @@ $pagination_entity_name = "Vorbestellungen";
updateRimoTypesLink();
});
+
+
+
diff --git a/application/ADBWohneinheit/ADBWohneinheitController.php b/application/ADBWohneinheit/ADBWohneinheitController.php
index 9f921d266..50f093d7a 100644
--- a/application/ADBWohneinheit/ADBWohneinheitController.php
+++ b/application/ADBWohneinheit/ADBWohneinheitController.php
@@ -237,4 +237,50 @@ class ADBWohneinheitController extends mfBaseController {
"IS_ADMIN" => $isAdmin,
]);
}
+
+ protected function getContactsAction() {
+ $post = json_decode(file_get_contents('php://input'), true);
+ if (empty($post['id'])) {
+ self::returnJson(['success' => false, 'message' => 'Wohneinheit ID fehlt.']);
+ return;
+ }
+
+ $unit = new ADBWohneinheit($post['id']);
+ if (!$unit->id) {
+ self::returnJson(['success' => false, 'message' => 'Wohneinheit nicht gefunden.']);
+ return;
+ }
+
+ $contact = $unit->contact;
+
+ $contacts = !empty($contact) ? json_decode($contact, true) : [];
+
+ self::returnJson(['success' => true, 'contacts' => $contacts, 'header' =>
+ ($unit->hausnummer->strasse ? $unit->hausnummer->strasse->name : '') . ' ' .
+ ($unit->hausnummer ? $unit->hausnummer->hausnummer : '') . ', ' .
+ ($unit->hausnummer->plz ? $unit->hausnummer->plz->plz : '') . ' ' .
+ ($unit->hausnummer->ortschaft ? $unit->hausnummer->ortschaft->name : '')
+ ]);
+ }
+
+ protected function saveContactsAction() {
+ $post = json_decode(file_get_contents('php://input'), true);
+ if (empty($post['id']) || !isset($post['data'])) {
+ self::returnJson(['success' => false, 'message' => 'ID oder Daten fehlen.']);
+ return;
+ }
+
+ $unit = new ADBWohneinheit($post['id']);
+ if (!$unit->id) {
+ self::returnJson(['success' => false, 'message' => 'Wohneinheit nicht gefunden.']);
+ return;
+ }
+
+ $unit->contact = json_encode($post['data']);
+ if ($unit->save()) {
+ self::returnJson(['success' => true, 'message' => 'Kontakt erfolgreich gespeichert.']);
+ } else {
+ self::returnJson(['success' => false, 'message' => 'Fehler beim Speichern der Kontakt.']);
+ }
+ }
}
diff --git a/db/migrations/20250916153000_adb_wohneinheit_add_contact.php b/db/migrations/20250916153000_adb_wohneinheit_add_contact.php
new file mode 100644
index 000000000..631d10a4e
--- /dev/null
+++ b/db/migrations/20250916153000_adb_wohneinheit_add_contact.php
@@ -0,0 +1,34 @@
+getEnvironment() == 'addressdb') {
+ $table = $this->table('Wohneinheit');
+ if (!$table->hasColumn('contact')) {
+ $table->addColumn('contact', 'text', [
+ 'null' => true,
+ 'default' => null,
+ 'after' => 'note'
+ ])
+ ->save();
+ }
+ }
+ }
+
+ public function down(): void
+ {
+ if ($this->getEnvironment() == 'addressdb') {
+ $table = $this->table('Wohneinheit');
+ if ($table->hasColumn('contact')) {
+ $table->removeColumn('contact')
+ ->save();
+ }
+ }
+ }
+}
diff --git a/public/js/pages/AddressDB/.gitkeep b/public/js/pages/AddressDB/.gitkeep
new file mode 100644
index 000000000..e69de29bb
diff --git a/public/js/pages/AddressDB/ADBWohneinheitContactManager.js b/public/js/pages/AddressDB/ADBWohneinheitContactManager.js
new file mode 100644
index 000000000..a830a0889
--- /dev/null
+++ b/public/js/pages/AddressDB/ADBWohneinheitContactManager.js
@@ -0,0 +1,282 @@
+const ADBWohneinheitContactManager = {
+ modal: null,
+ homeId: null,
+ header: null,
+ contacts: [],
+ editingIndex: null,
+
+ init() {
+ document.body.addEventListener('click', this.handleBodyClick.bind(this));
+ },
+
+ handleBodyClick(e) {
+ const trigger = e.target.closest('[data-home-id][data-home-contact]');
+ if (trigger) {
+ e.preventDefault();
+ this.homeId = trigger.dataset.homeId;
+ this.openModal();
+ }
+ },
+
+ async openModal() {
+ this.editingIndex = null;
+ this.createModal();
+ this.showLoading();
+ try {
+ const response = await axios.post('/ADBWohneinheit/getContacts', { id: this.homeId });
+ if (response.data.success) {
+ this.contacts = response.data.contacts || [];
+ this.header = response.data.header || {};
+ this.render();
+ } else {
+ this.showError(response.data.message);
+ }
+ } catch (error) {
+ this.showError('Fehler beim Laden der Kontaktdaten.');
+ console.error(error);
+ }
+ },
+
+ createModal() {
+ if (this.modal) {
+ $(this.modal).modal('hide');
+ this.modal.remove();
+ }
+
+ const modalHtml = `
+
+ `;
+ document.body.insertAdjacentHTML('beforeend', modalHtml);
+ this.modal = document.getElementById('contactManagerModal');
+ $(this.modal).modal('show');
+ this.modal.addEventListener('hidden.bs.modal', () => {
+ if (document.body.contains(this.modal)) {
+ this.modal.remove();
+ }
+ this.modal = null;
+ });
+ },
+
+ render() {
+ const body = this.modal.querySelector('.modal-body');
+ const contactListHtml = this.contacts.map((contact, index) => this.renderContact(contact, index)).join('');
+
+ this.modal.querySelector('.modal-title').innerText = `Kontakt verwalten (WE-ID: ${this.homeId}) [${this.header}]`;
+
+ body.innerHTML = `
+
+
Bestehende Kontakte
+ ${this.contacts.length ? `
` : '
Keine Kontakte vorhanden.
'}
+
+
+
+
${this.editingIndex !== null ? 'Kontakt bearbeiten' : 'Neuen Kontakt hinzufügen'}
+ ${this.renderForm(this.editingIndex !== null ? this.contacts[this.editingIndex] : {})}
+
+ `;
+ this.attachFormListeners();
+ },
+
+ renderContact(contact, index) {
+ const isCompany = contact.firma && contact.firma.trim() !== '';
+ const displayName = isCompany ? contact.firma : `${contact.vorname || ''} ${contact.nachname || ''}`.trim();
+ const typeLabel = contact.type === 'renter' ? 'Mieter' : (contact.type === 'prospect' ? 'Interessent' : '');
+ const typeBadge = typeLabel ? `
${typeLabel}` : '';
+
+ return `
+
+
+ ${displayName}${typeBadge}
+
+ ${contact.telefon || 'Kein Telefon'} | ${contact.email || 'Keine E-Mail'}
+
+
+
+
+
+
+ `;
+ },
+
+ renderForm(contact = {}) {
+ return `
+
+ `;
+ },
+
+ attachFormListeners() {
+ const form = this.modal.querySelector('#contactForm');
+ if (!form) return;
+
+ const firmaInput = form.querySelector('[name="firma"]');
+ const vornameInput = form.querySelector('[name="vorname"]');
+ const nachnameInput = form.querySelector('[name="nachname"]');
+
+ const togglePersonFields = () => {
+ const isCompany = firmaInput.value.trim() !== '';
+ vornameInput.disabled = isCompany;
+ nachnameInput.disabled = isCompany;
+ if (isCompany) {
+ vornameInput.value = '';
+ nachnameInput.value = '';
+ }
+ };
+
+ const toggleCompanyField = () => {
+ const isPerson = vornameInput.value.trim() !== '' || nachnameInput.value.trim() !== '';
+ firmaInput.disabled = isPerson;
+ if (isPerson) {
+ firmaInput.value = '';
+ }
+ };
+
+ firmaInput.addEventListener('input', togglePersonFields);
+ vornameInput.addEventListener('input', toggleCompanyField);
+ nachnameInput.addEventListener('input', toggleCompanyField);
+
+ togglePersonFields();
+ toggleCompanyField();
+
+ form.addEventListener('submit', this.handleFormSubmit.bind(this));
+
+ this.modal.querySelector('.modal-body').addEventListener('click', (e) => {
+ const button = e.target.closest('button[data-action]');
+ if(!button) return;
+
+ e.preventDefault();
+ const action = button.dataset.action;
+ const index = button.dataset.index;
+
+ if (action === 'edit') this.handleEdit(index);
+ if (action === 'delete') this.handleDelete(index);
+ if (action === 'cancel-edit') this.handleCancelEdit();
+ });
+ },
+
+ handleFormSubmit(e) {
+ e.preventDefault();
+ const formData = new FormData(e.target);
+ const newContact = Object.fromEntries(formData.entries());
+
+ if (this.editingIndex !== null) {
+ this.contacts[this.editingIndex] = newContact;
+ } else {
+ this.contacts.push(newContact);
+ }
+ this.saveContacts();
+ },
+
+ handleEdit(index) {
+ this.editingIndex = parseInt(index, 10);
+ this.render();
+ },
+
+ handleCancelEdit() {
+ this.editingIndex = null;
+ this.render();
+ },
+
+ handleDelete(index) {
+ if (confirm('Sollen die Kontaktdaten wirklich gelöscht werden?')) {
+ this.contacts.splice(index, 1);
+ this.saveContacts();
+ }
+ },
+
+ async saveContacts() {
+ this.showLoading('Speichern...');
+ try {
+ const response = await axios.post('/ADBWohneinheit/saveContacts', {
+ id: this.homeId,
+ data: this.contacts
+ });
+
+ if (response.data.success) {
+ this.editingIndex = null;
+ const freshResponse = await axios.post('/ADBWohneinheit/getContacts', { id: this.homeId });
+ this.contacts = freshResponse.data.contacts || [];
+ this.render();
+ window.notify('success', response.data.message);
+ } else {
+ this.showError(response.data.message);
+ }
+ } catch(error) {
+ this.showError('Fehler beim Speichern der Kontaktdaten.');
+ console.error(error);
+ }
+ },
+
+ showLoading(message = 'Laden...') {
+ const body = this.modal.querySelector('.modal-body');
+ body.innerHTML = `
`;
+ },
+
+ showError(message) {
+ const body = this.modal.querySelector('.modal-body');
+ body.innerHTML = `
${message}
`;
+ setTimeout(() => this.render(), 3000);
+ }
+};
+
+document.addEventListener('DOMContentLoaded', () => {
+ ADBWohneinheitContactManager.init();
+});