aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/DB')
-rw-r--r--perllib/FixMyStreet/DB/Result/Body.pm25
-rw-r--r--perllib/FixMyStreet/DB/Result/Comment.pm91
-rw-r--r--perllib/FixMyStreet/DB/Result/Contact.pm13
-rw-r--r--perllib/FixMyStreet/DB/Result/ContactDefectType.pm46
-rw-r--r--perllib/FixMyStreet/DB/Result/DefectType.pm66
-rw-r--r--perllib/FixMyStreet/DB/Result/Problem.pm148
-rw-r--r--perllib/FixMyStreet/DB/Result/ResponsePriority.pm6
-rw-r--r--perllib/FixMyStreet/DB/Result/ResponseTemplate.pm6
-rw-r--r--perllib/FixMyStreet/DB/Result/User.pm41
-rw-r--r--perllib/FixMyStreet/DB/ResultSet/DefectType.pm22
10 files changed, 420 insertions, 44 deletions
diff --git a/perllib/FixMyStreet/DB/Result/Body.pm b/perllib/FixMyStreet/DB/Result/Body.pm
index 037b69352..82015ad2d 100644
--- a/perllib/FixMyStreet/DB/Result/Body.pm
+++ b/perllib/FixMyStreet/DB/Result/Body.pm
@@ -75,6 +75,12 @@ __PACKAGE__->has_many(
{ "foreign.body_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
+__PACKAGE__->has_many(
+ "defect_types",
+ "FixMyStreet::DB::Result::DefectType",
+ { "foreign.body_id" => "self.id" },
+ { cascade_copy => 0, cascade_delete => 0 },
+);
__PACKAGE__->belongs_to(
"parent",
"FixMyStreet::DB::Result::Body",
@@ -112,8 +118,8 @@ __PACKAGE__->has_many(
);
-# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-09-06 15:33:04
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ZuzscnLqcx0k512cTZ/kdg
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-02-13 15:11:11
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:BOJANVwg3kR/1VjDq0LykA
sub url {
my ( $self, $c, $args ) = @_;
@@ -127,4 +133,19 @@ sub areas {
return \%ids;
}
+=head2 get_cobrand_handler
+
+Get a cobrand object for this body, if there is one.
+
+e.g.
+ * if the problem was sent to Bromley it will return ::Bromley
+ * if the problem was sent to Camden it will return nothing
+
+=cut
+
+sub get_cobrand_handler {
+ my $self = shift;
+ return FixMyStreet::Cobrand->body_handler($self->areas);
+}
+
1;
diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm
index f5601639a..cf1ba444d 100644
--- a/perllib/FixMyStreet/DB/Result/Comment.pm
+++ b/perllib/FixMyStreet/DB/Result/Comment.pm
@@ -6,6 +6,7 @@ package FixMyStreet::DB::Result::Comment;
use strict;
use warnings;
+use FixMyStreet::Template;
use base 'DBIx::Class::Core';
__PACKAGE__->load_components("FilterColumn", "InflateColumn::DateTime", "EncodedColumn");
@@ -199,7 +200,7 @@ __PACKAGE__->has_many(
"admin_log_entries",
"FixMyStreet::DB::Result::AdminLog",
{ "foreign.object_id" => "self.id" },
- {
+ {
cascade_copy => 0, cascade_delete => 0,
where => { 'object_type' => 'update' },
}
@@ -223,4 +224,92 @@ __PACKAGE__->might_have(
{ cascade_copy => 0, cascade_delete => 1 },
);
+=head2 meta_line
+
+Returns a string to be used on a report update, describing some of the metadata
+about an update
+
+=cut
+
+sub meta_line {
+ my ( $self, $c ) = @_;
+
+ my $meta = '';
+
+ $c->stash->{last_state} ||= '';
+
+ if ($self->anonymous or !$self->name) {
+ $meta = sprintf( _( 'Posted anonymously at %s' ), Utils::prettify_dt( $self->confirmed ) )
+ } elsif ($self->user->from_body) {
+ my $user_name = FixMyStreet::Template::html_filter($self->user->name);
+ my $body = $self->user->body;
+ if ($body eq 'Bromley Council') {
+ $body = "$body <img src='/cobrands/bromley/favicon.png' alt=''>";
+ } elsif ($body eq 'Royal Borough of Greenwich') {
+ $body = "$body <img src='/cobrands/greenwich/favicon.png' alt=''>";
+ }
+ if ($c->user_exists and $c->user->has_permission_to('view_body_contribute_details', $self->problem->bodies_str_ids)) {
+ $meta = sprintf( _( 'Posted by <strong>%s</strong> (%s) at %s' ), $body, $user_name, Utils::prettify_dt( $self->confirmed ) );
+ } else {
+ $meta = sprintf( _( 'Posted by <strong>%s</strong> at %s' ), $body, Utils::prettify_dt( $self->confirmed ) );
+ }
+ } else {
+ $meta = sprintf( _( 'Posted by %s at %s' ), FixMyStreet::Template::html_filter($self->name), Utils::prettify_dt( $self->confirmed ) )
+ }
+
+ my $update_state = '';
+
+ if ($self->mark_fixed) {
+ $update_state = _( 'marked as fixed' );
+ } elsif ($self->mark_open) {
+ $update_state = _( 'reopened' );
+ } elsif ($self->problem_state) {
+ my $state = $self->problem_state_display;
+
+ if ($state eq 'confirmed') {
+ if ($c->stash->{last_state}) {
+ $update_state = _( 'reopened' )
+ }
+ } elsif ($state eq 'investigating') {
+ $update_state = _( 'marked as investigating' )
+ } elsif ($state eq 'planned') {
+ $update_state = _( 'marked as planned' )
+ } elsif ($state eq 'in progress') {
+ $update_state = _( 'marked as in progress' )
+ } elsif ($state eq 'action scheduled') {
+ $update_state = _( 'marked as action scheduled' )
+ } elsif ($state eq 'closed') {
+ $update_state = _( 'marked as closed' )
+ } elsif ($state eq 'fixed') {
+ $update_state = _( 'marked as fixed' )
+ } elsif ($state eq 'unable to fix') {
+ $update_state = _( 'marked as no further action' )
+ } elsif ($state eq 'not responsible') {
+ $update_state = _( "marked as not the council's responsibility" )
+ } elsif ($state eq 'duplicate') {
+ $update_state = _( 'closed as a duplicate report' )
+ } elsif ($state eq 'internal referral') {
+ $update_state = _( 'marked as an internal referral' )
+ }
+
+ if ($c->cobrand->moniker eq 'bromley' || (
+ $self->problem->bodies_str &&
+ $self->problem->bodies_str eq '2482'
+ )) {
+ if ($state eq 'not responsible') {
+ $update_state = 'marked as third party responsibility'
+ }
+ }
+
+ }
+
+ if ($update_state ne $c->stash->{last_state} and $update_state) {
+ $meta .= ", $update_state";
+ }
+
+ $c->stash->{last_state} = $update_state;
+
+ return $meta;
+};
+
1;
diff --git a/perllib/FixMyStreet/DB/Result/Contact.pm b/perllib/FixMyStreet/DB/Result/Contact.pm
index 0c9a7c0d8..a620b7358 100644
--- a/perllib/FixMyStreet/DB/Result/Contact.pm
+++ b/perllib/FixMyStreet/DB/Result/Contact.pm
@@ -56,6 +56,12 @@ __PACKAGE__->belongs_to(
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
);
__PACKAGE__->has_many(
+ "contact_defect_types",
+ "FixMyStreet::DB::Result::ContactDefectType",
+ { "foreign.contact_id" => "self.id" },
+ { cascade_copy => 0, cascade_delete => 0 },
+);
+__PACKAGE__->has_many(
"contact_response_priorities",
"FixMyStreet::DB::Result::ContactResponsePriority",
{ "foreign.contact_id" => "self.id" },
@@ -69,8 +75,8 @@ __PACKAGE__->has_many(
);
-# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-09-06 15:33:04
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ocmQGeFJtO3wmvyx6W+EKQ
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-02-13 15:11:11
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:f9VepR/oPyr3z6PUpJ4w2A
__PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn");
__PACKAGE__->rabx_column('extra');
@@ -82,11 +88,12 @@ with 'FixMyStreet::Roles::Extra';
__PACKAGE__->many_to_many( response_templates => 'contact_response_templates', 'response_template' );
__PACKAGE__->many_to_many( response_priorities => 'contact_response_priorities', 'response_priority' );
+__PACKAGE__->many_to_many( defect_types => 'contact_defect_types', 'defect_type' );
sub get_metadata_for_input {
my $self = shift;
my $id_field = $self->id_field;
- my @metadata = grep { $_->{code} !~ /^(easting|northing|$id_field)$/ } @{$self->get_extra_fields};
+ my @metadata = grep { $_->{code} !~ /^(easting|northing|closest_address|$id_field)$/ } @{$self->get_extra_fields};
# Just in case the extra data is in an old parsed format
foreach (@metadata) {
diff --git a/perllib/FixMyStreet/DB/Result/ContactDefectType.pm b/perllib/FixMyStreet/DB/Result/ContactDefectType.pm
new file mode 100644
index 000000000..2199f0b42
--- /dev/null
+++ b/perllib/FixMyStreet/DB/Result/ContactDefectType.pm
@@ -0,0 +1,46 @@
+use utf8;
+package FixMyStreet::DB::Result::ContactDefectType;
+
+# 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("contact_defect_types");
+__PACKAGE__->add_columns(
+ "id",
+ {
+ data_type => "integer",
+ is_auto_increment => 1,
+ is_nullable => 0,
+ sequence => "contact_defect_types_id_seq",
+ },
+ "contact_id",
+ { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
+ "defect_type_id",
+ { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
+);
+__PACKAGE__->set_primary_key("id");
+__PACKAGE__->belongs_to(
+ "contact",
+ "FixMyStreet::DB::Result::Contact",
+ { id => "contact_id" },
+ { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
+);
+__PACKAGE__->belongs_to(
+ "defect_type",
+ "FixMyStreet::DB::Result::DefectType",
+ { id => "defect_type_id" },
+ { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
+);
+
+
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-02-13 15:11:11
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:VIczmM0OXXpWgQVpop3SMw
+
+
+# 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/DefectType.pm b/perllib/FixMyStreet/DB/Result/DefectType.pm
new file mode 100644
index 000000000..a2969f59e
--- /dev/null
+++ b/perllib/FixMyStreet/DB/Result/DefectType.pm
@@ -0,0 +1,66 @@
+use utf8;
+package FixMyStreet::DB::Result::DefectType;
+
+# 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("defect_types");
+__PACKAGE__->add_columns(
+ "id",
+ {
+ data_type => "integer",
+ is_auto_increment => 1,
+ is_nullable => 0,
+ sequence => "defect_types_id_seq",
+ },
+ "body_id",
+ { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
+ "name",
+ { data_type => "text", is_nullable => 0 },
+ "description",
+ { data_type => "text", is_nullable => 0 },
+ "extra",
+ { data_type => "text", is_nullable => 1 },
+);
+__PACKAGE__->set_primary_key("id");
+__PACKAGE__->add_unique_constraint("defect_types_body_id_name_key", ["body_id", "name"]);
+__PACKAGE__->belongs_to(
+ "body",
+ "FixMyStreet::DB::Result::Body",
+ { id => "body_id" },
+ { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
+);
+__PACKAGE__->has_many(
+ "contact_defect_types",
+ "FixMyStreet::DB::Result::ContactDefectType",
+ { "foreign.defect_type_id" => "self.id" },
+ { cascade_copy => 0, cascade_delete => 0 },
+);
+__PACKAGE__->has_many(
+ "problems",
+ "FixMyStreet::DB::Result::Problem",
+ { "foreign.defect_type_id" => "self.id" },
+ { cascade_copy => 0, cascade_delete => 0 },
+);
+
+
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-02-13 15:11:11
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:BBLjb/aAoTKJZerdYCeBMQ
+
+__PACKAGE__->many_to_many( contacts => 'contact_defect_types', 'contact' );
+
+__PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn");
+__PACKAGE__->rabx_column('extra');
+
+use Moo;
+use namespace::clean -except => [ 'meta' ];
+
+with 'FixMyStreet::Roles::Extra';
+
+
+1;
diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm
index 203e72fae..84db41490 100644
--- a/perllib/FixMyStreet/DB/Result/Problem.pm
+++ b/perllib/FixMyStreet/DB/Result/Problem.pm
@@ -108,6 +108,8 @@ __PACKAGE__->add_columns(
{ data_type => "text", is_nullable => 1 },
"response_priority_id",
{ data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
+ "defect_type_id",
+ { data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->has_many(
@@ -116,6 +118,17 @@ __PACKAGE__->has_many(
{ "foreign.problem_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
+__PACKAGE__->belongs_to(
+ "defect_type",
+ "FixMyStreet::DB::Result::DefectType",
+ { id => "defect_type_id" },
+ {
+ is_deferrable => 0,
+ join_type => "LEFT",
+ on_delete => "NO ACTION",
+ on_update => "NO ACTION",
+ },
+);
__PACKAGE__->has_many(
"moderation_original_datas",
"FixMyStreet::DB::Result::ModerationOriginalData",
@@ -153,8 +166,8 @@ __PACKAGE__->has_many(
);
-# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-09-07 11:01:40
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:iH9c4VZZN/ONnhN6g89DFw
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-02-13 15:11:11
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:8zzWlJX7OQOdvrGxKuZUmg
# Add fake relationship to stored procedure table
__PACKAGE__->has_one(
@@ -182,6 +195,8 @@ use Utils;
use FixMyStreet::Map::FMS;
use LWP::Simple qw($ua);
use RABX;
+use URI;
+use URI::QueryParam;
my $IM = eval {
require Image::Magick;
@@ -511,6 +526,30 @@ sub admin_url {
return $cobrand->admin_base_url . '/report_edit/' . $self->id;
}
+=head2 tokenised_url
+
+Return a url for this problem report that logs a user in
+
+=cut
+
+sub tokenised_url {
+ my ($self, $user, $params) = @_;
+
+ my $token = FixMyStreet::App->model('DB::Token')->create(
+ {
+ scope => 'email_sign_in',
+ data => {
+ id => $self->id,
+ email => $user->email,
+ r => $self->url,
+ p => $params,
+ }
+ }
+ );
+
+ return "/M/". $token->token;
+}
+
=head2 is_open
Returns 1 if the problem is in a open state otherwise 0.
@@ -523,6 +562,16 @@ sub is_open {
return exists $self->open_states->{ $self->state } ? 1 : 0;
}
+=head2 is_in_progress
+
+Sees if the problem is in an open, not 'confirmed' state.
+
+=cut
+
+sub is_in_progress {
+ my $self = shift;
+ return $self->is_open && $self->state ne 'confirmed' ? 1 : 0;
+}
=head2 is_fixed
@@ -587,9 +636,7 @@ sub meta_line {
my $meta = '';
my $category = $problem->category;
- if ($c->cobrand->can('change_category_text')) {
- $category = $c->cobrand->change_category_text($category);
- }
+ $category = $c->cobrand->call_hook(change_category_text => $category) || $category;
if ( $problem->anonymous ) {
if ( $problem->service and $category && $category ne _('Other') ) {
@@ -606,20 +653,28 @@ sub meta_line {
$meta = sprintf( _('Reported anonymously at %s'), $date_time );
}
} else {
+ my $problem_name = $problem->name;
+
+ if ($c->user_exists and
+ $c->user->has_permission_to('view_body_contribute_details', $problem->bodies_str_ids) and
+ $problem->name ne $problem->user->name) {
+ $problem_name = sprintf('%s (%s)', $problem->name, $problem->user->name );
+ }
+
if ( $problem->service and $category && $category ne _('Other') ) {
$meta = sprintf(
_('Reported via %s in the %s category by %s at %s'),
$problem->service, $category,
- $problem->name, $date_time
+ $problem_name, $date_time
);
} elsif ( $problem->service ) {
$meta = sprintf( _('Reported via %s by %s at %s'),
- $problem->service, $problem->name, $date_time );
+ $problem->service, $problem_name, $date_time );
} elsif ( $category and $category ne _('Other') ) {
$meta = sprintf( _('Reported in the %s category by %s at %s'),
- $category, $problem->name, $date_time );
+ $category, $problem_name, $date_time );
} else {
- $meta = sprintf( _('Reported by %s at %s'), $problem->name, $date_time );
+ $meta = sprintf( _('Reported by %s at %s'), $problem_name, $date_time );
}
}
@@ -651,6 +706,34 @@ sub body {
return $body;
}
+
+=head2 time_ago
+ Returns how long ago a problem was reported in an appropriately
+ prettified duration, depending on the duration.
+=cut
+
+sub time_ago {
+ my ( $self, $date ) = @_;
+ $date ||= 'confirmed';
+ my $duration = time() - $self->$date->epoch;
+
+ return Utils::prettify_duration( $duration );
+}
+
+=head2 days_ago
+
+ Returns how many days ago a problem was reported.
+
+=cut
+
+sub days_ago {
+ my ( $self, $date ) = @_;
+ $date ||= 'confirmed';
+ my $now = DateTime->now( time_zone => FixMyStreet->time_zone || FixMyStreet->local_time_zone );
+ my $duration = $now->delta_days($self->$date);
+ return $duration->delta_days;
+}
+
=head2 response_templates
Returns all ResponseTemplates attached to this problem's bodies, in alphabetical
@@ -684,6 +767,18 @@ sub response_priorities {
return $self->result_source->schema->resultset('ResponsePriority')->for_bodies($self->bodies_str_ids, $self->category);
}
+=head2 defect_types
+
+Returns all DefectTypes attached to this problem's category/contact, in
+alphabetical order of name.
+
+=cut
+
+sub defect_types {
+ my $self = shift;
+ return $self->result_source->schema->resultset('DefectType')->for_bodies($self->bodies_str_ids, $self->category);
+}
+
# returns true if the external id is the council's ref, i.e., useful to publish it
# (by way of an example, the Oxfordshire send method returns a useful reference when
# it succeeds, so that is the ref we should show on the problem report page).
@@ -700,17 +795,10 @@ sub can_display_external_id {
sub duration_string {
my ( $problem, $c ) = @_;
- my $body;
- if ( $c->cobrand->can('link_to_council_cobrand') ) {
- $body = $c->cobrand->link_to_council_cobrand($problem);
- } else {
- $body = $problem->body( $c );
- }
- if ( $c->cobrand->can('get_body_handler_for_problem') ) {
- my $handler = $c->cobrand->get_body_handler_for_problem( $problem );
- if ( $handler->can('is_council_with_case_management') && $handler->is_council_with_case_management ) {
- return sprintf(_('Received by %s moments later'), $body);
- }
+ my $body = $c->cobrand->call_hook(link_to_council_cobrand => $problem) || $problem->body($c);
+ my $handler = $c->cobrand->call_hook(get_body_handler_for_problem => $problem);
+ if ( $handler && $handler->call_hook('is_council_with_case_management') ) {
+ return sprintf(_('Received by %s moments later'), $body);
}
return unless $problem->whensent;
return sprintf(_('Sent to %s %s later'), $body,
@@ -1048,4 +1136,24 @@ has duplicates => (
},
);
+has traffic_management_options => (
+ is => 'ro',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ my $cobrand = $self->get_cobrand_logged;
+ $cobrand = $cobrand->call_hook(get_body_handler_for_problem => $self) || $cobrand;
+ return $cobrand->traffic_management_options;
+ },
+);
+
+has inspection_log_entry => (
+ is => 'ro',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ return $self->admin_log_entries->search({ action => 'inspected' }, { order_by => { -desc => 'whenedited' } })->first;
+ },
+);
+
1;
diff --git a/perllib/FixMyStreet/DB/Result/ResponsePriority.pm b/perllib/FixMyStreet/DB/Result/ResponsePriority.pm
index 6bc8474fa..44635d174 100644
--- a/perllib/FixMyStreet/DB/Result/ResponsePriority.pm
+++ b/perllib/FixMyStreet/DB/Result/ResponsePriority.pm
@@ -26,6 +26,8 @@ __PACKAGE__->add_columns(
{ data_type => "boolean", default_value => \"false", is_nullable => 0 },
"description",
{ data_type => "text", is_nullable => 1 },
+ "external_id",
+ { data_type => "text", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->add_unique_constraint("response_priorities_body_id_name_key", ["body_id", "name"]);
@@ -49,8 +51,8 @@ __PACKAGE__->has_many(
);
-# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-10-17 16:37:28
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:wok3cPA7cPjG4e9lnc1PIg
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-12-14 17:12:09
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:glsO0fLK6fNvg4TmW1DMPg
__PACKAGE__->many_to_many( contacts => 'contact_response_priorities', 'contact' );
diff --git a/perllib/FixMyStreet/DB/Result/ResponseTemplate.pm b/perllib/FixMyStreet/DB/Result/ResponseTemplate.pm
index 0d4377dba..5a2029eb1 100644
--- a/perllib/FixMyStreet/DB/Result/ResponseTemplate.pm
+++ b/perllib/FixMyStreet/DB/Result/ResponseTemplate.pm
@@ -33,6 +33,8 @@ __PACKAGE__->add_columns(
},
"auto_response",
{ data_type => "boolean", default_value => \"false", is_nullable => 0 },
+ "state",
+ { data_type => "text", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->add_unique_constraint("response_templates_body_id_title_key", ["body_id", "title"]);
@@ -50,8 +52,8 @@ __PACKAGE__->has_many(
);
-# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-08-24 11:29:04
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:KRm0RHbtrzuxzH0S/UAsdw
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-12-01 15:10:52
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ySPzQpFwJNki8XBjCNiqZQ
__PACKAGE__->many_to_many( contacts => 'contact_response_templates', 'contact' );
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm
index f4e5144f8..cf6de9a76 100644
--- a/perllib/FixMyStreet/DB/Result/User.pm
+++ b/perllib/FixMyStreet/DB/Result/User.pm
@@ -248,6 +248,15 @@ sub split_name {
return { first => $first || '', last => $last || '' };
}
+has body_permissions => (
+ is => 'ro',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ return [ $self->user_body_permissions->all ];
+ },
+);
+
sub permissions {
my ($self, $c, $body_id) = @_;
@@ -258,9 +267,7 @@ sub permissions {
return unless $self->belongs_to_body($body_id);
- my @permissions = $self->user_body_permissions->search({
- body_id => $self->from_body->id,
- })->all;
+ my @permissions = grep { $_->body_id == $self->from_body->id } @{$self->body_permissions};
return { map { $_->permission_type => 1 } @permissions };
}
@@ -269,33 +276,37 @@ sub has_permission_to {
return 1 if $self->is_superuser;
return 0 if !$body_ids || (ref $body_ids && !@$body_ids);
+ $body_ids = [ $body_ids ] unless ref $body_ids;
+ my %body_ids = map { $_ => 1 } @$body_ids;
- my $permission = $self->user_body_permissions->find({
- permission_type => $permission_type,
- body_id => $body_ids,
- });
- return $permission ? 1 : 0;
+ foreach (@{$self->body_permissions}) {
+ return 1 if $_->permission_type eq $permission_type && $body_ids{$_->body_id};
+ }
+ return 0;
}
=head2 has_body_permission_to
-Checks if the User has a from_body set, and the specified permission on that body.
+Checks if the User has a from_body set, the specified permission on that body,
+and optionally that their from_body is one particular body.
Instead of saying:
- ($user->from_body && $user->has_permission_to('user_edit', $user->from_body->id))
+ ($user->from_body && $user->from_body->id == $body_id && $user->has_permission_to('user_edit', $body_id))
You can just say:
- $user->has_body_permission_to('user_edit')
-
-NB unlike has_permission_to, this doesn't blindly return 1 if the user is a superuser.
+ $user->has_body_permission_to('user_edit', $body_id)
=cut
sub has_body_permission_to {
- my ($self, $permission_type) = @_;
+ my ($self, $permission_type, $body_id) = @_;
+
+ return 1 if $self->is_superuser;
+
return unless $self->from_body;
+ return if $body_id && $self->from_body->id != $body_id;
return $self->has_permission_to($permission_type, $self->from_body->id);
}
@@ -371,6 +382,8 @@ around add_to_planned_reports => sub {
around remove_from_planned_reports => sub {
my ($orig, $self, $report) = @_;
$self->user_planned_reports->active->for_report($report->id)->remove();
+ $report->unset_extra_metadata('order');
+ $report->update;
};
sub active_planned_reports {
diff --git a/perllib/FixMyStreet/DB/ResultSet/DefectType.pm b/perllib/FixMyStreet/DB/ResultSet/DefectType.pm
new file mode 100644
index 000000000..a873ef252
--- /dev/null
+++ b/perllib/FixMyStreet/DB/ResultSet/DefectType.pm
@@ -0,0 +1,22 @@
+package FixMyStreet::DB::ResultSet::DefectType;
+use base 'DBIx::Class::ResultSet';
+
+use strict;
+use warnings;
+
+sub for_bodies {
+ my ($rs, $bodies, $category) = @_;
+ my $attrs = {
+ 'me.body_id' => $bodies,
+ };
+ if ($category) {
+ $attrs->{'contact.category'} = [ $category, undef ];
+ }
+ $rs->search($attrs, {
+ order_by => 'name',
+ join => { 'contact_defect_types' => 'contact' },
+ distinct => 1,
+ });
+}
+
+1;