]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/cgi-bin/lib-setup.cgi
updating header and fixing some minor input bugs
[Evergreen.git] / Open-ILS / src / cgi-bin / lib-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 our %config;
12 do '../setup.pl';
13
14 OpenILS::Application::Storage::CDBI->connection($config{dsn},$config{usr});
15 OpenILS::Application::Storage::CDBI->db_Main->{ AutoCommit } = 1;
16
17 my $cgi = new CGI;
18
19 #-------------------------------------------------------------------------------
20 # setup part
21 #-------------------------------------------------------------------------------
22
23 my %org_cols = ( qw/id SysID name Name parent_ou Parent ou_type OrgUnitType shortname ShortName/ );
24
25 my @col_display_order = ( qw/id name shortname ou_type parent_ou/ );
26
27 if (my $action = $cgi->param('action')) {
28         if ( $action eq 'Update' ) {
29                 for my $id ( ($cgi->param('id')) ) {
30                         my $u = actor::org_unit->retrieve($id);
31                         for my $col ( keys %org_cols ) {
32                                 next if ($cgi->param($col."_$id") =~ /Select One/o);
33                                 $u->$col( $cgi->param($col."_$id") );
34                         }
35                         $u->update;
36                 }
37         } elsif ( $action eq 'Add New' ) {
38                 actor::org_unit->create( { map { defined($cgi->param($_)) ? ($_ => $cgi->param($_)) : () } keys %org_cols } );
39         }
40 }
41
42 #-------------------------------------------------------------------------------
43 # HTML part
44 #-------------------------------------------------------------------------------
45
46 print <<HEADER;
47 Content-type: text/html
48
49 <html>
50
51 <head>
52         <style>
53                 table.table_class {
54                         border: dashed lightgrey 1px;
55                         background-color: #EEE;
56                         border-collapse: collapse;
57                 }
58
59                 deactivated {
60                         color: lightgrey;
61                 }
62
63                 tr.new_row_class {
64                         background: grey;
65                 }
66
67                 tr.row_class td {
68                         border: solid lightgrey 1px;
69                 }
70                 
71                 tr.header_class th {
72                         background-color: lightblue;
73                         border: solid blue 1px;
74                         padding: 2px;
75                 }
76
77         </style>
78         <script language='javascript' src='/js/widgets/xtree.js'></script>
79 </head>
80
81 <body style='padding: 25px;'>
82
83 <a href="$config{index}">Home</a>
84
85 <h1>Library Hierarchy Setup</h1>
86 <hr/>
87 HEADER
88
89 my $uri = $cgi->url(-relative=>1);
90
91 my $top;
92 for my $lib ( actor::org_unit->search( {parent_ou=>undef} ) ) {
93         my $name = $lib->name;
94         $name =~ s/'/\\'/og;
95         print <<"       HEADER";
96 <div style="float: left;">
97         <script language='javascript'>
98
99                 function getById (id) { return document.getElementById(id); }
100                 function createAppElement (el) { return document.createElement(el); }
101                 function createAppTextNode (txt) { return document.createTextNode(txt); }
102         
103                 var node_$lib = new WebFXTree('$name','$uri?action=child&id=$lib');
104         HEADER
105         $top = $lib->id;
106         last;
107 }
108
109 for my $lib ( actor::org_unit->search_like( {parent_ou => '%'}, {order_by => 'id'} ) ) {
110         my $name = $lib->name;
111         $name =~ s/'/\\'/og;
112         my $parent = $lib->parent_ou;
113         print <<"       JS"
114                 var node_$lib = new WebFXTreeItem('$name','$uri?action=child&id=$lib');
115         JS
116 }for my $lib ( sort {$a->name cmp $b->name} actor::org_unit->retrieve_all ) {
117         my $parent = $lib->parent_ou;
118         next unless $parent;
119         print <<"       JS"
120                 node_$parent.add(node_$lib);
121         JS
122 }
123
124 print <<HEADER;
125                 document.write(node_$top);
126         </script>
127 </div>
128 <div>
129
130 HEADER
131
132 #-------------------------------------------------------------------------------
133 # Logic part
134 #-------------------------------------------------------------------------------
135
136 if (my $action = $cgi->param('action')) {
137         if ( $action eq 'child' ) {
138                 my $id = $cgi->param('id');
139                 if ($id) {
140                         my $node = actor::org_unit->retrieve($id);
141                         #-----------------------------------------------------------------------
142                         # child form
143                         #-----------------------------------------------------------------------
144
145                         print "<h2>Edit ".$node->name."</h2>";
146                         print   "<form method='POST'>".
147                                 "<table class='table_class'><tr class='header_class'>\n";
148         
149                         print Tr(
150                                 th($org_cols{id}),
151                                 td( $node->id() ),
152                         );
153                         print Tr(
154                                 th($org_cols{name}),
155                                 td("<input type='text' name='name_$node' value=\"". $node->name() ."\">"),
156                         );
157                         print Tr(
158                                 th($org_cols{shortname}),
159                                 td("<input type='text' name='shortname_$node' value='". $node->shortname() ."'>"),
160                         );
161                         print Tr(
162                                 th($org_cols{ou_type}),
163                                 td("<select name='ou_type_$node'>".do{
164                                                         my $out = '<option>-- Select One --</option>';
165                                                         for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
166                                                                 $out .= "<option value='$type' ".do {
167                                                                         if ($node->ou_type == $type->id) {
168                                                                                 "selected";
169                                                                         }}.'>'.$type->name.'</option>'
170                                                         }
171                                                         $out;
172                                                 }."</select>"),
173                         );
174                         print Tr(
175                                 th($org_cols{parent_ou}),
176                                 td("<select name='parent_ou_$node'>".do{
177                                                 my $out = '<option>-- Select One --</option>';
178                                                 for my $org ( sort {$a->id <=> $b->id} actor::org_unit->retrieve_all) {
179                                                         $out .= "<option value='$org' ".do {
180                                                                 if ($node->parent_ou == $org->id) {
181                                                                         "selected";
182                                                                 }}.'>'.do{'&nbsp;&nbsp;'x$org->ou_type->depth}.$org->name.'</option>'
183                                                 }
184                                                 $out;
185                                         }."</select><input type='hidden' value='$node' name='id'>"),
186                         );
187
188                         print Tr( "<td colspan='2'><input type='submit' name='action' value='Update'/></td>" );
189
190                         print   "</table></form><hr/>";
191
192
193                         print "<h2>New Child</h2>";
194         
195                         print   "<form method='POST'>".
196                                 "<table class='table_class'>\n";
197
198                         print Tr(
199                                 th($org_cols{name}),
200                                 td("<input type='text' name='name'>"),
201                         );
202                         print Tr(
203                                 th($org_cols{shortname}),
204                                 td("<input type='text' name='shortname'>"),
205                         );
206                         print Tr(
207                                 th($org_cols{ou_type}),
208                                 td("<select name='ou_type'>".do{
209                                                 my $out = '<option>-- Select One --</option>';
210                                                 for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
211                                                         $out .= "<option value='$type'>".$type->name.'</option>'
212                                                 }
213                                                 $out;
214                                         }."</select>"),
215                         );
216                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent_ou'>",
217                                   "<input type='submit' name='action' value='Add New'/></td>" );
218                         print   "</table></form><hr/>";
219                 }
220         }
221 }
222         
223 print "</div></body></html>";
224
225