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