libraries are now filtered on id (there are duplicate names, such as "Bookmobile")
[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 '##CONFIG##/live-db-setup.pl';
13
14 OpenILS::Application::Storage::CDBI->connection($config{dsn},$config{usr},$config{pw});
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         } elsif ( $action eq 'Save Address' ) {
40                 my $org = actor::org_unit->retrieve($cgi->param('id'));
41
42                 my $addr = {};
43
44                 $$addr{org_unit} = $cgi->param('org_unit') || $org->id;
45                 $$addr{street1} = $cgi->param('street1');
46                 $$addr{street2} = $cgi->param('street2');
47                 $$addr{city} = $cgi->param('city');
48                 $$addr{county} = $cgi->param('county');
49                 $$addr{state} = $cgi->param('state');
50                 $$addr{country} = $cgi->param('country');
51                 $$addr{post_code} = $cgi->param('post_code');
52
53                 my $a_type = $cgi->param('addr_type');
54
55
56                 my $a = actor::org_address->retrieve($cgi->param('aid'));
57
58                 if ($a) {
59                         for (keys %$addr) {
60                                 next unless $$addr{$_};
61                                 $a->$_($$addr{$_});
62                         }
63                         $a->update;
64                 } else {
65                         $a = actor::org_address->create( {map {defined($$addr{$_}) ? ($_ => $$addr{$_}) : ()} keys %$addr} );
66                 }
67
68                 $org->$a_type($a->id);
69                 $org->update;
70         }
71 }
72
73 #-------------------------------------------------------------------------------
74 # HTML part
75 #-------------------------------------------------------------------------------
76
77 print <<HEADER;
78 Content-type: text/html
79
80 <html>
81
82 <head>
83         <style>
84                 table.table_class {
85                         border: dashed lightgrey 1px;
86                         background-color: #EEE;
87                         border-collapse: collapse;
88                 }
89
90                 deactivated {
91                         color: lightgrey;
92                 }
93
94                 tr.new_row_class {
95                         background: grey;
96                 }
97
98                 tr.row_class td {
99                         border: solid lightgrey 1px;
100                 }
101                 
102                 tr.header_class th {
103                         background-color: lightblue;
104                         border: solid blue 1px;
105                         padding: 2px;
106                 }
107
108
109 /*--------------------------------------------------|
110 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
111 |---------------------------------------------------|
112 | Copyright (c) 2002-2003 Geir Landrö               |
113 |--------------------------------------------------*/
114
115 .dtree {
116         font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
117         font-size: 11px;
118         color: #666;
119         white-space: nowrap;
120 }
121 .dtree img {
122         border: 0px;
123         vertical-align: middle;
124 }
125 .dtree a {
126         color: #333;
127         text-decoration: none;
128 }
129 .dtree a.node, .dtree a.nodeSel {
130         white-space: nowrap;
131         padding: 1px 2px 1px 2px;
132 }
133 .dtree a.node:hover, .dtree a.nodeSel:hover {
134         color: #333;
135         text-decoration: underline;
136 }
137 .dtree a.nodeSel {
138         background-color: #c0d2ec;
139 }
140 .dtree .clip {
141         overflow: hidden;
142 }
143
144
145         </style>
146         <script language='javascript' src='support/dtree.js'></script>
147 </head>
148
149 <body style='padding: 25px;'>
150
151 <a href="$config{index}">Home</a>
152
153 <h1>Library Hierarchy Setup</h1>
154 <hr/>
155 HEADER
156
157 my $uri = $cgi->url(-relative=>1);
158
159 my $top;
160 for my $lib ( actor::org_unit->search( {parent_ou=>undef} ) ) {
161         my $name = $lib->name;
162         $name =~ s/'/\\'/og;
163         $top = $lib->id;
164         print <<"       HEADER";
165 <div style="float: left;">
166         <script language='javascript'>
167         var tree = new dTree("tree");
168         tree.add($lib, -1, "$name", "$uri?action=child&id=$lib", "$name");
169         HEADER
170         $top = $lib->id;
171         last;
172 }
173
174 for my $lib ( actor::org_unit->search_like( {parent_ou => '%'}, {order_by => 'name'} ) ) {
175         my $name = $lib->name;
176         $name =~ s/'/\\'/og;
177         my $parent = $lib->parent_ou;
178         print "\ttree.add($lib, $parent, \"$name\", \"$uri?action=child&id=$lib\", \"$name\");\n";
179 }
180
181 print <<HEADER;
182                 tree.closeAllChildren($top);
183                 document.write(tree.toString());
184         </script>
185 </div>
186 <div>
187
188 HEADER
189
190 #-------------------------------------------------------------------------------
191 # Logic part
192 #-------------------------------------------------------------------------------
193
194 if (my $action = $cgi->param('action')) {
195         if ( $action eq 'child' ) {
196                 my $id = $cgi->param('id');
197                 if ($id) {
198                         my $node = actor::org_unit->retrieve($id);
199                         #-----------------------------------------------------------------------
200                         # child form
201                         #-----------------------------------------------------------------------
202
203                         print "<h2>Edit ".$node->name."</h2>";
204                         print   "<form method='POST'>".
205                                 "<table class='table_class'><tr class='header_class'>\n";
206         
207                         print Tr(
208                                 th($org_cols{id}),
209                                 td( $node->id() ),
210                         );
211                         print Tr(
212                                 th($org_cols{name}),
213                                 td("<input type='text' name='name_$node' value=\"". $node->name() ."\">"),
214                         );
215                         print Tr(
216                                 th($org_cols{shortname}),
217                                 td("<input type='text' name='shortname_$node' value='". $node->shortname() ."'>"),
218                         );
219                         print Tr(
220                                 th($org_cols{ou_type}),
221                                 td("<select name='ou_type_$node'>".do{
222                                                         my $out = '<option>-- Select One --</option>';
223                                                         for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
224                                                                 $out .= "<option value='$type' ".do {
225                                                                         if ($node->ou_type == $type->id) {
226                                                                                 "selected";
227                                                                         }}.'>'.$type->name.'</option>'
228                                                         }
229                                                         $out;
230                                                 }."</select>"),
231                         );
232                         print Tr(
233                                 th($org_cols{parent_ou}),
234                                 td("<select name='parent_ou_$node'>".do{
235                                                 my $out = '<option>-- Select One --</option>';
236                                                 for my $org ( sort {$a->id <=> $b->id} actor::org_unit->retrieve_all) {
237                                                         $out .= "<option value='$org' ".do {
238                                                                 if ($node->parent_ou == $org->id) {
239                                                                         "selected";
240                                                                 }}.'>'.do{'&nbsp;&nbsp;'x$org->ou_type->depth}.$org->name.'</option>'
241                                                 }
242                                                 $out;
243                                         }."</select><input type='hidden' value='$node' name='id'>"),
244                         );
245
246                         print Tr( "<td colspan='2'><input type='submit' name='action' value='Update'/></td>" );
247
248                         print   "</table></form><hr/><table cellspacing='20'><tr>";
249
250
251                         #-------------------------------------------------------------------------
252                         # Address edit form
253                         #-------------------------------------------------------------------------
254
255                         my %addrs = (   ill_address     => 'ILL Address',
256                                         holds_address   => 'Consortial Holds Address',
257                                         mailing_address => 'Mailing Address',
258                                         billing_address => 'Physical Address'
259                         );
260                         for my $a (qw/billing_address mailing_address holds_address ill_address/) {
261                                 my $addr = actor::org_address->retrieve( $node->$a ) if ($node->$a);
262
263                                 my %ah = (      street1         => $addr?$addr->street1:'',
264                                                 street2         => $addr?$addr->street2:'',
265                                                 city            => $addr?$addr->city:'',
266                                                 county          => $addr?$addr->county:'',
267                                                 state           => $addr?$addr->state:'',
268                                                 country         => $addr?$addr->country:'US',
269                                                 post_code       => $addr?$addr->post_code:'',
270                                                 org_unit        => $addr?$addr->org_unit:$node->id,
271                                                 id              => $addr?$addr->id:'',
272                                 );
273
274                                 print '</tr><tr>' if ($a eq 'holds_address');
275                                 print <<"                               TABLE";
276
277 <td>
278 <form method='POST'>
279 <table class='table_class'>
280         <tr>
281                 <th colspan=2>$addrs{$a}</th>
282         </tr>
283         <tr>
284                 <th>SysID</th>
285                 <td><input type='text' name='aid' value='$ah{id}'></td>
286         </tr>
287         <tr>
288                 <th>*Street 1</th>
289                 <td><input type='text' name='street1' value='$ah{street1}'></td>
290         </tr>
291         <tr>
292                 <th>Street 2</th>
293                 <td><input type='text' name='street2' value='$ah{street2}'></td>
294         </tr>
295         <tr>
296                 <th>*City</th>
297                 <td><input type='text' name='city' value='$ah{city}'></td>
298         </tr>
299         <tr>
300                 <th>County</th>
301                 <td><input type='text' name='county' value='$ah{county}'></td>
302         </tr>
303         <tr>
304                 <th>*State</th>
305                 <td><input type='text' name='state' value='$ah{state}'></td>
306         </tr>
307         <tr>
308                 <th>*Country</th>
309                 <td><input type='text' name='country' value='$ah{country}'></td>
310         </tr>
311         <tr>
312                 <th>*ZIP</th>
313                 <td><input type='text' name='post_code' value='$ah{post_code}'></td>
314         </tr>
315 </table>
316 <input type='hidden' name='org_unit' value='$ah{org_unit}'>
317 <input type='hidden' name='addr_type' value='$a'>
318 <input type='hidden' name='id' value='$node'>
319 <input type='submit' name='action' value='Save Address'>
320 </form></td>
321
322                                 TABLE
323                         }
324
325                         print "<tr></table><hr><h2>New Child</h2>";
326         
327                         print   "<form method='POST'>".
328                                 "<table class='table_class'>\n";
329
330                         print Tr(
331                                 th($org_cols{name}),
332                                 td("<input type='text' name='name'>"),
333                         );
334                         print Tr(
335                                 th($org_cols{shortname}),
336                                 td("<input type='text' name='shortname'>"),
337                         );
338                         print Tr(
339                                 th($org_cols{ou_type}),
340                                 td("<select name='ou_type'>".do{
341                                                 my $out = '<option>-- Select One --</option>';
342                                                 for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
343                                                         $out .= "<option value='$type'>".$type->name.'</option>'
344                                                 }
345                                                 $out;
346                                         }."</select>"),
347                         );
348                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent_ou'>",
349                                   "<input type='submit' name='action' value='Add New'/></td>" );
350                         print   "</table></form><hr/>";
351                 }
352         }
353 }
354         
355 print "</div></body></html>";
356
357