diff options
Diffstat (limited to 'perllib/FixMyStreet/DB')
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Body.pm | 108 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/BodyArea.pm | 33 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Contact.pm | 17 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/ContactsHistory.pm | 6 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Open311conf.pm | 61 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 138 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Token.pm | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/User.pm | 60 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/AlertType.pm | 10 | ||||
-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 | 97 |
13 files changed, 342 insertions, 218 deletions
diff --git a/perllib/FixMyStreet/DB/Result/Body.pm b/perllib/FixMyStreet/DB/Result/Body.pm new file mode 100644 index 000000000..83704563a --- /dev/null +++ b/perllib/FixMyStreet/DB/Result/Body.pm @@ -0,0 +1,108 @@ +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 }, + "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 }, + "parent", + { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, +); +__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 => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "CASCADE", + }, +); +__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 => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "CASCADE", + }, +); +__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.07017 @ 2012-12-19 12:47:10 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:DdtXjMWRpz20ZHjtY3oP2w + +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..844a3277d --- /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 => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-12-19 12:47:10 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:aAr+Nadyu8IckZlK6+PTNg + + __PACKAGE__->set_primary_key(__PACKAGE__->columns); + +1; diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index b551be9ef..33fbb9356 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -91,6 +91,7 @@ __PACKAGE__->filter_column( 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); }, @@ -146,8 +147,8 @@ sub check_for_errors { $errors{update} = _('Please enter a message') unless $self->text =~ m/\S/; - if ( $self->text && $self->problem && $self->problem->council - && $self->problem->council eq '2482' && length($self->text) > 2000 ) { + if ( $self->text && $self->problem && $self->problem->bodies_str + && $self->problem->bodies_str eq '2482' && length($self->text) > 2000 ) { $errors{update} = _('Updates are limited to 2000 characters in length. Please shorten your update'); } diff --git a/perllib/FixMyStreet/DB/Result/Contact.pm b/perllib/FixMyStreet/DB/Result/Contact.pm index 993e3524b..551bcd019 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,11 +48,17 @@ __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 => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); -# 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.07017 @ 2012-12-13 12:34:33 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:imXq3EtrC0FrQwj+E2xfBw __PACKAGE__->filter_column( extra => { @@ -60,6 +66,7 @@ __PACKAGE__->filter_column( 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); }, 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 f01a20dec..000000000 --- a/perllib/FixMyStreet/DB/Result/Open311conf.pm +++ /dev/null @@ -1,61 +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 }, - "send_extended_statuses", - { 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 0238e3b09..5bde902d4 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 }, ); @@ -124,8 +124,8 @@ __PACKAGE__->belongs_to( ); -# 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.07017 @ 2012-12-13 15:13:48 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:H2P3Og37G569nQdQA1IWaA # Add fake relationship to stored procedure table __PACKAGE__->has_one( @@ -141,6 +141,7 @@ __PACKAGE__->filter_column( 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); }, @@ -161,6 +162,7 @@ __PACKAGE__->filter_column( 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); }, @@ -259,24 +261,26 @@ 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 { - my $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, - }; - - return wantarray ? keys %{$states} : $states; + return wantarray ? keys %{$visible_states} : $visible_states; +} +sub visible_states_add_unconfirmed { + $visible_states->{unconfirmed} = 1; } =head2 @@ -336,7 +340,6 @@ sub council_states { return wantarray ? keys %{$states} : $states; } - my $tz = DateTime::TimeZone->new( name => "local" ); sub confirmed_local { @@ -378,6 +381,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(); @@ -402,9 +411,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'); @@ -431,8 +440,8 @@ sub check_for_errors { $self->category(undef); } - if ( $self->council && $self->detail && - $self->council eq '2482' && length($self->detail) > 2000 ) { + if ( $self->bodies_str && $self->detail && + $self->bodies_str eq '2482' && length($self->detail) > 2000 ) { $errors{detail} = _('Reports are limited to 2000 characters in length. Please shorten your report'); } @@ -462,18 +471,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; +} + +=head2 bodies -Returns an arrayref of councils to which a report was sent. +Returns an arrayref 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 @@ -557,8 +574,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_local ); my $meta = ''; # FIXME Should be in cobrand @@ -623,21 +639,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; @@ -652,7 +669,7 @@ sub body { sub can_display_external_id { my $self = shift; if ($self->external_id && $self->send_method_used && - ($self->send_method_used eq 'barnet' || $self->council =~ /2237/)) { + ($self->send_method_used eq 'barnet' || $self->bodies_str =~ /2237/)) { return 1; } return 0; @@ -696,6 +713,14 @@ sub duration_string { ); } +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 ); @@ -789,6 +814,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 ? Utils::prettify_dt( $self->confirmed_local, 1 ): '', + created_pp => Utils::prettify_dt( $self->created_local, 1 ), + }; +} + # 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/Token.pm b/perllib/FixMyStreet/DB/Result/Token.pm index b223ada3a..028300842 100644 --- a/perllib/FixMyStreet/DB/Result/Token.pm +++ b/perllib/FixMyStreet/DB/Result/Token.pm @@ -60,6 +60,7 @@ __PACKAGE__->filter_column( 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); }, diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm index 7f43d1a52..481b654c9 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 => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "CASCADE", + }, ); __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.07017 @ 2012-12-14 09:23:59 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:aw374WQraL5ysOvUmUIU3w __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 d903f8eb2..8e9b3d17e 100644 --- a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm +++ b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm @@ -89,13 +89,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} } ); @@ -166,7 +167,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 +184,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"; @@ -254,6 +255,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 faed3b8ac..078c78d0e 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; @@ -220,15 +217,19 @@ sub categories_summary { } sub send_reports { + my ( $rs, $site_override ) = @_; + # Set up site, language etc. my ($verbose, $nomail) = 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); @@ -255,10 +256,12 @@ sub send_reports { 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' ) ); + $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} = $cobrand->admin_base_url . 'report_edit/' . $row->id; $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,29 +305,28 @@ 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 ); } 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 }); + $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(); @@ -333,8 +335,8 @@ sub send_reports { $sending_skipped_by_method{ $sender }++ if $reporters{ $sender }->skipped; } else { - push @dear, $name; - $reporters{ $sender }->add_council( $council, $areas_info->{$council}, $sender_info->{config} ); + push @dear, $body->name; + $reporters{ $sender }->add_body( $body, $sender_info->{config} ); } } @@ -350,7 +352,7 @@ sub send_reports { $h{subcategory_line} = sprintf(_("Subcategory: %s"), $row->subcategory) . "\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,9 +362,8 @@ sub send_reports { } $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,39 +376,11 @@ sub send_reports { next unless $sender_count; - if (mySociety::Config::get('STAGING_SITE')) { - # on a staging server send emails to ourselves rather than the councils - # however, we can configure a list of councils that we use non email - # delivery, e.g. Open311, for testing purposes. For those we want to - # send using the non email method and for everyone else we want to use - # email - my @testing_councils = split( '\|', mySociety::Config::get('TESTING_COUNCILS') ); - - # we only care about non missing councils so we get the missing ones - # and then essentially throw them away as we're not going to have - # configured them to do anything. - my %councils = map { $_ => 1 } @{ $row->councils }; - - # We now take the councils that we have contact details for and if any of them - # are in the list of testing councils we look a bit harder otherwise we throw - # away all the non email delivery methods - if ( grep { $councils{ $_ } } @testing_councils ) { - my %tc = map { $_ => 1 } @testing_councils; - my @non_matching = grep { !$tc{$_} } keys %councils; - for my $sender ( keys %reporters ) { - next if $sender =~ /FixMyStreet::SendReport::(Email|NI)/; - for my $council ( @non_matching ) { - $reporters{$sender}->delete_council( $council ); - } - } - if ( @non_matching ) { - $reporters{'FixMyStreet::SendReport::Email'} = FixMyStreet::SendReport::Email->new(); - } - } else { - %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)/ } keys %reporters; + unless (%reporters) { + %reporters = ( 'FixMyStreet::SendReport::Email' => FixMyStreet::SendReport::Email->new() ); } } @@ -468,7 +441,7 @@ sub send_reports { 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) { |