Skip to content

Commit 72097f8

Browse files
committed
[spalenque] - #14553
* found a couple of places that were deleting Languages (CMS remove from speaker and onbeforedelete speaker) * improved the way we save the relations for speaker: AreasOfExpertise, OtherPresentationLinks, TravelPreferences * added new migration to clean up these tables flooded with orphaned rows
1 parent df3f9bc commit 72097f8

6 files changed

Lines changed: 142 additions & 47 deletions

File tree

migrations/migrations.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ migrations:
117117
- LanguagesSeedMigration
118118
- PresentationSpeakerLanguageMigration
119119
- PageLinkMigration
120-
- LanguagesUpdateMigration
120+
- LanguagesUpdateMigration
121+
- CleanupSpeakerEmptyRelations

registration/code/ui/forms/EditSpeakerProfileForm.php

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -264,43 +264,81 @@ function addAction($data, $form) {
264264

265265

266266
// Expertise
267-
$speaker->AreasOfExpertise()->removeAll();
268-
if ($data['Expertise']) {
267+
if (isset($data['Expertise'])) {
269268
$expertise = explode(',', $data['Expertise']);
270269

271270
foreach ($expertise as $exp) {
272271
if (trim($exp) != '') {
273-
if(!$expertise = SpeakerExpertise::get()->filter(['SpeakerID' => 0, 'Expertise' => $exp])->first()) {
272+
if(!$expertise = $speaker->AreasOfExpertise()->find('Expertise', $exp)) {
274273
$expertise = SpeakerExpertise::create(['Expertise' => $exp]);
274+
$speaker->AreasOfExpertise()->add( $expertise );
275275
}
276-
277-
$speaker->AreasOfExpertise()->add( $expertise );
278276
}
279277
}
278+
// remove missing
279+
foreach($speaker->AreasOfExpertise() as $exp){
280+
if (!in_array($exp->Expertise, $expertise)) {
281+
$exp->delete();
282+
}
283+
}
284+
} else {
285+
// remove all
286+
foreach($speaker->AreasOfExpertise() as $exp){
287+
$exp->delete();
288+
}
280289
}
281290

282291

283292
// Presentation Link
284-
$speaker->OtherPresentationLinks()->removeAll();
285-
foreach ($data['PresentationLink'] as $key => $link) {
286-
if (trim($link) != '') {
287-
$presentation_title = trim($data['PresentationTitle'][$key]);
288-
$presentation_link = SpeakerPresentationLink::create(array(
289-
'LinkUrl' => $link,
290-
'Title' => $presentation_title
291-
));
292-
$speaker->OtherPresentationLinks()->add( $presentation_link );
293+
if(isset($data['PresentationLink'])) {
294+
foreach ($data['PresentationLink'] as $key => $link) {
295+
if (trim($link) != '') {
296+
$presentation_title = trim($data['PresentationTitle'][$key]);
297+
if ($has_link = $speaker->OtherPresentationLinks()->find('LinkUrl',$link)) {
298+
$has_link->Title = $presentation_title;
299+
$has_link->write();
300+
} else {
301+
$presentation_link = SpeakerPresentationLink::create(array(
302+
'LinkUrl' => $link,
303+
'Title' => $presentation_title
304+
));
305+
$speaker->OtherPresentationLinks()->add( $presentation_link );
306+
}
307+
308+
}
309+
}
310+
// remove missing
311+
foreach($speaker->OtherPresentationLinks() as $pl){
312+
if (!in_array($pl->LinkUrl, $data['PresentationLink'])) {
313+
$pl->delete();
314+
}
293315
}
294316
}
295317

318+
296319
// Travel Preferences
297-
$speaker->TravelPreferences()->removeAll();
320+
$current_items = $speaker->TravelPreferences()->column('Country');
298321
if(isset($data['CountriesToTravel'])) {
299-
foreach ($data['CountriesToTravel'] as $travel_country) {
300-
$travel_pref = SpeakerTravelPreference::create(array(
301-
'Country' => $travel_country
302-
));
303-
$speaker->TravelPreferences()->add($travel_pref);
322+
$countries = explode(',', $data['CountriesToTravel']);
323+
foreach ($countries as $travel_country) {
324+
if (!in_array($travel_country, $current_items)) {
325+
$travel_pref = SpeakerTravelPreference::create(array(
326+
'Country' => $travel_country
327+
));
328+
$speaker->TravelPreferences()->add($travel_pref);
329+
}
330+
}
331+
332+
// remove missing
333+
foreach($speaker->TravelPreferences() as $tp){
334+
if (!in_array($tp->Country, $countries)) {
335+
$tp->delete();
336+
}
337+
}
338+
} else {
339+
// remove all
340+
foreach($speaker->TravelPreferences() as $tp){
341+
$tp->delete();
304342
}
305343
}
306344

summit/code/forms/SpeakerForm.php

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

summit/code/infrastructure/active_records/events/presentations/PresentationSpeaker.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ protected function onBeforeDelete() {
115115
foreach($this->TravelPreferences() as $e){
116116
$e->delete();
117117
}
118-
foreach($this->Languages() as $e){
119-
$e->delete();
120-
}
121118
foreach($this->PromoCodes() as $e){
122119
$e->delete();
123120
}
@@ -131,6 +128,7 @@ protected function onBeforeDelete() {
131128
$this->Presentations()->removeAll();
132129
$this->OrganizationalRoles()->removeAll();
133130
$this->ActiveInvolvements()->removeAll();
131+
$this->Languages()->removeAll();
134132

135133
// set moderator to zero
136134

@@ -351,7 +349,7 @@ public function getCMSFields()
351349
$fields->addFieldToTab('Root.Main', $gridField);
352350

353351
// Languages
354-
$config = GridFieldConfig_RecordEditor::create();
352+
$config = GridFieldConfig_RelationEditor::create();
355353
$gridField = new GridField('Languages', 'Languages', $this->Languages(), $config);
356354
$fields->addFieldToTab('Root.Main', $gridField);
357355

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
class CleanupSpeakerEmptyRelations extends AbstractDBMigrationTask
4+
{
5+
6+
protected $title = "CleanupSpeakerEmptyRelations";
7+
8+
9+
protected $description = "clean up orphaned relations for speakers";
10+
11+
12+
function doUp()
13+
{
14+
global $database;
15+
DB::query("DELETE FROM SpeakerPresentationLink WHERE SpeakerID = 0");
16+
DB::query("DELETE FROM SpeakerTravelPreference WHERE SpeakerID = 0");
17+
DB::query("DELETE FROM SpeakerExpertise WHERE SpeakerID = 0");
18+
}
19+
20+
function doDown()
21+
{
22+
23+
}
24+
}

summit/javascript/speaker-form.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ $(document).ready(function(){
6767
languages.initialize();
6868

6969
$('#'+form_id+'_Language').tagsinput({
70-
freeInput: true,
70+
freeInput: false,
7171
maxTags: 5,
72-
trimValue: true,
7372
typeaheadjs: [
7473
{
7574
hint: true,

0 commit comments

Comments
 (0)