aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/App/Controller')
-rw-r--r--perllib/FixMyStreet/App/Controller/Auth.pm49
-rw-r--r--perllib/FixMyStreet/App/Controller/Contact.pm15
-rw-r--r--perllib/FixMyStreet/App/Controller/Dashboard.pm155
-rw-r--r--perllib/FixMyStreet/App/Controller/Photo.pm6
-rw-r--r--perllib/FixMyStreet/App/Controller/Report.pm2
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/New.pm79
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/Update.pm31
-rwxr-xr-xperllib/FixMyStreet/App/Controller/Static.pm7
8 files changed, 278 insertions, 66 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Auth.pm b/perllib/FixMyStreet/App/Controller/Auth.pm
index c67de692a..3dc25dedf 100644
--- a/perllib/FixMyStreet/App/Controller/Auth.pm
+++ b/perllib/FixMyStreet/App/Controller/Auth.pm
@@ -7,6 +7,7 @@ BEGIN { extends 'Catalyst::Controller'; }
use Email::Valid;
use Net::Domain::TLD;
use mySociety::AuthToken;
+use JSON;
=head1 NAME
@@ -250,6 +251,54 @@ sub sign_out : Local {
$c->logout();
}
+sub ajax_sign_in : Path('ajax/sign_in') {
+ my ( $self, $c ) = @_;
+
+ my $return = {};
+ if ( $c->forward( 'sign_in' ) ) {
+ $return->{name} = $c->user->name;
+ } else {
+ $return->{error} = 1;
+ }
+
+ my $body = JSON->new->utf8(1)->encode( $return );
+ $c->res->content_type('application/json; charset=utf-8');
+ $c->res->body($body);
+
+ return 1;
+}
+
+sub ajax_sign_out : Path('ajax/sign_out') {
+ my ( $self, $c ) = @_;
+
+ $c->logout();
+
+ my $body = JSON->new->utf8(1)->encode( { signed_out => 1 } );
+ $c->res->content_type('application/json; charset=utf-8');
+ $c->res->body($body);
+
+ return 1;
+}
+
+sub ajax_check_auth : Path('ajax/check_auth') {
+ my ( $self, $c ) = @_;
+
+ my $code = 401;
+ my $data = { not_authorized => 1 };
+
+ if ( $c->user ) {
+ $data = { name => $c->user->name };
+ $code = 200;
+ }
+
+ my $body = JSON->new->utf8(1)->encode( $data );
+ $c->res->content_type('application/json; charset=utf-8');
+ $c->res->code($code);
+ $c->res->body($body);
+
+ return 1;
+}
+
=head2 check_auth
Utility page - returns a simple message 'OK' and a 200 response if the user is
diff --git a/perllib/FixMyStreet/App/Controller/Contact.pm b/perllib/FixMyStreet/App/Controller/Contact.pm
index 91580f05a..c300aee9a 100644
--- a/perllib/FixMyStreet/App/Controller/Contact.pm
+++ b/perllib/FixMyStreet/App/Controller/Contact.pm
@@ -139,17 +139,17 @@ sub prepare_params_for_email : Private {
$c->stash->{message} =~ s/\r\n/\n/g;
$c->stash->{subject} =~ s/\r|\n/ /g;
- my $base_url = $c->cobrand->base_url_for_emails( $c->cobrand->extra_data );
- my $admin_base_url = $c->cobrand->admin_base_url
- || 'https://secure.mysociety.org/admin/bci/';
+ my $base_url = $c->cobrand->base_url_for_emails( $c->cobrand->extra_data );
+ my $admin_url = $c->cobrand->admin_base_url;
if ( $c->stash->{update} ) {
my $problem_url = $base_url . '/report/' . $c->stash->{update}->problem_id
. '#update_' . $c->stash->{update}->id;
- my $admin_url = $admin_base_url . 'update_edit/' . $c->stash->{update}->id;
+ my $admin_url = " - $admin_url" . 'update_edit/' . $c->stash->{update}->id
+ if $admin_url;
$c->stash->{message} .= sprintf(
- " \n\n[ Complaint about update %d on report %d - %s - %s ]",
+ " \n\n[ Complaint about update %d on report %d - %s%s ]",
$c->stash->{update}->id,
$c->stash->{update}->problem_id,
$problem_url, $admin_url
@@ -158,9 +158,10 @@ sub prepare_params_for_email : Private {
elsif ( $c->stash->{problem} ) {
my $problem_url = $base_url . '/report/' . $c->stash->{problem}->id;
- my $admin_url = $admin_base_url . 'report_edit/' . $c->stash->{problem}->id;
+ $admin_url = " - $admin_url" . 'report_edit/' . $c->stash->{problem}->id
+ if $admin_url;
$c->stash->{message} .= sprintf(
- " \n\n[ Complaint about report %d - %s - %s ]",
+ " \n\n[ Complaint about report %d - %s%s ]",
$c->stash->{problem}->id,
$problem_url, $admin_url
);
diff --git a/perllib/FixMyStreet/App/Controller/Dashboard.pm b/perllib/FixMyStreet/App/Controller/Dashboard.pm
index 0273e0eda..e4266dc64 100644
--- a/perllib/FixMyStreet/App/Controller/Dashboard.pm
+++ b/perllib/FixMyStreet/App/Controller/Dashboard.pm
@@ -3,6 +3,7 @@ use Moose;
use namespace::autoclean;
use DateTime;
+use File::Slurp;
BEGIN { extends 'Catalyst::Controller'; }
@@ -18,6 +19,49 @@ Catalyst Controller.
=cut
+sub example : Local : Args(0) {
+ my ( $self, $c ) = @_;
+ $c->stash->{template} = 'dashboard/index.html';
+
+ $c->stash->{children} = {};
+ for my $i (1..3) {
+ $c->stash->{children}{$i} = { id => $i, name => "Ward $i" };
+ }
+
+ # TODO Set up manual version of what the below would do
+ #$c->forward( '/report/new/setup_categories_and_councils' );
+
+ # See if we've had anything from the dropdowns - perhaps vary results if so
+ $c->stash->{ward} = $c->req->param('ward');
+ $c->stash->{category} = $c->req->param('category');
+ $c->stash->{q_state} = $c->req->param('state');
+
+ eval {
+ my $data = File::Slurp::read_file(
+ FixMyStreet->path_to( 'data/dashboard.json' )->stringify
+ );
+ my $j = JSON->new->utf8->decode($data);
+ if ( !$c->stash->{ward} && !$c->stash->{category} ) {
+ $c->stash->{problems} = $j->{counts_all};
+ } else {
+ $c->stash->{problems} = $j->{counts_some};
+ }
+ $c->stash->{council} = $j->{council};
+ $c->stash->{children} = $j->{wards};
+ $c->stash->{category_options} = $j->{category_options};
+ if ( lc($c->stash->{q_state}) eq 'all' or !$c->stash->{q_state} ) {
+ $c->stash->{lists} = $j->{lists}->{all};
+ } else {
+ $c->stash->{lists} = $j->{lists}->{filtered};
+ }
+ };
+ if ($@) {
+ $c->stash->{message} = _("There was a problem showing this page. Please try again later.") . ' ' .
+ sprintf(_('The error was: %s'), $@);
+ $c->stash->{template} = 'errors/generic.html';
+ }
+}
+
=head2 check_page_allowed
Checks if we can view this page, and if not redirect to 404.
@@ -48,20 +92,21 @@ sub index : Path : Args(0) {
# Set up the data for the dropdowns
+ my $council_detail = mySociety::MaPit::call('area', $council );
+ $c->stash->{council} = $council_detail;
+
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->stash->{all_councils} = { $council => $council_detail };
$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...
@@ -71,40 +116,54 @@ sub index : Path : Args(0) {
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}
- }
+ $c->stash->{where} = \%where;
+ my $prob_where = { %where };
+ $prob_where->{state} = $prob_where->{'problem.state'};
+ delete $prob_where->{'problem.state'};
+ $c->stash->{prob_where} = $prob_where;
my %counts;
my $t = DateTime->today;
+ $counts{wtd} = $c->forward( 'updates_search', [ $t->subtract( days => $t->dow - 1 ) ] );
+ $counts{week} = $c->forward( 'updates_search', [ DateTime->now->subtract( weeks => 1 ) ] );
+ $counts{weeks} = $c->forward( 'updates_search', [ DateTime->now->subtract( weeks => 4 ) ] );
+ $counts{ytd} = $c->forward( 'updates_search', [ DateTime->today->set( day => 1, month => 1 ) ] );
- $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 )
- } } ] );
+ $c->stash->{problems} = \%counts;
- $counts{ytd} = $c->forward( 'updates_search', [ {
- %where,
- 'me.confirmed' => { '>=', DateTime->today->set( day => 1, month => 1 )
- } } ] );
+ # List of reports underneath summary table
- $c->stash->{problems} = \%counts;
+ $c->stash->{q_state} = $c->req->param('state') || '';
+ if ( $c->stash->{q_state} eq 'fixed' ) {
+ $prob_where->{state} = [ FixMyStreet::DB::Result::Problem->fixed_states() ];
+ } elsif ( $c->stash->{q_state} ) {
+ $prob_where->{state} = $c->stash->{q_state};
+ }
+ my $params = {
+ %$prob_where,
+ 'me.confirmed' => { '>=', DateTime->now->subtract( days => 30 ) },
+ };
+ my @problems = $c->cobrand->problems->search( $params )->all;
+ my %problems;
+ foreach (@problems) {
+ if ($_->confirmed >= DateTime->now->subtract(days => 7)) {
+ push @{$problems{1}}, $_;
+ } elsif ($_->confirmed >= DateTime->now->subtract(days => 14)) {
+ push @{$problems{2}}, $_;
+ } else {
+ push @{$problems{3}}, $_;
+ }
+ }
+ $c->stash->{lists} = \%problems;
}
sub updates_search : Private {
- my ( $self, $c, $params ) = @_;
+ my ( $self, $c, $time ) = @_;
+
+ my $params = {
+ %{$c->stash->{where}},
+ 'me.confirmed' => { '>=', $time },
+ };
my $comments = $c->model('DB::Comment')->search(
$params,
@@ -124,14 +183,48 @@ sub updates_search : Private {
'fixed - user', 'fixed', 'unconfirmed', 'hidden',
'partial', 'planned');
+ for my $vars (
+ [ 'time_to_fix', 'fixed - council' ],
+ [ 'time_to_mark', 'in progress', 'planned', 'investigating', 'closed' ],
+ ) {
+ my $col = shift @$vars;
+ my $substmt = "select min(id) from comment where me.problem_id=comment.problem_id and problem_state in ('"
+ . join("','", @$vars) . "')";
+ $comments = $c->model('DB::Comment')->search(
+ { %$params,
+ problem_state => $vars,
+ 'me.id' => \"= ($substmt)",
+ },
+ {
+ select => [
+ { count => 'me.id' },
+ { avg => { extract => "epoch from me.confirmed-problem.confirmed" } },
+ ],
+ as => [ qw/state_count time/ ],
+ join => 'problem'
+ }
+ )->first;
+ $counts{$col} = int( ($comments->get_column('time')||0) / 60 / 60 / 24 + 0.5 );
+ }
+
$counts{fixed_user} = $c->model('DB::Comment')->search(
- { %$params, mark_fixed => 1 }, { join => 'problem' }
+ { %$params, mark_fixed => 1, problem_state => undef }, { join => 'problem' }
)->count;
- $params->{state} = $params->{'problem.state'};
- delete $params->{'problem.state'};
+ $params = {
+ %{$c->stash->{prob_where}},
+ 'me.confirmed' => { '>=', $time },
+ };
$counts{total} = $c->cobrand->problems->search( $params )->count;
+ $params = {
+ %{$c->stash->{prob_where}},
+ 'me.confirmed' => { '>=', $time },
+ state => 'confirmed',
+ '(select min(id) from comment where me.id=problem_id and problem_state is not null)' => undef,
+ };
+ $counts{not_marked} = $c->cobrand->problems->search( $params )->count;
+
return \%counts;
}
diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm
index a6717fc33..fc4c3fde7 100644
--- a/perllib/FixMyStreet/App/Controller/Photo.pm
+++ b/perllib/FixMyStreet/App/Controller/Photo.pm
@@ -6,6 +6,8 @@ BEGIN {extends 'Catalyst::Controller'; }
use DateTime::Format::HTTP;
use Digest::SHA1 qw(sha1_hex);
+use File::Path;
+use File::Slurp;
use Path::Class;
=head1 NAME
@@ -95,6 +97,10 @@ sub index :LocalRegex('^(c/)?(\d+)(?:\.(full|tn|fp))?\.jpeg$') {
sub output : Private {
my ( $self, $c, $photo ) = @_;
+ # Save to file
+ File::Path::make_path( FixMyStreet->path_to( 'web', 'photo', 'c' )->stringify );
+ File::Slurp::write_file( FixMyStreet->path_to( 'web', $c->req->path )->stringify, \$photo );
+
my $dt = DateTime->now()->add( years => 1 );
$c->res->content_type( 'image/jpeg' );
diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm
index cca625bd5..6f249b2fe 100644
--- a/perllib/FixMyStreet/App/Controller/Report.pm
+++ b/perllib/FixMyStreet/App/Controller/Report.pm
@@ -125,6 +125,8 @@ sub format_problem_for_display : Private {
$c->stash->{add_alert} = 1;
}
+ $c->stash->{extra_name_info} = $problem->council eq '2482' ? 1 : 0;
+
$c->forward('generate_map_tags');
return 1;
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm
index 37ff1ae76..5bf184ae6 100644
--- a/perllib/FixMyStreet/App/Controller/Report/New.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/New.pm
@@ -104,6 +104,68 @@ sub report_new : Path : Args(0) {
$c->forward('redirect_or_confirm_creation');
}
+# This is for the new phonegap versions of the app. It looks a lot like
+# report_new but there's a few workflow differences as we only ever want
+# to sent JSON back here
+
+sub report_new_ajax : Path('mobile') : Args(0) {
+ my ( $self, $c ) = @_;
+
+ # create the report - loading a partial if available
+ $c->forward('initialize_report');
+
+ unless ( $c->forward('determine_location') ) {
+ $c->stash->{ json_response } = { errors => 'Unable to determine location' };
+ $c->forward('send_json_response');
+ return 1;
+ }
+
+ $c->forward('setup_categories_and_councils');
+ $c->forward('process_user');
+ $c->forward('process_report');
+ $c->forward('/photo/process_photo');
+
+ unless ($c->forward('check_for_errors')) {
+ $c->stash->{ json_response } = { errors => $c->stash->{field_errors} };
+ $c->stash->{ json_response }->{check_name} = $c->user->name if $c->stash->{check_name};
+ $c->forward('send_json_response');
+ return 1;
+ }
+
+ $c->forward('save_user_and_report');
+
+ my $report = $c->stash->{report};
+ my $data = $c->stash->{token_data} || {};
+ my $token = $c->model("DB::Token")->create( {
+ scope => 'problem',
+ data => {
+ %$data,
+ id => $report->id
+ }
+ } );
+ if ( $report->confirmed ) {
+ $c->stash->{ json_response } = { success => 1, report => $report->id };
+ } else {
+ $c->stash->{token_url} = $c->uri_for_email( '/P', $token->token );
+ $c->send_email( 'problem-confirm.txt', {
+ to => [ [ $report->user->email, $report->name ] ],
+ } );
+ $c->stash->{ json_response } = { success => 1 };
+ }
+
+ $c->forward('send_json_response');
+}
+
+sub send_json_response : Private {
+ my ( $self, $c ) = @_;
+
+ my $body = JSON->new->utf8(1)->encode(
+ $c->stash->{json_response},
+ );
+ $c->res->content_type('application/json; charset=utf-8');
+ $c->res->body($body);
+}
+
sub report_form_ajax : Path('ajax') : Args(0) {
my ( $self, $c ) = @_;
@@ -124,7 +186,6 @@ sub report_form_ajax : Path('ajax') : Args(0) {
# render templates to get the html
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')
: '';
@@ -133,7 +194,6 @@ sub report_form_ajax : Path('ajax') : Args(0) {
{
councils_text => $councils_text,
category => $category,
- has_open311 => $has_open311,
extra_name_info => $extra_name_info,
}
);
@@ -579,10 +639,6 @@ sub setup_categories_and_councils : Private {
$area_ids_to_list{ $contact->area_id } = 1;
- next # TODO - move this to the cobrand
- if $c->cobrand->moniker eq 'southampton'
- && $contact->category =~ /Street lighting|Traffic lights/;
-
next if $contact->category eq _('Other');
unless ( $seen{$contact->category} ) {
@@ -595,8 +651,9 @@ sub setup_categories_and_councils : Private {
}
if (@category_options) {
- @category_options =
- ( _('-- Pick a category --'), @category_options, _('Other') );
+ @category_options = ( _('-- Pick a category --'), @category_options );
+ push @category_options, _('Other')
+ unless $first_council->{id} == 2482;
$category_label = _('Category');
}
}
@@ -679,6 +736,7 @@ sub process_user : Private {
my $user = $c->user->obj;
$report->user( $user );
$report->name( $user->name );
+ $c->stash->{check_name} = 1;
$c->stash->{field_errors}->{name} = _('You have successfully signed in; please check and confirm your details are accurate:');
$c->log->info($user->id . ' logged in during problem creation');
return 1;
@@ -712,6 +770,7 @@ sub process_report : Private {
(
'title', 'detail', 'pc', #
'detail_size', 'detail_depth',
+ 'detail_offensive',
'may_show_name', #
'category', #
'partial', #
@@ -732,7 +791,7 @@ sub process_report : Private {
$report->title( Utils::cleanup_text( $params{title} ) );
my $detail = Utils::cleanup_text( $params{detail}, { allow_multiline => 1 } );
- for my $w ('depth', 'size') {
+ for my $w ('depth', 'size', 'offensive') {
next unless $params{"detail_$w"};
next if $params{"detail_$w"} eq '-- Please select --';
$detail .= "\n\n\u$w: " . $params{"detail_$w"};
@@ -813,7 +872,7 @@ sub process_report : Private {
};
}
- $c->cobrand->process_extras( $c, \@contacts, \@extra );
+ $c->cobrand->process_extras( $c, $contacts[0]->area_id, \@extra );
if ( @extra ) {
$c->stash->{report_meta} = \@extra;
diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm
index 5b0dad170..3a2b40c2c 100644
--- a/perllib/FixMyStreet/App/Controller/Report/Update.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm
@@ -201,13 +201,16 @@ sub process_update : Private {
$update->problem_state( $params{state} );
}
+ my @extra; # Next function fills this, but we don't need it here.
+ # This is just so that the error checkign for these extra fields runs.
+ # TODO Use extra here as it is used on reports.
+ $c->cobrand->process_extras( $c, $update->problem->council, \@extra );
+
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');
+ $extras{email_alerts_requested} = $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 } ) {
@@ -392,14 +395,20 @@ sub signup_for_alerts : Private {
if ( $c->stash->{add_alert} ) {
my $update = $c->stash->{update};
- my $alert = $c->model('DB::Alert')->find_or_create(
- user => $update->user,
- alert_type => 'new_updates',
- parameter => $update->problem_id,
- cobrand => $update->cobrand,
- cobrand_data => $update->cobrand_data,
- lang => $update->lang,
- );
+ my $options = {
+ user => $update->user,
+ alert_type => 'new_updates',
+ parameter => $update->problem_id,
+ };
+ my $alert = $c->model('DB::Alert')->find($options);
+ unless ($alert) {
+ $alert = $c->model('DB::Alert')->create({
+ %$options,
+ cobrand => $update->cobrand,
+ cobrand_data => $update->cobrand_data,
+ lang => $update->lang,
+ });
+ }
$alert->confirm();
} elsif ( $c->user && ( my $alert = $c->user->alert_for_problem($c->stash->{update}->problem_id) ) ) {
diff --git a/perllib/FixMyStreet/App/Controller/Static.pm b/perllib/FixMyStreet/App/Controller/Static.pm
index d4b7a1b83..723f0f2e1 100755
--- a/perllib/FixMyStreet/App/Controller/Static.pm
+++ b/perllib/FixMyStreet/App/Controller/Static.pm
@@ -22,13 +22,6 @@ sub about : Global : Args(0) {
# don't need to do anything here - should just pass through.
}
-sub for_councils : Path('/for-councils') : Args(0) {
- my ( $self, $c ) = @_;
-}
-sub for_councils_faq : Path('/for-councils/faq') : Args(0) {
- my ( $self, $c ) = @_;
-}
-
sub privacy : Global : Args(0) {
my ( $self, $c ) = @_;
}