diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 0457f2e..e2fa1ae 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -204,6 +204,10 @@ sub startup { $admin_routes->get('/import/add')->to('admin-import#get_add'); $admin_routes->post('/import/add')->to('admin-import#post_add'); $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'); # my $user_routes = $r->under('/')->to('root#under'); diff --git a/lib/Pear/LocalLoop/Controller/Admin/Import.pm b/lib/Pear/LocalLoop/Controller/Admin/Import.pm index 87fb963..c2b3ef1 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Import.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Import.pm @@ -31,12 +31,14 @@ sub list { my $import_value_rs = $c->result_set->get_values($set_id); my $import_users_rs = $c->result_set->get_users($set_id); my $import_org_rs = $c->result_set->get_orgs($set_id); + my $import_lookup_rs = $c->result_set->get_lookups($set_id); $c->stash( import_set => $import_set, import_value_rs => $import_value_rs, import_users_rs => $import_users_rs, import_org_rs => $import_org_rs, + import_lookup_rs => $import_lookup_rs, ); } @@ -90,7 +92,6 @@ sub post_add { } for my $data ( @$csv_output ) { - Dwarn $data; for my $key ( qw/ user value organisation / ) { unless ( defined $data->{$key} ) { $c->flash( error => "Undefined [$key] data found", csv_data => $csv_data, date_format => $date_format ); @@ -134,6 +135,63 @@ sub post_add { $c->redirect_to( '/admin/import/' . $value_set->id ); } +sub get_user { + my $c = shift; + my $set_id = $c->param('set_id'); + my $user_name = $c->param('user'); + + my $values_rs = $c->result_set->find($set_id)->values->search( + { + user_name => $user_name, + ignore_value => 0, + } + ); + + unless ( $values_rs->count > 0 ) { + $c->flash( error => 'User 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 => $user_name }, + ); + + my $entity_id = $c->param('entity'); + + my $users_rs = $c->schema->resultset('User'); + + if ( defined $entity_id && $users_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 => $user_name, + entity_id => $entity_id, + }, + ); + } + } elsif ( defined $entity_id ) { + $c->stash( error => "User does not exist" ); + } + + $c->stash( + users_rs => $users_rs, + lookup => $lookup_result, + user_name => $user_name, + ); +} + +sub get_org { + my $c = shift; + +} + +sub set_org { + my $c = shift; + +} + sub get_value { my $c = shift; my $set_id = $c->param('set_id'); diff --git a/lib/Pear/LocalLoop/Schema/Result/User.pm b/lib/Pear/LocalLoop/Schema/Result/User.pm index 162fa12..9eee628 100644 --- a/lib/Pear/LocalLoop/Schema/Result/User.pm +++ b/lib/Pear/LocalLoop/Schema/Result/User.pm @@ -128,6 +128,18 @@ sub name { } } +sub full_name { + my $self = shift; + + if ( defined $self->entity->customer ) { + return $self->entity->customer->full_name; + } elsif ( defined $self->entity->organisation ) { + return $self->entity->organisation->name; + } else { + return; + } +} + # TODO Deprecate this sub? sub type { my $self = shift; diff --git a/lib/Pear/LocalLoop/Schema/ResultSet/ImportSet.pm b/lib/Pear/LocalLoop/Schema/ResultSet/ImportSet.pm index 614110a..b8bd24e 100644 --- a/lib/Pear/LocalLoop/Schema/ResultSet/ImportSet.pm +++ b/lib/Pear/LocalLoop/Schema/ResultSet/ImportSet.pm @@ -13,7 +13,7 @@ sub get_values { 'values', undef, { - order_by => { -asc => 'id' }, + order_by => { '-asc' => 'id' }, }, ); } @@ -40,4 +40,17 @@ sub get_orgs { ); } +sub get_lookups { + my $self = shift; + my $id = shift; + + return $self->find($id)->search_related( + 'lookups', + undef, + { + order_by => { '-asc' => 'id' }, + }, + ); +} + 1; diff --git a/templates/admin/import/get_user.html.ep b/templates/admin/import/get_user.html.ep new file mode 100644 index 0000000..d0ead9d --- /dev/null +++ b/templates/admin/import/get_user.html.ep @@ -0,0 +1,48 @@ +% layout 'admin_errors'; +% title 'Import'; +