aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin.pm102
-rw-r--r--perllib/FixMyStreet/Cobrand/Default.pm11
-rw-r--r--perllib/FixMyStreet/DB/Result/Contact.pm12
-rw-r--r--perllib/FixMyStreet/DB/Result/ContactResponseTemplate.pm46
-rw-r--r--perllib/FixMyStreet/DB/Result/ResponseTemplate.pm13
-rw-r--r--perllib/FixMyStreet/TestMech.pm11
6 files changed, 153 insertions, 42 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm
index 652113734..3c02c1318 100644
--- a/perllib/FixMyStreet/App/Controller/Admin.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin.pm
@@ -309,7 +309,7 @@ sub body : Path('body') : Args(1) {
$c->forward('update_contacts');
}
- $c->forward('display_contacts');
+ $c->forward('fetch_contacts');
return 1;
}
@@ -457,14 +457,14 @@ sub check_body_params : Private {
}
}
-sub display_contacts : Private {
+sub fetch_contacts : Private {
my ( $self, $c ) = @_;
my $contacts = $c->stash->{body}->contacts->search(undef, { order_by => [ 'category' ] } );
$c->stash->{contacts} = $contacts;
$c->stash->{live_contacts} = $contacts->search({ deleted => 0 });
- if ( $c->get_param('text') && $c->get_param('text') == 1 ) {
+ if ( $c->get_param('text') && $c->get_param('text') eq '1' ) {
$c->stash->{template} = 'admin/council_contacts.txt';
$c->res->content_type('text/plain; charset=utf-8');
return 1;
@@ -893,57 +893,84 @@ sub categories_for_point : Private {
sub templates : Path('templates') : Args(0) {
my ( $self, $c ) = @_;
- $c->detach( '/page_error_404_not_found' )
- unless $c->cobrand->moniker eq 'zurich';
-
my $user = $c->user;
- $self->templates_for_body($c, $user->from_body );
+ if ($user->is_superuser) {
+ $c->forward('fetch_all_bodies');
+ $c->stash->{template} = 'admin/templates_index.html';
+ } elsif ( $user->from_body ) {
+ $c->forward('load_template_body', [ $user->from_body->id ]);
+ $c->res->redirect( $c->uri_for( 'templates', $c->stash->{body}->id ) );
+ } else {
+ $c->detach( '/page_error_404_not_found' );
+ }
}
sub templates_view : Path('templates') : Args(1) {
my ($self, $c, $body_id) = @_;
- $c->detach( '/page_error_404_not_found' )
- unless $c->cobrand->moniker eq 'zurich';
+ $c->forward('load_template_body', [ $body_id ]);
- # e.g. for admin
+ my @templates = $c->stash->{body}->response_templates->search(
+ undef,
+ {
+ order_by => 'title'
+ }
+ );
- my $body = $c->model('DB::Body')->find($body_id)
- or $c->detach( '/page_error_404_not_found' );
+ $c->stash->{response_templates} = \@templates;
- $self->templates_for_body($c, $body);
+ $c->stash->{template} = 'admin/templates.html';
}
sub template_edit : Path('templates') : Args(2) {
my ( $self, $c, $body_id, $template_id ) = @_;
- $c->detach( '/page_error_404_not_found' )
- unless $c->cobrand->moniker eq 'zurich';
-
- my $body = $c->model('DB::Body')->find($body_id)
- or $c->detach( '/page_error_404_not_found' );
- $c->stash->{body} = $body;
+ $c->forward('load_template_body', [ $body_id ]);
my $template;
if ($template_id eq 'new') {
- $template = $body->response_templates->new({});
+ $template = $c->stash->{body}->response_templates->new({});
}
else {
- $template = $body->response_templates->find( $template_id )
+ $template = $c->stash->{body}->response_templates->find( $template_id )
or $c->detach( '/page_error_404_not_found' );
}
+ $c->forward('fetch_contacts');
+ my @contacts = $template->contacts->all;
+ my @live_contacts = $c->stash->{live_contacts}->all;
+ my %active_contacts = map { $_->id => 1 } @contacts;
+ my @all_contacts = map { {
+ id => $_->id,
+ category => $_->category,
+ active => $active_contacts{$_->id},
+ } } @live_contacts;
+ $c->stash->{contacts} = \@all_contacts;
+
if ($c->req->method eq 'POST') {
- if ($c->get_param('delete_template') eq _("Delete template")) {
+ if ($c->get_param('delete_template') && $c->get_param('delete_template') eq _("Delete template")) {
+ $template->contact_response_templates->delete_all;
$template->delete;
} else {
$template->title( $c->get_param('title') );
- $template->text ( $c->get_param('text') );
+ $template->text( $c->get_param('text') );
+ $template->auto_response( $c->get_param('auto_response') ? 1 : 0 );
$template->update_or_insert;
+
+ my @live_contact_ids = map { $_->id } @live_contacts;
+ my @new_contact_ids = grep { $c->get_param("contacts[$_]") } @live_contact_ids;
+ $template->contact_response_templates->search({
+ contact_id => { '!=' => \@new_contact_ids },
+ })->delete;
+ foreach my $contact_id (@new_contact_ids) {
+ $template->contact_response_templates->find_or_create({
+ contact_id => $contact_id,
+ });
+ }
}
- $c->res->redirect( $c->uri_for( 'templates', $body->id ) );
+ $c->res->redirect( $c->uri_for( 'templates', $c->stash->{body}->id ) );
}
$c->stash->{response_template} = $template;
@@ -951,22 +978,25 @@ sub template_edit : Path('templates') : Args(2) {
$c->stash->{template} = 'admin/template_edit.html';
}
+sub load_template_body : Private {
+ my ($self, $c, $body_id) = @_;
-sub templates_for_body {
- my ( $self, $c, $body ) = @_;
-
- $c->stash->{body} = $body;
+ my $zurich_user = $c->user->from_body && $c->cobrand->moniker eq 'zurich';
+ my $has_permission = $c->user->from_body &&
+ $c->user->from_body->id eq $body_id &&
+ $c->user->has_permission_to('template_edit', $body_id);
- my @templates = $body->response_templates->search(
- undef,
- {
- order_by => 'title'
- }
- );
+ unless ( $c->user->is_superuser || $zurich_user || $has_permission ) {
+ $c->detach( '/page_error_404_not_found' );
+ }
- $c->stash->{response_templates} = \@templates;
+ # Regular users can only view their own body's templates
+ if ( !$c->user->is_superuser && $body_id ne $c->user->from_body->id ) {
+ $c->res->redirect( $c->uri_for( 'templates', $c->user->from_body->id ) );
+ }
- $c->stash->{template} = 'admin/templates.html';
+ $c->stash->{body} = $c->model('DB::Body')->find($body_id)
+ or $c->detach( '/page_error_404_not_found' );
}
sub users: Path('users') : Args(0) {
diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm
index ee110a18b..a58f50b18 100644
--- a/perllib/FixMyStreet/Cobrand/Default.pm
+++ b/perllib/FixMyStreet/Cobrand/Default.pm
@@ -646,7 +646,7 @@ sub admin_pages {
'summary' => [_('Summary'), 0],
'bodies' => [_('Bodies'), 1],
'reports' => [_('Reports'), 2],
- 'timeline' => [_('Timeline'), 3],
+ 'timeline' => [_('Timeline'), 4],
'users' => [_('Users'), 5],
'flagged' => [_('Flagged'), 6],
'stats' => [_('Stats'), 7],
@@ -661,6 +661,12 @@ sub admin_pages {
if ( $user->is_superuser ) {
$pages->{config} = [ _('Configuration'), 8];
};
+ # And some that need special permissions
+ if ( $user->is_superuser || $user->has_body_permission_to('template_edit') ) {
+ $pages->{templates} = [_('Templates'), 3],
+ $pages->{template_edit} = [undef, undef],
+ };
+
return $pages;
}
@@ -711,6 +717,9 @@ sub available_permissions {
user_assign_body => _("Grant access to the admin"),
user_assign_areas => _("Assign users to areas"), # future use
},
+ _("Bodies") => {
+ template_edit => _("Add/edit response templates"),
+ },
};
}
diff --git a/perllib/FixMyStreet/DB/Result/Contact.pm b/perllib/FixMyStreet/DB/Result/Contact.pm
index 58d8e58de..ea9b656aa 100644
--- a/perllib/FixMyStreet/DB/Result/Contact.pm
+++ b/perllib/FixMyStreet/DB/Result/Contact.pm
@@ -55,10 +55,16 @@ __PACKAGE__->belongs_to(
{ id => "body_id" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
);
+__PACKAGE__->has_many(
+ "contact_response_templates",
+ "FixMyStreet::DB::Result::ContactResponseTemplate",
+ { "foreign.contact_id" => "self.id" },
+ { cascade_copy => 0, cascade_delete => 0 },
+);
-# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hq/BFHDEu4OUI4MSy3OyHg
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-08-24 11:29:04
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:CXUabm3Yd11OoIYJceSPag
__PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn");
__PACKAGE__->rabx_column('extra');
@@ -68,6 +74,8 @@ use namespace::clean -except => [ 'meta' ];
with 'FixMyStreet::Roles::Extra';
+__PACKAGE__->many_to_many( response_templates => 'contact_response_templates', 'response_template' );
+
sub get_metadata_for_input {
my $self = shift;
my $id_field = $self->id_field;
diff --git a/perllib/FixMyStreet/DB/Result/ContactResponseTemplate.pm b/perllib/FixMyStreet/DB/Result/ContactResponseTemplate.pm
new file mode 100644
index 000000000..3c777533c
--- /dev/null
+++ b/perllib/FixMyStreet/DB/Result/ContactResponseTemplate.pm
@@ -0,0 +1,46 @@
+use utf8;
+package FixMyStreet::DB::Result::ContactResponseTemplate;
+
+# 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_response_templates");
+__PACKAGE__->add_columns(
+ "id",
+ {
+ data_type => "integer",
+ is_auto_increment => 1,
+ is_nullable => 0,
+ sequence => "contact_response_templates_id_seq",
+ },
+ "contact_id",
+ { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
+ "response_template_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(
+ "response_template",
+ "FixMyStreet::DB::Result::ResponseTemplate",
+ { id => "response_template_id" },
+ { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
+);
+
+
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-08-24 11:29:04
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:d6niNsxi2AsijhvJSuQeKw
+
+
+# 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/ResponseTemplate.pm b/perllib/FixMyStreet/DB/Result/ResponseTemplate.pm
index d189bf3ec..0d4377dba 100644
--- a/perllib/FixMyStreet/DB/Result/ResponseTemplate.pm
+++ b/perllib/FixMyStreet/DB/Result/ResponseTemplate.pm
@@ -31,6 +31,8 @@ __PACKAGE__->add_columns(
is_nullable => 0,
original => { default_value => \"now()" },
},
+ "auto_response",
+ { data_type => "boolean", default_value => \"false", is_nullable => 0 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->add_unique_constraint("response_templates_body_id_title_key", ["body_id", "title"]);
@@ -40,11 +42,18 @@ __PACKAGE__->belongs_to(
{ id => "body_id" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
);
+__PACKAGE__->has_many(
+ "contact_response_templates",
+ "FixMyStreet::DB::Result::ContactResponseTemplate",
+ { "foreign.response_template_id" => "self.id" },
+ { cascade_copy => 0, cascade_delete => 0 },
+);
-# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-07-20 14:38:36
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ECFQLMxOFGwv7cwfHLlszw
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-08-24 11:29:04
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:KRm0RHbtrzuxzH0S/UAsdw
+__PACKAGE__->many_to_many( contacts => 'contact_response_templates', 'contact' );
# You can replace this text with custom code or comments, and it will be preserved on regeneration
1;
diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm
index c3583bb3e..ee9631bdf 100644
--- a/perllib/FixMyStreet/TestMech.pm
+++ b/perllib/FixMyStreet/TestMech.pm
@@ -600,12 +600,21 @@ sub delete_body {
my $body = shift;
$mech->delete_problems_for_body($body->id);
- $body->contacts->delete;
+ $mech->delete_contact($_) for $body->contacts;
$mech->delete_user($_) for $body->users;
+ $_->delete for $body->response_templates;
$body->body_areas->delete;
$body->delete;
}
+sub delete_contact {
+ my $mech = shift;
+ my $contact = shift;
+
+ $contact->contact_response_templates->delete_all;
+ $contact->delete;
+}
+
sub delete_problems_for_body {
my $mech = shift;
my $body = shift;