From 7df6fecfc407a6f356631c55e9f7172261f3ce00 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 14 Nov 2017 15:02:46 +0000 Subject: [PATCH] Added org lookup and assignment for import --- lib/Pear/LocalLoop.pm | 1 - lib/Pear/LocalLoop/Controller/Admin/Import.pm | 45 ++++++++++++++++--- templates/admin/import/get_org.html.ep | 36 +++++++++++++++ templates/admin/import/list.html.ep | 6 ++- 4 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 templates/admin/import/get_org.html.ep diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 2980c4e..b8d768b 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -207,7 +207,6 @@ sub startup { $admin_routes->get('/import/:set_id')->to('admin-import#list'); $admin_routes->get('/import/:set_id/user')->to('admin-import#get_user'); $admin_routes->get('/import/:set_id/org')->to('admin-import#get_org'); - $admin_routes->post('/import/:set_id/org')->to('admin-import#set_org'); $admin_routes->get('/import/:set_id/:value_id')->to('admin-import#get_value'); $admin_routes->post('/import/:set_id/:value_id')->to('admin-import#post_value'); diff --git a/lib/Pear/LocalLoop/Controller/Admin/Import.pm b/lib/Pear/LocalLoop/Controller/Admin/Import.pm index c1c2fe1..9d840a1 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Import.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Import.pm @@ -193,16 +193,51 @@ sub get_user { ); } - - sub get_org { my $c = shift; + my $set_id = $c->param('set_id'); + my $org_name = $c->param('org'); -} + my $values_rs = $c->result_set->find($set_id)->values->search( + { + org_name => $org_name, + ignore_value => 0, + } + ); -sub set_org { - my $c = shift; + unless ( $values_rs->count > 0 ) { + $c->flash( error => 'Organisation not found or all values are ignored' ); + return $c->redirect_to( '/admin/import/' . $set_id ); + } + my $lookup_result = $c->result_set->find($set_id)->lookups->find( + { name => $org_name }, + ); + + my $entity_id = $c->param('entity'); + + my $orgs_rs = $c->schema->resultset('Organisation'); + + if ( defined $entity_id && $orgs_rs->find({ entity_id => $entity_id }) ) { + if ( defined $lookup_result ) { + $lookup_result->update({ entity_id => $entity_id }); + } else { + $lookup_result = $c->result_set->find($set_id)->lookups->create( + { + name => $org_name, + entity_id => $entity_id, + }, + ); + } + } elsif ( defined $entity_id ) { + $c->stash( error => "Organisation does not exist" ); + } + + $c->stash( + orgs_rs => $orgs_rs, + lookup => $lookup_result, + org_name => $org_name, + ); } sub get_value { diff --git a/templates/admin/import/get_org.html.ep b/templates/admin/import/get_org.html.ep new file mode 100644 index 0000000..b6bc8a1 --- /dev/null +++ b/templates/admin/import/get_org.html.ep @@ -0,0 +1,36 @@ +% layout 'admin_errors'; +% title 'Import'; +
+
+

<%= $org_name %>

+
+ +
+
+

+ Organisations +

+
+ Choose a user to assign to this name +
+
+ % for my $org ( $orgs_rs->all ) { + +
+
+ %= $org->name +
+
+
+ % } +
+
+
+
+ diff --git a/templates/admin/import/list.html.ep b/templates/admin/import/list.html.ep index 919e982..e7b8bac 100644 --- a/templates/admin/import/list.html.ep +++ b/templates/admin/import/list.html.ep @@ -59,10 +59,14 @@ %= $org->org_name
+ % if ( my $lookup = $import_lookup_rs->find({ name => $org->org_name }) ) { + <%= $lookup->entity->name %> + % } else { Unassigned + % }
- Select + Select