aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/My.pm
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2016-07-20 14:58:34 +0100
committerDave Arter <davea@mysociety.org>2016-08-22 10:36:01 +0100
commitbbb067ec5ddc88862be50819076bfe63c744122e (patch)
tree49a3e2fe62f45848495e41394e37c6536b4def44 /perllib/FixMyStreet/App/Controller/My.pm
parentf3649ee94bb80e8b33f0eea8a817760475157b3b (diff)
Add user planned reports.
A user with the appropriate permission can add/remove reports from their list of planned reports using a button on a report page. The list can be viewed at /my/planned.
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/My.pm')
-rw-r--r--perllib/FixMyStreet/App/Controller/My.pm72
1 files changed, 66 insertions, 6 deletions
diff --git a/perllib/FixMyStreet/App/Controller/My.pm b/perllib/FixMyStreet/App/Controller/My.pm
index 72391fee2..b15750c98 100644
--- a/perllib/FixMyStreet/App/Controller/My.pm
+++ b/perllib/FixMyStreet/App/Controller/My.pm
@@ -2,6 +2,8 @@ package FixMyStreet::App::Controller::My;
use Moose;
use namespace::autoclean;
+use JSON::MaybeXS;
+
BEGIN { extends 'Catalyst::Controller'; }
=head1 NAME
@@ -16,6 +18,11 @@ Catalyst Controller.
=cut
+sub begin : Private {
+ my ($self, $c) = @_;
+ $c->detach( '/auth/redirect' ) unless $c->user;
+}
+
=head2 index
=cut
@@ -23,10 +30,28 @@ Catalyst Controller.
sub my : Path : Args(0) {
my ( $self, $c ) = @_;
- $c->detach( '/auth/redirect' ) unless $c->user;
+ $c->stash->{problems_rs} = $c->cobrand->problems->search(
+ { user_id => $c->user->id });
+ $c->forward('get_problems');
+ $c->forward('get_updates');
+ $c->forward('setup_page_data');
+}
+
+sub planned : Local : Args(0) {
+ my ( $self, $c ) = @_;
+
+ $c->detach('/page_error_403_access_denied', [])
+ unless $c->user->from_body && $c->user->has_permission_to('planned_reports', $c->user->from_body->id);
+
+ $c->stash->{problems_rs} = $c->user->active_planned_reports;
+ $c->forward('get_problems');
+ $c->forward('setup_page_data');
+}
+
+sub get_problems : Private {
+ my ($self, $c) = @_;
my $p_page = $c->get_param('p') || 1;
- my $u_page = $c->get_param('u') || 1;
$c->forward( '/reports/stash_report_filter_status' );
@@ -36,7 +61,6 @@ sub my : Path : Args(0) {
my $states = $c->stash->{filter_problem_states};
my $params = {
state => [ keys %$states ],
- user_id => $c->user->id,
};
my $category = $c->get_param('filter_category');
@@ -45,7 +69,7 @@ sub my : Path : Args(0) {
$c->stash->{filter_category} = $category;
}
- my $rs = $c->cobrand->problems->search( $params, {
+ my $rs = $c->stash->{problems_rs}->search( $params, {
order_by => { -desc => 'confirmed' },
rows => 50
} )->page( $p_page );
@@ -57,8 +81,14 @@ sub my : Path : Args(0) {
}
$c->stash->{problems_pager} = $rs->pager;
$c->stash->{problems} = $problems;
+ $c->stash->{pins} = $pins;
+}
+
+sub get_updates : Private {
+ my ($self, $c) = @_;
- $rs = $c->user->comments->search(
+ my $u_page = $c->get_param('u') || 1;
+ my $rs = $c->user->comments->search(
{ state => 'confirmed' },
{
order_by => { -desc => 'confirmed' },
@@ -69,8 +99,12 @@ sub my : Path : Args(0) {
$c->stash->{has_content} += scalar @updates;
$c->stash->{updates} = \@updates;
$c->stash->{updates_pager} = $rs->pager;
+}
- my @categories = $c->cobrand->problems->search( { user_id => $c->user->id }, {
+sub setup_page_data : Private {
+ my ($self, $c) = @_;
+
+ my @categories = $c->stash->{problems_rs}->search({}, {
columns => [ 'category' ],
distinct => 1,
order_by => [ 'category' ],
@@ -79,6 +113,7 @@ sub my : Path : Args(0) {
$c->stash->{filter_categories} = \@categories;
$c->stash->{page} = 'my';
+ my $pins = $c->stash->{pins};
FixMyStreet::Map::display_map(
$c,
latitude => $pins->[0]{latitude},
@@ -89,6 +124,31 @@ sub my : Path : Args(0) {
if @$pins;
}
+sub planned_change : Path('planned/change') {
+ my ($self, $c) = @_;
+ $c->forward('/auth/check_csrf_token');
+
+ my $id = $c->get_param('id');
+ $c->forward( '/report/load_problem_or_display_error', [ $id ] );
+
+ my $change = $c->get_param('change');
+ $c->detach('/page_error_403_access_denied', [])
+ unless $change && $change =~ /add|remove/;
+
+ if ($change eq 'add') {
+ $c->user->add_to_planned_reports($c->stash->{problem});
+ } elsif ($change eq 'remove') {
+ $c->user->remove_from_planned_reports($c->stash->{problem});
+ }
+
+ if ($c->get_param('ajax')) {
+ $c->res->content_type('application/json; charset=utf-8');
+ $c->res->body(encode_json({ outcome => $change }));
+ } else {
+ $c->res->redirect( $c->uri_for_action('report/display', $id) );
+ }
+}
+
__PACKAGE__->meta->make_immutable;
1;