]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/cgi-bin/lib-setup.cgi
add a note about hiding all hidden OUs
[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} .'<span style="color:red">*</span>'),
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><span style='color:red;'>*</span>".
287                                 "You must hide every OU you want hidden, not just an anscestor!</form>";
288
289                         #-------------------------------------------------------------------------
290                         # Hours of operation form
291                         #-------------------------------------------------------------------------
292
293                 
294                         
295                         my %dow = (
296                                 0 => 'Monday',
297                                 1 => 'Tuesday',
298                                 2 => 'Wednesday',
299                                 3 => 'Thursday',
300                                 4 => 'Friday',
301                                 5 => 'Saturday',
302                                 6 => 'Sunday',
303                         );
304                                 
305                         print "<hr/><h2>Hours of Operation for ".$node->name."</h2>".
306                                 "<form method='POST'>".
307                                 "<table class='table_class'><tr class='header_class'>\n";
308
309                         print Tr(
310                                 th('Day of Week'),
311                                 th('Open Time'),
312                                 th('Close Time'),
313                         );
314
315                         my $hoo = actor::org_unit::hours_of_operation->find_or_create( { id => $node->id } );
316                         for my $day ( 0 .. 6 ) {
317                                 my $open = "dow_${day}_open";
318                                 my $close = "dow_${day}_close";
319
320                                 print Tr(
321                                         th($dow{$day}),
322                                         td("<input type='text' name='$open' value=\"". $hoo->$open  ."\">"),
323                                         td("<input type='text' name='$close' value=\"". $hoo->$close ."\">"),
324                                 );
325                         }
326
327                         print Tr( "<td colspan='3'>".
328                                   "<input type='hidden' value='$node' name='id'>".
329                                   "<input type='submit' name='action' value='Update Hours'/></td>"
330                         );
331                         print   "</table></form>";
332                         
333                         
334                         #-------------------------------------------------------------------------
335                         # Address edit form
336                         #-------------------------------------------------------------------------
337
338                         print "<hr/><h2>Adresses for ".$node->name."</h2>";
339                         print   "<table cellspacing='20'><tr>";
340                         my %addrs = (   ill_address     => 'ILL Address',
341                                         holds_address   => 'Consortial Holds Address',
342                                         mailing_address => 'Mailing Address',
343                                         billing_address => 'Physical Address'
344                         );
345                         for my $a (qw/billing_address mailing_address holds_address ill_address/) {
346                                 my $addr = actor::org_address->retrieve( $node->$a ) if ($node->$a);
347
348                                 my %ah = (      street1         => $addr?$addr->street1:'',
349                                                 street2         => $addr?$addr->street2:'',
350                                                 city            => $addr?$addr->city:'',
351                                                 county          => $addr?$addr->county:'',
352                                                 state           => $addr?$addr->state:'',
353                                                 country         => $addr?$addr->country:'US',
354                                                 post_code       => $addr?$addr->post_code:'',
355                                                 org_unit        => $addr?$addr->org_unit:$node->id,
356                                                 id              => $addr?$addr->id:'',
357                                 );
358
359                                 #print '</tr><tr>' if ($a eq 'holds_address');
360                                 print <<"                               TABLE";
361
362 <td>
363 <form method='POST'>
364 <table class='table_class'>
365         <tr>
366                 <th colspan=2>$addrs{$a}</th>
367         </tr>
368         <tr>
369                 <th>SysID</th>
370                 <td><input type='text' name='aid' value='$ah{id}'></td>
371         </tr>
372         <tr>
373                 <th>*Street 1</th>
374                 <td><input type='text' name='street1' value='$ah{street1}'></td>
375         </tr>
376         <tr>
377                 <th>Street 2</th>
378                 <td><input type='text' name='street2' value='$ah{street2}'></td>
379         </tr>
380         <tr>
381                 <th>*City</th>
382                 <td><input type='text' name='city' value='$ah{city}'></td>
383         </tr>
384         <tr>
385                 <th>County</th>
386                 <td><input type='text' name='county' value='$ah{county}'></td>
387         </tr>
388         <tr>
389                 <th>*State</th>
390                 <td><input type='text' name='state' value='$ah{state}'></td>
391         </tr>
392         <tr>
393                 <th>*Country</th>
394                 <td><input type='text' name='country' value='$ah{country}'></td>
395         </tr>
396         <tr>
397                 <th>*ZIP</th>
398                 <td><input type='text' name='post_code' value='$ah{post_code}'></td>
399         </tr>
400 </table>
401 <input type='hidden' name='org_unit' value='$ah{org_unit}'>
402 <input type='hidden' name='addr_type' value='$a'>
403 <input type='hidden' name='id' value='$node'>
404 <input type='submit' name='action' value='Save Address'>
405 </form></td>
406
407                                 TABLE
408                         }
409
410                         print "<tr></table><hr><h2>New Child</h2>";
411         
412                         print   "<form method='POST'>".
413                                 "<table class='table_class'>\n";
414
415                         print Tr(
416                                 th($org_cols{name}),
417                                 td("<input type='text' name='name'>"),
418                         );
419                         print Tr(
420                                 th($org_cols{shortname}),
421                                 td("<input type='text' name='shortname'>"),
422                         );
423                         print Tr(
424                                 th($org_cols{ou_type}),
425                                 td("<select name='ou_type'>".do{
426                                                 my $out = '<option>-- Select One --</option>';
427                                                 for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
428                                                         $out .= "<option value='$type'>".$type->name.'</option>'
429                                                 }
430                                                 $out;
431                                         }."</select>"),
432                         );
433                         print Tr(
434                                 th($org_cols{email}),
435                                 td("<input type='text' name='email'>"),
436                         );
437                         print Tr(
438                                 th($org_cols{phone}),
439                                 td("<input type='text' name='phone'>"),
440                         );
441
442                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent_ou'>",
443                                   "<input type='submit' name='action' value='Add New'/></td>" );
444                         print   "</table></form><hr/>";
445                 }
446         }
447 }
448         
449 print "</div></body></html>";
450
451