diff options
author | pezholio <pezholio@gmail.com> | 2017-05-24 11:27:19 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-06-20 17:58:59 +0100 |
commit | bdd1e1627a1be6d9253ecc4e6904e68948227914 (patch) | |
tree | 8acbe51dd2419778eeacd7c8512b1b515da5a2a3 /perllib | |
parent | 6c4f3f78bfada011441411f308967298984e7ba5 (diff) |
Populate defect types dropdown on category change
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 6 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/DefectType.pm | 32 | ||||
-rw-r--r-- | perllib/FixMyStreet/Roles/ContactExtra.pm | 43 |
3 files changed, 68 insertions, 13 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index 823f4d08f..1d322e88c 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -633,6 +633,12 @@ sub setup_categories_and_bodies : Private { # keysort does not appear to obey locale so use strcoll (see i18n.t) @contacts = sort { strcoll( $a->category, $b->category ) } @contacts; + # Get defect types for inspectors + if ($c->cobrand->can('council_area_id')) { + my $category_defect_types = FixMyStreet::App->model('DB::DefectType')->by_categories($c->cobrand->council_area_id, @contacts); + $c->stash->{category_defect_types} = $category_defect_types; + } + my %seen; foreach my $contact (@contacts) { diff --git a/perllib/FixMyStreet/DB/ResultSet/DefectType.pm b/perllib/FixMyStreet/DB/ResultSet/DefectType.pm index a873ef252..b2ef77f7c 100644 --- a/perllib/FixMyStreet/DB/ResultSet/DefectType.pm +++ b/perllib/FixMyStreet/DB/ResultSet/DefectType.pm @@ -3,20 +3,26 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; +use Moo; +use HTML::Entities; -sub for_bodies { - my ($rs, $bodies, $category) = @_; - my $attrs = { - 'me.body_id' => $bodies, - }; - if ($category) { - $attrs->{'contact.category'} = [ $category, undef ]; - } - $rs->search($attrs, { - order_by => 'name', - join => { 'contact_defect_types' => 'contact' }, - distinct => 1, - }); +with('FixMyStreet::Roles::ContactExtra'); + +sub join_table { + return 'contact_defect_types'; +} + +sub map_extras { + my ($rs, @ts) = @_; + return map { + my $meta = $_->get_extra_metadata(); + my %extra = map { $_ => encode_entities($meta->{$_}) } keys %$meta; + { + id => $_->id, + name => encode_entities($_->name), + extra => \%extra + } + } @ts; } 1; diff --git a/perllib/FixMyStreet/Roles/ContactExtra.pm b/perllib/FixMyStreet/Roles/ContactExtra.pm new file mode 100644 index 000000000..d23a094f3 --- /dev/null +++ b/perllib/FixMyStreet/Roles/ContactExtra.pm @@ -0,0 +1,43 @@ +package FixMyStreet::Roles::ContactExtra; + +use Moo::Role; +use JSON::MaybeXS; + +requires 'join_table', 'map_extras'; + +sub for_bodies { + my ($rs, $bodies, $category) = @_; + my $join_table = $rs->join_table(); + my $attrs = { + 'me.body_id' => $bodies, + }; + my $filters = { + order_by => 'name', + join => { $join_table => 'contact' }, + distinct => 1, + }; + if ($category) { + $attrs->{'contact.category'} = [ $category, undef ]; + } + $rs->search($attrs, $filters); +} + +sub by_categories { + my ($rs, $area_id, @contacts) = @_; + my %body_ids = map { $_->body_id => 1 } FixMyStreet::DB->resultset('BodyArea')->search({ area_id => $area_id }); + my @body_ids = keys %body_ids; + my %extras = (); + my @results = $rs->for_bodies(\@body_ids, undef); + @contacts = grep { $body_ids{$_->body_id} } @contacts; + + foreach my $contact (@contacts) { + my $join_table = $rs->join_table(); + my @ts = grep { !defined($_->$join_table->first) || $_->$join_table->find({contact_id => $contact->get_column('id')}) } @results; + @ts = $rs->map_extras(@ts); + $extras{$contact->category} = encode_json(\@ts); + } + + return \%extras; +} + +1; |