diff options
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/Catalyst/Plugin/Session/State/Cookie.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/My.pm | 11 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 19 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/Update.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 51 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Open311conf.pm | 23 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 10 | ||||
-rw-r--r-- | perllib/Open311.pm | 98 | ||||
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 70 | ||||
-rw-r--r-- | perllib/Open311/PopulateServiceList.pm | 26 |
10 files changed, 293 insertions, 24 deletions
diff --git a/perllib/Catalyst/Plugin/Session/State/Cookie.pm b/perllib/Catalyst/Plugin/Session/State/Cookie.pm index 7c9f32878..ead8be38d 100644 --- a/perllib/Catalyst/Plugin/Session/State/Cookie.pm +++ b/perllib/Catalyst/Plugin/Session/State/Cookie.pm @@ -51,7 +51,7 @@ sub cookie_is_rejecting { my ( $c, $cookie ) = @_; # Don't output cookie for JS files. mySociety addition - return 1 if substr $c->request->path, -3 eq '.js'; + return 1 if substr($c->request->path, -3) eq '.js'; if ( $cookie->{path} ) { return 1 if index '/'.$c->request->path, $cookie->{path}; diff --git a/perllib/FixMyStreet/App/Controller/My.pm b/perllib/FixMyStreet/App/Controller/My.pm index 60e9dd09f..3de83b265 100644 --- a/perllib/FixMyStreet/App/Controller/My.pm +++ b/perllib/FixMyStreet/App/Controller/My.pm @@ -30,9 +30,16 @@ sub my : Path : Args(0) { my $pins = []; my $problems = {}; - my $rs = $c->user->problems->search( { + + my $params = { state => [ FixMyStreet::DB::Result::Problem->visible_states() ], - }, { + }; + $params = { + %{ $c->cobrand->problems_clause }, + %$params + } if $c->cobrand->problems_clause; + + my $rs = $c->user->problems->search( $params, { order_by => { -desc => 'confirmed' }, rows => 50 } )->page( $p_page ); diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index af4cdd5aa..68a30ca2c 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -124,12 +124,14 @@ sub report_form_ajax : Path('ajax') : Args(0) { my $category = $c->render_fragment( 'report/new/category.html'); my $councils_text = $c->render_fragment( 'report/new/councils_text.html'); my $has_open311 = keys %{ $c->stash->{category_extras} }; + my $extra_name_info = $c->render_fragment('report/new/extra_name.html'); my $body = JSON->new->utf8(1)->encode( { councils_text => $councils_text, category => $category, has_open311 => $has_open311, + extra_name_info => $extra_name_info, } ); @@ -542,7 +544,7 @@ sub setup_categories_and_councils : Private { ); $category_label = _('Property type:'); - } elsif ($first_council->{type} eq 'LBO') { + } elsif ($first_council->{id} != 2482 && $first_council->{type} eq 'LBO') { $area_ids_to_list{ $first_council->{id} } = 1; @category_options = ( @@ -589,6 +591,7 @@ sub setup_categories_and_councils : Private { $c->stash->{category_options} = \@category_options; $c->stash->{category_extras} = \%category_extras; $c->stash->{category_extras_json} = encode_json \%category_extras; + $c->stash->{extra_name_info} = $first_council->{id} == 2482 ? 1 : 0; my @missing_details_councils = grep { !$area_ids_to_list{$_} } # @@ -733,7 +736,7 @@ sub process_report : Private { $councils = join( ',', @{ $c->stash->{area_ids_to_list} } ) || -1; $report->council( $councils ); - } elsif ( $first_council->{type} eq 'LBO') { + } elsif ( $first_council->{id} != 2482 && $first_council->{type} eq 'LBO') { unless ( Utils::london_categories()->{ $report->category } ) { $c->stash->{field_errors}->{category} = _('Please choose a category'); @@ -785,6 +788,16 @@ sub process_report : Private { }; } + if ( $contacts[0]->area_id == 2482 ) { + for my $field ( qw/ fms_extra_title / ) { + push @extra, { + name => $field, + description => uc( $field), + value => $c->request->param( $field ) || '', + }; + } + } + if ( @extra ) { $c->stash->{report_meta} = \@extra; $report->extra( \@extra ); @@ -916,6 +929,8 @@ sub check_for_errors : Private { %{ $c->stash->{report}->check_for_errors }, ); + # FIXME: need to check for required bromley fields here + # if they're got the login details wrong when signing in then # we don't care about the name field even though it's validated # by the user object diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm index 15444f556..772a0b8ee 100644 --- a/perllib/FixMyStreet/App/Controller/Report/Update.pm +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -185,6 +185,13 @@ sub process_update : Private { $update->problem_state( $params{state} ); } + if ( $c->req->param('fms_extra_title') ) { + my %extras = (); + $extras{title} = $c->req->param('fms_extra_title'); + $extras{email_alerts_required} = $c->req->param('add_alert'); + $update->extra( \%extras ); + } + $c->stash->{update} = $update; $c->stash->{add_alert} = $c->req->param('add_alert'); diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 195fe4019..d31bf5510 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -1,3 +1,4 @@ +use utf8; package FixMyStreet::DB::Result::Comment; # Created by DBIx::Class::Schema::Loader @@ -7,7 +8,6 @@ use strict; use warnings; use base 'DBIx::Class::Core'; - __PACKAGE__->load_components("FilterColumn", "InflateColumn::DateTime", "EncodedColumn"); __PACKAGE__->table("comment"); __PACKAGE__->add_columns( @@ -54,29 +54,62 @@ __PACKAGE__->add_columns( { data_type => "boolean", is_nullable => 0 }, "problem_state", { data_type => "text", is_nullable => 1 }, + "external_id", + { data_type => "text", is_nullable => 1 }, + "extra", + { data_type => "text", is_nullable => 1 }, + "send_fail_count", + { data_type => "integer", default_value => 0, is_nullable => 0 }, + "send_fail_reason", + { data_type => "text", is_nullable => 1 }, + "send_fail_timestamp", + { data_type => "timestamp", is_nullable => 1 }, + "whensent", + { data_type => "timestamp", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->belongs_to( - "user", - "FixMyStreet::DB::Result::User", - { id => "user_id" }, - { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, -); -__PACKAGE__->belongs_to( "problem", "FixMyStreet::DB::Result::Problem", { id => "problem_id" }, { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, ); +__PACKAGE__->belongs_to( + "user", + "FixMyStreet::DB::Result::User", + { id => "user_id" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-06-27 10:07:32 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ilLn3dlagg5COdpZDmzrVQ +# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-26 15:44:18 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:nvkElEgSU6XcLd9znSqhmQ + +__PACKAGE__->filter_column( + extra => { + filter_from_storage => sub { + my $self = shift; + my $ser = shift; + return undef unless defined $ser; + my $h = new IO::String($ser); + return RABX::wire_rd($h); + }, + filter_to_storage => sub { + my $self = shift; + my $data = shift; + my $ser = ''; + my $h = new IO::String($ser); + RABX::wire_wr( $data, $h ); + return $ser; + }, + } +); use DateTime::TimeZone; use Image::Size; use Moose; use namespace::clean -except => [ 'meta' ]; +use RABX; with 'FixMyStreet::Roles::Abuser'; diff --git a/perllib/FixMyStreet/DB/Result/Open311conf.pm b/perllib/FixMyStreet/DB/Result/Open311conf.pm index 0a5784560..b9cd432ee 100644 --- a/perllib/FixMyStreet/DB/Result/Open311conf.pm +++ b/perllib/FixMyStreet/DB/Result/Open311conf.pm @@ -1,3 +1,4 @@ +use utf8; package FixMyStreet::DB::Result::Open311conf; # Created by DBIx::Class::Schema::Loader @@ -7,7 +8,6 @@ use strict; use warnings; use base 'DBIx::Class::Core'; - __PACKAGE__->load_components("FilterColumn", "InflateColumn::DateTime", "EncodedColumn"); __PACKAGE__->table("open311conf"); __PACKAGE__->add_columns( @@ -26,13 +26,30 @@ __PACKAGE__->add_columns( { data_type => "text", is_nullable => 1 }, "api_key", { data_type => "text", is_nullable => 1 }, + "send_method", + { data_type => "text", is_nullable => 1 }, + "send_comments", + { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "comment_user_id", + { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->add_unique_constraint("open311conf_area_id_key", ["area_id"]); +__PACKAGE__->belongs_to( + "comment_user", + "FixMyStreet::DB::Result::User", + { id => "comment_user_id" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "CASCADE", + }, +); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-07-29 18:09:25 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ryqCpvwjNtQrZm4I3s0hxg +# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-26 17:03:34 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:fC6Ws8p/pXyjgqfm2LRKsw # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index ce7488703..8c479953b 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -84,6 +84,12 @@ __PACKAGE__->add_columns( { data_type => "boolean", default_value => \"false", is_nullable => 0 }, "geocode", { data_type => "bytea", is_nullable => 1 }, + "send_fail_count", + { data_type => "integer", is_nullable => 1 }, + "send_fail_reason", + { data_type => "text", is_nullable => 1 }, + "send_fail_timestamp", + { data_type => "timestamp", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( @@ -106,8 +112,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-09-19 14:38:43 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:nq8Ufn/SEoDGSrrGlHIxag +# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-16 10:08:56 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:VODeZlWk8l/+IzBBlRNV0A # Add fake relationship to stored procedure table __PACKAGE__->has_one( diff --git a/perllib/Open311.pm b/perllib/Open311.pm index e26e3e4c6..e609fb086 100644 --- a/perllib/Open311.pm +++ b/perllib/Open311.pm @@ -12,10 +12,13 @@ has api_key => ( is => 'ro', isa => 'Str' ); has endpoint => ( is => 'ro', isa => 'Str' ); has test_mode => ( is => 'ro', isa => 'Bool' ); has test_uri_used => ( is => 'rw', 'isa' => 'Str' ); +has test_req_used => ( is => 'rw' ); has test_get_returns => ( is => 'rw' ); -has endpoints => ( is => 'rw', default => sub { { services => 'services.xml', requests => 'requests.xml' } } ); +has endpoints => ( is => 'rw', default => sub { { services => 'services.xml', requests => 'requests.xml', service_request_updates => 'update.xml', update => 'update.xml' } } ); has debug => ( is => 'ro', isa => 'Bool', default => 0 ); has debug_details => ( is => 'rw', 'isa' => 'Str', default => '' ); +has success => ( is => 'rw', 'isa' => 'Bool', default => 0 ); +has error => ( is => 'rw', 'isa' => 'Str', default => '' ); sub get_service_list { my $self = shift; @@ -74,7 +77,9 @@ EOT my $extras = $problem->extra; for my $attr ( @$extras ) { - my $name = sprintf( 'attribute[%s]', $attr->{name} ); + my $attr_name = $attr->{name}; + $attr_name =~ s/fms_extra_//; + my $name = sprintf( 'attribute[%s]', $attr_name ); $params->{ $name } = $attr->{value}; } } @@ -129,6 +134,72 @@ sub get_service_request_id_from_token { } } +sub get_service_request_updates { + my $self = shift; + + my $params = {}; + + 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; +} + +sub post_service_request_update { + my $self = shift; + my $comment = shift; + + my $params = { + update_id_ext => $comment->id, + updated_datetime => $comment->confirmed, + service_request_id => $comment->problem->external_id, + service_request_id_ext => $comment->problem->id, + status => $comment->problem->is_open ? 'OPEN' : 'CLOSED', + email => $comment->user->email, + description => $comment->text, + public_anonymity_required => $comment->anonymous ? 'TRUE' : 'FALSE', + # also need last_name, title + }; + + if ( $comment->extra ) { + $params->{'email_alerts_request'} + = $comment->extra->{email_alerts_requested} ? 'TRUE' : 'FALSE'; + $params->{'title'} = $comment->extra->{title}; + } + + my $response = $self->_post( $self->endpoints->{update}, $params ); + + if ( $response ) { + 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 }; + } + } else { + my $token = $obj->{ request_update }->{ token }; + if ( $token ) { + return $self->get_service_request_id_from_token( $token ); + } + } + } + + warn sprintf( "Failed to submit comment %s over Open311, response - %s\n%s", $comment->id, $response, $self->debug_details ); + return 0; + } +} + sub _get { my $self = shift; my $path = shift; @@ -144,10 +215,17 @@ sub _get { my $content; if ( $self->test_mode ) { + $self->success(1); $content = $self->test_get_returns->{ $path }; $self->test_uri_used( $uri->as_string ); } else { $content = get( $uri->as_string ); + if ( $content == undef ) { + $self->success(0); + $self->error('Failed to fetch'); + } else { + $self->success(1); + } } return $content; @@ -171,17 +249,27 @@ sub _post { $self->debug_details( $self->debug_details . "\nrequest:" . $req->as_string ); my $ua = LWP::UserAgent->new(); - my $res = $ua->request( $req ); + my $res; + + if ( $self->test_mode ) { + $res = $self->test_get_returns->{ $path }; + $self->test_req_used( $req ); + } else { + $res = $ua->request( $req ); + } if ( $res->is_success ) { + $self->success(1); return $res->decoded_content; } else { - warn sprintf( + $self->success(0); + $self->error( sprintf( "request failed: %s\nerror: %s\n%s", $res->status_line, $self->_process_error( $res->decoded_content ), $self->debug_details - ); + ) ); + warn $self->error; return 0; } } diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm new file mode 100644 index 000000000..db9fe9436 --- /dev/null +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -0,0 +1,70 @@ +package Open311::GetServiceRequestUpdates; + +use Moose; +use Open311; +use FixMyStreet::App; + +has council_list => ( is => 'ro' ); +has system_user => ( is => 'ro' ); + +sub update_comments { + my ( $self, $open311, $council_details ) = @_; + + my $requests = $open311->get_service_request_updates( ); + + return 0 unless $open311->success; + + for my $request (@$requests) { + my $request_id = $request->{service_request_id}; + + # If there's no request id then we can't work out + # what problem it belongs to so just skip + next unless $request_id; + + my $problem = + FixMyStreet::App->model('DB::Problem') + ->search( { + external_id => $request_id, + council => { like => '%' . $council_details->{areaid} . '%' }, + } ); + + if (my $p = $problem->first) { + my $c = $p->comments->search( { external_id => $request->{update_id} } ); + + if ( !$c->first ) { + my $comment = FixMyStreet::App->model('DB::Comment')->new( + { + problem => $p, + user => $self->system_user, + external_id => $request->{update_id}, + text => $request->{description}, + mark_fixed => 0, + mark_open => 0, + anonymous => 0, + name => $self->system_user->name + } + ); + $comment->confirm; + + + if ( $p->is_open and $request->{status} eq 'closed' ) { + $p->state( 'fixed - council' ); + $p->update; + + $comment->mark_fixed( 1 ); + } elsif ( ( $p->is_closed || $p->is_fixed ) and $request->{status} eq 'open' ) { + $p->state( 'confirmed' ); + $p->update; + + $comment->mark_open( 1 ); + } + + $comment->insert(); + } + } + } + + return 1; +} + +1; diff --git a/perllib/Open311/PopulateServiceList.pm b/perllib/Open311/PopulateServiceList.pm index cfec9005d..ac355a33c 100644 --- a/perllib/Open311/PopulateServiceList.pm +++ b/perllib/Open311/PopulateServiceList.pm @@ -189,6 +189,12 @@ sub _add_contact_to_meta { print "Fetching meta data for $self->_current_service->{service_code}\n"; 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 } + ]; + } + # turn the data into something a bit more friendly to use my @meta = # remove trailing colon as we add this when we display so we don't want 2 @@ -197,6 +203,26 @@ sub _add_contact_to_meta { sort { $a->{order} <=> $b->{order} } @{ $meta_data->{attributes}->{attribute} }; + # we add these later on from bromley so don't list them here + # as we don't want to display them + if ( $self->_current_council->area_id == 2482 ) { + my %ignore = map { $_ => 1 } qw/ + service_request_id_ext + requested_datetime + report_url + title + last_name + email + easting + northing + report_title + public_anonymity_required + email_alerts_requested + /; + + @meta = grep { ! $ignore{ $_->{ code } } } @meta; + } + $contact->extra( \@meta ); $contact->update; } |