]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/cgi-bin/lib-setup.cgi
forcing shortname to upper case
[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                                 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{parent_ou}),
249                                 td("<select name='parent_ou_$node'>".do{
250                                                 my $out = '<option>-- Select One --</option>';
251                                                 for my $org ( sort {$a->id <=> $b->id} actor::org_unit->retrieve_all) {
252                                                         $out .= "<option value='$org' ".do {
253                                                                 if ($node->parent_ou == $org->id) {
254                                                                         "selected";
255                                                                 }}.'>'.do{'&nbsp;&nbsp;'x$org->ou_type->depth}.$org->name.'</option>'
256                                                 }
257                                                 $out;
258                                         }."</select><input type='hidden' value='$node' name='id'>"),
259                         );
260
261                         print Tr( "<td colspan='2'><input type='submit' name='action' value='Update'/></td>" );
262                         print   "</table></form>";
263
264                         #-------------------------------------------------------------------------
265                         # Hours of operation form
266                         #-------------------------------------------------------------------------
267
268                 
269                         
270                         my %dow = (
271                                 0 => 'Monday',
272                                 1 => 'Tuesday',
273                                 2 => 'Wednesday',
274                                 3 => 'Thursday',
275                                 4 => 'Friday',
276                                 5 => 'Saturday',
277                                 6 => 'Sunday',
278                         );
279                                 
280                         print "<hr/><h2>Hours of Operation for ".$node->name."</h2>".
281                                 "<form method='POST'>".
282                                 "<table class='table_class'><tr class='header_class'>\n";
283
284                         print Tr(
285                                 th('Day of Week'),
286                                 th('Open Time'),
287                                 th('Close Time'),
288                         );
289
290                         my $hoo = actor::org_unit::hours_of_operation->find_or_create( { id => $node->id } );
291                         for my $day ( 0 .. 6 ) {
292                                 my $open = "dow_${day}_open";
293                                 my $close = "dow_${day}_close";
294
295                                 print Tr(
296                                         th($dow{$day}),
297                                         td("<input type='text' name='$open' value=\"". $hoo->$open  ."\">"),
298                                         td("<input type='text' name='$close' value=\"". $hoo->$close ."\">"),
299                                 );
300                         }
301
302                         print Tr( "<td colspan='3'>".
303                                   "<input type='hidden' value='$node' name='id'>".
304                                   "<input type='submit' name='action' value='Update Hours'/></td>"
305                         );
306                         print   "</table></form>";
307                         
308                         
309                         #-------------------------------------------------------------------------
310                         # Address edit form
311                         #-------------------------------------------------------------------------
312
313                         print "<hr/><h2>Adresses for ".$node->name."</h2>";
314                         print   "<table cellspacing='20'><tr>";
315                         my %addrs = (   ill_address     => 'ILL Address',
316                                         holds_address   => 'Consortial Holds Address',
317                                         mailing_address => 'Mailing Address',
318                                         billing_address => 'Physical Address'
319                         );
320                         for my $a (qw/billing_address mailing_address holds_address ill_address/) {
321                                 my $addr = actor::org_address->retrieve( $node->$a ) if ($node->$a);
322
323                                 my %ah = (      street1         => $addr?$addr->street1:'',
324                                                 street2         => $addr?$addr->street2:'',
325                                                 city            => $addr?$addr->city:'',
326                                                 county          => $addr?$addr->county:'',
327                                                 state           => $addr?$addr->state:'',
328                                                 country         => $addr?$addr->country:'US',
329                                                 post_code       => $addr?$addr->post_code:'',
330                                                 org_unit        => $addr?$addr->org_unit:$node->id,
331                                                 id              => $addr?$addr->id:'',
332                                 );
333
334                                 #print '</tr><tr>' if ($a eq 'holds_address');
335                                 print <<"                               TABLE";
336
337 <td>
338 <form method='POST'>
339 <table class='table_class'>
340         <tr>
341                 <th colspan=2>$addrs{$a}</th>
342         </tr>
343         <tr>
344                 <th>SysID</th>
345                 <td><input type='text' name='aid' value='$ah{id}'></td>
346         </tr>
347         <tr>
348                 <th>*Street 1</th>
349                 <td><input type='text' name='street1' value='$ah{street1}'></td>
350         </tr>
351         <tr>
352                 <th>Street 2</th>
353                 <td><input type='text' name='street2' value='$ah{street2}'></td>
354         </tr>
355         <tr>
356                 <th>*City</th>
357                 <td><input type='text' name='city' value='$ah{city}'></td>
358         </tr>
359         <tr>
360                 <th>County</th>
361                 <td><input type='text' name='county' value='$ah{county}'></td>
362         </tr>
363         <tr>
364                 <th>*State</th>
365                 <td><input type='text' name='state' value='$ah{state}'></td>
366         </tr>
367         <tr>
368                 <th>*Country</th>
369                 <td><input type='text' name='country' value='$ah{country}'></td>
370         </tr>
371         <tr>
372                 <th>*ZIP</th>
373                 <td><input type='text' name='post_code' value='$ah{post_code}'></td>
374         </tr>
375 </table>
376 <input type='hidden' name='org_unit' value='$ah{org_unit}'>
377 <input type='hidden' name='addr_type' value='$a'>
378 <input type='hidden' name='id' value='$node'>
379 <input type='submit' name='action' value='Save Address'>
380 </form></td>
381
382                                 TABLE
383                         }
384
385                         print "<tr></table><hr><h2>New Child</h2>";
386         
387                         print   "<form method='POST'>".
388                                 "<table class='table_class'>\n";
389
390                         print Tr(
391                                 th($org_cols{name}),
392                                 td("<input type='text' name='name'>"),
393                         );
394                         print Tr(
395                                 th($org_cols{shortname}),
396                                 td("<input type='text' name='shortname'>"),
397                         );
398                         print Tr(
399                                 th($org_cols{ou_type}),
400                                 td("<select name='ou_type'>".do{
401                                                 my $out = '<option>-- Select One --</option>';
402                                                 for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
403                                                         $out .= "<option value='$type'>".$type->name.'</option>'
404                                                 }
405                                                 $out;
406                                         }."</select>"),
407                         );
408                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent_ou'>",
409                                   "<input type='submit' name='action' value='Add New'/></td>" );
410                         print   "</table></form><hr/>";
411                 }
412         }
413 }
414         
415 print "</div></body></html>";
416
417