4 SGN::Controller::AJAX::People - a REST controller class to provide the
5 backend for the sgn_people schema
9 REST interface for searching people, getting user data, etc.
13 Naama Menda <nm249@cornell.edu>
18 package SGN
::Controller
::AJAX
::People
;
23 use List
::MoreUtils qw
/any /;
25 use CXGN
::People
::Schema
;
26 use CXGN
::People
::Roles
;
28 BEGIN { extends
'Catalyst::Controller::REST' }
31 default => 'application/json',
33 map => { 'application/json' => 'JSON' },
40 Public Path: /ajax/people/autocomplete
42 Autocomplete a person name. Takes a single GET param,
43 C<person>, responds with a JSON array of completions for that term.
47 sub autocomplete
: Local
: ActionClass
('REST') { }
49 sub autocomplete_GET
:Args
(1) {
50 my ( $self, $c , $print_id ) = @_;
52 my $person = $c->req->param('term');
53 # trim and regularize whitespace
54 $person =~ s/(^\s+|\s+)$//g;
56 my $q = "SELECT sp_person_id, first_name, last_name FROM sgn_people.sp_person
57 WHERE lower(first_name) like ? OR lower(last_name) like ? and censor =0 and disabled IS NULL
60 my $sth = $c->dbc->dbh->prepare($q);
61 $sth->execute( lc "$person\%" , lc "$person\%" );
63 while (my ($sp_person_id, $first_name, $last_name) = $sth->fetchrow_array ) {
64 $sp_person_id = $print_id ?
"," . $sp_person_id : undef;
65 push @results , "$first_name, $last_name $sp_person_id";
67 $c->stash->{rest
} = \
@results;
70 sub people_and_roles
: Path
('/ajax/people/people_and_roles') : ActionClass
('REST') { }
72 sub people_and_roles_GET
: Args
(0) {
75 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
76 my $person_roles = CXGN
::People
::Roles
->new({ bcs_schema
=>$schema });
77 my $sp_persons = $person_roles->get_sp_persons();
78 my $sp_roles = $person_roles->get_sp_roles();
79 my %results = ( sp_persons
=> $sp_persons, sp_roles
=> $sp_roles );
80 $c->stash->{rest
} = \
%results;
83 sub add_person_role
: Path
('/ajax/people/add_person_role') : ActionClass
('REST') { }
85 sub add_person_role_GET
: Args
(0) {
88 my $user = $c->user();
90 $c->stash->{rest
} = {error
=>'You must be logged in first!'};
93 if (!$user->check_roles("curator")) {
94 $c->stash->{rest
} = {error
=>'You must be logged in with the correct role!'};
97 my $sp_person_id = $c->req->param('sp_person_id');
98 my $sp_role_id = $c->req->param('sp_role_id');
99 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
100 my $person_roles = CXGN
::People
::Roles
->new({ bcs_schema
=>$schema });
101 my $add_role = $person_roles->add_sp_person_role($sp_person_id, $sp_role_id);
102 $c->stash->{rest
} = {success
=>1};
105 sub roles
:Chained
('/') PathPart
('ajax/roles') CaptureArgs
(0) {
109 print STDERR
"ajax/roles...\n";
111 $c->stash->{message
} = "processing";
114 sub list_roles
:Chained
('roles') PathPart
('list') Args
(0) {
118 print STDERR
"roles list\n";
120 $c->stash->{rest
} = { error
=> "You must be logged in to use this function." };
124 my $schema = $c->dbic_schema("CXGN::People::Schema");
127 my $rs1 = $schema->resultset("SpRole")->search( { } );
128 while (my $row = $rs1->next()) {
129 $roles{$row->sp_role_id} = $row->name();
132 my $rs2 = $schema->resultset("SpPerson")->search(
133 { censor
=> 0, disabled
=> undef },
134 { join => 'sp_person_roles',
135 '+select' => ['sp_person_roles.sp_role_id', 'sp_person_roles.sp_person_role_id' ],
136 '+as' => ['sp_role_id', 'sp_person_role_id' ],
137 order_by
=> 'sp_role_id' });
142 my %role_colors = ( curator
=> 'red', submitter
=> 'orange', user
=> 'green' );
143 my $default_color = "#0275d8";
146 while (my $row = $rs2->next()) {
147 my $person_name = $row->first_name." ".$row->last_name();
148 my $delete_link = "";
149 my $add_user_link = ' <a href="#" onclick="javascript:add_user_role('.$row->get_column('sp_person_id').", \'".$person_name."\')\"><span style=\"color:darkgrey;width:8px;height:8px;border:solid;border-width:1px;padding:1px;\"><b>+</b></a></span>";
150 if ($c->user()->has_role("curator")) {
151 $delete_link = '<a href="javascript:delete_user_role('.$row->get_column('sp_person_role_id').')"><b>X</b></a>';
158 $hash{$row->sp_person_id}->{userlink
} = '<a href="/solpeople/personal-info.pl?sp_person_id='.$row->sp_person_id().'">'.$row->first_name()." ".$row->last_name().'</a>';
160 my $role_name = $roles{$row->get_column('sp_role_id')};
162 print STDERR
"ROLE : $role_name\n";
164 if (! $c->user()->has_role("curator")) {
165 # only show breeding programs
166 if ($role_name !~ /curator|user|submitter/) {
167 $hash{$row->sp_person_id}->{userroles
} .= '<span style="border-radius:16px;color:white;border-style:solid;border:1px;padding:8px;margin:10px;background-color:'.$default_color.'"><b>'.$role_name."</b></span>";
171 my $color = $role_colors{$role_name} || $default_color;
172 $hash{$row->sp_person_id}->{userroles
} .= '<span style="border-radius:16px;color:white;border-style:solid;border:1px;padding:8px;margin:6px;background-color:'.$color.'"><b>'. $delete_link." ".$role_name."</b></span>";
173 $hash{$row->sp_person_id}->{add_user_link
} = $add_user_link;
178 foreach my $k (keys %hash) {
179 $hash{$k}->{userroles
} .= $hash{$k}->{add_user_link
};
180 push @data, [ $hash{$k}->{userlink
}, $hash{$k}->{userroles
} ];
183 $c->stash->{rest
} = { data
=> \
@data };
186 # sub add_user :Chained('roles') PathPart('add/association/user') CaptureArgs(1) {
189 # my $user_id = shift;
191 # $c->stash->{sp_person_id} = $user_id;
195 # sub add_user_role :Chained('add_user') PathPart('role') CaptureArgs(1) {
198 # my $role_id = shift;
200 # if (! $c->user()) {
201 # $c->stash->{rest} = { error => "You must be logged in to use this function." };
205 # if (! $c->user()->has_role("curator")) {
206 # $c->stash->{rest} = { error => "You don't have the necessary privileges for maintaining user roles." };
214 sub delete :Chained
('roles') PathPart
('delete/association') Args
(1) {
217 my $sp_person_role_id = shift;
220 $c->stash->{rest
} = { error
=> "You must be logged in to use this function." };
224 if (! $c->user()->has_role("curator")) {
225 $c->stash->{rest
} = { error
=> "You don't have the necessary privileges for maintaining user roles." };
229 my $schema = $c->dbic_schema("CXGN::People::Schema");
231 my $row = $schema->resultset("SpPersonRole")->find( { sp_person_role_id
=> $sp_person_role_id } );
234 $c->stash->{rest
} = { error
=> 'The relationship does not exist.' };
239 $c->stash->{rest
} = { message
=> "Role associated with user deleted." };