diff options
author | Struan Donald <struan@exo.org.uk> | 2019-05-17 15:00:34 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2019-06-17 14:24:22 +0100 |
commit | 5ed63071d7813fdcbcb0f6f7d5bacdd28ce90a10 (patch) | |
tree | a9b1119ee33e9b77981e14db07b67f93874d84e9 /perllib/Open311/PopulateServiceList.pm | |
parent | a2280170ea13758b6b9d042351801d81b576beeb (diff) |
allow multiple groups in open311 services group tag
Only parses as multiple groups if cobrand is configured to handle them
to stop issues with existing groups with commas in them.
Groups are parsed as CSV so as to allow commas in group names.
Diffstat (limited to 'perllib/Open311/PopulateServiceList.pm')
-rw-r--r-- | perllib/Open311/PopulateServiceList.pm | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/perllib/Open311/PopulateServiceList.pm b/perllib/Open311/PopulateServiceList.pm index bbc00231f..5f7ca10a3 100644 --- a/perllib/Open311/PopulateServiceList.pm +++ b/perllib/Open311/PopulateServiceList.pm @@ -2,6 +2,7 @@ package Open311::PopulateServiceList; use Moo; use Open311; +use Text::CSV; has bodies => ( is => 'ro' ); has found_contacts => ( is => 'rw', default => sub { [] } ); @@ -263,10 +264,21 @@ sub _set_contact_group { my ($self, $contact) = @_; my $groups_enabled = $self->_current_body_cobrand && $self->_current_body_cobrand->enable_category_groups; + my $multi_groups_enabled = $self->_current_body_cobrand && $self->_current_body_cobrand->enable_multiple_category_groups; my $old_group = $contact->get_extra_metadata('group') || ''; my $new_group = $groups_enabled ? $self->_current_service->{group} || '' : ''; - if ($old_group ne $new_group) { + if ($multi_groups_enabled && $new_group =~ /,/) { + my $csv = Text::CSV->new; + if ( $csv->parse($new_group) ) { + $new_group = [ $csv->fields ]; + } else { + warn "error parsing groups for " . $self->_current_body_cobrand->moniker . "contact " . $contact->category . ": $new_group\n"; + $new_group = [ $new_group ]; + } + } + + if ($self->_groups_different($old_group, $new_group)) { if ($new_group) { $contact->set_extra_metadata(group => $new_group); $contact->update({ @@ -300,6 +312,21 @@ sub _set_contact_non_public { }) if $keywords{private}; } +sub _groups_different { + my ($self, $old, $new) = @_; + + my $diff = 1; + if ($old && $new) { + $old = [ $old ] unless ref $old eq 'ARRAY'; + $new = [ $new ] unless ref $new eq 'ARRAY'; + $diff = join( ',', sort(@$old) ) ne join( ',', sort(@$new) ); + } elsif (!$old && !$new) { + $diff = 0; + } + + return $diff; +} + sub _delete_contacts_not_in_service_list { my $self = shift; |