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