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