diff options
-rw-r--r-- | perllib/FixMyStreet/App.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/JSON.pm | 82 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Reports.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 14 | ||||
-rw-r--r-- | perllib/Problems.pm | 108 | ||||
-rw-r--r-- | templates/web/default/front_stats.html | 3 |
6 files changed, 67 insertions, 147 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index 09a8609fe..4aa9cc75b 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -177,7 +177,7 @@ sub setup_request { $c->log->debug( sprintf "Set lang to '%s' and cobrand to '%s'", $set_lang, $cobrand->moniker ); - Problems::set_site_restriction_with_cobrand_object($cobrand); + $c->model('DB::Problem')->set_restriction( $cobrand->site_restriction() ); Memcached::set_namespace( FixMyStreet->config('BCI_DB_NAME') . ":" ); diff --git a/perllib/FixMyStreet/App/Controller/JSON.pm b/perllib/FixMyStreet/App/Controller/JSON.pm index d3688f19a..cbd483f16 100644 --- a/perllib/FixMyStreet/App/Controller/JSON.pm +++ b/perllib/FixMyStreet/App/Controller/JSON.pm @@ -7,6 +7,7 @@ BEGIN { extends 'Catalyst::Controller'; } use JSON; use DateTime; use DateTime::Format::ISO8601; +use List::MoreUtils 'uniq'; =head1 NAME @@ -69,42 +70,53 @@ sub problems : Local { } # query the database - $c->stash->{response} = - $type eq 'new_problems' - ? Problems::created_in_interval( $start_date, $end_date ) - : Problems::fixed_in_interval( $start_date, $end_date ); -} + my ( $state, $date_col ); + if ( $type eq 'new_problems' ) { + $state = 'confirmed'; + $date_col = 'created'; + } elsif ( $type eq 'fixed_problems' ) { + $state = 'fixed'; + $date_col = 'lastupdate'; + } + + my $one_day = DateTime::Duration->new( days => 1 ); + my @problems = $c->model('DB::Problem')->site_restricted->search( { + $date_col => { + '>=' => $start_dt, + '<=' => $end_dt + $one_day, + }, + state => $state, + }, { + columns => [ + 'id', 'title', 'council', 'category', + 'detail', 'name', 'anonymous', 'confirmed', + 'whensent', 'service', + ] + } ); + + my @councils; + foreach my $problem (@problems) { + $problem->name( '' ) if $problem->anonymous == 1; + $problem->service( 'Web interface' ) if $problem->service eq ''; + if ($problem->council) { + (my $council = $problem->council) =~ s/\|.*//g; + my @council_ids = split /,/, $council; + push(@councils, @council_ids); + $problem->council( \@council_ids ); + } + } + @councils = uniq @councils; + my $areas_info = mySociety::MaPit::call('areas', \@councils); + foreach my $problem (@problems) { + if ($problem->council) { + my @council_names = map { $areas_info->{$_}->{name} } @{$problem->council} ; + $problem->council( join(' and ', @council_names) ); + } + } -# If we convert this code to be fully DBIC based then the following snippet is a -# good start. The roadblock to doing it fully is the 'site_restriction' in the -# SQL which is currently provided as SQL, rather than something that could be -# easily added to the DBIC query. The hardest cobrand to change would be the -# cities - so perhaps do it after we know wether that needs to be kept or not. -# -# my $state = -# $type eq 'new_problems' ? 'confirmed' -# : $type eq 'fixed_problems' ? 'fixed_problems' -# : die; -# -# my $one_day = DateTime::Duration->new( days => 1 ); -# -# my $problems = $c->model('DB::Problem')->search( -# { -# created => { -# '>=' => $start_dt, -# '<=' => $end_dt + $one_day, -# }, -# state => $state, -# # ------ add is site_restriction here ------- -# }, -# { -# columns => [ -# 'id', 'title', 'council', 'category', -# 'detail', 'name', 'anonymous', 'confirmed', -# 'whensent', 'service', -# ] -# } -# ); + @problems = map { { $_->get_columns } } @problems; + $c->stash->{response} = \@problems; +} sub end : Private { my ( $self, $c ) = @_; diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm index 993cd752e..7cb9ad9fb 100644 --- a/perllib/FixMyStreet/App/Controller/Reports.pm +++ b/perllib/FixMyStreet/App/Controller/Reports.pm @@ -246,8 +246,7 @@ sub load_problems : Private { my ( $self, $c ) = @_; my $where = { - state => [ 'confirmed', 'fixed' ], - %{ Problems::site_restriction() } + state => [ 'confirmed', 'fixed' ] }; if ($c->stash->{ward}) { $where->{areas} = { 'like', '%' . $c->stash->{ward}->{id} . '%' }; # FIXME Check this is secure @@ -255,7 +254,7 @@ sub load_problems : Private { $where->{areas} = { 'like', '%' . $c->stash->{council}->{id} . '%' }; } my $current_timestamp = Problems::current_timestamp(); - my $problems = $c->model('DB::Problem')->search( + my $problems = $c->model('DB::Problem')->site_restricted->search( $where, { columns => [ diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index 835ab1b45..b029caf3b 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -40,4 +40,18 @@ sub summary_count { ); } +my $site_restriction; +my $site_key; + +sub set_restriction { + my ( $rs, $sql, $key, $restriction ) = @_; + $site_key = $key; + $site_restriction = $restriction; +} + +sub site_restricted { + my ( $rs ) = @_; + return $rs->search( $site_restriction ); +} + 1; diff --git a/perllib/Problems.pm b/perllib/Problems.pm index c31fdd507..143b5bb9b 100644 --- a/perllib/Problems.pm +++ b/perllib/Problems.pm @@ -21,21 +21,6 @@ use mySociety::MaPit; my $site_restriction = ''; my $site_key = 0; -my $site_restriction_hash = {}; - -sub site_restriction { - return $site_restriction_hash; -} - -# Set the site restrictions using the new cobrand style - no need to special -# case 'fixmystreet' as default cobrand takes care of that. -sub set_site_restriction_with_cobrand_object { - my $cobrand = shift; - - my $cobrand_data = $cobrand->extra_data; - ( $site_restriction, $site_key, $site_restriction_hash ) = - $cobrand->site_restriction($cobrand_data); -} sub current_timestamp { my $current_timestamp = dbh()->selectrow_array('select ms_current_timestamp()'); @@ -141,38 +126,6 @@ sub recent { return $result; } -# sub front_stats { -# my ($q) = @_; -# my $fixed = Problems::recent_fixed(); -# my $updates = Problems::number_comments(); -# my $new = Problems::recent_new('1 week'); -# (my $new_pretty = $new) =~ s/(?<=\d)(?=(?:\d\d\d)+$)/,/g; -# my $new_text = sprintf(mySociety::Locale::nget('<big>%s</big> report in past week', -# '<big>%s</big> reports in past week', $new), $new_pretty); -# if ($q->{site} ne 'emptyhomes' && $new > $fixed) { -# $new = Problems::recent_new('3 days'); -# ($new_pretty = $new) =~ s/(?<=\d)(?=(?:\d\d\d)+$)/,/g; -# $new_text = sprintf(mySociety::Locale::nget('<big>%s</big> report recently', '<big>%s</big> reports recently', $new), $new_pretty); -# } -# (my $fixed_pretty = $fixed) =~ s/(?<=\d)(?=(?:\d\d\d)+$)/,/g; -# (my $updates_pretty = $updates) =~ s/(?<=\d)(?=(?:\d\d\d)+$)/,/g; -# -# my $out = ''; -# $out .= $q->h2(_('FixMyStreet updates')); -# my $lastmo = ''; -# if ($q->{site} ne 'emptyhomes'){ -# $lastmo = $q->div(sprintf(mySociety::Locale::nget("<big>%s</big> fixed in past month", "<big>%s</big> fixed in past month", $fixed), $fixed), $fixed_pretty); -# } -# $out .= $q->div({-id => 'front_stats'}, -# $q->div($new_text), -# ($q->{site} ne 'emptyhomes' ? $q->div(sprintf(mySociety::Locale::nget("<big>%s</big> fixed in past month", "<big>%s</big> fixed in past month", $fixed), $fixed_pretty)) : ''), -# $q->div(sprintf(mySociety::Locale::nget("<big>%s</big> update on reports", -# "<big>%s</big> updates on reports", $updates), $updates_pretty)) -# ); -# return $out; -# -# } - # Problems around a location sub around_map { @@ -236,67 +189,6 @@ sub fetch_problem { return $p; } -# API functions - -sub problems_matching_criteria { - my ($criteria, @params) = @_; - my $problems = select_all( - "select id, title, council, category, detail, name, anonymous, - confirmed, whensent, service - from problem - $criteria - $site_restriction", @params); - - my @councils; - foreach my $problem (@$problems){ - if ($problem->{anonymous} == 1){ - $problem->{name} = ''; - } - if ($problem->{service} eq ''){ - $problem->{service} = 'Web interface'; - } - if ($problem->{council}) { - $problem->{council} =~ s/\|.*//g; - my @council_ids = split /,/, $problem->{council}; - push(@councils, @council_ids); - $problem->{council} = \@council_ids; - } - } - my $areas_info = mySociety::MaPit::call('areas', \@councils); - foreach my $problem (@$problems){ - if ($problem->{council}) { - my @council_names = map { $areas_info->{$_}->{name} } @{$problem->{council}} ; - $problem->{council} = join(' and ', @council_names); - } - } - return $problems; -} - -sub fixed_in_interval { - my ($start_date, $end_date) = @_; - my $criteria = "where state='fixed' and date_trunc('day',lastupdate)>=? and -date_trunc('day',lastupdate)<=?"; - return problems_matching_criteria($criteria, $start_date, $end_date); -} - -sub created_in_interval { - my ($start_date, $end_date) = @_; - my $criteria = "where state='confirmed' and date_trunc('day',created)>=? and -date_trunc('day',created)<=?"; - return problems_matching_criteria($criteria, $start_date, $end_date); -} - -=item data_sharing_notification_start - -Returns the unix datetime when the T&Cs that explicitly allow for users' data to be displayed -on other sites. - -=cut - -sub data_sharing_notification_start { - return 1255392000; -} - # Report functions =item council_problems WARD COUNCIL diff --git a/templates/web/default/front_stats.html b/templates/web/default/front_stats.html index 847df2497..385414768 100644 --- a/templates/web/default/front_stats.html +++ b/templates/web/default/front_stats.html @@ -4,6 +4,7 @@ # 'Template::Plugin::Number::Format' %] +<h2>[% loc('FixMyStreet updates') %]</h2> [% stats = c.cobrand.front_stats_data(); @@ -37,7 +38,9 @@ <div id="front_stats"> <div>[% tprintf( new_text, stats.new ) | comma %]</div> + [% IF c.cobrand.moniker != 'emptyhomes' %] <div>[% tprintf( fixed_text, stats.fixed ) | comma %]</div> + [% END %] <div>[% tprintf( updates_text, stats.updates ) | comma %]</div> </div> |