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