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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
package FixMyStreet::App::Controller::Waste;
use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller' }
use utf8;
use Lingua::EN::Inflect qw( NUMWORDS );
use FixMyStreet::App::Form::Waste::UPRN;
use FixMyStreet::App::Form::Waste::AboutYou;
use FixMyStreet::App::Form::Waste::Request;
use FixMyStreet::App::Form::Waste::Report;
sub auto : Private {
my ( $self, $c ) = @_;
my $cobrand_check = $c->cobrand->feature('waste');
$c->detach( '/page_error_404_not_found' ) if !$cobrand_check;
return 1;
}
sub index : Path : Args(0) {
my ( $self, $c ) = @_;
if (my $uprn = $c->get_param('address')) {
$c->detach('redirect_to_uprn', [ $uprn ]);
}
$c->stash->{title} = 'What is your address?';
my $form = FixMyStreet::App::Form::Waste::UPRN->new( cobrand => $c->cobrand );
$form->process( params => $c->req->body_params );
if ($form->validated) {
my $addresses = $form->value->{postcode};
$form = address_list_form($addresses);
}
$c->stash->{form} = $form;
}
sub address_list_form {
my $addresses = shift;
HTML::FormHandler->new(
field_list => [
address => {
required => 1,
type => 'Select',
widget => 'RadioGroup',
label => 'Select an address',
tags => { last_differs => 1, small => 1 },
options => $addresses,
},
go => {
type => 'Submit',
value => 'Continue',
element_attr => { class => 'govuk-button' },
},
],
);
}
sub redirect_to_uprn : Private {
my ($self, $c, $uprn) = @_;
my $uri = '/waste/uprn/' . $uprn;
my $type = $c->get_param('type') || '';
$uri .= '/request' if $type eq 'request';
$uri .= '/report' if $type eq 'report';
$c->res->redirect($uri);
$c->detach;
}
sub uprn : Chained('/') : PathPart('waste/uprn') : CaptureArgs(1) {
my ($self, $c, $uprn) = @_;
if ($uprn eq 'missing') {
$c->stash->{template} = 'waste/missing.html';
$c->detach;
}
$c->forward('/auth/get_csrf_token');
my $property = $c->stash->{property} = $c->cobrand->call_hook(look_up_property => $uprn);
$c->detach( '/page_error_404_not_found', [] ) unless $property;
$c->stash->{uprn} = $uprn;
$c->stash->{latitude} = $property->{latitude};
$c->stash->{longitude} = $property->{longitude};
$c->stash->{service_data} = $c->cobrand->call_hook(bin_services_for_address => $property) || [];
$c->stash->{services} = { map { $_->{service_id} => $_ } @{$c->stash->{service_data}} };
}
sub bin_days : Chained('uprn') : PathPart('') : Args(0) {
my ($self, $c) = @_;
}
sub construct_bin_request_form {
my $c = shift;
my $field_list = [];
foreach (@{$c->stash->{service_data}}) {
next unless $_->{next};
my $name = $_->{service_name};
my $containers = $_->{request_containers};
my $max = $_->{request_max};
foreach my $id (@$containers) {
push @$field_list, "container-$id" => {
type => 'Checkbox',
apply => [
{
when => { "quantity-$id" => sub { $_[0] > 0 } },
check => qr/^1$/,
message => 'Please tick the box',
},
],
label => $name,
option_label => $c->stash->{containers}->{$id},
tags => { toggle => "form-quantity-$id-row" },
};
$name = ''; # Only on first container
push @$field_list, "quantity-$id" => {
type => 'Select',
label => 'Quantity',
tags => {
hint => "You can request a maximum of " . NUMWORDS($max) . " containers",
initial_hidden => 1,
},
options => [
{ value => "", label => '-' },
map { { value => $_, label => $_ } } (1..$max),
],
required_when => { "container-$id" => 1 },
};
}
}
return $field_list;
}
sub request : Chained('uprn') : Args(0) {
my ($self, $c) = @_;
my $field_list = construct_bin_request_form($c);
$c->stash->{first_page} = 'request';
$c->stash->{form_class} = 'FixMyStreet::App::Form::Waste::Request';
$c->stash->{page_list} = [
request => {
fields => [ grep { ! ref $_ } @$field_list, 'submit' ],
title => 'Which containers do you need?',
next => 'about_you',
},
];
$c->stash->{field_list} = $field_list;
$c->forward('form');
}
sub process_request_data : Private {
my ($self, $c, $form) = @_;
my $data = $form->saved_data;
my $address = $c->stash->{property}->{address};
my @services = grep { /^container-/ && $data->{$_} } keys %$data;
foreach (@services) {
my ($id) = /container-(.*)/;
my $container = $c->stash->{containers}{$id};
my $quantity = $data->{"quantity-$id"};
$data->{title} = "Request new $container";
$data->{detail} = "Quantity: $quantity\n\n$address";
$c->set_param('Container_Type', $id);
$c->set_param('Quantity', $quantity);
$c->forward('add_report', [ $data ]) or return;
push @{$c->stash->{report_ids}}, $c->stash->{report}->id;
}
return 1;
}
sub construct_bin_report_form {
my $c = shift;
my $field_list = [];
foreach (@{$c->stash->{service_data}}) {
next unless $_->{last};
my $id = $_->{service_id};
my $name = $_->{service_name};
push @$field_list, "service-$id" => {
type => 'Checkbox',
label => $name,
option_label => $name,
};
}
return $field_list;
}
sub report : Chained('uprn') : Args(0) {
my ($self, $c) = @_;
my $field_list = construct_bin_report_form($c);
$c->stash->{first_page} = 'report';
$c->stash->{form_class} = 'FixMyStreet::App::Form::Waste::Report';
$c->stash->{page_list} = [
report => {
fields => [ grep { ! ref $_ } @$field_list, 'submit' ],
title => 'Select your missed collection',
next => 'about_you',
},
];
$c->stash->{field_list} = $field_list;
$c->forward('form');
}
sub process_report_data : Private {
my ($self, $c, $form) = @_;
my $data = $form->saved_data;
my $address = $c->stash->{property}->{address};
my @services = grep { /^service-/ && $data->{$_} } keys %$data;
foreach (@services) {
my ($id) = /service-(.*)/;
my $service = $c->stash->{services}{$id}{service_name};
$data->{title} = "Report missed $service";
$data->{detail} = "$data->{title}\n\n$address";
$c->set_param('service_id', $id);
$c->forward('add_report', [ $data ]) or return;
push @{$c->stash->{report_ids}}, $c->stash->{report}->id;
}
return 1;
}
sub load_form {
my ($c, $previous_form) = @_;
my $page;
if ($previous_form) {
$page = $previous_form->next;
} else {
$page = $c->forward('get_page');
}
my $form = $c->stash->{form_class}->new(
page_list => $c->stash->{page_list},
$c->stash->{field_list} ? (field_list => $c->stash->{field_list}) : (),
page_name => $page,
csrf_token => $c->stash->{csrf_token},
c => $c,
previous_form => $previous_form,
saved_data_encoded => $c->get_param('saved_data'),
no_preload => 1,
);
if (!$form->has_current_page) {
$c->detach('/page_error_400_bad_request', [ 'Bad request' ]);
}
return $form;
}
sub form : Private {
my ($self, $c) = @_;
my $form = load_form($c);
if ($c->get_param('process')) {
$c->forward('/auth/check_csrf_token');
$form->process(params => $c->req->body_params);
if ($form->validated) {
$form = load_form($c, $form);
}
}
$form->process unless $form->processed;
$c->stash->{template} = $form->template || 'waste/index.html';
$c->stash->{form} = $form;
}
sub get_page : Private {
my ($self, $c) = @_;
my $goto = $c->get_param('goto') || '';
my $process = $c->get_param('process') || '';
$goto = $c->stash->{first_page} unless $goto || $process;
if ($goto && $process) {
$c->detach('/page_error_400_bad_request', [ 'Bad request' ]);
}
return $goto || $process;
}
sub add_report : Private {
my ( $self, $c, $data ) = @_;
$c->stash->{cobrand_data} = 'waste';
# XXX Is this best way to do this?
if ($c->user_exists && $c->user->from_body && $c->user->email ne $data->{email}) {
$c->set_param('form_as', 'another_user');
$c->set_param('username', $data->{email} || $data->{phone});
} else {
$c->set_param('username_register', $data->{email} || $data->{phone});
}
# Set the data as if a new report form has been submitted
$c->set_param('submit_problem', 1);
$c->set_param('pc', '');
$c->set_param('non_public', 1);
$c->set_param('name', $data->{name});
$c->set_param('phone', $data->{phone});
$c->set_param('category', $data->{category});
$c->set_param('title', $data->{title});
$c->set_param('detail', $data->{detail});
$c->set_param('uprn', $c->stash->{uprn});
$c->forward('setup_categories_and_bodies') unless $c->stash->{contacts};
$c->forward('/report/new/non_map_creation', [['/waste/remove_name_errors']]) or return;
my $report = $c->stash->{report};
$report->confirm;
$report->update;
$c->model('DB::Alert')->find_or_create({
user => $report->user,
alert_type => 'new_updates',
parameter => $report->id,
cobrand => $report->cobrand,
lang => $report->lang,
})->confirm;
return 1;
}
sub remove_name_errors : Private {
my ($self, $c) = @_;
# We do not mind about missing title/split name here
my $field_errors = $c->stash->{field_errors};
delete $field_errors->{fms_extra_title};
delete $field_errors->{first_name};
delete $field_errors->{last_name};
}
sub setup_categories_and_bodies : Private {
my ($self, $c) = @_;
$c->stash->{all_areas} = $c->stash->{all_areas_mapit} = { $c->cobrand->council_area_id => { id => $c->cobrand->council_area_id } };
$c->forward('/report/new/setup_categories_and_bodies');
my $contacts = $c->stash->{contacts};
@$contacts = grep { grep { $_ eq 'Waste' } @{$_->groups} } @$contacts;
}
__PACKAGE__->meta->make_immutable;
1;
|