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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
package FixMyStreet::App::Controller::Report::Update;
use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller'; }
use Path::Class;
use Utils;
=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 ) = @_;
$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');
}
sub confirm : Private {
my ( $self, $c ) = @_;
$c->stash->{update}->confirm;
$c->stash->{update}->update;
$c->forward('update_problem');
$c->forward('signup_for_alerts');
return 1;
}
sub update_problem : Private {
my ( $self, $c ) = @_;
my $display_questionnaire = 0;
my $update = $c->stash->{update};
my $problem = $c->stash->{problem} || $update->problem;
if ( $update->mark_fixed ) {
$problem->state('fixed');
if ( $update->user->id == $problem->user->id ) {
$problem->send_questionnaire(0);
if ( $c->cobrand->ask_ever_reported
&& !$problem->user->answered_ever_reported )
{
$display_questionnaire = 1;
}
}
}
$problem->lastupdate( \'ms_current_timestamp()' );
$problem->update;
$c->stash->{problem_id} = $problem->id;
if ($display_questionnaire) {
$c->detach('/questionnaire/creator_fixed');
}
return 1;
}
=head2 process_user
Load user from the database or prepare a new one.
=cut
sub process_user : Private {
my ( $self, $c ) = @_;
my $update = $c->stash->{update};
$update->user( $c->user->obj ) if $c->user;
# 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;
$update->user( $c->model('DB::User')->find_or_new( { email => $email } ) )
unless $update->user;
$update->user->name( Utils::trim_text( $params{name} ) )
if $params{name};
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 problem 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', 'fixed' );
$params{update} =
Utils::cleanup_text( $params{update}, { allow_multiline => 1 } );
my $name = Utils::trim_text( $params{name} );
my $anonymous = 1;
$anonymous = 0 if ( $name && $c->req->param('may_show_name') );
my $update = $c->model('DB::Comment')->new(
{
text => $params{update},
name => $name,
problem => $c->stash->{problem},
state => 'unconfirmed',
mark_fixed => $params{fixed} ? 1 : 0,
cobrand => $c->cobrand->moniker,
cobrand_data => $c->cobrand->extra_update_data,
lang => $c->stash->{lang_code},
anonymous => $anonymous,
}
);
$c->stash->{update} = $update;
$c->stash->{add_alert} = $c->req->param('add_alert');
return 1;
}
=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 },
);
# we don't care if there are errors with this...
delete $field_errors{name};
# 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->{field_errors} = \%field_errors;
$c->stash->{errors} ||= [];
push @{ $c->stash->{errors} },
_('There were problems with your update. Please see below.');
return;
}
=head2 save_update
Save the update and the user as appropriate.
=cut
sub save_update : Private {
my ( $self, $c ) = @_;
my $update = $c->stash->{update};
if ( !$update->user->in_storage ) {
$update->user->insert;
}
elsif ( $c->user && $c->user->id == $update->user->id ) {
# Logged in and same user, so can confirm update straight away
$update->user->update;
$update->confirm;
}
# If there was a photo add that too
if ( my $fileid = $c->stash->{upload_fileid} ) {
my $file = file( $c->config->{UPLOAD_CACHE}, "$fileid.jpg" );
my $blob = $file->slurp;
$file->remove;
$update->photo($blob);
}
if ( $update->in_storage ) {
$update->update;
}
else {
$update->insert;
}
return 1;
}
=head2 redirect_or_confirm_creation
Now that the update has been created either redirect the user to problem page if it
has been confirmed or email them a token if it has not been.
=cut
sub redirect_or_confirm_creation : Private {
my ( $self, $c ) = @_;
my $update = $c->stash->{update};
# If confirmed send the user straight there.
if ( $update->confirmed ) {
$c->forward( 'update_problem' );
$c->forward( 'signup_for_alerts' );
my $report_uri = $c->uri_for( '/report', $update->problem_id );
$c->res->redirect($report_uri);
$c->detach;
}
# otherwise create a confirm token and email it to them.
my $token = $c->model("DB::Token")->create(
{
scope => 'comment',
data => {
id => $update->id,
add_alert => ( $c->req->param('add_alert') ? 1 : 0 ),
}
}
);
$c->stash->{token_url} = $c->uri_for_email( '/C', $token->token );
$c->send_email( 'update-confirm.txt', {
to => $update->name
? [ [ $update->user->email, $update->name ] ]
: $update->user->email,
} );
# tell user that they've been sent an email
$c->stash->{template} = 'email_sent.html';
$c->stash->{email_type} = 'update';
return 1;
}
=head2 signup_for_alerts
If the user has selected to be signed up for alerts then create a
new_updates alert. Or if they're logged in and they've unticked the
box, disable their alert.
NB: this does not check if they are a registered user so that should
happen before calling this.
=cut
sub signup_for_alerts : Private {
my ( $self, $c ) = @_;
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,
);
$alert->confirm();
} elsif ( $c->user && ( my $alert = $c->user->alert_for_problem($c->stash->{update}->problem_id) ) ) {
$alert->disable();
}
return 1;
}
__PACKAGE__->meta->make_immutable;
1;
|