diff options
Diffstat (limited to 'perllib/FixMyStreet/DB')
-rw-r--r-- | perllib/FixMyStreet/DB/RABXColumn.pm | 98 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Alert.pm | 39 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/AlertSent.pm | 6 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Body.pm | 112 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/BodyArea.pm | 33 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 78 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Contact.pm | 37 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/ContactsHistory.pm | 6 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Open311conf.pm | 60 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 325 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Questionnaire.pm | 34 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Token.pm | 24 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/User.pm | 60 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/AlertType.pm | 46 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Body.pm | 17 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Nearby.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 157 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm | 4 |
18 files changed, 718 insertions, 425 deletions
diff --git a/perllib/FixMyStreet/DB/RABXColumn.pm b/perllib/FixMyStreet/DB/RABXColumn.pm new file mode 100644 index 000000000..5f1583018 --- /dev/null +++ b/perllib/FixMyStreet/DB/RABXColumn.pm @@ -0,0 +1,98 @@ +package FixMyStreet::DB::RABXColumn; + +use strict; +use warnings; + +use IO::String; +use RABX; + +=head1 NAME + +FixMyStreet::DB::RABXColumn + +=head2 DESCRIPTION + +This is a helper component that will setup the RABX serialisation for some +fields. This is useful for when you want to persist some data structure such as +hashrefs etc. + +This code will also change the default FilterColumn behaviour so that whenever +your set a column, or specify a RABX'd column in an ->update the value is saved +to the database. The default behaviour is to check if the value is already set, +and for hashrefs this means that changes to the contents are missed as it is +still the same hashref. + +By putting all this code in one place there is also much less repetition. + +=cut + +# Store which columns are RABX cols. +# $RABX_COLUMNS{$class}{$col} = 1 +my %RABX_COLUMNS = (); + +sub _get_class_identifier { + my $class = ref $_[0] || $_[0]; + $class =~ s/.*?(\w+)$/$1/; + return $class; +} + +=head1 METHODS + +=head2 rabx_column + + # In one of your ::Result:: modules + __PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); + __PACKAGE__->rabx_column('data'); + +This sets up the filtering to and from the database, and also changes the +set_filtered_column behaviour to not trust the cache. + +=cut + +sub rabx_column { + my ($class, $col) = @_; + + # Apply the filtering for this column + $class->filter_column( + $col => { + filter_from_storage => sub { + my $self = shift; + my $ser = shift; + return undef unless defined $ser; + utf8::encode($ser) if utf8::is_utf8($ser); + my $h = new IO::String($ser); + return RABX::wire_rd($h); + }, + filter_to_storage => sub { + my $self = shift; + my $data = shift; + my $ser = ''; + my $h = new IO::String($ser); + RABX::wire_wr( $data, $h ); + return $ser; + }, + } + ); + + # store that this column is a RABX column. + $RABX_COLUMNS{ _get_class_identifier($class) }{$col} = 1; +} + + +sub set_filtered_column { + my ($self, $col, $val) = @_; + + my $class = ref $self; + + # because filtered objects may be expensive to marshall for storage there + # is a cache that attempts to detect if they have changed or not. For us + # this cache breaks things and our marshalling is cheap, so clear it when + # trying set a column. + delete $self->{_filtered_column}{$col} + if $RABX_COLUMNS{ _get_class_identifier($class) }{$col}; + + return $self->next::method($col, $val); +} + + +1; diff --git a/perllib/FixMyStreet/DB/Result/Alert.pm b/perllib/FixMyStreet/DB/Result/Alert.pm index ca9ad45c2..4ce72f873 100644 --- a/perllib/FixMyStreet/DB/Result/Alert.pm +++ b/perllib/FixMyStreet/DB/Result/Alert.pm @@ -48,7 +48,7 @@ __PACKAGE__->belongs_to( "alert_type", "FixMyStreet::DB::Result::AlertType", { ref => "alert_type" }, - { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, + { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); __PACKAGE__->has_many( "alerts_sent", @@ -60,12 +60,12 @@ __PACKAGE__->belongs_to( "user", "FixMyStreet::DB::Result::User", { id => "user_id" }, - { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, + { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-08 17:19:55 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:vump36YxUO4FQi5Do6DwvA +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:d9yIFiTGtbtFaULXZNKstQ # You can replace this text with custom code or comments, and it will be preserved on regeneration @@ -77,22 +77,21 @@ with 'FixMyStreet::Roles::Abuser'; my $tz = DateTime::TimeZone->new( name => "local" ); - -sub whensubscribed_local { - my $self = shift; - - return $self->whensubscribed - ? $self->whensubscribed->set_time_zone($tz) - : $self->whensubscribed; -} - -sub whendisabled_local { - my $self = shift; - - return $self->whendisabled - ? $self->whendisabled->set_time_zone($tz) - : $self->whendisabled; -} +my $tz_f; +$tz_f = DateTime::TimeZone->new( name => FixMyStreet->config('TIME_ZONE') ) + if FixMyStreet->config('TIME_ZONE'); + +my $stz = sub { + my ( $orig, $self ) = ( shift, shift ); + my $s = $self->$orig(@_); + return $s unless $s && UNIVERSAL::isa($s, "DateTime"); + $s->set_time_zone($tz); + $s->set_time_zone($tz_f) if $tz_f; + return $s; +}; + +around whensubscribed => $stz; +around whendisabled => $stz; =head2 confirm diff --git a/perllib/FixMyStreet/DB/Result/AlertSent.pm b/perllib/FixMyStreet/DB/Result/AlertSent.pm index a537c95cd..422e010a9 100644 --- a/perllib/FixMyStreet/DB/Result/AlertSent.pm +++ b/perllib/FixMyStreet/DB/Result/AlertSent.pm @@ -26,12 +26,12 @@ __PACKAGE__->belongs_to( "alert", "FixMyStreet::DB::Result::Alert", { id => "alert_id" }, - { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, + { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-08 17:19:55 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:oN+36hDWJuc0hqkCW9BHOw +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:COwsprqRSNZS1IxJrPYgMQ # 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 new file mode 100644 index 000000000..be4adeca9 --- /dev/null +++ b/perllib/FixMyStreet/DB/Result/Body.pm @@ -0,0 +1,112 @@ +use utf8; +package FixMyStreet::DB::Result::Body; + +# 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("body"); +__PACKAGE__->add_columns( + "id", + { + data_type => "integer", + is_auto_increment => 1, + is_nullable => 0, + sequence => "body_id_seq", + }, + "name", + { data_type => "text", is_nullable => 0 }, + "parent", + { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, + "endpoint", + { data_type => "text", is_nullable => 1 }, + "jurisdiction", + { data_type => "text", is_nullable => 1 }, + "api_key", + { data_type => "text", is_nullable => 1 }, + "send_method", + { data_type => "text", is_nullable => 1 }, + "send_comments", + { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "comment_user_id", + { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, + "suppress_alerts", + { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "can_be_devolved", + { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "send_extended_statuses", + { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "deleted", + { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "external_url", + { data_type => "text", is_nullable => 0 }, +); +__PACKAGE__->set_primary_key("id"); +__PACKAGE__->has_many( + "bodies", + "FixMyStreet::DB::Result::Body", + { "foreign.parent" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); +__PACKAGE__->has_many( + "body_areas", + "FixMyStreet::DB::Result::BodyArea", + { "foreign.body_id" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); +__PACKAGE__->belongs_to( + "comment_user", + "FixMyStreet::DB::Result::User", + { id => "comment_user_id" }, + { + is_deferrable => 0, + join_type => "LEFT", + on_delete => "NO ACTION", + on_update => "NO ACTION", + }, +); +__PACKAGE__->has_many( + "contacts", + "FixMyStreet::DB::Result::Contact", + { "foreign.body_id" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); +__PACKAGE__->belongs_to( + "parent", + "FixMyStreet::DB::Result::Body", + { id => "parent" }, + { + is_deferrable => 0, + join_type => "LEFT", + on_delete => "NO ACTION", + on_update => "NO ACTION", + }, +); +__PACKAGE__->has_many( + "users", + "FixMyStreet::DB::Result::User", + { "foreign.from_body" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 18:11:23 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hTOxxiiHmC8nmQK/p8dXhQ + +sub url { + my ( $self, $c ) = @_; + # XXX $areas_info was used here for Norway parent - needs body parents, I guess + return $c->uri_for( '/reports/' . $c->cobrand->short_name( $self ) ); +} + +sub areas { + my $self = shift; + my %ids = map { $_->area_id => 1 } $self->body_areas->all; + return \%ids; +} + +1; diff --git a/perllib/FixMyStreet/DB/Result/BodyArea.pm b/perllib/FixMyStreet/DB/Result/BodyArea.pm new file mode 100644 index 000000000..4447777dc --- /dev/null +++ b/perllib/FixMyStreet/DB/Result/BodyArea.pm @@ -0,0 +1,33 @@ +use utf8; +package FixMyStreet::DB::Result::BodyArea; + +# 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("body_areas"); +__PACKAGE__->add_columns( + "body_id", + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, + "area_id", + { data_type => "integer", is_nullable => 0 }, +); +__PACKAGE__->add_unique_constraint("body_areas_body_id_area_id_idx", ["body_id", "area_id"]); +__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 @ 2013-09-10 17:11:54 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:+hzie6kHleUBoEt199c/nQ + + __PACKAGE__->set_primary_key(__PACKAGE__->columns); + +1; diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 8c9fea282..e170a5655 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -54,6 +54,10 @@ __PACKAGE__->add_columns( { data_type => "boolean", default_value => \"false", is_nullable => 0 }, "problem_state", { data_type => "text", is_nullable => 1 }, + "external_id", + { data_type => "text", is_nullable => 1 }, + "extra", + { data_type => "text", is_nullable => 1 }, "send_fail_count", { data_type => "integer", default_value => 0, is_nullable => 0 }, "send_fail_reason", @@ -62,76 +66,52 @@ __PACKAGE__->add_columns( { data_type => "timestamp", is_nullable => 1 }, "whensent", { data_type => "timestamp", is_nullable => 1 }, - "external_id", - { data_type => "text", is_nullable => 1 }, - "extra", - { data_type => "text", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->belongs_to( "problem", "FixMyStreet::DB::Result::Problem", { id => "problem_id" }, - { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, + { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); __PACKAGE__->belongs_to( "user", "FixMyStreet::DB::Result::User", { id => "user_id" }, - { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, + { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-07-11 18:53:26 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:tSejJzLxHD/fMWjpa10lfA - -__PACKAGE__->filter_column( - extra => { - filter_from_storage => sub { - my $self = shift; - my $ser = shift; - return undef unless defined $ser; - my $h = new IO::String($ser); - return RABX::wire_rd($h); - }, - filter_to_storage => sub { - my $self = shift; - my $data = shift; - my $ser = ''; - my $h = new IO::String($ser); - RABX::wire_wr( $data, $h ); - return $ser; - }, - } -); +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:D/+UWcF7JO/EkCiJaAHUOw + +__PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); +__PACKAGE__->rabx_column('extra'); use DateTime::TimeZone; use Image::Size; use Moose; use namespace::clean -except => [ 'meta' ]; -use RABX; with 'FixMyStreet::Roles::Abuser'; my $tz = DateTime::TimeZone->new( name => "local" ); -sub created_local { - my $self = shift; +my $tz_f; +$tz_f = DateTime::TimeZone->new( name => FixMyStreet->config('TIME_ZONE') ) + if FixMyStreet->config('TIME_ZONE'); - return $self->created - ? $self->created->set_time_zone($tz) - : $self->created; -} +my $stz = sub { + my ( $orig, $self ) = ( shift, shift ); + my $s = $self->$orig(@_); + return $s unless $s && UNIVERSAL::isa($s, "DateTime"); + $s->set_time_zone($tz); + $s->set_time_zone($tz_f) if $tz_f; + return $s; +}; -sub confirmed_local { - my $self = shift; - - # if confirmed is null then it doesn't get inflated so don't - # try and set the timezone - return $self->confirmed - ? $self->confirmed->set_time_zone($tz) - : $self->confirmed; -} +around created => $stz; +around confirmed => $stz; # You can replace this text with custom code or comments, and it will be preserved on regeneration @@ -146,6 +126,12 @@ sub check_for_errors { $errors{update} = _('Please enter a message') unless $self->text =~ m/\S/; + # Bromley Council custom character limit + if ( $self->text && $self->problem && $self->problem->bodies_str + && $self->problem->bodies_str eq '2482' && length($self->text) > 1750 ) { + $errors{update} = sprintf( _('Updates are limited to %s characters in length. Please shorten your update'), 1750 ); + } + return \%errors; } @@ -175,8 +161,8 @@ sub get_photo_params { =head2 meta_problem_state -Returns a string suitable for display in the update meta section. -Mostly removes the '- council/user' bit from fixed states +Returns a string suitable for display lookup in the update meta section. +Removes the '- council/user' bit from fixed states. =cut diff --git a/perllib/FixMyStreet/DB/Result/Contact.pm b/perllib/FixMyStreet/DB/Result/Contact.pm index 993e3524b..eca028c9b 100644 --- a/perllib/FixMyStreet/DB/Result/Contact.pm +++ b/perllib/FixMyStreet/DB/Result/Contact.pm @@ -18,8 +18,8 @@ __PACKAGE__->add_columns( is_nullable => 0, sequence => "contacts_id_seq", }, - "area_id", - { data_type => "integer", is_nullable => 0 }, + "body_id", + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, "category", { data_type => "text", default_value => "Other", is_nullable => 0 }, "email", @@ -48,30 +48,19 @@ __PACKAGE__->add_columns( { data_type => "text", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); -__PACKAGE__->add_unique_constraint("contacts_area_id_category_idx", ["area_id", "category"]); +__PACKAGE__->add_unique_constraint("contacts_body_id_category_idx", ["body_id", "category"]); +__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.07017 @ 2012-08-31 10:29:17 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:t6yOPhZmedV/eH6AUvHI6w +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hq/BFHDEu4OUI4MSy3OyHg -__PACKAGE__->filter_column( - extra => { - filter_from_storage => sub { - my $self = shift; - my $ser = shift; - return undef unless defined $ser; - my $h = new IO::String($ser); - return RABX::wire_rd($h); - }, - filter_to_storage => sub { - my $self = shift; - my $data = shift; - my $ser = ''; - my $h = new IO::String($ser); - RABX::wire_wr( $data, $h ); - return $ser; - }, - } -); +__PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); +__PACKAGE__->rabx_column('extra'); 1; diff --git a/perllib/FixMyStreet/DB/Result/ContactsHistory.pm b/perllib/FixMyStreet/DB/Result/ContactsHistory.pm index deb00fb95..7126d91c9 100644 --- a/perllib/FixMyStreet/DB/Result/ContactsHistory.pm +++ b/perllib/FixMyStreet/DB/Result/ContactsHistory.pm @@ -20,7 +20,7 @@ __PACKAGE__->add_columns( }, "contact_id", { data_type => "integer", is_nullable => 0 }, - "area_id", + "body_id", { data_type => "integer", is_nullable => 0 }, "category", { data_type => "text", default_value => "Other", is_nullable => 0 }, @@ -40,8 +40,8 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("contacts_history_id"); -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-08 17:19:55 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dN2ueIDoP3d/+Mg1UDqsMw +# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-12-12 16:37:16 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:sxflEBBn0Mn0s3MroWnWFA # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/perllib/FixMyStreet/DB/Result/Open311conf.pm b/perllib/FixMyStreet/DB/Result/Open311conf.pm deleted file mode 100644 index 8051e27de..000000000 --- a/perllib/FixMyStreet/DB/Result/Open311conf.pm +++ /dev/null @@ -1,60 +0,0 @@ -use utf8; -package FixMyStreet::DB::Result::Open311conf; - -# 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("open311conf"); -__PACKAGE__->add_columns( - "id", - { - data_type => "integer", - is_auto_increment => 1, - is_nullable => 0, - sequence => "open311conf_id_seq", - }, - "area_id", - { data_type => "integer", is_nullable => 0 }, - "endpoint", - { data_type => "text", is_nullable => 0 }, - "jurisdiction", - { data_type => "text", is_nullable => 1 }, - "api_key", - { data_type => "text", is_nullable => 1 }, - "send_method", - { data_type => "text", is_nullable => 1 }, - "send_comments", - { data_type => "boolean", default_value => \"false", is_nullable => 0 }, - "comment_user_id", - { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, - "suppress_alerts", - { data_type => "boolean", default_value => \"false", is_nullable => 0 }, - "can_be_devolved", - { data_type => "boolean", default_value => \"false", is_nullable => 0 }, -); -__PACKAGE__->set_primary_key("id"); -__PACKAGE__->add_unique_constraint("open311conf_area_id_key", ["area_id"]); -__PACKAGE__->belongs_to( - "comment_user", - "FixMyStreet::DB::Result::User", - { id => "comment_user_id" }, - { - is_deferrable => 1, - join_type => "LEFT", - on_delete => "CASCADE", - on_update => "CASCADE", - }, -); - - -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-08-29 14:04:20 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Yoult8K/ldH6DMAKURtr3Q - - -# 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/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 02e5adb7d..3463ebab6 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -24,7 +24,7 @@ __PACKAGE__->add_columns( { data_type => "double precision", is_nullable => 0 }, "longitude", { data_type => "double precision", is_nullable => 0 }, - "council", + "bodies_str", { data_type => "text", is_nullable => 1 }, "areas", { data_type => "text", is_nullable => 0 }, @@ -99,7 +99,7 @@ __PACKAGE__->add_columns( "external_source_id", { data_type => "text", is_nullable => 1 }, "interest_count", - { data_type => "integer", is_nullable => 1 }, + { data_type => "integer", default_value => 0, is_nullable => 1 }, "subcategory", { data_type => "text", is_nullable => 1 }, ); @@ -120,12 +120,12 @@ __PACKAGE__->belongs_to( "user", "FixMyStreet::DB::Result::User", { id => "user_id" }, - { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, + { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-12-03 17:48:10 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:xN/RB8Vx50CwyOeBjvJezQ +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:U/4BT8EGfcCLKA/7LX+qyQ # Add fake relationship to stored procedure table __PACKAGE__->has_one( @@ -135,52 +135,15 @@ __PACKAGE__->has_one( { cascade_copy => 0, cascade_delete => 0 }, ); -__PACKAGE__->filter_column( - extra => { - filter_from_storage => sub { - my $self = shift; - my $ser = shift; - return undef unless defined $ser; - my $h = new IO::String($ser); - return RABX::wire_rd($h); - }, - filter_to_storage => sub { - my $self = shift; - my $data = shift; - my $ser = ''; - my $h = new IO::String($ser); - RABX::wire_wr( $data, $h ); - return $ser; - }, - } -); - -__PACKAGE__->filter_column( - geocode => { - filter_from_storage => sub { - my $self = shift; - my $ser = shift; - return undef unless defined $ser; - my $h = new IO::String($ser); - return RABX::wire_rd($h); - }, - filter_to_storage => sub { - my $self = shift; - my $data = shift; - my $ser = ''; - my $h = new IO::String($ser); - RABX::wire_wr( $data, $h ); - return $ser; - }, - } -); +__PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); +__PACKAGE__->rabx_column('extra'); +__PACKAGE__->rabx_column('geocode'); use DateTime::TimeZone; use Image::Size; use Moose; use namespace::clean -except => [ 'meta' ]; use Utils; -use RABX; with 'FixMyStreet::Roles::Abuser'; @@ -196,10 +159,11 @@ HASHREF. sub open_states { my $states = { - 'confirmed' => 1, - 'investigating' => 1, - 'planned' => 1, - 'in progress' => 1, + 'confirmed' => 1, + 'investigating' => 1, + 'in progress' => 1, + 'planned' => 1, + 'action scheduled' => 1, }; return wantarray ? keys %{$states} : $states; @@ -237,7 +201,11 @@ HASHREF. sub closed_states { my $states = { - 'closed' => 1, + 'closed' => 1, + 'unable to fix' => 1, + 'not responsible' => 1, + 'duplicate' => 1, + 'internal referral' => 1, }; return wantarray ? keys %{$states} : $states; @@ -248,61 +216,111 @@ sub closed_states { @states = FixMyStreet::DB::Problem::visible_states(); -Get a list or states that should be visible on the site. If called in +Get a list of states that should be visible on the site. If called in array context then returns an array of names, otherwise returns a HASHREF. =cut +my $visible_states = { + 'confirmed' => 1, + 'investigating' => 1, + 'in progress' => 1, + 'planned' => 1, + 'action scheduled' => 1, + 'fixed' => 1, + 'fixed - council' => 1, + 'fixed - user' => 1, + 'unable to fix' => 1, + 'not responsible' => 1, + 'duplicate' => 1, + 'closed' => 1, + 'internal referral' => 1, +}; sub visible_states { + return wantarray ? keys %{$visible_states} : $visible_states; +} +sub visible_states_add_unconfirmed { + $visible_states->{unconfirmed} = 1; +} + +=head2 + + @states = FixMyStreet::DB::Problem::all_states(); + +Get a list of all states that a problem can have. If called in +array context then returns an array of names, otherwise returns a +HASHREF. + +=cut + +sub all_states { my $states = { - 'confirmed' => 1, - 'planned' => 1, - 'investigating' => 1, - 'in progress' => 1, - 'fixed' => 1, - 'fixed - council' => 1, - 'fixed - user' => 1, - 'closed' => 1, + 'hidden' => 1, + 'partial' => 1, + 'unconfirmed' => 1, + 'confirmed' => 1, + 'investigating' => 1, + 'in progress' => 1, + 'planned' => 1, + 'action scheduled' => 1, + 'fixed' => 1, + 'fixed - council' => 1, + 'fixed - user' => 1, + 'unable to fix' => 1, + 'not responsible' => 1, + 'duplicate' => 1, + 'closed' => 1, + 'internal referral' => 1, }; return wantarray ? keys %{$states} : $states; } +=head2 -my $tz = DateTime::TimeZone->new( name => "local" ); - -sub confirmed_local { - my $self = shift; + @states = FixMyStreet::DB::Problem::council_states(); - return $self->confirmed - ? $self->confirmed->set_time_zone($tz) - : $self->confirmed; -} +Get a list of states that are availble to council users. If called in +array context then returns an array of names, otherwise returns a +HASHREF. -sub created_local { - my $self = shift; +=cut +sub council_states { + my $states = { + 'confirmed' => 1, + 'investigating' => 1, + 'action scheduled' => 1, + 'in progress' => 1, + 'fixed - council' => 1, + 'unable to fix' => 1, + 'not responsible' => 1, + 'duplicate' => 1, + 'internal referral' => 1, + }; - return $self->created - ? $self->created->set_time_zone($tz) - : $self->created; + return wantarray ? keys %{$states} : $states; } -sub whensent_local { - my $self = shift; +my $tz = DateTime::TimeZone->new( name => "local" ); - return $self->whensent - ? $self->whensent->set_time_zone($tz) - : $self->whensent; -} +my $tz_f; +$tz_f = DateTime::TimeZone->new( name => FixMyStreet->config('TIME_ZONE') ) + if FixMyStreet->config('TIME_ZONE'); -sub lastupdate_local { - my $self = shift; +my $stz = sub { + my ( $orig, $self ) = ( shift, shift ); + my $s = $self->$orig(@_); + return $s unless $s && UNIVERSAL::isa($s, "DateTime"); + $s->set_time_zone($tz); + $s->set_time_zone($tz_f) if $tz_f; + return $s; +}; - return $self->lastupdate - ? $self->lastupdate->set_time_zone($tz) - : $self->lastupdate; -} +around created => $stz; +around confirmed => $stz; +around whensent => $stz; +around lastupdate => $stz; around service => sub { my ( $orig, $self ) = ( shift, shift ); @@ -311,6 +329,12 @@ around service => sub { return $s; }; +sub title_safe { + my $self = shift; + return _('Awaiting moderation') if $self->cobrand eq 'zurich' && $self->state eq 'unconfirmed'; + return $self->title; +} + =head2 check_for_errors $error_hashref = $problem->check_for_errors(); @@ -335,9 +359,9 @@ sub check_for_errors { $errors{detail} = _('Please enter some details') unless $self->detail =~ m/\S/; - $errors{council} = _('No council selected') - unless $self->council - && $self->council =~ m/^(?:-1|[\d,]+(?:\|[\d,]+)?)$/; + $errors{bodies} = _('No council selected') + unless $self->bodies_str + && $self->bodies_str =~ m/^(?:-1|[\d,]+(?:\|[\d,]+)?)$/; if ( !$self->name || $self->name !~ m/\S/ ) { $errors{name} = _('Please enter your name'); @@ -348,7 +372,7 @@ sub check_for_errors { { $errors{name} = _( 'Please enter your full name, councils need this information – if you do not wish your name to be shown on the site, untick the box below' - ); + ) unless $self->cobrand eq 'emptyhomes'; } if ( $self->category @@ -364,6 +388,18 @@ sub check_for_errors { $self->category(undef); } + if ( $self->bodies_str && $self->detail ) { + # Custom character limit: + # Bromley Council + if ( $self->bodies_str eq '2482' && length($self->detail) > 1750 ) { + $errors{detail} = sprintf( _('Reports are limited to %s characters in length. Please shorten your report'), 1750 ); + } + # Oxfordshire + if ( $self->bodies_str eq '2237' && length($self->detail) > 1700 ) { + $errors{detail} = sprintf( _('Reports are limited to %s characters in length. Please shorten your report'), 1700 ); + } + } + return \%errors; } @@ -390,18 +426,26 @@ sub confirm { return 1; } -=head2 councils +sub bodies_str_ids { + my $self = shift; + return unless $self->bodies_str; + (my $bodies = $self->bodies_str) =~ s/\|.*$//; + my @bodies = split( /,/, $bodies ); + return \@bodies; +} -Returns an arrayref of councils to which a report was sent. +=head2 bodies + +Returns a hashref of bodies to which a report was sent. =cut -sub councils { +sub bodies($) { my $self = shift; - return [] unless $self->council; - (my $council = $self->council) =~ s/\|.*$//; - my @council = split( /,/, $council ); - return \@council; + return {} unless $self->bodies_str; + my $bodies = $self->bodies_str_ids; + my @bodies = FixMyStreet::App->model('DB::Body')->search({ id => $bodies })->all; + return { map { $_->id => $_ } @bodies }; } =head2 url @@ -485,8 +529,7 @@ meta data about the report. sub meta_line { my ( $problem, $c ) = @_; - my $date_time = - Utils::prettify_epoch( $problem->confirmed_local->epoch ); + my $date_time = Utils::prettify_dt( $problem->confirmed ); my $meta = ''; # FIXME Should be in cobrand @@ -494,11 +537,7 @@ sub meta_line { my $category = _($problem->category); utf8::decode($category); - if ($problem->anonymous) { - $meta = sprintf(_('%s, reported anonymously at %s'), $category, $date_time); - } else { - $meta = sprintf(_('%s, reported by %s at %s'), $category, $problem->name, $date_time); - } + $meta = sprintf(_('%s, reported at %s'), $category, $date_time); } else { @@ -507,11 +546,11 @@ sub meta_line { and $problem->category && $problem->category ne _('Other') ) { $meta = - sprintf( _('Reported by %s in the %s category anonymously at %s'), + sprintf( _('Reported via %s in the %s category anonymously at %s'), $problem->service, $problem->category, $date_time ); } elsif ( $problem->service ) { - $meta = sprintf( _('Reported by %s anonymously at %s'), + $meta = sprintf( _('Reported via %s anonymously at %s'), $problem->service, $date_time ); } elsif ( $problem->category and $problem->category ne _('Other') ) { @@ -527,13 +566,13 @@ sub meta_line { and $problem->category && $problem->category ne _('Other') ) { $meta = sprintf( - _('Reported by %s in the %s category by %s at %s'), + _('Reported via %s in the %s category by %s at %s'), $problem->service, $problem->category, $problem->name, $date_time ); } elsif ( $problem->service ) { - $meta = sprintf( _('Reported by %s by %s at %s'), + $meta = sprintf( _('Reported via %s by %s at %s'), $problem->service, $problem->name, $date_time ); } elsif ( $problem->category and $problem->category ne _('Other') ) { @@ -555,21 +594,22 @@ sub body { my ( $problem, $c ) = @_; my $body; if ($problem->external_body) { - $body = $problem->external_body; + if ($problem->cobrand eq 'zurich') { + $body = $c->model('DB::Body')->find({ id => $problem->external_body }); + } else { + $body = $problem->external_body; + } } else { - my $councils = $problem->councils; - my $areas_info = mySociety::MaPit::call('areas', $councils); + my $bodies = $problem->bodies; $body = join( _(' and '), map { - my $name = $areas_info->{$_}->{name}; + my $name = $_->name; if (mySociety::Config::get('AREA_LINKS_FROM_PROBLEMS')) { - '<a href="' - . $c->uri_for( '/reports/' . $c->cobrand->short_name( $areas_info->{$_} ) ) - . '">' . $name . '</a>'; + '<a href="' . $_->url($c) . '">' . $name . '</a>'; } else { $name; } - } @$councils + } values %$bodies ); } return $body; @@ -583,10 +623,10 @@ 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') { + if ($self->external_id && $self->bodies_str =~ /2237/) { return 1; } - return 0; + return 0; } # TODO Some/much of this could be moved to the template @@ -603,7 +643,7 @@ sub processed_summary_string { my ( $problem, $c ) = @_; my ($duration_clause, $external_ref_clause); if ($problem->whensent) { - $duration_clause = $problem->duration_string($c) + $duration_clause = $problem->duration_string($c); } if ($problem->can_display_external_id) { if ($duration_clause) { @@ -623,19 +663,30 @@ sub duration_string { my ( $problem, $c ) = @_; my $body = $problem->body( $c ); return sprintf(_('Sent to %s %s later'), $body, - Utils::prettify_duration($problem->whensent_local->epoch - $problem->confirmed_local->epoch, 'minute') + Utils::prettify_duration($problem->whensent->epoch - $problem->confirmed->epoch, 'minute') ); } +sub local_coords { + my $self = shift; + if ($self->cobrand eq 'zurich') { + my ($x, $y) = Geo::Coordinates::CH1903::from_latlon($self->latitude, $self->longitude); + return ( int($x+0.5), int($y+0.5) ); + } +} + =head2 update_from_open311_service_request - $p->update_from_open311_service_request( $request, $council_details, $system_user ); + $p->update_from_open311_service_request( $request, $body, $system_user ); -Updates the problem based on information in the passed in open311 request. If the request -has an older update time than the problem's lastupdate time then nothing happens. +Updates the problem based on information in the passed in open311 request +(standard, not the extension that uses GetServiceRequestUpdates) . If the +request has an older update time than the problem's lastupdate time then +nothing happens. -Otherwise a comment will be created if there is status update text in the open311 request. -If the open311 request has a state of closed then the problem will be marked as fixed. +Otherwise a comment will be created if there is status update text in the +open311 request. If the open311 request has a state of closed then the problem +will be marked as fixed. NB: a comment will always be created if the problem is being marked as fixed. @@ -644,7 +695,7 @@ Fixed problems will not be re-opened by this method. =cut sub update_from_open311_service_request { - my ( $self, $request, $council_details, $system_user ) = @_; + my ( $self, $request, $body, $system_user ) = @_; my ( $updated, $status_notes ); @@ -667,11 +718,10 @@ sub update_from_open311_service_request { mark_fixed => 0, user => $system_user, anonymous => 0, - name => $council_details->{name}, + name => $body->name, } ); - my $w3c = DateTime::Format::W3CDTF->new; my $req_time = $w3c->parse_datetime( $request->{updated_datetime} ); @@ -720,6 +770,29 @@ sub update_send_failed { } ); } +sub as_hashref { + my $self = shift; + my $c = shift; + + return { + id => $self->id, + title => $self->title, + category => $self->category, + detail => $self->detail, + latitude => $self->latitude, + longitude => $self->longitude, + postcode => $self->postcode, + state => $self->state, + state_t => _( $self->state ), + used_map => $self->used_map, + is_fixed => $self->fixed_states->{ $self->state } ? 1 : 0, + photo => $self->get_photo_params, + meta => $self->confirmed ? $self->meta_line( $c ) : '', + confirmed_pp => $self->confirmed ? $c->cobrand->prettify_dt( $self->confirmed ): '', + created_pp => $c->cobrand->prettify_dt( $self->created ), + }; +} + # 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/Questionnaire.pm b/perllib/FixMyStreet/DB/Result/Questionnaire.pm index b6791603a..7f9c79d9a 100644 --- a/perllib/FixMyStreet/DB/Result/Questionnaire.pm +++ b/perllib/FixMyStreet/DB/Result/Questionnaire.pm @@ -36,31 +36,33 @@ __PACKAGE__->belongs_to( "problem", "FixMyStreet::DB::Result::Problem", { id => "problem_id" }, - { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, + { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-08 17:19:55 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:NGlSRjoBpDoIvK3EueqN6Q +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:oL1Hk4/bNG14CY74GA75SA use DateTime::TimeZone; +use Moose; +use namespace::clean -except => [ 'meta' ]; my $tz = DateTime::TimeZone->new( name => "local" ); -sub whensent_local { - my $self = shift; +my $tz_f; +$tz_f = DateTime::TimeZone->new( name => FixMyStreet->config('TIME_ZONE') ) + if FixMyStreet->config('TIME_ZONE'); - return $self->whensent - ? $self->whensent->set_time_zone($tz) - : $self->whensent; -} +my $stz = sub { + my ( $orig, $self ) = ( shift, shift ); + my $s = $self->$orig(@_); + return $s unless $s && UNIVERSAL::isa($s, "DateTime"); + $s->set_time_zone($tz); + $s->set_time_zone($tz_f) if $tz_f; + return $s; +}; -sub whenanswered_local { - my $self = shift; - - return $self->whenanswered - ? $self->whenanswered->set_time_zone($tz) - : $self->whenanswered; -} +around whensent => $stz; +around whenanswered => $stz; 1; diff --git a/perllib/FixMyStreet/DB/Result/Token.pm b/perllib/FixMyStreet/DB/Result/Token.pm index b223ada3a..5525fe7a5 100644 --- a/perllib/FixMyStreet/DB/Result/Token.pm +++ b/perllib/FixMyStreet/DB/Result/Token.pm @@ -34,8 +34,6 @@ __PACKAGE__->set_primary_key("scope", "token"); # use mySociety::DBHandle qw(dbh); use mySociety::AuthToken; -use IO::String; -use RABX; =head1 NAME @@ -54,25 +52,9 @@ ms_current_timestamp. =cut -__PACKAGE__->filter_column( - data => { - filter_from_storage => sub { - my $self = shift; - my $ser = shift; - return undef unless defined $ser; - my $h = new IO::String($ser); - return RABX::wire_rd($h); - }, - filter_to_storage => sub { - my $self = shift; - my $data = shift; - my $ser = ''; - my $h = new IO::String($ser); - RABX::wire_wr( $data, $h ); - return $ser; - }, - } -); +__PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); +__PACKAGE__->rabx_column('data'); + sub new { my ( $class, $attrs ) = @_; diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm index 7f43d1a52..523382670 100644 --- a/perllib/FixMyStreet/DB/Result/User.pm +++ b/perllib/FixMyStreet/DB/Result/User.pm @@ -26,8 +26,8 @@ __PACKAGE__->add_columns( { data_type => "text", is_nullable => 1 }, "password", { data_type => "text", default_value => "", is_nullable => 0 }, - "from_council", - { data_type => "integer", is_nullable => 1 }, + "from_body", + { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, "flagged", { data_type => "boolean", default_value => \"false", is_nullable => 0 }, "title", @@ -42,16 +42,27 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); __PACKAGE__->has_many( + "bodies", + "FixMyStreet::DB::Result::Body", + { "foreign.comment_user_id" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); +__PACKAGE__->has_many( "comments", "FixMyStreet::DB::Result::Comment", { "foreign.user_id" => "self.id" }, { cascade_copy => 0, cascade_delete => 0 }, ); -__PACKAGE__->has_many( - "open311confs", - "FixMyStreet::DB::Result::Open311conf", - { "foreign.comment_user_id" => "self.id" }, - { cascade_copy => 0, cascade_delete => 0 }, +__PACKAGE__->belongs_to( + "from_body", + "FixMyStreet::DB::Result::Body", + { id => "from_body" }, + { + is_deferrable => 0, + join_type => "LEFT", + on_delete => "NO ACTION", + on_update => "NO ACTION", + }, ); __PACKAGE__->has_many( "problems", @@ -61,8 +72,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-05-01 16:20:29 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:LKi8u5IYnHW1+Mez64nvGg +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:jRAtXRLRNozCmthAg9p0dA __PACKAGE__->add_columns( "password" => { @@ -144,38 +155,27 @@ sub alert_for_problem { } ); } -sub council { +sub body { my $self = shift; - - return '' unless $self->from_council; - - my $key = 'council_name:' . $self->from_council; - my $result = Memcached::get($key); - - unless ($result) { - my $area_info = mySociety::MaPit::call('area', $self->from_council); - $result = $area_info->{name}; - Memcached::set($key, $result, 86400); - } - - return $result; + return '' unless $self->from_body; + return $self->from_body->name; } -=head2 belongs_to_council +=head2 belongs_to_body - $belongs_to_council = $user->belongs_to_council( $council_list ); + $belongs_to_body = $user->belongs_to_body( $bodies ); -Returns true if the user belongs to the comma seperated list of council ids passed in +Returns true if the user belongs to the comma seperated list of body ids passed in =cut -sub belongs_to_council { +sub belongs_to_body { my $self = shift; - my $council = shift; + my $bodies = shift; - my %councils = map { $_ => 1 } split ',', $council; + my %bodies = map { $_ => 1 } split ',', $bodies; - return 1 if $self->from_council && $councils{ $self->from_council }; + return 1 if $self->from_body && $bodies{ $self->from_body->id }; return 0; } diff --git a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm index 468df2654..545b54c60 100644 --- a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm +++ b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm @@ -29,6 +29,7 @@ sub email_alerts ($) { $query .= " $item_table.id as item_id, $item_table.text as item_text, $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 @@ -58,6 +59,7 @@ sub email_alerts ($) { while (my $row = $query->fetchrow_hashref) { my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->{alert_cobrand})->new(); + $cobrand->set_lang_and_domain( $row->{alert_lang}, 1, FixMyStreet->path_to('locale')->stringify ); # Cobranded and non-cobranded messages can share a database. In this case, the conf file # should specify a vhost to send the reports for each cobrand, so that they don't get sent @@ -89,13 +91,14 @@ sub email_alerts ($) { } my $url = $cobrand->base_url( $row->{alert_cobrand_data} ); - if ( $hashref_restriction && $hashref_restriction->{council} && $row->{council} ne $hashref_restriction->{council} ) { + if ( $hashref_restriction && $hashref_restriction->{bodies_str} && $row->{bodies_str} ne $hashref_restriction->{bodies_str} ) { $url = mySociety::Config::get('BASE_URL'); } # this is currently only for new_updates if ($row->{item_text}) { - if ( $row->{alert_user_id} == $row->{user_id} ) { + if ( $cobrand->moniker ne 'zurich' && $row->{alert_user_id} == $row->{user_id} ) { # This is an alert to the same user who made the report - make this a login link + # Don't bother with Zurich which has no accounts my $user = FixMyStreet::App->model('DB::User')->find( { id => $row->{alert_user_id} } ); @@ -112,6 +115,27 @@ sub email_alerts ($) { $data{problem_url} = $url . "/report/" . $row->{id}; } $data{data} .= $row->{item_name} . ' : ' if $row->{item_name} && !$row->{item_anonymous}; + if ( $cobrand->include_time_in_update_alerts ) { + # this is basically recreating the code from the inflate wrapper + # in the database model. + my $tz; + if ( FixMyStreet->config('TIME_ZONE') ) { + $tz = FixMyStreet->config('TIME_ZONE'); + } + + my $parser = DateTime::Format::Pg->new(); + my $dt = $parser->parse_timestamp( $row->{item_confirmed} ); + my $l_tz = DateTime::TimeZone->new( name => "local" ); + # We need to always set this otherwise we end up with the DateTime + # object being in the floating timezone in which case applying a + # subsequent timezone set will have no effect. + $dt->set_time_zone( $l_tz ); + if ( $tz ) { + my $tz_obj = DateTime::TimeZone->new( name => $tz ); + $dt->set_time_zone( $tz_obj ); + } + $data{data} .= $cobrand->prettify_dt( $dt, 'alert' ) . "\n\n"; + } $data{data} .= $row->{item_text} . "\n\n------\n\n"; # this is ward and council problems } else { @@ -155,6 +179,7 @@ sub email_alerts ($) { while (my $alert = $query->next) { my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($alert->cobrand)->new(); next unless $cobrand->email_host; + next if $alert->is_from_abuser; my $longitude = $alert->parameter; my $latitude = $alert->parameter2; @@ -166,7 +191,7 @@ sub email_alerts ($) { }; my $states = "'" . join( "', '", FixMyStreet::DB::Result::Problem::visible_states() ) . "'"; my %data = ( template => $template, data => '', alert_id => $alert->id, alert_email => $alert->user->email, lang => $alert->lang, cobrand => $alert->cobrand, cobrand_data => $alert->cobrand_data ); - my $q = "select problem.id, problem.council, problem.postcode, problem.geocode, problem.title from problem_find_nearby(?, ?, ?) as nearby, problem, users + my $q = "select problem.id, problem.bodies_str, problem.postcode, problem.geocode, problem.title from problem_find_nearby(?, ?, ?) as nearby, problem, users where nearby.problem_id = problem.id and problem.user_id = users.id and problem.state in ($states) @@ -183,7 +208,7 @@ sub email_alerts ($) { parameter => $row->{id}, } ); my $url = $cobrand->base_url( $alert->cobrand_data ); - if ( $hashref_restriction && $hashref_restriction->{council} && $row->{council} ne $hashref_restriction->{council} ) { + if ( $hashref_restriction && $hashref_restriction->{bodies_str} && $row->{bodies_str} ne $hashref_restriction->{bodies_str} ) { $url = mySociety::Config::get('BASE_URL'); } $data{data} .= $url . "/report/" . $row->{id} . " - $row->{title}\n\n"; @@ -202,7 +227,7 @@ sub _send_aggregated_alert_email(%) { my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($data{cobrand})->new(); - $cobrand->set_lang_and_domain( $data{lang}, 1 ); + $cobrand->set_lang_and_domain( $data{lang}, 1, FixMyStreet->path_to('locale')->stringify ); if (!$data{alert_email}) { my $user = FixMyStreet::App->model('DB::User')->find( { @@ -211,6 +236,11 @@ sub _send_aggregated_alert_email(%) { $data{alert_email} = $user->email; } + my ($domain) = $data{alert_email} =~ m{ @ (.*) \z }x; + return if FixMyStreet::App->model('DB::Abuse')->search( { + email => [ $data{alert_email}, $domain ] + } )->first; + my $token = FixMyStreet::App->model("DB::Token")->new_result( { scope => 'alert', data => { @@ -230,13 +260,12 @@ sub _send_aggregated_alert_email(%) { unless -e $template; $template = Utils::read_file($template); - my $sender = $cobrand->contact_email; - (my $from = $sender) =~ s/team/fms-DO-NOT-REPLY/; # XXX + my $sender = FixMyStreet->config('DO_NOT_REPLY_EMAIL'); my $result = FixMyStreet::App->send_email_cron( { _template_ => $template, _parameters_ => \%data, - From => [ $from, _($cobrand->contact_name) ], + From => [ $sender, _($cobrand->contact_name) ], To => $data{alert_email}, }, $sender, @@ -255,6 +284,7 @@ sub _get_address_from_gecode { my $geocode = shift; return '' unless defined $geocode; + utf8::encode($geocode) if utf8::is_utf8($geocode); my $h = new IO::String($geocode); my $data = RABX::wire_rd($h); diff --git a/perllib/FixMyStreet/DB/ResultSet/Body.pm b/perllib/FixMyStreet/DB/ResultSet/Body.pm new file mode 100644 index 000000000..6802ed604 --- /dev/null +++ b/perllib/FixMyStreet/DB/ResultSet/Body.pm @@ -0,0 +1,17 @@ +package FixMyStreet::DB::ResultSet::Body; +use base 'DBIx::Class::ResultSet'; + +use strict; +use warnings; + +sub for_areas { + my ( $rs, @areas ) = @_; + + my $result = $rs->search( + { 'body_areas.area_id' => \@areas }, + { join => 'body_areas' } + ); + return $result; +} + +1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm index 191223572..91c44d5f4 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm @@ -21,12 +21,7 @@ sub nearby { } if $c->cobrand->problems_clause; my $attrs = { - join => 'problem', - columns => [ - 'problem.id', 'problem.title', 'problem.latitude', - 'problem.longitude', 'distance', 'problem.state', - 'problem.confirmed', { 'problem.photo' => 'problem.photo is not null' }, - ], + prefetch => 'problem', bind => [ $mid_lat, $mid_lon, $dist ], order_by => [ 'distance', { -desc => 'created' } ], rows => $limit, diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index 6da383d6c..c108f7e29 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -84,15 +84,16 @@ sub _recent { my $key = $photos ? 'recent_photos' : 'recent'; $key .= ":$site_key:$num"; + # unconfirmed might be returned for e.g. Zurich, but would mean in moderation, so no photo + my @states = grep { $_ ne 'unconfirmed' } FixMyStreet::DB::Result::Problem->visible_states(); my $query = { non_public => 0, - state => [ FixMyStreet::DB::Result::Problem->visible_states() ], + state => \@states, }; $query->{photo} = { '!=', undef } if $photos; my $attrs = { - columns => [ 'id', 'title', 'confirmed' ], - order_by => { -desc => 'confirmed' }, + order_by => { -desc => 'coalesce(confirmed, created)' }, rows => $num, }; @@ -134,10 +135,6 @@ sub around_map { my ( $rs, $min_lat, $max_lat, $min_lon, $max_lon, $interval, $limit ) = @_; my $attr = { order_by => { -desc => 'created' }, - columns => [ - 'id', 'title', 'latitude', 'longitude', 'state', 'confirmed', - { photo => 'photo is not null' }, - ], }; $attr->{rows} = $limit if $limit; @@ -159,7 +156,7 @@ sub around_map { sub timeline { my ( $rs ) = @_; - my $prefetch = + my $prefetch = FixMyStreet::App->model('DB')->schema->storage->sql_maker->quote_char ? [ qw/user/ ] : []; @@ -219,46 +216,74 @@ sub categories_summary { return \%categories; } +sub get_admin_url { + my ($rs, $cobrand, $row) = @_; + return $cobrand->admin_base_url . '/report_edit/' . $row->id; +} + sub send_reports { + my ( $rs, $site_override ) = @_; + # Set up site, language etc. - my ($verbose, $nomail) = CronFns::options(); + my ($verbose, $nomail, $debug_mode) = CronFns::options(); + my $base_url = mySociety::Config::get('BASE_URL'); - my $site = CronFns::site($base_url); + 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( { - state => [ 'confirmed', 'fixed' ], + state => $states, whensent => undef, - council => { '!=', undef }, + bodies_str => { '!=', undef }, } ); my (%notgot, %note); my $send_report = FixMyStreet::SendReport->new(); my $senders = $send_report->get_senders; - my %sending_skipped_by_method; + my $debug_unsent_count = 0; + debug_print("starting to loop through unsent problem reports...") if $debug_mode; while (my $row = $unsent->next) { my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new(); - # Cobranded and non-cobranded messages can share a database. In this case, the conf file - # should specify a vhost to send the reports for each cobrand, so that they don't get sent + if ($debug_mode) { + $debug_unsent_count++; + print "\n"; + debug_print("state=" . $row->state . ", bodies_str=" . $row->bodies_str . ($row->cobrand? ", cobrand=" . $row->cobrand : ""), $row->id); + } + + # Cobranded and non-cobranded messages can share a database. In this case, the conf file + # should specify a vhost to send the reports for each cobrand, so that they don't get sent # more than once if there are multiple vhosts running off the same database. The email_host # call checks if this is the host that sends mail for this cobrand. - next unless $cobrand->email_host(); + if (! $cobrand->email_host()) { + debug_print("skipping because this host does not send reports for cobrand " . $cobrand->moniker, $row->id) if $debug_mode; + next; + } + $cobrand->set_lang_and_domain($row->lang, 1); - if ( $row->is_from_abuser ) { + if ( $row->is_from_abuser) { + $row->update( { state => 'hidden' } ); + debug_print("hiding because its sender is flagged as an abuser", $row->id) if $debug_mode; + next; + } elsif ( $row->title =~ /app store test/i ) { $row->update( { state => 'hidden' } ); + debug_print("hiding because it is an app store test message", $row->id) if $debug_mode; next; } # Template variables for the email my $email_base_url = $cobrand->base_url_for_report($row); my %h = map { $_ => $row->$_ } qw/id title detail name category latitude longitude used_map/; - map { $h{$_} = $row->user->$_ } qw/email phone/; - $h{confirmed} = DateTime::Format::Pg->format_datetime( $row->confirmed->truncate (to => 'second' ) ); + map { $h{$_} = $row->user->$_ || '' } qw/email phone/; + $h{confirmed} = DateTime::Format::Pg->format_datetime( $row->confirmed->truncate (to => 'second' ) ) + if $row->confirmed; $h{query} = $row->postcode; $h{url} = $email_base_url . $row->url; + $h{admin_url} = $rs->get_admin_url($cobrand, $row); $h{phone_line} = $h{phone} ? _('Phone:') . " $h{phone}\n\n" : ''; if ($row->photo) { $h{has_photo} = _("This web page also contains a photo of the problem, provided by the user.") . "\n\n"; @@ -302,39 +327,42 @@ sub send_reports { my ( $sender_count ); if ($site eq 'emptyhomes') { - my $council = $row->council; - my $areas_info = mySociety::MaPit::call('areas', $council); + my $body = $row->bodies_str; + $body = FixMyStreet::App->model("DB::Body")->find($body); my $sender = "FixMyStreet::SendReport::EmptyHomes"; $reporters{ $sender } = $sender->new() unless $reporters{$sender}; - $reporters{ $sender }->add_council( $council, $areas_info->{$council} ); + $reporters{ $sender }->add_body( $body ); + $sender_count = 1; } else { # XXX Needs locks! - my @all_councils = split /,|\|/, $row->council; - my ($councils, $missing) = $row->council =~ /^([\d,]+)(?:\|([\d,]+))?/; - my @councils = split(/,/, $councils); - my $areas_info = mySociety::MaPit::call('areas', \@all_councils); + # 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 }, + { order_by => 'name' }, + ); + $missing = FixMyStreet::App->model("DB::Body")->find($missing) if $missing; my @dear; - foreach my $council (@councils) { - my $name = $areas_info->{$council}->{name}; - - my $sender_info = $cobrand->get_council_sender( $council, $areas_info->{$council}, $row->category ); + while (my $body = $bodies->next) { + my $sender_info = $cobrand->get_body_sender( $body, $row->category ); my $sender = "FixMyStreet::SendReport::" . $sender_info->{method}; if ( ! exists $senders->{ $sender } ) { - warn "No such sender [ $sender ] for council $name ( $council )"; + warn "No such sender [ $sender ] for body $body->name ( $body->id )"; next; } $reporters{ $sender } ||= $sender->new(); if ( $reporters{ $sender }->should_skip( $row ) ) { - $sending_skipped_by_method{ $sender }++ if - $reporters{ $sender }->skipped; + debug_print("skipped by sender " . $sender_info->{method} . " (might be due to previous failed attempts?)", $row->id) if $debug_mode; } else { - push @dear, $name; - $reporters{ $sender }->add_council( $council, $areas_info->{$council}, $sender_info->{config} ); + debug_print("OK, adding recipient body " . $body->id . ":" . $body->name . ", " . $body->send_method, $row->id) if $debug_mode; + push @dear, $body->name; + $reporters{ $sender }->add_body( $body, $sender_info->{config} ); } } @@ -352,7 +380,7 @@ sub send_reports { $h{subcategory_line} = "\n\n"; } - $h{councils_name} = join(_(' and '), @dear); + $h{bodies_name} = join(_(' and '), @dear); if ($h{category} eq _('Other')) { $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to both councils covering the location of the problem, as the user did not categorise it; please ignore it if you're not the correct council to deal with the issue, or let us know what category of problem this is so we can add it to our system.") . " ]\n\n" : ''; @@ -360,11 +388,10 @@ sub send_reports { $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to several councils covering the location of the problem, as the category selected is provided for all of them; please ignore it if you're not the correct council to deal with the issue.") . " ]\n\n" : ''; } - $h{missing} = ''; + $h{missing} = ''; if ($missing) { - my $name = $areas_info->{$missing}->{name}; $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.'), $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->name) . " ]\n\n"; } @@ -375,16 +402,16 @@ sub send_reports { die 'Report not going anywhere for ID ' . $row->id . '!'; } - next unless $sender_count; + if (! $sender_count) { + debug_print("can't send because sender count is zero", $row->id) if $debug_mode; + next; + } - if (mySociety::Config::get('STAGING_SITE')) { - # on a staging server send emails to ourselves rather than the councils - my @testing_councils = split( '\|', mySociety::Config::get('TESTING_COUNCILS') ); - unless ( grep { $row->council eq $_ } @testing_councils ) { - %reporters = map { $_ => $reporters{$_} } grep { /FixMyStreet::SendReport::(Email|NI)/ } keys %reporters; - unless (%reporters) { - %reporters = ( 'FixMyStreet::SendReport::Email' => FixMyStreet::SendReport::Email->new() ); - } + if (mySociety::Config::get('STAGING_SITE') && !mySociety::Config::get('SEND_REPORTS_ON_STAGING')) { + # on a staging server send emails to ourselves rather than the bodies + %reporters = map { $_ => $reporters{$_} } grep { /FixMyStreet::SendReport::(Email|NI|EmptyHomes)/ } keys %reporters; + unless (%reporters) { + %reporters = ( 'FixMyStreet::SendReport::Email' => FixMyStreet::SendReport::Email->new() ); } } @@ -392,6 +419,7 @@ sub send_reports { my $result = -1; for my $sender ( keys %reporters ) { + debug_print("sending using " . $sender, $row->id) if $debug_mode; $result *= $reporters{ $sender }->send( $row, \%h ); if ( $reporters{ $sender }->unconfirmed_counts) { foreach my $e (keys %{ $reporters{ $sender }->unconfirmed_counts } ) { @@ -414,6 +442,7 @@ sub send_reports { if ( $cobrand->report_sent_confirmation_email && !$h{anonymous_report}) { _send_report_sent_email( $row, \%h, $nomail ); } + debug_print("send successful: OK", $row->id) if $debug_mode; } else { my @errors; for my $sender ( keys %reporters ) { @@ -422,34 +451,35 @@ sub send_reports { } } $row->update_send_failed( join( '|', @errors ) ); + debug_print("send FAILED: " . join( '|', @errors ), $row->id) if $debug_mode; + } + } + if ($debug_mode) { + print "\n"; + if ($debug_unsent_count) { + debug_print("processed all unsent reports (total: $debug_unsent_count)"); + } else { + debug_print("no unsent reports were found (must have whensent=null and suitable bodies_str & state) -- nothing to send"); } } - if ($verbose) { + if ($verbose || $debug_mode) { print "Council email addresses that need checking:\n" if keys %notgot; foreach my $e (keys %notgot) { foreach my $c (keys %{$notgot{$e}}) { print " " . $notgot{$e}{$c} . " problem, to $e category $c (" . $note{$e}{$c}. ")\n"; } } - if (keys %sending_skipped_by_method) { - my $c = 0; - print "\nProblem reports that send-reports did not attempt to send the following:\n"; - foreach my $send_method (sort keys %sending_skipped_by_method) { - printf " %-24s %4d\n", "$send_method:", $sending_skipped_by_method{$send_method}; - $c+=$sending_skipped_by_method{$send_method}; - } - printf " %-24s %4d\n", "Total:", $c; - } my $sending_errors = ''; my $unsent = FixMyStreet::App->model("DB::Problem")->search( { state => [ 'confirmed', 'fixed' ], whensent => undef, - council => { '!=', undef }, + bodies_str => { '!=', undef }, send_fail_count => { '>', 0 } } ); while (my $row = $unsent->next) { - $sending_errors .= "* http://www.fixmystreet.com/report/" . $row->id . ", failed " + my $base_url = mySociety::Config::get('BASE_URL'); + $sending_errors .= "* " . $base_url . "/report/" . $row->id . ", failed " . $row->send_fail_count . " times, last at " . $row->send_fail_timestamp . ", reason " . $row->send_fail_reason . "\n"; } @@ -485,4 +515,11 @@ sub _send_report_sent_email { ); } +sub debug_print { + my $msg = shift; + my $id = shift || ''; + $id = "report $id: " if $id; + print "[] $id$msg\n"; +} + 1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm b/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm index d6b3eb5cb..6f2c19b5e 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm @@ -50,6 +50,7 @@ sub send_questionnaires_period { # Not all cobrands send questionnaires next unless $cobrand->send_questionnaires; + next if $row->is_from_abuser; # Cobranded and non-cobranded messages can share a database. In this case, the conf file # should specify a vhost to send the reports for each cobrand, so that they don't get sent @@ -89,9 +90,8 @@ sub send_questionnaires_period { } ); $h{url} = $cobrand->base_url($row->cobrand_data) . '/Q/' . $token->token; - my $sender = $cobrand->contact_email; + my $sender = FixMyStreet->config('DO_NOT_REPLY_EMAIL'); my $sender_name = _($cobrand->contact_name); - $sender =~ s/team/fms-DO-NOT-REPLY/; print "Sending questionnaire " . $questionnaire->id . ", problem " . $row->id . ", token " . $token->token . " to " |