adding description column
[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 '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         </style>
97         <script language='javascript' src='/js/widgets/xtree.js'></script>
98 </head>
99
100 <body style='padding: 25px;'>
101
102 <a href="$config{index}">Home</a>
103
104 <h1>User Group Hierarchy Setup</h1>
105 <hr/>
106 HEADER
107
108 my $uri = $cgi->url(-relative=>1);
109
110 my $top;
111 for my $grp ( permission::grp_tree->search( {parent=>undef} ) ) {
112         my $name = $grp->name;
113         $name =~ s/'/\\'/og;
114         print <<"       HEADER";
115 <div style="float: left;">
116         <script language='javascript'>
117
118                 function getById (id) { return document.getElementById(id); }
119                 function createAppElement (el) { return document.createElement(el); }
120                 function createAppTextNode (txt) { return document.createTextNode(txt); }
121         
122                 var node_$grp = new WebFXTree('$name','$uri?action=child&id=$grp');
123         HEADER
124         $top = $grp->id;
125         last;
126 }
127
128 for my $grp ( permission::grp_tree->search_like( {parent => '%'}, {order_by => 'id'} ) ) {
129         my $name = $grp->name;
130         $name =~ s/'/\\'/og;
131         my $parent = $grp->parent;
132         print <<"       JS"
133                 var node_$grp = new WebFXTreeItem('$name','$uri?action=child&id=$grp');
134         JS
135 }
136
137 for my $grp ( sort {$a->name cmp $b->name} permission::grp_tree->retrieve_all ) {
138         my $parent = $grp->parent;
139         next unless $parent;
140         print <<"       JS"
141                 node_$parent.add(node_$grp);
142         JS
143 }
144
145 print <<HEADER;
146                 document.write(node_$top);
147         </script>
148 </div>
149 <div style="float:right; width:50%;">
150
151 HEADER
152
153 #-------------------------------------------------------------------------------
154 # Logic part
155 #-------------------------------------------------------------------------------
156
157 if (my $action = $cgi->param('action')) {
158         if ( $action eq 'child' ) {
159                 my $id = $cgi->param('id');
160                 if ($id) {
161                         my $node = permission::grp_tree->retrieve($id);
162                         #-----------------------------------------------------------------------
163                         # child form
164                         #-----------------------------------------------------------------------
165
166                         print "<h2>Edit Group '".$node->name."'</h2>";
167                         print   "<form method='POST'>".
168                                 "<table class='table_class'><tr class='header_class'>\n";
169         
170                         print Tr(
171                                 th($org_cols{id}),
172                                 td( $node->id() ),
173                         );
174                         print Tr(
175                                 th($org_cols{name}),
176                                 td("<input type='text' name='name_$node' value=\"". $node->name() ."\">"),
177                         );
178                         print Tr(
179                                 th($org_cols{parent}),
180                                 td("<select name='parent_$node'>".do{
181                                                 my $out = '<option>-- Select One --</option>';
182                                                 for my $org ( sort {$a->id <=> $b->id} permission::grp_tree->retrieve_all) {
183                                                         $out .= "<option value='$org' ".do {
184                                                                 if ($node->parent == $org->id) {
185                                                                         "selected";
186                                                                 }}.'>'.$org->name.'</option>'
187                                                 }
188                                                 $out;
189                                         }."</select><input type='hidden' value='$node' name='id'>"),
190                         );
191                         print Tr(
192                                 th($org_cols{description}),
193                                 td("<input type='text' name='description_$node' value=\"". $node->description() ."\">"),
194                         );
195
196                         print Tr( "<td colspan='2'><input type='submit' name='action' value='Update'/></td>" );
197
198                         print   "</table></form><hr/>";
199
200
201                         print "<h2>Group Permissions</h2>";
202
203                         print   "<form method='POST'>".
204                                 "<table class='table_class'>\n".
205                                 "<tr class='header_class'><th>Permission</th><th>Select</th><th>At Depth</th></tr>";
206
207                         for my $perm ( sort {$a->code cmp $b->code} permission::perm_list->retrieve_all ) {
208                                 my $grp = $node;
209                                 my $out = '<select name="depth_'.$perm->id.'"><option value="">-- Select One --</option>';
210                                 for my $outype ( actor::org_unit_type->retrieve_all ) {
211                                         my $grp = $node;
212                                         $out .= "<option value='".$outype->depth."' ".do{
213                                                 my $stuff = '';
214                                                 do {
215                                                         if ($grp) {
216                                                                 my $setting = permission::grp_perm_map->search(
217                                                                                 { grp  => $grp->id,
218                                                                                   perm => $perm->id }
219                                                                 )->next;
220                                                                 $stuff = "selected " if($setting && $outype->depth == $setting->depth);
221                                                                 if($stuff && $setting && $setting->grp != $node->id) {
222                                                                         $out =~ s/^<select/<select disabled/o;
223                                                                 }
224                                                         }
225                                                 } while (!$stuff && $grp && ($grp = $grp->parent));
226                                                 $stuff;
227                                         }.">".$outype->name."</option>";
228                                 }
229                                 $out .= "</select>";
230                                 $grp = $node;
231                                 print Tr( "<td>".$perm->code."</td><td>".
232                                           "<input type='checkbox' name='permission_$perm' value='$perm' ".
233                                           do{
234                                                 my $stuff = '';
235                                                 do {
236                                                         if ($grp) {
237                                                                 my $setting = permission::grp_perm_map->search(
238                                                                                 { grp  => $grp->id,
239                                                                                   perm => $perm->id }
240                                                                 )->next;
241                                                                 $stuff = "checked " if ($setting);
242                                                                 $stuff .= "disabled " if($setting && $setting->grp != $node->id);
243                                                         }
244                                                 } while (!$stuff && $grp && ($grp = $grp->parent));
245                                                 $stuff;
246                                           }.
247                                           "></td><td>$out</td>"
248
249                                 );
250                         }
251                         
252                         print Tr( "<td colspan='3'><input type='hidden' value='$node' name='perms'>",
253                                   "<input type='submit' name='action' value='Set Permissions'/></td>" );
254                         print   "</table></form><hr/>";
255
256
257
258                         print "<h2>New Child</h2>";
259         
260                         print   "<form method='POST'>".
261                                 "<table class='table_class'>\n";
262
263                         print Tr(
264                                 th($org_cols{name}),
265                                 td("<input type='text' name='name'>"),
266                         );
267                         print Tr(
268                                 th($org_cols{description}),
269                                 td("<input type='text' name='description'>"),
270                         );
271                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent'>",
272                                   "<input type='submit' name='action' value='Add New'/></td>" );
273                         print   "</table></form><hr/>";
274
275                 }
276         }
277 }
278         
279 print "</div></body></html>";
280
281