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