@@ -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
0 commit comments