libraries are now filtered on id (there are duplicate names, such as "Bookmobile")
[Evergreen.git] / Open-ILS / src / cgi-bin / usr_group-setup.cgi
1 #!/usr/bin/perl
2 use strict;
3
4 use OpenILS::Application::Storage;
5 use OpenILS::Application::Storage::CDBI;
6
7 # I need to abstract the driver loading away...
8 use OpenILS::Application::Storage::Driver::Pg;
9
10 use CGI qw/:standard start_*/;
11
12 our %config;
13 do '##CONFIG##/live-db-setup.pl';
14
15 OpenILS::Application::Storage::CDBI->connection($config{dsn},$config{usr},$config{pw});
16 OpenILS::Application::Storage::CDBI->db_Main->{ AutoCommit } = 1;
17
18 my $cgi = new CGI;
19
20 #-------------------------------------------------------------------------------
21 # setup part
22 #-------------------------------------------------------------------------------
23
24 my %org_cols = ( qw/id GroupID name Name parent ParentGroup description Description/ );
25
26 my @col_display_order = ( qw/id name parent description/ );
27
28 if (my $action = $cgi->param('action')) {
29         if ( $action eq 'Update' ) {
30                 for my $id ( ($cgi->param('id')) ) {
31                         my $u = permission::grp_tree->retrieve($id);
32                         for my $col ( keys %org_cols ) {
33                                 next if ($cgi->param($col."_$id") =~ /Select One/o);
34                                 $u->$col( $cgi->param($col."_$id") );
35                         }
36                         $u->update;
37                 }
38         } elsif ( $action eq 'Set Permissions' ) {
39                 my $grp = permission::grp_tree->retrieve($cgi->param('perms'));
40                 my @ids = $cgi->param('permission');
41                 for my $perm ( permission::perm_list->retrieve_all ) {
42                         if (my $id = $cgi->param('permission_'.$perm->id) ) {
43                                 my $p = permission::grp_perm_map->search({perm=>$id,grp=>$grp->id})->next;
44                                 my $d = $cgi->param("depth_$id");
45                                 if (!$p) {
46                                         $p = permission::grp_perm_map->create({perm=>$id,grp=>$grp->id,depth=>$d});
47                                 } else {
48                                         $p->depth( $d );
49                                 }
50                                 $p->update;
51                         } else {
52                                 permission::grp_perm_map->search({perm=>$perm->id,grp=>$grp->id})->delete_all;
53                         }
54                 }
55                 $cgi->param('action'=>'child');
56         } elsif ( $action eq 'Add New' ) {
57                 permission::grp_tree->create( { map { defined($cgi->param($_)) ? ($_ => $cgi->param($_)) : () } keys %org_cols } );
58         }
59 }
60
61 #-------------------------------------------------------------------------------
62 # HTML part
63 #-------------------------------------------------------------------------------
64
65 print <<HEADER;
66 Content-type: text/html
67
68 <html>
69
70 <head>
71         <style>
72                 table.table_class {
73                         border: dashed lightgrey 1px;
74                         background-color: #EEE;
75                         border-collapse: collapse;
76                 }
77
78                 deactivated {
79                         color: lightgrey;
80                 }
81
82                 tr.new_row_class {
83                         background: grey;
84                 }
85
86                 tr.row_class td {
87                         border: solid lightgrey 1px;
88                 }
89                 
90                 tr.header_class th {
91                         background-color: lightblue;
92                         border: solid blue 1px;
93                         padding: 2px;
94                 }
95
96 /*--------------------------------------------------|
97 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
98 |---------------------------------------------------|
99 | Copyright (c) 2002-2003 Geir Landrö               |
100 |--------------------------------------------------*/
101
102 .dtree {
103         font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
104         font-size: 11px;
105         color: #666;
106         white-space: nowrap;
107 }
108 .dtree img {
109         border: 0px;
110         vertical-align: middle;
111 }
112 .dtree a {
113         color: #333;
114         text-decoration: none;
115 }
116 .dtree a.node, .dtree a.nodeSel {
117         white-space: nowrap;
118         padding: 1px 2px 1px 2px;
119 }
120 .dtree a.node:hover, .dtree a.nodeSel:hover {
121         color: #333;
122         text-decoration: underline;
123 }
124 .dtree a.nodeSel {
125         background-color: #c0d2ec;
126 }
127 .dtree .clip {
128         overflow: hidden;
129 }
130
131
132         </style>
133         <script language='javascript' src='support/dtree.js'></script>
134 </head>
135
136 <body style='padding: 25px;'>
137
138 <a href="$config{index}">Home</a>
139
140 <h1>User Group Hierarchy Setup</h1>
141 <hr/>
142 HEADER
143
144 my $uri = $cgi->url(-relative=>1);
145
146 my $top;
147 for my $grp ( permission::grp_tree->search( {parent=>undef} ) ) {
148         my $name = $grp->name;
149         my $desc = $grp->description || $grp->name;
150         $top = $grp->id;
151         $name =~ s/'/\\'/og;
152         print <<"       HEADER";
153 <div style="float: left;">
154         <script language='javascript'>
155         var tree = new dTree("tree");
156         tree.add($grp, -1, "$name", "$uri?action=child&id=$grp", "$desc");
157         HEADER
158         $top = $grp->id;
159         last;
160 }
161
162 for my $grp ( permission::grp_tree->search_like( {parent => '%'}, {order_by => 'name'} ) ) {
163         my $name = $grp->name;
164         my $desc = $grp->description || $grp->name;
165         $name =~ s/'/\\'/og;
166         my $parent = $grp->parent;
167         print "\ttree.add($grp, $parent, \"$name\", \"$uri?action=child&id=$grp\", \"$desc\");\n";
168 }
169
170 print <<HEADER;
171         tree.closeAllChildren($top);
172         document.write(tree.toString());
173         </script>
174 </div>
175 <div style="float:right; width:50%;">
176 HEADER
177
178 #-------------------------------------------------------------------------------
179 # Logic part
180 #-------------------------------------------------------------------------------
181
182 if (my $action = $cgi->param('action')) {
183         if ( $action eq 'child' ) {
184                 my $id = $cgi->param('id');
185                 if ($id) {
186                         my $node = permission::grp_tree->retrieve($id);
187                         #-----------------------------------------------------------------------
188                         # child form
189                         #-----------------------------------------------------------------------
190
191                         print "<h2>Edit Group '".$node->name."'</h2>";
192                         print   "<form method='POST'>".
193                                 "<table class='table_class'><tr class='header_class'>\n";
194         
195                         print Tr(
196                                 th($org_cols{id}),
197                                 td( $node->id() ),
198                         );
199                         print Tr(
200                                 th($org_cols{name}),
201                                 td("<input type='text' name='name_$node' value=\"". $node->name() ."\">"),
202                         );
203                         print Tr(
204                                 th($org_cols{parent}),
205                                 td("<select name='parent_$node'>".do{
206                                                 my $out = '<option>-- Select One --</option>';
207                                                 for my $org ( sort {$a->id <=> $b->id} permission::grp_tree->retrieve_all) {
208                                                         $out .= "<option value='$org' ".do {
209                                                                 if ($node->parent == $org->id) {
210                                                                         "selected";
211                                                                 }}.'>'.$org->name.'</option>'
212                                                 }
213                                                 $out;
214                                         }."</select><input type='hidden' value='$node' name='id'>"),
215                         );
216                         print Tr(
217                                 th($org_cols{description}),
218                                 td("<input type='text' name='description_$node' value=\"". $node->description() ."\">"),
219                         );
220
221                         print Tr( "<td colspan='2'><input type='submit' name='action' value='Update'/></td>" );
222
223                         print   "</table></form><hr/>";
224
225
226                         print "<h2>Group Permissions</h2>";
227
228                         print   "<form method='POST'>".
229                                 "<table class='table_class'>\n".
230                                 "<tr class='header_class'><th>Permission</th><th>Select</th><th>At Depth</th></tr>";
231
232                         for my $perm ( sort {$a->code cmp $b->code} permission::perm_list->retrieve_all ) {
233                                 my $grp = $node;
234                                 my $out = '<select name="depth_'.$perm->id.'"><option value="">-- Select One --</option>';
235                                 for my $outype ( actor::org_unit_type->retrieve_all ) {
236                                         my $grp = $node;
237                                         $out .= "<option value='".$outype->depth."' ".do{
238                                                 my $stuff = '';
239                                                 do {
240                                                         if ($grp) {
241                                                                 my $setting = permission::grp_perm_map->search(
242                                                                                 { grp  => $grp->id,
243                                                                                   perm => $perm->id }
244                                                                 )->next;
245                                                                 $stuff = "selected " if($setting && $outype->depth == $setting->depth);
246                                                                 if($stuff && $setting && $setting->grp != $node->id) {
247                                                                         $out =~ s/^<select/<select disabled/o;
248                                                                 }
249                                                         }
250                                                 } while (!$stuff && $grp && ($grp = $grp->parent));
251                                                 $stuff;
252                                         }.">".$outype->name."</option>";
253                                 }
254                                 $out .= "</select>";
255                                 $grp = $node;
256                                 print Tr( "<td>".$perm->code."</td><td>".
257                                           "<input type='checkbox' name='permission_$perm' value='$perm' ".
258                                           do{
259                                                 my $stuff = '';
260                                                 do {
261                                                         if ($grp) {
262                                                                 my $setting = permission::grp_perm_map->search(
263                                                                                 { grp  => $grp->id,
264                                                                                   perm => $perm->id }
265                                                                 )->next;
266                                                                 $stuff = "checked " if ($setting);
267                                                                 $stuff .= "disabled " if($setting && $setting->grp != $node->id);
268                                                         }
269                                                 } while (!$stuff && $grp && ($grp = $grp->parent));
270                                                 $stuff;
271                                           }.
272                                           "></td><td>$out</td>"
273
274                                 );
275                         }
276                         
277                         print Tr( "<td colspan='3'><input type='hidden' value='$node' name='perms'>",
278                                   "<input type='submit' name='action' value='Set Permissions'/></td>" );
279                         print   "</table></form><hr/>";
280
281
282
283                         print "<h2>New Child</h2>";
284         
285                         print   "<form method='POST'>".
286                                 "<table class='table_class'>\n";
287
288                         print Tr(
289                                 th($org_cols{name}),
290                                 td("<input type='text' name='name'>"),
291                         );
292                         print Tr(
293                                 th($org_cols{description}),
294                                 td("<input type='text' name='description'>"),
295                         );
296                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent'>",
297                                   "<input type='submit' name='action' value='Add New'/></td>" );
298                         print   "</table></form><hr/>";
299
300                 }
301         }
302 }
303         
304 print "</div></body></html>";
305
306