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