diff options
Diffstat (limited to 'perllib/FixMyStreet/DB')
-rw-r--r-- | perllib/FixMyStreet/DB/Result/AdminLog.pm | 9 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Alert.pm | 9 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/AlertSent.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Body.pm | 22 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 9 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/ModerationOriginalData.pm | 11 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 92 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/ResponseTemplate.pm | 49 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Token.pm | 12 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Alert.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/AlertType.pm | 32 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Comment.pm | 34 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Contact.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Nearby.pm | 18 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 57 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm | 46 |
16 files changed, 268 insertions, 147 deletions
diff --git a/perllib/FixMyStreet/DB/Result/AdminLog.pm b/perllib/FixMyStreet/DB/Result/AdminLog.pm index 41bc3100a..d60915cfc 100644 --- a/perllib/FixMyStreet/DB/Result/AdminLog.pm +++ b/perllib/FixMyStreet/DB/Result/AdminLog.pm @@ -29,13 +29,16 @@ __PACKAGE__->add_columns( "whenedited", { data_type => "timestamp", - default_value => \"ms_current_timestamp()", + default_value => \"current_timestamp", is_nullable => 0, + original => { default_value => \"now()" }, }, "user_id", { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, "reason", { data_type => "text", default_value => "", is_nullable => 0 }, + "time_spent", + { data_type => "integer", default_value => "0", is_nullable => 0 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->belongs_to( @@ -51,7 +54,7 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07035 @ 2014-07-31 15:58:54 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:okGiaKaVYaTrlz0LCV01vA +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2015-08-13 16:33:38 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:RCi1FEwb9T2MZ2X+QOTTUA 1; diff --git a/perllib/FixMyStreet/DB/Result/Alert.pm b/perllib/FixMyStreet/DB/Result/Alert.pm index c64cb2ff4..35cce8368 100644 --- a/perllib/FixMyStreet/DB/Result/Alert.pm +++ b/perllib/FixMyStreet/DB/Result/Alert.pm @@ -37,8 +37,9 @@ __PACKAGE__->add_columns( "whensubscribed", { data_type => "timestamp", - default_value => \"ms_current_timestamp()", + default_value => \"current_timestamp", is_nullable => 0, + original => { default_value => \"now()" }, }, "whendisabled", { data_type => "timestamp", is_nullable => 1 }, @@ -64,8 +65,8 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:d9yIFiTGtbtFaULXZNKstQ +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2015-08-13 16:33:38 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:5RNyB430T8PqtFlmGV/MUg # You can replace this text with custom code or comments, and it will be preserved on regeneration @@ -106,7 +107,7 @@ sub confirm { sub disable { my $self = shift; - $self->whendisabled( \'ms_current_timestamp()' ); + $self->whendisabled( \'current_timestamp' ); $self->update; return 1; diff --git a/perllib/FixMyStreet/DB/Result/AlertSent.pm b/perllib/FixMyStreet/DB/Result/AlertSent.pm index 422e010a9..83043a33b 100644 --- a/perllib/FixMyStreet/DB/Result/AlertSent.pm +++ b/perllib/FixMyStreet/DB/Result/AlertSent.pm @@ -18,8 +18,9 @@ __PACKAGE__->add_columns( "whenqueued", { data_type => "timestamp", - default_value => \"ms_current_timestamp()", + default_value => \"current_timestamp", is_nullable => 0, + original => { default_value => \"now()" }, }, ); __PACKAGE__->belongs_to( @@ -30,8 +31,8 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:COwsprqRSNZS1IxJrPYgMQ +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2015-08-13 16:33:38 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:/+Vodu8VJxJ0EY9P3Qjjjw # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/perllib/FixMyStreet/DB/Result/Body.pm b/perllib/FixMyStreet/DB/Result/Body.pm index 0c1046cd8..a2e004c6a 100644 --- a/perllib/FixMyStreet/DB/Result/Body.pm +++ b/perllib/FixMyStreet/DB/Result/Body.pm @@ -18,12 +18,6 @@ __PACKAGE__->add_columns( is_nullable => 0, sequence => "body_id_seq", }, - "name", - { data_type => "text", is_nullable => 0 }, - "external_url", - { data_type => "text", is_nullable => 1 }, - "parent", - { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, "endpoint", { data_type => "text", is_nullable => 1 }, "jurisdiction", @@ -42,8 +36,14 @@ __PACKAGE__->add_columns( { data_type => "boolean", default_value => \"false", is_nullable => 0 }, "send_extended_statuses", { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "name", + { data_type => "text", is_nullable => 0 }, + "parent", + { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, "deleted", { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "external_url", + { data_type => "text", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( @@ -87,6 +87,12 @@ __PACKAGE__->belongs_to( }, ); __PACKAGE__->has_many( + "response_templates", + "FixMyStreet::DB::Result::ResponseTemplate", + { "foreign.body_id" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); +__PACKAGE__->has_many( "user_body_permissions", "FixMyStreet::DB::Result::UserBodyPermission", { "foreign.body_id" => "self.id" }, @@ -100,8 +106,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07035 @ 2014-07-29 13:54:07 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:PhUeFDRLSQVMk7Sts5K6MQ +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2015-02-19 16:13:43 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:d6GuQm8vrNmCc4NWw58srA sub url { my ( $self, $c, $args ) = @_; diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 836462ed5..0caaa8968 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -31,8 +31,9 @@ __PACKAGE__->add_columns( "created", { data_type => "timestamp", - default_value => \"ms_current_timestamp()", + default_value => \"current_timestamp", is_nullable => 0, + original => { default_value => \"now()" }, }, "confirmed", { data_type => "timestamp", is_nullable => 1 }, @@ -88,8 +89,8 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07035 @ 2014-07-31 15:59:43 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:08AtJ6CZFyUe7qKMF50MHg +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2015-08-13 16:33:38 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ZR+YNA1Jej3s+8mr52iq6Q # __PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); @@ -144,7 +145,7 @@ sub confirm { my $self = shift; $self->state( 'confirmed' ); - $self->confirmed( \'ms_current_timestamp()' ); + $self->confirmed( \'current_timestamp' ); } =head2 get_photo_params diff --git a/perllib/FixMyStreet/DB/Result/ModerationOriginalData.pm b/perllib/FixMyStreet/DB/Result/ModerationOriginalData.pm index 08d03f94b..d7240cd5d 100644 --- a/perllib/FixMyStreet/DB/Result/ModerationOriginalData.pm +++ b/perllib/FixMyStreet/DB/Result/ModerationOriginalData.pm @@ -33,8 +33,9 @@ __PACKAGE__->add_columns( "created", { data_type => "timestamp", - default_value => \"ms_current_timestamp()", + default_value => \"current_timestamp", is_nullable => 0, + original => { default_value => \"now()" }, }, ); __PACKAGE__->set_primary_key("id"); @@ -46,7 +47,7 @@ __PACKAGE__->belongs_to( { is_deferrable => 0, join_type => "LEFT", - on_delete => "NO ACTION", + on_delete => "CASCADE,", on_update => "NO ACTION", }, ); @@ -54,12 +55,12 @@ __PACKAGE__->belongs_to( "problem", "FixMyStreet::DB::Result::Problem", { id => "problem_id" }, - { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, + { is_deferrable => 0, on_delete => "CASCADE,", on_update => "NO ACTION" }, ); -# Created by DBIx::Class::Schema::Loader v0.07035 @ 2014-07-31 15:59:43 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:yR1Vi7cJQrX67dFwAcJW6w +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2015-08-13 16:33:38 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:DBtGjCJykDtLnGtkj638eA # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index bed2f160a..3b7f8bcfd 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -53,8 +53,9 @@ __PACKAGE__->add_columns( "created", { data_type => "timestamp", - default_value => \"ms_current_timestamp()", + default_value => \"current_timestamp", is_nullable => 0, + original => { default_value => \"now()" }, }, "confirmed", { data_type => "timestamp", is_nullable => 1 }, @@ -71,8 +72,9 @@ __PACKAGE__->add_columns( "lastupdate", { data_type => "timestamp", - default_value => \"ms_current_timestamp()", + default_value => \"current_timestamp", is_nullable => 0, + original => { default_value => \"now()" }, }, "whensent", { data_type => "timestamp", is_nullable => 1 }, @@ -102,6 +104,8 @@ __PACKAGE__->add_columns( { data_type => "integer", default_value => 0, is_nullable => 1 }, "subcategory", { data_type => "text", is_nullable => 1 }, + "bodies_missing", + { data_type => "text", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( @@ -130,8 +134,8 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07035 @ 2014-07-31 15:57:02 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:EvD4sS1mdJJyI1muZ4TrCw +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2015-08-13 16:33:38 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Go+T9oFRfwQ1Ag89qPpF/g # Add fake relationship to stored procedure table __PACKAGE__->has_one( @@ -319,10 +323,6 @@ sub visible_states_remove { } } -sub visible_states_add_unconfirmed { - $_[0]->visible_states_add('unconfirmed') -} - =head2 @states = FixMyStreet::DB::Problem::council_states(); @@ -400,7 +400,7 @@ sub check_for_errors { $errors{bodies} = _('No council selected') unless $self->bodies_str - && $self->bodies_str =~ m/^(?:-1|[\d,]+(?:\|[\d,]+)?)$/; + && $self->bodies_str =~ m/^(?:-1|[\d,]+)$/; if ( !$self->name || $self->name !~ m/\S/ ) { $errors{name} = _('Please enter your name'); @@ -441,15 +441,14 @@ sub confirm { return if $self->state eq 'confirmed'; $self->state('confirmed'); - $self->confirmed( \'ms_current_timestamp()' ); + $self->confirmed( \'current_timestamp' ); return 1; } sub bodies_str_ids { my $self = shift; return unless $self->bodies_str; - (my $bodies = $self->bodies_str) =~ s/\|.*$//; - my @bodies = split( /,/, $bodies ); + my @bodies = split( /,/, $self->bodies_str ); return \@bodies; } @@ -480,11 +479,17 @@ sub url { =head2 get_photo_params -Returns a hashref of details of any attached photo for use in templates. +Returns a hashref of details of the attached photo, if any, for use in templates. + +NB: this method doesn't currently support multiple photos gracefully. + +Use get_photoset($c) instead to do the right thing with reports with 0, 1, or more photos. =cut sub get_photo_params { + # use Carp 'cluck'; + # cluck "get_photo_params called"; # TEMPORARY die to make sure I've done right thing with Zurich templates my $self = shift; return FixMyStreet::App::get_photo_params($self, 'id'); } @@ -634,6 +639,25 @@ sub body { return $body; } +=head2 response_templates + +Returns all ResponseTemplates attached to this problem's bodies, in alphabetical +order of title. + +=cut + +sub response_templates { + my $problem = shift; + return FixMyStreet::App->model('DB::ResponseTemplate')->search( + { + body_id => $problem->bodies_str_ids + }, + { + order_by => 'title' + } + ); +} + # returns true if the external id is the council's ref, i.e., useful to publish it # (by way of an example, the barnet send method returns a useful reference when # it succeeds, so that is the ref we should show on the problem report page). @@ -642,8 +666,7 @@ sub body { # Note: this only makes sense when called on a problem that has been sent! sub can_display_external_id { my $self = shift; - if ($self->external_id && $self->send_method_used && - ($self->send_method_used eq 'barnet' || $self->bodies_str =~ /2237/)) { + if ($self->external_id && $self->send_method_used && $self->bodies_str =~ /2237/) { return 1; } return 0; @@ -735,8 +758,8 @@ sub update_from_open311_service_request { { problem_id => $self->id, state => 'confirmed', - created => $updated || \'ms_current_timestamp()', - confirmed => \'ms_current_timestamp()', + created => $updated || \'current_timestamp', + confirmed => \'current_timestamp', text => $status_notes, mark_open => 0, mark_fixed => 0, @@ -789,7 +812,7 @@ sub update_send_failed { $self->update( { send_fail_count => $self->send_fail_count + 1, - send_fail_timestamp => \'ms_current_timestamp()', + send_fail_timestamp => \'current_timestamp', send_fail_reason => $msg } ); } @@ -828,6 +851,27 @@ sub latest_moderation_log_entry { return $self->admin_log_entries->search({ action => 'moderation' }, { order_by => 'id desc' })->first; } +=head2 get_photoset + +Return a PhotoSet object for all photos attached to this field + + my $photoset = $obj->get_photoset( $c ); + print $photoset->num_images; + return $photoset->get_image_data(num => 0, size => 'full'); + +=cut + +sub get_photoset { + my ($self, $c) = @_; + my $class = 'FixMyStreet::App::Model::PhotoSet'; + eval "use $class"; + return $class->new({ + c => $c, + data => $self->photo, + object => $self, + }); +} + __PACKAGE__->has_many( "admin_log_entries", "FixMyStreet::DB::Result::AdminLog", @@ -838,6 +882,18 @@ __PACKAGE__->has_many( } ); +sub get_time_spent { + my $self = shift; + my $admin_logs = $self->admin_log_entries->search({}, + { + group_by => 'object_id', + columns => [ + { sum_time_spent => { sum => 'time_spent' } }, + ] + })->single; + return $admin_logs ? $admin_logs->get_column('sum_time_spent') : 0; +} + # we need the inline_constructor bit as we don't inherit from Moose __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); diff --git a/perllib/FixMyStreet/DB/Result/ResponseTemplate.pm b/perllib/FixMyStreet/DB/Result/ResponseTemplate.pm new file mode 100644 index 000000000..48a1ab3ae --- /dev/null +++ b/perllib/FixMyStreet/DB/Result/ResponseTemplate.pm @@ -0,0 +1,49 @@ +use utf8; +package FixMyStreet::DB::Result::ResponseTemplate; + +# Created by DBIx::Class::Schema::Loader +# DO NOT MODIFY THE FIRST PART OF THIS FILE + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; +__PACKAGE__->load_components("FilterColumn", "InflateColumn::DateTime", "EncodedColumn"); +__PACKAGE__->table("response_templates"); +__PACKAGE__->add_columns( + "id", + { + data_type => "integer", + is_auto_increment => 1, + is_nullable => 0, + sequence => "response_templates_id_seq", + }, + "body_id", + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, + "title", + { data_type => "text", is_nullable => 0 }, + "text", + { data_type => "text", is_nullable => 0 }, + "created", + { + data_type => "timestamp", + default_value => \"current_timestamp", + is_nullable => 0, + }, +); +__PACKAGE__->set_primary_key("id"); +__PACKAGE__->add_unique_constraint("response_templates_body_id_title_key", ["body_id", "title"]); +__PACKAGE__->belongs_to( + "body", + "FixMyStreet::DB::Result::Body", + { id => "body_id" }, + { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2015-02-19 16:13:43 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:xzhmxtu0taAnBMZN0HBocw + + +# You can replace this text with custom code or comments, and it will be preserved on regeneration +1; diff --git a/perllib/FixMyStreet/DB/Result/Token.pm b/perllib/FixMyStreet/DB/Result/Token.pm index 0156af137..a60e23839 100644 --- a/perllib/FixMyStreet/DB/Result/Token.pm +++ b/perllib/FixMyStreet/DB/Result/Token.pm @@ -20,15 +20,16 @@ __PACKAGE__->add_columns( "created", { data_type => "timestamp", - default_value => \"ms_current_timestamp()", + default_value => \"current_timestamp", is_nullable => 0, + original => { default_value => \"now()" }, }, ); __PACKAGE__->set_primary_key("scope", "token"); -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-08 17:19:55 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:+LLZ8P5GXqPetuGyrra2vw +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2015-08-13 16:33:38 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:HkvzOY5STjOdXN64hxg5NA use mySociety::AuthToken; @@ -42,8 +43,7 @@ Representation of mySociety::AuthToken in the DBIx::Class world. The 'data' value is automatically inflated and deflated in the same way that the AuthToken would do it. 'token' is set to a new random value by default and the -'created' timestamp is achieved using the database function -ms_current_timestamp. +'created' timestamp is achieved using the database function current_timestamp. =cut @@ -55,7 +55,7 @@ sub new { my ( $class, $attrs ) = @_; $attrs->{token} ||= mySociety::AuthToken::random_token(); - $attrs->{created} ||= \'ms_current_timestamp()'; + $attrs->{created} ||= \'current_timestamp'; my $new = $class->next::method($attrs); return $new; diff --git a/perllib/FixMyStreet/DB/ResultSet/Alert.pm b/perllib/FixMyStreet/DB/ResultSet/Alert.pm index 5848265f1..bb1c61141 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Alert.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Alert.pm @@ -14,7 +14,7 @@ sub timeline_created { return $rs->search( { - whensubscribed => { '>=', \"ms_current_timestamp()-'7 days'::interval" }, + whensubscribed => { '>=', \"current_timestamp-'7 days'::interval" }, confirmed => 1, %{ $restriction }, }, @@ -29,7 +29,7 @@ sub timeline_disabled { return $rs->search( { - whendisabled => { '>=', \"ms_current_timestamp()-'7 days'::interval" }, + whendisabled => { '>=', \"current_timestamp-'7 days'::interval" }, %{ $restriction }, }, ); diff --git a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm index 0b430008a..25c727e25 100644 --- a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm +++ b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm @@ -30,18 +30,19 @@ sub email_alerts ($) { $item_table.name as item_name, $item_table.anonymous as item_anonymous, $item_table.confirmed as item_confirmed, $head_table.* - from alert - inner join $item_table on alert.parameter::integer = $item_table.${head_table}_id - inner join $head_table on alert.parameter::integer = $head_table.id + from alert, $item_table, $head_table + where alert.parameter::integer = $head_table.id + and $item_table.${head_table}_id = $head_table.id "; } else { $query .= " $item_table.*, $item_table.id as item_id - from alert, $item_table"; + from alert, $item_table + where 1 = 1"; } $query .= " - where alert_type='$ref' and whendisabled is null and $item_table.confirmed >= whensubscribed - and $item_table.confirmed >= ms_current_timestamp() - '7 days'::interval + and alert_type='$ref' and whendisabled is null and $item_table.confirmed >= whensubscribed + and $item_table.confirmed >= current_timestamp - '7 days'::interval and (select whenqueued from alert_sent where alert_sent.alert_id = alert.id and alert_sent.parameter::integer = $item_table.id) is null and $item_table.user_id <> alert.user_id and " . $alert_type->item_where . " @@ -69,8 +70,6 @@ sub email_alerts ($) { # this is for the new_updates alerts next if $row->{non_public} and $row->{user_id} != $row->{alert_user_id}; - my $hashref_restriction = $cobrand->site_restriction( $row->{cobrand_data} ); - FixMyStreet::App->model('DB::AlertSent')->create( { alert_id => $row->{alert_id}, parameter => $row->{item_id}, @@ -89,10 +88,7 @@ sub email_alerts ($) { $data{state_message} = _("This report is currently marked as open."); } - my $url = $cobrand->base_url( $row->{alert_cobrand_data} ); - if ( $hashref_restriction && $hashref_restriction->{bodies_str} && $row->{bodies_str} ne $hashref_restriction->{bodies_str} ) { - $url = mySociety::Config::get('BASE_URL'); - } + my $url = $cobrand->base_url_for_report($row); # this is currently only for new_updates if ($row->{item_text}) { if ( $cobrand->moniker ne 'zurich' && $row->{alert_user_id} == $row->{user_id} ) { @@ -171,7 +167,6 @@ sub email_alerts ($) { my $longitude = $alert->parameter; my $latitude = $alert->parameter2; - my $hashref_restriction = $cobrand->site_restriction( $alert->cobrand_data ); my $d = mySociety::Gaze::get_radius_containing_population($latitude, $longitude, 200000); # Convert integer to GB locale string (with a ".") $d = mySociety::Locale::in_gb_locale { @@ -184,7 +179,7 @@ sub email_alerts ($) { and problem.user_id = users.id and problem.state in ($states) and problem.non_public = 'f' - and problem.confirmed >= ? and problem.confirmed >= ms_current_timestamp() - '7 days'::interval + and problem.confirmed >= ? and problem.confirmed >= current_timestamp - '7 days'::interval and (select whenqueued from alert_sent where alert_sent.alert_id = ? and alert_sent.parameter::integer = problem.id) is null and users.email <> ? order by confirmed desc"; @@ -195,10 +190,7 @@ sub email_alerts ($) { alert_id => $alert->id, parameter => $row->{id}, } ); - my $url = $cobrand->base_url( $alert->cobrand_data ); - if ( $hashref_restriction && $hashref_restriction->{bodies_str} && $row->{bodies_str} ne $hashref_restriction->{bodies_str} ) { - $url = mySociety::Config::get('BASE_URL'); - } + my $url = $cobrand->base_url_for_report($row); $data{data} .= $url . "/report/" . $row->{id} . " - $row->{title}\n\n"; if ( exists $row->{geocode} && $row->{geocode} ) { my $nearest_st = _get_address_from_gecode( $row->{geocode} ); @@ -241,15 +233,13 @@ sub _send_aggregated_alert_email(%) { my $template = FixMyStreet->get_email_template($cobrand->moniker, $data{lang}, "$data{template}.txt"); - my $sender = FixMyStreet->config('DO_NOT_REPLY_EMAIL'); my $result = FixMyStreet::App->send_email_cron( { _template_ => $template, _parameters_ => \%data, - From => [ $sender, _($cobrand->contact_name) ], To => $data{alert_email}, }, - $sender, + undef, 0, $cobrand, $data{lang} diff --git a/perllib/FixMyStreet/DB/ResultSet/Comment.pm b/perllib/FixMyStreet/DB/ResultSet/Comment.pm index 70f8027aa..1b6afb819 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Comment.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Comment.pm @@ -4,19 +4,24 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; +sub to_body { + my ($rs, $body_restriction) = @_; + return FixMyStreet::DB::ResultSet::Problem::to_body($rs, $body_restriction); +} + + sub timeline { - my ( $rs, $restriction ) = @_; + my ( $rs, $body_restriction ) = @_; my $prefetch = FixMyStreet::App->model('DB')->schema->storage->sql_maker->quote_char ? [ qw/user/ ] : []; - return $rs->search( + return $rs->to_body($body_restriction)->search( { state => 'confirmed', - created => { '>=', \"ms_current_timestamp()-'7 days'::interval" }, - %{ $restriction }, + created => { '>=', \"current_timestamp-'7 days'::interval" }, }, { prefetch => $prefetch, @@ -25,17 +30,18 @@ sub timeline { } sub summary_count { - my ( $rs, $restriction ) = @_; + my ( $rs, $body_restriction ) = @_; - return $rs->search( - $restriction, - { - group_by => ['me.state'], - select => [ 'me.state', { count => 'me.id' } ], - as => [qw/state state_count/], - join => 'problem' - } - ); + my $params = { + group_by => ['me.state'], + select => [ 'me.state', { count => 'me.id' } ], + as => [qw/state state_count/], + }; + if ($body_restriction) { + $rs = $rs->to_body($body_restriction); + $params->{join} = 'problem'; + } + return $rs->search(undef, $params); } 1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Contact.pm b/perllib/FixMyStreet/DB/ResultSet/Contact.pm index 6fa6a03a0..f402b5461 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Contact.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Contact.pm @@ -4,6 +4,8 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; +sub me { join('.', shift->current_source_alias, shift || q{}) } + =head2 not_deleted $rs = $rs->not_deleted(); @@ -14,7 +16,7 @@ Filter down to not deleted contacts - which have C<deleted> set to false; sub not_deleted { my $rs = shift; - return $rs->search( { deleted => 0 } ); + return $rs->search( { $rs->me('deleted') => 0 } ); } sub summary_count { diff --git a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm index a0ccb8a6d..a6b00ef7b 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm @@ -19,12 +19,10 @@ sub nearby { if $interval; $params->{id} = { -not_in => $ids } if $ids; - $params = { - %{ $c->cobrand->problems_clause }, - %$params - } if $c->cobrand->problems_clause; $params->{category} = $category if $category; + $rs = FixMyStreet::DB::ResultSet::Problem::to_body($rs, $c->cobrand->body_restriction); + my $attrs = { prefetch => 'problem', bind => [ $mid_lat, $mid_lon, $dist ], @@ -36,16 +34,4 @@ sub nearby { return \@problems; } -# XXX Not currently used, so not migrating at present. -#sub fixed_nearby { -# my ($dist, $mid_lat, $mid_lon) = @_; -# mySociety::Locale::in_gb_locale { select_all( -# "select id, title, latitude, longitude, distance -# from problem_find_nearby(?, ?, $dist) as nearby, problem -# where nearby.problem_id = problem.id and state='fixed' -# site_restriction -# order by lastupdate desc", $mid_lat, $mid_lon); -# } -#} - 1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index 7885c28b3..e9f5d0f8e 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -20,6 +20,18 @@ sub set_restriction { $site_key = $key; } +sub to_body { + my ($rs, $bodies) = @_; + return $rs unless $bodies; + unless (ref $bodies eq 'ARRAY') { + $bodies = [ map { ref $_ ? $_->id : $_ } $bodies ]; + } + $rs = $rs->search( + \[ "regexp_split_to_array(bodies_str, ',') && ?", [ {} => $bodies ] ] + ); + return $rs; +} + # Front page statistics sub recent_fixed { @@ -168,9 +180,9 @@ sub timeline { return $rs->search( { -or => { - created => { '>=', \"ms_current_timestamp()-'7 days'::interval" }, - confirmed => { '>=', \"ms_current_timestamp()-'7 days'::interval" }, - whensent => { '>=', \"ms_current_timestamp()-'7 days'::interval" }, + created => { '>=', \"current_timestamp-'7 days'::interval" }, + confirmed => { '>=', \"current_timestamp-'7 days'::interval" }, + whensent => { '>=', \"current_timestamp-'7 days'::interval" }, } }, { @@ -198,7 +210,10 @@ sub unique_users { return $rs->search( { state => [ FixMyStreet::DB::Result::Problem->visible_states() ], }, { - select => [ { count => { distinct => 'user_id' } } ], + select => [ { distinct => 'user_id' } ], + as => [ 'user_id' ] + } )->as_subselect_rs->search( undef, { + select => [ { count => 'user_id' } ], as => [ 'count' ] } )->first->get_column('count'); } @@ -235,8 +250,8 @@ sub send_reports { my $site = $site_override || CronFns::site($base_url); my $states = [ 'confirmed', 'fixed' ]; - $states = [ 'unconfirmed', 'confirmed', 'in progress', 'planned', 'closed' ] if $site eq 'zurich'; - my $unsent = FixMyStreet::App->model("DB::Problem")->search( { + $states = [ 'unconfirmed', 'confirmed', 'in progress', 'planned', 'closed', 'investigating' ] if $site eq 'zurich'; + my $unsent = $rs->search( { state => $states, whensent => undef, bodies_str => { '!=', undef }, @@ -320,15 +335,19 @@ sub send_reports { $cobrand->process_additional_metadata_for_email($row, \%h); } - # XXX Needs locks! - # XXX Only copes with at most one missing body - my ($bodies, $missing) = $row->bodies_str =~ /^([\d,]+)(?:\|(\d+))?/; - my @bodies = split(/,/, $bodies); - $bodies = FixMyStreet::App->model("DB::Body")->search( - { id => \@bodies }, + my $bodies = FixMyStreet::App->model("DB::Body")->search( + { id => $row->bodies_str_ids }, { order_by => 'name' }, ); - $missing = FixMyStreet::App->model("DB::Body")->find($missing) if $missing; + + my $missing; + if ($row->bodies_missing) { + my @missing = FixMyStreet::App->model("DB::Body")->search( + { id => [ split /,/, $row->bodies_missing ] }, + { order_by => 'name' } + )->get_column('name')->all; + $missing = join(' / ', @missing) if @missing; + } my @dear; my %reporters = (); @@ -337,7 +356,7 @@ sub send_reports { my $sender = "FixMyStreet::SendReport::" . $sender_info->{method}; if ( ! exists $senders->{ $sender } ) { - warn "No such sender [ $sender ] for body $body->name ( $body->id )"; + warn sprintf "No such sender [ $sender ] for body %s ( %d )", $body->name, $body->id; next; } $reporters{ $sender } ||= $sender->new(); @@ -400,7 +419,7 @@ sub send_reports { $h{missing} = ''; if ($missing) { $h{missing} = '[ ' - . sprintf(_('We realise this problem might be the responsibility of %s; however, we don\'t currently have any contact details for them. If you know of an appropriate contact address, please do get in touch.'), $missing->name) + . sprintf(_('We realise this problem might be the responsibility of %s; however, we don\'t currently have any contact details for them. If you know of an appropriate contact address, please do get in touch.'), $missing) . " ]\n\n"; } @@ -433,8 +452,8 @@ sub send_reports { unless ($result) { $row->update( { - whensent => \'ms_current_timestamp()', - lastupdate => \'ms_current_timestamp()', + whensent => \'current_timestamp', + lastupdate => \'current_timestamp', } ); if ( $cobrand->report_sent_confirmation_email && !$h{anonymous_report}) { _send_report_sent_email( $row, \%h, $nomail, $cobrand ); @@ -468,7 +487,7 @@ sub send_reports { } } my $sending_errors = ''; - my $unsent = FixMyStreet::App->model("DB::Problem")->search( { + my $unsent = $rs->search( { state => [ 'confirmed', 'fixed' ], whensent => undef, bodies_str => { '!=', undef }, @@ -499,7 +518,7 @@ sub _send_report_sent_email { _template_ => $template, _parameters_ => $h, To => $row->user->email, - From => mySociety::Config::get('CONTACT_EMAIL'), + From => [ mySociety::Config::get('CONTACT_EMAIL'), $cobrand->contact_name ], }, mySociety::Config::get('CONTACT_EMAIL'), $nomail, diff --git a/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm b/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm index 63a91697d..bf1c68c49 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm @@ -22,7 +22,7 @@ sub send_questionnaires_period { whensent => [ '-and', { '!=', undef }, - { '<', \"ms_current_timestamp() - '$period'::interval" }, + { '<', \"current_timestamp - '$period'::interval" }, ], send_questionnaire => 1, }; @@ -34,7 +34,7 @@ sub send_questionnaires_period { } else { $q_params->{'-or'} = [ '(select max(whensent) from questionnaire where me.id=problem_id)' => undef, - '(select max(whenanswered) from questionnaire where me.id=problem_id)' => { '<', \"ms_current_timestamp() - '$period'::interval" } + '(select max(whenanswered) from questionnaire where me.id=problem_id)' => { '<', \"current_timestamp - '$period'::interval" } ]; } @@ -70,7 +70,7 @@ sub send_questionnaires_period { my $questionnaire = FixMyStreet::App->model('DB::Questionnaire')->create( { problem_id => $row->id, - whensent => \'ms_current_timestamp()', + whensent => \'current_timestamp', } ); # We won't send another questionnaire unless they ask for it (or it was @@ -84,9 +84,6 @@ sub send_questionnaires_period { } ); $h{url} = $cobrand->base_url($row->cobrand_data) . '/Q/' . $token->token; - my $sender = FixMyStreet->config('DO_NOT_REPLY_EMAIL'); - my $sender_name = _($cobrand->contact_name); - print "Sending questionnaire " . $questionnaire->id . ", problem " . $row->id . ", token " . $token->token . " to " . $row->user->email . "\n" @@ -97,9 +94,8 @@ sub send_questionnaires_period { _template_ => $template, _parameters_ => \%h, To => [ [ $row->user->email, $row->name ] ], - From => [ $sender, $sender_name ], }, - $sender, + undef, $params->{nomail}, $cobrand ); @@ -117,32 +113,36 @@ sub send_questionnaires_period { sub timeline { my ( $rs, $restriction ) = @_; + my $attrs; + if (%$restriction) { + $attrs = { + -select => [qw/me.*/], + prefetch => [qw/problem/], + } + } return $rs->search( { -or => { - whenanswered => { '>=', \"ms_current_timestamp()-'7 days'::interval" }, - 'me.whensent' => { '>=', \"ms_current_timestamp()-'7 days'::interval" }, + whenanswered => { '>=', \"current_timestamp-'7 days'::interval" }, + 'me.whensent' => { '>=', \"current_timestamp-'7 days'::interval" }, }, %{ $restriction }, }, - { - -select => [qw/me.*/], - prefetch => [qw/problem/], - } + $attrs ); } sub summary_count { my ( $rs, $restriction ) = @_; - return $rs->search( - $restriction, - { - group_by => [ \'whenanswered is not null' ], - select => [ \'(whenanswered is not null)', { count => 'me.id' } ], - as => [qw/answered questionnaire_count/], - join => 'problem' - } - ); + my $params = { + group_by => [ \'whenanswered is not null' ], + select => [ \'(whenanswered is not null)', { count => 'me.id' } ], + as => [qw/answered questionnaire_count/], + }; + if (%$restriction) { + $params->{join} = 'problem'; + } + return $rs->search($restriction, $params); } 1; |