@@ -253,23 +253,34 @@ public function saveInto(DataObjectInterface $dataObject, $fieldList = null) {
253253
254254 $ speaker = $ dataObject ;
255255
256+ // Expertise
256257 $ expertise_csv = $ this ->fields ->fieldByName ("Expertise " )->Value ();
257- $ expertise_array = explode (', ' ,$ expertise_csv );
258- $ exp_ids = array ();
259- if ($ expertise_array ) {
260- foreach ($ expertise_array as $ expertise ) {
261- if (empty ($ expertise )) continue ;
262- $ expertise = Convert::raw2sql (trim ($ expertise ));
263- if (!$ anexp = $ speaker ->AreasOfExpertise ()->find ('Expertise ' , $ expertise )) {
264- $ anexp = SpeakerExpertise::create (array ('Expertise ' => $ expertise ));
258+ if ($ expertise_csv ) {
259+ $ expertises = explode (', ' , $ expertise_csv );
260+
261+ foreach ($ expertises as $ exp ) {
262+ if (trim ($ exp ) != '' ) {
263+ if (!$ expertise = $ speaker ->AreasOfExpertise ()->find ('Expertise ' , $ exp )) {
264+ $ expertise = SpeakerExpertise::create (['Expertise ' => $ exp ]);
265+ $ speaker ->AreasOfExpertise ()->add ( $ expertise );
266+ }
265267 }
266-
267- $ anexp ->write ();
268- $ exp_ids [] = $ anexp ->ID ;
268+ }
269+ // remove missing
270+ foreach ($ speaker ->AreasOfExpertise () as $ exp ){
271+ if (!in_array ($ exp ->Expertise , $ expertises )) {
272+ $ exp ->delete ();
273+ }
274+ }
275+ } else {
276+ // remove all
277+ foreach ($ speaker ->AreasOfExpertise () as $ exp ){
278+ $ exp ->delete ();
269279 }
270280 }
271- $ speaker ->AreasOfExpertise ()->setByIdList ($ exp_ids );
272281
282+
283+ // Languages
273284 $ language = $ this ->fields ->fieldByName ("Language " )->Value ();
274285 $ speaker ->Languages ()->removeAll ();
275286 foreach (explode (', ' ,$ language ) as $ lang_name ) {
@@ -278,7 +289,8 @@ public function saveInto(DataObjectInterface $dataObject, $fieldList = null) {
278289 $ speaker ->Languages ()->add ($ lang );
279290 }
280291
281- $ link_ids = [];
292+ // Presentation Link
293+ $ links = [];
282294 for ($ i = 1 ; $ i <= 5 ; $ i ++ ){
283295 $ link = $ this ->fields ->fieldByName ("PresentationLink[ {$ i }] " );
284296 $ title = $ this ->fields ->fieldByName ("PresentationTitle[ {$ i }] " );
@@ -294,12 +306,20 @@ public function saveInto(DataObjectInterface $dataObject, $fieldList = null) {
294306 $ alink ->Title = $ title_val ;
295307 }
296308
309+ $ links [] = $ alink ->LinkUrl ;
297310 $ alink ->write ();
298- $ link_ids [] = $ alink ->ID ;
311+ $ speaker ->OtherPresentationLinks ()->add ( $ alink );
312+ }
313+
314+ // remove missing links
315+ foreach ($ speaker ->OtherPresentationLinks () as $ pl ){
316+ if (!in_array ($ pl ->LinkUrl , $ links )) {
317+ $ pl ->delete ();
318+ }
299319 }
300- $ speaker ->OtherPresentationLinks ()->setByIdList ($ link_ids );
301320
302321
322+ // Org Roles
303323 $ roles = $ this ->fields ->fieldByName ("OrganizationalRole " )->Value ();
304324 if ($ roles && in_array (0 ,$ roles )) { // 0 is the id for Other
305325 $ other_role = $ this ->fields ->fieldByName ("OtherOrganizationalRole " )->Value ();
@@ -314,8 +334,9 @@ public function saveInto(DataObjectInterface $dataObject, $fieldList = null) {
314334 }
315335 $ speaker ->OrganizationalRoles ()->setByIdList ($ roles );
316336
337+
338+ // Travel Preferences
317339 $ countries_2_travel = $ this ->fields ->fieldByName ('CountriesToTravel ' );
318- $ country_ids = array ();
319340 if (!is_null ($ countries_2_travel )) {
320341 $ country_array = $ countries_2_travel ->Value ();
321342 if ($ country_array ) {
@@ -324,14 +345,28 @@ public function saveInto(DataObjectInterface $dataObject, $fieldList = null) {
324345 $ country_name = Convert::raw2sql (trim ($ country_name ));
325346 if (!$ acountry = $ speaker ->TravelPreferences ()->find ('Country ' ,$ country_name )) {
326347 $ acountry = SpeakerTravelPreference::create (array ('Country ' => $ country_name ));
348+ $ speaker ->TravelPreferences ()->add ($ acountry );
349+ }
350+ }
351+ // remove missing
352+ foreach ($ speaker ->TravelPreferences () as $ tp ){
353+ if (!in_array ($ tp ->Country , $ country_array )) {
354+ $ tp ->delete ();
327355 }
328-
329- $ acountry ->write ();
330- $ country_ids [] = $ acountry ->ID ;
356+ }
357+ } else {
358+ // remove all
359+ foreach ($ speaker ->TravelPreferences () as $ tp ){
360+ $ tp ->delete ();
331361 }
332362 }
363+ } else {
364+ // remove all
365+ foreach ($ speaker ->TravelPreferences () as $ tp ){
366+ $ tp ->delete ();
367+ }
333368 }
334- $ speaker -> TravelPreferences ()-> setByIdList ( $ country_ids );
369+
335370
336371 }
337372
0 commit comments