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