aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Report/Update.pm
blob: 6294084016904ec59e06b0b97bc47848cb9c0d26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package FixMyStreet::App::Controller::Report::Update;

use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Report::Update

=head1 DESCRIPTION

Creates an update to a report

=cut

sub report_update : Path : Args(0) {
    my ( $self, $c ) = @_;

#    my $q = shift;
#    my @vars = qw(id name rznvy update fixed upload_fileid add_alert);
#    my %input = map { $_ => $q->param($_) || '' } @vars;
#    my @errors;
#    my %field_errors;
#
#    my $fh = $q->upload('photo');
#    if ($fh) {
#        my $err = Page::check_photo($q, $fh);
#        push @errors, $err if $err;
#    }
#
#    my $image;
#    if ($fh) {
#        try {
#            $image = Page::process_photo($fh);
#        } catch Error::Simple with {
#            my $e = shift;
#            push(@errors, sprintf(_("That image doesn't appear to have uploaded correctly (%s), please try again."), $e));
#        };
#    }
#
#    if ($input{upload_fileid}) {
#        open FP, mySociety::Config::get('UPLOAD_CACHE') . $input{upload_fileid};
#        $image = join('', <FP>);
#        close FP;
#    }
#
#    return display_problem($q, \@errors, \%field_errors) if (@errors || scalar(keys(%field_errors)));
#    my $cobrand = Page::get_cobrand($q);
#    my $cobrand_data = Cobrand::extra_update_data($cobrand, $q);
#    my $id = dbh()->selectrow_array("select nextval('comment_id_seq');");
#    Utils::workaround_pg_bytea("insert into comment
#        (id, problem_id, name, email, website, text, state, mark_fixed, photo, lang, cobrand, cobrand_data)
#        values (?, ?, ?, ?, '', ?, 'unconfirmed', ?, ?, ?, ?, ?)", 7,
#        $id, $input{id}, $input{name}, $input{rznvy}, $input{update},
#        $input{fixed} ? 't' : 'f', $image, $mySociety::Locale::lang, $cobrand, $cobrand_data);
#
#    my %h = ();
#    $h{update} = $input{update};
#    $h{name} = $input{name} ? $input{name} : _("Anonymous");
#    my $base = Page::base_url_with_lang($q, undef, 1);
#    $h{url} = $base . '/C/' . mySociety::AuthToken::store('update', { id => $id, add_alert => $input{add_alert} } );
#    dbh()->commit();
#
#    my $out = Page::send_confirmation_email($q, $input{rznvy}, $input{name}, 'update', %h);
#    return $out;

         $c->forward('setup_page')
      && $c->forward('process_user')
      && $c->forward('process_update')
      && $c->forward('check_for_errors')
      or $c->go( '/report/display', [ $c->req->param('id') ] );

    $c->forward('save_update');

    # just go back to the report page for now
    $c->go( '/report/display', [ $c->req->param('id') ] );
    return 1;
}

sub setup_page : Private {
    my ( $self, $c ) = @_;

    my $problem =
      $c->model('DB::Problem')->find( { id => $c->req->param('id') } );

    return unless $problem;

    $c->stash->{problem} = $problem;

    return 1;
}

=head2 process_user

Load user from the database or prepare a new one.

=cut

sub process_user : Private {
    my ( $self, $c ) = @_;

    # FIXME - If user already logged in use them regardless

    # Extract all the params to a hash to make them easier to work with
    my %params =    #
      map { $_ => scalar $c->req->param($_) }    #
      ( 'rznvy', 'name' );

    # cleanup the email address
    my $email = $params{rznvy} ? lc $params{rznvy} : '';
    $email =~ s{\s+}{}g;

    my $update_user = $c->model('DB::User')->find_or_new( { email => $email } );

    # set the user's name if they don't have one
    $update_user->name( _trim_text( $params{name} ) )
      unless $update_user->name;

    $c->stash->{update_user} = $update_user;

    return 1;
}

=head2 process_update

Take the submitted params and create a new update item. Does not save
anything to the database.

NB: relies on their being a probem and update_user in the stash. May
want to move adding these elsewhere

=cut

sub process_update : Private {
    my ( $self, $c ) = @_;

    my %params =    #
      map { $_ => scalar $c->req->param($_) } ( 'update', 'name' );

    my $update = $c->model('DB::Comment')->new(
        {
            text    => $params{update},
            name    => _trim_text( $params{name} ),
            problem => $c->stash->{problem},
            user    => $c->stash->{update_user}
        }
    );

    $c->stash->{update} = $update;

    return 1;
}

sub _trim_text {
    my $input = shift;
    for ($input) {
        last unless $_;
        s{\s+}{ }g;    # all whitespace to single space
        s{^ }{};       # trim leading
        s{ $}{};       # trim trailing
    }
    return $input;
}

=head2 check_for_errors

Examine the user and the report for errors. If found put them on stash and
return false.

=cut

sub check_for_errors : Private {
    my ( $self, $c ) = @_;

    # let the model check for errors
    my %field_errors = (
        %{ $c->stash->{update_user}->check_for_errors },
        %{ $c->stash->{update}->check_for_errors },
    );

    $c->log->debug( join ', ', keys %field_errors );

    # all good if no errors
    return 1 unless scalar keys %field_errors;

    $c->stash->{field_errors} = \%field_errors;

    return;
}

=head2 save_update

Save the update and the user as appropriate.

=cut

sub save_update : Private {
    my ( $self, $c ) = @_;

    if ( $c->stash->{update_user}->in_storage ) {
        $c->stash->{update_user}->update_user;
    } else {
        $c->stash->{update_user}->insert;
    }

    if ( $c->stash->{update}->in_storage ) {
        $c->stash->{update}->update;
    } else {
        $c->stash->{update}->insert;
    }
}

__PACKAGE__->meta->make_immutable;

1;