diff options
author | Dave Whiteland <dave@mysociety.org> | 2012-05-29 15:57:41 +0100 |
---|---|---|
committer | Dave Whiteland <dave@mysociety.org> | 2012-05-29 15:57:41 +0100 |
commit | 67da8efc720d2d0bd22bd9fe8655b7e983b35bb4 (patch) | |
tree | 38b8570647124df06c637d4b923f6010211ef328 /perllib/FixMyStreet | |
parent | 40b3a51d33caefa8f5fb97ce9be18ef936c7e260 (diff) | |
parent | 131ff6e9bf3626d6a8fff6ae54669d250148a63a (diff) |
Merge remote branch 'origin/master' into fmb-read-only
Conflicts:
bin/send-reports
perllib/FixMyStreet/Cobrand/Default.pm
perllib/FixMyStreet/Cobrand/FixMyStreet.pm
templates/web/fixmystreet/alert/index.html
templates/web/fixmystreet/around/display_location.html
web/cobrands/fixmystreet/_layout.scss
web/js/map-OpenLayers.js
Diffstat (limited to 'perllib/FixMyStreet')
39 files changed, 1653 insertions, 152 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index 262379b79..3d9f0a511 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -53,6 +53,7 @@ __PACKAGE__->config( 'Plugin::Session' => { # Catalyst::Plugin::Session::Store::DBIC dbic_class => 'DB::Session', expires => 3600 * 24 * 7 * 4, # 4 weeks + cookie_secure => 2, }, 'Plugin::Authentication' => { @@ -180,12 +181,7 @@ sub setup_request { Memcached::set_namespace( FixMyStreet->config('FMS_DB_NAME') . ":" ); - my $map = $host =~ /^osm\./ ? 'OSM' : $c->req->param('map_override'); - #if ($c->sessionid) { - # $map = $c->session->{map}; - # $map = undef unless $map eq 'OSM'; - #} - FixMyStreet::Map::set_map_class( $map ); + FixMyStreet::Map::set_map_class( $cobrand->map_type || $c->req->param('map_override') ); return $c; } diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index 198acade6..998cb83a8 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -8,6 +8,8 @@ use POSIX qw(strftime strcoll); use Digest::MD5 qw(md5_hex); use mySociety::EmailUtil qw(is_valid_email); +use FixMyStreet::SendReport; + =head1 NAME FixMyStreet::App::Controller::Admin- Catalyst Controller @@ -338,7 +340,7 @@ sub update_contacts : Private { } elsif ( $posted eq 'open311' ) { $c->forward('check_token'); - my %params = map { $_ => $c->req->param($_) } qw/open311_id endpoint jurisdiction api_key area_id/; + my %params = map { $_ => $c->req->param($_) || '' } qw/open311_id endpoint jurisdiction api_key area_id send_method send_comments suppress_alerts comment_user_id/; if ( $params{open311_id} ) { my $conf = $c->model('DB::Open311Conf')->find( { id => $params{open311_id} } ); @@ -346,6 +348,10 @@ sub update_contacts : Private { $conf->endpoint( $params{endpoint} ); $conf->jurisdiction( $params{jurisdiction} ); $conf->api_key( $params{api_key} ); + $conf->send_method( $params{send_method} ); + $conf->send_comments( $params{send_comments} || 0); + $conf->suppress_alerts( $params{suppress_alerts} || 0); + $conf->comment_user_id( $params{comment_user_id} || undef ); $conf->update(); @@ -356,6 +362,10 @@ sub update_contacts : Private { $conf->endpoint( $params{endpoint} ); $conf->jurisdiction( $params{jurisdiction} ); $conf->api_key( $params{api_key} ); + $conf->send_method( $params{send_method} ); + $conf->send_comments( $params{send_comments} || 0); + $conf->suppress_alerts( $params{suppress_alerts} || 0); + $conf->comment_user_id( $params{comment_user_id} || undef ); $conf->insert(); @@ -378,6 +388,9 @@ sub display_contacts : Private { $c->stash->{contacts} = $contacts; + my @methods = map { $_ =~ s/FixMyStreet::SendReport:://; $_ } keys %{ FixMyStreet::SendReport->get_senders }; + $c->stash->{send_methods} = \@methods; + my $open311 = $c->model('DB::Open311Conf')->search( { area_id => $area_id } ); @@ -492,7 +505,7 @@ sub search_reports : Path('search_reports') { 'me.id' => $search_n, 'user.email' => { ilike => $like_search }, 'me.name' => { ilike => $like_search }, - title => { ilike => $like_search }, + 'me.title' => { ilike => $like_search }, detail => { ilike => $like_search }, council => { like => $like_search }, cobrand_data => { like => $like_search }, @@ -571,8 +584,7 @@ sub report_edit : Path('report_edit') : Args(1) { } )->first; - $c->detach( '/page_error_404_not_found', - [ _('The requested URL was not found on this server.') ] ) + $c->detach( '/page_error_404_not_found' ) unless $problem; $c->stash->{problem} = $problem; @@ -734,8 +746,7 @@ sub update_edit : Path('update_edit') : Args(1) { } )->first; - $c->detach( '/page_error_404_not_found', - [ _('The requested URL was not found on this server.') ] ) + $c->detach( '/page_error_404_not_found' ) unless $update; $c->forward('get_token'); @@ -1068,7 +1079,7 @@ sub check_token : Private { my ( $self, $c ) = @_; if ( !$c->req->param('token') || $c->req->param('token' ) ne $c->stash->{token} ) { - $c->detach( '/page_error_404_not_found', [ _('The requested URL was not found on this server.') ] ); + $c->detach( '/page_error_404_not_found' ); } return 1; @@ -1238,7 +1249,7 @@ sub check_page_allowed : Private { $page ||= 'summary'; if ( !grep { $_ eq $page } keys %{ $c->stash->{allowed_pages} } ) { - $c->detach( '/page_error_404_not_found', [ _('The requested URL was not found on this server.') ] ); + $c->detach( '/page_error_404_not_found' ); } return 1; diff --git a/perllib/FixMyStreet/App/Controller/Alert.pm b/perllib/FixMyStreet/App/Controller/Alert.pm index 2698c6ac0..6e9ae819c 100644 --- a/perllib/FixMyStreet/App/Controller/Alert.pm +++ b/perllib/FixMyStreet/App/Controller/Alert.pm @@ -191,7 +191,11 @@ sub create_alert : Private { $alert->insert(); } - $alert->confirm() if $c->user && $c->user->id == $alert->user->id; + if ( $c->user && $c->user->id == $alert->user->id ) { + $alert->confirm(); + } else { + $alert->confirmed(0); + } $c->stash->{alert} = $alert; } @@ -470,6 +474,7 @@ sub add_recent_photos : Private { { $c->stash->{photos} = $c->cobrand->recent_photos( + 'alert', $num_photos, $c->stash->{latitude}, $c->stash->{longitude}, @@ -477,7 +482,7 @@ sub add_recent_photos : Private { ); } else { - $c->stash->{photos} = $c->cobrand->recent_photos($num_photos); + $c->stash->{photos} = $c->cobrand->recent_photos('alert', $num_photos); } return 1; diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm index fc6df20c3..f2bb23350 100644 --- a/perllib/FixMyStreet/App/Controller/Around.pm +++ b/perllib/FixMyStreet/App/Controller/Around.pm @@ -186,10 +186,11 @@ sub display_location : Private { @pins = map { # Here we might have a DB::Problem or a DB::Nearby, we always want the problem. my $p = (ref $_ eq 'FixMyStreet::App::Model::DB::Nearby') ? $_->problem : $_; + my $colour = $c->cobrand->pin_colour( $p, 'around' ); { latitude => $p->latitude, longitude => $p->longitude, - colour => 'yellow', # $p->is_fixed ? 'green' : 'red', + colour => $colour, id => $p->id, title => $p->title, } diff --git a/perllib/FixMyStreet/App/Controller/Contact.pm b/perllib/FixMyStreet/App/Controller/Contact.pm index f28d37989..91580f05a 100644 --- a/perllib/FixMyStreet/App/Controller/Contact.pm +++ b/perllib/FixMyStreet/App/Controller/Contact.pm @@ -183,7 +183,7 @@ generally required to stash sub setup_request : Private { my ( $self, $c ) = @_; - $c->stash->{contact_email} = $c->cobrand->contact_email; + $c->stash->{contact_email} = $c->cobrand->contact_email( 'contact' ); $c->stash->{contact_email} =~ s/\@/@/; for my $param (qw/em subject message/) { @@ -205,7 +205,7 @@ Sends the email sub send_email : Private { my ( $self, $c ) = @_; - my $recipient = $c->cobrand->contact_email(); + my $recipient = $c->cobrand->contact_email( 'contact' ); my $recipient_name = $c->cobrand->contact_name(); $c->stash->{host} = $c->req->header('HOST'); diff --git a/perllib/FixMyStreet/App/Controller/Dashboard.pm b/perllib/FixMyStreet/App/Controller/Dashboard.pm new file mode 100644 index 000000000..0273e0eda --- /dev/null +++ b/perllib/FixMyStreet/App/Controller/Dashboard.pm @@ -0,0 +1,152 @@ +package FixMyStreet::App::Controller::Dashboard; +use Moose; +use namespace::autoclean; + +use DateTime; + +BEGIN { extends 'Catalyst::Controller'; } + +=head1 NAME + +FixMyStreet::App::Controller::Dashboard - Catalyst Controller + +=head1 DESCRIPTION + +Catalyst Controller. + +=head1 METHODS + +=cut + +=head2 check_page_allowed + +Checks if we can view this page, and if not redirect to 404. + +=cut + +sub check_page_allowed : Private { + my ( $self, $c ) = @_; + + $c->detach( '/auth/redirect' ) unless $c->user_exists; + + $c->detach( '/page_error_404_not_found' ) + unless $c->user_exists && $c->user->from_council; + + return $c->user->from_council; +} + +=head2 index + +Show the dashboard table. + +=cut + +sub index : Path : Args(0) { + my ( $self, $c ) = @_; + + my $council = $c->forward('check_page_allowed'); + + # Set up the data for the dropdowns + + my $children = mySociety::MaPit::call('area/children', $council, + type => $mySociety::VotingArea::council_child_types, + ); + $c->stash->{children} = $children; + + # XXX Hmm, this is probably the best way to go + $c->stash->{all_councils} = { $council => { id => $council } }; + $c->forward( '/report/new/setup_categories_and_councils' ); + + # See if we've had anything from the dropdowns + + $c->stash->{ward} = $c->req->param('ward'); + $c->stash->{category} = $c->req->param('category'); + $c->stash->{q_state} = $c->req->param('state'); + + my %where = ( + council => $council, # XXX This will break in a two tier council. Restriction needs looking at... + 'problem.state' => [ FixMyStreet::DB::Result::Problem->visible_states() ], + ); + $where{areas} = { 'like', '%,' . $c->stash->{ward} . ',%' } + if $c->stash->{ward}; + $where{category} = $c->stash->{category} + if $c->stash->{category}; + if ( $c->stash->{q_state} eq 'fixed' ) { + $where{'problem.state'} = [ FixMyStreet::DB::Result::Problem->fixed_states() ]; + } elsif ( $c->stash->{q_state} ) { + $where{'problem.state'} = $c->stash->{q_state} + } + + my %counts; + my $t = DateTime->today; + + $counts{wtd} = $c->forward( 'updates_search', [ { + %where, + 'me.confirmed' => { '>=', $t->subtract( days => $t->dow - 1 ) + } } ] ); + + $counts{week} = $c->forward( 'updates_search', [ { + %where, + 'me.confirmed' => { '>=', DateTime->now->subtract( weeks => 1 ) + } } ] ); + + $counts{weeks} = $c->forward( 'updates_search', [ { + %where, + 'me.confirmed' => { '>=', DateTime->now->subtract( weeks => 4 ) + } } ] ); + + $counts{ytd} = $c->forward( 'updates_search', [ { + %where, + 'me.confirmed' => { '>=', DateTime->today->set( day => 1, month => 1 ) + } } ] ); + + $c->stash->{problems} = \%counts; +} + +sub updates_search : Private { + my ( $self, $c, $params ) = @_; + + my $comments = $c->model('DB::Comment')->search( + $params, + { + group_by => [ 'problem_state' ], + select => [ 'problem_state', { count => 'me.id' } ], + as => [ qw/state state_count/ ], + join => 'problem' + } + ); + + my %counts = + map { ($_->state||'-') => $_->get_column('state_count') } $comments->all; + %counts = + map { $_ => $counts{$_} || 0 } + ('confirmed', 'investigating', 'in progress', 'closed', 'fixed - council', + 'fixed - user', 'fixed', 'unconfirmed', 'hidden', + 'partial', 'planned'); + + $counts{fixed_user} = $c->model('DB::Comment')->search( + { %$params, mark_fixed => 1 }, { join => 'problem' } + )->count; + + $params->{state} = $params->{'problem.state'}; + delete $params->{'problem.state'}; + $counts{total} = $c->cobrand->problems->search( $params )->count; + + return \%counts; +} + +=head1 AUTHOR + +Matthew Somerville + +=head1 LICENSE + +Copyright (c) 2012 UK Citizens Online Democracy. All rights reserved. +Licensed under the Affero GPL. + +=cut + +__PACKAGE__->meta->make_immutable; + +1; + diff --git a/perllib/FixMyStreet/App/Controller/My.pm b/perllib/FixMyStreet/App/Controller/My.pm index 3de83b265..c00264315 100644 --- a/perllib/FixMyStreet/App/Controller/My.pm +++ b/perllib/FixMyStreet/App/Controller/My.pm @@ -48,7 +48,7 @@ sub my : Path : Args(0) { push @$pins, { latitude => $problem->latitude, longitude => $problem->longitude, - colour => $problem->is_fixed ? 'green' : 'red', + colour => $c->cobrand->pin_colour( $problem, 'my' ), id => $problem->id, title => $problem->title, }; diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm index 5d5832b08..a6717fc33 100644 --- a/perllib/FixMyStreet/App/Controller/Photo.pm +++ b/perllib/FixMyStreet/App/Controller/Photo.pm @@ -5,6 +5,7 @@ use namespace::autoclean; BEGIN {extends 'Catalyst::Controller'; } use DateTime::Format::HTTP; +use Digest::SHA1 qw(sha1_hex); use Path::Class; =head1 NAME @@ -136,6 +137,96 @@ sub _crop { return $blobs[0]; } +=head2 process_photo + +Handle the photo - either checking and storing it after an upload or retrieving +it from the cache. + +Store any error message onto 'photo_error' in stash. +=cut + +sub process_photo : Private { + my ( $self, $c ) = @_; + + return + $c->forward('process_photo_upload') + || $c->forward('process_photo_cache') + || 1; # always return true +} + +sub process_photo_upload : Private { + my ( $self, $c ) = @_; + + # check for upload or return + my $upload = $c->req->upload('photo') + || return; + + # check that the photo is a jpeg + my $ct = $upload->type; + $ct =~ s/x-citrix-//; # Thanks, Citrix + # Had a report of a JPEG from an Android 2.1 coming through as a byte stream + unless ( $ct eq 'image/jpeg' || $ct eq 'image/pjpeg' || $ct eq 'application/octet-stream' ) { + $c->log->info('Bad photo tried to upload, type=' . $ct); + $c->stash->{photo_error} = _('Please upload a JPEG image only'); + return; + } + + # get the photo into a variable + my $photo_blob = eval { + my $filename = $upload->tempname; + my $out = `jhead -se -autorot $filename 2>&1`; + die _("Please upload a JPEG image only"."\n") if $out =~ /Not JPEG:/; + my $photo = $upload->slurp; + return $photo; + }; + if ( my $error = $@ ) { + my $format = _( +"That image doesn't appear to have uploaded correctly (%s), please try again." + ); + $c->stash->{photo_error} = sprintf( $format, $error ); + return; + } + + # we have an image we can use - save it to the upload dir for storage + my $cache_dir = dir( $c->config->{UPLOAD_DIR} ); + $cache_dir->mkpath; + unless ( -d $cache_dir && -w $cache_dir ) { + warn "Can't find/write to photo cache directory '$cache_dir'"; + return; + } + + my $fileid = sha1_hex($photo_blob); + $upload->copy_to( file($cache_dir, $fileid . '.jpeg') ); + + # stick the hash on the stash, so don't have to reupload in case of error + $c->stash->{upload_fileid} = $fileid; + + return 1; +} + +=head2 process_photo_cache + +Look for the upload_fileid parameter and check it matches a file on disk. If it +does return true and put fileid on stash, otherwise false. + +=cut + +sub process_photo_cache : Private { + my ( $self, $c ) = @_; + + # get the fileid and make sure it is just a hex number + my $fileid = $c->req->param('upload_fileid') || ''; + $fileid =~ s{[^0-9a-f]}{}gi; + return unless $fileid; + + my $file = file( $c->config->{UPLOAD_DIR}, "$fileid.jpeg" ); + return unless -e $file; + + $c->stash->{upload_fileid} = $fileid; + return 1; +} + + =head1 AUTHOR Struan Donald diff --git a/perllib/FixMyStreet/App/Controller/Questionnaire.pm b/perllib/FixMyStreet/App/Controller/Questionnaire.pm index 6ed7ddd9d..6aa4f7604 100755 --- a/perllib/FixMyStreet/App/Controller/Questionnaire.pm +++ b/perllib/FixMyStreet/App/Controller/Questionnaire.pm @@ -244,7 +244,7 @@ sub process_questionnaire : Private { push @errors, _('Please provide some explanation as to why you\'re reopening this report') if $c->stash->{been_fixed} eq 'No' && $c->stash->{problem}->is_fixed() && !$c->stash->{update}; - $c->forward('/report/new/process_photo'); + $c->forward('/photo/process_photo'); push @errors, $c->stash->{photo_error} if $c->stash->{photo_error}; @@ -294,7 +294,7 @@ sub display : Private { pins => [ { latitude => $problem->latitude, longitude => $problem->longitude, - colour => $problem->is_fixed() ? 'green' : 'red', + colour => $c->cobrand->pin_colour( $problem, 'questionnaire' ), } ], ); } diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm index afe180c29..cca625bd5 100644 --- a/perllib/FixMyStreet/App/Controller/Report.pm +++ b/perllib/FixMyStreet/App/Controller/Report.pm @@ -153,6 +153,36 @@ sub generate_map_tags : Private { return 1; } +sub delete :Local :Args(1) { + my ( $self, $c, $id ) = @_; + + $c->forward( 'load_problem_or_display_error', [ $id ] ); + my $p = $c->stash->{problem}; + + my $uri = $c->uri_for( '/report', $id ); + + return $c->res->redirect($uri) unless $c->user_exists; + + my $council = $c->user->obj->from_council; + return $c->res->redirect($uri) unless $council; + + my %councils = map { $_ => 1 } @{$p->councils}; + return $c->res->redirect($uri) unless $councils{$council}; + + $p->state('hidden'); + $p->lastupdate( \'ms_current_timestamp()' ); + $p->update; + + $c->model('DB::AdminLog')->create( { + admin_user => $c->user->email, + object_type => 'problem', + action => 'state_change', + object_id => $id, + } ); + + return $c->res->redirect($uri); +} + __PACKAGE__->meta->make_immutable; 1; diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index 6d22a0556..37ff1ae76 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -5,7 +5,6 @@ use namespace::autoclean; BEGIN { extends 'Catalyst::Controller'; } use FixMyStreet::Geocode; -use Digest::SHA1 qw(sha1_hex); use Encode; use Image::Magick; use List::MoreUtils qw(uniq); @@ -99,7 +98,7 @@ sub report_new : Path : Args(0) { return unless $c->forward('check_form_submitted'); $c->forward('process_user'); $c->forward('process_report'); - $c->forward('process_photo'); + $c->forward('/photo/process_photo'); return unless $c->forward('check_for_errors'); $c->forward('save_user_and_report'); $c->forward('redirect_or_confirm_creation'); @@ -126,12 +125,16 @@ 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->stash->{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, } ); @@ -156,7 +159,7 @@ sub category_extras_ajax : Path('category_extras') : Args(0) { $c->forward('setup_categories_and_councils'); my $category_extra = ''; - if ( $c->stash->{category_extras}->{ $c->req->param('category') } ) { + if ( $c->stash->{category_extras}->{ $c->req->param('category') } && @{ $c->stash->{category_extras}->{ $c->req->param('category') } } >= 1 ) { $c->stash->{report_meta} = {}; $c->stash->{report} = { category => $c->req->param('category') }; $c->stash->{category_extras} = { $c->req->param('category' ) => $c->stash->{category_extras}->{ $c->req->param('category') } }; @@ -211,7 +214,7 @@ sub report_import : Path('/import') { } # handle the photo upload - $c->forward( 'process_photo_upload' ); + $c->forward( '/photo/process_photo_upload' ); my $fileid = $c->stash->{upload_fileid}; if ( my $error = $c->stash->{photo_error} ) { push @errors, $error; @@ -396,6 +399,13 @@ sub initialize_report : Private { } + if ( $c->req->param('first_name') && $c->req->param('last_name') ) { + $c->stash->{first_name} = $c->req->param('first_name'); + $c->stash->{last_name} = $c->req->param('last_name'); + + $c->req->param( 'name', sprintf( '%s %s', $c->req->param('first_name'), $c->req->param('last_name') ) ); + } + # Capture whether the map was used $report->used_map( $c->req->param('skipped') ? 0 : 1 ); @@ -544,7 +554,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; my @local_categories; @@ -557,7 +567,7 @@ sub setup_categories_and_councils : Private { _('-- Pick a category --'), @local_categories ); - $category_label = _('Category:'); + $category_label = _('Category'); } else { @@ -587,7 +597,7 @@ sub setup_categories_and_councils : Private { if (@category_options) { @category_options = ( _('-- Pick a category --'), @category_options, _('Other') ); - $category_label = _('Category:'); + $category_label = _('Category'); } } @@ -597,6 +607,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{$_} } # @@ -639,9 +650,10 @@ sub process_user : Private { # The user is already signed in if ( $c->user_exists ) { my $user = $c->user->obj; - my %params = map { $_ => scalar $c->req->param($_) } ( 'name', 'phone' ); + my %params = map { $_ => scalar $c->req->param($_) } ( 'name', 'phone', 'fms_extra_title' ); $user->name( Utils::trim_text( $params{name} ) ) if $params{name}; $user->phone( Utils::trim_text( $params{phone} ) ); + $user->title( Utils::trim_text( $params{fms_extra_title} ) ); $report->user( $user ); $report->name( $user->name ); return 1; @@ -649,7 +661,7 @@ sub process_user : Private { # Extract all the params to a hash to make them easier to work with my %params = map { $_ => scalar $c->req->param($_) } - ( 'email', 'name', 'phone', 'password_register' ); + ( 'email', 'name', 'phone', 'password_register', 'fms_extra_title' ); # cleanup the email address my $email = $params{email} ? lc $params{email} : ''; @@ -677,6 +689,7 @@ sub process_user : Private { $report->user->phone( Utils::trim_text( $params{phone} ) ); $report->user->password( Utils::trim_text( $params{password_register} ) ) if $params{password_register}; + $report->user->title( Utils::trim_text( $params{fms_extra_title} ) ); $report->name( Utils::trim_text( $params{name} ) ); return 1; @@ -748,7 +761,7 @@ sub process_report : Private { } $report->council( $first_council->{id} ); - } 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'); @@ -800,6 +813,8 @@ sub process_report : Private { }; } + $c->cobrand->process_extras( $c, \@contacts, \@extra ); + if ( @extra ) { $c->stash->{report_meta} = \@extra; $report->extra( \@extra ); @@ -828,91 +843,6 @@ sub process_report : Private { return 1; } -=head2 process_photo - -Handle the photo - either checking and storing it after an upload or retrieving -it from the cache. - -Store any error message onto 'photo_error' in stash. -=cut - -sub process_photo : Private { - my ( $self, $c ) = @_; - - return - $c->forward('process_photo_upload') - || $c->forward('process_photo_cache') - || 1; # always return true -} - -sub process_photo_upload : Private { - my ( $self, $c ) = @_; - - # check for upload or return - my $upload = $c->req->upload('photo') - || return; - - # check that the photo is a jpeg - my $ct = $upload->type; - unless ( $ct eq 'image/jpeg' || $ct eq 'image/pjpeg' ) { - $c->stash->{photo_error} = _('Please upload a JPEG image only'); - return; - } - - # get the photo into a variable - my $photo_blob = eval { - my $filename = $upload->tempname; - my $out = `jhead -se -autorot $filename`; - my $photo = $upload->slurp; - return $photo; - }; - if ( my $error = $@ ) { - my $format = _( -"That image doesn't appear to have uploaded correctly (%s), please try again." - ); - $c->stash->{photo_error} = sprintf( $format, $error ); - return; - } - - # we have an image we can use - save it to the upload dir for storage - my $cache_dir = dir( $c->config->{UPLOAD_DIR} ); - $cache_dir->mkpath; - unless ( -d $cache_dir && -w $cache_dir ) { - warn "Can't find/write to photo cache directory '$cache_dir'"; - return; - } - - my $fileid = sha1_hex($photo_blob); - $upload->copy_to( file($cache_dir, $fileid . '.jpeg') ); - - # stick the hash on the stash, so don't have to reupload in case of error - $c->stash->{upload_fileid} = $fileid; - - return 1; -} - -=head2 process_photo_cache - -Look for the upload_fileid parameter and check it matches a file on disk. If it -does return true and put fileid on stash, otherwise false. - -=cut - -sub process_photo_cache : Private { - my ( $self, $c ) = @_; - - # get the fileid and make sure it is just a hex number - my $fileid = $c->req->param('upload_fileid') || ''; - $fileid =~ s{[^0-9a-f]}{}gi; - return unless $fileid; - - my $file = file( $c->config->{UPLOAD_DIR}, "$fileid.jpeg" ); - return unless -e $file; - - $c->stash->{upload_fileid} = $fileid; - return 1; -} - =head2 check_for_errors Examine the user and the report for errors. If found put them on stash and @@ -931,6 +861,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 @@ -972,10 +904,12 @@ sub save_user_and_report : Private { name => $report->user->name, phone => $report->user->phone, password => $report->user->password, + title => $report->user->title, }; $report->user->name( undef ); $report->user->phone( undef ); $report->user->password( '', 1 ); + $report->user->title( undef ); $report->user->insert(); $c->log->info($report->user->id . ' created for this report'); } @@ -991,6 +925,7 @@ sub save_user_and_report : Private { name => $report->user->name, phone => $report->user->phone, password => $report->user->password, + title => $report->user->title, }; $report->user->discard_changes(); $c->log->info($report->user->id . ' exists, but is not logged in for this report'); diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm index 15444f556..5b0dad170 100644 --- a/perllib/FixMyStreet/App/Controller/Report/Update.pm +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -23,7 +23,7 @@ sub report_update : Path : Args(0) { $c->forward( '/report/load_problem_or_display_error', [ $c->req->param('id') ] ); $c->forward('process_update'); $c->forward('process_user'); - $c->forward('/report/new/process_photo'); + $c->forward('/photo/process_photo'); $c->forward('check_for_errors') or $c->go( '/report/display', [ $c->req->param('id') ] ); @@ -104,13 +104,18 @@ sub process_user : Private { my $user = $c->user->obj; my $name = scalar $c->req->param('name'); $user->name( Utils::trim_text( $name ) ) if $name; + my $title = scalar $c->req->param('fms_extra_title'); + if ( $title ) { + $c->log->debug( 'user exists and title is ' . $title ); + $user->title( Utils::trim_text( $title ) ); + } $update->user( $user ); return 1; } # Extract all the params to a hash to make them easier to work with my %params = map { $_ => scalar $c->req->param($_) } - ( 'rznvy', 'name', 'password_register' ); + ( 'rznvy', 'name', 'password_register', 'fms_extra_title' ); # cleanup the email address my $email = $params{rznvy} ? lc $params{rznvy} : ''; @@ -136,6 +141,8 @@ sub process_user : Private { if $params{name}; $update->user->password( Utils::trim_text( $params{password_register} ) ) if $params{password_register}; + $update->user->title( Utils::trim_text( $params{fms_extra_title} ) ) + if $params{fms_extra_title}; return 1; } @@ -153,6 +160,15 @@ want to move adding these elsewhere sub process_update : Private { my ( $self, $c ) = @_; + if ( $c->req->param('first_name' ) && $c->req->param('last_name' ) ) { + my $first_name = $c->req->param('first_name'); + my $last_name = $c->req->param('last_name'); + $c->req->param('name', sprintf( '%s %s', $first_name, $last_name ) ); + + $c->stash->{first_name} = $first_name; + $c->stash->{last_name} = $last_name; + } + my %params = map { $_ => scalar $c->req->param($_) } ( 'update', 'name', 'fixed', 'state', 'reopen' ); @@ -185,6 +201,24 @@ 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->{fms_extra_title} = $c->req->param('fms_extra_title'); + } + + if ( $c->stash->{ first_name } && $c->stash->{ last_name } ) { + my $extra = $update->extra || {}; + $extra->{first_name} = $c->stash->{ first_name }; + $extra->{last_name} = $c->stash->{ last_name }; + $update->extra( $extra ); + } + + $c->log->debug( 'name is ' . $c->req->param('name') ); + $c->stash->{update} = $update; $c->stash->{add_alert} = $c->req->param('add_alert'); @@ -268,6 +302,7 @@ sub save_update : Private { } elsif ( $c->user && $c->user->id == $update->user->id ) { # Logged in and same user, so can confirm update straight away + $c->log->debug( 'user exists' ); $update->user->update; $update->confirm; } else { diff --git a/perllib/FixMyStreet/App/Controller/Root.pm b/perllib/FixMyStreet/App/Controller/Root.pm index 9cdf0b523..7f7d7f5fd 100644 --- a/perllib/FixMyStreet/App/Controller/Root.pm +++ b/perllib/FixMyStreet/App/Controller/Root.pm @@ -68,7 +68,7 @@ Forward to the standard 404 error page sub default : Path { my ( $self, $c ) = @_; - $c->detach('/page_error_404_not_found'); + $c->detach('/page_error_404_not_found', []); } =head2 page_error_404_not_found, page_error_410_gone diff --git a/perllib/FixMyStreet/App/Controller/Tokens.pm b/perllib/FixMyStreet/App/Controller/Tokens.pm index b974f94e6..1434838f2 100644 --- a/perllib/FixMyStreet/App/Controller/Tokens.pm +++ b/perllib/FixMyStreet/App/Controller/Tokens.pm @@ -71,6 +71,7 @@ sub confirm_problem : Path('/P') { $problem->user->name( $data->{name} ) if $data->{name}; $problem->user->phone( $data->{phone} ) if $data->{phone}; $problem->user->password( $data->{password}, 1 ) if $data->{password}; + $problem->user->title( $data->{title} ) if $data->{title}; $problem->user->update; } $c->authenticate( { email => $problem->user->email }, 'no_password' ); diff --git a/perllib/FixMyStreet/App/Model/EmailSend.pm b/perllib/FixMyStreet/App/Model/EmailSend.pm index 73086c65f..761856bde 100644 --- a/perllib/FixMyStreet/App/Model/EmailSend.pm +++ b/perllib/FixMyStreet/App/Model/EmailSend.pm @@ -35,7 +35,7 @@ elsif ( my $smtp_host = FixMyStreet->config('SMTP_SMARTHOST') ) { # Email::Send::SMTP $args = { - mailer => 'SMTP', + mailer => 'FixMyStreet::EmailSend', mailer_args => [ Host => $smtp_host ], }; } diff --git a/perllib/FixMyStreet/Cobrand.pm b/perllib/FixMyStreet/Cobrand.pm index 647261e32..b88f6facc 100644 --- a/perllib/FixMyStreet/Cobrand.pm +++ b/perllib/FixMyStreet/Cobrand.pm @@ -109,4 +109,22 @@ sub get_class_for_moniker { return 'FixMyStreet::Cobrand::Default'; } +=head2 exists + + FixMyStreet::Cobrand->exists( $moniker ); + +Given a moniker, returns true if that cobrand is available to us for use + +=cut + +sub exists { + my ( $class, $moniker ) = @_; + + foreach my $avail ( $class->available_cobrand_classes ) { + return 1 if $moniker eq $avail->{moniker}; + } + + return 0; +} + 1; diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm index 75174b638..f7d885da2 100644 --- a/perllib/FixMyStreet/Cobrand/Bromley.pm +++ b/perllib/FixMyStreet/Cobrand/Bromley.pm @@ -1,5 +1,7 @@ package FixMyStreet::Cobrand::Bromley; -use base 'FixMyStreet::Cobrand::UKCouncils'; +use mro 'c3'; +use parent 'FixMyStreet::Cobrand::FixMyStreet'; +use parent 'FixMyStreet::Cobrand::UKCouncils'; use strict; use warnings; @@ -9,6 +11,10 @@ sub council_area { return 'Bromley'; } sub council_name { return 'Bromley Council'; } sub council_url { return 'bromley'; } +sub base_url { + 'https://fix.bromley.gov.uk'; +} + sub path_to_web_templates { my $self = shift; return [ @@ -17,10 +23,16 @@ sub path_to_web_templates { ]; } +sub site_title { + my ($self) = @_; + return "London Borough of Bromley - Report a problem in Bromley\x{2019}s streets or parks"; +} + sub disambiguate_location { my $self = shift; return { %{ $self->SUPER::disambiguate_location() }, + town => 'Bromley', centre => '51.366836,0.040623', span => '0.154963,0.24347', bounds => [ '51.289355,-0.081112', '51.444318,0.162358' ], @@ -31,5 +43,46 @@ sub example_places { return ( 'BR1 3UH', 'Glebe Rd, Bromley' ); } +sub map_type { + 'Bromley'; +} + +sub on_map_default_max_pin_age { + return '1 month'; +} + +# Bromley pins always yellow +sub pin_colour { + my ( $self, $p, $context ) = @_; + return 'yellow'; +} + +sub recent_photos { + my ( $self, $area, $num, $lat, $lon, $dist ) = @_; + $num = 3 if $num > 3 && $area eq 'alert'; + return $self->problems->recent_photos( $num, $lat, $lon, $dist ); +} + +sub send_questionnaires { + return 0; +} + +sub ask_ever_reported { + return 0; +} + +sub process_extras { + my $self = shift; + $self->SUPER::process_extras( @_, [ 'first_name', 'last_name' ] ); +} + +sub contact_email { + my $self = shift; + my $type = shift || ''; + return join( '@', 'info', 'bromley.gov.uk' ) if $type eq 'contact'; + return $self->next::method(); +} +sub contact_name { 'Bromley Council (do not reply)'; } + 1; diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index e7359d33e..be024f134 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -173,6 +173,7 @@ EASTING and NORTHING. sub recent_photos { my $self = shift; + my $area = shift; return $self->problems->recent_photos(@_); } @@ -350,6 +351,17 @@ Return the title to be used in page heads. sub site_title { 'FixMyStreet' } +=head2 map_type + +Return an override type of map if necessary. + +=cut +sub map_type { + my $self = shift; + return 'OSM' if $self->{c}->req->uri->host =~ /^osm\./; + return; +} + =head2 on_map_list_limit Return the maximum number of items to be given in the list of reports on the map @@ -576,6 +588,14 @@ first time they' ve reported a problem sub ask_ever_reported { 1 } +=head2 send_questionnaires + +Return a boolean indicating whether people should be sent questionnaire emails. + +=cut + +sub send_questionnaires { 1 } + =head2 admin_pages List of names of pages to display on the admin interface @@ -740,6 +760,8 @@ sub council_rss_alert_options { ( $ward->{id_name} = $ward->{short_name} ) =~ tr/+/_/; } } + $council->{name} = 'London Borough of Bromley' + if $council->{name} eq 'Bromley Council'; push @options, { @@ -916,6 +938,8 @@ Get stats to display on the council reports page sub get_report_stats { return 0; } +sub get_council_sender { return 'Email' }; + sub example_places { return [ 'B2 4QA', 'Tib St, Manchester' ]; } @@ -936,7 +960,21 @@ If set to an arrayref, will plot those area ID(s) from mapit on all the /around =cut -sub areas_on_around { +sub areas_on_around {} + +sub process_extras {} + +=head 2 pin_colour + +Returns the colour of pin to be used for a particular report +(so perhaps different depending upon the age of the report). + +=cut +sub pin_colour { + my ( $self, $p, $context ) = @_; + #return 'green' if time() - $p->confirmed_local->epoch < 7 * 24 * 60 * 60; + return 'yellow' if $context eq 'around'; + return $p->is_fixed ? 'green' : 'red'; } 1; diff --git a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm index f6571fed5..aba58495e 100644 --- a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm +++ b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm @@ -14,6 +14,21 @@ sub enter_postcode_text { return _("Enter a nearby GB postcode, or street name and area"); } +sub get_council_sender { + my ( $self, $area_id, $area_info ) = @_; + + my $send_method; + + my $council_config = FixMyStreet::App->model("DB::Open311conf")->search( { area_id => $area_id } )->first; + $send_method = $council_config->send_method if $council_config; + + return $send_method if $send_method; + + return 'London' if $area_info->{type} eq 'LBO'; + + return 'Email'; +} + sub generate_problem_banner { my ( $self, $problem ) = @_; @@ -40,5 +55,33 @@ sub generate_problem_banner { return $banner; } +sub process_extras { + my $self = shift; + my $ctx = shift; + my $contacts = shift; + my $extra = shift; + my $fields = shift || []; + + if ( $contacts->[0]->area_id == 2482 ) { + my @fields = ( 'fms_extra_title', @$fields ); + for my $field ( @fields ) { + my $value = $ctx->request->param( $field ); + + if ( !$value ) { + $ctx->stash->{field_errors}->{ $field } = _('This information is required'); + } + push @$extra, { + name => $field, + description => uc( $field), + value => $value || '', + }; + } + + if ( $ctx->request->param('fms_extra_title') ) { + $ctx->stash->{fms_extra_title} = $ctx->request->param('fms_extra_title'); + $ctx->stash->{extra_name_info} = 1; + } + } +} 1; diff --git a/perllib/FixMyStreet/Cobrand/UKCouncils.pm b/perllib/FixMyStreet/Cobrand/UKCouncils.pm index 69c4b18d4..8a6954418 100644 --- a/perllib/FixMyStreet/Cobrand/UKCouncils.pm +++ b/perllib/FixMyStreet/Cobrand/UKCouncils.pm @@ -7,6 +7,10 @@ use warnings; use Carp; use URI::Escape; +sub is_council { + 1; +} + sub site_restriction { my $self = shift; return ( "and council='" . $self->council_id . "'", $self->council_url, { council => sprintf('%d', $self->council_id) } ); @@ -77,10 +81,24 @@ sub all_councils_report { } sub recent_photos { - my ( $self, $num, $lat, $lon, $dist ) = @_; + my ( $self, $area, $num, $lat, $lon, $dist ) = @_; $num = 2 if $num == 3; return $self->problems->recent_photos( $num, $lat, $lon, $dist ); } -1; +sub get_council_sender { + my ( $self, $area_id, $area_info ) = @_; + + my $send_method; + + my $council_config = FixMyStreet::App->model("DB::Open311conf")->search( { area_id => $area_id } )->first; + $send_method = $council_config->send_method if $council_config; + return $send_method if $send_method; + + return 'London' if $area_info->{type} eq 'LBO'; + + return 'Email'; +} + +1; diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 5b45c63a8..91695d7d0 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -54,6 +54,18 @@ __PACKAGE__->add_columns( { data_type => "boolean", default_value => \"false", 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( @@ -70,13 +82,34 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-08 17:19:55 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:E+96vo/AB0zz1jAEPj/OKw +# 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 742a12ebd..c95b0c8f2 100644 --- a/perllib/FixMyStreet/DB/Result/Open311conf.pm +++ b/perllib/FixMyStreet/DB/Result/Open311conf.pm @@ -28,13 +28,30 @@ __PACKAGE__->add_columns( { 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 }, + "suppress_alerts", + { data_type => "boolean", default_value => \"false", is_nullable => 0 }, ); __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.07017 @ 2012-03-08 17:19:55 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ClYnPB2gsKapnfHuco5d/w +# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-05-11 13:30:31 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ByJbRe/Y/9Z1WHdG8kaIHg # 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 192e539bd..9cbaef6c2 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -85,7 +85,7 @@ __PACKAGE__->add_columns( "geocode", { data_type => "bytea", is_nullable => 1 }, "send_fail_count", - { data_type => "integer", default_value => 0, is_nullable => 0 }, + { data_type => "integer", is_nullable => 1 }, "send_fail_reason", { data_type => "text", is_nullable => 1 }, "send_fail_timestamp", @@ -578,7 +578,7 @@ sub body { # Note: this only makes sense when called on a problem that has been sent! sub can_display_external_id { my $self = shift; - if ($self->external_id && $self->send_method_used eq 'barnet') { + if ($self->external_id && $self->send_method_used && $self->send_method_used eq 'barnet') { return 1; } return 0; @@ -704,6 +704,17 @@ sub update_from_open311_service_request { return 1; } +sub update_send_failed { + my $self = shift; + my $msg = shift; + + $self->update( { + send_fail_count => $self->send_fail_count + 1, + send_fail_timestamp => \'ms_current_timestamp()', + send_fail_reason => $msg + } ); +} + # we need the inline_constructor bit as we don't inherit from Moose __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm index e13d88b88..7f43d1a52 100644 --- a/perllib/FixMyStreet/DB/Result/User.pm +++ b/perllib/FixMyStreet/DB/Result/User.pm @@ -30,6 +30,8 @@ __PACKAGE__->add_columns( { data_type => "integer", is_nullable => 1 }, "flagged", { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "title", + { data_type => "text", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->add_unique_constraint("users_email_key", ["email"]); @@ -46,6 +48,12 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); __PACKAGE__->has_many( + "open311confs", + "FixMyStreet::DB::Result::Open311conf", + { "foreign.comment_user_id" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); +__PACKAGE__->has_many( "problems", "FixMyStreet::DB::Result::Problem", { "foreign.user_id" => "self.id" }, @@ -53,8 +61,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-08 17:19:55 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:tM1LUGrqDeQnF4BDgnYXGQ +# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-05-01 16:20:29 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:LKi8u5IYnHW1+Mez64nvGg __PACKAGE__->add_columns( "password" => { @@ -172,4 +180,22 @@ sub belongs_to_council { return 0; } +=head2 split_name + + $name = $user->split_name; + printf( 'Welcome %s %s', $name->{first}, $name->{last} ); + +Returns a hashref with first and last keys with first name(s) and last name. +NB: the spliting algorithm is extremely basic. + +=cut + +sub split_name { + my $self = shift; + + my ($first, $last) = $self->name =~ /^(\S*)(?: (.*))?$/; + + return { first => $first || '', last => $last || '' }; +} + 1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index 3557e77c7..a7738becf 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -4,6 +4,16 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; +use CronFns; + +use Utils; +use mySociety::Config; +use mySociety::EmailUtil; +use mySociety::MaPit; + +use FixMyStreet::App; +use FixMyStreet::SendReport; + my $site_restriction; my $site_key; @@ -209,4 +219,209 @@ sub categories_summary { return \%categories; } +sub send_reports { + # Set up site, language etc. + my ($verbose, $nomail) = CronFns::options(); + my $base_url = mySociety::Config::get('BASE_URL'); + my $site = CronFns::site($base_url); + + my $unsent = FixMyStreet::App->model("DB::Problem")->search( { + state => [ 'confirmed', 'fixed' ], + whensent => undef, + council => { '!=', undef }, + } ); + my (%notgot, %note); + + my $send_report = FixMyStreet::SendReport->new(); + my $senders = $send_report->get_senders; + my %sending_skipped_by_method; + + while (my $row = $unsent->next) { + + my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new(); + + # Cobranded and non-cobranded messages can share a database. In this case, the conf file + # should specify a vhost to send the reports for each cobrand, so that they don't get sent + # more than once if there are multiple vhosts running off the same database. The email_host + # call checks if this is the host that sends mail for this cobrand. + next unless $cobrand->email_host(); + $cobrand->set_lang_and_domain($row->lang, 1); + if ( $row->is_from_abuser ) { + $row->update( { state => 'hidden' } ); + next; + } + + # Template variables for the email + my $email_base_url = $cobrand->base_url_for_emails($row->cobrand_data); + my %h = map { $_ => $row->$_ } qw/id title detail name category latitude longitude used_map/; + map { $h{$_} = $row->user->$_ } qw/email phone/; + $h{confirmed} = DateTime::Format::Pg->format_datetime( $row->confirmed->truncate (to => 'second' ) ); + + $h{query} = $row->postcode; + $h{url} = $email_base_url . '/report/' . $row->id; + $h{phone_line} = $h{phone} ? _('Phone:') . " $h{phone}\n\n" : ''; + if ($row->photo) { + $h{has_photo} = _("This web page also contains a photo of the problem, provided by the user.") . "\n\n"; + $h{image_url} = $email_base_url . '/photo/' . $row->id . '.full.jpeg'; + } else { + $h{has_photo} = ''; + $h{image_url} = ''; + } + $h{fuzzy} = $row->used_map ? _('To view a map of the precise location of this issue') + : _('The user could not locate the problem on a map, but to see the area around the location they entered'); + $h{closest_address} = ''; + + # If we are in the UK include eastings and northings, and nearest stuff + $h{easting_northing} = ''; + if ( $cobrand->country eq 'GB' ) { + + ( $h{easting}, $h{northing} ) = Utils::convert_latlon_to_en( $h{latitude}, $h{longitude} ); + + # email templates don't have conditionals so we need to farmat this here + $h{easting_northing} # + = "Easting: $h{easting}\n\n" # + . "Northing: $h{northing}\n\n"; + + } + + if ( $row->used_map ) { + $h{closest_address} = $cobrand->find_closest( $h{latitude}, $h{longitude}, $row ); + } + + my %reporters = (); + my (@to, @recips, $template, $areas_info, $sender_count ); + if ($site eq 'emptyhomes') { + + my $council = $row->council; + $areas_info = mySociety::MaPit::call('areas', $council); + my $name = $areas_info->{$council}->{name}; + my $sender = "FixMyStreet::SendReport::EmptyHomes"; + $reporters{ $sender } = $sender->new() unless $reporters{$sender}; + $reporters{ $sender }->add_council( $council, $name ); + $template = Utils::read_file("$FindBin::Bin/../templates/email/emptyhomes/" . $row->lang . "/submit.txt"); + + } else { + + # XXX Needs locks! + my @all_councils = split /,|\|/, $row->council; + my ($councils, $missing) = $row->council =~ /^([\d,]+)(?:\|([\d,]+))?/; + my @councils = split(/,/, $councils); + $areas_info = mySociety::MaPit::call('areas', \@all_councils); + my @dear; + + foreach my $council (@councils) { + my $name = $areas_info->{$council}->{name}; + + my $sender = $cobrand->get_council_sender( $council, $areas_info->{$council} ); + $sender = "FixMyStreet::SendReport::$sender"; + + if ( ! exists $senders->{ $sender } ) { + warn "No such sender [ $sender ] for council $name ( $council )"; + next; + } + $reporters{ $sender } ||= $sender->new(); + + if ( $reporters{ $sender }->should_skip( $row ) ) { + $sending_skipped_by_method{ $sender }++ if + $reporters{ $sender }->skipped; + } else { + push @dear, $name; + $reporters{ $sender }->add_council( $council, $name ); + } + } + + $template = 'submit.txt'; + $template = 'submit-brent.txt' if $row->council eq 2488 || $row->council eq 2237; + my $template_path = FixMyStreet->path_to( "templates", "email", $cobrand->moniker, $template )->stringify; + $template_path = FixMyStreet->path_to( "templates", "email", "default", $template )->stringify + unless -e $template_path; + $template = Utils::read_file( $template_path ); + + if ($h{category} eq _('Other')) { + $h{category_footer} = _('this type of local problem'); + $h{category_line} = ''; + } else { + $h{category_footer} = "'" . $h{category} . "'"; + $h{category_line} = sprintf(_("Category: %s"), $h{category}) . "\n\n"; + } + + $h{councils_name} = join(_(' and '), @dear); + if ($h{category} eq _('Other')) { + $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to both councils covering the location of the problem, as the user did not categorise it; please ignore it if you're not the correct council to deal with the issue, or let us know what category of problem this is so we can add it to our system.") . " ]\n\n" + : ''; + } else { + $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to several councils covering the location of the problem, as the category selected is provided for all of them; please ignore it if you're not the correct council to deal with the issue.") . " ]\n\n" + : ''; + } + $h{missing} = ''; + if ($missing) { + my $name = $areas_info->{$missing}->{name}; + $h{missing} = '[ ' + . sprintf(_('We realise this problem might be the responsibility of %s; however, we don\'t currently have any contact details for them. If you know of an appropriate contact address, please do get in touch.'), $name) + . " ]\n\n"; + } + + $sender_count = scalar @dear; + } + + unless ( keys %reporters ) { + die 'Report not going anywhere for ID ' . $row->id . '!'; + } + + next unless $sender_count; + + if (mySociety::Config::get('STAGING_SITE')) { + # on a staging server send emails to ourselves rather than the councils + my @testing_councils = split( '\|', mySociety::Config::get('TESTING_COUNCILS') ); + unless ( grep { $row->council eq $_ } @testing_councils ) { + %reporters = ( + 'FixMyStreet::SendReport::Email' => $reporters{ 'FixMyStreet::SendReport::Email' } || FixMyStreet::SendReport::Email->new() + ); + } + } + + # Multiply results together, so one success counts as a success. + my $result = -1; + + for my $sender ( keys %reporters ) { + $result *= $reporters{ $sender }->send( + $row, \%h, \@to, $template, \@recips, $nomail, $areas_info + ); + } + + if ($result == mySociety::EmailUtil::EMAIL_SUCCESS) { + $row->update( { + whensent => \'ms_current_timestamp()', + lastupdate => \'ms_current_timestamp()', + } ); + } else { + my @errors; + for my $sender ( keys %reporters ) { + unless ( $reporters{ $sender }->success ) { + push @errors, $reporters{ $sender }->error; + } + } + $row->update_send_failed( join( '|', @errors ) ); + } + } + + if ($verbose) { + print "Council email addresses that need checking:\n" if keys %notgot; + foreach my $e (keys %notgot) { + foreach my $c (keys %{$notgot{$e}}) { + print $notgot{$e}{$c} . " problem, to $e category $c (" . $note{$e}{$c}. ")\n"; + } + } + if (keys %sending_skipped_by_method) { + my $c = 0; + print "\nProblem reports that send-reports did not attempt to send the following:\n"; + foreach my $send_method (sort keys %sending_skipped_by_method) { + printf " %-24s %4d\n", "$send_method:", $sending_skipped_by_method{$send_method}; + $c+=$sending_skipped_by_method{$send_method}; + } + printf " %-24s %4d\n", "Total:", $c; + } + } +} + 1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm b/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm index 0cf01b6d1..753eb2084 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm @@ -48,6 +48,9 @@ sub send_questionnaires_period { my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new(); $cobrand->set_lang_and_domain($row->lang, 1); + # Not all cobrands send questionnaires + next unless $cobrand->send_questionnaires; + # Cobranded and non-cobranded messages can share a database. In this case, the conf file # should specify a vhost to send the reports for each cobrand, so that they don't get sent # more than once if there are multiple vhosts running off the same database. The email_host diff --git a/perllib/FixMyStreet/EmailSend.pm b/perllib/FixMyStreet/EmailSend.pm new file mode 100644 index 000000000..61d8a70c2 --- /dev/null +++ b/perllib/FixMyStreet/EmailSend.pm @@ -0,0 +1,12 @@ +package FixMyStreet::EmailSend; +use base Email::Send::SMTP; + +sub get_env_sender { + # Should really use cobrand's contact_email function, but not sure how + # best to access that from in here. + my $sender = FixMyStreet->config('CONTACT_EMAIL'); + $sender =~ s/team/fms-DO-NOT-REPLY/; + return $sender; +} + +1; diff --git a/perllib/FixMyStreet/Geocode/Bing.pm b/perllib/FixMyStreet/Geocode/Bing.pm index a24f7c102..3bbb9dcdc 100644 --- a/perllib/FixMyStreet/Geocode/Bing.pm +++ b/perllib/FixMyStreet/Geocode/Bing.pm @@ -44,8 +44,6 @@ sub string { if (!$js) { return { error => _('Sorry, we could not parse that location. Please try again.') }; - } elsif ($js =~ /BT\d/ && $params->{bing_country} eq 'United Kingdom') { - return { error => _("We do not currently cover Northern Ireland, I'm afraid.") }; } $js = JSON->new->utf8->allow_nonref->decode($js); @@ -54,11 +52,15 @@ sub string { } my $results = $js->{resourceSets}->[0]->{resources}; - my ( $error, @valid_locations, $latitude, $longitude ); + my ( $error, @valid_locations, $latitude, $longitude, $ni ); foreach (@$results) { my $address = $_->{name}; next unless $_->{address}->{countryRegion} eq $params->{bing_country}; + if ($params->{bing_country} eq 'United Kingdom' && $_->{address}{adminDistrict} eq 'Northern Ireland') { + $ni = 1; + next; + } # Getting duplicate, yet different, results from Bing sometimes next if @valid_locations @@ -80,6 +82,10 @@ sub string { push (@valid_locations, $_); } + if ($ni && !scalar @valid_locations) { + return { error => _("We do not currently cover Northern Ireland, I'm afraid.") }; + } + return { latitude => $latitude, longitude => $longitude } if scalar @valid_locations == 1; return { error => $error }; } diff --git a/perllib/FixMyStreet/Map/Bromley.pm b/perllib/FixMyStreet/Map/Bromley.pm new file mode 100644 index 000000000..20821236f --- /dev/null +++ b/perllib/FixMyStreet/Map/Bromley.pm @@ -0,0 +1,22 @@ +#!/usr/bin/perl +# +# FixMyStreet:Map::Bromley +# Bromley have slightly different tiles, with trees etc. +# +# Copyright (c) 2012 UK Citizens Online Democracy. All rights reserved. +# Email: matthew@mysociety.org; WWW: http://www.mysociety.org/ + +package FixMyStreet::Map::Bromley; +use base 'FixMyStreet::Map::FMS'; + +use strict; + +sub map_type { + return '[ [ "", "a-" ], "https://{S}fix.bromley.gov.uk/tilma" ]'; +} + +sub map_tile_base { + '-', "https://%sfix.bromley.gov.uk/tilma/%d/%d/%d.png"; +} + +1; diff --git a/perllib/FixMyStreet/Map/FMS.pm b/perllib/FixMyStreet/Map/FMS.pm index 24842c861..b1dd29002 100644 --- a/perllib/FixMyStreet/Map/FMS.pm +++ b/perllib/FixMyStreet/Map/FMS.pm @@ -37,23 +37,28 @@ sub get_quadkey { return $key; } +sub map_tile_base { + '.', "http://%stilma.mysociety.org/sv/%d/%d/%d.png"; +} + sub map_tiles { my ($self, $x, $y, $z) = @_; if ($z >= 16) { + my ($tile_sep, $tile_base) = $self->map_tile_base; return [ - "http://a.tilma.mysociety.org/sv/$z/" . ($x-1) . "/" . ($y-1) . ".png", - "http://b.tilma.mysociety.org/sv/$z/$x/" . ($y-1) . ".png", - "http://c.tilma.mysociety.org/sv/$z/" . ($x-1) . "/$y.png", - "http://tilma.mysociety.org/sv/$z/$x/$y.png", + sprintf($tile_base, 'a' . $tile_sep, $z, $x-1, $y-1), + sprintf($tile_base, 'b' . $tile_sep, $z, $x, $y-1), + sprintf($tile_base, 'c' . $tile_sep, $z, $x-1, $y), + sprintf($tile_base, '', $z, $x, $y), ]; } else { my $url = "g=701"; $url .= "&productSet=mmOS" if $z > 10; return [ - "http://ecn.t0.tiles.virtualearth.net/tiles/r" . get_quadkey($x-1, $y-1, $z) . ".png?$url", - "http://ecn.t1.tiles.virtualearth.net/tiles/r" . get_quadkey($x, $y-1, $z) . ".png?$url", - "http://ecn.t2.tiles.virtualearth.net/tiles/r" . get_quadkey($x-1, $y, $z) . ".png?$url", - "http://ecn.t3.tiles.virtualearth.net/tiles/r" . get_quadkey($x, $y, $z) . ".png?$url", + "//ecn.t0.tiles.virtualearth.net/tiles/r" . get_quadkey($x-1, $y-1, $z) . ".png?$url", + "//ecn.t1.tiles.virtualearth.net/tiles/r" . get_quadkey($x, $y-1, $z) . ".png?$url", + "//ecn.t2.tiles.virtualearth.net/tiles/r" . get_quadkey($x-1, $y, $z) . ".png?$url", + "//ecn.t3.tiles.virtualearth.net/tiles/r" . get_quadkey($x, $y, $z) . ".png?$url", ]; } } diff --git a/perllib/FixMyStreet/Map/OSM.pm b/perllib/FixMyStreet/Map/OSM.pm index 8ca5949d8..119337e37 100644 --- a/perllib/FixMyStreet/Map/OSM.pm +++ b/perllib/FixMyStreet/Map/OSM.pm @@ -109,15 +109,9 @@ sub map_pins { my @pins = map { # Here we might have a DB::Problem or a DB::Nearby, we always want the problem. my $p = (ref $_ eq 'FixMyStreet::App::Model::DB::Nearby') ? $_->problem : $_; - #{ - # latitude => $p->latitude, - # longitude => $p->longitude, - # colour => $p->state eq 'fixed' ? 'green' : 'red', - # id => $p->id, - # title => $p->title, - #} + my $colour = $c->cobrand->pin_colour( $p, 'around' ); [ $p->latitude, $p->longitude, - 'yellow', # $p->is_fixed ? 'green' : 'red', + $colour, $p->id, $p->title ] } @$around_map, @$nearby; diff --git a/perllib/FixMyStreet/SendReport.pm b/perllib/FixMyStreet/SendReport.pm new file mode 100644 index 000000000..915fe4a20 --- /dev/null +++ b/perllib/FixMyStreet/SendReport.pm @@ -0,0 +1,45 @@ +package FixMyStreet::SendReport; + +use Moose; + +use Module::Pluggable + sub_name => 'senders', + search_path => __PACKAGE__, + require => 1; + +has 'councils' => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); +has 'to' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } ); +has 'success' => ( is => 'rw', isa => 'Bool', default => 0 ); +has 'error' => ( is => 'rw', isa => 'Str', default => '' ); +has 'skipped' => ( 'is' => 'rw', isa => 'Str', default => '' ); + + +sub should_skip { + return 0; +} + +sub get_senders { + my $self = shift; + + my %senders = map { $_ => 1 } $self->senders; + + return \%senders; +} + +sub reset { + my $self = shift; + + $self->councils( {} ); + $self->to( [] ); +} + +sub add_council { + my $self = shift; + my $council = shift; + my $name = shift; + + $self->councils->{ $council } = $name; +} + + +1; diff --git a/perllib/FixMyStreet/SendReport/Barnet.pm b/perllib/FixMyStreet/SendReport/Barnet.pm new file mode 100644 index 000000000..9a54dd91d --- /dev/null +++ b/perllib/FixMyStreet/SendReport/Barnet.pm @@ -0,0 +1,222 @@ +package FixMyStreet::SendReport::Barnet; + +use Moose; + +BEGIN { extends 'FixMyStreet::SendReport'; } + +use BarnetInterfaces::service::ZLBB_SERVICE_ORDER; +use Encode; +use Utils; +use mySociety::Config; +use mySociety::Web qw(ent); + +# maximum number of webservice attempts to send before not trying any more (XXX may be better in config?) +use constant SEND_FAIL_RETRIES_CUTOFF => 3; + +# specific council numbers +use constant COUNCIL_ID_BARNET => 2489; +use constant MAX_LINE_LENGTH => 132; + +sub should_skip { + my $self = shift; + my $problem = shift; + + my $council_name = 'Barnet'; + my $err_msg = ""; + + if ($problem->send_fail_count >= SEND_FAIL_RETRIES_CUTOFF) { + $council_name &&= " to $council_name"; + $err_msg = "skipped: problem id=" . $problem->id . " send$council_name has failed " + . $problem->send_fail_count . " times, cutoff is " . SEND_FAIL_RETRIES_CUTOFF; + + $self->skipped( $err_msg ); + + return 1; + } +} + +sub construct_message { + my %h = @_; + my $message = <<EOF; +Subject: $h{title} + +Details: $h{detail} + +$h{fuzzy}, or to provide an update on the problem, please visit the following link: + +$h{url} + +$h{closest_address} +EOF +} + + +sub send { + my ( $self, $problem, $h, $to, $template, $recips, $nomail ) = @_; + + my %h = %$h; + + $h{message} = construct_message(%h); + + my $return = 1; + my $err_msg = ""; + + my $default_kbid = 14; # This is the default, "Street Scene" + my $kbid = sprintf( "%050d", Utils::barnet_categories()->{$h{category}} || $default_kbid); + + my $geo_code = "$h{easting} $h{northing}"; + + my $interface = BarnetInterfaces::service::ZLBB_SERVICE_ORDER->new(); + + my ($nearest_postcode, $nearest_street) = ('', ''); + for ($h{closest_address}) { + $nearest_postcode = sprintf("%-10s", $1) if /Nearest postcode [^:]+: ((\w{1,4}\s?\w+|\w+))/; + # use partial postcode or comma as delimiter, strip leading number (possible letter 221B) off too + # "99 Foo Street, London N11 1XX" becomes Foo Street + # "99 Foo Street N11 1XX" becomes Foo Street + $nearest_street = $1 if /Nearest road [^:]+: (?:\d+\w? )?(.*?)(\b[A-Z]+\d|,|$)/m; + } + my $postcode = mySociety::PostcodeUtil::is_valid_postcode($h{query}) + ? $h{query} : $nearest_postcode; # use given postcode if available + + # note: endpoint can be of form 'https://username:password@url' + my $council_config = FixMyStreet::App->model("DB::Open311conf")->search( { area_id => COUNCIL_ID_BARNET} )->first; + if ($council_config and $council_config->endpoint) { + $interface->set_proxy($council_config->endpoint); + # Barnet web service doesn't like namespaces in the elements so use a prefix + $interface->set_prefix('urn'); + # uncomment these lines to print XML that will be sent rather + # than connecting to the endpoint + #$interface->outputxml(1); + #$interface->no_dispatch(1); + } else { + die "Barnet webservice FAIL: looks like you're missing some config data: no endpoint (URL) found for area_id=" . COUNCIL_ID_BARNET; + } + + eval { + my $result = $interface->Z_CRM_SERVICE_ORDER_CREATE( { + ET_RETURN => { # ignored by server + item => { + TYPE => "", ID => "", NUMBER => "", MESSAGE => "", LOG_NO => "", LOG_MSG_NO => "", + MESSAGE_V1 => "", MESSAGE_V2 => "", MESSAGE_V3 => "", MESSAGE_V4 => "", PARAMETER => "", + ROW => "", FIELD => "", SYSTEM => "", + }, + }, + IT_PROBLEM_DESC => { # MyTypes::TABLE_OF_CRMT_SERVICE_REQUEST_TEXT + item => [ # MyTypes::CRMT_SERVICE_REQUEST_TEXT + map { { TEXT_LINE => $_ } } split_text_with_entities(ent(encode_utf8($h{message})), 132) # char132 + ], + }, + IV_CUST_EMAIL => truncate_string_with_entities(ent(encode_utf8($h{email})), 241), # char241 + IV_CUST_NAME => truncate_string_with_entities(ent(encode_utf8($h{name})), 50), # char50 + IV_KBID => $kbid, # char50 + IV_PROBLEM_ID => $h{id}, # char35 + IV_PROBLEM_LOC => { # MyTypes::BAPI_TTET_ADDRESS_COM + COUNTRY2 => 'GB', # char2 + REGION => "", # char3 + COUNTY => "", # char30 + CITY => "", # char30 + POSTALCODE => $postcode, # char10 + STREET => truncate_string_with_entities(ent(encode_utf8($nearest_street)), 30), # char30 + STREETNUMBER => "", # char5 + GEOCODE => $geo_code, # char32 + }, + IV_PROBLEM_SUB => truncate_string_with_entities(ent(encode_utf8($h{title})), 40), # char40 + }, + ); + if ($result) { + # currently not using this: get_EV_ORDER_GUID (maybe that's the customer number in the CRM) + if (my $barnet_id = $result->get_EV_ORDER_NO()) { + $problem->external_id( $barnet_id ); + $problem->external_body( 'Barnet Borough Council' ); # better to use $problem->body()? + $problem->send_method_used('barnet'); + $return = 0; + } else { + my @returned_items = split /<item[^>]*>/, $result->get_ET_RETURN; + my @messages = (); + foreach my $item (@returned_items) { + if ($item=~/<MESSAGE [^>]*>\s*(\S.*?)<\/MESSAGE>/) { # if there's a non-null MESSAGE in there, grab it + push @messages, $1; # best stab at extracting useful error message back from convoluted response + } + } + push @messages, "service returned no external id" unless @messages; + $err_msg = "Failed (problem id $h{id}): " . join(" \n ", @messages); + } + } else { + my %fault = ( + 'code' => $result->get_faultcode(), + 'actor' => $result->get_faultactor(), + 'string' => $result->get_faultstring(), + 'detail' => $result->get_detail(), # possibly only contains debug info + ); + $fault{$_}=~s/^\s*|\s*$//g foreach keys %fault; + $fault{actor}&&=" (actor: $fault{actor})"; + $fault{'detail'} &&= "\n" . $fault{'detail'}; + $err_msg = "Failed (problem id $h{id}): Fault $fault{code}$fault{actor}\n$fault{string}$fault{detail}"; + } + + }; + print "$err_msg\n" if $err_msg; + if ($@) { + my $e = shift; + print "Caught an error: $@\n"; + } + if ( $return ) { + $self->error( "Error sending to Barnet: $err_msg" ); + } + $self->success( !$return ); + return $return; +} + +# for barnet webservice: max-length fields require truncate and split + +# truncate_string_with_entities +# args: text to truncate +# max number of chars +# returns: string truncated +# Note: must not partially truncate an entity (e.g., &) +sub truncate_string_with_entities { + my ($str, $max_len) = @_; + my $retVal = ""; + foreach my $chunk (split /(\&(?:\#\d+|\w+);)/, $str) { + if ($chunk=~/^\&(\#\d+|\w+);$/){ + my $next = $retVal.$chunk; + last if length $next > $max_len; + $retVal=$next + } else { + $retVal.=$chunk; + if (length $retVal > $max_len) { + $retVal = substr($retVal, 0, $max_len); + last + } + } + } + return $retVal +} + +# split_text_with_entities into lines +# args: text to be broken into lines +# max length (option: uses constant MAX_LINE_LENGTH) +# returns: array of lines +# Must not to split an entity (e.g., &) +# Not worrying about hyphenating here, since a word is only ever split if +# it's longer than the whole line, which is uncommon in genuine problem reports +sub split_text_with_entities { + my ($text, $max_line_length) = @_; + $max_line_length ||= MAX_LINE_LENGTH; + my @lines; + foreach my $line (split "\n", $text) { + while (length $line > $max_line_length) { + if (! ($line =~ s/^(.{1,$max_line_length})\s// # break on a space + or $line =~ s/^(.{1,$max_line_length})(\&(\#\d+|\w+);)/$2/ # break before an entity + or $line =~ s/(.{$max_line_length})//)) { # break the word ruthlessly + $line =~ s/(.*)//; # otherwise gobble whole line (which is now shorter than max length) + } + push @lines, $1; + } + push @lines, $line; + } + return @lines; +} + +1; diff --git a/perllib/FixMyStreet/SendReport/EastHants.pm b/perllib/FixMyStreet/SendReport/EastHants.pm new file mode 100644 index 000000000..681a9d4c4 --- /dev/null +++ b/perllib/FixMyStreet/SendReport/EastHants.pm @@ -0,0 +1,62 @@ +package FixMyStreet::SendReport::EastHants; + +use Moose; + +BEGIN { extends 'FixMyStreet::SendReport'; } + +# export just what we need as error if we use :try +use Error qw(try otherwise); +use Encode; +use mySociety::Web qw(ent); +use EastHantsWSDL; + +sub construct_message { + my %h = @_; + my $message = ''; + $message .= "[ This report was also sent to the district council covering the location of the problem, as the user did not categorise it; please ignore if you're not the correct council to deal with the issue. ]\n\n" + if $h{multiple}; + $message .= <<EOF; +Subject: $h{title} + +Details: $h{detail} + +$h{fuzzy}, or to provide an update on the problem, please visit the following link: + +$h{url} + +$h{closest_address} +EOF + return $message; +} + +sub send { + return if mySociety::Config::get('STAGING_SITE'); + + my ( $self, $row, $h, $to, $template, $recips, $nomail ) = @_; + + # FIXME: should not recreate this each time + my $eh_service; + + $h->{category} = 'Customer Services' if $h->{category} eq 'Other'; + $h->{message} = construct_message( %$h ); + my $return = 1; + $eh_service ||= EastHantsWSDL->on_fault(sub { my($soap, $res) = @_; die ref $res ? $res->faultstring : $soap->transport->status, "\n"; }); + try { + # ServiceName, RemoteCreatedBy, Salutation, FirstName, Name, Email, Telephone, HouseNoName, Street, Town, County, Country, Postcode, Comments, FurtherInfo, ImageURL + my $message = ent(encode_utf8($h->{message})); + my $name = ent(encode_utf8($h->{name})); + my $result = $eh_service->INPUTFEEDBACK( + $h->{category}, 'FixMyStreet', '', '', $name, $h->{email}, $h->{phone}, + '', '', '', '', '', '', $message, 'Yes', $h->{image_url} + ); + $return = 0 if $result eq 'Report received'; + } otherwise { + my $e = shift; + print "Caught an error: $e\n"; + $self->error( "Error sending to East Hants: $e" ); + }; + $self->success( !$return ); + return $return; +} + +1; diff --git a/perllib/FixMyStreet/SendReport/Email.pm b/perllib/FixMyStreet/SendReport/Email.pm new file mode 100644 index 000000000..239bee715 --- /dev/null +++ b/perllib/FixMyStreet/SendReport/Email.pm @@ -0,0 +1,111 @@ +package FixMyStreet::SendReport::Email; + +use Moose; + +BEGIN { extends 'FixMyStreet::SendReport'; } + +use mySociety::EmailUtil; + +sub build_recipient_list { + my $self = shift; + my $row = shift; + my %recips; + + my $all_confirmed = 1; + foreach my $council ( keys %{ $self->councils } ) { + my $contact = FixMyStreet::App->model("DB::Contact")->find( { + deleted => 0, + area_id => $council, + category => $row->category + } ); + + my ($council_email, $confirmed, $note) = ( $contact->email, $contact->confirmed, $contact->note ); + + $council_email = essex_contact($row->latitude, $row->longitude) if $council == 2225; + $council_email = oxfordshire_contact($row->latitude, $row->longitude) if $council == 2237 && $council_email eq 'SPECIAL'; + + unless ($confirmed) { + $all_confirmed = 0; + #$note = 'Council ' . $row->council . ' deleted' + #unless $note; + $council_email = 'N/A' unless $council_email; + #$notgot{$council_email}{$row->category}++; + #$note{$council_email}{$row->category} = $note; + } + + push @{ $self->to }, [ $council_email, $self->councils->{ $council } ]; + $recips{$council_email} = 1; + } + + return () unless $all_confirmed; + return keys %recips; +} + +sub send { + my $self = shift; + my ( $row, $h, $to, $template, $recips, $nomail, $areas_info ) = @_; + + my @recips; + + @recips = $self->build_recipient_list( $row, $areas_info ); + + # on a staging server send emails to ourselves rather than the councils + if (mySociety::Config::get('STAGING_SITE')) { + @recips = ( mySociety::Config::get('CONTACT_EMAIL') ); + } + + return unless @recips; + + my $result = FixMyStreet::App->send_email_cron( + { + _template_ => $template, + _parameters_ => $h, + To => $self->to, + From => [ $row->user->email, $row->name ], + }, + mySociety::Config::get('CONTACT_EMAIL'), + \@recips, + $nomail + ); + + if ( $result == mySociety::EmailUtil::EMAIL_SUCCESS ) { + $self->success(1); + } else { + $self->error( 'Failed to send email' ); + } + + return $result; +} + +# Essex has different contact addresses depending upon the district +# Might be easier if we start storing in the db all areas covered by a point +# Will do for now :) +sub essex_contact { + my $district = _get_district_for_contact(@_); + my $email; + $email = 'eastarea' if $district == 2315 || $district == 2312; + $email = 'midarea' if $district == 2317 || $district == 2314 || $district == 2316; + $email = 'southarea' if $district == 2319 || $district == 2320 || $district == 2310; + $email = 'westarea' if $district == 2309 || $district == 2311 || $district == 2318 || $district == 2313; + die "Returned district $district which is not in Essex!" unless $email; + return "highways.$email\@essexcc.gov.uk"; +} + +# Oxfordshire has different contact addresses depending upon the district +sub oxfordshire_contact { + my $district = _get_district_for_contact(@_); + my $email; + $email = 'northernarea' if $district == 2419 || $district == 2420 || $district == 2421; + $email = 'southernarea' if $district == 2417 || $district == 2418; + die "Returned district $district which is not in Oxfordshire!" unless $email; + return "$email\@oxfordshire.gov.uk"; +} + +sub _get_district_for_contact { + my ( $lat, $lon ) = @_; + my $district = + mySociety::MaPit::call( 'point', "4326/$lon,$lat", type => 'DIS' ); + ($district) = keys %$district; + return $district; +} +1; diff --git a/perllib/FixMyStreet/SendReport/EmptyHomes.pm b/perllib/FixMyStreet/SendReport/EmptyHomes.pm new file mode 100644 index 000000000..9453b4ca5 --- /dev/null +++ b/perllib/FixMyStreet/SendReport/EmptyHomes.pm @@ -0,0 +1,51 @@ +package FixMyStreet::SendReport::EmptyHomes; + +use Moose; +use namespace::autoclean; + +BEGIN { extends 'FixMyStreet::SendReport::Email'; } + +sub build_recipient_list { + my $self = shift; + my $row = shift; + my $areas_info = shift; + my %recips; + + my $all_confirmed = 1; + foreach my $council ( keys %{ $self->councils } ) { + my $contact = FixMyStreet::App->model("DB::Contact")->find( { + deleted => 0, + area_id => $council, + category => 'Empty property', + } ); + + my ($council_email, $confirmed, $note) = ( $contact->email, $contact->confirmed, $contact->note ); + + $council_email = essex_contact($row->latitude, $row->longitude) if $council == 2225; + $council_email = oxfordshire_contact($row->latitude, $row->longitude) if $council == 2237 && $council_email eq 'SPECIAL'; + + unless ($confirmed) { + $all_confirmed = 0; + #$note = 'Council ' . $row->council . ' deleted' + #unless $note; + $council_email = 'N/A' unless $council_email; + #$notgot{$council_email}{$row->category}++; + #$note{$council_email}{$row->category} = $note; + } + + push @{ $self->to }, [ $council_email, $self->councils->{ $council } ]; + $recips{$council_email} = 1; + + my $country = $areas_info->{$council}->{country}; + if ($country eq 'W') { + $recips{ 'shelter@' . mySociety::Config::get('EMAIL_DOMAIN') } = 1; + } else { + $recips{ 'eha@' . mySociety::Config::get('EMAIL_DOMAIN') } = 1; + } + } + + return () unless $all_confirmed; + return keys %recips; +} + +1; diff --git a/perllib/FixMyStreet/SendReport/London.pm b/perllib/FixMyStreet/SendReport/London.pm new file mode 100644 index 000000000..58ecb2375 --- /dev/null +++ b/perllib/FixMyStreet/SendReport/London.pm @@ -0,0 +1,113 @@ +package FixMyStreet::SendReport::London; + +use Moose; + +BEGIN { extends 'FixMyStreet::SendReport'; } + +use Digest::MD5; +use LWP::UserAgent; +use LWP::Simple; + +use Utils; + +sub construct_message { + my %h = @_; + return <<EOF, +A user of FixMyStreet has submitted the following report of a local +problem that they believe might require your attention. + +Subject: $h{title} + +Details: $h{detail} + +$h{fuzzy}, or to provide an update on the problem, please visit the +following link: + +$h{url} + +$h{closest_address} +Yours, +The FixMyStreet team +EOF +} + +sub send { + return if mySociety::Config::get('STAGING_SITE'); + my ( $self, $row, $h, $to, $template, $recips, $nomail ) = @_; + + $h->{message} = construct_message( %$h ); + my $phone = $h->{phone}; + my $mobile = ''; + if ($phone && $phone =~ /^\s*07/) { + $mobile = $phone; + $phone = ''; + } + my ($first, $last) = $h->{name} =~ /^(\S*)(?: (.*))?$/; + my %params = ( + Key => mySociety::Config::get('LONDON_REPORTIT_KEY'), + Signature => Digest::MD5::md5_hex( $h->{confirmed} . mySociety::Config::get('LONDON_REPORTIT_SECRET') ), + Type => Utils::london_categories()->{$h->{category}}, + RequestDate => $h->{confirmed}, + RequestMethod => 'Web', + ExternalId => $h->{url}, + 'Customer.Title' => '', + 'Customer.FirstName' => $first, + 'Customer.Surname' => $last, + 'Customer.Email' => $h->{email}, + 'Customer.Phone' => $phone, + 'Customer.Mobile' => $mobile, + 'ProblemDescription' => $h->{message}, + ); + if ($h->{used_map}) { + $params{'Location.Latitude'} = $h->{latitude}; + $params{'Location.Longitude'} = $h->{longitude}; + } elsif (mySociety::PostcodeUtil::is_valid_postcode($h->{query})) { + # Didn't use map, and entered postcode, so use that. + $params{'Location.Postcode'} = $h->{query}; + } else { + # Otherwise, lat/lon is all we have, even if it's wrong. + $params{'Location.Latitude'} = $h->{latitude}; + $params{'Location.Longitude'} = $h->{longitude}; + } + if ($h->{has_photo}) { + $params{'Document1.Name'} = 'Photograph'; + $params{'Document1.MimeType'} = 'image/jpeg'; + $params{'Document1.URL'} = $h->{image_url}; + $params{'Document1.URLPublic'} = 'true'; + } + my $browser = LWP::UserAgent->new; + my $response = $browser->post( mySociety::Config::get('LONDON_REPORTIT_URL'), \%params ); + my $out = $response->content; + if ($response->code ne 200) { + print "Failed to post $h->{id} to London API, response was " . $response->code . " $out\n"; + $self->error( "Failed to post $h->{id} to London API, response was " . $response->code . " $out" ); + return 1; + } + my ($id) = $out =~ /<caseid>(.*?)<\/caseid>/; + my ($org) = $out =~ /<organisation>(.*?)<\/organisation>/; + my ($team) = $out =~ /<team>(.*?)<\/team>/; + + $org = london_lookup($org); + $row->external_id( $id ); + $row->external_body( $org ); + $row->external_team( $team ); + $self->success(1); + return 0; +} + +sub london_lookup { + my $org = shift || ''; + my $str = "Unknown ($org)"; + open(FP, "$FindBin::Bin/../data/dft.csv"); + while (<FP>) { + /^(.*?),(.*)/; + if ($org eq $1) { + $str = $2; + last; + } + } + close FP; + return $str; +} + +1; diff --git a/perllib/FixMyStreet/SendReport/Open311.pm b/perllib/FixMyStreet/SendReport/Open311.pm new file mode 100644 index 000000000..845a6295c --- /dev/null +++ b/perllib/FixMyStreet/SendReport/Open311.pm @@ -0,0 +1,125 @@ +package FixMyStreet::SendReport::Open311; + +use Moose; +use namespace::autoclean; + +BEGIN { extends 'FixMyStreet::SendReport'; } + +use FixMyStreet::App; +use mySociety::Config; +use DateTime::Format::W3CDTF; +use Open311; + +sub should_skip { + my $self = shift; + my $row = shift; + + if ( $row->send_fail_count > 0 ) { + if ( bromley_retry_timeout($row) ) { + return 1; + } + } +} + +sub send { + my $self = shift; + my ( $row, $h, $to, $template, $recips, $nomail ) = @_; + + my $result = -1; + + foreach my $council ( keys %{ $self->councils } ) { + my $conf = FixMyStreet::App->model("DB::Open311conf")->search( { area_id => $council, endpoint => { '!=', '' } } )->first; + + my $always_send_latlong = 1; + my $send_notpinpointed = 0; + + my $basic_desc = 0; + + # Extra bromley fields + if ( $row->council =~ /2482/ ) { + + my $extra = $row->extra; + if ( $row->used_map || ( !$row->used_map && !$row->postcode ) ) { + push @$extra, { name => 'northing', value => $h->{northing} }; + push @$extra, { name => 'easting', value => $h->{easting} }; + } + push @$extra, { name => 'report_url', value => $h->{url} }; + push @$extra, { name => 'service_request_id_ext', value => $row->id }; + push @$extra, { name => 'report_title', value => $row->title }; + push @$extra, { name => 'public_anonymity_required', value => $row->anonymous ? 'TRUE' : 'FALSE' }; + push @$extra, { name => 'email_alerts_requested', value => 'FALSE' }; # always false as can never request them + push @$extra, { name => 'requested_datetime', value => DateTime::Format::W3CDTF->format_datetime($row->confirmed_local->set_nanosecond(0)) }; + push @$extra, { name => 'email', value => $row->user->email }; + $row->extra( $extra ); + + $always_send_latlong = 0; + $send_notpinpointed = 1; + + $basic_desc = 1; + } + + # FIXME: we've already looked this up before + my $contact = FixMyStreet::App->model("DB::Contact")->find( { + deleted => 0, + area_id => $conf->area_id, + category => $row->category + } ); + + my $open311 = Open311->new( + jurisdiction => $conf->jurisdiction, + endpoint => $conf->endpoint, + api_key => $conf->api_key, + always_send_latlong => $always_send_latlong, + send_notpinpointed => $send_notpinpointed, + basic_description => $basic_desc, + ); + + # non standard west berks end points + if ( $row->council =~ /2619/ ) { + $open311->endpoints( { services => 'Services', requests => 'Requests' } ); + } + + # required to get round issues with CRM constraints + if ( $row->council =~ /2218/ ) { + $row->user->name( $row->user->id . ' ' . $row->user->name ); + } + + my $resp = $open311->send_service_request( $row, $h, $contact->email ); + + # make sure we don't save user changes from above + if ( $row->council =~ /2218/ || $row->council =~ /2482/ ) { + $row->discard_changes(); + } + + if ( $resp ) { + $row->external_id( $resp ); + $result *= 0; + $self->success( 1 ); + } else { + $result *= 1; + # temporary fix to resolve some issues with west berks + if ( $row->council =~ /2619/ ) { + $result *= 0; + } + } + } + + $self->error( 'Failed to send over Open311' ) unless $self->success; + + return $result; +} + +sub bromley_retry_timeout { + my $row = shift; + + my $tz = DateTime::TimeZone->new( name => 'local' ); + my $now = DateTime->now( time_zone => $tz ); + my $diff = $now - $row->send_fail_timestamp; + if ( $diff->in_units( 'minutes' ) < 30 ) { + return 1; + } + + return 0; +} + +1; diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm index 7daf01f56..dc42c1aba 100644 --- a/perllib/FixMyStreet/TestMech.pm +++ b/perllib/FixMyStreet/TestMech.pm @@ -225,6 +225,7 @@ sub form_errors { my $result = scraper { process 'div.form-error', 'errors[]', 'TEXT'; process 'p.form-error', 'errors[]', 'TEXT'; + process 'p.error', 'errors[]', 'TEXT'; } ->scrape( $mech->response ); return $result->{errors} || []; |