diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 90 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin/AreaStats.pm | 27 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Around.pm | 15 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Auth.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Auth/Phone.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Auth/Profile.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report.pm | 6 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Reports.pm | 46 |
8 files changed, 114 insertions, 84 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index 0caa25710..719b04cf6 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -797,6 +797,19 @@ sub reports : Path('reports') { } +sub update_user : Private { + my ($self, $c, $object) = @_; + my $parsed = FixMyStreet::SMS->parse_username($c->get_param('username')); + if ($parsed->{email} || ($parsed->{phone} && $parsed->{may_be_mobile})) { + my $user = $c->model('DB::User')->find_or_create({ $parsed->{type} => $parsed->{username} }); + if ($user->id && $user->id != $object->user->id) { + $object->user( $user ); + return 1; + } + } + return 0; +} + sub report_edit : Path('report_edit') : Args(1) { my ( $self, $c, $id ) = @_; @@ -901,14 +914,7 @@ sub report_edit : Path('report_edit') : Args(1) { $problem->set_inflated_columns(\%columns); $c->forward( '/admin/report_edit_category', [ $problem ] ); - - my $parsed = FixMyStreet::SMS->parse_username($c->get_param('username')); - if ($parsed->{email} || ($parsed->{phone} && $parsed->{phone}->is_mobile)) { - my $user = $c->model('DB::User')->find_or_create({ $parsed->{type} => $parsed->{username} }); - if ($user->id && $user->id != $problem->user->id) { - $problem->user( $user ); - } - } + $c->forward('update_user', [ $problem ]); # Deal with photos my $remove_photo_param = $self->_get_remove_photo_param($c); @@ -950,7 +956,7 @@ Handles changing a problem's category and the complexity that comes with it. =cut sub report_edit_category : Private { - my ($self, $c, $problem) = @_; + my ($self, $c, $problem, $no_comment) = @_; if ((my $category = $c->get_param('category')) ne $problem->category) { my $category_old = $problem->category; @@ -978,16 +984,21 @@ sub report_edit_category : Private { } $problem->bodies_str(join( ',', @new_body_ids )); - $problem->add_to_comments({ - text => '*' . sprintf(_('Category changed from ‘%s’ to ‘%s’'), $category_old, $category) . '*', - created => \'current_timestamp', - confirmed => \'current_timestamp', - user_id => $c->user->id, - name => $c->user->from_body ? $c->user->from_body->name : $c->user->name, - state => 'confirmed', - mark_fixed => 0, - anonymous => 0, - }); + my $update_text = '*' . sprintf(_('Category changed from ‘%s’ to ‘%s’'), $category_old, $category) . '*'; + if ($no_comment) { + $c->stash->{update_text} = $update_text; + } else { + $problem->add_to_comments({ + text => $update_text, + created => \'current_timestamp', + confirmed => \'current_timestamp', + user_id => $c->user->id, + name => $c->user->from_body ? $c->user->from_body->name : $c->user->name, + state => 'confirmed', + mark_fixed => 0, + anonymous => 0, + }); + } } } @@ -1251,14 +1262,7 @@ sub update_edit : Path('update_edit') : Args(1) { $update->anonymous( $c->get_param('anonymous') ); $update->state( $new_state ); - my $parsed = FixMyStreet::SMS->parse_username($c->get_param('username')); - if ($parsed->{email} || ($parsed->{phone} && $parsed->{phone}->is_mobile)) { - my $user = $c->model('DB::User')->find_or_create({ $parsed->{type} => $parsed->{username} }); - if ($user->id && $user->id != $update->user->id) { - $edited = 1; - $update->user( $user ); - } - } + $edited = 1 if $c->forward('update_user', [ $update ]); if ( $new_state eq 'confirmed' and $old_state eq 'unconfirmed' ) { $update->confirmed( \'current_timestamp' ); @@ -1298,6 +1302,18 @@ sub update_edit : Path('update_edit') : Args(1) { return 1; } +sub phone_check : Private { + my ($self, $c, $phone) = @_; + my $parsed = FixMyStreet::SMS->parse_username($phone); + if ($parsed->{phone} && $parsed->{may_be_mobile}) { + return $parsed->{username}; + } elsif ($parsed->{phone}) { + $c->stash->{field_errors}->{phone} = _('Please enter a mobile number'); + } else { + $c->stash->{field_errors}->{phone} = _('Please check your phone number is correct'); + } +} + sub user_add : Path('user_edit') : Args(0) { my ( $self, $c ) = @_; @@ -1329,14 +1345,8 @@ sub user_add : Path('user_edit') : Args(0) { } if ($phone_v) { - my $parsed = FixMyStreet::SMS->parse_username($phone); - if ($parsed->{phone} && $parsed->{phone}->is_mobile) { - $phone = $parsed->{username}; - } elsif ($parsed->{phone}) { - $c->stash->{field_errors}->{phone} = _('Please enter a mobile number'); - } else { - $c->stash->{field_errors}->{phone} = _('Please check your phone number is correct'); - } + my $parsed_phone = $c->forward('phone_check', [ $phone ]); + $phone = $parsed_phone if $parsed_phone; } my $existing_email = $email_v && $c->model('DB::User')->find( { email => $email } ); @@ -1417,14 +1427,8 @@ sub user_edit : Path('user_edit') : Args(1) { } if ($phone_v) { - my $parsed = FixMyStreet::SMS->parse_username($phone); - if ($parsed->{phone} && $parsed->{phone}->is_mobile) { - $phone = $parsed->{username}; - } elsif ($parsed->{phone}) { - $c->stash->{field_errors}->{phone} = _('Please enter a mobile number'); - } else { - $c->stash->{field_errors}->{phone} = _('Please check your phone number is correct'); - } + my $parsed_phone = $c->forward('phone_check', [ $phone ]); + $phone = $parsed_phone if $parsed_phone; } unless ($user->name) { diff --git a/perllib/FixMyStreet/App/Controller/Admin/AreaStats.pm b/perllib/FixMyStreet/App/Controller/Admin/AreaStats.pm index 932631cba..2058ea872 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/AreaStats.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/AreaStats.pm @@ -78,6 +78,8 @@ sub stats : Private { my ($self, $c) = @_; my $date = DateTime->now->subtract(days => 30); + # set it to midnight so we get consistent result through the day + $date->truncate( to => 'day' ); $c->forward('/admin/fetch_contacts'); @@ -145,21 +147,21 @@ sub stats : Private { my $comments = $c->model('DB::Comment')->to_body( $c->stash->{body} )->search( - $params, { - join => 'problem' + %$params, + 'me.id' => { 'in' => \"(select min(id) from comment where me.problem_id=comment.problem_id and problem_state not in ('', 'confirmed') group by problem_state)" }, + }, + { + join => 'problem', + group_by => [ 'problem_state' ], + select => [ 'problem_state', { count => 'me.id' } ], + as => [ qw/problem_state state_count/ ], } ); - # you can have multiple comments with the same problem state so need to only count - # one instance. - my %state_seen = (); while (my $comment = $comments->next) { my $meta_state = $state_map->{$comment->problem_state}; - my $key = $comment->problem->id . "-$meta_state"; - next if $state_seen{$key}; - $c->stash->{$meta_state} += 1; - $state_seen{$key} = 1; + $c->stash->{$meta_state} += $comment->get_column('state_count'); } $params = { @@ -205,7 +207,12 @@ sub stats : Private { join => 'problem' } )->first; - $c->stash->{average} = int( ($comments->get_column('time')||0)/ 60 / 60 / 24 + 0.5 ); + my $raw_average = $comments->get_column('time'); + if (defined $raw_average) { + $c->stash->{average} = int( $raw_average / 60 / 60 / 24 + 0.5 ); + } else { + $c->stash->{average} = -1; + } } sub load_user_body : Private { diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm index 44ccb5259..6af780c35 100644 --- a/perllib/FixMyStreet/App/Controller/Around.pm +++ b/perllib/FixMyStreet/App/Controller/Around.pm @@ -8,6 +8,7 @@ use FixMyStreet::Map; use Encode; use JSON::MaybeXS; use Utils; +use Try::Tiny; =head1 NAME @@ -364,13 +365,17 @@ sub lookup_by_ref : Private { external_id => $ref ]); - if ( $problems->count == 0) { - $c->detach( '/page_error_404_not_found', [] ); - } elsif ( $problems->count == 1 ) { - $c->res->redirect( $c->uri_for( '/report', $problems->first->id ) ); - } else { + my $count = try { + $problems->count; + } catch { + 0; + }; + + if ($count > 1) { $c->stash->{ref} = $ref; $c->stash->{matching_reports} = [ $problems->all ]; + } elsif ($count == 1) { + $c->res->redirect( $c->uri_for( '/report', $problems->first->id ) ); } } diff --git a/perllib/FixMyStreet/App/Controller/Auth.pm b/perllib/FixMyStreet/App/Controller/Auth.pm index b453f593b..80e407147 100644 --- a/perllib/FixMyStreet/App/Controller/Auth.pm +++ b/perllib/FixMyStreet/App/Controller/Auth.pm @@ -119,7 +119,7 @@ sub code_sign_in : Private { my $parsed = FixMyStreet::SMS->parse_username($username); if ($parsed->{type} eq 'phone' && FixMyStreet->config('SMS_AUTHENTICATION')) { - $c->forward('phone/sign_in', [ $parsed->{phone} ]); + $c->forward('phone/sign_in', [ $parsed ]); } else { $c->forward('email_sign_in', [ $parsed->{username} ]); } diff --git a/perllib/FixMyStreet/App/Controller/Auth/Phone.pm b/perllib/FixMyStreet/App/Controller/Auth/Phone.pm index 4e9f92596..e4ffc2205 100644 --- a/perllib/FixMyStreet/App/Controller/Auth/Phone.pm +++ b/perllib/FixMyStreet/App/Controller/Auth/Phone.pm @@ -45,19 +45,19 @@ and sets up the token/etc to deal with the response. =cut sub sign_in : Private { - my ( $self, $c, $phone ) = @_; + my ( $self, $c, $parsed ) = @_; - unless ($phone) { + unless ($parsed->{phone}) { $c->stash->{username_error} = 'other_phone'; return; } - unless ($phone->is_mobile) { + unless ($parsed->{may_be_mobile}) { $c->stash->{username_error} = 'nonmobile'; return; } - (my $number = $phone->format) =~ s/\s+//g; + (my $number = $parsed->{phone}->format) =~ s/\s+//g; if ( FixMyStreet->config('SIGNUPS_DISABLED') && !$c->model('DB::User')->find({ phone => $number }) diff --git a/perllib/FixMyStreet/App/Controller/Auth/Profile.pm b/perllib/FixMyStreet/App/Controller/Auth/Profile.pm index ecf009150..acffd3019 100644 --- a/perllib/FixMyStreet/App/Controller/Auth/Profile.pm +++ b/perllib/FixMyStreet/App/Controller/Auth/Profile.pm @@ -118,14 +118,14 @@ sub change_phone : Path('/auth/change_phone') { # If we've not used a mobile and we're not specifically verifying, # and phone isn't our only verified way of logging in, # then allow change of number (for e.g. landline). - if (!FixMyStreet->config('SMS_AUTHENTICATION') || (!$parsed->{phone}->is_mobile && !$c->stash->{verifying} && $c->user->email_verified)) { + if (!FixMyStreet->config('SMS_AUTHENTICATION') || (!$parsed->{may_be_mobile} && !$c->stash->{verifying} && $c->user->email_verified)) { $c->user->update({ phone => $phone, phone_verified => 0 }); $c->flash->{flash_message} = _('You have successfully added your phone number.'); $c->res->redirect('/my'); $c->detach; } - $c->forward('/auth/phone/sign_in', [ $parsed->{phone} ]); + $c->forward('/auth/phone/sign_in', [ $parsed ]); } sub verify_item : Path('/auth/verify') : Args(1) { diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm index ba9fa11b9..c72c75d3a 100644 --- a/perllib/FixMyStreet/App/Controller/Report.pm +++ b/perllib/FixMyStreet/App/Controller/Report.pm @@ -424,7 +424,11 @@ sub inspect : Private { } if ($permissions->{report_inspect} || $permissions->{report_edit_category}) { - $c->forward( '/admin/report_edit_category', [ $problem ] ); + $c->forward( '/admin/report_edit_category', [ $problem, 1 ] ); + + if ($c->stash->{update_text}) { + $update_text .= "\n\n" . $c->stash->{update_text}; + } # The new category might require extra metadata (e.g. pothole size), so # we need to update the problem with the new values. diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm index 8f8205719..187dfb299 100644 --- a/perllib/FixMyStreet/App/Controller/Reports.pm +++ b/perllib/FixMyStreet/App/Controller/Reports.pm @@ -131,9 +131,10 @@ sub ward : Path : Args(2) { $c->forward('/auth/get_csrf_token'); + my @wards = split /\|/, $ward || ""; $c->forward( 'body_check', [ $body ] ); - $c->forward( 'ward_check', [ $ward ] ) - if $ward; + $c->forward( 'ward_check', [ @wards ] ) + if @wards; $c->forward( 'check_canonical_url', [ $body ] ); $c->forward( 'stash_report_filter_status' ); $c->forward( 'load_and_group_problems' ); @@ -166,7 +167,7 @@ sub ward : Path : Args(2) { my %map_params = ( latitude => @$pins ? $pins->[0]{latitude} : 0, longitude => @$pins ? $pins->[0]{longitude} : 0, - area => $c->stash->{ward} ? $c->stash->{ward}->{id} : [ keys %{$c->stash->{body}->areas} ], + area => [ $c->stash->{wards} ? map { $_->{id} } @{$c->stash->{wards}} : keys %{$c->stash->{body}->areas} ], any_zoom => 1, ); FixMyStreet::Map::display_map( @@ -176,7 +177,7 @@ sub ward : Path : Args(2) { $c->cobrand->tweak_all_reports_map( $c ); # List of wards - if ( !$c->stash->{ward} && $c->stash->{body}->id && $c->stash->{body}->body_areas->first ) { + if ( !$c->stash->{wards} && $c->stash->{body}->id && $c->stash->{body}->body_areas->first ) { my $children = mySociety::MaPit::call('area/children', [ $c->stash->{body}->body_areas->first->area_id ], type => $c->cobrand->area_types_children, ); @@ -343,18 +344,20 @@ sub body_check : Private { =head2 ward_check -This action checks the ward name from a URI exists and is part of the right +This action checks the ward names from a URI exists and are part of the right parent, already found with body_check. It either stores the ward Area if okay, or redirects to the body page if bad. =cut sub ward_check : Private { - my ( $self, $c, $ward ) = @_; + my ( $self, $c, @wards ) = @_; - $ward =~ s/\+/ /g; - $ward =~ s/\.html//; - $ward =~ s{_}{/}g; + foreach (@wards) { + s/\+/ /g; + s/\.html//; + s{_}{/}g; + } # Could be from RSS area, or body... my $parent_id; @@ -366,15 +369,20 @@ sub ward_check : Private { $parent_id = $c->stash->{area}->{id}; } - my $qw = mySociety::MaPit::call('areas', $ward, + my $qw = mySociety::MaPit::call('area/children', [ $parent_id ], type => $c->cobrand->area_types_children, ); + my %names = map { $_ => 1 } @wards; + my @areas; foreach my $area (sort { $a->{name} cmp $b->{name} } values %$qw) { - if ($area->{parent_area} == $parent_id) { - $c->stash->{ward} = $area; - return; - } + push @areas, $area if $names{$area->{name}}; + } + if (@areas) { + $c->stash->{ward} = $areas[0] if @areas == 1; + $c->stash->{wards} = \@areas; + return; } + # Given a false ward name $c->stash->{body} = $c->stash->{area} unless $c->stash->{body}; @@ -448,8 +456,10 @@ sub load_and_group_problems : Private { my $problems = $c->cobrand->problems; - if ($c->stash->{ward}) { - $where->{areas} = { 'like', '%,' . $c->stash->{ward}->{id} . ',%' }; + if ($c->stash->{wards}) { + $where->{areas} = [ + map { { 'like', '%,' . $_->{id} . ',%' } } @{$c->stash->{wards}} + ]; $problems = $problems->to_body($c->stash->{body}); } elsif ($c->stash->{body}) { $problems = $problems->to_body($c->stash->{body}); @@ -510,8 +520,8 @@ sub redirect_body : Private { $url .= "/rss" if $c->stash->{rss}; $url .= '/reports'; $url .= '/' . $c->cobrand->short_name( $c->stash->{body} ); - $url .= '/' . $c->cobrand->short_name( $c->stash->{ward} ) - if $c->stash->{ward}; + $url .= '/' . join('|', map { $c->cobrand->short_name($_) } @{$c->stash->{wards}}) + if $c->stash->{wards}; $c->res->redirect( $c->uri_for($url, $c->req->params ) ); } |