0c9d9b4d46ac0bbc36ccdaac41a18e0f10c9e28e
[working/Evergreen.git] / Open-ILS / src / cgi-bin / lib-setup.cgi
1 #!/usr/bin/perl
2 use strict;
3
4 use OpenILS::Application::Storage;
5 use OpenILS::Application::Storage::CDBI;
6
7 # I need to abstract the driver loading away...
8 use OpenILS::Application::Storage::Driver::Pg;
9
10 use CGI qw/:standard start_*/;
11 our %config;
12 do 'setup.pl';
13
14 OpenILS::Application::Storage::CDBI->connection($config{dsn},$config{usr},$config{pw});
15 OpenILS::Application::Storage::CDBI->db_Main->{ AutoCommit } = 1;
16
17 my $cgi = new CGI;
18
19 #-------------------------------------------------------------------------------
20 # setup part
21 #-------------------------------------------------------------------------------
22
23 my %org_cols = ( qw/id SysID name Name parent_ou Parent ou_type OrgUnitType shortname ShortName/ );
24
25 my @col_display_order = ( qw/id name shortname ou_type parent_ou/ );
26
27 if (my $action = $cgi->param('action')) {
28         if ( $action eq 'Update' ) {
29                 for my $id ( ($cgi->param('id')) ) {
30                         my $u = actor::org_unit->retrieve($id);
31                         for my $col ( keys %org_cols ) {
32                                 next if ($cgi->param($col."_$id") =~ /Select One/o);
33                                 $u->$col( $cgi->param($col."_$id") );
34                         }
35                         $u->update;
36                 }
37         } elsif ( $action eq 'Add New' ) {
38                 actor::org_unit->create( { map { defined($cgi->param($_)) ? ($_ => $cgi->param($_)) : () } keys %org_cols } );
39         } elsif ( $action eq 'Save Address' ) {
40                 my $org = actor::org_unit->retrieve($cgi->param('id'));
41
42                 my $addr = {};
43
44                 $$addr{org_unit} = $cgi->param('org_unit') || $org->id;
45                 $$addr{street1} = $cgi->param('street1');
46                 $$addr{street2} = $cgi->param('street2');
47                 $$addr{city} = $cgi->param('city');
48                 $$addr{county} = $cgi->param('county');
49                 $$addr{state} = $cgi->param('state');
50                 $$addr{country} = $cgi->param('country');
51                 $$addr{post_code} = $cgi->param('post_code');
52
53                 my $a_type = $cgi->param('addr_type');
54
55
56                 my $a = actor::org_address->retrieve($cgi->param('aid'));
57
58                 if ($a) {
59                         for (keys %$addr) {
60                                 next unless $$addr{$_};
61                                 $a->$_($$addr{$_});
62                         }
63                         $a->update;
64                 } else {
65                         $a = actor::org_address->create( {map {defined($$addr{$_}) ? ($_ => $$addr{$_}) : ()} keys %$addr} );
66                 }
67
68                 $org->$a_type($a->id);
69                 $org->update;
70         }
71 }
72
73 #-------------------------------------------------------------------------------
74 # HTML part
75 #-------------------------------------------------------------------------------
76
77 print <<HEADER;
78 Content-type: text/html
79
80 <html>
81
82 <head>
83         <style>
84                 table.table_class {
85                         border: dashed lightgrey 1px;
86                         background-color: #EEE;
87                         border-collapse: collapse;
88                 }
89
90                 deactivated {
91                         color: lightgrey;
92                 }
93
94                 tr.new_row_class {
95                         background: grey;
96                 }
97
98                 tr.row_class td {
99                         border: solid lightgrey 1px;
100                 }
101                 
102                 tr.header_class th {
103                         background-color: lightblue;
104                         border: solid blue 1px;
105                         padding: 2px;
106                 }
107
108         </style>
109         <script language='javascript' src='/js/widgets/xtree.js'></script>
110 </head>
111
112 <body style='padding: 25px;'>
113
114 <a href="$config{index}">Home</a>
115
116 <h1>Library Hierarchy Setup</h1>
117 <hr/>
118 HEADER
119
120 my $uri = $cgi->url(-relative=>1);
121
122 my $top;
123 for my $lib ( actor::org_unit->search( {parent_ou=>undef} ) ) {
124         my $name = $lib->name;
125         $name =~ s/'/\\'/og;
126         print <<"       HEADER";
127 <div style="float: left;">
128         <script language='javascript'>
129
130                 function getById (id) { return document.getElementById(id); }
131                 function createAppElement (el) { return document.createElement(el); }
132                 function createAppTextNode (txt) { return document.createTextNode(txt); }
133         
134                 var node_$lib = new WebFXTree('$name','$uri?action=child&id=$lib');
135         HEADER
136         $top = $lib->id;
137         last;
138 }
139
140 for my $lib ( actor::org_unit->search_like( {parent_ou => '%'}, {order_by => 'id'} ) ) {
141         my $name = $lib->name;
142         $name =~ s/'/\\'/og;
143         my $parent = $lib->parent_ou;
144         print <<"       JS"
145                 var node_$lib = new WebFXTreeItem('$name','$uri?action=child&id=$lib');
146         JS
147 }for my $lib ( sort {$a->name cmp $b->name} actor::org_unit->retrieve_all ) {
148         my $parent = $lib->parent_ou;
149         next unless $parent;
150         print <<"       JS"
151                 node_$parent.add(node_$lib);
152         JS
153 }
154
155 print <<HEADER;
156                 document.write(node_$top);
157         </script>
158 </div>
159 <div>
160
161 HEADER
162
163 #-------------------------------------------------------------------------------
164 # Logic part
165 #-------------------------------------------------------------------------------
166
167 if (my $action = $cgi->param('action')) {
168         if ( $action eq 'child' ) {
169                 my $id = $cgi->param('id');
170                 if ($id) {
171                         my $node = actor::org_unit->retrieve($id);
172                         #-----------------------------------------------------------------------
173                         # child form
174                         #-----------------------------------------------------------------------
175
176                         print "<h2>Edit ".$node->name."</h2>";
177                         print   "<form method='POST'>".
178                                 "<table class='table_class'><tr class='header_class'>\n";
179         
180                         print Tr(
181                                 th($org_cols{id}),
182                                 td( $node->id() ),
183                         );
184                         print Tr(
185                                 th($org_cols{name}),
186                                 td("<input type='text' name='name_$node' value=\"". $node->name() ."\">"),
187                         );
188                         print Tr(
189                                 th($org_cols{shortname}),
190                                 td("<input type='text' name='shortname_$node' value='". $node->shortname() ."'>"),
191                         );
192                         print Tr(
193                                 th($org_cols{ou_type}),
194                                 td("<select name='ou_type_$node'>".do{
195                                                         my $out = '<option>-- Select One --</option>';
196                                                         for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
197                                                                 $out .= "<option value='$type' ".do {
198                                                                         if ($node->ou_type == $type->id) {
199                                                                                 "selected";
200                                                                         }}.'>'.$type->name.'</option>'
201                                                         }
202                                                         $out;
203                                                 }."</select>"),
204                         );
205                         print Tr(
206                                 th($org_cols{parent_ou}),
207                                 td("<select name='parent_ou_$node'>".do{
208                                                 my $out = '<option>-- Select One --</option>';
209                                                 for my $org ( sort {$a->id <=> $b->id} actor::org_unit->retrieve_all) {
210                                                         $out .= "<option value='$org' ".do {
211                                                                 if ($node->parent_ou == $org->id) {
212                                                                         "selected";
213                                                                 }}.'>'.do{'&nbsp;&nbsp;'x$org->ou_type->depth}.$org->name.'</option>'
214                                                 }
215                                                 $out;
216                                         }."</select><input type='hidden' value='$node' name='id'>"),
217                         );
218
219                         print Tr( "<td colspan='2'><input type='submit' name='action' value='Update'/></td>" );
220
221                         print   "</table></form><hr/><table cellspacing='20'><tr>";
222
223
224                         #-------------------------------------------------------------------------
225                         # Address edit form
226                         #-------------------------------------------------------------------------
227
228                         my %addrs = (   ill_address     => 'ILL Address',
229                                         holds_address   => 'Consortial Holds Address',
230                                         mailing_address => 'Mailing Address',
231                                         billing_address => 'Physical Address'
232                         );
233                         for my $a (qw/billing_address mailing_address holds_address ill_address/) {
234                                 my $addr = actor::org_address->retrieve( $node->$a ) if ($node->$a);
235
236                                 my %ah = (      street1         => $addr?$addr->street1:'',
237                                                 street2         => $addr?$addr->street2:'',
238                                                 city            => $addr?$addr->city:'',
239                                                 county          => $addr?$addr->county:'',
240                                                 state           => $addr?$addr->state:'',
241                                                 country         => $addr?$addr->country:'US',
242                                                 post_code       => $addr?$addr->post_code:'',
243                                                 org_unit        => $addr?$addr->org_unit:$node->id,
244                                                 id              => $addr?$addr->id:'',
245                                 );
246
247                                 print '</tr><tr>' if ($a eq 'holds_address');
248                                 print <<"                               TABLE";
249
250 <td>
251 <form method='POST'>
252 <table class='table_class'>
253         <tr>
254                 <th colspan=2>$addrs{$a}</th>
255         </tr>
256         <tr>
257                 <th>SysID</th>
258                 <td><input type='text' name='aid' value='$ah{id}'></td>
259         </tr>
260         <tr>
261                 <th>*Street 1</th>
262                 <td><input type='text' name='street1' value='$ah{street1}'></td>
263         </tr>
264         <tr>
265                 <th>Street 2</th>
266                 <td><input type='text' name='street2' value='$ah{street2}'></td>
267         </tr>
268         <tr>
269                 <th>*City</th>
270                 <td><input type='text' name='city' value='$ah{city}'></td>
271         </tr>
272         <tr>
273                 <th>County</th>
274                 <td><input type='text' name='county' value='$ah{county}'></td>
275         </tr>
276         <tr>
277                 <th>*State</th>
278                 <td><input type='text' name='state' value='$ah{state}'></td>
279         </tr>
280         <tr>
281                 <th>*Country</th>
282                 <td><input type='text' name='country' value='$ah{country}'></td>
283         </tr>
284         <tr>
285                 <th>*ZIP</th>
286                 <td><input type='text' name='post_code' value='$ah{post_code}'></td>
287         </tr>
288 </table>
289 <input type='hidden' name='org_unit' value='$ah{org_unit}'>
290 <input type='hidden' name='addr_type' value='$a'>
291 <input type='hidden' name='id' value='$node'>
292 <input type='submit' name='action' value='Save Address'>
293 </form></td>
294
295                                 TABLE
296                         }
297
298                         print "<tr></table><hr><h2>New Child</h2>";
299         
300                         print   "<form method='POST'>".
301                                 "<table class='table_class'>\n";
302
303                         print Tr(
304                                 th($org_cols{name}),
305                                 td("<input type='text' name='name'>"),
306                         );
307                         print Tr(
308                                 th($org_cols{shortname}),
309                                 td("<input type='text' name='shortname'>"),
310                         );
311                         print Tr(
312                                 th($org_cols{ou_type}),
313                                 td("<select name='ou_type'>".do{
314                                                 my $out = '<option>-- Select One --</option>';
315                                                 for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
316                                                         $out .= "<option value='$type'>".$type->name.'</option>'
317                                                 }
318                                                 $out;
319                                         }."</select>"),
320                         );
321                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent_ou'>",
322                                   "<input type='submit' name='action' value='Add New'/></td>" );
323                         print   "</table></form><hr/>";
324                 }
325         }
326 }
327         
328 print "</div></body></html>";
329
330