diff options
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 103 |
1 files changed, 55 insertions, 48 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index a1038e909..376d4a000 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -188,10 +188,8 @@ sub report_form_ajax : Path('ajax') : Args(0) { # work out the location for this report and do some checks if ( ! $c->forward('determine_location') ) { - my $body = encode_json({ error => $c->stash->{location_error} }); - $c->res->content_type('application/json; charset=utf-8'); - $c->res->body($body); - return; + $c->stash->{json_response} = { error => $c->stash->{location_error} }; + $c->detach('send_json_response'); } $c->forward('setup_categories_and_bodies'); @@ -220,22 +218,27 @@ sub report_form_ajax : Path('ajax') : Args(0) { $contribute_as->{body} = $ca_body if $ca_body; } - my $body = encode_json( - { - bodies => \@list_of_names, - councils_text => $councils_text, - councils_text_private => $councils_text_private, - category => $category, - extra_name_info => $extra_name_info, - titles_list => $extra_titles_list, - %$contribute_as ? (contribute_as => $contribute_as) : (), - $top_message ? (top_message => $top_message) : (), - unresponsive => $c->stash->{unresponsive}->{ALL} || '', - } - ); + my %by_category; + foreach my $contact (@{$c->stash->{category_options}}) { + next if ref $contact eq 'HASH'; # Ignore the 'Pick a category' line + my $cat = $c->stash->{category} = $contact->category; + my $body = $c->forward('by_category_ajax_data', [ 'all', $cat ]); + $by_category{$cat} = $body; + } - $c->res->content_type('application/json; charset=utf-8'); - $c->res->body($body); + $c->stash->{json_response} = { + bodies => \@list_of_names, + councils_text => $councils_text, + councils_text_private => $councils_text_private, + category => $category, + extra_name_info => $extra_name_info, + titles_list => $extra_titles_list, + %$contribute_as ? (contribute_as => $contribute_as) : (), + $top_message ? (top_message => $top_message) : (), + unresponsive => $c->stash->{unresponsive}->{ALL} || '', + by_category => \%by_category, + }; + $c->detach('send_json_response'); } sub category_extras_ajax : Path('category_extras') : Args(0) { @@ -243,29 +246,24 @@ sub category_extras_ajax : Path('category_extras') : Args(0) { $c->forward('initialize_report'); if ( ! $c->forward('determine_location') ) { - my $body = encode_json({ error => _("Sorry, we could not find that location.") }); - $c->res->content_type('application/json; charset=utf-8'); - $c->res->body($body); - return 1; + $c->stash->{json_response} = { error => _("Sorry, we could not find that location.") }; + $c->detach('send_json_response'); } $c->forward('setup_categories_and_bodies'); $c->forward('setup_report_extra_fields'); - $c->forward('check_for_category'); + $c->forward('check_for_category'); my $category = $c->stash->{category} || ""; $category = '' if $category eq _('-- Pick a category --'); - my $bodies = $c->forward('contacts_to_bodies', [ $category ]); + $c->stash->{json_response} = $c->forward('by_category_ajax_data', [ 'one', $category ]); + $c->forward('send_json_response'); +} - my $list_of_names = [ map { $_->name } ($category ? @$bodies : values %{$c->stash->{bodies_to_list}}) ]; - my $vars = { - $category ? (list_of_names => $list_of_names) : (), - }; +sub by_category_ajax_data : Private { + my ($self, $c, $type, $category) = @_; - my $category_extra = ''; - my $category_extra_json = []; my $generate; - my $unresponsive = ''; if ( $c->stash->{category_extras}->{$category} && @{ $c->stash->{category_extras}->{$category} } >= 1 ) { $c->stash->{category_extras} = { $category => $c->stash->{category_extras}->{$category} }; $generate = 1; @@ -276,27 +274,36 @@ sub category_extras_ajax : Path('category_extras') : Args(0) { if ($c->stash->{report_extra_fields}) { $generate = 1; } + + my $bodies = $c->forward('contacts_to_bodies', [ $category ]); + my $list_of_names = [ map { $_->name } ($category ? @$bodies : values %{$c->stash->{bodies_to_list}}) ]; + my $vars = { + $category ? (list_of_names => $list_of_names) : (), + }; + + my $body = { + bodies => $list_of_names, + }; + if ($generate) { - $category_extra = $c->render_fragment('report/new/category_extras.html', $vars); - $category_extra_json = $c->forward('generate_category_extra_json'); - } + $body->{category_extra} = $c->render_fragment('report/new/category_extras.html', $vars); + $body->{category_extra_json} = $c->forward('generate_category_extra_json'); - my $councils_text = $c->render_fragment( 'report/new/councils_text.html', $vars); - my $councils_text_private = $c->render_fragment( 'report/new/councils_text_private.html'); + } - $unresponsive = $c->stash->{unresponsive}->{$category} || $c->stash->{unresponsive}->{ALL} || ''; + my $unresponsive = $c->stash->{unresponsive}->{$category}; + $unresponsive ||= $c->stash->{unresponsive}->{ALL} || '' if $type eq 'one'; - my $body = encode_json({ - category_extra => $category_extra, - councils_text => $councils_text, - councils_text_private => $councils_text_private, - category_extra_json => $category_extra_json, - unresponsive => $unresponsive, - bodies => $list_of_names, - }); + # unresponsive must return empty string if okay, as that's what mobile app checks + if ($type eq 'one' || ($type eq 'all' && $unresponsive)) { + $body->{unresponsive} = $unresponsive; + if ($type eq 'all' && $unresponsive) { + $body->{councils_text} = $c->render_fragment( 'report/new/councils_text.html', $vars); + $body->{councils_text_private} = $c->render_fragment( 'report/new/councils_text_private.html'); + } + } - $c->res->content_type('application/json; charset=utf-8'); - $c->res->body($body); + return $body; } =head2 report_import |