Skip to content

Commit 659cc5e

Browse files
ContactForm: Fix saving and populating contact addresses (#198)
fix #197
2 parents 5da809e + 5cd9b18 commit 659cc5e

File tree

2 files changed

+25
-44
lines changed

2 files changed

+25
-44
lines changed

library/Notifications/Web/Form/ContactForm.php

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -161,31 +161,17 @@ protected function assemble()
161161
public function populate($values)
162162
{
163163
if ($values instanceof Contact) {
164-
$formValues = [];
165-
if (! isset($formValues['contact'])) {
166-
$formValues['contact'] = [
164+
$formValues = [
165+
'contact' => [
167166
'full_name' => $values->full_name,
168167
'username' => $values->username,
169168
'color' => $values->color,
170169
'default_channel_id' => $values->default_channel_id
171-
];
172-
}
170+
]
171+
];
173172

174173
foreach ($values->contact_address as $contactInfo) {
175-
if (! isset($formValues['contact_address'])) {
176-
$formValues['contact_address'] = [
177-
'email' => null,
178-
'rocketchat' => null
179-
];
180-
}
181-
182-
if ($contactInfo->type === 'email') {
183-
$formValues['contact_address']['email' ] = $contactInfo->address;
184-
}
185-
186-
if ($contactInfo->type === 'rocketchat') {
187-
$formValues['contact_address']['rocketchat'] = $contactInfo->address;
188-
}
174+
$formValues['contact_address'][$contactInfo->type] = $contactInfo->address;
189175
}
190176

191177
$values = $formValues;
@@ -196,7 +182,12 @@ public function populate($values)
196182
return $this;
197183
}
198184

199-
public function addOrUpdateContact()
185+
/**
186+
* Add or update the contact and its corresponding contact addresses
187+
*
188+
* @return void
189+
*/
190+
public function addOrUpdateContact(): void
200191
{
201192
$contactInfo = $this->getValues();
202193

@@ -208,22 +199,27 @@ public function addOrUpdateContact()
208199
$addressFromDb = [];
209200
if ($this->contactId === null) {
210201
$this->db->insert('contact', $contact);
211-
212202
$this->contactId = $this->db->lastInsertId();
213203
} else {
214-
$this->db->update('contact', $contact, ['id = ?' => $this->contactId]);
204+
$contactFromDb = (array) $this->db->fetchOne(
205+
Contact::on($this->db)->withoutColumns(['id'])
206+
->filter(Filter::equal('id', $this->contactId))
207+
->assembleSelect()
208+
);
215209

216-
$addressObjects = ContactAddress::on($this->db);
210+
if (! empty(array_diff_assoc($contact, $contactFromDb))) {
211+
$this->db->update('contact', $contact, ['id = ?' => $this->contactId]);
212+
}
217213

218-
$addressObjects->filter(Filter::equal('contact_id', $this->contactId));
214+
$addressObjects = (ContactAddress::on($this->db))
215+
->filter(Filter::equal('contact_id', $this->contactId));
219216

220217
foreach ($addressObjects as $addressRow) {
221-
$addressFromDb[$addressRow->type] = [$addressRow->id, $addressRow->address];
218+
$addressFromDb[$addressRow->type] = [$addressRow->id, $addressRow->address];
222219
}
223220
}
224221

225-
$addr = ! empty($addressFromDb) ? $addressFromDb : $addressFromForm;
226-
foreach ($addr as $type => $value) {
222+
foreach ($addressFromForm as $type => $value) {
227223
$this->insertOrUpdateAddress($type, $addressFromForm, $addressFromDb);
228224
}
229225

phpstan-baseline-standard.neon

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -940,14 +940,9 @@ parameters:
940940
count: 1
941941
path: library/Notifications/Web/Form/ContactForm.php
942942

943-
-
944-
message: "#^Call to an undefined method ipl\\\\Sql\\\\Connection\\:\\:lastInsertId\\(\\)\\.$#"
945-
count: 1
946-
path: library/Notifications/Web/Form/ContactForm.php
947-
948943
-
949944
message: "#^Cannot access property \\$address on mixed\\.$#"
950-
count: 3
945+
count: 2
951946
path: library/Notifications/Web/Form/ContactForm.php
952947

953948
-
@@ -957,7 +952,7 @@ parameters:
957952

958953
-
959954
message: "#^Cannot access property \\$type on mixed\\.$#"
960-
count: 3
955+
count: 2
961956
path: library/Notifications/Web/Form/ContactForm.php
962957

963958
-
@@ -970,11 +965,6 @@ parameters:
970965
count: 1
971966
path: library/Notifications/Web/Form/ContactForm.php
972967

973-
-
974-
message: "#^Method Icinga\\\\Module\\\\Notifications\\\\Web\\\\Form\\\\ContactForm\\:\\:addOrUpdateContact\\(\\) has no return type specified\\.$#"
975-
count: 1
976-
path: library/Notifications/Web/Form/ContactForm.php
977-
978968
-
979969
message: "#^Method Icinga\\\\Module\\\\Notifications\\\\Web\\\\Form\\\\ContactForm\\:\\:assemble\\(\\) has no return type specified\\.$#"
980970
count: 1
@@ -995,11 +985,6 @@ parameters:
995985
count: 1
996986
path: library/Notifications/Web/Form/ContactForm.php
997987

998-
-
999-
message: "#^Offset 'contact' on array\\{\\} in isset\\(\\) does not exist\\.$#"
1000-
count: 1
1001-
path: library/Notifications/Web/Form/ContactForm.php
1002-
1003988
-
1004989
message: "#^Method Icinga\\\\Module\\\\Notifications\\\\Web\\\\Form\\\\EventRuleDecorator\\:\\:assemble\\(\\) has no return type specified\\.$#"
1005990
count: 1

0 commit comments

Comments
 (0)