diff options
Diffstat (limited to 'perllib/FixMyStreet/DB/ResultSet')
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Alert.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Comment.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Contact.pm | 64 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 31 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/State.pm | 6 |
5 files changed, 84 insertions, 31 deletions
diff --git a/perllib/FixMyStreet/DB/ResultSet/Alert.pm b/perllib/FixMyStreet/DB/ResultSet/Alert.pm index c61053fff..ddf80bc52 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Alert.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Alert.pm @@ -7,11 +7,6 @@ use warnings; sub timeline_created { my ( $rs, $restriction ) = @_; - my $prefetch = - $rs->result_source->storage->sql_maker->quote_char ? - [ qw/alert_type user/ ] : - [ qw/alert_type/ ]; - return $rs->search( { whensubscribed => { '>=', \"current_timestamp-'7 days'::interval" }, @@ -19,7 +14,7 @@ sub timeline_created { %{ $restriction }, }, { - prefetch => $prefetch, + prefetch => [ qw/alert_type user/ ], } ); } diff --git a/perllib/FixMyStreet/DB/ResultSet/Comment.pm b/perllib/FixMyStreet/DB/ResultSet/Comment.pm index b9a3df62d..034b86a40 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Comment.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Comment.pm @@ -13,18 +13,13 @@ sub to_body { sub timeline { my ( $rs ) = @_; - my $prefetch = - $rs->result_source->storage->sql_maker->quote_char ? - [ qw/user/ ] : - []; - return $rs->search( { 'me.state' => 'confirmed', 'me.created' => { '>=', \"current_timestamp-'7 days'::interval" }, }, { - prefetch => $prefetch, + prefetch => 'user', } ); } diff --git a/perllib/FixMyStreet/DB/ResultSet/Contact.pm b/perllib/FixMyStreet/DB/ResultSet/Contact.pm index 8ef6d1ac5..801d20cc0 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Contact.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Contact.pm @@ -3,6 +3,7 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; +use POSIX qw(strcoll); sub me { join('.', shift->current_source_alias, shift || q{}) } @@ -16,7 +17,12 @@ Filter down to not deleted contacts (so active or inactive). sub not_deleted { my $rs = shift; - return $rs->search( { $rs->me('state') => { '!=' => 'deleted' } } ); + return $rs->search( { $rs->me('state') => { -not_in => [ 'deleted', 'staff' ] } } ); +} + +sub not_deleted_admin { + my $rs = shift; + return $rs->search( { $rs->me('state') => { -not_in => [ 'deleted' ] } } ); } sub active { @@ -24,6 +30,53 @@ sub active { $rs->search( { $rs->me('state') => [ 'unconfirmed', 'confirmed' ] } ); } +sub for_new_reports { + my ($rs, $c, $bodies) = @_; + my $params = { + $rs->me('body_id') => [ keys %$bodies ], + }; + + if ($c->user_exists && $c->user->is_superuser) { + # Everything normal OR any staff states + $params->{$rs->me('state')} = [ 'unconfirmed', 'confirmed', 'staff' ]; + } elsif ($c->user_exists && $c->user->from_body) { + # Everything normal OR staff state in the user body + $params->{'-or'} = [ + $rs->me('state') => [ 'unconfirmed', 'confirmed' ], + { + $rs->me('body_id') => $c->user->from_body->id, + $rs->me('state') => 'staff', + }, + ]; + } else { + $params->{$rs->me('state')} = [ 'unconfirmed', 'confirmed' ]; + } + + $rs->search($params, { prefetch => 'body' }); +} + +sub translated { + my $rs = shift; + my $schema = $rs->result_source->schema; + $rs->search(undef, { + '+columns' => { 'msgstr' => 'translations.msgstr' }, + join => 'translations', + bind => [ 'category', $schema->lang, 'contact' ], + }); +} + +sub all_sorted { + my $rs = shift; + + my @contacts = $rs->translated->all; + @contacts = sort { + my $a_name = $a->get_extra_metadata('display_name') || $a->get_column('msgstr') || $a->category; + my $b_name = $b->get_extra_metadata('display_name') || $b->get_column('msgstr') || $b->category; + strcoll($a_name, $b_name) + } @contacts; + return @contacts; +} + sub summary_count { my ( $rs, $restriction ) = @_; @@ -37,4 +90,13 @@ sub summary_count { ); } +sub group_lookup { + my $rs = shift; + map { + my $group = $_->get_extra_metadata('group') || ''; + $group = join(',', ref $group ? @$group : $group); + $_->category => $group + } $rs->all; +} + 1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index 37fc34057..e23cf78e1 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -141,7 +141,7 @@ sub _recent { $query->{photo} = { '!=', undef } if $photos; my $attrs = { - order_by => { -desc => 'coalesce(confirmed, created)' }, + order_by => { -desc => \'coalesce(confirmed, created)' }, rows => $num, }; @@ -155,10 +155,11 @@ sub _recent { } else { $probs = Memcached::get($key); if ($probs) { - # Need to reattach schema so that confirmed column gets reinflated. - $probs->[0]->result_source->schema( $rs->result_source->schema ) if $probs->[0]; - # Catch any cached ones since hidden - $probs = [ grep { $_->photo && ! $_->is_hidden } @$probs ]; + # Need to refetch to check if hidden since cached + $probs = [ $rs->search({ + id => [ map { $_->id } @$probs ], + %$query, + }, $attrs)->all ]; } else { $probs = [ $rs->search( $query, $attrs )->all ]; Memcached::set($key, $probs, _cache_timeout()); @@ -207,11 +208,6 @@ sub around_map { sub timeline { my ( $rs ) = @_; - my $prefetch = - $rs->result_source->storage->sql_maker->quote_char ? - [ qw/user/ ] : - []; - return $rs->search( { -or => { @@ -221,7 +217,7 @@ sub timeline { } }, { - prefetch => $prefetch, + prefetch => 'user', } ); } @@ -245,12 +241,9 @@ sub unique_users { return $rs->search( { state => [ FixMyStreet::DB::Result::Problem->visible_states() ], }, { - select => [ { distinct => 'user_id' } ], - as => [ 'user_id' ] - } )->as_subselect_rs->search( undef, { - select => [ { count => 'user_id' } ], - as => [ 'count' ] - } )->first->get_column('count'); + columns => [ 'user_id' ], + distinct => 1, + } ); } sub categories_summary { @@ -273,7 +266,9 @@ sub categories_summary { sub include_comment_counts { my $rs = shift; my $order_by = $rs->{attrs}{order_by}; - return $rs unless ref $order_by eq 'HASH' && $order_by->{-desc} eq 'comment_count'; + return $rs unless + (ref $order_by eq 'ARRAY' && ref $order_by->[0] eq 'HASH' && $order_by->[0]->{-desc} eq 'comment_count') + || (ref $order_by eq 'HASH' && $order_by->{-desc} eq 'comment_count'); $rs->search({}, { '+select' => [ { "" => \'(select count(*) from comment where problem_id=me.id and state=\'confirmed\')', diff --git a/perllib/FixMyStreet/DB/ResultSet/State.pm b/perllib/FixMyStreet/DB/ResultSet/State.pm index 3e6169aeb..4f98efbf2 100644 --- a/perllib/FixMyStreet/DB/ResultSet/State.pm +++ b/perllib/FixMyStreet/DB/ResultSet/State.pm @@ -1,6 +1,7 @@ package FixMyStreet::DB::ResultSet::State; use base 'DBIx::Class::ResultSet'; +use utf8; use strict; use warnings; use Memcached; @@ -74,8 +75,13 @@ sub display { return $unchanging->{$label} if $unchanging->{$label}; if ($cobrand && $label eq 'not responsible') { return 'third party responsibility' if $cobrand eq 'bromley'; + return "not Island Roads’ responsibility" if $cobrand eq 'isleofwight'; + return "not TfL’s responsibility" if $cobrand eq 'tfl'; return _("not the council's responsibility"); } + if ($cobrand && $cobrand eq 'oxfordshire' && $label eq 'unable to fix') { + return 'Investigation complete'; + } my ($state) = $rs->_filter(sub { $_->label eq $label }); return $label unless $state; $state->name($translate_now->{$label}) if $translate_now->{$label}; |