From 049610384121eb9167c7fb8a50e80d6fedae4292 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Tue, 16 Sep 2025 14:10:03 +0000 Subject: [PATCH] Adb wohneinheit/add contacts --- Layout/default/AddressDB/View.php | 7 +- Layout/default/Preorder/Index.php | 4 + .../ADBWohneinheitController.php | 46 +++ ...0916153000_adb_wohneinheit_add_contact.php | 34 +++ public/js/pages/AddressDB/.gitkeep | 0 .../AddressDB/ADBWohneinheitContactManager.js | 282 ++++++++++++++++++ 6 files changed, 371 insertions(+), 2 deletions(-) create mode 100644 db/migrations/20250916153000_adb_wohneinheit_add_contact.php create mode 100644 public/js/pages/AddressDB/.gitkeep create mode 100644 public/js/pages/AddressDB/ADBWohneinheitContactManager.js diff --git a/Layout/default/AddressDB/View.php b/Layout/default/AddressDB/View.php index ffb737587..4f486f884 100644 --- a/Layout/default/AddressDB/View.php +++ b/Layout/default/AddressDB/View.php @@ -179,7 +179,10 @@ wohneinheiten as $unit): ?> - $unit->id])?>"> + + + $unit->id])?>"> + id?> oaid): ?> @@ -391,4 +394,4 @@ 'json'); } - \ No newline at end of file + diff --git a/Layout/default/Preorder/Index.php b/Layout/default/Preorder/Index.php index 31c3ab649..7d62b7af8 100644 --- a/Layout/default/Preorder/Index.php +++ b/Layout/default/Preorder/Index.php @@ -628,6 +628,7 @@ $pagination_entity_name = "Vorbestellungen";
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 ? `
    ${contactListHtml}
` : '

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 ` +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    +
    + ${this.editingIndex !== null ? `` : ''} + +
    +
    + `; + }, + + 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 = `
    Loading...

    ${message}

    `; + }, + + showError(message) { + const body = this.modal.querySelector('.modal-body'); + body.innerHTML = `
    ${message}
    `; + setTimeout(() => this.render(), 3000); + } +}; + +document.addEventListener('DOMContentLoaded', () => { + ADBWohneinheitContactManager.init(); +});