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