]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/cgi-bin/lib-setup.cgi
just belt-and-suspenders on the bool matching
[Evergreen.git] / Open-ILS / src / cgi-bin / lib-setup.cgi
1 #!/usr/bin/perl
2 use strict;
3
4 use DateTime;
5 use OpenILS::Application::Storage;
6 use OpenILS::Application::Storage::CDBI;
7
8 # I need to abstract the driver loading away...
9 use OpenILS::Application::Storage::Driver::Pg;
10
11 use CGI qw/:standard start_*/;
12 our %config;
13 do '##CONFIG##/live-db-setup.pl';
14 #do '/openils/conf/live-db-setup.pl';
15
16 OpenILS::Application::Storage::CDBI->connection($config{dsn},$config{usr},$config{pw});
17 OpenILS::Application::Storage::CDBI->db_Main->{ AutoCommit } = 1;
18
19
20 my $cgi = new CGI;
21
22 #-------------------------------------------------------------------------------
23 # setup part
24 #-------------------------------------------------------------------------------
25
26 my %org_cols = ( qw/id SysID name Name parent_ou Parent ou_type OrgUnitType shortname ShortName email Email phone Phone opac_visible OPACVisible/ );
27
28 my @col_display_order = ( qw/id name shortname ou_type email phone opac_visible parent_ou/ );
29
30 if (my $action = $cgi->param('action')) {
31         if ( $action eq 'Update' ) {
32                 for my $id ( ($cgi->param('id')) ) {
33                         my $u = actor::org_unit->retrieve($id);
34                         for my $col ( keys %org_cols ) {
35                                 next if ($cgi->param($col."_$id") =~ /Select One/o);
36                                 if ($col eq 'shortname') {
37                                         $u->$col( uc( $cgi->param($col."_$id") ) );
38                                 } else {
39                                         $u->$col( $cgi->param($col."_$id") );
40                                 }
41                         }
42                         $u->update;
43                 }
44         } elsif ( $action eq 'Update Hours' ) {
45                 for my $id ( ($cgi->param('id')) ) {
46                         my $hoo = actor::org_unit::hours_of_operation->retrieve($id);
47                         for my $col ( $hoo->columns('Essential') ) {
48                                 $hoo->$col( $cgi->param($col) );
49                         }
50                         $hoo->update;
51                 }
52         } elsif ( $action eq 'Add New' ) {
53                 actor::org_unit->create( { map { defined($cgi->param($_)) ? ($_ => $cgi->param($_)) : () } keys %org_cols } );
54         } elsif ( $action eq 'Save Address' ) {
55                 my $org = actor::org_unit->retrieve($cgi->param('id'));
56
57                 my $addr = {};
58
59                 $$addr{org_unit} = $cgi->param('org_unit') || $org->id;
60                 $$addr{street1} = $cgi->param('street1');
61                 $$addr{street2} = $cgi->param('street2');
62                 $$addr{city} = $cgi->param('city');
63                 $$addr{county} = $cgi->param('county');
64                 $$addr{state} = $cgi->param('state');
65                 $$addr{country} = $cgi->param('country');
66                 $$addr{post_code} = $cgi->param('post_code');
67
68                 my $a_type = $cgi->param('addr_type');
69
70
71                 my $a = actor::org_address->retrieve($cgi->param('aid'));
72
73                 if ($a) {
74                         for (keys %$addr) {
75                                 next unless $$addr{$_};
76                                 $a->$_($$addr{$_});
77                         }
78                         $a->update;
79                 } else {
80                         $a = actor::org_address->create( {map {defined($$addr{$_}) ? ($_ => $$addr{$_}) : ()} keys %$addr} );
81                 }
82
83                 $org->$a_type($a->id);
84                 $org->update;
85         }
86 }
87
88 #-------------------------------------------------------------------------------
89 # HTML part
90 #-------------------------------------------------------------------------------
91
92 print <<HEADER;
93 Content-type: text/html
94
95 <html>
96
97 <head>
98         <style>
99                 table.table_class {
100                         border: dashed lightgrey 1px;
101                         background-color: #EEE;
102                         border-collapse: collapse;
103                 }
104
105                 deactivated {
106                         color: lightgrey;
107                 }
108
109                 tr.new_row_class {
110                         background: grey;
111                 }
112
113                 tr.row_class td {
114                         border: solid lightgrey 1px;
115                 }
116                 
117                 tr.header_class th {
118                         background-color: lightblue;
119                         border: solid blue 1px;
120                         padding: 2px;
121                 }
122
123
124 /*--------------------------------------------------|
125 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
126 |---------------------------------------------------|
127 | Copyright (c) 2002-2003 Geir Landrö               |
128 |--------------------------------------------------*/
129
130 .dtree {
131         font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
132         font-size: 11px;
133         color: #666;
134         white-space: nowrap;
135 }
136 .dtree img {
137         border: 0px;
138         vertical-align: middle;
139 }
140 .dtree a {
141         color: #333;
142         text-decoration: none;
143 }
144 .dtree a.node, .dtree a.nodeSel {
145         white-space: nowrap;
146         padding: 1px 2px 1px 2px;
147 }
148 .dtree a.node:hover, .dtree a.nodeSel:hover {
149         color: #333;
150         text-decoration: underline;
151 }
152 .dtree a.nodeSel {
153         background-color: #c0d2ec;
154 }
155 .dtree .clip {
156         overflow: hidden;
157 }
158
159
160         </style>
161         <script language='javascript' src='support/dtree.js'></script>
162 </head>
163
164 <body style='padding: 25px;'>
165
166 <a href="$config{index}">Home</a>
167
168 <h1>Library Hierarchy Setup</h1>
169 <hr/>
170 HEADER
171
172 my $uri = $cgi->url(-relative=>1);
173
174 my $top;
175 for my $lib ( actor::org_unit->search( {parent_ou=>undef} ) ) {
176         my $name = $lib->name;
177         $name =~ s/'/\\'/og;
178         $top = $lib->id;
179         print <<"       HEADER";
180 <div>
181         <script language='javascript'>
182         var tree = new dTree("tree");
183         tree.add($lib, -1, "$name", "$uri?action=child&id=$lib", "$name");
184         HEADER
185         $top = $lib->id;
186         last;
187 }
188
189 for my $lib ( actor::org_unit->search_like( {parent_ou => '%'}, {order_by => 'name'} ) ) {
190         my $name = $lib->name;
191         $name =~ s/'/\\'/og;
192         my $parent = $lib->parent_ou;
193         print "\ttree.add($lib, $parent, \"$name\", \"$uri?action=child&id=$lib\", \"$name\");\n";
194 }
195
196 print <<HEADER;
197                 tree.closeAllChildren($top);
198                 document.write(tree.toString());
199         </script>
200 </div>
201 <div>
202
203 HEADER
204
205 #-------------------------------------------------------------------------------
206 # Logic part
207 #-------------------------------------------------------------------------------
208
209 if (my $action = $cgi->param('action')) {
210         if ( $action eq 'child' ) {
211                 my $id = $cgi->param('id');
212                 if ($id) {
213                         my $node = actor::org_unit->retrieve($id);
214                         #-----------------------------------------------------------------------
215                         # child form
216                         #-----------------------------------------------------------------------
217
218                         print "<hr/><h2>Edit ".$node->name."</h2>";
219                         print   "<form method='POST'>".
220                                 "<table class='table_class'><tr class='header_class'>\n";
221         
222                         print Tr(
223                                 th($org_cols{id}),
224                                 td( $node->id() ),
225                         );
226                         print Tr(
227                                 th($org_cols{name}),
228                                 td("<input type='text' name='name_$node' value=\"". $node->name() ."\">"),
229                         );
230                         print Tr(
231                                 th($org_cols{shortname}),
232                                 td("<input type='text' name='shortname_$node' value='". $node->shortname() ."'>"),
233                         );
234                         print Tr(
235                                 th($org_cols{ou_type}),
236                                 td("<select name='ou_type_$node'>".do{
237                                                         my $out = '<option>-- Select One --</option>';
238                                                         for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
239                                                                 $out .= "<option value='$type' ".do {
240                                                                         if ($node->ou_type == $type->id) {
241                                                                                 "selected";
242                                                                         }}.'>'.$type->name.'</option>'
243                                                         }
244                                                         $out;
245                                                 }."</select>"),
246                         );
247                         print Tr(
248                                 th($org_cols{email}),
249                                 td("<input type='text' name='email_$node' value=\"". $node->email() ."\">"),
250                         );
251
252                         print Tr(
253                                 th($org_cols{phone}),
254                                 td("<input type='text' name='phone_$node' value='". $node->phone() ."'>"),
255                         );
256
257                         print Tr(
258                                 th($org_cols{opac_visible}),
259                                 td("<select name='opac_visible_$node'>".
260                                         do {
261                                                 my $out = "<option value='t' ";
262                                                 $out .= ($node->opac_visible =~ /^[y1t]+/i) ?  "selected='yes'" : "";
263                                                 $out .= ">True</option><option value='f' ";
264                                                 $out .= ($node->opac_visible =~ /^[n0f]+/i) ?  "selected='yes'" : "";
265                                                 $out .= ">False</option>";
266                                                 $out;
267                                         }."</select>"
268                                 ),
269                         );
270
271                         print Tr(
272                                 th($org_cols{parent_ou}),
273                                 td("<select name='parent_ou_$node'>".do{
274                                                 my $out = '<option>-- Select One --</option>';
275                                                 for my $org ( sort {$a->id <=> $b->id} actor::org_unit->retrieve_all) {
276                                                         $out .= "<option value='$org' ".do {
277                                                                 if ($node->parent_ou == $org->id) {
278                                                                         "selected";
279                                                                 }}.'>'.do{'&nbsp;&nbsp;'x$org->ou_type->depth}.$org->name.'</option>'
280                                                 }
281                                                 $out;
282                                         }."</select><input type='hidden' value='$node' name='id'>"),
283                         );
284
285                         print Tr( "<td colspan='2'><input type='submit' name='action' value='Update'/></td>" );
286                         print   "</table></form>";
287
288                         #-------------------------------------------------------------------------
289                         # Hours of operation form
290                         #-------------------------------------------------------------------------
291
292                 
293                         
294                         my %dow = (
295                                 0 => 'Monday',
296                                 1 => 'Tuesday',
297                                 2 => 'Wednesday',
298                                 3 => 'Thursday',
299                                 4 => 'Friday',
300                                 5 => 'Saturday',
301                                 6 => 'Sunday',
302                         );
303                                 
304                         print "<hr/><h2>Hours of Operation for ".$node->name."</h2>".
305                                 "<form method='POST'>".
306                                 "<table class='table_class'><tr class='header_class'>\n";
307
308                         print Tr(
309                                 th('Day of Week'),
310                                 th('Open Time'),
311                                 th('Close Time'),
312                         );
313
314                         my $hoo = actor::org_unit::hours_of_operation->find_or_create( { id => $node->id } );
315                         for my $day ( 0 .. 6 ) {
316                                 my $open = "dow_${day}_open";
317                                 my $close = "dow_${day}_close";
318
319                                 print Tr(
320                                         th($dow{$day}),
321                                         td("<input type='text' name='$open' value=\"". $hoo->$open  ."\">"),
322                                         td("<input type='text' name='$close' value=\"". $hoo->$close ."\">"),
323                                 );
324                         }
325
326                         print Tr( "<td colspan='3'>".
327                                   "<input type='hidden' value='$node' name='id'>".
328                                   "<input type='submit' name='action' value='Update Hours'/></td>"
329                         );
330                         print   "</table></form>";
331                         
332                         
333                         #-------------------------------------------------------------------------
334                         # Address edit form
335                         #-------------------------------------------------------------------------
336
337                         print "<hr/><h2>Adresses for ".$node->name."</h2>";
338                         print   "<table cellspacing='20'><tr>";
339                         my %addrs = (   ill_address     => 'ILL Address',
340                                         holds_address   => 'Consortial Holds Address',
341                                         mailing_address => 'Mailing Address',
342                                         billing_address => 'Physical Address'
343                         );
344                         for my $a (qw/billing_address mailing_address holds_address ill_address/) {
345                                 my $addr = actor::org_address->retrieve( $node->$a ) if ($node->$a);
346
347                                 my %ah = (      street1         => $addr?$addr->street1:'',
348                                                 street2         => $addr?$addr->street2:'',
349                                                 city            => $addr?$addr->city:'',
350                                                 county          => $addr?$addr->county:'',
351                                                 state           => $addr?$addr->state:'',
352                                                 country         => $addr?$addr->country:'US',
353                                                 post_code       => $addr?$addr->post_code:'',
354                                                 org_unit        => $addr?$addr->org_unit:$node->id,
355                                                 id              => $addr?$addr->id:'',
356                                 );
357
358                                 #print '</tr><tr>' if ($a eq 'holds_address');
359                                 print <<"                               TABLE";
360
361 <td>
362 <form method='POST'>
363 <table class='table_class'>
364         <tr>
365                 <th colspan=2>$addrs{$a}</th>
366         </tr>
367         <tr>
368                 <th>SysID</th>
369                 <td><input type='text' name='aid' value='$ah{id}'></td>
370         </tr>
371         <tr>
372                 <th>*Street 1</th>
373                 <td><input type='text' name='street1' value='$ah{street1}'></td>
374         </tr>
375         <tr>
376                 <th>Street 2</th>
377                 <td><input type='text' name='street2' value='$ah{street2}'></td>
378         </tr>
379         <tr>
380                 <th>*City</th>
381                 <td><input type='text' name='city' value='$ah{city}'></td>
382         </tr>
383         <tr>
384                 <th>County</th>
385                 <td><input type='text' name='county' value='$ah{county}'></td>
386         </tr>
387         <tr>
388                 <th>*State</th>
389                 <td><input type='text' name='state' value='$ah{state}'></td>
390         </tr>
391         <tr>
392                 <th>*Country</th>
393                 <td><input type='text' name='country' value='$ah{country}'></td>
394         </tr>
395         <tr>
396                 <th>*ZIP</th>
397                 <td><input type='text' name='post_code' value='$ah{post_code}'></td>
398         </tr>
399 </table>
400 <input type='hidden' name='org_unit' value='$ah{org_unit}'>
401 <input type='hidden' name='addr_type' value='$a'>
402 <input type='hidden' name='id' value='$node'>
403 <input type='submit' name='action' value='Save Address'>
404 </form></td>
405
406                                 TABLE
407                         }
408
409                         print "<tr></table><hr><h2>New Child</h2>";
410         
411                         print   "<form method='POST'>".
412                                 "<table class='table_class'>\n";
413
414                         print Tr(
415                                 th($org_cols{name}),
416                                 td("<input type='text' name='name'>"),
417                         );
418                         print Tr(
419                                 th($org_cols{shortname}),
420                                 td("<input type='text' name='shortname'>"),
421                         );
422                         print Tr(
423                                 th($org_cols{ou_type}),
424                                 td("<select name='ou_type'>".do{
425                                                 my $out = '<option>-- Select One --</option>';
426                                                 for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
427                                                         $out .= "<option value='$type'>".$type->name.'</option>'
428                                                 }
429                                                 $out;
430                                         }."</select>"),
431                         );
432                         print Tr(
433                                 th($org_cols{email}),
434                                 td("<input type='text' name='email'>"),
435                         );
436                         print Tr(
437                                 th($org_cols{phone}),
438                                 td("<input type='text' name='phone'>"),
439                         );
440
441                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent_ou'>",
442                                   "<input type='submit' name='action' value='Add New'/></td>" );
443                         print   "</table></form><hr/>";
444                 }
445         }
446 }
447         
448 print "</div></body></html>";
449
450