diff options
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/App.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 11 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 20 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/FixMyStreet.pm | 9 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/UKCouncils.pm | 14 | ||||
-rw-r--r-- | perllib/FixMyStreet/Email.pm | 8 | ||||
-rw-r--r-- | perllib/Open311.pm | 72 | ||||
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 3 | ||||
-rw-r--r-- | perllib/Open311/GetUpdates.pm | 19 | ||||
-rw-r--r-- | perllib/Open311/PopulateServiceList.pm | 28 |
10 files changed, 99 insertions, 90 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index 1a651d282..be0e91101 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -310,10 +310,7 @@ sub send_email { from => [ $sender, _($sender_name) ], %{ $c->stash }, %$extra_stash_values, - additional_template_paths => [ - FixMyStreet->path_to( 'templates', 'email', $c->cobrand->moniker, $c->stash->{lang_code} )->stringify, - FixMyStreet->path_to( 'templates', 'email', $c->cobrand->moniker )->stringify, - ] + additional_template_paths => $c->cobrand->path_to_email_templates($c->stash->{lang_code}), }; return if FixMyStreet::Email::is_abuser($c->model('DB')->schema, $vars->{to}); diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index 696234d32..af9ca50b5 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -226,7 +226,6 @@ sub category_extras_ajax : Path('category_extras') : Args(0) { my $category_extra = ''; my $generate; if ( $c->stash->{category_extras}->{$category} && @{ $c->stash->{category_extras}->{$category} } >= 1 ) { - $c->stash->{report_meta} = {}; $c->stash->{category_extras} = { $category => $c->stash->{category_extras}->{$category} }; $generate = 1; } @@ -641,8 +640,14 @@ sub setup_categories_and_bodies : Private { push @category_options, $contact->category; my $metas = $contact->get_extra_fields; - $category_extras{ $contact->category } = $metas - if scalar @$metas; + if (scalar @$metas) { + foreach (@$metas) { + if (ref $_->{values} eq 'HASH') { + $_->{values} = [ map { { name => $_->{name}[0], key => $_->{key}[0] } } @{$_->{values}->{value}} ]; + } + } + $category_extras{ $contact->category } = $metas; + } my $body_send_method = $bodies{$contact->body_id}->send_method || ''; $c->stash->{unresponsive}{$contact->category} = $contact->body_id diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 4dc024d48..76d73d96e 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -27,7 +27,25 @@ Returns the path to the templates for this cobrand - by default sub path_to_web_templates { my $self = shift; my $paths = [ - FixMyStreet->path_to( 'templates/web', $self->moniker )->stringify, + FixMyStreet->path_to( 'templates/web', $self->moniker ), + ]; + return $paths; +} + +=head1 path_to_email_templates + + $path = $cobrand->path_to_email_templates( ); + +Returns the path to the email templates for this cobrand - by default +"templates/email/$moniker" (and then default in Email.pm). + +=cut + +sub path_to_email_templates { + my ( $self, $lang_code ) = @_; + my $paths = [ + FixMyStreet->path_to( 'templates', 'email', $self->moniker, $lang_code ), + FixMyStreet->path_to( 'templates', 'email', $self->moniker ), ]; return $paths; } diff --git a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm index c8c1eef66..61011a414 100644 --- a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm +++ b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm @@ -7,9 +7,16 @@ use constant COUNCIL_ID_BROMLEY => 2482; sub path_to_web_templates { my $self = shift; return [ - FixMyStreet->path_to( 'templates/web/fixmystreet.com' )->stringify, + FixMyStreet->path_to( 'templates/web/fixmystreet.com' ), ]; } +sub path_to_email_templates { + my ( $self, $lang_code ) = @_; + return [ + FixMyStreet->path_to( 'templates', 'email', 'fixmystreet.com'), + ]; +} + # FixMyStreet should return all cobrands sub restriction { diff --git a/perllib/FixMyStreet/Cobrand/UKCouncils.pm b/perllib/FixMyStreet/Cobrand/UKCouncils.pm index 0321e0297..c8512f852 100644 --- a/perllib/FixMyStreet/Cobrand/UKCouncils.pm +++ b/perllib/FixMyStreet/Cobrand/UKCouncils.pm @@ -16,11 +16,21 @@ sub is_council { sub path_to_web_templates { my $self = shift; return [ - FixMyStreet->path_to( 'templates/web', $self->moniker )->stringify, - FixMyStreet->path_to( 'templates/web/fixmystreet-uk-councils' )->stringify, + FixMyStreet->path_to( 'templates/web', $self->moniker ), + FixMyStreet->path_to( 'templates/web/fixmystreet-uk-councils' ), ]; } +sub path_to_email_templates { + my ( $self, $lang_code ) = @_; + my $paths = [ + FixMyStreet->path_to( 'templates', 'email', $self->moniker, $lang_code ), + FixMyStreet->path_to( 'templates', 'email', $self->moniker ), + FixMyStreet->path_to( 'templates', 'email', 'fixmystreet.com'), + ]; + return $paths; +} + sub site_key { my $self = shift; return $self->council_url; diff --git a/perllib/FixMyStreet/Email.pm b/perllib/FixMyStreet/Email.pm index ce7dad47a..d955f6f72 100644 --- a/perllib/FixMyStreet/Email.pm +++ b/perllib/FixMyStreet/Email.pm @@ -92,13 +92,11 @@ sub send_cron { unpack('h*', random_bytes(5, 1)), FixMyStreet->config('EMAIL_DOMAIN') ); + my @include_path = @{ $cobrand->path_to_email_templates($lang_code) }; + push @include_path, FixMyStreet->path_to( 'templates', 'email', 'default' ); my $tt = Template->new({ ENCODING => 'utf8', - INCLUDE_PATH => [ - FixMyStreet->path_to( 'templates', 'email', $cobrand->moniker, $lang_code )->stringify, - FixMyStreet->path_to( 'templates', 'email', $cobrand->moniker )->stringify, - FixMyStreet->path_to( 'templates', 'email', 'default' )->stringify, - ], + INCLUDE_PATH => \@include_path, }); $vars->{signature} = _render_template($tt, 'signature.txt', $vars); $vars->{site_name} = Utils::trim_text(_render_template($tt, 'site-name.txt', $vars)); diff --git a/perllib/Open311.pm b/perllib/Open311.pm index fb793b027..6434be1fb 100644 --- a/perllib/Open311.pm +++ b/perllib/Open311.pm @@ -74,17 +74,10 @@ sub send_service_request { my $obj = $self->_get_xml_object( $response ); if ( $obj ) { - if ( $obj->{ request }->{ service_request_id } ) { - my $request_id = $obj->{request}->{service_request_id}; - - unless ( ref $request_id ) { - return $request_id; - } - } else { - my $token = $obj->{ request }->{ token }; - if ( $token ) { - return $self->get_service_request_id_from_token( $token ); - } + if ( my $request_id = $obj->{request}->[0]->{service_request_id} ) { + return $request_id unless ref $request_id; + } elsif ( my $token = $obj->{request}->[0]->{token} ) { + return $self->get_service_request_id_from_token( $token ); } } @@ -215,8 +208,8 @@ sub get_service_request_id_from_token { my $obj = $self->_get_xml_object( $service_token_xml ); - if ( $obj && $obj->{ request }->{ service_request_id } ) { - return $obj->{ request }->{ service_request_id }; + if ( $obj && $obj->{request}->[0]->{service_request_id} ) { + return $obj->{request}->[0]->{service_request_id}; } else { return 0; } @@ -240,15 +233,7 @@ sub get_service_request_updates { my $xml = $self->_get( $self->endpoints->{service_request_updates}, $params || undef ); my $service_requests = $self->_get_xml_object( $xml ); - my $requests; - if ( ref $service_requests->{request_update } eq 'ARRAY' ) { - $requests = $service_requests->{request_update}; - } - else { - $requests = [ $service_requests->{request_update} ]; - } - - return $requests; + return $service_requests->{request_update}; } sub post_service_request_update { @@ -263,16 +248,10 @@ sub post_service_request_update { my $obj = $self->_get_xml_object( $response ); if ( $obj ) { - if ( $obj->{ request_update }->{ update_id } ) { - my $update_id = $obj->{request_update}->{update_id}; - - # if there's nothing in the update_id element we get a HASHREF back - unless ( ref $update_id ) { - return $obj->{ request_update }->{ update_id }; - } + if ( my $update_id = $obj->{request_update}->[0]->{update_id} ) { + return $update_id unless ref $update_id; } else { - my $token = $obj->{ request_update }->{ token }; - if ( $token ) { + if ( my $token = $obj->{request_update}->[0]->{token} ) { return $self->get_service_request_id_from_token( $token ); } } @@ -455,7 +434,6 @@ sub _process_error { my $msg = ''; if ( ref $obj && exists $obj->{error} ) { my $errors = $obj->{error}; - $errors = [ $errors ] if ref $errors ne 'ARRAY'; $msg .= sprintf( "%s: %s\n", $_->{code}, $_->{description} ) for @{ $errors }; } @@ -463,16 +441,28 @@ sub _process_error { } sub _get_xml_object { - my $self = shift; - my $xml= shift; - - my $simple = XML::Simple->new(); - my $obj; - - eval { - $obj = $simple ->parse_string( $xml, ForceArray => [ qr/^key$/, qr/^name$/ ] ); + my ($self, $xml) = @_; + + # Of these, services/service_requests/service_request_updates are root + # elements, so GroupTags has no effect, but this is used in ForceArray too. + my $group_tags = { + services => 'service', + attributes => 'attribute', + values => 'value', + service_requests => 'request', + errors => 'error', + service_request_updates => 'request_update', + }; + my $simple = XML::Simple->new( + ForceArray => [ values %$group_tags ], + KeyAttr => {}, + GroupTags => $group_tags, + SuppressEmpty => undef, + ); + my $obj = eval { + $simple->parse_string($xml); }; - return $obj; } + 1; diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index daa5fb64d..921b2996d 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -122,8 +122,7 @@ sub update_comments { } ); - # ref test as XML::Simple will have returned an empty hashref for empty element - if ($request->{media_url} && !ref $request->{media_url}) { + if ($request->{media_url}) { my $ua = LWP::UserAgent->new; my $res = $ua->get($request->{media_url}); if ( $res->is_success && $res->content_type eq 'image/jpeg' ) { diff --git a/perllib/Open311/GetUpdates.pm b/perllib/Open311/GetUpdates.pm index 901e78809..1b1e339e3 100644 --- a/perllib/Open311/GetUpdates.pm +++ b/perllib/Open311/GetUpdates.pm @@ -42,23 +42,12 @@ sub update_reports { my ( $self, $report_ids, $open311, $body ) = @_; my $service_requests = $open311->get_service_requests( $report_ids ); - - my $requests; - - # XML::Simple is a bit inconsistent in how it structures - # things depending on the number of children an element has :( - if ( ref $service_requests->{request} eq 'ARRAY' ) { - $requests = $service_requests->{request}; - } - else { - $requests = [ $service_requests->{request} ]; - } + my $requests = $service_requests->{request}; for my $request (@$requests) { - # if it's a ref that means it's an empty element - # however, if there's no updated date then we can't - # tell if it's newer that what we have so we should skip it - next if ref $request->{updated_datetime} || ! exists $request->{updated_datetime}; + # if there's no updated date then we can't + # tell if it's newer than what we have so we should skip it + next unless $request->{updated_datetime}; my $request_id = $request->{service_request_id}; diff --git a/perllib/Open311/PopulateServiceList.pm b/perllib/Open311/PopulateServiceList.pm index 3236f3798..f5f117fb5 100644 --- a/perllib/Open311/PopulateServiceList.pm +++ b/perllib/Open311/PopulateServiceList.pm @@ -71,8 +71,6 @@ sub process_services { $self->found_contacts( [] ); my $services = $list->{service}; - # XML might only have one result and then squashed the 'array'-ness - $services = [ $services ] unless ref $services eq 'ARRAY'; foreach my $service ( @$services ) { $self->_current_service( $service ); $self->process_service; @@ -83,17 +81,21 @@ sub process_services { sub process_service { my $self = shift; - my $category = $self->_current_body->areas->{2218} ? - $self->_current_service->{description} : - $self->_current_service->{service_name}; + my $service_name = $self->_normalize_service_name; + + unless (defined $self->_current_service->{service_code}) { + warn "Service $service_name has no service code for body @{[$self->_current_body->id]}\n" + if $self->verbose >= 1; + return; + } - print $self->_current_service->{service_code} . ': ' . $category . "\n" if $self->verbose >= 2; + print $self->_current_service->{service_code} . ': ' . $service_name . "\n" if $self->verbose >= 2; my $contacts = $self->schema->resultset('Contact')->search( { body_id => $self->_current_body->id, -OR => [ email => $self->_current_service->{service_code}, - category => $category, + category => $service_name, ] } ); @@ -102,7 +104,7 @@ sub process_service { printf( "Multiple contacts for service code %s, category %s - Skipping\n", $self->_current_service->{service_code}, - $category, + $service_name, ); # best to not mark them as deleted as we don't know what we're doing @@ -205,13 +207,7 @@ sub _add_meta_to_contact { print "Fetching meta data for " . $self->_current_service->{service_code} . "\n" if $self->verbose >= 2; my $meta_data = $self->_current_open311->get_service_meta_info( $self->_current_service->{service_code} ); - if ( ref $meta_data->{ attributes }->{ attribute } eq 'HASH' ) { - $meta_data->{ attributes }->{ attribute } = [ - $meta_data->{ attributes }->{ attribute } - ]; - } - - if ( ! $meta_data->{attributes}->{attribute} ) { + unless ($meta_data->{attributes}) { warn sprintf( "Empty meta data for %s at %s", $self->_current_service->{service_code}, $self->_current_body->endpoint ) @@ -225,7 +221,7 @@ sub _add_meta_to_contact { map { $_->{description} =~ s/:\s*//; $_ } # there is a display order and we only want to sort once sort { $a->{order} <=> $b->{order} } - @{ $meta_data->{attributes}->{attribute} }; + @{ $meta_data->{attributes} }; # Some Open311 endpoints, such as Bromley and Warwickshire send <metadata> # for attributes which we *don't* want to display to the user (e.g. as |