Merge branch 'master' into topic/simple_image_upload
[sgn.git] / cgi-bin / solpeople / admin / attribute.pl
blob83ca853857efefbd573704ee8fbeef25329afb10
2 use strict;
4 use CXGN::DB::Connection;
5 use CXGN::Page;
6 use CXGN::Login;
7 use CXGN::People;
8 use CXGN::DB::Connection;
10 my $page = CXGN::Page->new("Make Attributions", "Lukas and John");
11 our $dbh = CXGN::DB::Connection-> new("sgn_people");
13 my($action, $data_type, $data_id, $attributed_to_id, $attributed_to_type, $role_id)=$page->get_encoded_arguments('action', 'data_type', 'data_id','attributed_to_id', 'attributed_to_type', 'role_id');
15 my $logged_in_person_id=CXGN::Login->new($dbh)->verify_session();
16 my $logged_in_user=CXGN::People::Person->new($dbh, $logged_in_person_id);
17 my $logged_in_person_id=$logged_in_user->get_sp_person_id();
18 my $logged_in_username=$logged_in_user->get_first_name()." ".$logged_in_user->get_last_name();
19 my $logged_in_user_type=$logged_in_user->get_user_type();
21 if($logged_in_user_type ne 'curator') {
22 $page->message_page("You don't have the necessary privileges to perform this action! ");
27 $page->header();
28 if ($action eq "verify") { verify( $data_type, $data_id, $attributed_to_id, $attributed_to_type, $role_id); }
29 elsif ($action eq "store" ) { store( $data_type, $data_id, $attributed_to_id, $attributed_to_type, $role_id); }
30 else { form(); }
31 $page->footer();
34 sub form {
36 my $schema = $dbh->qualify_schema("metadata");
38 my $q = "SELECT $schema.roles.role_id, $schema.roles.role_name FROM $schema.roles";
39 my $sth = $dbh->prepare($q);
40 $sth->execute();
41 my @roles = ();
42 my @role_ids=();
43 while (my ($role_id, $role) = $sth->fetchrow_array()) {
44 push @roles, $role;
45 push @role_ids, $role_id;
48 my $role_select = "<select name=\"role_id\" >";
49 for (my $i=0; $i<@roles; $i++) {
50 $role_select .= qq { <option value="$role_ids[$i]">$roles[$i]</option> };
52 $role_select .= "</select>";
55 print <<HTML;
57 <form>
58 <table><tr>
59 <td>Data type:</td>
60 <td>
61 <select name="data_type">
62 <option value="bac">BAC</option>
63 <option value="marker">Marker</option>
64 </select>
65 </td>
66 <td>Data id:</td>
67 <td> <input name="data_id" /></td>
68 </tr>
69 <tr>
70 <td>Role type:</td>
71 <td colspan="3">
72 $role_select
73 </td>
74 </tr>
76 <tr>
77 <td>Attribution to</td>
78 <td>
79 <select name="attributed_to_type">
80 <option value="person">Person</option>
81 <option value="organization">Organization</option>
82 <option value="project">Project</option>
83 </select>
84 </td>
85 <td>id</td>
86 <td> <input name="attributed_to_id" /></td>
87 </tr></table>
90 <br />
91 <input type="hidden" name="action" value="verify" />
92 <input type="submit" value="Submit" />
94 </form>
97 HTML
102 sub verify {
103 my ($data_type, $data_id, $attributed_to_id, $attributed_to_type, $role_id) = @_;
106 if (!$attributed_to_type || !$attributed_to_id || !$data_type || !$data_id) {
107 $page->message_page("Need all data filled in!");
110 my $attributed_name = "";
111 if ($attributed_to_type eq "person") {
113 my $p = CXGN::People::Person->new_person($dbh, $attributed_to_id);
114 $attributed_name = $p->get_first_name()." ".$p->get_last_name();
116 elsif ($attributed_to_type eq "organization") {
117 my $p = CXGN::People::Organization->new($dbh, $attributed_to_id);
118 $attributed_name = $p->get_name();
120 elsif ($attributed_to_type eq "project") {
121 my $p = CXGN::People::Project->new($dbh, $attributed_to_id);
122 $attributed_name = $p->get_name();
125 my $q = "";
126 if ($data_type eq "bac") {
127 my $schema = $dbh->qualify_schema("genomic");
128 $q = "SELECT * FROM $schema.clone WHERE $schema.clone.clone_id=?";
130 if ($data_type eq "marker") {
131 my $schema = $dbh->qualify_schema("sgn");
132 $q = "SELECT * FROM $schema.markers WHERE $schema.markers.marker_id=?";
134 my $sth = $dbh->prepare($q);
135 $sth->execute($data_id);
136 my ($hashref) = $sth->fetchrow_hashref();
138 print qq { <div class="boxbgcolor2"><b>Database object</b><br /><br /> };
139 foreach my $k (keys %$hashref) {
140 print "$k: $$hashref{$k}<br />\n" if (exists $$hashref{$k});
142 print "<br /></div><br />Attribute to : <br />";
143 print "<b>$attributed_name</b><br /><br />";
145 print <<HTML;
146 <form>
147 <input type="hidden" name="role_id" value="$role_id" />
148 <input type="hidden" name="attributed_to_id" value="$attributed_to_id" />
149 <input type="hidden" name="attributed_to_type" value="$attributed_to_type" />
150 <input type="hidden" name="data_type" value="$data_type" />
151 <input type="hidden" name="data_id" value="$data_id" />
152 <input type="hidden" name="action" value="store" />
154 <input type="submit" value="Store" />
155 </form>
157 HTML
162 sub store {
163 my ($data_type, $data_id, $attributed_to_id, $attributed_to_type, $role_id) = @_;
165 my $database_name="";
166 my $table_name = "";
168 if ($data_type eq "bac") {
169 $database_name="genomic";
170 $table_name="clone";
172 elsif ($data_type eq "marker") {
173 $database_name="sgn";
174 $table_name="markers";
176 my $person_id=undef;
177 my $organization_id=undef;
178 my $project_id=undef;
180 if ($attributed_to_type eq "person") {
181 $person_id = $attributed_to_id;
183 elsif ($attributed_to_type eq "organization") {
184 $organization_id = $attributed_to_id;
186 elsif ($attributed_to_type eq "project") {
187 $project_id=$attributed_to_id;
190 my $q = "SELECT attribution_id from metadata.attribution WHERE database_name=? AND table_name=? AND row_id=?";
191 my $sth = $dbh->prepare($q);
192 $sth->execute($database_name,$table_name, $data_id);
193 my $attribution_id="";
194 if ($attribution_id = ($sth->fetchrow_array())[0]) {
195 print "The attribution is already in the database.\n";
198 else {
199 my $iq = "INSERT INTO metadata.attribution (database_name, table_name, row_id) VALUES (?, ?, ?)";
201 my $sth = $dbh->prepare($iq);
202 $sth->execute($database_name, $table_name, $data_id);
203 $attribution_id = $dbh->last_insert_id("attribution", "metadata");
206 my $q2 = "SELECT attribution_to_id FROM metadata.attribution_to WHERE
207 person_id=? AND organization_id =? AND project_id=?";
209 my $sth2 = $dbh->prepare($q2);
210 $sth2->execute($person_id, $organization_id, $project_id);
212 my ($attributed_to_id) = ($sth2->fetchrow_array())[0];
214 if (!$attributed_to_id) {
215 my $iq2="INSERT INTO metadata.attribution_to (attribution_id, person_id, organiztion_id, project_id, role_id) VALUES (?, ?, ?, ?, ?)";
218 my $isth = $dbh->prepare($iq2);
219 $isth -> execute($attribution_id, $person_id, $organization_id, $project_id, $role_id);
221 else {
222 print "The data object was already attributed to that entity in the database.";
224 print "All done!\n";