diff options
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Alert.pm | 75 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/My.pm | 41 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 74 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/Update.pm | 67 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 12 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/User.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/Map/FMS.pm | 2 | ||||
-rw-r--r-- | perllib/Utils.pm | 23 |
10 files changed, 215 insertions, 93 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Alert.pm b/perllib/FixMyStreet/App/Controller/Alert.pm index d1759a330..ff92a7d2d 100644 --- a/perllib/FixMyStreet/App/Controller/Alert.pm +++ b/perllib/FixMyStreet/App/Controller/Alert.pm @@ -55,17 +55,14 @@ Target for subscribe form sub subscribe : Path('subscribe') : Args(0) { my ( $self, $c ) = @_; - if ( $c->req->param('rss') ) { - $c->detach('rss'); - } + $c->detach('rss') if $c->req->param('rss'); + # if it exists then it's been submitted so we should # go to subscribe email and let it work out the next step - elsif ( exists $c->req->params->{'rznvy'} ) { - $c->detach('subscribe_email'); - } - elsif ( $c->req->params->{'id'} ) { - $c->go('updates'); - } + $c->detach('subscribe_email') + if exists $c->req->params->{'rznvy'} || $c->req->params->{'alert'}; + + $c->go('updates') if $c->req->params->{'id'}; # shouldn't get to here but if we have then do something sensible $c->go('index'); @@ -116,25 +113,18 @@ Sign up to email alerts sub subscribe_email : Private { my ( $self, $c ) = @_; - my $type = $c->req->param('type'); - $c->stash->{email_type} = 'alert'; + $c->stash->{errors} = []; + $c->forward('process_user'); - my @errors; - push @errors, _('Please enter a valid email address') - unless is_valid_email( $c->req->param('rznvy') ); - push @errors, _('Please select the type of alert you want') + my $type = $c->req->param('type'); + push @{ $c->stash->{errors} }, _('Please select the type of alert you want') if $type && $type eq 'local' && !$c->req->param('feed'); - if (@errors) { - $c->stash->{errors} = \@errors; + if (@{ $c->stash->{errors} }) { $c->go('updates') if $type && $type eq 'updates'; $c->go('list') if $type && $type eq 'local'; $c->go('index'); } - my $email = $c->req->param('rznvy'); - $c->stash->{email} = $email; - $c->forward('process_user'); - if ( $type eq 'updates' ) { $c->forward('set_update_alert_options'); } @@ -200,14 +190,12 @@ sub create_alert : Private { $options->{cobrand_data} = $c->cobrand->extra_update_data(); $options->{lang} = $c->stash->{lang_code}; - if ( $c->user && $c->stash->{alert_user}->in_storage && $c->user->id == $c->stash->{alert_user}->id ) { - $options->{confirmed} = 1; - } - $alert = $c->model('DB::Alert')->new($options); $alert->insert(); } + $alert->confirm() if $c->user && $c->user->id == $alert->user->id; + $c->stash->{alert} = $alert; } @@ -303,6 +291,7 @@ sub send_confirmation_email : Private { $c->send_email( 'alert-confirm.txt', { to => $c->stash->{alert}->user->email } ); + $c->stash->{email_type} = 'alert'; $c->stash->{template} = 'email_sent.html'; } @@ -331,12 +320,46 @@ sub prettify_pc : Private { return 1; } +=head2 process_user + +Fetch/check email address + +=cut + sub process_user : Private { my ( $self, $c ) = @_; - my $email = $c->stash->{email}; + if ( $c->user_exists ) { + $c->stash->{alert_user} = $c->user->obj; + return; + } + + # Extract all the params to a hash to make them easier to work with + my %params = map { $_ => scalar $c->req->param($_) } + ( 'rznvy' ); # , 'password_register' ); + + # cleanup the email address + my $email = $params{rznvy} ? lc $params{rznvy} : ''; + $email =~ s{\s+}{}g; + + push @{ $c->stash->{errors} }, _('Please enter a valid email address') + unless is_valid_email( $email ); + my $alert_user = $c->model('DB::User')->find_or_new( { email => $email } ); $c->stash->{alert_user} = $alert_user; + +# # The user is trying to sign in. We only care about email from the params. +# if ( $c->req->param('submit_sign_in') ) { +# unless ( $c->forward( '/auth/sign_in', [ $email ] ) ) { +# $c->stash->{field_errors}->{password} = _('There was a problem with your email/password combination. Please try again.'); +# return 1; +# } +# my $user = $c->user->obj; +# $c->stash->{alert_user} = $user; +# return 1; +# } +# +# $alert_user->password( Utils::trim_text( $params{password_register} ) ); } =head2 setup_coordinate_rss_feeds diff --git a/perllib/FixMyStreet/App/Controller/My.pm b/perllib/FixMyStreet/App/Controller/My.pm index 79d5c5681..b1359ae03 100644 --- a/perllib/FixMyStreet/App/Controller/My.pm +++ b/perllib/FixMyStreet/App/Controller/My.pm @@ -22,7 +22,48 @@ Catalyst Controller. sub my : Path : Args(0) { my ( $self, $c ) = @_; + $c->detach( '/auth/redirect' ) unless $c->user; + + # Even though front end doesn't yet have it, have it on this page, it's better! + FixMyStreet::Map::set_map_class( 'FMS' ); + + my $p_page = $c->req->params->{p} || 1; + my $u_page = $c->req->params->{u} || 1; + + my $pins = []; + my $problems = {}; + my $rs = $c->user->problems->search( undef, + { rows => 50 } )->page( $p_page ); + + while ( my $problem = $rs->next ) { + push @$pins, { + latitude => $problem->latitude, + longitude => $problem->longitude, + colour => $problem->state eq 'fixed' ? 'green' : 'red', + id => $problem->id, + title => $problem->title, + }; + push @{ $problems->{$problem->state} }, $problem; + } + $c->stash->{problems_pager} = $rs->pager; + $c->stash->{problems} = $problems; + + $rs = $c->user->comments->search( + { state => 'confirmed' }, + { rows => 50 } )->page( $u_page ); + my @updates = $rs->all; + $c->stash->{updates} = \@updates; + $c->stash->{updates_pager} = $rs->pager; + + FixMyStreet::Map::display_map( + $c, + latitude => $pins->[0]{latitude}, + longitude => $pins->[0]{longitude}, + pins => $pins, + any_zoom => 1, + ) + if @$pins; } __PACKAGE__->meta->make_immutable; diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index a6be6c90c..4488ce8cd 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -28,7 +28,10 @@ Create a new report, or complete a partial one . =head2 flow control -submit_problem: true if a problem has been submitted +submit_problem: true if a problem has been submitted, at all. +submit_sign_in: true if the sign in button has been clicked by logged out user. +submit_register: true if the register/confirm by email button has been clicked +by logged out user. =head2 location (required) @@ -90,14 +93,13 @@ sub report_new : Path : Args(0) { $c->forward('generate_map'); # deal with the user and report and check both are happy - return - unless $c->forward('check_form_submitted') - && $c->forward('process_user') - && $c->forward('process_report') - && $c->forward('process_photo') - && $c->forward('check_for_errors') - && $c->forward('save_user_and_report') - && $c->forward('redirect_or_confirm_creation'); + return unless $c->forward('check_form_submitted'); + $c->forward('process_user'); + $c->forward('process_report'); + $c->forward('process_photo'); + return unless $c->forward('check_for_errors'); + $c->forward('save_user_and_report'); + $c->forward('redirect_or_confirm_creation'); } =head2 report_import @@ -548,22 +550,48 @@ Load user from the database or prepare a new one. sub process_user : Private { my ( $self, $c ) = @_; + my $report = $c->stash->{report}; + + # The user is already signed in + if ( $c->user_exists ) { + my $user = $c->user->obj; + my %params = map { $_ => scalar $c->req->param($_) } ( 'name', 'phone' ); + $user->name( Utils::trim_text( $params{name} ) ) if $params{name}; + $user->phone( Utils::trim_text( $params{phone} ) ); + $report->user( $user ); + $report->name( $user->name ); + return 1; + } + # Extract all the params to a hash to make them easier to work with - my %params = # - map { $_ => scalar $c->req->param($_) } # - ( 'email', 'name', 'phone', ); + my %params = map { $_ => scalar $c->req->param($_) } + ( 'email', 'name', 'phone', 'password_register' ); # cleanup the email address my $email = $params{email} ? lc $params{email} : ''; $email =~ s{\s+}{}g; - my $report = $c->stash->{report}; $report->user( $c->model('DB::User')->find_or_new( { email => $email } ) ) unless $report->user; - # set the user's name and phone (if given) + # The user is trying to sign in. We only care about email from the params. + if ( $c->req->param('submit_sign_in') ) { + unless ( $c->forward( '/auth/sign_in' ) ) { + $c->stash->{field_errors}->{password} = _('There was a problem with your email/password combination. Please try again.'); + return 1; + } + my $user = $c->user->obj; + $report->user( $user ); + $report->name( $user->name ); + $c->stash->{field_errors}->{name} = _('You have successfully signed in; please check and confirm your details are accurate:'); + return 1; + } + + # set the user's name, phone, and password $report->user->name( Utils::trim_text( $params{name} ) ) if $params{name}; $report->user->phone( Utils::trim_text( $params{phone} ) ); + $report->user->password( Utils::trim_text( $params{password_register} ) ); + $report->name( Utils::trim_text( $params{name} ) ); return 1; } @@ -584,7 +612,7 @@ sub process_report : Private { map { $_ => scalar $c->req->param($_) } # ( 'title', 'detail', 'pc', # - 'name', 'may_show_name', # + 'may_show_name', # 'category', # 'partial', # ); @@ -606,7 +634,6 @@ sub process_report : Private { Utils::cleanup_text( $params{detail}, { allow_multiline => 1 } ) ); # set these straight from the params - $report->name( Utils::trim_text( $params{name} ) ); $report->category( _ $params{category} ); my $areas = $c->stash->{all_areas}; @@ -624,10 +651,7 @@ sub process_report : Private { } elsif ( $first_council->{type} eq 'LBO') { unless ( Utils::london_categories()->{ $report->category } ) { - # TODO Perfect world, this wouldn't short-circuit, other errors would - # be included as well. - $c->stash->{field_errors} = { category => _('Please choose a category') }; - return; + $c->stash->{field_errors}->{category} = _('Please choose a category'); } $report->council( $first_council->{id} ); @@ -646,8 +670,9 @@ sub process_report : Private { )->all; unless ( @contacts ) { - $c->stash->{field_errors} = { category => _('Please choose a category') }; - return; + $c->stash->{field_errors}->{category} = _('Please choose a category'); + $report->council( -1 ); + return 1; } # construct the council string: @@ -662,8 +687,7 @@ sub process_report : Private { } elsif ( @{ $c->stash->{area_ids_to_list} } ) { # There was an area with categories, but we've not been given one. Bail. - $c->stash->{field_errors} = { category => _('Please choose a category') }; - return; + $c->stash->{field_errors}->{category} = _('Please choose a category'); } else { @@ -783,7 +807,9 @@ sub check_for_errors : Private { my ( $self, $c ) = @_; # let the model check for errors + $c->stash->{field_errors} ||= {}; my %field_errors = ( + %{ $c->stash->{field_errors} }, %{ $c->stash->{report}->user->check_for_errors }, %{ $c->stash->{report}->check_for_errors }, ); diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm index 76bd89174..2916e1ee1 100644 --- a/perllib/FixMyStreet/App/Controller/Report/Update.pm +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -20,15 +20,15 @@ Creates an update to a report sub report_update : Path : Args(0) { my ( $self, $c ) = @_; - $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('check_for_errors') + $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('check_for_errors') or $c->go( '/report/display', [ $c->req->param('id') ] ); - return $c->forward('save_update') - && $c->forward('redirect_or_confirm_creation'); + $c->forward('save_update'); + $c->forward('redirect_or_confirm_creation'); } sub confirm : Private { @@ -72,6 +72,10 @@ sub update_problem : Private { $problem->state( $update->problem_state ); } + if ( $update->mark_open && $update->user->id == $problem->user->id ) { + $problem->state('confirmed'); + } + $problem->lastupdate( \'ms_current_timestamp()' ); $problem->update; @@ -96,12 +100,17 @@ sub process_user : Private { my $update = $c->stash->{update}; - $update->user( $c->user->obj ) if $c->user; + if ( $c->user_exists ) { + my $user = $c->user->obj; + my $name = scalar $c->req->param('name'); + $user->name( Utils::trim_text( $name ) ) if $name; + $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' ); + my %params = map { $_ => scalar $c->req->param($_) } + ( 'rznvy', 'name', 'password_register' ); # cleanup the email address my $email = $params{rznvy} ? lc $params{rznvy} : ''; @@ -110,8 +119,22 @@ sub process_user : Private { $update->user( $c->model('DB::User')->find_or_new( { email => $email } ) ) unless $update->user; + # The user is trying to sign in. We only care about email from the params. + if ( $c->req->param('submit_sign_in') ) { + unless ( $c->forward( '/auth/sign_in', [ $email ] ) ) { + $c->stash->{field_errors}->{password} = _('There was a problem with your email/password combination. Please try again.'); + return 1; + } + my $user = $c->user->obj; + $update->user( $user ); + $update->name( $user->name ); + $c->stash->{field_errors}->{name} = _('You have successfully signed in; please check and confirm your details are accurate:'); + return 1; + } + $update->user->name( Utils::trim_text( $params{name} ) ) if $params{name}; + $update->user->password( Utils::trim_text( $params{password_register} ) ); return 1; } @@ -130,16 +153,15 @@ sub process_update : Private { my ( $self, $c ) = @_; my %params = - map { $_ => scalar $c->req->param($_) } ( 'update', 'name', 'fixed', 'state' ); + map { $_ => scalar $c->req->param($_) } ( 'update', 'name', 'fixed', 'state', 'reopen' ); $params{update} = Utils::cleanup_text( $params{update}, { allow_multiline => 1 } ); my $name = Utils::trim_text( $params{name} ); + my $anonymous = $c->req->param('may_show_name') ? 0 : 1; - my $anonymous = 1; - - $anonymous = 0 if ( $name && $c->req->param('may_show_name') ); + $params{reopen} = 0 unless $c->user && $c->user->id == $c->stash->{problem}->user->id; my $update = $c->model('DB::Comment')->new( { @@ -148,6 +170,7 @@ sub process_update : Private { problem => $c->stash->{problem}, state => 'unconfirmed', mark_fixed => $params{fixed} ? 1 : 0, + mark_open => $params{reopen} ? 1 : 0, cobrand => $c->cobrand->moniker, cobrand_data => $c->cobrand->extra_update_data, lang => $c->stash->{lang_code}, @@ -196,25 +219,27 @@ sub check_for_errors : Private { } # let the model check for errors + $c->stash->{field_errors} ||= {}; my %field_errors = ( + %{ $c->stash->{field_errors} }, %{ $c->stash->{update}->user->check_for_errors }, %{ $c->stash->{update}->check_for_errors }, ); - # we don't care if there are errors with this... - delete $field_errors{name}; + if ( my $photo_error = delete $c->stash->{photo_error} ) { + $field_errors{photo} = $photo_error; + } # all good if no errors return 1 unless ( scalar keys %field_errors - || ( $c->stash->{errors} && scalar @{ $c->stash->{errors} } ) - || $c->stash->{photo_error} ); + || ( $c->stash->{errors} && scalar @{ $c->stash->{errors} } ) ); $c->stash->{field_errors} = \%field_errors; $c->stash->{errors} ||= []; - push @{ $c->stash->{errors} }, - _('There were problems with your update. Please see below.'); + #push @{ $c->stash->{errors} }, + # _('There were problems with your update. Please see below.'); return; } diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 878e98c10..d8cd426ba 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -405,7 +405,8 @@ sub uri { $uri = URI->new( $uri ); $uri->query_param( zoom => 3 ) if $uri->query_param('lat') && !$uri->query_param('zoom'); - $uri->query_param( map => $map_class ); # FIXME Only on /around, /report? + + # $uri->query_param( map => $map_class ); # FIXME Only on /around, /report? return $uri; } diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 00f88e136..958194eb8 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -107,6 +107,9 @@ sub check_for_errors { my %errors = (); + $errors{name} = _('Please enter your name') + if !$self->name || $self->name !~ m/\S/; + $errors{update} = _('Please enter a message') unless $self->text =~ m/\S/; diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 1c8487ae7..d5d4b8102 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -274,7 +274,7 @@ sub check_for_errors { unless $self->council && $self->council =~ m/^(?:-1|[\d,]+(?:\|[\d,]+)?)$/; - if ( $self->name !~ m/\S/ ) { + if ( !$self->name || $self->name !~ m/\S/ ) { $errors{name} = _('Please enter your name'); } elsif (length( $self->name ) < 5 @@ -499,8 +499,7 @@ sub meta_line { return $meta; } -# TODO Some/much of this could be moved to the template -sub duration_string { +sub body { my ( $problem, $c ) = @_; my $body; if ($problem->external_body) { @@ -522,6 +521,13 @@ sub duration_string { } @councils ); } + return $body; +} + +# TODO Some/much of this could be moved to the template +sub duration_string { + my ( $problem, $c ) = @_; + my $body = $problem->body( $c ); return sprintf(_('Sent to %s %s later'), $body, Utils::prettify_duration($problem->whensent_local->epoch - $problem->confirmed_local->epoch, 'minute') ); diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm index 577d91c31..ada19a406 100644 --- a/perllib/FixMyStreet/DB/Result/User.pm +++ b/perllib/FixMyStreet/DB/Result/User.pm @@ -86,14 +86,6 @@ sub check_for_errors { if ( !$self->name || $self->name !~ m/\S/ ) { $errors{name} = _('Please enter your name'); } - elsif (length( $self->name ) < 5 - || $self->name !~ m/\s/ - || $self->name =~ m/\ba\s*n+on+((y|o)mo?u?s)?(ly)?\b/i ) - { - $errors{name} = _( -'Please enter your full name, councils need this information - if you do not wish your name to be shown on the site, untick the box' - ); - } if ( $self->email !~ /\S/ ) { $errors{email} = _('Please enter your email'); diff --git a/perllib/FixMyStreet/Map/FMS.pm b/perllib/FixMyStreet/Map/FMS.pm index a5a2dd9f0..c54ee8f3b 100644 --- a/perllib/FixMyStreet/Map/FMS.pm +++ b/perllib/FixMyStreet/Map/FMS.pm @@ -21,7 +21,7 @@ sub map_template { } sub copyright { - return _('Map contains Ordnance Survey data © Crown copyright and database right 2010. Microsoft'); + return _('Map contains Ordnance Survey data © Crown copyright and database right 2010.<br>© 2011 <a href="http://www.bing.com/maps/">Microsoft</a>. © AND, Navteq, Ordnance Survey.'); } sub get_quadkey { diff --git a/perllib/Utils.pm b/perllib/Utils.pm index 44234607f..39c251876 100644 --- a/perllib/Utils.pm +++ b/perllib/Utils.pm @@ -210,20 +210,25 @@ sub cleanup_text { } sub prettify_epoch { - my ($s, $short) = @_; + my ( $s, $type ) = @_; + $type = 'short' if $type eq '1'; + my @s = localtime($s); - my $tt = strftime('%H:%M', @s); + my $tt = ''; + $tt = strftime('%H:%M', @s) unless $type eq 'date'; my @t = localtime(); if (strftime('%Y%m%d', @s) eq strftime('%Y%m%d', @t)) { - $tt = "$tt " . _('today'); - } elsif (strftime('%Y %U', @s) eq strftime('%Y %U', @t)) { - $tt = "$tt, " . decode_utf8(strftime('%A', @s)); - } elsif ($short) { - $tt = "$tt, " . decode_utf8(strftime('%e %b %Y', @s)); + return "$tt " . _('today'); + } + $tt .= ', ' unless $type eq 'date'; + if (strftime('%Y %U', @s) eq strftime('%Y %U', @t)) { + $tt .= decode_utf8(strftime('%A', @s)); + } elsif ($type eq 'short') { + $tt .= decode_utf8(strftime('%e %b %Y', @s)); } elsif (strftime('%Y', @s) eq strftime('%Y', @t)) { - $tt = "$tt, " . decode_utf8(strftime('%A %e %B %Y', @s)); + $tt .= decode_utf8(strftime('%A %e %B %Y', @s)); } else { - $tt = "$tt, " . decode_utf8(strftime('%a %e %B %Y', @s)); + $tt .= decode_utf8(strftime('%a %e %B %Y', @s)); } return $tt; } |